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