Explorar el Código

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

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@2688 a333f486-631f-4898-b8df-5754b55c2be0
dflaven hace 12 años
padre
commit
eadee78eae
Se han modificado 2 ficheros con 38 adiciones y 7 borrados
  1. 13 7
      application/cmdbabstract.class.inc.php
  2. 25 0
      core/trigger.class.inc.php

+ 13 - 7
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);

+ 25 - 0
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