trigger.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Persistent class Trigger and derived
  20. * User defined triggers, that may be used in conjunction with user defined actions
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. /**
  26. * A user defined trigger, to customize the application
  27. * A trigger will activate an action
  28. *
  29. * @package iTopORM
  30. */
  31. abstract class Trigger extends cmdbAbstractObject
  32. {
  33. public static function Init()
  34. {
  35. $aParams = array
  36. (
  37. "category" => "core/cmdb",
  38. "key_type" => "autoincrement",
  39. "name_attcode" => "description",
  40. "state_attcode" => "",
  41. "reconc_keys" => array('description'),
  42. "db_table" => "priv_trigger",
  43. "db_key_field" => "id",
  44. "db_finalclass_field" => "realclass",
  45. "display_template" => "",
  46. );
  47. MetaModel::Init_Params($aParams);
  48. //MetaModel::Init_InheritAttributes();
  49. MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeLinkedSetIndirect("action_list", array("linked_class"=>"lnkTriggerAction", "ext_key_to_me"=>"trigger_id", "ext_key_to_remote"=>"action_id", "allowed_values"=>null, "count_min"=>1, "count_max"=>0, "depends_on"=>array())));
  51. // Display lists
  52. MetaModel::Init_SetZListItems('details', array('finalclass', 'description', 'action_list')); // Attributes to be displayed for the complete details
  53. MetaModel::Init_SetZListItems('list', array('finalclass')); // Attributes to be displayed for a list
  54. // Search criteria
  55. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  56. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  57. }
  58. public function DoActivate($aContextArgs)
  59. {
  60. // Find the related actions
  61. $oLinkedActions = $this->Get('action_list');
  62. while ($oLink = $oLinkedActions->Fetch())
  63. {
  64. $iActionId = $oLink->Get('action_id');
  65. $oAction = MetaModel::GetObject('Action', $iActionId);
  66. if ($oAction->IsActive())
  67. {
  68. $oAction->DoExecute($this, $aContextArgs);
  69. }
  70. }
  71. }
  72. /**
  73. * Check whether the given object is in the scope of this trigger
  74. * and can potentially be the subject of notifications
  75. * @param DBObject $oObject The object to check
  76. * @return bool
  77. */
  78. public function IsInScope(DBObject $oObject)
  79. {
  80. // By default the answer is no
  81. // Overload this function in your own derived class for a different behavior
  82. return false;
  83. }
  84. }
  85. abstract class TriggerOnObject extends Trigger
  86. {
  87. public static function Init()
  88. {
  89. $aParams = array
  90. (
  91. "category" => "core/cmdb",
  92. "key_type" => "autoincrement",
  93. "name_attcode" => "description",
  94. "state_attcode" => "",
  95. "reconc_keys" => array('description'),
  96. "db_table" => "priv_trigger_onobject",
  97. "db_key_field" => "id",
  98. "db_finalclass_field" => "",
  99. "display_template" => "",
  100. );
  101. MetaModel::Init_Params($aParams);
  102. MetaModel::Init_InheritAttributes();
  103. MetaModel::Init_AddAttribute(new AttributeClass("target_class", array("class_category"=>"bizmodel", "more_values"=>"User,UserExternal,UserInternal,UserLDAP,UserLocal", "sql"=>"target_class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  104. MetaModel::Init_AddAttribute(new AttributeOQL("filter", array("allowed_values"=>null, "sql"=>"filter", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  105. // Display lists
  106. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'filter', 'action_list')); // Attributes to be displayed for the complete details
  107. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class', 'description')); // Attributes to be displayed for a list
  108. // Search criteria
  109. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  110. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  111. }
  112. public function DoCheckToWrite()
  113. {
  114. parent::DoCheckToWrite();
  115. $sFilter = trim($this->Get('filter'));
  116. if (strlen($sFilter) > 0)
  117. {
  118. try
  119. {
  120. $oSearch = DBObjectSearch::FromOQL($sFilter);
  121. if (!MetaModel::IsParentClass($this->Get('target_class'), $oSearch->GetClass()))
  122. {
  123. $this->m_aCheckIssues[] = Dict::Format('TriggerOnObject:WrongFilterClass', $this->Get('target_class'));
  124. }
  125. }
  126. catch(OqlException $e)
  127. {
  128. $this->m_aCheckIssues[] = Dict::Format('TriggerOnObject:WrongFilterQuery', $e->getMessage());
  129. }
  130. }
  131. }
  132. /**
  133. * Check whether the given object is in the scope of this trigger
  134. * and can potentially be the subject of notifications
  135. * @param DBObject $oObject The object to check
  136. * @return bool
  137. */
  138. public function IsInScope(DBObject $oObject)
  139. {
  140. $sRootClass = $this->Get('target_class');
  141. return ($oObject instanceof $sRootClass);
  142. }
  143. public function DoActivate($aContextArgs)
  144. {
  145. $bGo = true;
  146. if (isset($aContextArgs['this->object()']))
  147. {
  148. $bGo = $this->IsTargetObject($aContextArgs['this->object()']->GetKey());
  149. }
  150. if ($bGo)
  151. {
  152. parent::DoActivate($aContextArgs);
  153. }
  154. }
  155. public function IsTargetObject($iObjectId)
  156. {
  157. $sFilter = trim($this->Get('filter'));
  158. if (strlen($sFilter) > 0)
  159. {
  160. $oSearch = DBObjectSearch::FromOQL($sFilter);
  161. $oSearch->AddCondition('id', $iObjectId, '=');
  162. $oSet = new DBObjectSet($oSearch);
  163. $bRet = ($oSet->Count() > 0);
  164. }
  165. else
  166. {
  167. $bRet = true;
  168. }
  169. return $bRet;
  170. }
  171. }
  172. /**
  173. * To trigger notifications when a ticket is updated from the portal
  174. */
  175. class TriggerOnPortalUpdate extends TriggerOnObject
  176. {
  177. public static function Init()
  178. {
  179. $aParams = array
  180. (
  181. "category" => "core/cmdb,bizmodel",
  182. "key_type" => "autoincrement",
  183. "name_attcode" => "description",
  184. "state_attcode" => "",
  185. "reconc_keys" => array('description'),
  186. "db_table" => "priv_trigger_onportalupdate",
  187. "db_key_field" => "id",
  188. "db_finalclass_field" => "",
  189. "display_template" => "",
  190. );
  191. MetaModel::Init_Params($aParams);
  192. MetaModel::Init_InheritAttributes();
  193. // Display lists
  194. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'filter', 'action_list')); // Attributes to be displayed for the complete details
  195. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class', 'description')); // Attributes to be displayed for a list
  196. // Search criteria
  197. }
  198. }
  199. abstract class TriggerOnStateChange extends TriggerOnObject
  200. {
  201. public static function Init()
  202. {
  203. $aParams = array
  204. (
  205. "category" => "core/cmdb",
  206. "key_type" => "autoincrement",
  207. "name_attcode" => "description",
  208. "state_attcode" => "",
  209. "reconc_keys" => array('description'),
  210. "db_table" => "priv_trigger_onstatechange",
  211. "db_key_field" => "id",
  212. "db_finalclass_field" => "",
  213. "display_template" => "",
  214. );
  215. MetaModel::Init_Params($aParams);
  216. MetaModel::Init_InheritAttributes();
  217. MetaModel::Init_AddAttribute(new AttributeString("state", array("allowed_values"=>null, "sql"=>"state", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  218. // Display lists
  219. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'filter', 'state', 'action_list')); // Attributes to be displayed for the complete details
  220. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class', 'state')); // Attributes to be displayed for a list
  221. // Search criteria
  222. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class', 'state')); // Criteria of the std search form
  223. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  224. }
  225. }
  226. class TriggerOnStateEnter extends TriggerOnStateChange
  227. {
  228. public static function Init()
  229. {
  230. $aParams = array
  231. (
  232. "category" => "core/cmdb,bizmodel",
  233. "key_type" => "autoincrement",
  234. "name_attcode" => "description",
  235. "state_attcode" => "",
  236. "reconc_keys" => array('description'),
  237. "db_table" => "priv_trigger_onstateenter",
  238. "db_key_field" => "id",
  239. "db_finalclass_field" => "",
  240. "display_template" => "",
  241. );
  242. MetaModel::Init_Params($aParams);
  243. MetaModel::Init_InheritAttributes();
  244. // Display lists
  245. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'filter', 'state', 'action_list')); // Attributes to be displayed for the complete details
  246. MetaModel::Init_SetZListItems('list', array('target_class', 'state')); // Attributes to be displayed for a list
  247. // Search criteria
  248. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class', 'state')); // Criteria of the std search form
  249. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  250. }
  251. }
  252. class TriggerOnStateLeave extends TriggerOnStateChange
  253. {
  254. public static function Init()
  255. {
  256. $aParams = array
  257. (
  258. "category" => "core/cmdb,bizmodel",
  259. "key_type" => "autoincrement",
  260. "name_attcode" => "description",
  261. "state_attcode" => "",
  262. "reconc_keys" => array('description'),
  263. "db_table" => "priv_trigger_onstateleave",
  264. "db_key_field" => "id",
  265. "db_finalclass_field" => "",
  266. "display_template" => "",
  267. );
  268. MetaModel::Init_Params($aParams);
  269. MetaModel::Init_InheritAttributes();
  270. // Display lists
  271. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'filter', 'state', 'action_list')); // Attributes to be displayed for the complete details
  272. MetaModel::Init_SetZListItems('list', array('target_class', 'state')); // Attributes to be displayed for a list
  273. // Search criteria
  274. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class', 'state')); // Criteria of the std search form
  275. // MetaModel::Init_SetZListItems('advanced_search', array('')); // Criteria of the advanced search form
  276. }
  277. }
  278. class TriggerOnObjectCreate extends TriggerOnObject
  279. {
  280. public static function Init()
  281. {
  282. $aParams = array
  283. (
  284. "category" => "core/cmdb,bizmodel",
  285. "key_type" => "autoincrement",
  286. "name_attcode" => "description",
  287. "state_attcode" => "",
  288. "reconc_keys" => array('description'),
  289. "db_table" => "priv_trigger_onobjcreate",
  290. "db_key_field" => "id",
  291. "db_finalclass_field" => "",
  292. "display_template" => "",
  293. );
  294. MetaModel::Init_Params($aParams);
  295. MetaModel::Init_InheritAttributes();
  296. // Display lists
  297. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'filter', 'action_list')); // Attributes to be displayed for the complete details
  298. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class')); // Attributes to be displayed for a list
  299. // Search criteria
  300. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class')); // Criteria of the std search form
  301. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  302. }
  303. }
  304. class lnkTriggerAction extends cmdbAbstractObject
  305. {
  306. public static function Init()
  307. {
  308. $aParams = array
  309. (
  310. "category" => "core/cmdb,bizmodel",
  311. "key_type" => "autoincrement",
  312. "name_attcode" => "",
  313. "state_attcode" => "",
  314. "reconc_keys" => array('action_id', 'trigger_id'),
  315. "db_table" => "priv_link_action_trigger",
  316. "db_key_field" => "link_id",
  317. "db_finalclass_field" => "",
  318. "display_template" => "",
  319. "is_link" => true,
  320. );
  321. MetaModel::Init_Params($aParams);
  322. MetaModel::Init_AddAttribute(new AttributeExternalKey("action_id", array("targetclass"=>"Action", "jointype"=> '', "allowed_values"=>null, "sql"=>"action_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  323. MetaModel::Init_AddAttribute(new AttributeExternalField("action_name", array("allowed_values"=>null, "extkey_attcode"=> 'action_id', "target_attcode"=>"name")));
  324. MetaModel::Init_AddAttribute(new AttributeExternalKey("trigger_id", array("targetclass"=>"Trigger", "jointype"=> '', "allowed_values"=>null, "sql"=>"trigger_id", "is_null_allowed"=>false, "on_target_delete"=>DEL_AUTO, "depends_on"=>array())));
  325. MetaModel::Init_AddAttribute(new AttributeExternalField("trigger_name", array("allowed_values"=>null, "extkey_attcode"=> 'trigger_id', "target_attcode"=>"description")));
  326. MetaModel::Init_AddAttribute(new AttributeInteger("order", array("allowed_values"=>null, "sql"=>"order", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  327. // Display lists
  328. MetaModel::Init_SetZListItems('details', array('action_id', 'trigger_id', 'order')); // Attributes to be displayed for a list
  329. MetaModel::Init_SetZListItems('list', array('action_id', 'trigger_id', 'order')); // Attributes to be displayed for a list
  330. // Search criteria
  331. MetaModel::Init_SetZListItems('standard_search', array('action_id', 'trigger_id', 'order')); // Criteria of the std search form
  332. MetaModel::Init_SetZListItems('advanced_search', array('action_id', 'trigger_id', 'order')); // Criteria of the advanced search form
  333. }
  334. }
  335. class TriggerOnThresholdReached extends TriggerOnObject
  336. {
  337. public static function Init()
  338. {
  339. $aParams = array
  340. (
  341. "category" => "core/cmdb,bizmodel",
  342. "key_type" => "autoincrement",
  343. "name_attcode" => "description",
  344. "state_attcode" => "",
  345. "reconc_keys" => array('description'),
  346. "db_table" => "priv_trigger_threshold",
  347. "db_key_field" => "id",
  348. "db_finalclass_field" => "",
  349. "display_template" => "",
  350. );
  351. MetaModel::Init_Params($aParams);
  352. MetaModel::Init_InheritAttributes();
  353. MetaModel::Init_AddAttribute(new AttributeString("stop_watch_code", array("allowed_values"=>null, "sql"=>"stop_watch_code", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  354. MetaModel::Init_AddAttribute(new AttributeString("threshold_index", array("allowed_values"=>null, "sql"=>"threshold_index", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  355. // Display lists
  356. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'stop_watch_code', 'threshold_index', 'action_list')); // Attributes to be displayed for the complete details
  357. MetaModel::Init_SetZListItems('list', array('target_class', 'threshold_index', 'threshold_index')); // Attributes to be displayed for a list
  358. // Search criteria
  359. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class')); // Criteria of the std search form
  360. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  361. }
  362. }
  363. ?>