JoomlaPolska / jezyk-J4

Język polski dla Joomla 4
GNU General Public License v2.0
3 stars 5 forks source link

[4.3] Smart Search Sorting Drop-Down #297

Closed joomlapl-bot closed 1 year ago

joomlapl-bot commented 1 year ago

PR w związku ze zmianą oryginału https://github.com/joomla/joomla-cms/pull/35993 Poniżej zmiany w oryginale:

Click to expand the diff! ```diff diff --git a/administrator/components/com_finder/config.xml b/administrator/components/com_finder/config.xml index 261b7d7d6049..f4b727ecce81 100644 --- a/administrator/components/com_finder/config.xml +++ b/administrator/components/com_finder/config.xml @@ -217,6 +217,7 @@ + 'COM_FINDER_SORT_BY_RELEVANCE_ASC', + 'relevance.desc' => 'COM_FINDER_SORT_BY_RELEVANCE_DESC', + 'title.asc' => 'JGLOBAL_TITLE_ASC', + 'title.desc' => 'JGLOBAL_TITLE_DESC', + 'date.asc' => 'JDATE_ASC', + 'date.desc' => 'JDATE_DESC', + 'price.asc' => 'COM_FINDER_SORT_BY_PRICE_ASC', + 'price.desc' => 'COM_FINDER_SORT_BY_PRICE_DESC', + 'sale_price.asc' => 'COM_FINDER_SORT_BY_SALES_PRICE_ASC', + 'sale_price.desc' => 'COM_FINDER_SORT_BY_SALES_PRICE_DESC', + ]; + /** * An array of all excluded terms ids. * @@ -296,6 +319,99 @@ protected function getListQuery() return $query; } + /** + * Method to get the available sorting fields. + * + * @return array The sorting field objects. + * + * @throws \Exception + * + * @since __DEPLOY_VERSION__ + */ + public function getSortOrderFields() + { + $sortOrderFields = []; + $directions = ['asc', 'desc']; + $app = Factory::getApplication(); + $params = $app->getParams(); + $sortOrderFieldValues = $params->get('shown_sort_order', [], 'array'); + + if ($params->get('show_sort_order', 0, 'uint') && !empty($sortOrderFieldValues)) { + $defaultSortFieldValue = $params->get('sort_order', '', 'cmd'); + $queryUri = Uri::getInstance($this->getQuery()->toUri()); + + // If the default field is not included in the shown sort fields, add it. + if (!in_array($defaultSortFieldValue, $sortOrderFieldValues)) { + array_unshift($sortOrderFieldValues, $defaultSortFieldValue); + } + + foreach ($sortOrderFieldValues as $sortOrderFieldValue) { + foreach ($directions as $direction) { + // The relevance has only descending direction. Except if ascending is set in the parameters. + if ($sortOrderFieldValue === 'relevance' && $direction === 'asc' && $app->getParams()->get('sort_direction', 'desc') === 'desc') { + continue; + } + + $sortOrderFields[] = $this->getSortField($sortOrderFieldValue, $direction, $queryUri); + } + } + } + + // Import Finder plugins + PluginHelper::importPlugin('finder'); + + // Trigger an event, in case a plugin wishes to change the order fields. + $app->triggerEvent('onFinderSortOrderFields', [&$sortOrderFields]); + + return $sortOrderFields; + } + + /** + * Method to generate and return a sorting field + * + * @param string $value The value based on which the results will be sorted. + * @param string $direction The sorting direction ('asc' or 'desc'). + * @param Uri $queryUri The uri of the search query. + * + * @return \stdClass The sorting field object. + * + * @throws \Exception + * + * @since __DEPLOY_VERSION__ + */ + protected function getSortField(string $value, string $direction, Uri $queryUri) + { + $sortField = new \stdClass(); + $app = Factory::getApplication(); + + // We have to clone the query uri. Otherwise the next elements will use the same. + $queryUri = clone $queryUri; + $queryUri->setVar('o', $value); + $currentOrderingDirection = $app->getInput()->getWord('od', $app->getParams()->get('sort_direction', 'desc')); + + // Validate the sorting direction and add it only if it is different than the set in the params. + if (in_array($direction, ['asc', 'desc']) && $direction != $app->getParams()->get('sort_direction', 'desc')) { + $queryUri->setVar('od', StringHelper::strtolower($direction)); + } + + $label = ''; + + if (isset($this->sortOrderFieldsLabels[$value . '.' . $direction])) { + $label = Text::_($this->sortOrderFieldsLabels[$value . '.' . $direction]); + } + + $sortField->label = $label; + $sortField->url = $queryUri->toString(); + $currentSortOrderField = $app->getInput()->getWord('o', $app->getParams()->get('sort_order', 'relevance')); + $sortField->active = false; + + if ($value === StringHelper::strtolower($currentSortOrderField) && $direction === $currentOrderingDirection) { + $sortField->active = true; + } + + return $sortField; + } + /** * Method to get a store id based on model the configuration state. * @@ -418,6 +534,10 @@ protected function populateState($ordering = null, $direction = null) $this->setState('list.ordering', 'l.list_price'); break; + case 'sale_price': + $this->setState('list.ordering', 'l.sale_price'); + break; + case ($order === 'relevance' && !empty($this->includedTerms)): $this->setState('list.ordering', 'm.weight'); break; diff --git a/components/com_finder/src/View/Search/HtmlView.php b/components/com_finder/src/View/Search/HtmlView.php index 24a6e7603861..cf00b1d563a2 100644 --- a/components/com_finder/src/View/Search/HtmlView.php +++ b/components/com_finder/src/View/Search/HtmlView.php @@ -143,6 +143,8 @@ public function display($tpl = null) \JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderQuery') : null; $this->results = $this->get('Items'); \JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderResults') : null; + $this->sortOrderFields = $this->get('sortOrderFields'); + \JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderSortOrderFields') : null; $this->total = $this->get('Total'); \JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderTotal') : null; $this->pagination = $this->get('Pagination'); diff --git a/components/com_finder/tmpl/search/default.xml b/components/com_finder/tmpl/search/default.xml index 2a0f363c5b91..2d1eba9d5bed 100644 --- a/components/com_finder/tmpl/search/default.xml +++ b/components/com_finder/tmpl/search/default.xml @@ -256,6 +256,32 @@ + + + + + + + + + + + + + +params->get('show_sort_order', 0) && !empty($this->sortOrderFields) && !empty($this->results)) : ?> +
+ loadTemplate('sorting'); ?> +
+ query->highlight) && $this->params->get('highlight_terms', 1)) : ?> + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\HTML\HTMLHelper; +use Joomla\CMS\Language\Text; +use Joomla\CMS\Router\Route; + +?> + +
+ +
+ sortOrderFields as $sortOrderField) : ?> + active) : ?> + + + + + +
+
+
diff --git a/language/en-GB/com_finder.ini b/language/en-GB/com_finder.ini index b3d7edb93f9c..3853dae9e0bf 100644 --- a/language/en-GB/com_finder.ini +++ b/language/en-GB/com_finder.ini @@ -49,3 +49,10 @@ COM_FINDER_SEARCH_NO_RESULTS_HEADING="No Results Found" COM_FINDER_SEARCH_RESULTS_OF="Results %s - %s of %s" COM_FINDER_SEARCH_SIMILAR="Did you mean: %s?" COM_FINDER_SEARCH_TERMS="Search Terms:" +COM_FINDER_SORT_BY="Sort By:" +COM_FINDER_SORT_BY_PRICE_ASC="List price ascending" +COM_FINDER_SORT_BY_PRICE_DESC="List price descending" +COM_FINDER_SORT_BY_RELEVANCE_ASC="Relevance ascending" +COM_FINDER_SORT_BY_RELEVANCE_DESC="Relevance descending" +COM_FINDER_SORT_BY_SALES_PRICE_ASC="Sales price ascending" +COM_FINDER_SORT_BY_SALES_PRICE_DESC="Sales price descending" ```