Skip to content

Commit

Permalink
Refactor hierarchical facets (#3046)
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala authored Dec 4, 2023
1 parent 3f0eb5d commit 36e3625
Show file tree
Hide file tree
Showing 35 changed files with 356 additions and 1,271 deletions.
139 changes: 0 additions & 139 deletions module/VuFind/src/VuFind/AjaxHandler/GetFacetData.php

This file was deleted.

78 changes: 0 additions & 78 deletions module/VuFind/src/VuFind/AjaxHandler/GetFacetDataFactory.php

This file was deleted.

76 changes: 5 additions & 71 deletions module/VuFind/src/VuFind/AjaxHandler/GetSideFacets.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 2018-2019.
* Copyright (C) The National Library of Finland 2018-2023.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand Down Expand Up @@ -34,15 +34,11 @@
use Laminas\View\Renderer\RendererInterface;
use VuFind\Recommend\PluginManager as RecommendPluginManager;
use VuFind\Recommend\SideFacets;
use VuFind\Search\Base\Options;
use VuFind\Search\Base\Results;
use VuFind\Search\RecommendListener;
use VuFind\Search\SearchRunner;
use VuFind\Search\Solr\HierarchicalFacetHelper;
use VuFind\Search\UrlQueryHelper;
use VuFind\Session\Settings as SessionSettings;

use function in_array;
use function is_callable;

/**
Expand Down Expand Up @@ -73,13 +69,6 @@ class GetSideFacets extends \VuFind\AjaxHandler\AbstractBase implements \Laminas
*/
protected $searchRunner;

/**
* Hierarchical facet helper
*
* @var HierarchicalFacetHelper
*/
protected $facetHelper;

/**
* View renderer
*
Expand All @@ -90,23 +79,20 @@ class GetSideFacets extends \VuFind\AjaxHandler\AbstractBase implements \Laminas
/**
* Constructor
*
* @param SessionSettings $ss Session settings
* @param RecommendPluginManager $rpm Recommend plugin manager
* @param SearchRunner $sr Search runner
* @param HierarchicalFacetHelper $fh Facet helper
* @param RendererInterface $renderer View renderer
* @param SessionSettings $ss Session settings
* @param RecommendPluginManager $rpm Recommend plugin manager
* @param SearchRunner $sr Search runner
* @param RendererInterface $renderer View renderer
*/
public function __construct(
SessionSettings $ss,
\VuFind\Recommend\PluginManager $rpm,
SearchRunner $sr,
HierarchicalFacetHelper $fh,
RendererInterface $renderer
) {
$this->sessionSettings = $ss;
$this->recommendPluginManager = $rpm;
$this->searchRunner = $sr;
$this->facetHelper = $fh;
$this->renderer = $renderer;
}

Expand Down Expand Up @@ -235,24 +221,11 @@ protected function formatFacets(
Results $results
) {
$response = [];
$options = $results->getOptions();
$hierarchicalFacets = $options->getHierarchicalFacets();
$hierarchicalFacetSortOptions
= $recommend->getHierarchicalFacetSortOptions();
$facetSet = $recommend->getFacetSet();
$urlHelper = $results->getUrlQuery();
foreach ($facets as $facet) {
if (strpos($facet, ':')) {
$response[$facet]['checkboxCount']
= $this->getCheckboxFacetCount($facet, $results);
} elseif (in_array($facet, $hierarchicalFacets)) {
$response[$facet]['list'] = $this->getHierarchicalFacetData(
$facet,
$hierarchicalFacetSortOptions,
$facetSet[$facet]['list'] ?? [],
$urlHelper,
$results->getOptions(),
);
} else {
$context['facet'] = $facet;
$context['cluster'] = $facetSet[$facet] ?? [];
Expand All @@ -279,43 +252,4 @@ protected function getCheckboxFacetCount($facet, Results $results)
// There's currently no good way to return counts for checkbox filters.
return null;
}

/**
* Get facet data for a hierarchical facet
*
* @param string $facet Facet
* @param array $sortOptions Hierarchical facet sort options
* @param array $facetList Facet list
* @param UrlQueryHelper $urlHelper UrlQueryHelper for creating facet URLs
* @param Options $options Results options
*
* @return array
*/
protected function getHierarchicalFacetData(
$facet,
$sortOptions,
$facetList,
UrlQueryHelper $urlHelper,
Options $options
) {
if (!empty($sortOptions[$facet])) {
$this->facetHelper->sortFacetList(
$facetList,
'top' === $sortOptions[$facet]
);
}

$result = $this->facetHelper->buildFacetArray(
$facet,
$facetList,
$urlHelper,
false
);
$result = $this->facetHelper->filterFacets(
$facet,
$result,
$options
);
return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function __invoke(
$container->get(\VuFind\Session\Settings::class),
$container->get(\VuFind\Recommend\PluginManager::class),
$container->get(\VuFind\Search\SearchRunner::class),
$container->get(\VuFind\Search\Solr\HierarchicalFacetHelper::class),
$container->get('ViewRenderer')
);
return $result;
Expand Down
2 changes: 0 additions & 2 deletions module/VuFind/src/VuFind/AjaxHandler/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
'deleteRecordComment' => DeleteRecordComment::class,
'doiLookup' => DoiLookup::class,
'getACSuggestions' => GetACSuggestions::class,
'getFacetData' => GetFacetData::class,
'getIlsStatus' => GetIlsStatus::class,
'getItemStatuses' => GetItemStatuses::class,
'getLibraryPickupLocations' => GetLibraryPickupLocations::class,
Expand Down Expand Up @@ -92,7 +91,6 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
DeleteRecordComment::class => DeleteRecordCommentFactory::class,
DoiLookup::class => DoiLookupFactory::class,
GetACSuggestions::class => GetACSuggestionsFactory::class,
GetFacetData::class => GetFacetDataFactory::class,
GetIlsStatus::class => GetIlsStatusFactory::class,
GetItemStatuses::class => GetItemStatusesFactory::class,
GetLibraryPickupLocations::class => AbstractIlsAndUserActionFactory::class,
Expand Down
Loading

0 comments on commit 36e3625

Please sign in to comment.