Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item search filters - Multi type/collection #781

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions admin/themes/default/items/search-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@
<div class="five columns omega inputs">
<?php
echo $this->formSelect(
'collection',
'collection[]',
@$_REQUEST['collection'],
array('id' => 'collection-search'),
array('id' => 'collection-search', 'multiple' => true),
get_table_options('Collection', null, array('include_no_collection' => true))
);
?>
Expand All @@ -148,10 +148,10 @@
<div class="five columns omega inputs">
<?php
echo $this->formSelect(
'type',
'type[]',
@$_REQUEST['type'],
array('id' => 'item-type-search'),
get_table_options('ItemType')
array('id' => 'item-type-search', 'multiple' => true),
get_table_options('ItemType', null, array('include_no_item_type' => true))
);
?>
</div>
Expand Down
10 changes: 6 additions & 4 deletions application/models/Table/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ public function filterByCollection($select, $collections)
if (is_numeric($collection)) {
return (int) $collection;
}
return;
return '';
}, $collections);

$hasEmpty = in_array(null, $collectionIds);
// strict check to skip placeholder (empty string)
$hasEmpty = in_array(null, $collectionIds, true);
$collectionIds = array_filter($collectionIds);
if (!empty($collectionIds)) {
$select->joinLeft(
Expand Down Expand Up @@ -245,10 +246,11 @@ public function filterByItemType($select, $types)
if (is_string($type)) {
return $type;
}
return;
return '';
}, $types);

$hasEmpty = in_array(null, $typeIdsOrNames);
// strict check to skip placeholder (empty string)
$hasEmpty = in_array(null, $typeIdsOrNames, true);
$typeIdsOrNames = array_filter($typeIdsOrNames);
if ($typeIdsOrNames) {
$select->joinLeft(array(
Expand Down
10 changes: 10 additions & 0 deletions application/models/Table/ItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ public function findByName($itemTypeName)
$select->where($this->_name . '.name = ?', $itemTypeName);
return $this->fetchObject($select);
}

public function findPairsForSelectForm(array $options = array())
{
$pairs = parent::findPairsForSelectForm($options);

if (isset($options['include_no_item_type']) && $options['include_no_item_type']) {
$pairs = array(__('No Item Type')) + $pairs;
}
return $pairs;
}
}
37 changes: 27 additions & 10 deletions application/views/helpers/ItemSearchFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,39 @@ public function itemSearchFilters(array $params = null)
switch ($key) {
case 'type':
$filter = 'Item Type';
$itemType = $db->getTable('ItemType')->find($value);
if ($itemType) {
$displayValue = $itemType->name;
$value = (array) $value;
$displayValues = array();
$noIndex = array_search('0', $value, true);
if ($noIndex !== false) {
$displayValues[] = __('No Item Type');
unset($value[$noIndex]);
}
if (!empty($value)) {
$itemTypes = $db->getTable('ItemType')->findBy(array('id' => $value));
if ($itemTypes) {
$displayValues[] = implode(', ', pluck('name', $itemTypes));
}
}
$displayValue = implode(', ', $displayValues);
break;

case 'collection':
if ($value === '0') {
$displayValue = __('No Collection');
break;
$value = (array) $value;
$displayValues = array();
$noIndex = array_search('0', $value, true);
if ($noIndex !== false) {
$displayValues[] = __('No Collection');
unset($value[$noIndex]);
}

$collection = $db->getTable('Collection')->find($value);
if ($collection) {
$displayValue = metadata($collection, 'display_title', array('no_escape' => true));
if (!empty($value)) {
$collections = $db->getTable('Collection')->findBy(array('id' => $value));
if ($collections) {
$displayValues[] = implode(', ', array_map(function($collection) {
return metadata($collection, 'display_title', array('no_escape' => true));
}, $collections));
}
}
$displayValue = implode(', ', $displayValues);
break;

case 'user':
Expand Down
10 changes: 5 additions & 5 deletions application/views/scripts/items/search-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
<div class="inputs">
<?php
echo $this->formSelect(
'collection',
'collection[]',
@$_REQUEST['collection'],
array('id' => 'collection-search'),
array('id' => 'collection-search', 'multiple' => true),
get_table_options('Collection', null, array('include_no_collection' => true))
);
?>
Expand All @@ -135,10 +135,10 @@
<div class="inputs">
<?php
echo $this->formSelect(
'type',
'type[]',
@$_REQUEST['type'],
array('id' => 'item-type-search'),
get_table_options('ItemType')
array('id' => 'item-type-search', 'multiple' => true),
get_table_options('ItemType', null, array('include_no_item_type' => true))
);
?>
</div>
Expand Down