- md5
- ae0c0acd8e10d383e879dcee33b527aa
- sha1
- 801da5ebb2a41fd1a59098622b1a6e25e39655b7
- sha256
- 1a19ed684115deed75389e50ad8a58289edd63517e533d349dc1946d12822a32
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
<?php
/**
* @package Joomla.Site
* @subpackage com_finder
*
* @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Finder\Site\Helper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Finder route helper class.
*
* @since 2.5
*/
class RouteHelper
{
/**
* Method to get the route for a search page.
*
* @param integer $f The search filter id. [optional]
* @param string $q The search query string. [optional]
*
* @return string The search route.
*
* @since 2.5
*/
public static function getSearchRoute($f = null, $q = null)
{
// Get the menu item id.
$query = ['view' => 'search', 'q' => $q, 'f' => $f];
$item = self::getItemid($query);
// Get the base route.
$uri = clone Uri::getInstance('index.php?option=com_finder&view=search');
// Add the pre-defined search filter if present.
if ($f !== null) {
$uri->setVar('f', $f);
}
// Add the search query string if present.
if ($q !== null) {
$uri->setVar('q', $q);
}
// Add the menu item id if present.
if ($item !== null) {
$uri->setVar('Itemid', $item);
}
return $uri->toString(['path', 'query']);
}
/**
* Method to get the route for an advanced search page.
*
* @param integer $f The search filter id. [optional]
* @param string $q The search query string. [optional]
*
* @return string The advanced search route.
*
* @since 2.5
*/
public static function getAdvancedRoute($f = null, $q = null)
{
// Get the menu item id.
$query = ['view' => 'advanced', 'q' => $q, 'f' => $f];
$item = self::getItemid($query);
// Get the base route.
$uri = clone Uri::getInstance('index.php?option=com_finder&view=advanced');
// Add the pre-defined search filter if present.
if ($q !== null) {
$uri->setVar('f', $f);
}
// Add the search query string if present.
if ($q !== null) {
$uri->setVar('q', $q);
}
// Add the menu item id if present.
if ($item !== null) {
$uri->setVar('Itemid', $item);
}
return $uri->toString(['path', 'query']);
}
/**
* Method to get the most appropriate menu item for the route based on the
* supplied query needles.
*
* @param array $query An array of URL parameters.
*
* @return mixed An integer on success, null otherwise.
*
* @since 2.5
*/
public static function getItemid($query)
{
static $items, $active;
// Get the menu items for com_finder.
if (!$items || !$active) {
$app = Factory::getApplication();
$com = ComponentHelper::getComponent('com_finder');
$menu = $app->getMenu();
$active = $menu->getActive();
$items = $menu->getItems('component_id', $com->id);
$items = is_array($items) ? $items : [];
}
// Try to match the active view and filter.
if ($active && @$active->query['view'] == @$query['view'] && @$active->query['f'] == @$query['f']) {
return $active->id;
}
// Try to match the view, query, and filter.
foreach ($items as $item) {
if (@$item->query['view'] == @$query['view'] && @$item->query['q'] == @$query['q'] && @$item->query['f'] == @$query['f']) {
return $item->id;
}
}
// Try to match the view and filter.
foreach ($items as $item) {
if (@$item->query['view'] == @$query['view'] && @$item->query['f'] == @$query['f']) {
return $item->id;
}
}
// Try to match the view.
foreach ($items as $item) {
if (@$item->query['view'] == @$query['view']) {
return $item->id;
}
}
return null;
}
}
The same content is in 6 releases, Joomla 4.2.7-rc1 to 4.3.0-alpha3. See them all, or every version of this file.