trigger.module 15.1 KB
Newer Older
1
2
3
4
<?php
// $Id$

/**
5
6
7
8
 * @file
 * 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.
 */
9
10

/**
11
12
 * Implementation of hook_help().
 */
13
14
function trigger_help($path, $arg) {
  $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>';
15
  switch ($path) {
16
    case 'admin/build/trigger/comment':
17
      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>';
18
    case 'admin/build/trigger/node':
19
      return $explanation .'<p>'. t('Below you can assign actions to run when certain content-related triggers happen. For example, you could remove a post from the front page when the post is updated.') .'</p>';
20
    case 'admin/build/trigger/cron':
21
      return $explanation .'<p>'. t('Below you can assign actions to run when cron runs. More information on cron is available in the <a href="@system">System module help page</a>.', array('@system' => url('admin/help/system'))) .'</p>';
22
    case 'admin/build/trigger/taxonomy':
23
      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>';
24
    case 'admin/build/trigger/user':
25
      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>';
26
    case 'admin/help#trigger':
27
      $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>';
28
      $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>';
29
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@trigger">Trigger</a> page.', array('@trigger' => 'http://drupal.org/handbook/modules/trigger/')) .'</p>';
30
      return $output;
31
32
33
34
  }
}

/**
35
36
 * Implementation of hook_menu().
 */
37
38
39
function trigger_menu() {
  $items['admin/build/trigger'] = array(
    'title' => 'Triggers',
40
    'description' => 'Tell Drupal when to execute actions.',
41
42
43
    'page callback' => 'trigger_assign',
    'access callback' => 'trigger_access_check',
    'access arguments' => array('node'),
44
    'file' => 'trigger.admin.inc',
45
  );
46
47
48
  // We don't use a menu wildcard here because these are tabs,
  // not invisible items.
  $items['admin/build/trigger/node'] = array(
49
    'title' => 'Content',
50
    'page callback' => 'trigger_assign',
51
    'page arguments' => array('node'),
52
    'access arguments' => array('node'),
53
    'type' => MENU_LOCAL_TASK,
54
    'file' => 'trigger.admin.inc',
55
  );
56
57
58
  $items['admin/build/trigger/user'] = array(
    'title' => 'Users',
    'page callback' => 'trigger_assign',
59
    'page arguments' => array('user'),
60
    'access arguments' => array('user'),
61
    'type' => MENU_LOCAL_TASK,
62
    'file' => 'trigger.admin.inc',
63
  );
64
65
66
  $items['admin/build/trigger/comment'] = array(
    'title' => 'Comments',
    'page callback' => 'trigger_assign',
67
    'page arguments' => array('comment'),
68
    'access callback' => 'trigger_access_check',
69
70
    'access arguments' => array('comment'),
    'type' => MENU_LOCAL_TASK,
71
    'file' => 'trigger.admin.inc',
72
  );
73
  $items['admin/build/trigger/taxonomy'] = array(
74
    'title' => 'Taxonomy',
75
    'page callback' => 'trigger_assign',
76
    'page arguments' => array('taxonomy'),
77
    'access callback' => 'trigger_access_check',
78
79
    'access arguments' => array('taxonomy'),
    'type' => MENU_LOCAL_TASK,
80
    'file' => 'trigger.admin.inc',
81
  );
82
  $items['admin/build/trigger/cron'] = array(
83
    'title' => 'Cron',
84
    'page callback' => 'trigger_assign',
85
86
    'page arguments' => array('cron'),
    'type' => MENU_LOCAL_TASK,
87
    'file' => 'trigger.admin.inc',
88
89
90
91
92
93
  );

  // We want contributed modules to be able to describe
  // their hooks and have actions assignable to them.
  $hooks = module_invoke_all('hook_info');
  foreach ($hooks as $module => $hook) {
94
    // We've already done these.
95
96
97
98
99
100
    if (in_array($module, array('node', 'comment', 'user', 'system', 'taxonomy'))) {
      continue;
    }
    $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module));
    $info = unserialize($info);
    $nice_name = $info['name'];
101
    $items["admin/build/trigger/$module"] = array(
102
      'title' => $nice_name,
103
      'page callback' => 'trigger_assign',
104
      'page arguments' => array($module),
105
      'access arguments' => array($module),
106
      'type' => MENU_LOCAL_TASK,
107
      'file' => 'trigger.admin.inc',
108
109
    );
  }
110
  $items['admin/build/trigger/unassign'] = array(
111
    'title' => 'Unassign',
112
    'description' => 'Unassign an action from a trigger.',
113
    'page callback' => 'drupal_get_form',
114
    'page arguments' => array('trigger_unassign'),
115
    'type' => MENU_CALLBACK,
116
    'file' => 'trigger.admin.inc',
117
118
  );

119
  return $items;
120
121
122
123
124
}

/**
 * Access callback for menu system.
 */
125
function trigger_access_check($module) {
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  return (module_exists($module) && user_access('administer actions'));
}

/**
 * Get the aids of actions to be executed for a hook-op combination.
 *
 * @param $hook
 *   The name of the hook being fired.
 * @param $op
 *   The name of the operation being executed. Defaults to an empty string
 *   because some hooks (e.g., hook_cron()) do not have operations.
 * @return
 *   An array of action IDs.
 */
140
function _trigger_get_hook_aids($hook, $op = '') {
141
  $aids = array();
142
  $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);
143
144
145
146
147
148
149
150
151
  while ($action = db_fetch_object($result)) {
    $aids[$action->aid]['type'] = $action->type;
  }
  return $aids;
}

/**
 * Implementation of hook_theme().
 */
152
function trigger_theme() {
153
  return array(
154
    'trigger_display' => array(
155
156
      'arguments' => array('element'),
      'file' => 'trigger.admin.inc',
157
158
159
160
161
162
163
164
    ),
  );
}

/**
 * Implementation of hook_forms(). We reuse code by using the
 * same assignment form definition for each node-op combination.
 */
165
function trigger_forms() {
166
167
168
169
  $hooks = module_invoke_all('hook_info');
  foreach ($hooks as $module => $info) {
    foreach ($hooks[$module] as $hook => $ops) {
      foreach ($ops as $op => $description) {
170
        $forms['trigger_'. $hook .'_'. $op .'_assign_form'] = array('callback' => 'trigger_assign_form');
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
      }
    }
  }

  return $forms;
}

/**
 * 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 hook doesn't pass it.
 * So here we load the object the action expects.
 *
 * @param $type
 *   The type of action that is about to be called.
 * @param $node
 *   The node that was passed via the nodeapi hook.
 * @return
 *   The object expected by the action that is about to be called.
 */
192
function _trigger_normalize_node_context($type, $node) {
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  switch ($type) {
    // If an action that works on comments is being called in a node context,
    // the action is expecting a comment object. But we do not know which comment
    // to give it. The first? The most recent? All of them? So comment actions
    // in a node context are not supported.

    // An action that works on users is being called in a node context.
    // Load the user object of the node's author.
    case 'user':
      return user_load(array('uid' => $node->uid));
  }
}

/**
 * Implementation of hook_nodeapi().
 */
209
function trigger_nodeapi($node, $op, $a3, $a4) {
210
211
212
213
214
215
216
217
218
219
  // Keep objects for reuse so that changes actions make to objects can persist.
  static $objects;
  // Prevent recursion by tracking which operations have already been called.
  static $recursion;
  // Support a subset of operations.
  if (!in_array($op, array('view', 'update', 'presave', 'insert', 'delete')) || isset($recursion[$op])) {
    return;
  }
  $recursion[$op] = TRUE;

220
  $aids = _trigger_get_hook_aids('nodeapi', $op);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  if (!$aids) {
    return;
  }
  $context = array(
    'hook' => 'nodeapi',
    'op' => $op,
  );

  // We need to get the expected object if the action's type is not 'node'.
  // We keep the object in $objects so we can reuse it if we have multiple actions
  // that make changes to an object.
  foreach ($aids as $aid => $action_info) {
    if ($action_info['type'] != 'node') {
      if (!isset($objects[$action_info['type']])) {
235
        $objects[$action_info['type']] = _trigger_normalize_node_context($action_info['type'], $node);
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
      }
      // Since we know about the node, we pass that info along to the action.
      $context['node'] = $node;
      $result = actions_do($aid, $objects[$action_info['type']], $context, $a4, $a4);
    }
    else {
      actions_do($aid, $node, $context, $a3, $a4);
    }
  }
}

/**
 * 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 comment hook doesn't pass it.
 * So here we load the object the action expects.
 *
 * @param $type
 *   The type of action that is about to be called.
 * @param $comment
 *   The comment that was passed via the comment hook.
 * @return
 *   The object expected by the action that is about to be called.
 */
261
function _trigger_normalize_comment_context($type, $comment) {
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  switch ($type) {
    // An action that works with nodes is being called in a comment context.
    case 'node':
      return node_load(is_array($comment) ? $comment['nid'] : $comment->nid);

    // An action that works on users is being called in a comment context.
    case 'user':
      return user_load(array('uid' => is_array($comment) ? $comment['uid'] : $comment->uid));
  }
}

/**
 * Implementation of hook_comment().
 */
276
function trigger_comment($a1, $op) {
277
278
279
280
281
282
  // Keep objects for reuse so that changes actions make to objects can persist.
  static $objects;
  // We support a subset of operations.
  if (!in_array($op, array('insert', 'update', 'delete', 'view'))) {
    return;
  }
283
  $aids = _trigger_get_hook_aids('comment', $op);
284
285
286
287
288
289
290
291
292
293
  $context = array(
    'hook' => 'comment',
    'op' => $op,
  );
  // We need to get the expected object if the action's type is not 'comment'.
  // We keep the object in $objects so we can reuse it if we have multiple actions
  // that make changes to an object.
  foreach ($aids as $aid => $action_info) {
    if ($action_info['type'] != 'comment') {
      if (!isset($objects[$action_info['type']])) {
294
        $objects[$action_info['type']] = _trigger_normalize_comment_context($action_info['type'], $a1);
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
      }
      // Since we know about the comment, we pass it along to the action
      // in case it wants to peek at it.
      $context['comment'] = (object) $a1;
      actions_do($aid, $objects[$action_info['type']], $context);
    }
    else {
      actions_do($aid, (object) $a1, $context);
    }
  }
}

/**
 * Implementation of hook_cron().
 */
310
311
function trigger_cron() {
  $aids = _trigger_get_hook_aids('cron');
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  $context = array(
    'hook' => 'cron',
    'op' => '',
  );
  // Cron does not act on any specific object.
  $object = NULL;
  actions_do(array_keys($aids), $object, $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 hook doesn't pass it.
 * So here we load the object the action expects.
 *
 * @param $type
 *   The type of action that is about to be called.
 * @param $account
 *   The account object that was passed via the user hook.
 * @return
 *   The object expected by the action that is about to be called.
 */
335
function _trigger_normalize_user_context($type, $account) {
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  switch ($type) {
    // If an action that works on comments is being called in a user context,
    // the action is expecting a comment object. But we have no way of
    // determining the appropriate comment object to pass. So comment
    // actions in a user context are not supported.

    // An action that works with nodes is being called in a user context.
    // If a single node is being viewed, return the node.
    case 'node':
      // If we are viewing an individual node, return the node.
      if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
        return node_load(array('nid' => arg(1)));
      }
  }
}

/**
 * Implementation of hook_user().
 */
355
function trigger_user($op, &$edit, &$account, $category = NULL) {
356
357
358
359
360
361
  // Keep objects for reuse so that changes actions make to objects can persist.
  static $objects;
  // We support a subset of operations.
  if (!in_array($op, array('login', 'logout', 'insert', 'update', 'delete', 'view'))) {
    return;
  }
362
  $aids = _trigger_get_hook_aids('user', $op);
363
364
365
366
367
368
369
370
  $context = array(
    'hook' => 'user',
    'op' => $op,
    'form_values' => &$edit,
  );
  foreach ($aids as $aid => $action_info) {
    if ($action_info['type'] != 'user') {
      if (!isset($objects[$action_info['type']])) {
371
        $objects[$action_info['type']] = _trigger_normalize_user_context($action_info['type'], $account);
372
373
374
375
376
377
378
379
380
381
382
383
384
      }
      $context['account'] = $account;
      actions_do($aid, $objects[$action_info['type']], $context);
    }
    else {
      actions_do($aid, $account, $context, $category);
    }
  }
}

/**
 * Implementation of hook_taxonomy().
 */
385
function trigger_taxonomy($op, $type, $array) {
386
387
388
  if ($type != 'term') {
    return;
  }
389
  $aids = _trigger_get_hook_aids('taxonomy', $op);
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  $context = array(
    'hook' => 'taxonomy',
    'op' => $op
  );
  foreach ($aids as $aid => $action_info) {
    actions_do($aid, (object) $array, $context);
  }
}

/**
 * Often we generate a select field of all actions. This function
 * generates the options for that select.
 *
 * @param $type
 *   One of 'node', 'user', 'comment'.
 * @return
 *   Array keyed by action ID.
 */
408
function trigger_options($type = 'all') {
409
410
411
412
413
414
415
416
417
418
419
420
  $options = array(t('Choose an action'));
  foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
    $options[$action['type']][$aid] = $action['description'];
  }

  if ($type == 'all') {
    return $options;
  }
  else {
    return $options[$type];
  }
}