annotate modules/trigger/trigger.module @ 20:e3d20ebd63d1 tip

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