N°917: Fix refresh edit screen, avoid storing bad values in db

This commit is contained in:
Eric
2018-09-28 12:07:46 +02:00
parent b52aaaadf2
commit 40355cb2d0
6 changed files with 98 additions and 19 deletions

View File

@@ -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;

View File

@@ -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);
}
}
}
}
?>

View File

@@ -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);

View File

@@ -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)
{

View File

@@ -161,8 +161,6 @@ class ormSet
{
$aValues = array_merge($this->aPreserved, $this->aAdded);
sort($aValues);
return $aValues;
}

View File

@@ -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);
}
}
}
}
/**