diff --git a/core/config.class.inc.php b/core/config.class.inc.php index 67919c8e6..c661107b7 100644 --- a/core/config.class.inc.php +++ b/core/config.class.inc.php @@ -1160,6 +1160,14 @@ class Config 'source_of_value' => '', 'show_in_conf_sample' => false, ), + 'global_search.max_history_results' => array( + 'type' => 'integer', + 'description' => 'Max. number of elements in the history.', + 'default' => 10, + 'value' => 10, + 'source_of_value' => '', + 'show_in_conf_sample' => false, + ), 'breadcrumb.enabled' => array( 'type' => 'bool', 'description' => 'Whether or not the breadcrumbs is enabled', diff --git a/sources/application/UI/Component/GlobalSearch/GlobalSearch.php b/sources/application/UI/Component/GlobalSearch/GlobalSearch.php index f6f5419b6..e482e3c35 100644 --- a/sources/application/UI/Component/GlobalSearch/GlobalSearch.php +++ b/sources/application/UI/Component/GlobalSearch/GlobalSearch.php @@ -21,6 +21,7 @@ namespace Combodo\iTop\Application\UI\Component\GlobalSearch; use Combodo\iTop\Application\UI\UIBlock; +use MetaModel; use utils; /** @@ -47,6 +48,8 @@ class GlobalSearch extends UIBlock protected $sEndpoint; /** @var array $aLastQueries */ protected $aLastQueries; + /** @var int $iMaxHistoryResults Max. number of elements in the history */ + protected $iMaxHistoryResults; /** * GlobalSearch constructor. @@ -61,6 +64,7 @@ class GlobalSearch extends UIBlock parent::__construct($sId); $this->SetEndpoint(static::DEFAULT_ENDPOINT_REL_URL); $this->SetLastQueries($aLastQueries); + $this->iMaxHistoryResults = (int) MetaModel::GetConfig()->Get('quick_create.max_history_results'); } /** @@ -114,4 +118,13 @@ class GlobalSearch extends UIBlock { return $this->aLastQueries; } + + /** + * @see $iMaxHistoryResults + * @return int + */ + public function GetMaxHistoryResults(): int + { + return $this->iMaxHistoryResults; + } } \ No newline at end of file diff --git a/sources/application/UI/Component/GlobalSearch/GlobalSearchHelper.php b/sources/application/UI/Component/GlobalSearch/GlobalSearchHelper.php index 778680582..be5eb2583 100644 --- a/sources/application/UI/Component/GlobalSearch/GlobalSearchHelper.php +++ b/sources/application/UI/Component/GlobalSearch/GlobalSearchHelper.php @@ -21,6 +21,7 @@ namespace Combodo\iTop\Application\UI\Component\GlobalSearch; use appUserPreferences; +use MetaModel; use utils; /** @@ -33,7 +34,7 @@ use utils; */ class GlobalSearchHelper { - public const MAX_HISTORY_SIZE = 10; + /** @var string */ public const USER_PREF_CODE = 'global_search_history'; /** @@ -86,10 +87,7 @@ class GlobalSearchHelper array_unshift($aHistoryEntries, $aNewEntry); // Truncate history - if(count($aHistoryEntries) > static::MAX_HISTORY_SIZE) - { - $aHistoryEntries = array_slice($aHistoryEntries, 0, static::MAX_HISTORY_SIZE); - } + static::TruncateHistory($aHistoryEntries); appUserPreferences::SetPref(static::USER_PREF_CODE, $aHistoryEntries); } @@ -107,6 +105,7 @@ class GlobalSearchHelper { /** @var array $aHistoryEntries */ $aHistoryEntries = appUserPreferences::GetPref(static::USER_PREF_CODE, []); + static::TruncateHistory($aHistoryEntries); for($iIdx = 0; $iIdx < count($aHistoryEntries); $iIdx++) { @@ -125,4 +124,18 @@ class GlobalSearchHelper 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); + } + } } \ No newline at end of file