annotate modules/trigger/trigger.module @ 3:165d43f946a8 6.1

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