From 4c289edac74c0702b19b83c1e762f884611d38fe Mon Sep 17 00:00:00 2001 From: Molkobain Date: Tue, 6 Apr 2021 11:56:30 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B03887=20-=20Harmonize=20number=20of=20aut?= =?UTF-8?q?ocomplete=20displayed=20results=20throughout=20the=20app?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/ui.extkeywidget.class.inc.php | 7 ++- core/attributedef.class.inc.php | 16 +++++-- .../src/Controller/ObjectController.php | 2 +- pages/ajax.render.php | 4 +- sources/Form/Field/SelectObjectField.php | 47 +++++++++++++++++-- .../BsSelectObjectFieldRenderer.php | 2 +- 6 files changed, 62 insertions(+), 16 deletions(-) diff --git a/application/ui.extkeywidget.class.inc.php b/application/ui.extkeywidget.class.inc.php index 0d8026f9d..1751ee804 100644 --- a/application/ui.extkeywidget.class.inc.php +++ b/application/ui.extkeywidget.class.inc.php @@ -748,7 +748,7 @@ JS // Current extkey value, so we can display event if it is not available anymore (eg. archived). $iCurrentExtKeyId = (is_null($oObj) || $this->sAttCode === '') ? 0 : $oObj->Get($this->sAttCode); $oValuesSet = new ValueSetObjects($sFilter, 'friendlyname'); // Bypass GetName() to avoid the encoding by htmlentities - $iMax = 150; + $iMax = MetaModel::GetConfig()->Get('max_autocomplete_results'); $oValuesSet->SetLimit($iMax); $oValuesSet->SetSort(false); $oValuesSet->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', $this->bSearchMode); @@ -756,9 +756,8 @@ JS $aValuesContains = $oValuesSet->GetValuesForAutocomplete(array('this' => $oObj, 'current_extkey_id' => $iCurrentExtKeyId), $sContains, 'start_with'); asort($aValuesContains); $aValues = $aValuesContains; - if (sizeof($aValues) < $iMax) - { - $aValuesContains = $oValuesSet->GetValuesForAutocomplete(array('this' => $oObj, 'current_extkey_id' => $iCurrentExtKeyId), $sContains, 'contains'); + if (sizeof($aValues) < $iMax) { + $aValuesContains = $oValuesSet->GetValuesForAutocomplete(array('this' => $oObj, 'current_extkey_id' => $iCurrentExtKeyId), $sContains, 'contains'); asort($aValuesContains); $iSize = sizeof($aValuesContains); foreach ($aValuesContains as $sKey => $sFriendlyName) diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index ce57f8cd1..143b3fc7f 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -6845,6 +6845,15 @@ class AttributeExternalKey extends AttributeDBFieldVoid return $this->GetOptional('min_autocomplete_chars', MetaModel::GetConfig()->Get('min_autocomplete_chars')); } + /** + * @return int + * @since 3.0.0 + */ + public function GetMaxAutoCompleteResults(): int + { + return MetaModel::GetConfig()->Get('max_autocomplete_results'); + } + public function AllowTargetCreation() { return $this->GetOptional('allow_target_creation', MetaModel::GetConfig()->Get('allow_target_creation')); @@ -6889,8 +6898,7 @@ class AttributeExternalKey extends AttributeDBFieldVoid public function MakeFormField(DBObject $oObject, $oFormField = null) { - if ($oFormField === null) - { + if ($oFormField === null) { // Later : We should check $this->Get('display_style') and create a Radio / Select / ... regarding its value $sFormFieldClass = static::GetFormFieldClass(); $oFormField = new $sFormFieldClass($this->GetCode()); @@ -6899,11 +6907,11 @@ class AttributeExternalKey extends AttributeDBFieldVoid // Setting params $oFormField->SetMaximumComboLength($this->GetMaximumComboLength()); $oFormField->SetMinAutoCompleteChars($this->GetMinAutoCompleteChars()); + $oFormField->SetMaxAutoCompleteResults($this->GetMaxAutoCompleteResults()); $oFormField->SetHierarchical(MetaModel::IsHierarchicalClass($this->GetTargetClass())); // Setting choices regarding the field dependencies $aFieldDependencies = $this->GetPrerequisiteAttributes(); - if (!empty($aFieldDependencies)) - { + if (!empty($aFieldDependencies)) { $oTmpAttDef = $this; $oTmpField = $oFormField; $oFormField->SetOnFinalizeCallback(function () use ($oTmpField, $oTmpAttDef, $oObject) { diff --git a/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php b/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php index c100f3edd..52869cd24 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php +++ b/datamodels/2.x/itop-portal-base/portal/src/Controller/ObjectController.php @@ -640,7 +640,7 @@ class ObjectController extends BrickController $oSet = new DBObjectSet($oSearch, array(), array('this' => $oHostObject, 'ac_query' => '%'.$sQuery.'%')); $oSet->OptimizeColumnLoad(array($oSearch->GetClassAlias() => array('friendlyname'))); // Note : This limit is also used in the field renderer by typeahead to determine how many suggestions to display - $oSet->SetLimit(MetaModel::GetConfig()->Get('max_display_limit')); + $oSet->SetLimit(MetaModel::GetConfig()->Get('max_autocomplete_results')); // - Retrieving objects while ($oItem = $oSet->Fetch()) diff --git a/pages/ajax.render.php b/pages/ajax.render.php index aca5cb9d7..e0e3a1a17 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -2711,11 +2711,11 @@ EOF $oSearch = DBObjectSearch::FromOQL("SELECT $sTargetClass WHERE friendlyname LIKE :needle"); $oSet = new DBObjectSet($oSearch, array(), array('needle' => "%$sNeedle%")); $oSet->OptimizeColumnLoad(array($oSearch->GetClassAlias() => array())); - $oSet->SetLimit(5); + $oSet->SetLimit(MetaModel::GetConfig()->Get('max_autocomplete_results')); // Note: We have to this manually because of a bug in DBSearch not checking the user prefs. by default. $oSet->SetShowObsoleteData(utils::ShowObsoleteData()); - while($oObject = $oSet->Fetch()) { + while ($oObject = $oSet->Fetch()) { // Note $oObject finalclass might be different than $sTargetClass $sObjectClass = get_class($oObject); $iObjectId = $oObject->GetKey(); diff --git a/sources/Form/Field/SelectObjectField.php b/sources/Form/Field/SelectObjectField.php index 53cde9a2e..10aa0f042 100644 --- a/sources/Form/Field/SelectObjectField.php +++ b/sources/Form/Field/SelectObjectField.php @@ -26,6 +26,7 @@ use Combodo\iTop\Form\Validator\NotEmptyExtKeyValidator; use DBObjectSet; use DBSearch; use FieldExpression; +use MetaModel; use ScalarExpression; /** @@ -43,10 +44,22 @@ class SelectObjectField extends Field /** @var \DBSearch $oSearch */ protected $oSearch; - /** @var int $iMaximumComboLength */ + /** + * @see \Config 'max_combo_length' + * @var int $iMaximumComboLength + */ protected $iMaximumComboLength; - /** @var int $iMinAutoCompleteChars */ + /** + * @see \Config 'min_autocomplete_chars' + * @var int $iMinAutoCompleteChars + */ protected $iMinAutoCompleteChars; + /** + * @see \Config 'max_autocomplete_results' + * @var int + * @since 3.0.0 + */ + protected $iMaxAutoCompleteResults; /** @var bool $bHierarchical */ protected $bHierarchical; /** @var int $iControlType */ @@ -61,8 +74,9 @@ class SelectObjectField extends Field { parent::__construct($sId, $onFinalizeCallback); $this->oSearch = null; - $this->iMaximumComboLength = null; - $this->iMinAutoCompleteChars = 3; + $this->iMaximumComboLength = MetaModel::GetConfig()->Get('max_combo_length'); + $this->iMinAutoCompleteChars = MetaModel::GetConfig()->Get('min_autocomplete_chars'); + $this->iMaxAutoCompleteResults = MetaModel::GetConfig()->Get('max_autocomplete_results'); $this->bHierarchical = false; $this->iControlType = self::CONTROL_SELECT; $this->sSearchEndpoint = null; @@ -104,6 +118,21 @@ class SelectObjectField extends Field return $this; } + /** + * @see static::$iMaxAutoCompleteResults + * + * @param int $iMaxAutoCompleteResults + * + * @return $this; + * @since 3.0.0 + */ + public function SetMaxAutoCompleteResults(int $iMaxAutoCompleteResults) + { + $this->iMaxAutoCompleteResults = $iMaxAutoCompleteResults; + + return $this; + } + /** * @param bool $bHierarchical * @@ -187,6 +216,16 @@ class SelectObjectField extends Field return $this->iMinAutoCompleteChars; } + /** + * @see static::$iMaxAutoCompleteResults + * @return int + * @since 3.0.0 + */ + public function GetMaxAutoCompleteResults(): int + { + return $this->iMaxAutoCompleteResults; + } + /** * @return bool */ diff --git a/sources/Renderer/Bootstrap/FieldRenderer/BsSelectObjectFieldRenderer.php b/sources/Renderer/Bootstrap/FieldRenderer/BsSelectObjectFieldRenderer.php index d525eb85c..4241962c8 100644 --- a/sources/Renderer/Bootstrap/FieldRenderer/BsSelectObjectFieldRenderer.php +++ b/sources/Renderer/Bootstrap/FieldRenderer/BsSelectObjectFieldRenderer.php @@ -266,7 +266,7 @@ EOF },{ name: '{$this->oField->GetId()}', source: oAutocompleteSource_{$this->oField->GetId()}, - limit: 20, + limit: {$this->oField->GetMaxAutoCompleteResults()}, display: 'name', templates: { suggestion: Handlebars.compile('
{{name}}
'),