From 40355cb2d07eb6b85409c63f41a9428e442af86e Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 28 Sep 2018 12:07:46 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B0917:=20Fix=20refresh=20edit=20screen,=20?= =?UTF-8?q?avoid=20storing=20bad=20values=20in=20db?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/cmdbabstract.class.inc.php | 1 - application/query.class.inc.php | 32 ++++++++++++++++++++ application/wizardhelper.class.inc.php | 8 +++++ core/attributedef.class.inc.php | 42 ++++++++++++++++---------- core/ormset.class.inc.php | 2 -- core/trigger.class.inc.php | 32 ++++++++++++++++++++ 6 files changed, 98 insertions(+), 19 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 80e0efbe5..0683dad00 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -3306,7 +3306,6 @@ EOF { $oSet = new ormSet(get_class($this), $sAttCode); } - $oSet->SetValues($value['orig_value']); $oSet->ApplyDelta($value); $this->Set($sAttCode, $oSet); break; diff --git a/application/query.class.inc.php b/application/query.class.inc.php index 5d8831e1f..38d5ca7a1 100644 --- a/application/query.class.inc.php +++ b/application/query.class.inc.php @@ -137,6 +137,38 @@ class QueryOQL extends Query return $aFieldsMap; } + public function ComputeValues() + { + parent::ComputeValues(); + + // Remove unwanted attribute codes + $aChanges = $this->ListChanges(); + if (isset($aChanges['fields'])) + { + $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'fields'); + $aArgs = array('this' => $this); + $aAllowedValues = $oAttDef->GetAllowedValues($aArgs); + + /** @var \ormSet $oValue */ + $oValue = $this->Get('fields'); + $aValues = $oValue->GetValues(); + $bChanged = false; + foreach($aValues as $key => $sValue) + { + if (!isset($aAllowedValues[$sValue])) + { + unset($aValues[$key]); + $bChanged = true; + } + } + if ($bChanged) + { + $oValue->SetValues($aValues); + $this->Set('fields', $oValue); + } + } + } + } ?> diff --git a/application/wizardhelper.class.inc.php b/application/wizardhelper.class.inc.php index 379cd58a2..0f6def805 100644 --- a/application/wizardhelper.class.inc.php +++ b/application/wizardhelper.class.inc.php @@ -174,6 +174,14 @@ class WizardHelper } $oObj->Set($sAttCode, $value); } + else if ($oAttDef instanceof AttributeSet) // AttributeDate is derived from AttributeDateTime + { + $value = json_decode($value, true); + $oTagSet = new ormTagSet(get_class($oObj), $sAttCode); + $oTagSet->SetValues($value['orig_value']); + $oTagSet->ApplyDelta($value); + $oObj->Set($sAttCode, $oTagSet); + } else { $oObj->Set($sAttCode, $value); diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index a4a9159bf..a665ff651 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -6966,9 +6966,13 @@ class AttributeTagSet extends AttributeSet $oTagSet = new ormTagSet(MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()), $this->GetCode(), $this->GetMaxItems()); if (is_string($proposedValue) && !empty($proposedValue)) { - $proposedValue = trim("$proposedValue"); - $aTagCodes = $this->FromStringToArray($proposedValue); - $oTagSet->SetValues($aTagCodes); + $sJsonFromWidget = json_decode($proposedValue, true); + if (is_null($sJsonFromWidget)) + { + $proposedValue = trim("$proposedValue"); + $aTagCodes = $this->FromStringToArray($proposedValue); + $oTagSet->SetValues($aTagCodes); + } } elseif ($proposedValue instanceof ormTagSet) { @@ -9434,7 +9438,7 @@ abstract class AttributeSet extends AttributeDBFieldVoid ]; } $aJson['possible_values'] = $aSetKeyValData; - + $aRemoved = array(); if (is_null($oValue)) { $aJson['partial_values'] = array(); @@ -9456,13 +9460,15 @@ abstract class AttributeSet extends AttributeDBFieldVoid { if (!isset($aAllowedValues[$value])) { + // Remove unwanted values + $aRemoved[] = $value; unset($aOrigValues[$key]); } } $aJson['orig_value'] = array_values($aOrigValues); } $aJson['added'] = array(); - $aJson['removed'] = array(); + $aJson['removed'] = $aRemoved; $iMaxTags = $this->GetMaxItems(); $aJson['max_items_allowed'] = $iMaxTags; @@ -9810,21 +9816,25 @@ class AttributeClassAttCodeSet extends AttributeSet $aInvalidAttCodes = array(); if (is_string($proposedValue) && !empty($proposedValue)) { - $proposedValue = trim($proposedValue); - $aValues = array(); - foreach(explode(',', $proposedValue) as $sValue) + $aJsonFromWidget = json_decode($proposedValue, true); + if (is_null($aJsonFromWidget)) { - $sAttCode = trim($sValue); - if (empty($aAllowedAttributes) || isset($aAllowedAttributes[$sAttCode])) + $proposedValue = trim($proposedValue); + $aValues = array(); + foreach(explode(',', $proposedValue) as $sValue) { - $aValues[$sAttCode] = $sAttCode; - } - else - { - $aInvalidAttCodes[] = $sAttCode; + $sAttCode = trim($sValue); + if (empty($aAllowedAttributes) || isset($aAllowedAttributes[$sAttCode])) + { + $aValues[$sAttCode] = $sAttCode; + } + else + { + $aInvalidAttCodes[] = $sAttCode; + } } + $oSet->SetValues($aValues); } - $oSet->SetValues($aValues); } elseif ($proposedValue instanceof ormSet) { diff --git a/core/ormset.class.inc.php b/core/ormset.class.inc.php index 490e0fcee..4578bbdf0 100644 --- a/core/ormset.class.inc.php +++ b/core/ormset.class.inc.php @@ -161,8 +161,6 @@ class ormSet { $aValues = array_merge($this->aPreserved, $this->aAdded); - sort($aValues); - return $aValues; } diff --git a/core/trigger.class.inc.php b/core/trigger.class.inc.php index e8b7bc60b..1853ac35d 100644 --- a/core/trigger.class.inc.php +++ b/core/trigger.class.inc.php @@ -495,6 +495,38 @@ class TriggerOnObjectUpdate extends TriggerOnObject return false; } + public function ComputeValues() + { + parent::ComputeValues(); + + // Remove unwanted attribute codes + $aChanges = $this->ListChanges(); + if (isset($aChanges['target_attcodes'])) + { + $oAttDef = MetaModel::GetAttributeDef(get_class($this), 'target_attcodes'); + $aArgs = array('this' => $this); + $aAllowedValues = $oAttDef->GetAllowedValues($aArgs); + + /** @var \ormSet $oValue */ + $oValue = $this->Get('target_attcodes'); + $aValues = $oValue->GetValues(); + $bChanged = false; + foreach($aValues as $key => $sValue) + { + if (!isset($aAllowedValues[$sValue])) + { + unset($aValues[$key]); + $bChanged = true; + } + } + if ($bChanged) + { + $oValue->SetValues($aValues); + $this->Set('target_attcodes', $oValue); + } + } + } + } /**