Skip to content

Commit

Permalink
Switch advanced search to simple "block" system
Browse files Browse the repository at this point in the history
The current filters are each separated into their own partial and the
view simply loops over the partials. The existing "after"-style event is
converted to a filter of the array of partials.

(#852)
  • Loading branch information
zerocrates committed Sep 26, 2017
1 parent c5f7448 commit cd56af1
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 121 deletions.
7 changes: 5 additions & 2 deletions application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ function (ZendEvent $event) {
'view.advanced_search',
function (ZendEvent $event) {
if ('item' === $event->getParam('resourceType')) {
echo $event->getTarget()->partial('common/item-sets-advanced-search');
$partials = $event->getParam('partials');
$partials[] = 'common/advanced-search/item-sets';
$event->setParam('partials', $partials);
}
}
},
2
);
}

Expand Down
136 changes: 17 additions & 119 deletions application/view/common/advanced-search.phtml
Original file line number Diff line number Diff line change
@@ -1,126 +1,24 @@
<?php
$this->headLink()->prependStylesheet($this->assetUrl('css/advanced-search.css', 'Omeka'));

// Prepare the resource class query.
if (isset($query['resource_class_id'])) {
$resourceClassId = $query['resource_class_id'] ;
} else {
$resourceClassId = null;
}

// Prepare the site query.
if (isset($query['site_id'])) {
$siteId = $query['site_id'] ;
} else {
$siteId = null;
}

// Prepare the property queries.
$properties = isset($query['property']) ? $query['property'] : [];
$properties = array_filter($properties, function ($value) {
return isset($value['text']) ? '' !== trim($value['text']) : true;
});
if (!$properties) {
$properties[] = [];
}

if (isset($query['search'])) {
unset($properties[0]['joiner']);
array_unshift($properties, [
'property' => '',
'type' => 'in',
'text' => $query['search']
]);
}
$partials = [
'common/advanced-search/resource-class',
'common/advanced-search/properties',
'common/advanced-search/site-pool',
];

// Convenience functions to minimize inline code.
$queryOption = function($value, array $search, $key, $text) {
$selected = null;
if (isset($search[$key]) && $value === $search[$key]) {
$selected = ' selected';
}
return sprintf('<option value="%s"%s>%s</option>', $value, $selected, $text);
};
$queryText = function(array $search, $index) {
$text = isset($search['text']) ? $search['text'] : null;
return sprintf('<input type="text" class="query-text" name="%s" value="%s">',
$this->escapeHtml("property[$index][text]"),
$this->escapeHtml($text));
}
?>

<div class="field">
<div class="field-meta">
<label for="resource_class_id"><?php echo $this->translate('Search by class'); ?></label>
</div>
<div class="inputs">
<?php echo $this->resourceClassSelect([
'name' => 'resource_class_id',
'attributes' => ['id' => 'resource_class_id', 'value' => $resourceClassId]
]); ?>
</div>
</div>

<div id="property-queries" class="field removable multi-value" role="group" aria-labelledby="by-value-label">
<div class="field-meta">
<span id="by-value-label" class="label"><?php echo $this->translate('Search by value'); ?></span>
</div>
<div class="inputs">
<?php
$index = 0;
foreach ($properties as $property):
$stem = "property[$index]";
?>
<div class="value">
<select class="joiner" name="<?php echo $this->escapeHtml($stem . '[joiner]'); ?>">
<?php echo $queryOption('and', $property, 'joiner', $this->translate('AND')); ?>
<?php echo $queryOption('or', $property, 'joiner', $this->translate('OR')); ?>
</select>
<?php echo $this->propertySelect([
'name' => $stem . '[property]',
'attributes' => [
'class' => 'query-property',
'value' => isset($property['property']) ? $property['property'] : null,
],
'options' => [
'empty_option' => '[Any Property]', // @translate
]
]); ?>
<select class="query-type" name="<?php echo $this->escapeHtml($stem . '[type]'); ?>">
<?php echo $queryOption('eq', $property, 'type', $this->translate('is exactly')); ?>
<?php echo $queryOption('neq', $property, 'type', $this->translate('is not exactly')); ?>
<?php echo $queryOption('in', $property, 'type', $this->translate('contains')); ?>
<?php echo $queryOption('nin', $property, 'type', $this->translate('does not contain')); ?>
<?php echo $queryOption('ex', $property, 'type', $this->translate('has any value')); ?>
<?php echo $queryOption('nex', $property, 'type', $this->translate('has no values')); ?>
</select>
<?php echo $queryText($property, $index); ?>
<button type="button" class="o-icon-delete remove-value button"><?php echo $this->translate('Remove value'); ?></button>
</div>
<?php
$index++;
endforeach;
?>
<a href="#" class="add-value button"><?php echo $this->translate('Add new value'); ?></a>
</div>
</div>

<div class="field">
<div class="field-meta">
<label for="site_id"><?php echo $this->translate('In site pool'); ?></label>
</div>
<div class="inputs">
<?php echo $this->siteSelect([
'name' => 'site_id',
'attributes' => ['id' => 'site_id', 'value' => $siteId]
]); ?>
</div>
</div>

<?php $this->trigger(
$filterResults = $this->trigger(
'view.advanced_search',
[
'query' => isset($query) ? $query : null,
'resourceType' => isset($resourceType) ? $resourceType : null
]
); ?>
'resourceType' => isset($resourceType) ? $resourceType : null,
'partials' => $partials,
],
true
);
$partials = $filterResults['partials'];

foreach ($partials as $partial) {
echo $this->partial($partial, ['query' => $query]);
}
?>
77 changes: 77 additions & 0 deletions application/view/common/advanced-search/properties.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
// Prepare the property queries.
$properties = isset($query['property']) ? $query['property'] : [];
$properties = array_filter($properties, function ($value) {
return isset($value['text']) ? '' !== trim($value['text']) : true;
});
if (!$properties) {
$properties[] = [];
}

if (isset($query['search'])) {
unset($properties[0]['joiner']);
array_unshift($properties, [
'property' => '',
'type' => 'in',
'text' => $query['search']
]);
}

$queryOption = function($value, array $search, $key, $text) {
$selected = null;
if (isset($search[$key]) && $value === $search[$key]) {
$selected = ' selected';
}
return sprintf('<option value="%s"%s>%s</option>', $value, $selected, $text);
};
$queryText = function(array $search, $index) {
$text = isset($search['text']) ? $search['text'] : null;
return sprintf('<input type="text" class="query-text" name="%s" value="%s">',
$this->escapeHtml("property[$index][text]"),
$this->escapeHtml($text));
}
?>

<div id="property-queries" class="field removable multi-value" role="group" aria-labelledby="by-value-label">
<div class="field-meta">
<span id="by-value-label" class="label"><?php echo $this->translate('Search by value'); ?></span>
</div>
<div class="inputs">
<?php
$index = 0;
foreach ($properties as $property):
$stem = "property[$index]";
?>
<div class="value">
<select class="joiner" name="<?php echo $this->escapeHtml($stem . '[joiner]'); ?>">
<?php echo $queryOption('and', $property, 'joiner', $this->translate('AND')); ?>
<?php echo $queryOption('or', $property, 'joiner', $this->translate('OR')); ?>
</select>
<?php echo $this->propertySelect([
'name' => $stem . '[property]',
'attributes' => [
'class' => 'query-property',
'value' => isset($property['property']) ? $property['property'] : null,
],
'options' => [
'empty_option' => '[Any Property]', // @translate
]
]); ?>
<select class="query-type" name="<?php echo $this->escapeHtml($stem . '[type]'); ?>">
<?php echo $queryOption('eq', $property, 'type', $this->translate('is exactly')); ?>
<?php echo $queryOption('neq', $property, 'type', $this->translate('is not exactly')); ?>
<?php echo $queryOption('in', $property, 'type', $this->translate('contains')); ?>
<?php echo $queryOption('nin', $property, 'type', $this->translate('does not contain')); ?>
<?php echo $queryOption('ex', $property, 'type', $this->translate('has any value')); ?>
<?php echo $queryOption('nex', $property, 'type', $this->translate('has no values')); ?>
</select>
<?php echo $queryText($property, $index); ?>
<button type="button" class="o-icon-delete remove-value button"><?php echo $this->translate('Remove value'); ?></button>
</div>
<?php
$index++;
endforeach;
?>
<a href="#" class="add-value button"><?php echo $this->translate('Add new value'); ?></a>
</div>
</div>
20 changes: 20 additions & 0 deletions application/view/common/advanced-search/resource-class.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
// Prepare the resource class query.
if (isset($query['resource_class_id'])) {
$resourceClassId = $query['resource_class_id'] ;
} else {
$resourceClassId = null;
}
?>

<div class="field">
<div class="field-meta">
<label for="resource_class_id"><?php echo $this->translate('Search by class'); ?></label>
</div>
<div class="inputs">
<?php echo $this->resourceClassSelect([
'name' => 'resource_class_id',
'attributes' => ['id' => 'resource_class_id', 'value' => $resourceClassId]
]); ?>
</div>
</div>
19 changes: 19 additions & 0 deletions application/view/common/advanced-search/site-pool.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
// Prepare the site query.
if (isset($query['site_id'])) {
$siteId = $query['site_id'] ;
} else {
$siteId = null;
}
?>
<div class="field">
<div class="field-meta">
<label for="site_id"><?php echo $this->translate('In site pool'); ?></label>
</div>
<div class="inputs">
<?php echo $this->siteSelect([
'name' => 'site_id',
'attributes' => ['id' => 'site_id', 'value' => $siteId]
]); ?>
</div>
</div>

0 comments on commit cd56af1

Please sign in to comment.