N°2293 Object update hooks now have access to object changes

* new \cmdbAbstractObject::$m_aChanges for \iApplicationObjectExtension::OnDBUpdate calls
* calling ListChanges() from within \DBObject::AfterUpdate will now give the right informations
* update PHPDoc in iApplicationObjectExtension
This commit is contained in:
Pierre Goiffon
2019-08-23 10:12:36 +02:00
parent 58402cdda8
commit c97fd63e6d
3 changed files with 31 additions and 20 deletions

View File

@@ -344,15 +344,16 @@ interface iApplicationUIExtension
interface iApplicationObjectExtension
{
/**
* Invoked to determine wether an object has been modified in memory
* Invoked to determine whether an object has been modified in memory
*
* The GUI calls this verb to determine the message that will be displayed to the end-user.
* Anyhow, this API can be called in other contexts such as the CSV import tool.
*
* If the extension returns false, then the framework will perform the usual evaluation.
* Otherwise, the answer is definitively "yes, the object has changed".
*
* @param DBObject $oObject The target object
*
* @param \cmdbAbstractObject $oObject The target object
*
* @return boolean True if something has changed for the target object
*/
public function OnIsModified($oObject);
@@ -362,8 +363,9 @@ interface iApplicationObjectExtension
*
* The GUI calls this verb and reports any issue.
* Anyhow, this API can be called in other contexts such as the CSV import tool.
*
* @param DBObject $oObject The target object
*
* @param \cmdbAbstractObject $oObject The target object
*
* @return string[] A list of errors message. An error message is made of one line and it can be displayed to the end-user.
*/
public function OnCheckToWrite($oObject);
@@ -372,21 +374,23 @@ interface iApplicationObjectExtension
* Invoked to determine wether an object can be deleted from the database
*
* The GUI calls this verb and stops the deletion process if any issue is reported.
*
* Please not that it is not possible to cascade deletion by this mean: only stopper issues can be handled.
*
* @param DBObject $oObject The target object
*
* Please not that it is not possible to cascade deletion by this mean: only stopper issues can be handled.
*
* @param \cmdbAbstractObject $oObject The target object
*
* @return string[] A list of errors message. An error message is made of one line and it can be displayed to the end-user.
*/
public function OnCheckToDelete($oObject);
/**
* Invoked when an object is updated into the database
*
* The method is called right <b>after</b> the object has been written to the database.
*
* @param DBObject $oObject The target object
* Invoked when an object is updated into the database. The method is called right <b>after</b> the object has been written to the database.
*
* Changes made to the object can be get using {@link \cmdbAbstractObject::$m_aChanges}
*
* @param \cmdbAbstractObject $oObject The target object
* @param CMDBChange|null $oChange A change context. Since 2.0 it is fine to ignore it, as the framework does maintain this information once for all the changes made within the current page
*
* @return void
*/
public function OnDBUpdate($oObject, $oChange = null);
@@ -395,9 +399,10 @@ interface iApplicationObjectExtension
* Invoked when an object is created into the database
*
* The method is called right <b>after</b> the object has been written to the database.
*
* @param DBObject $oObject The target object
*
* @param \cmdbAbstractObject $oObject The target object
* @param CMDBChange|null $oChange A change context. Since 2.0 it is fine to ignore it, as the framework does maintain this information once for all the changes made within the current page
*
* @return void
*/
public function OnDBInsert($oObject, $oChange = null);

View File

@@ -54,8 +54,10 @@ require_once(APPROOT.'sources/application/search/criterionconversion/criterionto
abstract class cmdbAbstractObject extends CMDBObject implements iDisplay
{
protected $m_iFormId; // The ID of the form used to edit the object (when in edition mode !)
static $iGlobalFormId = 1;
protected static $iGlobalFormId = 1;
protected $aFieldsMap;
/** @var array attname => currentvalue Persists changes for {@link DBUpdate} */
protected $m_aChanges;
/**
* If true, bypass IsActionAllowedOnAttribute when writing this object
@@ -3731,6 +3733,8 @@ EOF
public function DBUpdate()
{
$this->m_aChanges = $this->ListChanges();
$res = parent::DBUpdate();
$this->SetWarningsAsSessionMessages('update');
@@ -3763,6 +3767,8 @@ EOF
unset($aUpdateReentrance[$sKey]);
}
$this->m_aChanges = array();
return $res;
}

View File

@@ -3148,12 +3148,12 @@ abstract class DBObject implements iDisplay
$this->DBWriteLinks();
$this->WriteExternalAttributes();
$this->AfterUpdate();
$this->m_bDirty = false;
$this->m_aTouchedAtt = array();
$this->m_aModifiedAtt = array();
$this->AfterUpdate();
// Reload to get the external attributes
if ($bHasANewExternalKeyValue)
{
@@ -3969,7 +3969,7 @@ abstract class DBObject implements iDisplay
}
/**
* this method is called after the object is updated into DB.
* This method is called after the object is updated into DB. You can get changes by calling {@link ListChanges}.
*
* @overwritable-hook You can extend this method in order to provide your own logic.
*/