trigger.module

You are here

  1. 7 modules/trigger/trigger.module
  2. 6 modules/trigger/trigger.module

Enables functions to be stored and executed at a later time when triggered by other modules or by one of Drupal's core API hooks.

Functions

Namesort descending Description
trigger_access_check Access callback for menu system.
trigger_actions_delete Implementation of hook_actions_delete().
trigger_comment Implementation of hook_comment().
trigger_cron Implementation of hook_cron().
trigger_forms Implementation of hook_forms(). We reuse code by using the same assignment form definition for each node-op combination.
trigger_help Implementation of hook_help().
trigger_menu Implementation of hook_menu().
trigger_nodeapi Implementation of hook_nodeapi().
trigger_options Often we generate a select field of all actions. This function generates the options for that select.
trigger_taxonomy Implementation of hook_taxonomy().
trigger_theme Implementation of hook_theme().
trigger_user Implementation of hook_user().
_trigger_get_hook_aids Get the aids of actions to be executed for a hook-op combination.
_trigger_normalize_comment_context When an action is called in a context that does not match its type, the object that the action expects must be retrieved. For example, when an action that works on nodes is called during the comment hook, the node object is not available since the…
_trigger_normalize_node_context When an action is called in a context that does not match its type, the object that the action expects must be retrieved. For example, when an action that works on users is called during the node hook, the user object is not available since the node…
_trigger_normalize_user_context When an action is called in a context that does not match its type, the object that the action expects must be retrieved. For example, when an action that works on nodes is called during the user hook, the node object is not available since the user…

File

modules/trigger/trigger.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables functions to be stored and executed at a later time when
  5. * triggered by other modules or by one of Drupal's core API hooks.
  6. */
  7. /**
  8. * Implementation of hook_help().
  9. */
  10. function trigger_help($path, $arg) {
  11. $explanation = '<p>'. t('Triggers are system events, such as when new content is added or when a user logs in. Trigger module combines these triggers with actions (functional tasks), such as unpublishing content or e-mailing an administrator. The <a href="@url">Actions settings page</a> contains a list of existing actions and provides the ability to create and configure additional actions.', array('@url' => url('admin/settings/actions'))) .'</p>';
  12. switch ($path) {
  13. case 'admin/build/trigger/comment':
  14. return $explanation .'<p>'. t('Below you can assign actions to run when certain comment-related triggers happen. For example, you could promote a post to the front page when a comment is added.') .'</p>';
  15. case 'admin/build/trigger/node':
  16. return $explanation .'<p>'. t('Below you can assign actions to run when certain content-related triggers happen. For example, you could send an e-mail to an administrator when a post is created or updated.') .'</p>';
  17. case 'admin/build/trigger/cron':
  18. return $explanation .'<p>'. t('Below you can assign actions to run during each pass of a <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))) .'</p>';
  19. case 'admin/build/trigger/taxonomy':
  20. return $explanation .'<p>'. t('Below you can assign actions to run when certain taxonomy-related triggers happen. For example, you could send an e-mail to an administrator when a term is deleted.') .'</p>';
  21. case 'admin/build/trigger/user':
  22. return $explanation .'<p>'. t("Below you can assign actions to run when certain user-related triggers happen. For example, you could send an e-mail to an administrator when a user account is deleted.") .'</p>';
  23. case 'admin/help#trigger':
  24. $output = '<p>'. t('The Trigger module provides the ability to trigger <a href="@actions">actions</a> upon system events, such as when new content is added or when a user logs in.', array('@actions' => url('admin/settings/actions'))) .'</p>';
  25. $output .= '<p>'. t('The combination of actions and triggers can perform many useful tasks, such as e-mailing an administrator if a user account is deleted, or automatically unpublishing comments that contain certain words. By default, there are five "contexts" of events (Comments, Content, Cron, Taxonomy, and Users), but more may be added by additional modules.') .'</p>';
  26. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@trigger">Trigger module</a>.', array('@trigger' => 'http://drupal.org/handbook/modules/trigger/')) .'</p>';
  27. return $output;
  28. }
  29. }
  30. /**
  31. * Implementation of hook_menu().
  32. */
  33. function trigger_menu() {
  34. $items['admin/build/trigger'] = array(
  35. 'title' => 'Triggers',
  36. 'description' => 'Tell Drupal when to execute actions.',
  37. 'page callback' => 'trigger_assign',
  38. 'access callback' => 'trigger_access_check',
  39. 'access arguments' => array('node'),
  40. 'file' => 'trigger.admin.inc',
  41. );
  42. // We don't use a menu wildcard here because these are tabs,
  43. // not invisible items.
  44. $items['admin/build/trigger/node'] = array(
  45. 'title' => 'Content',
  46. 'page callback' => 'trigger_assign',
  47. 'page arguments' => array('node'),
  48. 'access callback' => 'trigger_access_check',
  49. 'access arguments' => array('node'),
  50. 'type' => MENU_LOCAL_TASK,
  51. 'file' => 'trigger.admin.inc',
  52. );
  53. $items['admin/build/trigger/user'] = array(
  54. 'title' => 'Users',
  55. 'page callback' => 'trigger_assign',
  56. 'page arguments' => array('user'),
  57. 'access callback' => 'trigger_access_check',
  58. 'access arguments' => array('user'),
  59. 'type' => MENU_LOCAL_TASK,
  60. 'file' => 'trigger.admin.inc',
  61. );
  62. $items['admin/build/trigger/comment'] = array(
  63. 'title' => 'Comments',
  64. 'page callback' => 'trigger_assign',
  65. 'page arguments' => array('comment'),
  66. 'access callback' => 'trigger_access_check',
  67. 'access arguments' => array('comment'),
  68. 'type' => MENU_LOCAL_TASK,
  69. 'file' => 'trigger.admin.inc',
  70. );
  71. $items['admin/build/trigger/taxonomy'] = array(
  72. 'title' => 'Taxonomy',
  73. 'page callback' => 'trigger_assign',
  74. 'page arguments' => array('taxonomy'),
  75. 'access callback' => 'trigger_access_check',
  76. 'access arguments' => array('taxonomy'),
  77. 'type' => MENU_LOCAL_TASK,
  78. 'file' => 'trigger.admin.inc',
  79. );
  80. $items['admin/build/trigger/cron'] = array(
  81. 'title' => 'Cron',
  82. 'page callback' => 'trigger_assign',
  83. 'page arguments' => array('cron'),
  84. 'access arguments' => array('administer actions'),
  85. 'type' => MENU_LOCAL_TASK,
  86. 'file' => 'trigger.admin.inc',
  87. );
  88. // We want contributed modules to be able to describe
  89. // their hooks and have actions assignable to them.
  90. $hooks = module_invoke_all('hook_info');
  91. foreach ($hooks as $module => $hook) {
  92. // We've already done these.
  93. if (in_array($module, array('node', 'comment', 'user', 'system', 'taxonomy'))) {
  94. continue;
  95. }
  96. $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module));
  97. $info = unserialize($info);
  98. $nice_name = $info['name'];
  99. $items["admin/build/trigger/$module"] = array(
  100. 'title' => $nice_name,
  101. 'page callback' => 'trigger_assign',
  102. 'page arguments' => array($module),
  103. 'access callback' => 'trigger_access_check',
  104. 'access arguments' => array($module),
  105. 'type' => MENU_LOCAL_TASK,
  106. 'file' => 'trigger.admin.inc',
  107. );
  108. }
  109. $items['admin/build/trigger/unassign'] = array(
  110. 'title' => 'Unassign',
  111. 'description' => 'Unassign an action from a trigger.',
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('trigger_unassign'),
  114. 'access arguments' => array('administer actions'),
  115. 'type' => MENU_CALLBACK,
  116. 'file' => 'trigger.admin.inc',
  117. );
  118. return $items;
  119. }
  120. /**
  121. * Access callback for menu system.
  122. */
  123. function trigger_access_check($module) {
  124. return (module_exists($module) && user_access('administer actions'));
  125. }
  126. /**
  127. * Get the aids of actions to be executed for a hook-op combination.
  128. *
  129. * @param $hook
  130. * The name of the hook being fired.
  131. * @param $op
  132. * The name of the operation being executed. Defaults to an empty string
  133. * because some hooks (e.g., hook_cron()) do not have operations.
  134. * @return
  135. * An array of action IDs.
  136. */
  137. function _trigger_get_hook_aids($hook, $op = '') {
  138. $aids = array();
  139. $result = db_query("SELECT aa.aid, a.type FROM {trigger_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = '%s' AND aa.op = '%s' ORDER BY weight", $hook, $op);
  140. while ($action = db_fetch_object($result)) {
  141. $aids[$action->aid]['type'] = $action->type;
  142. }
  143. return $aids;
  144. }
  145. /**
  146. * Implementation of hook_theme().
  147. */
  148. function trigger_theme() {
  149. return array(
  150. 'trigger_display' => array(
  151. 'arguments' => array('element'),
  152. 'file' => 'trigger.admin.inc',
  153. ),
  154. );
  155. }
  156. /**
  157. * Implementation of hook_forms(). We reuse code by using the
  158. * same assignment form definition for each node-op combination.
  159. */
  160. function trigger_forms() {
  161. $hooks = module_invoke_all('hook_info');
  162. foreach ($hooks as $module => $info) {
  163. foreach ($hooks[$module] as $hook => $ops) {
  164. foreach ($ops as $op => $description) {
  165. $forms['trigger_'. $hook .'_'. $op .'_assign_form'] = array('callback' => 'trigger_assign_form');
  166. }
  167. }
  168. }
  169. return $forms;
  170. }
  171. /**
  172. * When an action is called in a context that does not match its type,
  173. * the object that the action expects must be retrieved. For example, when
  174. * an action that works on users is called during the node hook, the
  175. * user object is not available since the node hook doesn't pass it.
  176. * So here we load the object the action expects.
  177. *
  178. * @param $type
  179. * The type of action that is about to be called.
  180. * @param $node
  181. * The node that was passed via the nodeapi hook.
  182. * @return
  183. * The object expected by the action that is about to be called.
  184. */
  185. function _trigger_normalize_node_context($type, $node) {
  186. switch ($type) {
  187. // If an action that works on comments is being called in a node context,
  188. // the action is expecting a comment object. But we do not know which comment
  189. // to give it. The first? The most recent? All of them? So comment actions
  190. // in a node context are not supported.
  191. // An action that works on users is being called in a node context.
  192. // Load the user object of the node's author.
  193. case 'user':
  194. return user_load(array('uid' => $node->uid));
  195. }
  196. }
  197. /**
  198. * Implementation of hook_nodeapi().
  199. */
  200. function trigger_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  201. // Keep objects for reuse so that changes actions make to objects can persist.
  202. static $objects;
  203. // Prevent recursion by tracking which operations have already been called.
  204. static $recursion;
  205. // Support a subset of operations.
  206. if (!in_array($op, array('view', 'update', 'presave', 'insert', 'delete')) || isset($recursion[$op])) {
  207. return;
  208. }
  209. $recursion[$op] = TRUE;
  210. $aids = _trigger_get_hook_aids('nodeapi', $op);
  211. if (!$aids) {
  212. return;
  213. }
  214. $context = array(
  215. 'hook' => 'nodeapi',
  216. 'op' => $op,
  217. );
  218. // We need to get the expected object if the action's type is not 'node'.
  219. // We keep the object in $objects so we can reuse it if we have multiple actions
  220. // that make changes to an object.
  221. foreach ($aids as $aid => $action_info) {
  222. if ($action_info['type'] != 'node') {
  223. if (!isset($objects[$action_info['type']])) {
  224. $objects[$action_info['type']] = _trigger_normalize_node_context($action_info['type'], $node);
  225. }
  226. // Since we know about the node, we pass that info along to the action.
  227. $context['node'] = $node;
  228. $result = actions_do($aid, $objects[$action_info['type']], $context, $a3, $a4);
  229. }
  230. else {
  231. actions_do($aid, $node, $context, $a3, $a4);
  232. }
  233. }
  234. }
  235. /**
  236. * When an action is called in a context that does not match its type,
  237. * the object that the action expects must be retrieved. For example, when
  238. * an action that works on nodes is called during the comment hook, the
  239. * node object is not available since the comment hook doesn't pass it.
  240. * So here we load the object the action expects.
  241. *
  242. * @param $type
  243. * The type of action that is about to be called.
  244. * @param $comment
  245. * The comment that was passed via the comment hook.
  246. * @return
  247. * The object expected by the action that is about to be called.
  248. */
  249. function _trigger_normalize_comment_context($type, $comment) {
  250. switch ($type) {
  251. // An action that works with nodes is being called in a comment context.
  252. case 'node':
  253. return node_load(is_array($comment) ? $comment['nid'] : $comment->nid);
  254. // An action that works on users is being called in a comment context.
  255. case 'user':
  256. return user_load(array('uid' => is_array($comment) ? $comment['uid'] : $comment->uid));
  257. }
  258. }
  259. /**
  260. * Implementation of hook_comment().
  261. */
  262. function trigger_comment($a1, $op) {
  263. // Keep objects for reuse so that changes actions make to objects can persist.
  264. static $objects;
  265. // We support a subset of operations.
  266. if (!in_array($op, array('insert', 'update', 'delete', 'view'))) {
  267. return;
  268. }
  269. $aids = _trigger_get_hook_aids('comment', $op);
  270. $context = array(
  271. 'hook' => 'comment',
  272. 'op' => $op,
  273. );
  274. // We need to get the expected object if the action's type is not 'comment'.
  275. // We keep the object in $objects so we can reuse it if we have multiple actions
  276. // that make changes to an object.
  277. foreach ($aids as $aid => $action_info) {
  278. if ($action_info['type'] != 'comment') {
  279. if (!isset($objects[$action_info['type']])) {
  280. $objects[$action_info['type']] = _trigger_normalize_comment_context($action_info['type'], $a1);
  281. }
  282. // Since we know about the comment, we pass it along to the action
  283. // in case it wants to peek at it.
  284. $context['comment'] = (object) $a1;
  285. actions_do($aid, $objects[$action_info['type']], $context);
  286. }
  287. else {
  288. $comment = (object) $a1;
  289. actions_do($aid, $comment, $context);
  290. }
  291. }
  292. }
  293. /**
  294. * Implementation of hook_cron().
  295. */
  296. function trigger_cron() {
  297. $aids = _trigger_get_hook_aids('cron', 'run');
  298. $context = array(
  299. 'hook' => 'cron',
  300. 'op' => 'run',
  301. );
  302. // Cron does not act on any specific object.
  303. $object = NULL;
  304. actions_do(array_keys($aids), $object, $context);
  305. }
  306. /**
  307. * When an action is called in a context that does not match its type,
  308. * the object that the action expects must be retrieved. For example, when
  309. * an action that works on nodes is called during the user hook, the
  310. * node object is not available since the user hook doesn't pass it.
  311. * So here we load the object the action expects.
  312. *
  313. * @param $type
  314. * The type of action that is about to be called.
  315. * @param $account
  316. * The account object that was passed via the user hook.
  317. * @return
  318. * The object expected by the action that is about to be called.
  319. */
  320. function _trigger_normalize_user_context($type, $account) {
  321. switch ($type) {
  322. // If an action that works on comments is being called in a user context,
  323. // the action is expecting a comment object. But we have no way of
  324. // determining the appropriate comment object to pass. So comment
  325. // actions in a user context are not supported.
  326. // An action that works with nodes is being called in a user context.
  327. // If a single node is being viewed, return the node.
  328. case 'node':
  329. // If we are viewing an individual node, return the node.
  330. if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
  331. return node_load(array('nid' => arg(1)));
  332. }
  333. }
  334. }
  335. /**
  336. * Implementation of hook_user().
  337. */
  338. function trigger_user($op, &$edit, &$account, $category = NULL) {
  339. // Keep objects for reuse so that changes actions make to objects can persist.
  340. static $objects;
  341. // We support a subset of operations.
  342. if (!in_array($op, array('login', 'logout', 'insert', 'update', 'delete', 'view'))) {
  343. return;
  344. }
  345. $aids = _trigger_get_hook_aids('user', $op);
  346. $context = array(
  347. 'hook' => 'user',
  348. 'op' => $op,
  349. 'form_values' => &$edit,
  350. );
  351. foreach ($aids as $aid => $action_info) {
  352. if ($action_info['type'] != 'user') {
  353. if (!isset($objects[$action_info['type']])) {
  354. $objects[$action_info['type']] = _trigger_normalize_user_context($action_info['type'], $account);
  355. }
  356. $context['account'] = $account;
  357. actions_do($aid, $objects[$action_info['type']], $context);
  358. }
  359. else {
  360. actions_do($aid, $account, $context, $category);
  361. }
  362. }
  363. }
  364. /**
  365. * Implementation of hook_taxonomy().
  366. */
  367. function trigger_taxonomy($op, $type, $array) {
  368. if ($type != 'term') {
  369. return;
  370. }
  371. $aids = _trigger_get_hook_aids('taxonomy', $op);
  372. $context = array(
  373. 'hook' => 'taxonomy',
  374. 'op' => $op
  375. );
  376. foreach ($aids as $aid => $action_info) {
  377. $taxonomy_object = (object) $array;
  378. actions_do($aid, $taxonomy_object, $context);
  379. }
  380. }
  381. /**
  382. * Often we generate a select field of all actions. This function
  383. * generates the options for that select.
  384. *
  385. * @param $type
  386. * One of 'node', 'user', 'comment'.
  387. * @return
  388. * Array keyed by action ID.
  389. */
  390. function trigger_options($type = 'all') {
  391. $options = array(t('Choose an action'));
  392. foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
  393. $options[$action['type']][$aid] = $action['description'];
  394. }
  395. if ($type == 'all') {
  396. return $options;
  397. }
  398. else {
  399. return $options[$type];
  400. }
  401. }
  402. /**
  403. * Implementation of hook_actions_delete().
  404. *
  405. * Remove all trigger entries for the given action, when deleted.
  406. */
  407. function trigger_actions_delete($aid) {
  408. db_query("DELETE FROM {trigger_assignments} WHERE aid = '%s'", $aid);
  409. }