Skip to content
Draft
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
4 changes: 4 additions & 0 deletions config/vufind/facets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ dateRangeFieldType[publishDateRange] = DateRangeField
;hierarchical[] = building
;hierarchical[] = format

; Any fields listed below will be treated as "new items" facets.
; Note that ranges displayed are defined in [NewItem] section of searches.ini.
newItems[] = first_indexed

; General sort options for hierarchical facets (Home page, Advanced Search and
; SideFacets).
;
Expand Down
2 changes: 1 addition & 1 deletion module/VuFind/src/VuFind/Controller/Plugin/NewItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,6 @@ public function getResultPages()
*/
public function getSolrFilter($range)
{
return 'first_indexed:[NOW-' . $range . 'DAY TO NOW]';
return 'first_indexed:[NOW-' . $range . 'DAYS/DAY TO *]';
}
}
6 changes: 6 additions & 0 deletions module/VuFind/src/VuFind/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ public function newitemfacetlistAction()
public function newitemresultsAction()
{
$newItemParams = $this->getNewItemParameters();
if ($this->newItems()->getMethod() === 'solr') {
$filter = (array)$this->params()->fromQuery('filter', []);
$filter[] = $this->newItems()->getSolrFilter($newItemParams['range']);
$query = ['filter' => $filter, 'hiddenFilters' => $newItemParams['hiddenFilters']];
return $this->redirect()->toRoute('search-results', options: compact('query'));
}
$this->setUpNewItemRequestParams($newItemParams);

// Don't save to history or memory -- history page doesn't handle correctly
Expand Down
47 changes: 47 additions & 0 deletions module/VuFind/src/VuFind/Search/Base/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,20 @@ abstract class Options implements TranslatorAwareInterface
*/
protected string $advancedFacetSettingsSection = 'Advanced_Settings';

/**
* New items facets
*
* @var array
*/
protected array $newItemsFacets;

/**
* Ranges for new items facets
*
* @var array
*/
protected array $newItemsFacetRanges;

/**
* Constructor
*
Expand Down Expand Up @@ -528,6 +542,19 @@ public function __construct(protected ConfigManagerInterface $configManager)
$this->hierarchicalFacetFilters = $this->facetSettings['HierarchicalFacetFilters'] ?? [];
$this->setTranslatedFacets((array)($advancedFacetSettings['translated_facets'] ?? []));
$this->specialAdvancedFacets = $advancedFacetSettings['special_facets'] ?? '';
$this->newItemsFacets = array_map(
function ($v) {
return true;
},
array_flip((array)($this->facetSettings['SpecialFacets']['newItems'] ?? []))
);
// Find out if there are user configured range options; if not, default to the standard 1/5/30 days:
$this->newItemsFacetRanges = array_filter(
array_map(
'intval',
explode(',', $this->searchSettings['NewItem']['ranges'] ?? '')
)
) ?: [1, 5, 30];

// Result display options:
$this->resultScrollerActive = (bool)(
Expand Down Expand Up @@ -985,6 +1012,26 @@ public function getHierarchicalFacetSortSettings()
return $this->hierarchicalFacetSortSettings;
}

/**
* Get new items facets.
*
* @return array
*/
public function getNewItemsFacets(): array
{
return $this->newItemsFacets;
}

/**
* Get filters for new items facets.
*
* @return array
*/
public function getNewItemsFacetFilters(): array
{
return [];
}

/**
* Get current spellcheck setting and (optionally) change it.
*
Expand Down
27 changes: 18 additions & 9 deletions module/VuFind/src/VuFind/Search/Base/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -1990,18 +1990,27 @@ public function getSelectedShards()
/**
* Translate a string (or string-castable object)
*
* @param string|object|array $target String to translate or an array of text
* domain and string to translate
* @param array $tokens Tokens to inject into the translated
* string
* @param string $default Default value to use if no translation is
* found (null for no default).
* @param string|object|array $target String to translate or an array of text
* domain and string to translate
* @param array $tokens Tokens to inject into the translated
* string
* @param string $default Default value to use if no translation is
* found (null for no default).
Comment on lines +1993 to +1998
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting suggestions:

Suggested change
* @param string|object|array $target String to translate or an array of text
* domain and string to translate
* @param array $tokens Tokens to inject into the translated
* string
* @param string $default Default value to use if no translation is
* found (null for no default).
* @param string|object|array $target String to translate or an array of text
* domain and string to translate
* @param array $tokens Tokens to inject into the translated string
* @param string $default Default value to use if no translation is
* found (null for no default).

* @param bool $useIcuFormatter Should we use an ICU message formatter instead
* of the default behavior?
* @param string[] $fallbackDomains Text domains to check if no match is found in
* the domain specified in $target
*
* @return string
*/
public function translate($target, $tokens = [], $default = null)
{
return $this->getOptions()->translate($target, $tokens, $default);
public function translate(
$target,
$tokens = [],
$default = null,
$useIcuFormatter = false,
$fallbackDomains = []
) {
return $this->getOptions()->translate($target, $tokens, $default, $useIcuFormatter, $fallbackDomains);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions module/VuFind/src/VuFind/Search/Solr/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,18 @@ public function getDateRangeFieldTypes(): array
{
return (array)($this->facetSettings['SpecialFacets']['dateRangeFieldType'] ?? []);
}

/**
* Get filters for new items facets.
*
* @return array
*/
public function getNewItemsFacetFilters(): array
{
$result = [];
foreach ($this->newItemsFacetRanges as $days) {
$result[$days] = "[NOW-{$days}DAYS/DAY TO *]";
}
return $result;
}
}
77 changes: 68 additions & 9 deletions module/VuFind/src/VuFind/Search/Solr/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

use VuFind\Config\Config;
use VuFind\Config\ConfigManagerInterface;
use VuFind\Date\Converter as DateConverter;
use VuFind\Solr\Utils;
use VuFindSearch\ParamBag;

use function count;
Expand Down Expand Up @@ -105,13 +107,6 @@ class Params extends \VuFind\Search\Base\Params
*/
protected $pivotFacets = null;

/**
* Hierarchical Facet Helper
*
* @var HierarchicalFacetHelper
*/
protected $facetHelper;

/**
* Are we searching by ID only (instead of a normal query)?
*
Expand Down Expand Up @@ -149,14 +144,15 @@ class Params extends \VuFind\Search\Base\Params
* @param \VuFind\Search\Base\Options $options Options to use
* @param ConfigManagerInterface $configManager Config manager
* @param ?HierarchicalFacetHelper $facetHelper Hierarchical facet helper
* @param ?DateConverter $dateConverter Date converter
*/
public function __construct(
$options,
ConfigManagerInterface $configManager,
?HierarchicalFacetHelper $facetHelper = null
protected ?HierarchicalFacetHelper $facetHelper = null,
protected ?DateConverter $dateConverter = null
) {
parent::__construct($options, $configManager);
$this->facetHelper = $facetHelper;

// Use basic facet limit by default, if set:
$facetConfigName = $options->getFacetsIni();
Expand Down Expand Up @@ -680,6 +676,10 @@ public function getPivotFacets()
*/
protected function formatFilterListEntry($field, $value, $operator, $translate)
{
if (isset($this->options->getNewItemsFacets()[$field])) {
return $this->formatNewItemsFilterListEntry($field, $value, $operator, $translate);
}

$filter = parent::formatFilterListEntry(
$field,
$value,
Expand Down Expand Up @@ -742,6 +742,65 @@ function ($part) {
return $filter;
}

/**
* Format a new items filter for use in getFilterList().
*
* @param string $field Field name
* @param string $value Field value
* @param string $operator Operator (AND/OR/NOT)
* @param bool $translate Should we translate the label?
*
* @return array
*/
protected function formatNewItemsFilterListEntry($field, $value, $operator, $translate): array
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function formatNewItemsFilterListEntry($field, $value, $operator, $translate): array
protected function formatNewItemsFilterListEntry(
string $field,
string $value,
string $operator,
bool $translate
): array

{
$range = Utils::parseRange($value);
$domain = $this->getOptions()->getTextDomainForTranslatedFacet($field);
[$from, $fromDate] = $this->formatNewItemsDateForDisplay(
$range['from'],
$domain
);
[$to, $toDate] = $this->formatNewItemsDateForDisplay(
$range['to'],
$domain
);
$ndash = html_entity_decode('–', ENT_NOQUOTES, 'UTF-8');
if ($fromDate && $toDate) {
$displayText = $from ? "$from $ndash" : $ndash;
$displayText .= $to ? " $to" : '';
} else {
$displayText = $from;
$displayText .= $to ? " $ndash $to" : '';
}
return compact('value', 'displayText', 'field', 'operator');
}

/**
* Format a Solr date for display
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should expand this comment to explain the return value... because at present, I don't entirely understand what the second Boolean value is doing.

*
* @param string $date Date
* @param string $domain Translation domain
*
* @return string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return string
* @return array

*/
protected function formatNewItemsDateForDisplay($date, $domain)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function formatNewItemsDateForDisplay($date, $domain)
protected function formatNewItemsDateForDisplay(string $date, string $domain): array

{
if ($date == '' || $date == '*') {
return ['', true];
}
if (preg_match('/^NOW-(\d+)DAY/', $date, $matches)) {
return [
$this->translate('past_days', ['range' => $matches[1]], useIcuFormatter: true),
false,
];
}
$date = substr($date, 0, 10);
return [
$this->dateConverter?->convertToDisplayDate('Y-m-d', $date) ?? $date,
true,
];
}

/**
* Get information on the current state of the boolean checkbox facets.
*
Expand Down
6 changes: 3 additions & 3 deletions module/VuFind/src/VuFind/Search/Solr/ParamsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function __invoke(
if (!empty($options)) {
throw new \Exception('Unexpected options passed to factory.');
}
$helper = $container
->get(\VuFind\Search\Solr\HierarchicalFacetHelper::class);
return parent::__invoke($container, $requestedName, [$helper]);
$helper = $container->get(\VuFind\Search\Solr\HierarchicalFacetHelper::class);
$dateConverter = $container->get(\VuFind\Date\Converter::class);
return parent::__invoke($container, $requestedName, [$helper, $dateConverter]);
}
}
23 changes: 19 additions & 4 deletions module/VuFind/src/VuFind/Search/UrlQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ protected function getAliasesForFacetField($field)
/**
* Remove a facet from the parameters.
*
* @param string $field Facet field
* @param string $value Facet value
* @param string $operator Facet type to add (AND, OR, NOT)
* @param string $field Facet field
* @param ?string $value Facet value, or null to remove all values for the facet field
* @param string $operator Facet type to add (AND, OR, NOT)
*
* @return UrlQueryHelper
*/
Expand All @@ -480,7 +480,7 @@ public function removeFacet($field, $value, $operator = 'AND')
= $this->parseFilter($current);
if (
!in_array($currentField, $fieldAliases)
|| $currentValue != $value
|| (null !== $value && $currentValue != $value)
) {
$newFilter[] = $current;
}
Expand All @@ -498,6 +498,21 @@ public function removeFacet($field, $value, $operator = 'AND')
return new static($params, $this->queryObject, $this->config, false);
}

/**
* Remove any instance of the facet from the parameters and add a new one.
*
* @param string $field Facet field
* @param string $value Facet value
* @param string $operator Facet type to add (AND, OR, NOT)
*
* @return string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this isn't returning a string...

Suggested change
* @return string
* @return static

*/
public function replaceFacet($field, $value, $operator = 'AND')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well add types to new methods...

Suggested change
public function replaceFacet($field, $value, $operator = 'AND')
public function replaceFacet(string $field, string $value, string $operator = 'AND'): static

{
return $this->removeFacet($field, null, $operator)
->addFacet($field, $value, $operator);
}

/**
* Remove a filter from the parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function testIllegalResultPages()
public function testGetSolrFilter()
{
$range = 30;
$expected = 'first_indexed:[NOW-' . $range . 'DAY TO NOW]';
$expected = 'first_indexed:[NOW-' . $range . 'DAYS/DAY TO *]';
$newItems = new NewItems(new Config([]));
$this->assertEquals($expected, $newItems->getSolrFilter($range));
}
Expand Down
Loading
Loading