From 25b1a0d8af5ac58c804332d420a046bcf6335dad Mon Sep 17 00:00:00 2001 From: Guillaume Lajarige Date: Tue, 16 Jan 2018 15:39:44 +0000 Subject: [PATCH] =?UTF-8?q?(Retrofit=20from=20trunk)=20N=C2=B0984=20Portal?= =?UTF-8?q?:=20Fix=20autocomplete=20field=20reset=20when=20changing=20valu?= =?UTF-8?q?e=20of=20parent=20field=20in=20request=20templates.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVN:2.4[5262] --- .../src/forms/objectformmanager.class.inc.php | 23 +++++--------- .../field/selectobjectfield.class.inc.php | 30 +++++++++++++++++-- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php b/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php index 785958ef57..835c49d9fe 100644 --- a/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php +++ b/datamodels/2.x/itop-portal-base/portal/src/forms/objectformmanager.class.inc.php @@ -19,7 +19,6 @@ namespace Combodo\iTop\Portal\Form; -use Combodo\iTop\Form\Field\Field; use \Exception; use \Silex\Application; use \utils; @@ -41,6 +40,7 @@ use \AttributeDateTime; use \AttachmentPlugIn; use \Combodo\iTop\Form\FormManager; use \Combodo\iTop\Form\Form; +use \Combodo\iTop\Form\Field\Field; use \Combodo\iTop\Form\Field\FileUploadField; use \Combodo\iTop\Form\Field\LabelField; use \Combodo\iTop\Portal\Helper\ApplicationHelper; @@ -715,20 +715,7 @@ class ObjectFormManager extends FormManager { // Note: We can't do this in AttributeExternalKey::MakeFormField() in the Field::SetOnFinalizeCallback() because at this point we have no information about the portal scope and ignore_silos flag, hence it always applies silos. // As a workaround we have to manually check if the field's current value is among the scope - - if(!$oField->GetReadOnly()) - { - /** @var DBObjectSearch $oValuesScope */ - $oValuesScope = $oField->GetSearch()->DeepClone(); - $oBinaryExp = new BinaryExpression(new FieldExpression('id', $oValuesScope->GetClassAlias()), '=', new ScalarExpression($oField->GetCurrentValue())); - $oValuesScope->AddConditionExpression($oBinaryExp); - $oValuesSet = new DBObjectSet($oValuesScope); - - if($oValuesSet->Count() === 0) - { - $oField->SetCurrentValue(null); - } - } + $oField->VerifyCurrentValue(); } // - Field that require processing on their subfields if (in_array(get_class($oField), array('Combodo\\iTop\\Form\\Field\\SubFormField'))) @@ -758,6 +745,11 @@ class ObjectFormManager extends FormManager $oCustomField->SetSearchEndpoint($sSearchEndpoint); } } + // - Field that require to check if the current value is among allowed ones + if (in_array(get_class($oCustomField), array('Combodo\\iTop\\Form\\Field\\SelectObjectField'))) + { + $oCustomField->VerifyCurrentValue(); + } } } } @@ -1204,5 +1196,4 @@ class ObjectFormManager extends FormManager $oAttachment->DBDelete(); } } - } diff --git a/sources/form/field/selectobjectfield.class.inc.php b/sources/form/field/selectobjectfield.class.inc.php index b74318aaa3..7a81a3d11e 100644 --- a/sources/form/field/selectobjectfield.class.inc.php +++ b/sources/form/field/selectobjectfield.class.inc.php @@ -19,8 +19,12 @@ namespace Combodo\iTop\Form\Field; -use \Closure; -use \DBSearch; +use Closure; +use DBSearch; +use DBObjectSet; +use BinaryExpression; +use FieldExpression; +use ScalarExpression; use Combodo\iTop\Form\Validator\NotEmptyExtKeyValidator; /** @@ -148,4 +152,26 @@ class SelectObjectField extends Field { return $this->sSearchEndpoint; } + + /** + * Resets current value is not among allowed ones. + * By default, reset is done ONLY when the field is not read-only. + * + * @param boolean $bAlways Set to true to verify even when the field is read-only. + */ + public function VerifyCurrentValue($bAlways = false) + { + if(!$this->GetReadOnly() || $bAlways) + { + $oValuesScope = $this->GetSearch()->DeepClone(); + $oBinaryExp = new BinaryExpression(new FieldExpression('id', $oValuesScope->GetClassAlias()), '=', new ScalarExpression($this->currentValue)); + $oValuesScope->AddConditionExpression($oBinaryExp); + $oValuesSet = new DBObjectSet($oValuesScope); + + if($oValuesSet->Count() === 0) + { + $this->currentValue = null; + } + } + } }