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

Set the default sort order for fulltext searches to desc #2192

Merged
merged 2 commits into from
May 21, 2024
Merged
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
6 changes: 6 additions & 0 deletions application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,12 @@ public function searchFulltext(ZendEvent $event)

if (isset($query['sort_by_default']) || !$qb->getDQLPart('orderBy')) {
$sortOrder = 'asc' === $query['sort_order'] ? 'ASC' : 'DESC';
if (isset($query['sort_order_default']) && isset($query['fulltext_search'])) {
// The default sort order for fulltext searches must be
// descending to account for the natural order of relevance
// scores.
$sortOrder = 'DESC';
}
$qb->orderBy($match, $sortOrder);
}
}
Expand Down
7 changes: 5 additions & 2 deletions application/src/Mvc/Controller/Plugin/SetBrowseDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class SetBrowseDefaults extends AbstractPlugin
public function __invoke($sortBy, $sortOrder = 'desc', $page = 1)
{
$query = $this->getController()->getRequest()->getQuery();
// Set the sort_by_default flag if the request doesn't pass a sort_by.
$query->set('sort_by_default', (null === $query->get('sort_by') || '' === $query->get('sort_by')) ? '' : null);
// Set the default sort flags if the request doesn't pass them.
$sortByDefault = (null === $query->get('sort_by') || '' === $query->get('sort_by')) ? '' : null;
$sortOrderDefault = (isset($sortByDefault) && (null === $query->get('sort_order') || '' === $query->get('sort_order'))) ? '' : null;
$query->set('sort_by_default', $sortByDefault);
$query->set('sort_order_default', $sortOrderDefault);
$query->set('sort_by', $query->get('sort_by', $sortBy));
$query->set('sort_order', $query->get('sort_order', $sortOrder));
$query->set('page', $query->get('page', $page));
Expand Down
1 change: 1 addition & 0 deletions application/src/Site/BlockLayout/BrowsePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function render(PhpRenderer $view, SitePageBlockRepresentation $block, $t
$query['sort_by'] = 'created';
}
if (!isset($query['sort_order'])) {
$query['sort_order_default'] = '';
$query['sort_order'] = 'desc';
}

Expand Down
2 changes: 1 addition & 1 deletion application/src/View/Helper/Browse.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function renderSortSelector($resourceTypeOrSortConfig) : string
$args = [
'sortConfig' => $sortConfig,
'sortByQuery' => (isset($query['sort_by_default']) && $isFulltextSearch) ? '' : $view->params()->fromQuery('sort_by'),
'sortOrderQuery' => $view->params()->fromQuery('sort_order'),
'sortOrderQuery' => (isset($query['sort_order_default']) && $isFulltextSearch) ? 'desc' : $view->params()->fromQuery('sort_order'),
];
$args = $view->trigger('view.sort-selector', $args, true);
return $view->partial('common/sort-selector', (array) $args);
Expand Down
8 changes: 6 additions & 2 deletions application/src/View/Helper/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,16 @@ protected function getUrl($page)
{
$query = $this->getView()->params()->fromQuery();
$query['page'] = (int) $page;
// Do not emit sorts if the corresponding default sort flags are set.
if (isset($query['sort_by_default'])) {
// Do not emit sort_by if the sort_by_default flag is set.
unset($query['sort_by']);
}
// Do not emit the sort_by_default flag
if (isset($query['sort_order_default'])) {
unset($query['sort_order']);
}
// Do not emit default sort flags.
unset($query['sort_by_default']);
unset($query['sort_order_default']);
$options = ['query' => $query];
if (is_string($this->fragment)) {
$options['fragment'] = $this->fragment;
Expand Down
1 change: 1 addition & 0 deletions application/view/common/advanced-search/sort.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ $sortBy->setAttribute('aria-label', 'Sort by'); // @translate
$sortBy->setEmptyOption('Select sort by…'); // @translate
$sortBy->setValueOptions($sortConfig);
$sortBy->setValue((!isset($query['sort_by_default']) && isset($query['sort_by'])) ? $query['sort_by'] : '');
$sortBy->setValue((!isset($query['sort_order_default']) && isset($query['sort_order'])) ? $query['sort_order'] : '');

$sortOrder = new Element\Select('sort_order');
$sortOrder->setAttribute('aria-label', 'Sort order'); // @translate
Expand Down
9 changes: 6 additions & 3 deletions application/view/common/pagination.phtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php
$translate = $this->plugin('translate');

// Do not emit the sort_by_default flag.
$removeQueries = ['page', 'sort_by_default'];
// Do not emit the sort default flags.
$removeQueries = ['page', 'sort_by_default', 'sort_order_default'];
// Do not emit sorts if the corresponding default sort flags are set.
if (isset($query['sort_by_default'])) {
// Do not emit sort_by if the sort_by_default flag is set.
$removeQueries[] = 'sort_by';
}
if (isset($query['sort_order_default'])) {
$removeQueries[] = 'sort_order';
}
?>
<nav class="pagination" role="navigation">
<?php if ($totalCount): ?>
Expand Down
2 changes: 1 addition & 1 deletion application/view/common/sort-selector.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
?>
<form class="sorting" action="">
<?php echo $this->queryToHiddenInputs(['sort_by', 'sort_order', 'sort_by_default']); ?>
<?php echo $this->queryToHiddenInputs(['sort_by', 'sort_order', 'sort_by_default', 'sort_order_default']); ?>
<select name="sort_by" aria-label="<?php echo $translate('Sort by'); ?>">
<?php foreach($sortConfig as $sortBy => $label): ?>
<option value="<?php echo $escape($sortBy); ?>"<?php echo ($sortByQuery === $sortBy) ? ' selected' : ''; ?>><?php echo $escape($label); ?></option>
Expand Down
Loading