From 538e9c11e88e8b02c9837f3f156f5277f79f6c0e Mon Sep 17 00:00:00 2001 From: Guillaume Lajarige Date: Thu, 24 Aug 2017 13:24:46 +0000 Subject: [PATCH] =?UTF-8?q?N=C2=B0779=20Date=20format=20handling=20in=20Li?= =?UTF-8?q?nkedSetIndirect=20was=20causing=20fatal=20error=20on=20object?= =?UTF-8?q?=20edition.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVN:trunk[4875] --- application/cmdbabstract.class.inc.php | 257 ++++++++++++++++--------- application/wizardhelper.class.inc.php | 12 ++ 2 files changed, 178 insertions(+), 91 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index caf52c600..3f5388ed9 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -3248,97 +3248,7 @@ EOF $aValues = array(); foreach($aAttList as $sAttCode) { - $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode); - if ($oAttDef->GetEditClass() == 'Document') - { - $value = array('fcontents' => utils::ReadPostedDocument("attr_{$sFormPrefix}{$sAttCode}", 'fcontents')); - } - elseif ($oAttDef->GetEditClass() == 'Image') - { - $oImage = utils::ReadPostedDocument("attr_{$sFormPrefix}{$sAttCode}", 'fcontents'); - $aSize = utils::GetImageSize($oImage->GetData()); - $oImage = utils::ResizeImageToFit($oImage, $aSize[0], $aSize[1], $oAttDef->Get('storage_max_width'), $oAttDef->Get('storage_max_height')); - $aOtherData = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data'); - if (is_array($aOtherData)) - { - $value = array('fcontents' => $oImage, 'remove' => $aOtherData['remove']); - } - else - { - $value = null; - } - } - elseif ($oAttDef->GetEditClass() == 'RedundancySetting') - { - $value = $oAttDef->ReadValueFromPostedForm($sFormPrefix); - } - elseif ($oAttDef->GetEditClass() == 'CustomFields') - { - $value = $oAttDef->ReadValueFromPostedForm($this, $sFormPrefix); - } - else if ($oAttDef->GetEditClass() == 'LinkedSet') - { - $aRawToBeCreated = json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbc", '{}', 'raw_data'), true); - $aToBeCreated = array(); - foreach($aRawToBeCreated as $aData) - { - $sSubFormPrefix = $aData['formPrefix']; - $sObjClass = isset($aData['class']) ? $aData['class'] : $oAttDef->GetLinkedClass(); - $aObjData = array(); - foreach($aData as $sKey => $value) - { - if (preg_match("/^attr_$sSubFormPrefix(.*)$/", $sKey, $aMatches)) - { - $aObjData[$aMatches[1]] = $value; - } - } - $aToBeCreated[] = array('class' => $sObjClass, 'data' => $aObjData); - } - - $aRawToBeModified = json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbm", '{}', 'raw_data'), true); - $aToBeModified = array(); - foreach($aRawToBeModified as $iObjKey => $aData) - { - $sSubFormPrefix = $aData['formPrefix']; - $aObjData = array(); - foreach($aData as $sKey => $value) - { - if (preg_match("/^attr_$sSubFormPrefix(.*)$/", $sKey, $aMatches)) - { - $aObjData[$aMatches[1]] = $value; - } - } - $aToBeModified[$iObjKey] = array('data' => $aObjData); - } - - $value = array( - 'to_be_created' => $aToBeCreated, - 'to_be_modified' => $aToBeModified, - 'to_be_deleted' => json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbd", '[]', 'raw_data'), true), - 'to_be_added' => json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tba", '[]', 'raw_data'), true), - 'to_be_removed' => json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbr", '[]', 'raw_data'), true) - ); - } - else if ($oAttDef instanceof AttributeDateTime) // AttributeDate is derived from AttributeDateTime - { - $value = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data'); - if ($value != null) - { - $oDate = $oAttDef->GetFormat()->Parse($value); - if ($oDate instanceof DateTime) - { - $value = $oDate->format($oAttDef->GetInternalFormat()); - } - else - { - $value = null; - } - } - } - else - { - $value = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data'); - } + $value = $this->PrepareValueFromPostedForm($sFormPrefix, $sAttCode); if (!is_null($value)) { $aValues[$sAttCode] = $value; @@ -3366,6 +3276,171 @@ EOF return $aErrors; } + /** + * @param string $sFormPrefix + * @param string $sAttCode + * @param string $sClass Optional parameter, host object's class for the $sAttCode + * @param array $aPostedData Optional parameter, used through recursive calls + * @return array|null + */ + protected function PrepareValueFromPostedForm($sFormPrefix, $sAttCode, $sClass = null, $aPostedData = null) + { + if($sClass === null) + { + $sClass = get_class($this); + } + + $value = null; + + $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode); + if ($oAttDef->GetEditClass() == 'Document') + { + $value = array('fcontents' => utils::ReadPostedDocument("attr_{$sFormPrefix}{$sAttCode}", 'fcontents')); + } + elseif ($oAttDef->GetEditClass() == 'Image') + { + $oImage = utils::ReadPostedDocument("attr_{$sFormPrefix}{$sAttCode}", 'fcontents'); + $aSize = utils::GetImageSize($oImage->GetData()); + $oImage = utils::ResizeImageToFit($oImage, $aSize[0], $aSize[1], $oAttDef->Get('storage_max_width'), $oAttDef->Get('storage_max_height')); + $aOtherData = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data'); + if (is_array($aOtherData)) + { + $value = array('fcontents' => $oImage, 'remove' => $aOtherData['remove']); + } + else + { + $value = null; + } + } + elseif ($oAttDef->GetEditClass() == 'RedundancySetting') + { + $value = $oAttDef->ReadValueFromPostedForm($sFormPrefix); + } + elseif ($oAttDef->GetEditClass() == 'CustomFields') + { + $value = $oAttDef->ReadValueFromPostedForm($this, $sFormPrefix); + } + else if ($oAttDef->GetEditClass() == 'LinkedSet') + { + /** @var AttributeLinkedSet $oAttDef */ + $aRawToBeCreated = json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbc", '{}', 'raw_data'), true); + $aToBeCreated = array(); + foreach($aRawToBeCreated as $aData) + { + $sSubFormPrefix = $aData['formPrefix']; + $sObjClass = isset($aData['class']) ? $aData['class'] : $oAttDef->GetLinkedClass(); + $aObjData = array(); + foreach($aData as $sKey => $value) + { + if (preg_match("/^attr_$sSubFormPrefix(.*)$/", $sKey, $aMatches)) + { + $sLinkClass = $oAttDef->GetLinkedClass(); + if($oAttDef->IsIndirect()) + { + $oLinkAttDef = MetaModel::GetAttributeDef($sLinkClass, $aMatches[1]); + // Recursing over n:n link datetime attributes + // Note: We might need to do it with other attribute types, like Document or redundancy setting. + if($oLinkAttDef instanceof AttributeDateTime) + { + $aObjData[$aMatches[1]] = $this->PrepareValueFromPostedForm($sSubFormPrefix, $aMatches[1], $sLinkClass, $aData); + } + else + { + $aObjData[$aMatches[1]] = $value; + } + } + else + { + $aObjData[$aMatches[1]] = $value; + } + } + } + $aToBeCreated[] = array('class' => $sObjClass, 'data' => $aObjData); + } + + $aRawToBeModified = json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbm", '{}', 'raw_data'), true); + $aToBeModified = array(); + foreach($aRawToBeModified as $iObjKey => $aData) + { + $sSubFormPrefix = $aData['formPrefix']; + $aObjData = array(); + foreach($aData as $sKey => $value) + { + if (preg_match("/^attr_$sSubFormPrefix(.*)$/", $sKey, $aMatches)) + { + $sLinkClass = $oAttDef->GetLinkedClass(); + if($oAttDef->IsIndirect()) + { + $oLinkAttDef = MetaModel::GetAttributeDef($sLinkClass, $aMatches[1]); + // Recursing over n:n link datetime attributes + // Note: We might need to do it with other attribute types, like Document or redundancy setting. + if($oLinkAttDef instanceof AttributeDateTime) + { + $aObjData[$aMatches[1]] = $this->PrepareValueFromPostedForm($sSubFormPrefix, $aMatches[1], $sLinkClass, $aData); + } + else + { + $aObjData[$aMatches[1]] = $value; + } + } + else + { + $aObjData[$aMatches[1]] = $value; + } + } + } + $aToBeModified[$iObjKey] = array('data' => $aObjData); + } + + $value = array( + 'to_be_created' => $aToBeCreated, + 'to_be_modified' => $aToBeModified, + 'to_be_deleted' => json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbd", '[]', 'raw_data'), true), + 'to_be_added' => json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tba", '[]', 'raw_data'), true), + 'to_be_removed' => json_decode(utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbr", '[]', 'raw_data'), true) + ); + } + else if ($oAttDef instanceof AttributeDateTime) // AttributeDate is derived from AttributeDateTime + { + // Retrieving value from array when present (means what we are in a recursion) + if($aPostedData !== null && isset($aPostedData['attr_'.$sFormPrefix.$sAttCode])) + { + $value = $aPostedData['attr_'.$sFormPrefix.$sAttCode]; + } + else + { + $value = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data'); + } + + if ($value != null) + { + $oDate = $oAttDef->GetFormat()->Parse($value); + if ($oDate instanceof DateTime) + { + $value = $oDate->format($oAttDef->GetInternalFormat()); + } + else + { + $value = null; + } + } + } + else + { + // Retrieving value from array when present (means what we are in a recursion) + if($aPostedData !== null && isset($aPostedData['attr_'.$sFormPrefix.$sAttCode])) + { + $value = $aPostedData['attr_'.$sFormPrefix.$sAttCode]; + } + else + { + $value = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data'); + } + } + + return $value; + } + /** * Updates the object from a given page argument */ diff --git a/application/wizardhelper.class.inc.php b/application/wizardhelper.class.inc.php index 79bcbd0a5..7cf924dad 100644 --- a/application/wizardhelper.class.inc.php +++ b/application/wizardhelper.class.inc.php @@ -84,6 +84,18 @@ class WizardHelper $oTargetObj = MetaModel::GetObject($sLinkedAttDef->GetTargetClass(), $aLinkedObject[$sLinkedAttCode]); $oLinkedObj->Set($sLinkedAttCode, $oTargetObj); } + elseif($sLinkedAttDef instanceof AttributeDateTime) + { + $sDate = $aLinkedObject[$sLinkedAttCode]; + if($sDate !== null && $sDate !== '') + { + $oDateTimeFormat = AttributeDateTime::GetFormat(); + $oDate = $oDateTimeFormat->Parse($sDate); + $sDate = $oDate->format('Y-m-d H:i:s'); + } + + $oLinkedObj->Set($sLinkedAttCode, $sDate); + } else { $oLinkedObj->Set($sLinkedAttCode, $aLinkedObject[$sLinkedAttCode]);