comparison modules/trigger/trigger.module @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 2427550111ae
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: trigger.module,v 1.13 2008/01/21 20:08:15 goba Exp $
3
4 /**
5 * @file
6 * Enables functions to be stored and executed at a later time when
7 * triggered by other modules or by one of Drupal's core API hooks.
8 */
9
10 /**
11 * Implementation of hook_help().
12 */
13 function trigger_help($path, $arg) {
14 $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 send an e-mail to an administrator when a post is created or updated.') .'</p>';
20 case 'admin/build/trigger/cron':
21 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>';
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, see the online handbook entry for <a href="@trigger">Trigger module</a>.', array('@trigger' => 'http://drupal.org/handbook/modules/trigger/')) .'</p>';
30 return $output;
31 }
32 }
33
34 /**
35 * Implementation of hook_menu().
36 */
37 function trigger_menu() {
38 $items['admin/build/trigger'] = array(
39 'title' => 'Triggers',
40 'description' => 'Tell Drupal when to execute actions.',
41 'page callback' => 'trigger_assign',
42 'access callback' => 'trigger_access_check',
43 'access arguments' => array('node'),
44 'file' => 'trigger.admin.inc',
45 );
46 // We don't use a menu wildcard here because these are tabs,
47 // not invisible items.
48 $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 $items['admin/build/trigger/user'] = array(
57 'title' => 'Users',
58 '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 $items['admin/build/trigger/comment'] = array(
65 'title' => 'Comments',
66 'page callback' => 'trigger_assign',
67 'page arguments' => array('comment'),
68 'access callback' => 'trigger_access_check',
69 'access arguments' => array('comment'),
70 '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 'access arguments' => array('taxonomy'),
79 '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 'page arguments' => array('cron'),
86 'type' => MENU_LOCAL_TASK,
87 'file' => 'trigger.admin.inc',
88 );
89
90 // We want contributed modules to be able to describe
91 // their hooks and have actions assignable to them.
92 $hooks = module_invoke_all('hook_info');
93 foreach ($hooks as $module => $hook) {
94 // We've already done these.
95 if (in_array($module, array('node', 'comment', 'user', 'system', 'taxonomy'))) {
96 continue;
97 }
98 $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module));
99 $info = unserialize($info);
100 $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 * Access callback for menu system.
124 */
125 function trigger_access_check($module) {
126 return (module_exists($module) && user_access('administer actions'));
127 }
128
129 /**
130 * Get the aids of actions to be executed for a hook-op combination.
131 *
132 * @param $hook
133 * The name of the hook being fired.
134 * @param $op
135 * The name of the operation being executed. Defaults to an empty string
136 * because some hooks (e.g., hook_cron()) do not have operations.
137 * @return
138 * An array of action IDs.
139 */
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 while ($action = db_fetch_object($result)) {
144 $aids[$action->aid]['type'] = $action->type;
145 }
146 return $aids;
147 }
148
149 /**
150 * Implementation of hook_theme().
151 */
152 function trigger_theme() {
153 return array(
154 'trigger_display' => array(
155 'arguments' => array('element'),
156 'file' => 'trigger.admin.inc',
157 ),
158 );
159 }
160
161 /**
162 * Implementation of hook_forms(). We reuse code by using the
163 * same assignment form definition for each node-op combination.
164 */
165 function trigger_forms() {
166 $hooks = module_invoke_all('hook_info');
167 foreach ($hooks as $module => $info) {
168 foreach ($hooks[$module] as $hook => $ops) {
169 foreach ($ops as $op => $description) {
170 $forms['trigger_'. $hook .'_'. $op .'_assign_form'] = array('callback' => 'trigger_assign_form');
171 }
172 }
173 }
174
175 return $forms;
176 }
177
178 /**
179 * When an action is called in a context that does not match its type,
180 * the object that the action expects must be retrieved. For example, when
181 * an action that works on users is called during the node hook, the
182 * user object is not available since the node hook doesn't pass it.
183 * So here we load the object the action expects.
184 *
185 * @param $type
186 * The type of action that is about to be called.
187 * @param $node
188 * The node that was passed via the nodeapi hook.
189 * @return
190 * The object expected by the action that is about to be called.
191 */
192 function _trigger_normalize_node_context($type, $node) {
193 switch ($type) {
194 // If an action that works on comments is being called in a node context,
195 // the action is expecting a comment object. But we do not know which comment
196 // to give it. The first? The most recent? All of them? So comment actions
197 // in a node context are not supported.
198
199 // An action that works on users is being called in a node context.
200 // Load the user object of the node's author.
201 case 'user':
202 return user_load(array('uid' => $node->uid));
203 }
204 }
205
206 /**
207 * Implementation of hook_nodeapi().
208 */
209 function trigger_nodeapi(&$node, $op, $a3, $a4) {
210 // Keep objects for reuse so that changes actions make to objects can persist.
211 static $objects;
212 // Prevent recursion by tracking which operations have already been called.
213 static $recursion;
214 // Support a subset of operations.
215 if (!in_array($op, array('view', 'update', 'presave', 'insert', 'delete')) || isset($recursion[$op])) {
216 return;
217 }
218 $recursion[$op] = TRUE;
219
220 $aids = _trigger_get_hook_aids('nodeapi', $op);
221 if (!$aids) {
222 return;
223 }
224 $context = array(
225 'hook' => 'nodeapi',
226 'op' => $op,
227 );
228
229 // We need to get the expected object if the action's type is not 'node'.
230 // We keep the object in $objects so we can reuse it if we have multiple actions
231 // that make changes to an object.
232 foreach ($aids as $aid => $action_info) {
233 if ($action_info['type'] != 'node') {
234 if (!isset($objects[$action_info['type']])) {
235 $objects[$action_info['type']] = _trigger_normalize_node_context($action_info['type'], $node);
236 }
237 // Since we know about the node, we pass that info along to the action.
238 $context['node'] = $node;
239 $result = actions_do($aid, $objects[$action_info['type']], $context, $a4, $a4);
240 }
241 else {
242 actions_do($aid, $node, $context, $a3, $a4);
243 }
244 }
245 }
246
247 /**
248 * When an action is called in a context that does not match its type,
249 * the object that the action expects must be retrieved. For example, when
250 * an action that works on nodes is called during the comment hook, the
251 * node object is not available since the comment hook doesn't pass it.
252 * So here we load the object the action expects.
253 *
254 * @param $type
255 * The type of action that is about to be called.
256 * @param $comment
257 * The comment that was passed via the comment hook.
258 * @return
259 * The object expected by the action that is about to be called.
260 */
261 function _trigger_normalize_comment_context($type, $comment) {
262 switch ($type) {
263 // An action that works with nodes is being called in a comment context.
264 case 'node':
265 return node_load(is_array($comment) ? $comment['nid'] : $comment->nid);
266
267 // An action that works on users is being called in a comment context.
268 case 'user':
269 return user_load(array('uid' => is_array($comment) ? $comment['uid'] : $comment->uid));
270 }
271 }
272
273 /**
274 * Implementation of hook_comment().
275 */
276 function trigger_comment($a1, $op) {
277 // Keep objects for reuse so that changes actions make to objects can persist.
278 static $objects;
279 // We support a subset of operations.
280 if (!in_array($op, array('insert', 'update', 'delete', 'view'))) {
281 return;
282 }
283 $aids = _trigger_get_hook_aids('comment', $op);
284 $context = array(
285 'hook' => 'comment',
286 'op' => $op,
287 );
288 // We need to get the expected object if the action's type is not 'comment'.
289 // We keep the object in $objects so we can reuse it if we have multiple actions
290 // that make changes to an object.
291 foreach ($aids as $aid => $action_info) {
292 if ($action_info['type'] != 'comment') {
293 if (!isset($objects[$action_info['type']])) {
294 $objects[$action_info['type']] = _trigger_normalize_comment_context($action_info['type'], $a1);
295 }
296 // Since we know about the comment, we pass it along to the action
297 // in case it wants to peek at it.
298 $context['comment'] = (object) $a1;
299 actions_do($aid, $objects[$action_info['type']], $context);
300 }
301 else {
302 $comment = (object) $a1;
303 actions_do($aid, $comment, $context);
304 }
305 }
306 }
307
308 /**
309 * Implementation of hook_cron().
310 */
311 function trigger_cron() {
312 $aids = _trigger_get_hook_aids('cron');
313 $context = array(
314 'hook' => 'cron',
315 'op' => '',
316 );
317 // Cron does not act on any specific object.
318 $object = NULL;
319 actions_do(array_keys($aids), $object, $context);
320 }
321
322 /**
323 * When an action is called in a context that does not match its type,
324 * the object that the action expects must be retrieved. For example, when
325 * an action that works on nodes is called during the user hook, the
326 * node object is not available since the user hook doesn't pass it.
327 * So here we load the object the action expects.
328 *
329 * @param $type
330 * The type of action that is about to be called.
331 * @param $account
332 * The account object that was passed via the user hook.
333 * @return
334 * The object expected by the action that is about to be called.
335 */
336 function _trigger_normalize_user_context($type, $account) {
337 switch ($type) {
338 // If an action that works on comments is being called in a user context,
339 // the action is expecting a comment object. But we have no way of
340 // determining the appropriate comment object to pass. So comment
341 // actions in a user context are not supported.
342
343 // An action that works with nodes is being called in a user context.
344 // If a single node is being viewed, return the node.
345 case 'node':
346 // If we are viewing an individual node, return the node.
347 if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
348 return node_load(array('nid' => arg(1)));
349 }
350 }
351 }
352
353 /**
354 * Implementation of hook_user().
355 */
356 function trigger_user($op, &$edit, &$account, $category = NULL) {
357 // Keep objects for reuse so that changes actions make to objects can persist.
358 static $objects;
359 // We support a subset of operations.
360 if (!in_array($op, array('login', 'logout', 'insert', 'update', 'delete', 'view'))) {
361 return;
362 }
363 $aids = _trigger_get_hook_aids('user', $op);
364 $context = array(
365 'hook' => 'user',
366 'op' => $op,
367 'form_values' => &$edit,
368 );
369 foreach ($aids as $aid => $action_info) {
370 if ($action_info['type'] != 'user') {
371 if (!isset($objects[$action_info['type']])) {
372 $objects[$action_info['type']] = _trigger_normalize_user_context($action_info['type'], $account);
373 }
374 $context['account'] = $account;
375 actions_do($aid, $objects[$action_info['type']], $context);
376 }
377 else {
378 actions_do($aid, $account, $context, $category);
379 }
380 }
381 }
382
383 /**
384 * Implementation of hook_taxonomy().
385 */
386 function trigger_taxonomy($op, $type, $array) {
387 if ($type != 'term') {
388 return;
389 }
390 $aids = _trigger_get_hook_aids('taxonomy', $op);
391 $context = array(
392 'hook' => 'taxonomy',
393 'op' => $op
394 );
395 foreach ($aids as $aid => $action_info) {
396 $taxonomy_object = (object) $array;
397 actions_do($aid, $taxonomy_object, $context);
398 }
399 }
400
401 /**
402 * Often we generate a select field of all actions. This function
403 * generates the options for that select.
404 *
405 * @param $type
406 * One of 'node', 'user', 'comment'.
407 * @return
408 * Array keyed by action ID.
409 */
410 function trigger_options($type = 'all') {
411 $options = array(t('Choose an action'));
412 foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
413 $options[$action['type']][$aid] = $action['description'];
414 }
415
416 if ($type == 'all') {
417 return $options;
418 }
419 else {
420 return $options[$type];
421 }
422 }
423
424 /**
425 * Implementation of hook_actions_delete().
426 *
427 * Remove all trigger entries for the given action, when deleted.
428 */
429 function trigger_actions_delete($aid) {
430 db_query("DELETE FROM {trigger_assignments} WHERE aid = '%s'", $aid);
431 }