Skip to content

Commit

Permalink
Added an option to list only used properties and resource classes in …
Browse files Browse the repository at this point in the history
…advanced search form.
  • Loading branch information
Daniel-KM committed Apr 19, 2020
1 parent 6f65c25 commit 8dcdeaf
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 34 deletions.
30 changes: 21 additions & 9 deletions application/src/Api/Adapter/PropertyAdapter.php
Expand Up @@ -71,13 +71,14 @@ public function hydrate(Request $request, EntityInterface $entity,

public function buildQuery(QueryBuilder $qb, array $query)
{
$expr = $qb->expr();
if (isset($query['owner_id']) && is_numeric($query['owner_id'])) {
$userAlias = $this->createAlias();
$qb->innerJoin(
'omeka_root.owner',
$userAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$userAlias.id",
$this->createNamedParameter($qb, $query['owner_id']))
);
Expand All @@ -88,7 +89,7 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.id",
$this->createNamedParameter($qb, $query['vocabulary_id']))
);
Expand All @@ -99,7 +100,7 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.namespaceUri",
$this->createNamedParameter($qb, $query['vocabulary_namespace_uri']))
);
Expand All @@ -110,15 +111,15 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.prefix",
$this->createNamedParameter($qb, $query['vocabulary_prefix']))
);
}

if (isset($query['local_name'])) {
$qb->andWhere($qb->expr()->eq(
"omeka_root.localName",
$qb->andWhere($expr->eq(
'omeka_root.localName',
$this->createNamedParameter($qb, $query['local_name']))
);
}
Expand All @@ -129,15 +130,26 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.prefix",
$this->createNamedParameter($qb, $prefix))
);
$qb->andWhere($qb->expr()->eq(
"omeka_root.localName",
$qb->andWhere($expr->eq(
'omeka_root.localName',
$this->createNamedParameter($qb, $localName))
);
}

// This key is used by PropertySelect.
if (!empty($query['used'])) {
$valuesAlias = $this->createAlias();
$qb->innerJoin(
'omeka_root.values',
$valuesAlias,
\Doctrine\ORM\Query\Expr\Join::WITH,
$expr->eq("$valuesAlias.property", 'omeka_root.id')
);
}
}

public function validateEntity(EntityInterface $entity, ErrorStore $errorStore)
Expand Down
30 changes: 21 additions & 9 deletions application/src/Api/Adapter/ResourceClassAdapter.php
Expand Up @@ -62,13 +62,14 @@ public function hydrate(Request $request, EntityInterface $entity,

public function buildQuery(QueryBuilder $qb, array $query)
{
$expr = $qb->expr();
if (isset($query['owner_id']) && is_numeric($query['owner_id'])) {
$userAlias = $this->createAlias();
$qb->innerJoin(
'omeka_root.owner',
$userAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$userAlias.id",
$this->createNamedParameter($qb, $query['owner_id']))
);
Expand All @@ -79,7 +80,7 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.id",
$this->createNamedParameter($qb, $query['vocabulary_id']))
);
Expand All @@ -90,7 +91,7 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.namespaceUri",
$this->createNamedParameter($qb, $query['vocabulary_namespace_uri']))
);
Expand All @@ -101,14 +102,14 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.prefix",
$this->createNamedParameter($qb, $query['vocabulary_prefix']))
);
}
if (isset($query['local_name'])) {
$qb->andWhere($qb->expr()->eq(
"omeka_root.localName",
$qb->andWhere($expr->eq(
'omeka_root.localName',
$this->createNamedParameter($qb, $query['local_name']))
);
}
Expand All @@ -119,15 +120,26 @@ public function buildQuery(QueryBuilder $qb, array $query)
'omeka_root.vocabulary',
$vocabularyAlias
);
$qb->andWhere($qb->expr()->eq(
$qb->andWhere($expr->eq(
"$vocabularyAlias.prefix",
$this->createNamedParameter($qb, $prefix))
);
$qb->andWhere($qb->expr()->eq(
"omeka_root.localName",
$qb->andWhere($expr->eq(
'omeka_root.localName',
$this->createNamedParameter($qb, $localName))
);
}

// This key is used by ResourceClassSelect.
if (!empty($query['used'])) {
$resourceAlias = $this->createAlias();
$qb->innerJoin(
'omeka_root.resources',
$resourceAlias,
\Doctrine\ORM\Query\Expr\Join::WITH,
$expr->eq("$resourceAlias.resourceClass", 'omeka_root.id')
);
}
}

public function validateEntity(EntityInterface $entity, ErrorStore $errorStore)
Expand Down
Expand Up @@ -57,6 +57,10 @@ public function getValueOptions()
if (!isset($query['sort_by'])) {
$query['sort_by'] = 'label';
}
if ($this->getOption('used_terms')) {
$query['used'] = true;
}

// Allow handlers to filter the query.
$args = $events->prepareArgs(['query' => $query]);
$events->trigger('form.vocab_member_select.query', $this, $args);
Expand Down
11 changes: 11 additions & 0 deletions application/src/Form/SiteSettingsForm.php
Expand Up @@ -207,6 +207,17 @@ public function init()
'required' => false,
],
]);
$searchFieldset->add([
'type' => \Zend\Form\Element\Checkbox::class,
'name' => 'search_used_terms',
'options' => [
'label' => 'List only used properties and resources classes', // @translate
'info' => 'Restrict the list of properties and resources classes to the used ones in advanced search form (for properties, when option "templates" is not used).', // @translate
],
'attributes' => [
'value' => $settings->get('search_used_terms', false),
],
]);
$searchFieldset->add([
'type' => 'Omeka\Form\Element\ResourceTemplateSelect',
'name' => 'search_apply_templates',
Expand Down
38 changes: 24 additions & 14 deletions application/view/common/advanced-search/properties.phtml
@@ -1,5 +1,14 @@
<?php
$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$hyperlink = $this->plugin('hyperlink');
$propertySelect = $this->plugin('propertySelect');
$siteSetting = $this->plugin('siteSetting');

$isSiteRequest = $this->status()->isSiteRequest();
$applyTemplates = $isSiteRequest ? $siteSetting('search_apply_templates') : false;
$usedTerms = $isSiteRequest ? $siteSetting('search_used_terms') : false;

// Prepare the property queries.
$properties = isset($query['property']) ? $query['property'] : [];
$properties = array_filter($properties, function ($value) {
Expand All @@ -14,7 +23,7 @@ if (isset($query['search'])) {
array_unshift($properties, [
'property' => '',
'type' => 'in',
'text' => $query['search']
'text' => $query['search'],
]);
}

Expand All @@ -25,13 +34,13 @@ $queryOption = function($value, array $search, $key, $text) {
}
return sprintf('<option value="%s"%s>%s</option>', $value, $selected, $text);
};
$queryText = function(array $search, $index) {
$queryText = function(array $search, $index) use ($escape, $translate) {
$text = isset($search['text']) ? $search['text'] : null;
return sprintf('<input type="text" class="query-text" name="%s" value="%s" aria-label="%s">',
$this->escapeHtml("property[$index][text]"),
$this->escapeHtml($text),
$this->escapeHtml($this->translate('Query text')));
}
$escape("property[$index][text]"),
$escape($text),
$escape($translate('Query text')));
};
?>

<div id="property-queries" class="field removable multi-value" role="group" aria-labelledby="by-value-label">
Expand All @@ -42,14 +51,14 @@ $queryText = function(array $search, $index) {
<?php
$index = 0;
foreach ($properties as $property):
$stem = "property[$index]";
$stem = "property[$index]";
?>
<div class="value">
<select class="joiner" name="<?php echo $this->escapeHtml($stem . '[joiner]'); ?>">
<select class="joiner" name="<?php echo $escape($stem . '[joiner]'); ?>">
<?php echo $queryOption('and', $property, 'joiner', $translate('AND')); ?>
<?php echo $queryOption('or', $property, 'joiner', $translate('OR')); ?>
</select>
<?php echo $this->propertySelect([
<?php echo $propertySelect([
'name' => $stem . '[property]',
'attributes' => [
'class' => 'query-property',
Expand All @@ -58,10 +67,11 @@ $queryText = function(array $search, $index) {
],
'options' => [
'empty_option' => '[Any Property]', // @translate
'apply_templates' => $this->status()->isSiteRequest() ? $this->siteSetting('search_apply_templates') : false,
]
'apply_templates' => $applyTemplates,
'used_terms' => $usedTerms,
],
]); ?>
<select class="query-type" name="<?php echo $this->escapeHtml($stem . '[type]'); ?>" aria-label="<?php echo $translate('Query type'); ?>">
<select class="query-type" name="<?php echo $escape($stem . '[type]'); ?>" aria-label="<?php echo $translate('Query type'); ?>">
<?php echo $queryOption('eq', $property, 'type', $translate('is exactly')); ?>
<?php echo $queryOption('neq', $property, 'type', $translate('is not exactly')); ?>
<?php echo $queryOption('in', $property, 'type', $translate('contains')); ?>
Expand All @@ -75,10 +85,10 @@ $queryText = function(array $search, $index) {
<button type="button" class="o-icon-delete remove-value button"><?php echo $translate('Remove value'); ?></button>
</div>
<?php
$index++;
++$index;
endforeach;

echo $this->hyperlink($translate('Add new value'), '#', ['class' => 'add-value button']);
echo $hyperlink($translate('Add new value'), '#', ['class' => 'add-value button']);
?>
</div>
</div>
13 changes: 11 additions & 2 deletions application/view/common/advanced-search/resource-class.phtml
@@ -1,5 +1,11 @@
<?php
$translate = $this->plugin('translate');
$resourceClassSelect = $this->plugin('resourceClassSelect');
$siteSetting = $this->plugin('siteSetting');

$isSiteRequest = $this->status()->isSiteRequest();
$usedTerms = $isSiteRequest ? $siteSetting('search_used_terms') : false;

// Prepare the resource class query.
$ids = isset($query['resource_class_id']) ? $query['resource_class_id'] : [];
if (!is_array($ids)) {
Expand All @@ -21,11 +27,14 @@ if (!$ids) {
<div class="inputs">
<?php foreach ($ids as $id): ?>
<div class="value">
<?php echo $this->resourceClassSelect([
<?php echo $resourceClassSelect([
'name' => 'resource_class_id[]',
'attributes' => [
'value' => $id,
'aria-labelledby' => 'by-resource-class-label'
'aria-labelledby' => 'by-resource-class-label',
],
'options' => [
'used_terms' => $usedTerms,
],
]); ?>
<button type="button" class="o-icon-delete remove-value button"><?php echo $translate('Remove value'); ?></button>
Expand Down

0 comments on commit 8dcdeaf

Please sign in to comment.