From cdb655f69fce0f08564a832468474a8e789dfe1c Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Fri, 24 Mar 2023 17:22:36 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B06042=20Delegate=20history=20creation=20(?= =?UTF-8?q?CMDBChangeOp=20instances)=20to=20the=20new=20AttributeDefinitio?= =?UTF-8?q?n::RecordAttChange=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/attributedef.class.inc.php | 190 +++++++++++++++++++++++++++- core/cmdbobject.class.inc.php | 214 +++----------------------------- 2 files changed, 202 insertions(+), 202 deletions(-) diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index fa17bb90a..6df62934d 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -1325,6 +1325,62 @@ abstract class AttributeDefinition return $sResult; } + /** + * @param \DBObject $oObject + * @param mixed $original + * @param mixed $value + * + * @throws \ArchivedObjectException + * @throws \CoreCannotSaveObjectException + * @throws \CoreException if cannot create object + * @throws \CoreUnexpectedValue + * @throws \CoreWarning + * @throws \MySQLException + * @throws \OQLException + * + * @uses GetChangeRecordAdditionalData + * @uses GetChangeRecordClassName + * + * @since 3.1.0 N°6042 + */ + public function RecordAttChange(DBObject $oObject, $original, $value): void + { + /** @var CMDBChangeOp $oMyChangeOp */ + $oMyChangeOp = MetaModel::NewObject($this->GetChangeRecordClassName()); + $oMyChangeOp->Set("objclass", get_class($oObject)); + $oMyChangeOp->Set("objkey", $oObject->GetKey()); + $oMyChangeOp->Set("attcode", $this->GetCode()); + + $this->GetChangeRecordAdditionalData($oMyChangeOp, $oObject, $original, $value); + + $oMyChangeOp->DBInsertNoReload(); + } + + /** + * Add attribute specific information in the {@link \CMDBChangeOp} instance + * + * @param \CMDBChangeOp $oMyChangeOp + * @param \DBObject $oObject + * @param $original + * @param $value + * + * @return void + * @used-by RecordAttChange + */ + protected function GetChangeRecordAdditionalData(CMDBChangeOp $oMyChangeOp, DBObject $oObject, $original, $value): void + { + $oMyChangeOp->Set("oldvalue", $original); + $oMyChangeOp->Set("newvalue", $value); + } + + /** + * @return string name of the children of {@link \CMDBChangeOp} class to use for the history record + * @used-by RecordAttChange + */ + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeScalar::class; + } /** * Parses a string to find some smart search patterns and build the corresponding search/OQL condition @@ -3398,6 +3454,16 @@ class AttributeBoolean extends AttributeInteger return $value; } + + public function RecordAttChange(DBObject $oObject, $original, $value): void + { + parent::RecordAttChange($oObject, $original ? 1 : 0, $value ? 1 : 0); + } + + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeScalar::class; + } } /** @@ -4186,6 +4252,21 @@ class AttributeEncryptedString extends AttributeString implements iAttributeNoGr return $aValues; } + + protected function GetChangeRecordAdditionalData(CMDBChangeOp $oMyChangeOp, DBObject $oObject, $original, $value): void + { + if (is_null($original)) { + $original = ''; + } + $oMyChangeOp->Set("prevstring", $original); + } + + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeEncrypted::class; + } + + } @@ -4611,6 +4692,22 @@ class AttributeText extends AttributeString $bConvertToPlainText); } } + + protected function GetChangeRecordAdditionalData(CMDBChangeOp $oMyChangeOp, DBObject $oObject, $original, $value): void + { + /** @noinspection PhpConditionCheckedByNextConditionInspection */ + if (false === is_null($original) && ($original instanceof ormCaseLog)) { + $original = $original->GetText(); + } + $oMyChangeOp->Set("prevdata", $original); + } + + protected function GetChangeRecordClassName(): string + { + return ($this->GetFormat() === 'html') + ? CMDBChangeOpSetAttributeHTML::class + : CMDBChangeOpSetAttributeText::class; + } } /** @@ -4648,6 +4745,13 @@ class AttributeLongText extends AttributeText // See mysql_field_len() return 65535 * 1024; // Limited... still 64 MB! } + + protected function GetChangeRecordClassName(): string + { + return ($this->GetFormat() === 'html') + ? CMDBChangeOpSetAttributeHTML::class + : CMDBChangeOpSetAttributeLongText::class; + } } /** @@ -5085,6 +5189,17 @@ class AttributeCaseLog extends AttributeLongText return $oFormField; } + + protected function GetChangeRecordAdditionalData(CMDBChangeOp $oMyChangeOp, DBObject $oObject, $original, $value): void + { + /** @var \ormCaseLog $value */ + $oMyChangeOp->Set("lastentry", $value->GetLatestEntryIndex()); + } + + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeCaseLog::class; + } } /** @@ -8000,8 +8115,7 @@ class AttributeURL extends AttributeString */ public function MakeFormField(DBObject $oObject, $oFormField = null) { - if ($oFormField === null) - { + if ($oFormField === null) { $sFormFieldClass = static::GetFormFieldClass(); $oFormField = new $sFormFieldClass($this->GetCode()); } @@ -8011,6 +8125,11 @@ class AttributeURL extends AttributeString return $oFormField; } + + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeURL::class; + } } /** @@ -8388,7 +8507,18 @@ class AttributeBlob extends AttributeDefinition return utils::IsNotNullOrEmptyString($proposedValue->GetData()) && utils::IsNotNullOrEmptyString($proposedValue->GetFileName()); } + protected function GetChangeRecordAdditionalData(CMDBChangeOp $oMyChangeOp, DBObject $oObject, $original, $value): void + { + if (is_null($original)) { + $original = new ormDocument(); + } + $oMyChangeOp->Set("prevdata", $original); + } + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeBlob::class; + } } /** @@ -9429,7 +9559,27 @@ class AttributeStopWatch extends AttributeDefinition return true; } + public function RecordAttChange(DBObject $oObject, $original, $value): void + { + // Stop watches - record changes for sub items only (they are visible, the rest is not visible) + // + foreach ($this->ListSubItems() as $sSubItemAttCode => $oSubItemAttDef) { + $item_value = $this->GetSubItemValue($oSubItemAttDef->Get('item_code'), $value, $oObject); + $item_original = $this->GetSubItemValue($oSubItemAttDef->Get('item_code'), $original, $oObject); + if ($item_value != $item_original) { + $oMyChangeOp = MetaModel::NewObject(CMDBChangeOpSetAttributeScalar::class); + $oMyChangeOp->Set("objclass", get_class($oObject)); + $oMyChangeOp->Set("objkey", $oObject->GetKey()); + $oMyChangeOp->Set("attcode", $sSubItemAttCode); + + $oMyChangeOp->Set("oldvalue", $item_original); + $oMyChangeOp->Set("newvalue", $item_value); + + $oMyChangeOp->DBInsertNoReload(); + } + } + } } /** @@ -9926,12 +10076,25 @@ class AttributeOneWayPassword extends AttributeDefinition implements iAttributeN if (is_string($proposedValue)) { return utils::IsNotNullOrEmptyString($proposedValue); } + return parent::HasAValue($proposedValue); } return $proposedValue->IsEmpty() === false; } + protected function GetChangeRecordAdditionalData(CMDBChangeOp $oMyChangeOp, DBObject $oObject, $original, $value): void + { + if (is_null($original)) { + $original = ''; + } + $oMyChangeOp->Set("prev_pwd", $original); + } + + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeOneWayPassword::class; + } } // Indexed array having two dimensions @@ -10704,6 +10867,21 @@ HTML; { return '\\Combodo\\iTop\\Form\\Field\\SetField'; } + + public function RecordAttChange(DBObject $oObject, $original, $value): void + { + /** @var \ormSet $original */ + /** @var \ormSet $value */ + parent::RecordAttChange($oObject, + implode(' ', $original->GetValues()), + implode(' ', $value->GetValues()) + ); + } + + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeTagSet::class; + } } /** @@ -13255,7 +13433,15 @@ class AttributeCustomFields extends AttributeDefinition return count($proposedValue->GetValues()) > 0; } + protected function GetChangeRecordAdditionalData(CMDBChangeOp $oMyChangeOp, DBObject $oObject, $original, $value): void + { + $oMyChangeOp->Set("prevdata", json_encode($original->GetValues())); + } + protected function GetChangeRecordClassName(): string + { + return CMDBChangeOpSetAttributeCustomFields::class; + } } class AttributeArchiveFlag extends AttributeBoolean diff --git a/core/cmdbobject.class.inc.php b/core/cmdbobject.class.inc.php index 27d2eea0d..6412000a7 100644 --- a/core/cmdbobject.class.inc.php +++ b/core/cmdbobject.class.inc.php @@ -344,211 +344,25 @@ abstract class CMDBObject extends DBObject * @param $original Original value * @param $value Current value * - * @throws \ArchivedObjectException - * @throws \CoreCannotSaveObjectException - * @throws \CoreException - * @throws \CoreUnexpectedValue - * @throws \CoreWarning - * @throws \MySQLException - * @throws \OQLException + * @throws \Exception + * @since 3.1.0 N°6042 now delegates history record creation to AttributeDefinition + * + * @uses \AttributeDefinition::RecordAttChange() */ protected function RecordAttChange($sAttCode, $original, $value) { $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode); - if ($oAttDef->IsExternalField()) return; - if ($oAttDef->IsLinkSet()) return; - if ($oAttDef->GetTrackingLevel() == ATTRIBUTE_TRACKING_NONE) return; + if ($oAttDef::IsExternalField()) { + return; + } + if ($oAttDef::IsLinkSet()) { + return; + } + if ($oAttDef->GetTrackingLevel() === ATTRIBUTE_TRACKING_NONE) { + return; + } - if ($oAttDef instanceOf AttributeOneWayPassword) - { - // One Way encrypted passwords' history is stored -one way- encrypted - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeOneWayPassword"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - - if (is_null($original)) - { - $original = ''; - } - $oMyChangeOp->Set("prev_pwd", $original); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeEncryptedString) - { - // Encrypted string history is stored encrypted - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeEncrypted"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - - if (is_null($original)) - { - $original = ''; - } - $oMyChangeOp->Set("prevstring", $original); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeBlob) - { - // Data blobs - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - - if (is_null($original)) - { - $original = new ormDocument(); - } - $oMyChangeOp->Set("prevdata", $original); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeStopWatch) - { - // Stop watches - record changes for sub items only (they are visible, the rest is not visible) - // - foreach ($oAttDef->ListSubItems() as $sSubItemAttCode => $oSubItemAttDef) - { - $item_value = $oAttDef->GetSubItemValue($oSubItemAttDef->Get('item_code'), $value, $this); - $item_original = $oAttDef->GetSubItemValue($oSubItemAttDef->Get('item_code'), $original, $this); - - if ($item_value != $item_original) - { - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sSubItemAttCode); - - $oMyChangeOp->Set("oldvalue", $item_original); - $oMyChangeOp->Set("newvalue", $item_value); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - } - } - elseif ($oAttDef instanceOf AttributeCaseLog) - { - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeCaseLog"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - - $oMyChangeOp->Set("lastentry", $value->GetLatestEntryIndex()); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeLongText) - { - // Data blobs - if ($oAttDef->GetFormat() == 'html') - { - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeHTML"); - } - else - { - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeLongText"); - } - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - - if (!is_null($original) && ($original instanceof ormCaseLog)) - { - $original = $original->GetText(); - } - $oMyChangeOp->Set("prevdata", $original); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeText) - { - // Data blobs - if ($oAttDef->GetFormat() == 'html') - { - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeHTML"); - } - else - { - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText"); - } - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - - if (!is_null($original) && ($original instanceof ormCaseLog)) - { - $original = $original->GetText(); - } - $oMyChangeOp->Set("prevdata", $original); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeBoolean) - { - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - $oMyChangeOp->Set("oldvalue", $original ? 1 : 0); - $oMyChangeOp->Set("newvalue", $value ? 1 : 0); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeHierarchicalKey) - { - // Hierarchical keys - // - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - $oMyChangeOp->Set("oldvalue", $original); - $oMyChangeOp->Set("newvalue", $value); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeCustomFields) - { - // Custom fields - // - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeCustomFields"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - $oMyChangeOp->Set("prevdata", json_encode($original->GetValues())); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeURL) - { - // URLs - // - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeURL"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - $oMyChangeOp->Set("oldvalue", $original); - $oMyChangeOp->Set("newvalue", $value); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - elseif ($oAttDef instanceOf AttributeSet) - { - // Tag Set - // - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeTagSet"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - $oMyChangeOp->Set("oldvalue", implode(' ', $original->GetValues())); - $oMyChangeOp->Set("newvalue", implode(' ', $value->GetValues())); - $iId = $oMyChangeOp->DBInsertNoReload(); - } - else - { - // Scalars - // - $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar"); - $oMyChangeOp->Set("objclass", get_class($this)); - $oMyChangeOp->Set("objkey", $this->GetKey()); - $oMyChangeOp->Set("attcode", $sAttCode); - $oMyChangeOp->Set("oldvalue", $original); - $oMyChangeOp->Set("newvalue", $value); - $iId = $oMyChangeOp->DBInsertNoReload(); - } + $oAttDef->RecordAttChange($this, $original, $value); } /**