From 23c0a679149d97d533db8b436f45b3882e2939e8 Mon Sep 17 00:00:00 2001 From: acognet Date: Thu, 20 Aug 2020 16:42:43 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B0580=20-=20Autocomplete=20with=20namesake?= =?UTF-8?q?s.=20Complement=20for=20select?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/ui.extkeywidget.class.inc.php | 29 ++++++++++++++-- core/metamodel.class.php | 42 ++++++++++++++++++++++- core/valuesetdef.class.inc.php | 33 ++++++++++++++---- js/extkeywidget.js | 12 +++++-- setup/compiler.class.inc.php | 27 +++++++++++++++ 5 files changed, 131 insertions(+), 12 deletions(-) diff --git a/application/ui.extkeywidget.class.inc.php b/application/ui.extkeywidget.class.inc.php index 2e40e8844..05a9877a5 100644 --- a/application/ui.extkeywidget.class.inc.php +++ b/application/ui.extkeywidget.class.inc.php @@ -183,6 +183,16 @@ class UIExtKeyWidget array_push($aOptions,$aOption); $oAllowedValues->Rewind(); + $bAddingValue=false; + + $aComplementAttributeSpec = MetaModel::GetComplementAttributeSpec($oAllowedValues->GetClass()); + $sFormatAdditionalField = $aComplementAttributeSpec[0]; + $aAdditionalField = $aComplementAttributeSpec[1]; + + if (count($aAdditionalField)>0) + { + $bAddingValue=true; + } while($oObj = $oAllowedValues->Fetch()) { $aOption=[]; @@ -213,6 +223,15 @@ EOF if ($oObj->IsObsolete()){ $aOption['obsolescence_flag'] ="1"; } + if ($bAddingValue) + { + $aArguments = []; + foreach ($aAdditionalField as $sAdditionalField) + { + array_push($aArguments,$oObj->Get($sAdditionalField)); + } + $aOption['additional_field'] = vsprintf($sFormatAdditionalField, $aArguments); + } array_push($aOptions,$aOption); } $sHTMLValue .= ""; @@ -715,7 +734,6 @@ EOF // 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; $oValuesSet->SetLimit($iMax); @@ -755,7 +773,14 @@ EOF $aJsonMap = array(); foreach ($aValues as $sKey => $aValue) { - $aJsonMap[] = array('value' => $sKey, 'label' => $aValue['label'], 'obsolescence_flag' => $aValue['obsolescence_flag']); + if ($aValue['additional_field'] != '') + { + $aJsonMap[] = array('value' => $sKey, 'label' => $aValue['label'], 'obsolescence_flag' => $aValue['obsolescence_flag'], 'additional_field' => $aValue['additional_field']); + } + else + { + $aJsonMap[] = array('value' => $sKey, 'label' => $aValue['label'], 'obsolescence_flag' => $aValue['obsolescence_flag']); + } } $oP->SetContentType('application/json'); diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 0fd59f580..d658e8c2b 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -748,7 +748,47 @@ abstract class MetaModel return array('%1$s', array($nameRawSpec)); } } - + /** + * @param string $sClass + * + * @return array + * @throws \CoreException + * @throws \DictExceptionMissingString + */ + final static public function GetComplementAttributeSpec($sClass) + { + self::_check_subclass($sClass); + $nameRawSpec = self::$m_aClassParams[$sClass]["name_complement_for_select"]; + if (is_array($nameRawSpec)) + { + $sFormat = Dict::S("Class:$sClass/ComplementForSelect", ''); + if (strlen($sFormat) == 0) + { + // Default to "%1$s %2$s..." + for($i = 1; $i <= count($nameRawSpec); $i++) + { + if (empty($sFormat)) + { + $sFormat .= '%'.$i.'$s'; + } + else + { + $sFormat .= ' %'.$i.'$s'; + } + } + } + return array($sFormat, $nameRawSpec); + } + elseif (empty($nameRawSpec)) + { + return array($sClass, array()); + } + else + { + // string -> attcode + return array('%1$s', array($nameRawSpec)); + } + } /** * Get the friendly name expression for a given class * diff --git a/core/valuesetdef.class.inc.php b/core/valuesetdef.class.inc.php index 0c1c28a84..3da211cee 100644 --- a/core/valuesetdef.class.inc.php +++ b/core/valuesetdef.class.inc.php @@ -349,11 +349,11 @@ class ValueSetObjects extends ValueSetDefinition $this->m_bSort = $bSort; } - public function GetValuesForAutocomplete($aArgs, $sContains = '', $sOperation = 'contains', $aAdditionalFields = array()) + public function GetValuesForAutocomplete($aArgs, $sContains = '', $sOperation = 'contains') { if (!$this->m_bIsLoaded || ($sContains != $this->m_sContains) || ($sOperation != $this->m_sOperation)) { - $this->LoadValuesForAutocomplete($aArgs, $sContains, $sOperation, $aAdditionalFields); + $this->LoadValuesForAutocomplete($aArgs, $sContains, $sOperation); $this->m_bIsLoaded = true; } // The results are already filtered and sorted (on friendly name) @@ -370,7 +370,7 @@ class ValueSetObjects extends ValueSetDefinition * @throws \CoreException * @throws \OQLException */ - protected function LoadValuesForAutocomplete($aArgs, $sContains = '', $sOperation = 'contains', $aAdditionalFields = array()) + protected function LoadValuesForAutocomplete($aArgs, $sContains = '', $sOperation = 'contains') { $this->m_sContains = $sContains; $this->m_sOperation = $sOperation; @@ -386,6 +386,7 @@ class ValueSetObjects extends ValueSetDefinition $oFilter = DBObjectSearch::FromOQL($this->m_sFilterExpr); $oFilter->SetShowObsoleteData(utils::ShowObsoleteData()); } + if (!$oFilter) return false; if (!is_null($this->m_oExtraCondition)) { @@ -399,7 +400,7 @@ class ValueSetObjects extends ValueSetDefinition } } - $oExpression = DBObjectSearch::GetPolymorphicExpression($oFilter->GetClass(), 'friendlyname'); + //$oExpression = DBObjectSearch::GetPolymorphicExpression($oFilter->GetClass(), 'friendlyname'); $sClass = $oFilter->GetClass(); switch ($sOperation) @@ -454,7 +455,16 @@ class ValueSetObjects extends ValueSetDefinition { $aAttToLoad = array($oFilter->GetClassAlias() => array($this->m_sValueAttCode)); } - $aAttToLoad=array_merge($aAttToLoad,$aAdditionalFields); + + $aComplementAttributeSpec = MetaModel::GetComplementAttributeSpec($sClass); + $sFormatAdditionalField = $aComplementAttributeSpec[0]; + $aAdditionalField = $aComplementAttributeSpec[1]; + + if (count($aAdditionalField)>0) + { + $aAttToLoad = array_merge ($aAttToLoad, [$oFilter->GetClassAlias() => $aAdditionalField]); + } + $oObjects->OptimizeColumnLoad($aAttToLoad); while ($oObject = $oObjects->Fetch()) { @@ -475,9 +485,18 @@ class ValueSetObjects extends ValueSetDefinition { $aData['obsolescence_flag']='0'; } - foreach ($aAdditionalFields as $sFieldName) + if (count($aAdditionalField)>0) { - $aData[$sFieldName] = $oObject->Get($sFieldName); + $aArguments = []; + foreach ($aAdditionalField as $sAdditionalField) + { + array_push ($aArguments,$oObject->Get($sAdditionalField)); + } + $aData['additional_field'] = vsprintf($sFormatAdditionalField, $aArguments); + } + else + { + $aData['additional_field']=''; } $this->m_aValues[$oObject->GetKey()] = $aData; } diff --git a/js/extkeywidget.js b/js/extkeywidget.js index a64d14e5e..e3f670135 100644 --- a/js/extkeywidget.js +++ b/js/extkeywidget.js @@ -61,6 +61,10 @@ function ExtKeyWidget(id, sTargetClass, sFilter, sTitle, bSelectMode, oWizHelper { val = item.label; } + if (item.additional_field != undefined ) + { + val = val+'
'+item.additional_field+''; + } return $("
") .append(val); } @@ -145,9 +149,13 @@ function ExtKeyWidget(id, sTargetClass, sFilter, sTitle, bSelectMode, oWizHelper { val = ' '+val; } - val=''+val+''; + if (item.additional_field != undefined ) + { + val = val+'
'+item.additional_field+''; + } + return $("
  • ") - .append(val) + .append(""+val+"") .appendTo(ul); }; diff --git a/setup/compiler.class.inc.php b/setup/compiler.class.inc.php index 028cf2131..bcf6a28a6 100644 --- a/setup/compiler.class.inc.php +++ b/setup/compiler.class.inc.php @@ -1140,6 +1140,33 @@ EOF } } + if ($oAdditionalValueForSelect = $oProperties->GetOptionalElement('complement_for_select')) + { + $oNameAttributes = $oAdditionalValueForSelect->GetUniqueElement('attributes'); + /** @var \DOMNodeList $oAttributes */ + $oAttributes = $oNameAttributes->getElementsByTagName('attribute'); + $aNameAttCodes = array(); + /** @var \MFElement $oAttribute */ + foreach($oAttributes as $oAttribute) + { + $aNameAttCodes[] = $oAttribute->getAttribute('id'); + } + if (count($aNameAttCodes) > 0) + { + // New style... + $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')"; + } + else + { + $sNameAttCode = "''"; + } + } + else + { + $sNameAttCode = "''"; + } + $aClassParams['name_complement_for_select'] = $sNameAttCode; + if ($oUniquenessRules = $oProperties->GetOptionalElement('uniqueness_rules')) { $aUniquenessRules = array();