N°4621 Fix naming inconsistencies in sources/*

This commit is contained in:
Pierre Goiffon
2021-12-31 15:21:08 +01:00
parent 16142bd979
commit 5f575d524a
192 changed files with 384 additions and 380 deletions

View File

@@ -0,0 +1,186 @@
<?php
/**
* Copyright (C) 2013-2021 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Application\UI\Base\Component\GlobalSearch;
use Combodo\iTop\Application\UI\Base\UIBlock;
use iKeyboardShortcut;
use MetaModel;
use utils;
/**
* Class GlobalSearch
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @package Combodo\iTop\Application\UI\Base\Component\GlobalSearch
* @internal
* @since 3.0.0
*/
class GlobalSearch extends UIBlock implements iKeyboardShortcut
{
// Overloaded constants
public const BLOCK_CODE = 'ibo-global-search';
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/global-search/layout';
public const DEFAULT_JS_TEMPLATE_REL_PATH = 'base/components/global-search/layout';
public const DEFAULT_JS_FILES_REL_PATH = [
'js/components/global-search.js',
];
public const DEFAULT_ENDPOINT_REL_URL = 'pages/UI.php';
/** @var string $sEndpoint Absolute endpoint URL of the search form */
protected $sEndpoint;
/** @var string Query currently in the input */
protected $sQuery;
/** @var array $aLastQueries */
protected $aLastQueries;
/** @var bool $bShowHistory Whether or not to display the elements in the history */
protected $bShowHistory;
/** @var int $iMaxHistoryResults Max. number of elements in the history */
protected $iMaxHistoryResults;
/**
* GlobalSearch constructor.
*
* @param array $aLastQueries
* @param string|null $sId
*
* @throws \Exception
*/
public function __construct(array $aLastQueries = [], ?string $sId = null)
{
parent::__construct($sId);
$this->SetEndpoint(static::DEFAULT_ENDPOINT_REL_URL);
$this->SetQuery('');
$this->SetLastQueries($aLastQueries);
$this->bShowHistory = (bool)MetaModel::GetConfig()->Get('global_search.show_history');
$this->iMaxHistoryResults = (int)MetaModel::GetConfig()->Get('global_search.max_history_results');
}
/**
* Set the search form endpoint URL.
* If $bRelativeUrl is true, then $sEndpoint will be complete with the app_root_url
*
* @param string $sEndpoint URL to the endpoint
* @param bool $bRelativeUrl Whether or not the $sEndpoint parameter is a relative URL
*
* @return $this
* @throws \Exception
*/
public function SetEndpoint(string $sEndpoint, bool $bRelativeUrl = true)
{
$this->sEndpoint = (($bRelativeUrl) ? utils::GetAbsoluteUrlAppRoot() : '').$sEndpoint;
return $this;
}
/**
* Return the absolute URL of the search form
*
* @return string
* @throws \Exception
*/
public function GetEndpoint(): string
{
return $this->sEndpoint;
}
/**
* @uses $sQuery
* @param string $sQuery
*
* @return $this
*/
public function SetQuery(string $sQuery)
{
$this->sQuery = $sQuery;
return $this;
}
/**
* @uses $sQuery
* @return string
*/
public function GetQuery(): string
{
return $this->sQuery;
}
/**
* @uses $sQuery
* @return bool
*/
public function HasQuery(): bool
{
return !empty($this->sQuery);
}
/**
* Set all the last queries at once
*
* @param array $aLastQueries
*
* @return $this
*/
public function SetLastQueries(array $aLastQueries)
{
$this->aLastQueries = $aLastQueries;
return $this;
}
/**
* Return the last queries (query itself, label as HTML)
*
* @return array
*/
public function GetLastQueries(): array
{
return $this->aLastQueries;
}
/**
* @see $bShowHistory
* @return bool
*/
public function GetShowHistory(): bool
{
return $this->bShowHistory;
}
/**
* @see $iMaxHistoryResults
* @return int
*/
public function GetMaxHistoryResults(): int
{
return $this->iMaxHistoryResults;
}
public static function GetShortcutKeys(): array
{
return [['id' => 'ibo-open-global-search', 'label' => 'UI:Component:GlobalSearch:KeyboardShortcut:OpenDrawer', 'key' => 'g', 'event' => 'open_drawer']];
}
public static function GetShortcutTriggeredElementSelector(): string
{
return "[data-role='".static::BLOCK_CODE."']";
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* Copyright (C) 2013-2021 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Application\UI\Base\Component\GlobalSearch;
/**
* Class GlobalSearchFactory
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @package Combodo\iTop\Application\UI\Base\Component\GlobalSearch
* @internal
* @since 3.0.0
*/
class GlobalSearchFactory
{
/**
* Make a GlobalSearch component with the history entries from the current user
*
* @return \Combodo\iTop\Application\UI\Base\Component\GlobalSearch\GlobalSearch
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
* @throws \Exception
*/
public static function MakeFromUserHistory()
{
$aLastClasses = GlobalSearchHelper::GetLastQueries();
return new GlobalSearch($aLastClasses, GlobalSearch::BLOCK_CODE);
}
}

View File

@@ -0,0 +1,145 @@
<?php
/**
* Copyright (C) 2013-2021 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Application\UI\Base\Component\GlobalSearch;
use appUserPreferences;
use MetaModel;
use utils;
/**
* Class GlobalSearchHelper
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @package Combodo\iTop\Application\UI\Base\Component\GlobalSearch
* @internal
* @since 3.0.0
*/
class GlobalSearchHelper
{
/** @var string */
public const USER_PREF_CODE = 'global_search_history';
/**
* Add $sQuery to the history. History is limited to the static::MAX_HISTORY_SIZE last queries.
*
* @param string $sQuery Raw search query
* @param string|null $sIconRelUrl Relative URL of the icon
* @param string|null $sLabelAsHtml Alternate label for the query (eg. more human readable or with highlights), MUST be html entities
* otherwise there can be XSS flaws
*
* @return void
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
* @throws \Exception
* @noinspection PhpUnused Called by /pages/UI.php and extensions overloading the global search
*/
public static function AddQueryToHistory(string $sQuery, ?string $sIconRelUrl = null, ?string $sLabelAsHtml = null)
{
$aNewEntry = [
'query' => $sQuery,
];
// Set icon only when necessary
if (!empty($sIconRelUrl))
{
//Ensure URL is relative to limit space in the preferences and avoid broken links in case app_root_url changes
$aNewEntry['icon_url'] = str_replace(utils::GetAbsoluteUrlAppRoot(), '', $sIconRelUrl);
}
// Set label only when necessary to avoid unnecessary space filling of the preferences in the DB
if(!empty($sLabelAsHtml))
{
$aNewEntry['label_html'] = $sLabelAsHtml;
}
/** @var array $aHistoryEntries */
$aHistoryEntries = appUserPreferences::GetPref(static::USER_PREF_CODE, []);
// Remove same query from history to avoid duplicates
for($iIdx = 0; $iIdx < count($aHistoryEntries); $iIdx++)
{
if($aHistoryEntries[$iIdx]['query'] === $sQuery)
{
unset($aHistoryEntries[$iIdx]);
}
}
// Add new entry
array_unshift($aHistoryEntries, $aNewEntry);
// Truncate history
static::TruncateHistory($aHistoryEntries);
appUserPreferences::SetPref(static::USER_PREF_CODE, $aHistoryEntries);
}
/**
* Return an array of past queries, including the query itself and its HTML label
*
* @return array
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
* @throws \Exception
*/
public static function GetLastQueries()
{
/** @var array $aHistoryEntries */
$aHistoryEntries = appUserPreferences::GetPref(static::USER_PREF_CODE, []);
static::TruncateHistory($aHistoryEntries);
for($iIdx = 0; $iIdx < count($aHistoryEntries); $iIdx++){
$sRawQuery = $aHistoryEntries[$iIdx]['query'];
// Make icon URL absolute
if(isset($aHistoryEntries[$iIdx]['icon_url'])){
$aHistoryEntries[$iIdx]['icon_url'] = utils::GetAbsoluteUrlAppRoot().$aHistoryEntries[$iIdx]['icon_url'];
}
// Add HTML label if missing
if(!isset($aHistoryEntries[$iIdx]['label_html'])) {
$aHistoryEntries[$iIdx]['label_html'] = utils::EscapeHtml($sRawQuery);
}
// Add URL
if(!isset($aHistoryEntries[$iIdx]['target_url'])){
$aHistoryEntries[$iIdx]['target_url'] = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=full_text&text='.urlencode($sRawQuery);
}
}
return $aHistoryEntries;
}
/**
* Truncate $aHistoryEntries to 'global_search.max_history_results' entries
*
* @param array $aHistoryEntries
*/
protected static function TruncateHistory(array &$aHistoryEntries): void
{
$iMaxHistoryResults = (int) MetaModel::GetConfig()->Get('global_search.max_history_results');
if(count($aHistoryEntries) > $iMaxHistoryResults)
{
$aHistoryEntries = array_slice($aHistoryEntries, 0, $iMaxHistoryResults);
}
}
}