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

Added an option to list only used properties and resource classes in advanced search form. #1423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 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,12 +120,12 @@ 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))
);
}
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 @@ -296,6 +296,17 @@ public function init()
'required' => false,
],
]);
$searchFieldset->add([
'type' => \Laminas\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
36 changes: 23 additions & 13 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 @@ -43,14 +52,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 @@ -59,10 +68,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 @@ -76,7 +86,7 @@ $queryText = function(array $search, $index) {
<button type="button" class="o-icon-delete remove-value button" aria-label="<?php echo $translate('Remove value'); ?>" title="<?php echo $translate('Remove value'); ?>"></button>
</div>
<?php
$index++;
++$index;
endforeach;
?>
</div>
Expand Down
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 @@ -22,11 +28,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" aria-label="<?php echo $translate('Remove value'); ?>" title="<?php echo $translate('Remove value'); ?>"></button>
Expand Down