trigger.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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
  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"=>null, "sql"=>"target_class", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  104. // Display lists
  105. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'action_list')); // Attributes to be displayed for the complete details
  106. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class', 'description')); // Attributes to be displayed for a list
  107. // Search criteria
  108. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  109. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  110. }
  111. /**
  112. * Check whether the given object is in the scope of this trigger
  113. * and can potentially be the subject of notifications
  114. * @param DBObject $oObject The object to check
  115. * @return bool
  116. */
  117. public function IsInScope(DBObject $oObject)
  118. {
  119. $sRootClass = $this->Get('target_class');
  120. return ($oObject instanceof $sRootClass);
  121. }
  122. }
  123. /**
  124. * To trigger notifications when a ticket is updated from the portal
  125. */
  126. class TriggerOnPortalUpdate extends TriggerOnObject
  127. {
  128. public static function Init()
  129. {
  130. $aParams = array
  131. (
  132. "category" => "core/cmdb,bizmodel",
  133. "key_type" => "autoincrement",
  134. "name_attcode" => "description",
  135. "state_attcode" => "",
  136. "reconc_keys" => array('description'),
  137. "db_table" => "priv_trigger_onportalupdate",
  138. "db_key_field" => "id",
  139. "db_finalclass_field" => "",
  140. "display_template" => "",
  141. );
  142. MetaModel::Init_Params($aParams);
  143. MetaModel::Init_InheritAttributes();
  144. // Display lists
  145. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'action_list')); // Attributes to be displayed for the complete details
  146. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class', 'description')); // Attributes to be displayed for a list
  147. // Search criteria
  148. }
  149. }
  150. abstract class TriggerOnStateChange extends TriggerOnObject
  151. {
  152. public static function Init()
  153. {
  154. $aParams = array
  155. (
  156. "category" => "core/cmdb",
  157. "key_type" => "autoincrement",
  158. "name_attcode" => "description",
  159. "state_attcode" => "",
  160. "reconc_keys" => array('description'),
  161. "db_table" => "priv_trigger_onstatechange",
  162. "db_key_field" => "id",
  163. "db_finalclass_field" => "",
  164. "display_template" => "",
  165. );
  166. MetaModel::Init_Params($aParams);
  167. MetaModel::Init_InheritAttributes();
  168. MetaModel::Init_AddAttribute(new AttributeString("state", array("allowed_values"=>null, "sql"=>"state", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  169. // Display lists
  170. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'state', 'action_list')); // Attributes to be displayed for the complete details
  171. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class', 'state')); // Attributes to be displayed for a list
  172. // Search criteria
  173. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class', 'state')); // Criteria of the std search form
  174. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  175. }
  176. }
  177. class TriggerOnStateEnter extends TriggerOnStateChange
  178. {
  179. public static function Init()
  180. {
  181. $aParams = array
  182. (
  183. "category" => "core/cmdb,bizmodel",
  184. "key_type" => "autoincrement",
  185. "name_attcode" => "description",
  186. "state_attcode" => "",
  187. "reconc_keys" => array('description'),
  188. "db_table" => "priv_trigger_onstateenter",
  189. "db_key_field" => "id",
  190. "db_finalclass_field" => "",
  191. "display_template" => "",
  192. );
  193. MetaModel::Init_Params($aParams);
  194. MetaModel::Init_InheritAttributes();
  195. // Display lists
  196. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'state', 'action_list')); // Attributes to be displayed for the complete details
  197. MetaModel::Init_SetZListItems('list', array('target_class', 'state')); // Attributes to be displayed for a list
  198. // Search criteria
  199. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class', 'state')); // Criteria of the std search form
  200. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  201. }
  202. }
  203. class TriggerOnStateLeave extends TriggerOnStateChange
  204. {
  205. public static function Init()
  206. {
  207. $aParams = array
  208. (
  209. "category" => "core/cmdb,bizmodel",
  210. "key_type" => "autoincrement",
  211. "name_attcode" => "description",
  212. "state_attcode" => "",
  213. "reconc_keys" => array('description'),
  214. "db_table" => "priv_trigger_onstateleave",
  215. "db_key_field" => "id",
  216. "db_finalclass_field" => "",
  217. "display_template" => "",
  218. );
  219. MetaModel::Init_Params($aParams);
  220. MetaModel::Init_InheritAttributes();
  221. // Display lists
  222. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'state', 'action_list')); // Attributes to be displayed for the complete details
  223. MetaModel::Init_SetZListItems('list', array('target_class', 'state')); // Attributes to be displayed for a list
  224. // Search criteria
  225. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class', 'state')); // Criteria of the std search form
  226. // MetaModel::Init_SetZListItems('advanced_search', array('')); // Criteria of the advanced search form
  227. }
  228. }
  229. class TriggerOnObjectCreate extends TriggerOnObject
  230. {
  231. public static function Init()
  232. {
  233. $aParams = array
  234. (
  235. "category" => "core/cmdb,bizmodel",
  236. "key_type" => "autoincrement",
  237. "name_attcode" => "description",
  238. "state_attcode" => "",
  239. "reconc_keys" => array('description'),
  240. "db_table" => "priv_trigger_onobjcreate",
  241. "db_key_field" => "id",
  242. "db_finalclass_field" => "",
  243. "display_template" => "",
  244. );
  245. MetaModel::Init_Params($aParams);
  246. MetaModel::Init_InheritAttributes();
  247. // Display lists
  248. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'action_list')); // Attributes to be displayed for the complete details
  249. MetaModel::Init_SetZListItems('list', array('finalclass', 'target_class')); // Attributes to be displayed for a list
  250. // Search criteria
  251. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class')); // Criteria of the std search form
  252. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  253. }
  254. }
  255. class lnkTriggerAction extends cmdbAbstractObject
  256. {
  257. public static function Init()
  258. {
  259. $aParams = array
  260. (
  261. "category" => "core/cmdb,bizmodel",
  262. "key_type" => "autoincrement",
  263. "name_attcode" => "",
  264. "state_attcode" => "",
  265. "reconc_keys" => array('action_id', 'trigger_id'),
  266. "db_table" => "priv_link_action_trigger",
  267. "db_key_field" => "link_id",
  268. "db_finalclass_field" => "",
  269. "display_template" => "",
  270. );
  271. MetaModel::Init_Params($aParams);
  272. 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())));
  273. MetaModel::Init_AddAttribute(new AttributeExternalField("action_name", array("allowed_values"=>null, "extkey_attcode"=> 'action_id', "target_attcode"=>"name")));
  274. 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())));
  275. MetaModel::Init_AddAttribute(new AttributeExternalField("trigger_name", array("allowed_values"=>null, "extkey_attcode"=> 'trigger_id', "target_attcode"=>"description")));
  276. MetaModel::Init_AddAttribute(new AttributeInteger("order", array("allowed_values"=>null, "sql"=>"order", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  277. // Display lists
  278. MetaModel::Init_SetZListItems('details', array('action_id', 'trigger_id', 'order')); // Attributes to be displayed for a list
  279. MetaModel::Init_SetZListItems('list', array('action_id', 'trigger_id', 'order')); // Attributes to be displayed for a list
  280. // Search criteria
  281. MetaModel::Init_SetZListItems('standard_search', array('action_id', 'trigger_id', 'order')); // Criteria of the std search form
  282. MetaModel::Init_SetZListItems('advanced_search', array('action_id', 'trigger_id', 'order')); // Criteria of the advanced search form
  283. }
  284. }
  285. class TriggerOnThresholdReached extends TriggerOnObject
  286. {
  287. public static function Init()
  288. {
  289. $aParams = array
  290. (
  291. "category" => "core/cmdb,bizmodel",
  292. "key_type" => "autoincrement",
  293. "name_attcode" => "description",
  294. "state_attcode" => "",
  295. "reconc_keys" => array('description'),
  296. "db_table" => "priv_trigger_threshold",
  297. "db_key_field" => "id",
  298. "db_finalclass_field" => "",
  299. "display_template" => "",
  300. );
  301. MetaModel::Init_Params($aParams);
  302. MetaModel::Init_InheritAttributes();
  303. 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())));
  304. MetaModel::Init_AddAttribute(new AttributeString("threshold_index", array("allowed_values"=>null, "sql"=>"threshold_index", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  305. // Display lists
  306. MetaModel::Init_SetZListItems('details', array('description', 'target_class', 'stop_watch_code', 'threshold_index', 'action_list')); // Attributes to be displayed for the complete details
  307. MetaModel::Init_SetZListItems('list', array('target_class', 'threshold_index', 'threshold_index')); // Attributes to be displayed for a list
  308. // Search criteria
  309. MetaModel::Init_SetZListItems('standard_search', array('description', 'target_class')); // Criteria of the std search form
  310. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  311. }
  312. }
  313. ?>