From a77765ec7ba718c660498a9da09a77dcbe3bd3ca Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Tue, 3 Dec 2024 10:27:45 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B08019=20-=20Enrich=20event=20with=20trans?= =?UTF-8?q?ition=20information?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/cmdbabstract.class.inc.php | 16 +++++++-------- application/datamodel.application.xml | 20 ++++++++++++++++++ core/dbobject.class.php | 28 +++++++++++++------------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 733c42255..2acfd9f41 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -5923,14 +5923,14 @@ JS * * @since 3.1.0 */ - final protected function FireEventCheckToWrite(): void + final protected function FireEventCheckToWrite(?string $sStimulusBeingApplied): void { - $this->FireEvent(EVENT_DB_CHECK_TO_WRITE, ['is_new' => $this->IsNew()]); + $this->FireEvent(EVENT_DB_CHECK_TO_WRITE, ['is_new' => $this->IsNew(), 'stimulus_applied' => $sStimulusBeingApplied]); } - final protected function FireEventBeforeWrite() + final protected function FireEventBeforeWrite(?string $sStimulusBeingApplied) { - $this->FireEvent(EVENT_DB_BEFORE_WRITE, ['is_new' => $this->IsNew()]); + $this->FireEvent(EVENT_DB_BEFORE_WRITE, ['is_new' => $this->IsNew(), 'stimulus_applied' => $sStimulusBeingApplied]); } /** @@ -5942,11 +5942,11 @@ JS * @throws \CoreException * @since 3.1.0 */ - final protected function FireEventAfterWrite(array $aChanges, bool $bIsNew): void + final protected function FireEventAfterWrite(array $aChanges, bool $bIsNew, ?string $sStimulusBeingApplied): void { $this->NotifyAttachedObjectsOnLinkClassModification(); $this->RemoveObjectAwaitingEventDbLinksChanged(get_class($this), $this->GetKey()); - $this->FireEvent(EVENT_DB_AFTER_WRITE, ['is_new' => $bIsNew, 'changes' => $aChanges]); + $this->FireEvent(EVENT_DB_AFTER_WRITE, ['is_new' => $bIsNew, 'changes' => $aChanges, 'stimulus_applied' => $sStimulusBeingApplied]); } ////////////// @@ -6179,9 +6179,9 @@ JS * @inheritDoc * @throws \CoreException */ - final protected function FireEventComputeValues(): void + final protected function FireEventComputeValues(?string $sStimulusBeingApplied): void { - $this->FireEvent(EVENT_DB_COMPUTE_VALUES); + $this->FireEvent(EVENT_DB_COMPUTE_VALUES, ['is_new' => $this->IsNew(), 'stimulus_applied' => $sStimulusBeingApplied]); } /** diff --git a/application/datamodel.application.xml b/application/datamodel.application.xml index 166a031ad..435e1260a 100644 --- a/application/datamodel.application.xml +++ b/application/datamodel.application.xml @@ -238,6 +238,10 @@ The object can be modified.]]> Creation flag boolean + + Life cycle stimulus applied (null if not within a transition) + string + Debug string string @@ -263,6 +267,10 @@ Call $this->AddCheckWarning($sWarningMessage) to display a warning. Creation flag boolean + + Life cycle stimulus applied (null if not within a transition) + string + Debug string string @@ -290,6 +298,10 @@ The modifications can be propagated to other objects.]]> array + + Life cycle stimulus applied (null if not within a transition) + string + Debug string string @@ -420,6 +432,14 @@ The only action allowed is to deny transitions with $this->DenyTransition($sTran The object inserted DBObject + + Creation flag + boolean + + + Life cycle stimulus applied (null if not within a transition) + string + Debug string string diff --git a/core/dbobject.class.php b/core/dbobject.class.php index b4f8c1c2a..971bfa3bb 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -212,7 +212,7 @@ abstract class DBObject implements iDisplay private $aEventListeners = []; private array $aAllowedTransitions = []; - private bool $bStimulusBeingApplied = false; + private ?string $sStimulusBeingApplied = null; /** * DBObject constructor. @@ -1208,7 +1208,7 @@ abstract class DBObject implements iDisplay if ($aCallInfo["function"] != "ComputeValues") continue; return; //skip! } - $this->FireEventComputeValues(); + $this->FireEventComputeValues($this->sStimulusBeingApplied); $oKPI = new ExecutionKPI(); $this->ComputeValues(); $oKPI->ComputeStatsForExtension($this, 'ComputeValues'); @@ -2671,7 +2671,7 @@ abstract class DBObject implements iDisplay // Ultimate check - ensure DB integrity $this->SetReadOnly('No modification allowed during CheckToCreate'); - $this->FireEventCheckToWrite(); + $this->FireEventCheckToWrite($this->sStimulusBeingApplied); $this->SetReadWrite(); $oKPI = new ExecutionKPI(); @@ -3400,7 +3400,7 @@ abstract class DBObject implements iDisplay $this->OnInsert(); $oKPI->ComputeStatsForExtension($this, 'OnInsert'); - $this->FireEventBeforeWrite(); + $this->FireEventBeforeWrite(null); // If not automatically computed, then check that the key is given by the caller if (!MetaModel::IsAutoIncrementKey($sRootClass)) { @@ -3535,7 +3535,7 @@ abstract class DBObject implements iDisplay */ protected function PostInsertActions(): void { - $this->FireEventAfterWrite([], true); + $this->FireEventAfterWrite([], true, null); $oKPI = new ExecutionKPI(); $this->AfterInsert(); $oKPI->ComputeStatsForExtension($this, 'AfterInsert'); @@ -3643,7 +3643,7 @@ abstract class DBObject implements iDisplay $this->OnUpdate(); $oKPI->ComputeStatsForExtension($this, 'OnUpdate'); - $this->FireEventBeforeWrite(); + $this->FireEventBeforeWrite($this->sStimulusBeingApplied); // Freeze the changes at this point $this->InitPreviousValuesForUpdatedAttributes(); @@ -3854,7 +3854,7 @@ abstract class DBObject implements iDisplay */ protected function PostUpdateActions(array $aChanges): void { - $this->FireEventAfterWrite($aChanges, false); + $this->FireEventAfterWrite($aChanges, false, $this->sStimulusBeingApplied); $oKPI = new ExecutionKPI(); $this->AfterUpdate(); $oKPI->ComputeStatsForExtension($this, 'AfterUpdate'); @@ -3866,9 +3866,9 @@ abstract class DBObject implements iDisplay $this->ActivateOnObjectUpdateTriggersForTargetObjects(); $sClass = get_class($this); - if ($this->bStimulusBeingApplied) + if (utils::IsNotNullOrEmptyString($this->sStimulusBeingApplied)) { - $this->bStimulusBeingApplied = false; + $this->sStimulusBeingApplied = null; $sStateAttCode = MetaModel::GetStateAttributeCode($sClass); $sPreviousState = $this->m_aPreviousValuesForUpdatedAttributes[$sStateAttCode]; // Change state triggers... @@ -4604,7 +4604,7 @@ abstract class DBObject implements iDisplay } if ($bSuccess) { - $this->bStimulusBeingApplied = true; + $this->sStimulusBeingApplied = $sStimulusCode; // Stop watches foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef) { @@ -6619,7 +6619,7 @@ abstract class DBObject implements iDisplay * @return void * @since 3.1.0 */ - protected function FireEventCheckToWrite(): void + protected function FireEventCheckToWrite(?string $sStimulusBeingApplied): void { } @@ -6627,7 +6627,7 @@ abstract class DBObject implements iDisplay * @return void * @since 3.1.0 */ - protected function FireEventBeforeWrite() + protected function FireEventBeforeWrite(?string $sStimulusBeingApplied) { } @@ -6637,7 +6637,7 @@ abstract class DBObject implements iDisplay * @return void * @since 3.1.0 */ - protected function FireEventAfterWrite(array $aChanges, bool $bIsNew): void + protected function FireEventAfterWrite(array $aChanges, bool $bIsNew, ?string $sStimulusBeingApplied): void { } @@ -6675,7 +6675,7 @@ abstract class DBObject implements iDisplay * @return void * @since 3.1.0 */ - protected function FireEventComputeValues(): void + protected function FireEventComputeValues(?string $sStimulusBeingApplied): void { }