Parent Directory
|
Revision Log
|
Revision Graph
#286573 by deekayen: remove menu access callback (artifact from backport)
| 1 | <?php |
| 2 | // $Id: actions.module,v 1.40 2008/08/03 03:41:44 jvandyk 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 | include_once(drupal_get_path('module', 'actions') .'/actions.inc'); |
| 11 | |
| 12 | /** |
| 13 | * Implementation of hook_help(). |
| 14 | */ |
| 15 | function actions_help($section) { |
| 16 | $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>'; |
| 17 | switch ($section) { |
| 18 | case 'admin/settings/actions': |
| 19 | case 'admin/settings/actions/manage': |
| 20 | $output = '<p>'. t('Actions are individual tasks that the system can do, such as unpublishing a piece of content or banning a user. Modules, such as the trigger module, can fire these actions when certain system events happen; for example, when a new post is added or when a user logs in. Modules may also provide additional actions.') .'</p>'; |
| 21 | $output .= '<p>'. t('There are two types of actions: simple and advanced. Simple actions do not require any additional configuration, and are listed here automatically. Advanced actions can do more than simple actions; for example, send an e-mail to a specified address, or check for certain words within a piece of content. These actions need to be created and configured first before they may be used. To create an advanced action, select the action from the drop-down below and click the <em>Create</em> button.') .'</p>'; |
| 22 | $output .= '<p>'. t('You may proceed to the <a href="@url">Triggers</a> page to assign these actions to system events.', array('@url' => url('admin/build/trigger'))) .'</p>'; |
| 23 | return $output; |
| 24 | case 'admin/settings/actions/configure': |
| 25 | return t('An advanced action offers additional configuration options which may be filled out below. Changing the <em>Description</em> field is recommended, in order to better identify the precise action taking place. This description will be displayed in modules such as the trigger module when assigning actions to system events, so it is best if it is as descriptive as possible (for example, "Send e-mail to Moderation Team" rather than simply "Send e-mail").'); |
| 26 | case 'admin/build/trigger/comment': |
| 27 | 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>'; |
| 28 | case 'admin/build/trigger/node': |
| 29 | 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>'; |
| 30 | case 'admin/build/trigger/cron': |
| 31 | return $explanation .'<p>'. t('Below you can assign actions to run during each pass of a cron maintenance task.</p>'); |
| 32 | case 'admin/build/trigger/taxonomy': |
| 33 | 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>'; |
| 34 | case 'admin/build/trigger/user': |
| 35 | 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>'; |
| 36 | case 'admin/help#trigger': |
| 37 | $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>'; |
| 38 | $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>'; |
| 39 | $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>'; |
| 40 | return $output; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Implementation of hook_menu(). |
| 46 | */ |
| 47 | function actions_menu($may_cache) { |
| 48 | $items = array(); |
| 49 | $access = user_access('administer actions'); |
| 50 | |
| 51 | if ($may_cache) { |
| 52 | $items[] = array( |
| 53 | 'path' => 'admin/settings/actions', |
| 54 | 'title' => t('Actions'), |
| 55 | 'description' => t('Manage the actions defined for your site.'), |
| 56 | 'callback' => 'actions_manage', |
| 57 | 'access' => $access, |
| 58 | ); |
| 59 | $items[] = array( |
| 60 | 'path' => 'admin/settings/actions/manage', |
| 61 | 'title' => t('Manage actions'), |
| 62 | 'description' => t('Manage the actions defined for your site.'), |
| 63 | 'callback' => 'actions_manage', |
| 64 | 'type' => MENU_DEFAULT_LOCAL_TASK, |
| 65 | 'weight' => -2, |
| 66 | ); |
| 67 | $items[] = array( |
| 68 | 'path' => 'admin/settings/actions/configure', |
| 69 | 'title' => t('Configure an advanced action'), |
| 70 | 'callback' => 'drupal_get_form', |
| 71 | 'callback arguments' => array('actions_configure'), |
| 72 | 'type' => MENU_CALLBACK, |
| 73 | ); |
| 74 | $items[] = array( |
| 75 | 'path' => 'admin/settings/actions/delete', |
| 76 | 'title' => t('Delete action'), |
| 77 | 'description' => t('Delete an action.'), |
| 78 | 'callback' => 'drupal_get_form', |
| 79 | 'callback arguments' => array('actions_delete_form'), |
| 80 | 'type' => MENU_CALLBACK, |
| 81 | ); |
| 82 | $items[] = array( |
| 83 | 'path' => 'admin/settings/actions/orphan', |
| 84 | 'title' => t('Remove orphans'), |
| 85 | 'callback' => 'actions_remove_orphans', |
| 86 | 'type' => MENU_CALLBACK, |
| 87 | ); |
| 88 | $items[] = array( |
| 89 | 'path' => 'admin/build/trigger', |
| 90 | 'title' => t('Triggers'), |
| 91 | 'description' => t('Tell Drupal when to execute actions.'), |
| 92 | 'callback' => 'actions_assign', |
| 93 | 'access' => $access, |
| 94 | ); |
| 95 | // We don't use a menu wildcard here because these are tabs, |
| 96 | // not invisible items. |
| 97 | $items[] = array( |
| 98 | 'path' => 'admin/build/trigger/node', |
| 99 | 'title' => t('Content'), |
| 100 | 'callback' => 'actions_assign', |
| 101 | 'callback arguments' => array('node'), |
| 102 | 'access' => $access, |
| 103 | 'type' => MENU_LOCAL_TASK, |
| 104 | ); |
| 105 | $items[] = array( |
| 106 | 'path' => 'admin/build/trigger/user', |
| 107 | 'title' => t('Users'), |
| 108 | 'callback' => 'actions_assign', |
| 109 | 'callback arguments' => array('user'), |
| 110 | 'access' => $access, |
| 111 | 'type' => MENU_LOCAL_TASK, |
| 112 | ); |
| 113 | $items[] = array( |
| 114 | 'path' => 'admin/build/trigger/comment', |
| 115 | 'title' => t('Comments'), |
| 116 | 'callback' => 'actions_assign', |
| 117 | 'callback arguments' => array('comment'), |
| 118 | 'access' => $access, |
| 119 | 'type' => MENU_LOCAL_TASK, |
| 120 | ); |
| 121 | $items[] = array( |
| 122 | 'path' => 'admin/build/trigger/taxonomy', |
| 123 | 'title' => t('Taxonomy'), |
| 124 | 'callback' => 'actions_assign', |
| 125 | 'callback arguments' => array('taxonomy'), |
| 126 | 'access' => $access, |
| 127 | 'type' => MENU_LOCAL_TASK, |
| 128 | ); |
| 129 | $items[] = array( |
| 130 | 'path' => 'admin/build/trigger/cron', |
| 131 | 'title' => t('Cron'), |
| 132 | 'callback' => 'actions_assign', |
| 133 | 'callback arguments' => array('cron'), |
| 134 | 'type' => MENU_LOCAL_TASK, |
| 135 | ); |
| 136 | |
| 137 | // We want contributed modules to be able to describe |
| 138 | // their hooks and have actions assignable to them. |
| 139 | $hooks = module_invoke_all('hook_info'); |
| 140 | foreach ($hooks as $module => $hook) { |
| 141 | // We've already done these. |
| 142 | if (in_array($module, array('node', 'comment', 'user', 'system', 'taxonomy'))) { |
| 143 | continue; |
| 144 | } |
| 145 | /* // Drupal 5 doesn't have an info column. |
| 146 | $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module)); |
| 147 | $info = unserialize($info); |
| 148 | $nice_name = $info['name']; // */ |
| 149 | // We get the name from the .info file of the module. |
| 150 | $filename = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = 'module'", $module)); |
| 151 | $info = _module_parse_info_file(dirname($filename) .'/'. $module .'.info'); |
| 152 | $items[] = array( |
| 153 | 'path' => 'admin/build/trigger/'. $module, |
| 154 | 'title' => $info['name'], |
| 155 | 'callback' => 'actions_assign', |
| 156 | 'callback arguments' => array($module), |
| 157 | 'access' => $access, |
| 158 | 'type' => MENU_LOCAL_TASK, |
| 159 | ); |
| 160 | } |
| 161 | $items[] = array( |
| 162 | 'path' => 'admin/build/trigger/unassign', |
| 163 | 'title' => t('Unassign'), |
| 164 | 'description' => t('Unassign an action from a trigger.'), |
| 165 | 'callback' => 'drupal_get_form', |
| 166 | 'callback arguments' => array('actions_unassign'), |
| 167 | 'type' => MENU_CALLBACK, |
| 168 | ); |
| 169 | |
| 170 | return $items; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Implementation of hook_perm(). |
| 176 | */ |
| 177 | function actions_perm() { |
| 178 | return array('administer actions'); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Menu callback. Display an overview of available and configured actions. |
| 183 | */ |
| 184 | function actions_manage() { |
| 185 | $output = ''; |
| 186 | $actions = actions_list(); |
| 187 | actions_synchronize($actions); |
| 188 | $actions_map = actions_actions_map($actions); |
| 189 | $options = array(t('Choose an advanced action')); |
| 190 | $unconfigurable = array(); |
| 191 | |
| 192 | foreach ($actions_map as $key => $array) { |
| 193 | if ($array['configurable']) { |
| 194 | $options[$key] = $array['description'] .'...'; |
| 195 | } |
| 196 | else { |
| 197 | $unconfigurable[] = $array; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | $row = array(); |
| 202 | $instances_present = db_fetch_object(db_query("SELECT aid FROM {actions} WHERE parameters <> ''")); |
| 203 | $header = array( |
| 204 | array('data' => t('Action type'), 'field' => 'type'), |
| 205 | array('data' => t('Description'), 'field' => 'description'), |
| 206 | array('data' => $instances_present ? t('Operations') : '', 'colspan' => '2') |
| 207 | ); |
| 208 | $sql = 'SELECT * FROM {actions}'; |
| 209 | $result = pager_query($sql . tablesort_sql($header), 50); |
| 210 | while ($action = db_fetch_object($result)) { |
| 211 | $row[] = array( |
| 212 | array('data' => $action->type), |
| 213 | array('data' => $action->description), |
| 214 | array('data' => $action->parameters ? l(t('configure'), "admin/settings/actions/configure/$action->aid") : ''), |
| 215 | array('data' => $action->parameters ? l(t('delete'), "admin/settings/actions/delete/$action->aid") : '') |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | if ($row) { |
| 220 | $pager = theme('pager', NULL, 50, 0); |
| 221 | if (!empty($pager)) { |
| 222 | $row[] = array(array('data' => $pager, 'colspan' => '3')); |
| 223 | } |
| 224 | $output .= '<h3>'. t('Actions available to Drupal:') .'</h3>'; |
| 225 | $output .= theme('table', $header, $row); |
| 226 | } |
| 227 | |
| 228 | if ($actions_map) { |
| 229 | $output .= drupal_get_form('actions_manage_form', $options); |
| 230 | } |
| 231 | |
| 232 | return $output; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Define the form for the actions overview page. |
| 237 | * |
| 238 | * @see actions_manage_form_submit() |
| 239 | * @ingroup forms |
| 240 | * @param $options |
| 241 | * An array of configurable actions. |
| 242 | * @return |
| 243 | * Form definition. |
| 244 | */ |
| 245 | function actions_manage_form($options = array()) { |
| 246 | $form['parent'] = array( |
| 247 | '#type' => 'fieldset', |
| 248 | '#title' => t('Make a new advanced action available'), |
| 249 | '#prefix' => '<div class="container-inline">', |
| 250 | '#suffix' => '</div>', |
| 251 | ); |
| 252 | $form['parent']['action'] = array( |
| 253 | '#type' => 'select', |
| 254 | '#default_value' => '', |
| 255 | '#options' => $options, |
| 256 | '#description' => '', |
| 257 | ); |
| 258 | $form['parent']['buttons']['submit'] = array( |
| 259 | '#type' => 'submit', |
| 260 | '#value' => t('Create'), |
| 261 | ); |
| 262 | return $form; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Process actions_manage form submissions. |
| 267 | */ |
| 268 | function actions_manage_form_submit($form_id, $form_values) { |
| 269 | if ($form_values['action']) { |
| 270 | return 'admin/settings/actions/configure/'. $form_values['action']; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Menu callback. Create the form for configuration of a single action. |
| 276 | * |
| 277 | * We provide the "Description" field. The rest of the form |
| 278 | * is provided by the action. We then provide the Save button. |
| 279 | * Because we are combining unknown form elements with the action |
| 280 | * configuration form, we use actions_ prefix on our elements. |
| 281 | * |
| 282 | * @see actions_configure_validate() |
| 283 | * @see actions_configure_submit() |
| 284 | * @param $action |
| 285 | * md5 hash of action ID or an integer. If it's an md5 hash, we |
| 286 | * are creating a new instance. If it's an integer, we're editing |
| 287 | * an existing instance. |
| 288 | * @return |
| 289 | * Form definition. |
| 290 | */ |
| 291 | function actions_configure($action = NULL) { |
| 292 | if ($action === NULL) { |
| 293 | drupal_goto('admin/settings/actions'); |
| 294 | } |
| 295 | |
| 296 | $actions_map = actions_actions_map(actions_list()); |
| 297 | $edit = array(); |
| 298 | |
| 299 | // Numeric action denotes saved instance of a configurable action; |
| 300 | // else we are creating a new action instance. |
| 301 | if (is_numeric($action)) { |
| 302 | $aid = $action; |
| 303 | // Load stored parameter values from database. |
| 304 | $data = db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", intval($aid))); |
| 305 | $edit['actions_description'] = $data->description; |
| 306 | $edit['actions_type'] = $data->type; |
| 307 | $function = $data->callback; |
| 308 | $action = md5($data->callback); |
| 309 | $params = unserialize($data->parameters); |
| 310 | if ($params) { |
| 311 | foreach ($params as $name => $val) { |
| 312 | $edit[$name] = $val; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | else { |
| 317 | $function = $actions_map[$action]['callback']; |
| 318 | $edit['actions_description'] = $actions_map[$action]['description']; |
| 319 | $edit['actions_type'] = $actions_map[$action]['type']; |
| 320 | } |
| 321 | |
| 322 | $form = array(); |
| 323 | $form['actions_description'] = array( |
| 324 | '#type' => 'textfield', |
| 325 | '#title' => t('Description'), |
| 326 | '#default_value' => $edit['actions_description'], |
| 327 | '#maxlength' => '255', |
| 328 | '#description' => t('A unique description for this advanced action. This description will be displayed in the interface of modules that integrate with actions, such as Trigger module.'), |
| 329 | '#weight' => -10 |
| 330 | ); |
| 331 | $action_form = $function .'_form'; |
| 332 | $form = array_merge($form, $action_form($edit)); |
| 333 | $form['actions_type'] = array( |
| 334 | '#type' => 'value', |
| 335 | '#value' => $edit['actions_type'], |
| 336 | ); |
| 337 | $form['actions_action'] = array( |
| 338 | '#type' => 'hidden', |
| 339 | '#value' => $action, |
| 340 | ); |
| 341 | // $aid is set when configuring an existing action instance. |
| 342 | if (isset($aid)) { |
| 343 | $form['actions_aid'] = array( |
| 344 | '#type' => 'hidden', |
| 345 | '#value' => $aid, |
| 346 | ); |
| 347 | } |
| 348 | $form['actions_configured'] = array( |
| 349 | '#type' => 'hidden', |
| 350 | '#value' => '1', |
| 351 | ); |
| 352 | $form['buttons']['submit'] = array( |
| 353 | '#type' => 'submit', |
| 354 | '#value' => t('Save'), |
| 355 | '#weight' => 13 |
| 356 | ); |
| 357 | |
| 358 | return $form; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Validate actions_configure form submissions. |
| 363 | */ |
| 364 | function actions_configure_validate($form_id, $form_values) { |
| 365 | $function = actions_function_lookup($form_values['actions_action']) .'_validate'; |
| 366 | // Hand off validation to the action. |
| 367 | if (function_exists($function)) { |
| 368 | $function('validate', $form_values); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Process actions_configure form submissions. |
| 374 | */ |
| 375 | function actions_configure_submit($form_id, $form_values) { |
| 376 | $function = actions_function_lookup($form_values['actions_action']); |
| 377 | $submit_function = $function .'_submit'; |
| 378 | |
| 379 | // Action will return keyed array of values to store. |
| 380 | $params = $submit_function($form_id, $form_values); |
| 381 | $aid = isset($form_values['actions_aid']) ? $form_values['actions_aid'] : NULL; |
| 382 | |
| 383 | actions_save($function, $form_values['actions_type'], $params, $form_values['actions_description'], $aid); |
| 384 | drupal_set_message(t('The action has been successfully saved.')); |
| 385 | |
| 386 | return 'admin/settings/actions/manage'; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Create the form for confirmation of deleting an action. |
| 391 | * |
| 392 | * @ingroup forms |
| 393 | * @see actions_delete_form_submit() |
| 394 | */ |
| 395 | function actions_delete_form($aid) {; |
| 396 | $action = actions_load($aid); |
| 397 | $form['aid'] = array( |
| 398 | '#type' => 'hidden', |
| 399 | '#value' => $action->aid, |
| 400 | ); |
| 401 | return confirm_form($form, |
| 402 | t('Are you sure you want to delete the action %action?', array('%action' => $action->description)), |
| 403 | 'admin/settings/actions/manage', |
| 404 | t('This cannot be undone.'), |
| 405 | t('Delete'), t('Cancel') |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Process actions_delete form submissions. |
| 411 | * |
| 412 | * Post-deletion operations for action deletion. |
| 413 | */ |
| 414 | function actions_delete_form_submit($form_id, $form_values) { |
| 415 | $aid = $form_values['aid']; |
| 416 | $action = actions_load($aid); |
| 417 | actions_delete($aid); |
| 418 | $description = check_plain($action->description); |
| 419 | watchdog('user', 'Deleted action %aid (%action)', array('%aid' => $aid, '%action' => $description)); |
| 420 | drupal_set_message(t('Action %action was deleted', array('%action' => $description))); |
| 421 | return 'admin/settings/actions/manage'; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Post-deletion operations for deleting action orphans. |
| 426 | * |
| 427 | * @param $orphaned |
| 428 | * An array of orphaned actions. |
| 429 | */ |
| 430 | function actions_action_delete_orphans_post($orphaned) { |
| 431 | foreach ($orphaned as $callback) { |
| 432 | drupal_set_message(t("Deleted orphaned action (%action).", array('%action' => $callback))); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Remove actions that are in the database but not supported by any enabled module. |
| 438 | */ |
| 439 | function actions_remove_orphans() { |
| 440 | actions_synchronize(actions_list(), TRUE); |
| 441 | drupal_goto('admin/settings/actions/manage'); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Admin page callbacks for the trigger module. |
| 446 | */ |
| 447 | |
| 448 | /** |
| 449 | * Build the form that allows users to assign actions to hooks. |
| 450 | * |
| 451 | * @param $type |
| 452 | * Name of hook. |
| 453 | * @return |
| 454 | * HTML form. |
| 455 | */ |
| 456 | function actions_assign($type = NULL) { |
| 457 | |
| 458 | // If no type is specified we default to node actions, since they |
| 459 | // are the most common. |
| 460 | if (!isset($type)) { |
| 461 | drupal_goto('admin/build/trigger/node'); |
| 462 | } |
| 463 | if ($type == 'node') { |
| 464 | $type = 'nodeapi'; |
| 465 | } |
| 466 | |
| 467 | $output = ''; |
| 468 | $hooks = module_invoke_all('hook_info'); |
| 469 | foreach ($hooks as $module => $hook) { |
| 470 | if (isset($hook[$type])) { |
| 471 | foreach ($hook[$type] as $op => $description) { |
| 472 | $form_id = 'actions_'. $type .'_'. $op .'_assign_form'; |
| 473 | $output .= drupal_get_form($form_id, $type, $op, $description['runs when']); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | return $output; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Confirm removal of an assigned action. |
| 482 | * |
| 483 | * @param $hook |
| 484 | * @param $op |
| 485 | * @param $aid |
| 486 | * The action ID. |
| 487 | * @ingroup forms |
| 488 | * @see actions_unassign_submit() |
| 489 | */ |
| 490 | function actions_unassign($hook = NULL, $op = NULL, $aid = NULL, $form_values = NULL) { |
| 491 | if (!($hook && $op && $aid)) { |
| 492 | drupal_goto('admin/build/trigger/assign'); |
| 493 | } |
| 494 | |
| 495 | $form['hook'] = array( |
| 496 | '#type' => 'value', |
| 497 | '#value' => $hook, |
| 498 | ); |
| 499 | $form['operation'] = array( |
| 500 | '#type' => 'value', |
| 501 | '#value' => $op, |
| 502 | ); |
| 503 | $form['aid'] = array( |
| 504 | '#type' => 'value', |
| 505 | '#value' => $aid, |
| 506 | ); |
| 507 | |
| 508 | $action = db_result(db_query("SELECT aid FROM {actions} WHERE MD5(aid) = '%s' AND parameters <> ''", $aid)); |
| 509 | $actions = actions_get_all_actions(); |
| 510 | |
| 511 | $destination = 'admin/build/trigger/'. ($hook == 'nodeapi' ? 'node' : $hook); |
| 512 | |
| 513 | return confirm_form($form, |
| 514 | t('Are you sure you want to unassign the action %title?', array('%title' => $actions[$action]['description'])), |
| 515 | $destination, |
| 516 | t('You can assign it again later if you wish.'), |
| 517 | t('Unassign'), t('Cancel') |
| 518 | ); |
| 519 | } |
| 520 | |
| 521 | function actions_unassign_submit($form_id, $form_values) { |
| 522 | if ($form_values['confirm'] == 1) { |
| 523 | $aid = db_result(db_query("SELECT aid FROM {actions} WHERE MD5(aid) = '%s'", $form_values['aid'])); |
| 524 | db_query("DELETE FROM {actions_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid); |
| 525 | watchdog('actions', t('Action %action has been unassigned.', array('%action' => check_plain($actions[$aid]['description'])))); |
| 526 | drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['description']))); |
| 527 | $hook = $form_values['hook'] == 'nodeapi' ? 'node' : $form_values['hook']; |
| 528 | return 'admin/build/trigger/'. $hook; |
| 529 | } |
| 530 | else { |
| 531 | drupal_goto('admin/build/trigger'); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Create the form definition for assigning an action to a hook-op combination. |
| 537 | * |
| 538 | * @param $form_values |
| 539 | * Information about the current form. |
| 540 | * @param $hook |
| 541 | * The name of the hook, e.g., 'nodeapi'. |
| 542 | * @param $op |
| 543 | * The name of the hook operation, e.g., 'insert'. |
| 544 | * @param $description |
| 545 | * A plain English description of what this hook operation does. |
| 546 | * @return |
| 547 | * |
| 548 | * @ingoup forms |
| 549 | * @see actions_assign_form_validate() |
| 550 | * @see actions_assign_form_submit() |
| 551 | */ |
| 552 | function actions_assign_form($hook, $op, $description, $form_value = NULL) { |
| 553 | $form['hook'] = array( |
| 554 | '#type' => 'hidden', |
| 555 | '#value' => $hook, |
| 556 | ); |
| 557 | $form['operation'] = array( |
| 558 | '#type' => 'hidden', |
| 559 | '#value' => $op, |
| 560 | ); |
| 561 | // All of these forms use the same validate and submit functions. |
| 562 | $form['#validate'] = array('actions_assign_form_validate' => array($form_id, &$form)); |
| 563 | $form['#submit'] = array('actions_assign_form_submit' => array($form_id, &$form)); |
| 564 | |
| 565 | $options = array(); |
| 566 | $functions = array(); |
| 567 | // Restrict the options list to actions that declare support for this hook-op |
| 568 | // combination. |
| 569 | foreach (actions_list() as $func => $metadata) { |
| 570 | if (isset($metadata['hooks']['any']) // The action supports any hook. |
| 571 | || (isset($metadata['hooks'][$hook]) // The action has declared which hooks it supports |
| 572 | && is_array($metadata['hooks'][$hook]) // by defining an array. |
| 573 | // Either the current op must be in the array or the token 'any' must be in the array. |
| 574 | && ((in_array($op, $metadata['hooks'][$hook])) || in_array('any', $metadata['hooks'][$hook])))) { |
| 575 | $functions[] = $func; |
| 576 | } |
| 577 | } |
| 578 | foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) { |
| 579 | if (in_array($action['callback'], $functions)) { |
| 580 | $options[$action['type']][$aid] = $action['description']; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | $form[$op] = array( |
| 585 | '#type' => 'fieldset', |
| 586 | '#title' => t('Trigger: ') . $description, |
| 587 | '#theme' => 'actions_display' |
| 588 | ); |
| 589 | // Retrieve actions that are already assigned to this hook-op combination. |
| 590 | $actions = _actions_get_hook_actions($hook, $op); |
| 591 | $form[$op]['assigned']['#type'] = 'value'; |
| 592 | $form[$op]['assigned']['#value'] = array(); |
| 593 | foreach ($actions as $aid => $description) { |
| 594 | $form[$op]['assigned']['#value'][$aid] = array( |
| 595 | 'description' => $description, |
| 596 | 'link' => l(t('unassign'), "admin/build/trigger/unassign/$hook/$op/". md5($aid)) |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | $form[$op]['parent'] = array( |
| 601 | '#prefix' => "<div class='container-inline'>", |
| 602 | '#suffix' => '</div>', |
| 603 | ); |
| 604 | // List possible actions that may be assigned. |
| 605 | if (count($options) != 0) { |
| 606 | array_unshift($options, t('Choose an action')); |
| 607 | $form[$op]['parent']['aid'] = array( |
| 608 | '#type' => 'select', |
| 609 | '#options' => $options, |
| 610 | ); |
| 611 | $form[$op]['parent']['submit'] = array( |
| 612 | '#type' => 'submit', |
| 613 | '#value' => t('Assign') |
| 614 | ); |
| 615 | } |
| 616 | else { |
| 617 | $form[$op]['none'] = array( |
| 618 | '#value' => t('No available actions for this trigger.') |
| 619 | ); |
| 620 | } |
| 621 | return $form; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Validation function for actions_assign_form(). |
| 626 | * |
| 627 | * Makes sure that the user is not re-assigning an action to an event. |
| 628 | */ |
| 629 | function actions_assign_form_validate($form_id, $form_values) { |
| 630 | if (!empty($form_values['aid'])) { |
| 631 | $aid = actions_function_lookup($form_values['aid']); |
| 632 | if (db_result(db_query("SELECT aid FROM {actions_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid))) { |
| 633 | form_set_error($form_values['operation'], t('The action you chose is already assigned to that trigger.')); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Submit function for actions_assign_form(). |
| 640 | */ |
| 641 | function actions_assign_form_submit($form_id, $form_values) { |
| 642 | if (!empty($form_values['aid'])) { |
| 643 | $callback = actions_function_lookup($form_values['aid']); |
| 644 | $aid = db_result(db_query("SELECT aid FROM {actions} WHERE MD5(aid) = '%s'", $form_values['aid'])); |
| 645 | $weight = db_result(db_query("SELECT MAX(weight) FROM {actions_assignments} WHERE hook = '%s' AND op = '%s'", $form_values['hook'], $form_values['operation'])); |
| 646 | db_query("INSERT INTO {actions_assignments} values ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1); |
| 647 | // If this action changes a node property, we need to save the node |
| 648 | // so the change will persist. |
| 649 | $actions = actions_list(); |
| 650 | if (isset($actions[$callback]['behavior']) && in_array('changes_node_property', $actions[$callback]['behavior']) && ($form_values['operation'] != 'presave')) { |
| 651 | // Delete previous node_save_action if it exists, and re-add a new one at a higher weight. |
| 652 | $save_post_action_assigned = db_result(db_query("SELECT aid FROM {actions_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation'])); |
| 653 | if ($save_post_action_assigned) { |
| 654 | db_query("DELETE FROM {actions_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation']); |
| 655 | } |
| 656 | db_query("INSERT INTO {actions_assignments} VALUES ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], 'node_save_action', $weight + 2); |
| 657 | if (!$save_post_action_assigned) { |
| 658 | drupal_set_message(t('You have added an action that changes a the property of a post. A Save post action has been added so that the property change will be saved.')); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Display actions assigned to this hook-op combination in a table. |
| 666 | * |
| 667 | * @param array $element |
| 668 | * The fieldset including all assigned actions. |
| 669 | * @return |
| 670 | * The rendered form with the table prepended. |
| 671 | * |
| 672 | * @ingroup themeable |
| 673 | */ |
| 674 | function theme_actions_display($element) { |
| 675 | $header = array(); |
| 676 | $rows = array(); |
| 677 | if (count($element['assigned']['#value'])) { |
| 678 | $header = array(array('data' => t('Name')), array('data' => t('Operation'))); |
| 679 | $rows = array(); |
| 680 | foreach ($element['assigned']['#value'] as $aid => $info) { |
| 681 | $rows[] = array( |
| 682 | $info['description'], |
| 683 | $info['link'] |
| 684 | ); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (count($rows)) { |
| 689 | $output = theme('table', $header, $rows) . drupal_render($element); |
| 690 | } |
| 691 | else { |
| 692 | $output = drupal_render($element); |
| 693 | } |
| 694 | return $output; |
| 695 | } |
| 696 | |
| 697 | |
| 698 | /** |
| 699 | * Get the actions that have already been defined for this |
| 700 | * type-hook-op combination. |
| 701 | * |
| 702 | * @param $type |
| 703 | * One of 'node', 'user', 'comment'. |
| 704 | * @param $hook |
| 705 | * The name of the hook for which actions have been assigned, |
| 706 | * e.g. 'nodeapi'. |
| 707 | * @param $op |
| 708 | * The hook operation for which the actions have been assigned, |
| 709 | * e.g., 'view'. |
| 710 | * @return |
| 711 | * An array of action descriptions keyed by action IDs. |
| 712 | */ |
| 713 | function _actions_get_hook_actions($hook, $op, $type = NULL) { |
| 714 | $actions = array(); |
| 715 | if ($type) { |
| 716 | $result = db_query("SELECT h.aid, a.description FROM {actions_assignments} h LEFT JOIN {actions} a on a.aid = h.aid WHERE a.type = '%s' AND h.hook = '%s' AND h.op = '%s' ORDER BY h.weight", $type, $hook, $op); |
| 717 | } |
| 718 | else { |
| 719 | $result = db_query("SELECT h.aid, a.description FROM {actions_assignments} h LEFT JOIN {actions} a on a.aid = h.aid WHERE h.hook = '%s' AND h.op = '%s' ORDER BY h.weight", $hook, $op); |
| 720 | } |
| 721 | while ($action = db_fetch_object($result)) { |
| 722 | $actions[$action->aid] = $action->description; |
| 723 | } |
| 724 | return $actions; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Get the aids of actions to be executed for a hook-op combination. |
| 729 | * |
| 730 | * @param $hook |
| 731 | * The name of the hook being fired. |
| 732 | * @param $op |
| 733 | * The name of the operation being executed. Defaults to an empty string |
| 734 | * because some hooks (e.g., hook_cron()) do not have operations. |
| 735 | * @return |
| 736 | * An array of action IDs. |
| 737 | */ |
| 738 | function _actions_get_hook_aids($hook, $op = '') { |
| 739 | $aids = array(); |
| 740 | $result = db_query("SELECT aa.aid, a.type FROM {actions_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = '%s' AND aa.op = '%s' ORDER BY weight", $hook, $op); |
| 741 | while ($action = db_fetch_object($result)) { |
| 742 | $aids[$action->aid]['type'] = $action->type; |
| 743 | } |
| 744 | return $aids; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Implementation of hook_theme(). |
| 749 | */ |
| 750 | function actions_theme() { |
| 751 | return array( |
| 752 | 'actions_display' => array( |
| 753 | 'arguments' => array('element'), |
| 754 | ), |
| 755 | ); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Implementation of hook_forms(). We reuse code by using the |
| 760 | * same assignment form definition for each node-op combination. |
| 761 | */ |
| 762 | function actions_forms() { |
| 763 | $hooks = module_invoke_all('hook_info'); |
| 764 | foreach ($hooks as $module => $info) { |
| 765 | foreach ($hooks[$module] as $hook => $ops) { |
| 766 | foreach ($ops as $op => $description) { |
| 767 | $forms['actions_'. $hook .'_'. $op .'_assign_form'] = array('callback' => 'actions_assign_form'); |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return $forms; |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * When an action is called in a context that does not match its type, |
| 777 | * the object that the action expects must be retrieved. For example, when |
| 778 | * an action that works on users is called during the node hook, the |
| 779 | * user object is not available since the node hook doesn't pass it. |
| 780 | * So here we load the object the action expects. |
| 781 | * |
| 782 | * @param $type |
| 783 | * The type of action that is about to be called. |
| 784 | * @param $node |
| 785 | * The node that was passed via the nodeapi hook. |
| 786 | * @return |
| 787 | * The object expected by the action that is about to be called. |
| 788 | */ |
| 789 | function _actions_normalize_node_context($type, $node) { |
| 790 | switch ($type) { |
| 791 | // If an action that works on comments is being called in a node context, |
| 792 | // the action is expecting a comment object. But we do not know which comment |
| 793 | // to give it. The first? The most recent? All of them? So comment actions |
| 794 | // in a node context are not supported. |
| 795 | |
| 796 | // An action that works on users is being called in a node context. |
| 797 | // Load the user object of the node's author. |
| 798 | case 'user': |
| 799 | return user_load(array('uid' => $node->uid)); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Implementation of hook_nodeapi(). |
| 805 | */ |
| 806 | function actions_nodeapi(&$node, $op, $a3, $a4) { |
| 807 | // Keep objects for reuse so that changes actions make to objects can persist. |
| 808 | static $objects; |
| 809 | // Prevent recursion by tracking which operations have already been called. |
| 810 | static $recursion; |
| 811 | // Support a subset of operations. |
| 812 | if (!in_array($op, array('view', 'update', 'presave', 'insert', 'delete')) || isset($recursion[$op][$node->nid])) { |
| 813 | return; |
| 814 | } |
| 815 | $recursion[$op][$node->nid] = TRUE; |
| 816 | |
| 817 | $aids = _actions_get_hook_aids('nodeapi', $op); |
| 818 | if (!$aids) { |
| 819 | return; |
| 820 | } |
| 821 | $context = array( |
| 822 | 'hook' => 'nodeapi', |
| 823 | 'op' => $op, |
| 824 | ); |
| 825 | |
| 826 | // We need to get the expected object if the action's type is not 'node'. |
| 827 | // We keep the object in $objects so we can reuse it if we have multiple actions |
| 828 | // that make changes to an object. |
| 829 | foreach ($aids as $aid => $action_info) { |
| 830 | if ($action_info['type'] != 'node') { |
| 831 | if (!isset($objects[$action_info['type']])) { |
| 832 | $objects[$action_info['type']] = _actions_normalize_node_context($action_info['type'], $node); |
| 833 | } |
| 834 | // Since we know about the node, we pass that info along to the action. |
| 835 | $context['node'] = $node; |
| 836 | $result = actions_do($aid, $objects[$action_info['type']], $context, $a4, $a4); |
| 837 | } |
| 838 | else { |
| 839 | actions_do($aid, $node, $context, $a3, $a4); |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * When an action is called in a context that does not match its type, |
| 846 | * the object that the action expects must be retrieved. For example, when |
| 847 | * an action that works on nodes is called during the comment hook, the |
| 848 | * node object is not available since the comment hook doesn't pass it. |
| 849 | * So here we load the object the action expects. |
| 850 | * |
| 851 | * @param $type |
| 852 | * The type of action that is about to be called. |
| 853 | * @param $comment |
| 854 | * The comment that was passed via the comment hook. |
| 855 | * @return |
| 856 | * The object expected by the action that is about to be called. |
| 857 | */ |
| 858 | function _actions_normalize_comment_context($type, $comment) { |
| 859 | switch ($type) { |
| 860 | // An action that works with nodes is being called in a comment context. |
| 861 | case 'node': |
| 862 | return node_load(is_array($comment) ? $comment['nid'] : $comment->nid); |
| 863 | |
| 864 | // An action that works on users is being called in a comment context. |
| 865 | case 'user': |
| 866 | return user_load(array('uid' => is_array($comment) ? $comment['uid'] : $comment->uid)); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * Implementation of hook_comment(). |
| 872 | */ |
| 873 | function actions_comment($a1, $op) { |
| 874 | // Keep objects for reuse so that changes actions make to objects can persist. |
| 875 | static $objects; |
| 876 | // We support a subset of operations. |
| 877 | if (!in_array($op, array('insert', 'update', 'delete', 'view'))) { |
| 878 | return; |
| 879 | } |
| 880 | $aids = _actions_get_hook_aids('comment', $op); |
| 881 | $context = array( |
| 882 | 'hook' => 'comment', |
| 883 | 'op' => $op, |
| 884 | ); |
| 885 | // We need to get the expected object if the action's type is not 'comment'. |
| 886 | // We keep the object in $objects so we can reuse it if we have multiple actions |
| 887 | // that make changes to an object. |
| 888 | foreach ($aids as $aid => $action_info) { |
| 889 | if ($action_info['type'] != 'comment') { |
| 890 | if (!isset($objects[$action_info['type']])) { |
| 891 | $objects[$action_info['type']] = _actions_normalize_comment_context($action_info['type'], $a1); |
| 892 | } |
| 893 | // Since we know about the comment, we pass it along to the action |
| 894 | // in case it wants to peek at it. |
| 895 | $context['comment'] = (object) $a1; |
| 896 | actions_do($aid, $objects[$action_info['type']], $context); |
| 897 | } |
| 898 | else { |
| 899 | $comment = (object) $a1; |
| 900 | actions_do($aid, $comment, $context); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /** |
| 906 | * Implementation of hook_cron(). |
| 907 | */ |
| 908 | function actions_cron() { |
| 909 | $aids = _actions_get_hook_aids('cron'); |
| 910 | $context = array( |
| 911 | 'hook' => 'cron', |
| 912 | 'op' => '', |
| 913 | ); |
| 914 | // Cron does not act on any specific object. |
| 915 | $object = NULL; |
| 916 | actions_do(array_keys($aids), $object, $context); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * When an action is called in a context that does not match its type, |
| 921 | * the object that the action expects must be retrieved. For example, when |
| 922 | * an action that works on nodes is called during the user hook, the |
| 923 | * node object is not available since the user hook doesn't pass it. |
| 924 | * So here we load the object the action expects. |
| 925 | * |
| 926 | * @param $type |
| 927 | * The type of action that is about to be called. |
| 928 | * @param $account |
| 929 | * The account object that was passed via the user hook. |
| 930 | * @return |
| 931 | * The object expected by the action that is about to be called. |
| 932 | */ |
| 933 | function _actions_normalize_user_context($type, $account) { |
| 934 | switch ($type) { |
| 935 | // If an action that works on comments is being called in a user context, |
| 936 | // the action is expecting a comment object. But we have no way of |
| 937 | // determining the appropriate comment object to pass. So comment |
| 938 | // actions in a user context are not supported. |
| 939 | |
| 940 | // An action that works with nodes is being called in a user context. |
| 941 | // If a single node is being viewed, return the node. |
| 942 | case 'node': |
| 943 | // If we are viewing an individual node, return the node. |
| 944 | if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) { |
| 945 | return node_load(array('nid' => arg(1))); |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Implementation of hook_user(). |
| 952 | */ |
| 953 | function actions_user($op, &$edit, &$account, $category = NULL) { |
| 954 | // Keep objects for reuse so that changes actions make to objects can persist. |
| 955 | static $objects; |
| 956 | // We support a subset of operations. |
| 957 | if (!in_array($op, array('login', 'logout', 'insert', 'update', 'delete', 'view'))) { |
| 958 | return; |
| 959 | } |
| 960 | $aids = _actions_get_hook_aids('user', $op); |
| 961 | $context = array( |
| 962 | 'hook' => 'user', |
| 963 | 'op' => $op, |
| 964 | 'form_values' => &$edit, |
| 965 | ); |
| 966 | foreach ($aids as $aid => $action_info) { |
| 967 | if ($action_info['type'] != 'user') { |
| 968 | if (!isset($objects[$action_info['type']])) { |
| 969 | $objects[$action_info['type']] = _actions_normalize_user_context($action_info['type'], $account); |
| 970 | } |
| 971 | $context['account'] = $account; |
| 972 | actions_do($aid, $objects[$action_info['type']], $context); |
| 973 | } |
| 974 | else { |
| 975 | actions_do($aid, $account, $context, $category); |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * Implementation of hook_taxonomy(). |
| 982 | */ |
| 983 | function actions_taxonomy($op, $type, $array) { |
| 984 | if ($type != 'term') { |
| 985 | return; |
| 986 | } |
| 987 | $aids = _actions_get_hook_aids('taxonomy', $op); |
| 988 | $context = array( |
| 989 | 'hook' => 'taxonomy', |
| 990 | 'op' => $op |
| 991 | ); |
| 992 | foreach ($aids as $aid => $action_info) { |
| 993 | $taxonomy_object = (object) $array; |
| 994 | actions_do($aid, $taxonomy_object, $context); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * Often we generate a select field of all actions. This function |
| 1000 | * generates the options for that select. |
| 1001 | * |
| 1002 | * @param $type |
| 1003 | * One of 'node', 'user', 'comment'. |
| 1004 | * @return |
| 1005 | * Array keyed by action ID. |
| 1006 | */ |
| 1007 | function actions_options($type = 'all') { |
| 1008 | $options = array(t('Choose an action')); |
| 1009 | foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) { |
| 1010 | $options[$action['type']][$aid] = $action['description']; |
| 1011 | } |
| 1012 | |
| 1013 | if ($type == 'all') { |
| 1014 | return $options; |
| 1015 | } |
| 1016 | else { |
| 1017 | return $options[$type]; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * Implementation of hook_actions_delete(). |
| 1023 | * |
| 1024 | * Remove all trigger entries for the given action, when deleted. |
| 1025 | */ |
| 1026 | function actions_actions_delete($aid) { |
| 1027 | db_query("DELETE FROM {actions_assignments} WHERE aid = '%s'", $aid); |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * Implementation of hook_hook_info(). |
| 1032 | */ |
| 1033 | function system_hook_info() { |
| 1034 | return array( |
| 1035 | 'system' => array( |
| 1036 | 'cron' => array( |
| 1037 | 'run' => array( |
| 1038 | 'runs when' => t('When cron runs'), |
| 1039 | ), |
| 1040 | ), |
| 1041 | ), |
| 1042 | ); |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * Return a form definition so the Send email action can be configured. |
| 1047 | * |
| 1048 | * @see system_send_email_action_validate() |
| 1049 | * @see system_send_email_action_submit() |
| 1050 | * @param $context |
| 1051 | * Default values (if we are editing an existing action instance). |
| 1052 | * @return |
| 1053 | * Form definition. |
| 1054 | */ |
| 1055 | function system_send_email_action_form($context) { |
| 1056 | // Set default values for form. |
| 1057 | if (!isset($context['recipient'])) { |
| 1058 | $context['recipient'] = ''; |
| 1059 | } |
| 1060 | if (!isset($context['subject'])) { |
| 1061 | $context['subject'] = ''; |
| 1062 | } |
| 1063 | if (!isset($context['message'])) { |
| 1064 | $context['message'] = ''; |
| 1065 | } |
| 1066 | |
| 1067 | $form['recipient'] = array( |
| 1068 | '#type' => 'textfield', |
| 1069 | '#title' => t('Recipient'), |
| 1070 | '#default_value' => $context['recipient'], |
| 1071 | '#maxlength' => '254', |
| 1072 | '#description' => t('The email address to which the message should be sent OR enter %author if you would like to send an e-mail to the author of the original post.', array('%author' => '%author')), |
| 1073 | ); |
| 1074 | $form['subject'] = array( |
| 1075 | '#type' => 'textfield', |
| 1076 | '#title' => t('Subject'), |
| 1077 | '#default_value' => $context['subject'], |
| 1078 | '#maxlength' => '254', |
| 1079 | '#description' => t('The subject of the message.'), |
| 1080 | ); |
| 1081 | $form['message'] = array( |
| 1082 | '#type' => 'textarea', |
| 1083 | '#title' => t('Message'), |
| 1084 | '#default_value' => $context['message'], |
| 1085 | '#cols' => '80', |
| 1086 | '#rows' => '20', |
| 1087 | '#description' => t('The message that should be sent. You may include the following variables: %site_name, %username, %uid, %node_url, %node_alias, %node_type, %title, %teaser, %body. Not all variables will be available in all contexts.'), |
| 1088 | ); |
| 1089 | return $form; |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * Validate system_send_email_action form submissions. |
| 1094 | */ |
| 1095 | function system_send_email_action_validate($form_id, $form_values) { |
| 1096 | // Validate the configuration form. |
| 1097 | if (!valid_email_address($form_values['recipient']) && $form_values['recipient'] != '%author') { |
| 1098 | // We want the literal %author placeholder to be emphasized in the error message. |
| 1099 | form_set_error('recipient', t('Please enter a valid email address or %author.', array('%author' => '%author'))); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Process system_send_email_action form submissions. |
| 1105 | */ |
| 1106 | function system_send_email_action_submit($form_id, $form_values) { |
| 1107 | // Process the HTML form to store configuration. The keyed array that |
| 1108 | // we return will be serialized to the database. |
| 1109 | $params = array( |
| 1110 | 'recipient' => $form_values['recipient'], |
| 1111 | 'subject' => $form_values['subject'], |
| 1112 | 'message' => $form_values['message'], |
| 1113 | ); |
| 1114 | return $params; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Implementation of a configurable Drupal action. Sends an email. |
| 1119 | */ |
| 1120 | function system_send_email_action($object, $context) { |
| 1121 | global $user; |
| 1122 | |
| 1123 | switch ($context['hook']) { |
| 1124 | case 'nodeapi': |
| 1125 | // Because this is not an action of type 'node' the node |
| 1126 | // will not be passed as $object, but it will still be available |
| 1127 | // in $context. |
| 1128 | $node = $context['node']; |
| 1129 | break; |
| 1130 | case 'comment': |
| 1131 | // The comment hook provides nid, in $context. |
| 1132 | $comment = $context['comment']; |
| 1133 | $node = node_load($comment->nid); |
| 1134 | break; |
| 1135 | case 'user': |
| 1136 | // Because this is not an action of type 'user' the user |
| 1137 | // object is not passed as $object, but it will still be available |
| 1138 | // in $context. |
| 1139 | $account = $context['account']; |
| 1140 | if (isset($context['node'])) { |
| 1141 | $node = $context['node']; |
| 1142 | } |
| 1143 | elseif ($context['recipient'] == '%author') { |
| 1144 | // If we don't have a node, we don't have a node author. |
| 1145 | watchdog('error', t("Cannot use '%author' token in this context.")); |
| 1146 | return; |
| 1147 | } |
| 1148 | break; |
| 1149 | default: |
| 1150 | // Check context for node. |
| 1151 | if (!isset($object) && isset($context['node'])) { |
| 1152 | $node = $context['node']; |
| 1153 | } |
| 1154 | else { |
| 1155 | // We are being called directly. |
| 1156 | $node = $object; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | $recipient = $context['recipient']; |
| 1161 | |
| 1162 | if (isset($node)) { |
| 1163 | if (!isset($account)) { |
| 1164 | $account = user_load(array('uid' => $node->uid)); |
| 1165 | } |
| 1166 | if ($recipient == '%author') { |
| 1167 | $recipient = $account->mail; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | if (!isset($account)) { |
| 1172 | $account = $user; |
| 1173 | } |
| 1174 | |
| 1175 | $subject = $context['subject']; |
| 1176 | $message = $context['message']; |
| 1177 | $from = variable_get('site_mail', ini_get('sendmail_from')); |
| 1178 | |
| 1179 | $variables = array( |
| 1180 | '%site_name' => variable_get('site_name', 'Drupal'), |
| 1181 | '%username' => $account-> |