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