From a0b191f3a3a88ee8dc7d80eb03c4a6ea10f29aed Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Tue, 16 Apr 2013 12:58:23 +0000 Subject: [PATCH] Fix for the "Notifications" tab: use the polymorphism to let each trigger determine which object is "In Scope" and thus can potentially have notifications related to it SVN:2.0[2689] --- application/cmdbabstract.class.inc.php | 20 +++++++++++++------- core/trigger.class.inc.php | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 81205a60b..b2e470032 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -406,18 +406,24 @@ abstract class cmdbAbstractObject extends CMDBObject implements iDisplay // Display Notifications after the other tabs since this tab disappears in edition if (!$bEditMode) { - // Get the actual class of the current object - // And look for triggers referring to it + // Look for any trigger that considers this object as "In Scope" // If any trigger has been found then display a tab with notifications // - $sClass = get_class($this); - $sClassList = implode("', '", MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL)); - $oTriggerSet = new CMDBObjectSet(DBObjectSearch::FromOQL("SELECT TriggerOnObject AS T WHERE T.target_class IN ('$sClassList')")); - if ($oTriggerSet->Count() > 0) + $oTriggerSet = new CMDBObjectSet(new DBObjectSearch('Trigger')); + $aTriggers = array(); + while($oTrigger = $oTriggerSet->Fetch()) + { + if($oTrigger->IsInScope($this)) + { + $aTriggers[] = $oTrigger->GetKey(); + } + } + if (count($aTriggers) > 0) { // Display notifications regarding the object $iId = $this->GetKey(); - $oNotifSearch = DBObjectSearch::FromOQL("SELECT EventNotificationEmail AS Ev JOIN TriggerOnObject AS T ON Ev.trigger_id = T.id WHERE T.target_class IN ('$sClassList') AND Ev.object_id = $iId"); + $sTriggersList = implode(',', $aTriggers); + $oNotifSearch = DBObjectSearch::FromOQL("SELECT EventNotificationEmail AS Ev JOIN Trigger AS T ON Ev.trigger_id = T.id WHERE T.id IN ($sTriggersList) AND Ev.object_id = $iId"); $oNotifSet = new DBObjectSet($oNotifSearch); $sCount = ($oNotifSet->Count() > 0) ? ' ('.$oNotifSet->Count().')' : ''; $oPage->SetCurrentTab(Dict::S('UI:NotificationsTab').$sCount); diff --git a/core/trigger.class.inc.php b/core/trigger.class.inc.php index 3383d44b3..bb7ac9ae3 100644 --- a/core/trigger.class.inc.php +++ b/core/trigger.class.inc.php @@ -76,6 +76,19 @@ abstract class Trigger extends cmdbAbstractObject } } } + + /** + * Check whether the given object is in the scope of this trigger + * and can potentially be the subject of notifications + * @param DBObject $oObject The object to check + * @return bool + */ + public function IsInScope(DBObject $oObject) + { + // By default the answer is no + // Overload this function in your own derived class for a different behavior + return false; + } } abstract class TriggerOnObject extends Trigger @@ -105,6 +118,18 @@ abstract class TriggerOnObject extends Trigger // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form } + + /** + * Check whether the given object is in the scope of this trigger + * and can potentially be the subject of notifications + * @param DBObject $oObject The object to check + * @return bool + */ + public function IsInScope(DBObject $oObject) + { + $sRootClass = $this->Get('target_class'); + return ($oObject instanceof $sRootClass); + } } /** * To trigger notifications when a ticket is updated from the portal