Parent Directory
|
Revision Log
|
Revision Graph
#311170 by Liliplanet: use theme function for username in system_message_action
| 1 | jvandyk | 1.1 | <?php |
| 2 | jvandyk | 1.43 | // $Id: actions.module,v 1.42 2008/09/08 17:58:46 jvandyk Exp $ |
| 3 | jvandyk | 1.1 | |
| 4 | /** | ||
| 5 | jvandyk | 1.31 | * @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 | jvandyk | 1.1 | |
| 12 | /** | ||
| 13 | jvandyk | 1.31 | * Implementation of hook_help(). |
| 14 | */ | ||
| 15 | jvandyk | 1.1 | function actions_help($section) { |
| 16 | jvandyk | 1.31 | $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 | jvandyk | 1.1 | switch ($section) { |
| 18 | jvandyk | 1.31 | 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 | jvandyk | 1.1 | } |
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | jvandyk | 1.31 | * Implementation of hook_menu(). |
| 46 | */ | ||
| 47 | jvandyk | 1.1 | function actions_menu($may_cache) { |
| 48 | $items = array(); | ||
| 49 | jvandyk | 1.40 | $access = user_access('administer actions'); |
| 50 | jvandyk | 1.32 | |
| 51 | jvandyk | 1.1 | if ($may_cache) { |
| 52 | jvandyk | 1.24 | $items[] = array( |
| 53 | jvandyk | 1.31 | '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 | jvandyk | 1.26 | $items[] = array( |
| 83 | jvandyk | 1.31 | 'path' => 'admin/settings/actions/orphan', |
| 84 | 'title' => t('Remove orphans'), | ||
| 85 | 'callback' => 'actions_remove_orphans', | ||
| 86 | 'type' => MENU_CALLBACK, | ||
| 87 | ); | ||
| 88 | jvandyk | 1.26 | $items[] = array( |
| 89 | jvandyk | 1.31 | 'path' => 'admin/build/trigger', |
| 90 | 'title' => t('Triggers'), | ||
| 91 | jvandyk | 1.26 | 'description' => t('Tell Drupal when to execute actions.'), |
| 92 | jvandyk | 1.31 | 'callback' => 'actions_assign', |
| 93 | jvandyk | 1.26 | 'access' => $access, |
| 94 | ); | ||
| 95 | jvandyk | 1.31 | // We don't use a menu wildcard here because these are tabs, |
| 96 | // not invisible items. | ||
| 97 | jvandyk | 1.26 | $items[] = array( |
| 98 | jvandyk | 1.31 | 'path' => 'admin/build/trigger/node', |
| 99 | 'title' => t('Content'), | ||
| 100 | jvandyk | 1.26 | 'callback' => 'actions_assign', |
| 101 | 'callback arguments' => array('node'), | ||
| 102 | jvandyk | 1.31 | 'access' => $access, |
| 103 | 'type' => MENU_LOCAL_TASK, | ||
| 104 | jvandyk | 1.26 | ); |
| 105 | $items[] = array( | ||
| 106 | jvandyk | 1.31 | 'path' => 'admin/build/trigger/user', |
| 107 | 'title' => t('Users'), | ||
| 108 | jvandyk | 1.26 | 'callback' => 'actions_assign', |
| 109 | 'callback arguments' => array('user'), | ||
| 110 | jvandyk | 1.31 | 'access' => $access, |
| 111 | 'type' => MENU_LOCAL_TASK, | ||
| 112 | jvandyk | 1.26 | ); |
| 113 | $items[] = array( | ||
| 114 | jvandyk | 1.31 | 'path' => 'admin/build/trigger/comment', |
| 115 | 'title' => t('Comments'), | ||
| 116 | 'callback' => 'actions_assign', | ||
| 117 | 'callback arguments' => array('comment'), | ||
| 118 | jvandyk | 1.26 | 'access' => $access, |
| 119 | jvandyk | 1.31 | 'type' => MENU_LOCAL_TASK, |
| 120 | ); | ||
| 121 | $items[] = array( | ||
| 122 | 'path' => 'admin/build/trigger/taxonomy', | ||
| 123 | 'title' => t('Taxonomy'), | ||
| 124 | jvandyk | 1.26 | 'callback' => 'actions_assign', |
| 125 | jvandyk | 1.31 | 'callback arguments' => array('taxonomy'), |
| 126 | 'access' => $access, | ||
| 127 | 'type' => MENU_LOCAL_TASK, | ||
| 128 | jvandyk | 1.26 | ); |
| 129 | $items[] = array( | ||
| 130 | jvandyk | 1.31 | 'path' => 'admin/build/trigger/cron', |
| 131 | jvandyk | 1.26 | 'title' => t('Cron'), |
| 132 | 'callback' => 'actions_assign', | ||
| 133 | 'callback arguments' => array('cron'), | ||
| 134 | jvandyk | 1.31 | 'type' => MENU_LOCAL_TASK, |
| 135 | jvandyk | 1.26 | ); |
| 136 | jvandyk | 1.31 | |
| 137 | jvandyk | 1.32 | // We want contributed modules to be able to describe |
| 138 | // their hooks and have actions assignable to them. | ||
| 139 | jvandyk | 1.31 | $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 | jvandyk | 1.32 | /* // Drupal 5 doesn't have an info column. |
| 146 | jvandyk | 1.31 | $info = db_result(db_query("SELECT info FROM {system} WHERE name = '%s'", $module)); |
| 147 | $info = unserialize($info); | ||
| 148 | jvandyk | 1.32 | $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 | jvandyk | 1.31 | $items[] = array( |
| 153 | 'path' => 'admin/build/trigger/'. $module, | ||
| 154 | jvandyk | 1.32 | 'title' => $info['name'], |
| 155 | jvandyk | 1.31 | 'callback' => 'actions_assign', |
| 156 | 'callback arguments' => array($module), | ||
| 157 | jvandyk | 1.32 | 'access' => $access, |
| 158 | jvandyk | 1.31 | 'type' => MENU_LOCAL_TASK, |
| 159 | ); | ||
| 160 | } | ||
| 161 | jvandyk | 1.26 | $items[] = array( |
| 162 | jvandyk | 1.31 | 'path' => 'admin/build/trigger/unassign', |
| 163 | jvandyk | 1.26 | 'title' => t('Unassign'), |
| 164 | jvandyk | 1.31 | 'description' => t('Unassign an action from a trigger.'), |
| 165 | 'callback' => 'drupal_get_form', | ||
| 166 | 'callback arguments' => array('actions_unassign'), | ||
| 167 | jvandyk | 1.26 | 'type' => MENU_CALLBACK, |
| 168 | ); | ||
| 169 | jvandyk | 1.32 | |
| 170 | jvandyk | 1.31 | return $items; |
| 171 | jvandyk | 1.24 | } |
| 172 | jvandyk | 1.31 | } |
| 173 | jvandyk | 1.1 | |
| 174 | jvandyk | 1.31 | /** |
| 175 | * Implementation of hook_perm(). | ||
| 176 | */ | ||
| 177 | function actions_perm() { | ||
| 178 | jvandyk | 1.40 | return array('administer actions'); |
| 179 | jvandyk | 1.1 | } |
| 180 | |||
| 181 | /** | ||
| 182 | jvandyk | 1.31 | * Menu callback. Display an overview of available and configured actions. |
| 183 | jvandyk | 1.1 | */ |
| 184 | jvandyk | 1.31 | function actions_manage() { |
| 185 | jvandyk | 1.1 | $output = ''; |
| 186 | $actions = actions_list(); | ||
| 187 | actions_synchronize($actions); | ||
| 188 | $actions_map = actions_actions_map($actions); | ||
| 189 | jvandyk | 1.31 | $options = array(t('Choose an advanced action')); |
| 190 | jvandyk | 1.1 | $unconfigurable = array(); |
| 191 | jvandyk | 1.15 | |
| 192 | jvandyk | 1.1 | foreach ($actions_map as $key => $array) { |
| 193 | if ($array['configurable']) { | ||
| 194 | jvandyk | 1.31 | $options[$key] = $array['description'] .'...'; |
| 195 | jvandyk | 1.1 | } |
| 196 | else { | ||
| 197 | $unconfigurable[] = $array; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | jvandyk | 1.15 | |
| 201 | jvandyk | 1.1 | $row = array(); |
| 202 | jvandyk | 1.40 | $instances_present = db_fetch_object(db_query("SELECT aid FROM {actions} WHERE parameters <> ''")); |
| 203 | jvandyk | 1.16 | $header = array( |
| 204 | jvandyk | 1.31 | array('data' => t('Action type'), 'field' => 'type'), |
| 205 | jvandyk | 1.16 | array('data' => t('Description'), 'field' => 'description'), |
| 206 | array('data' => $instances_present ? t('Operations') : '', 'colspan' => '2') | ||
| 207 | jvandyk | 1.31 | ); |
| 208 | jvandyk | 1.1 | $sql = 'SELECT * FROM {actions}'; |
| 209 | $result = pager_query($sql . tablesort_sql($header), 50); | ||
| 210 | jvandyk | 1.31 | while ($action = db_fetch_object($result)) { |
| 211 | jvandyk | 1.1 | $row[] = array( |
| 212 | jvandyk | 1.31 | 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 | jvandyk | 1.1 | ); |
| 217 | } | ||
| 218 | jvandyk | 1.15 | |
| 219 | jvandyk | 1.1 | if ($row) { |
| 220 | jvandyk | 1.18 | $pager = theme('pager', NULL, 50, 0); |
| 221 | jvandyk | 1.1 | if (!empty($pager)) { |
| 222 | $row[] = array(array('data' => $pager, 'colspan' => '3')); | ||
| 223 | } | ||
| 224 | jvandyk | 1.31 | $output .= '<h3>'. t('Actions available to Drupal:') .'</h3>'; |
| 225 | jvandyk | 1.1 | $output .= theme('table', $header, $row); |
| 226 | } | ||
| 227 | jvandyk | 1.15 | |
| 228 | jvandyk | 1.31 | if ($actions_map) { |
| 229 | $output .= drupal_get_form('actions_manage_form', $options); | ||
| 230 | } | ||
| 231 | |||
| 232 | jvandyk | 1.15 | return $output; |
| 233 | jvandyk | 1.1 | } |
| 234 | |||
| 235 | jvandyk | 1.28 | /** |
| 236 | * Define the form for the actions overview page. | ||
| 237 | * | ||
| 238 | jvandyk | 1.31 | * @see actions_manage_form_submit() |
| 239 | * @ingroup forms | ||
| 240 | jvandyk | 1.28 | * @param $options |
| 241 | * An array of configurable actions. | ||
| 242 | * @return | ||
| 243 | * Form definition. | ||
| 244 | */ | ||
| 245 | jvandyk | 1.31 | 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 | jvandyk | 1.24 | '#type' => 'select', |
| 254 | '#default_value' => '', | ||
| 255 | '#options' => $options, | ||
| 256 | '#description' => '', | ||
| 257 | ); | ||
| 258 | jvandyk | 1.31 | $form['parent']['buttons']['submit'] = array( |
| 259 | jvandyk | 1.24 | '#type' => 'submit', |
| 260 | jvandyk | 1.31 | '#value' => t('Create'), |
| 261 | jvandyk | 1.24 | ); |
| 262 | return $form; | ||
| 263 | } | ||
| 264 | |||
| 265 | jvandyk | 1.31 | /** |
| 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 | jvandyk | 1.24 | } |
| 272 | } | ||
| 273 | |||
| 274 | jvandyk | 1.1 | /** |
| 275 | jvandyk | 1.31 | * Menu callback. Create the form for configuration of a single action. |
| 276 | * | ||
| 277 | jvandyk | 1.1 | * We provide the "Description" field. The rest of the form |
| 278 | * is provided by the action. We then provide the Save button. | ||
| 279 | jvandyk | 1.30 | * Because we are combining unknown form elements with the action |
| 280 | * configuration form, we use actions_ prefix on our elements. | ||
| 281 | jvandyk | 1.29 | * |
| 282 | jvandyk | 1.31 | * @see actions_configure_validate() |
| 283 | * @see actions_configure_submit() | ||
| 284 | jvandyk | 1.29 | * @param $action |
| 285 | jvandyk | 1.31 | * 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 | jvandyk | 1.1 | */ |
| 291 | jvandyk | 1.24 | function actions_configure($action = NULL) { |
| 292 | if ($action === NULL) { | ||
| 293 | jvandyk | 1.31 | drupal_goto('admin/settings/actions'); |
| 294 | jvandyk | 1.24 | } |
| 295 | |||
| 296 | jvandyk | 1.1 | $actions_map = actions_actions_map(actions_list()); |
| 297 | jvandyk | 1.24 | $edit = array(); |
| 298 | jvandyk | 1.15 | |
| 299 | jvandyk | 1.31 | // Numeric action denotes saved instance of a configurable action; |
| 300 | // else we are creating a new action instance. | ||
| 301 | if (is_numeric($action)) { | ||
| 302 | jvandyk | 1.24 | $aid = $action; |
| 303 | jvandyk | 1.30 | // 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 | jvandyk | 1.31 | $function = $data->callback; |
| 308 | $action = md5($data->callback); | ||
| 309 | $params = unserialize($data->parameters); | ||
| 310 | jvandyk | 1.30 | if ($params) { |
| 311 | foreach ($params as $name => $val) { | ||
| 312 | $edit[$name] = $val; | ||
| 313 | jvandyk | 1.23 | } |
| 314 | jvandyk | 1.1 | } |
| 315 | } | ||
| 316 | jvandyk | 1.31 | else { |
| 317 | $function = $actions_map[$action]['callback']; | ||
| 318 | jvandyk | 1.30 | $edit['actions_description'] = $actions_map[$action]['description']; |
| 319 | $edit['actions_type'] = $actions_map[$action]['type']; | ||
| 320 | jvandyk | 1.15 | } |
| 321 | |||
| 322 | jvandyk | 1.31 | $form = array(); |
| 323 | jvandyk | 1.30 | $form['actions_description'] = array( |
| 324 | jvandyk | 1.15 | '#type' => 'textfield', |
| 325 | '#title' => t('Description'), | ||
| 326 | jvandyk | 1.30 | '#default_value' => $edit['actions_description'], |
| 327 | jvandyk | 1.15 | '#maxlength' => '255', |
| 328 | jvandyk | 1.31 | '#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 | jvandyk | 1.15 | '#weight' => -10 |
| 330 | ); | ||
| 331 | jvandyk | 1.31 | $action_form = $function .'_form'; |
| 332 | jvandyk | 1.29 | $form = array_merge($form, $action_form($edit)); |
| 333 | jvandyk | 1.30 | $form['actions_type'] = array( |
| 334 | jvandyk | 1.29 | '#type' => 'value', |
| 335 | jvandyk | 1.30 | '#value' => $edit['actions_type'], |
| 336 | jvandyk | 1.29 | ); |
| 337 | jvandyk | 1.30 | $form['actions_action'] = array( |
| 338 | jvandyk | 1.15 | '#type' => 'hidden', |
| 339 | jvandyk | 1.24 | '#value' => $action, |
| 340 | jvandyk | 1.15 | ); |
| 341 | jvandyk | 1.31 | // $aid is set when configuring an existing action instance. |
| 342 | if (isset($aid)) { | ||
| 343 | jvandyk | 1.30 | $form['actions_aid'] = array( |
| 344 | jvandyk | 1.15 | '#type' => 'hidden', |
| 345 | '#value' => $aid, | ||
| 346 | ); | ||
| 347 | jvandyk | 1.14 | } |
| 348 | jvandyk | 1.30 | $form['actions_configured'] = array( |
| 349 | jvandyk | 1.15 | '#type' => 'hidden', |
| 350 | '#value' => '1', | ||
| 351 | ); | ||
| 352 | $form['buttons']['submit'] = array( | ||
| 353 | '#type' => 'submit', | ||
| 354 | '#value' => t('Save'), | ||
| 355 | '#weight' => 13 | ||
| 356 | ); | ||
| 357 | |||
| 358 | jvandyk | 1.24 | return $form; |
| 359 | jvandyk | 1.15 | } |
| 360 | |||
| 361 | jvandyk | 1.31 | /** |
| 362 | * Validate actions_configure form submissions. | ||
| 363 | */ | ||
| 364 | jvandyk | 1.24 | function actions_configure_validate($form_id, $form_values) { |
| 365 | jvandyk | 1.31 | $function = actions_function_lookup($form_values['actions_action']) .'_validate'; |
| 366 | jvandyk | 1.28 | // Hand off validation to the action. |
| 367 | jvandyk | 1.31 | if (function_exists($function)) { |
| 368 | $function('validate', $form_values); | ||
| 369 | } | ||
| 370 | jvandyk | 1.15 | } |
| 371 | |||
| 372 | jvandyk | 1.31 | /** |
| 373 | * Process actions_configure form submissions. | ||
| 374 | */ | ||
| 375 | jvandyk | 1.24 | function actions_configure_submit($form_id, $form_values) { |
| 376 | jvandyk | 1.30 | $function = actions_function_lookup($form_values['actions_action']); |
| 377 | jvandyk | 1.31 | $submit_function = $function .'_submit'; |
| 378 | jvandyk | 1.29 | |
| 379 | jvandyk | 1.28 | // Action will return keyed array of values to store. |
| 380 | jvandyk | 1.30 | $params = $submit_function($form_id, $form_values); |
| 381 | jvandyk | 1.31 | $aid = isset($form_values['actions_aid']) ? $form_values['actions_aid'] : NULL; |
| 382 | jvandyk | 1.29 | |
| 383 | jvandyk | 1.30 | actions_save($function, $form_values['actions_type'], $params, $form_values['actions_description'], $aid); |
| 384 | jvandyk | 1.15 | drupal_set_message(t('The action has been successfully saved.')); |
| 385 | jvandyk | 1.24 | |
| 386 | jvandyk | 1.31 | return 'admin/settings/actions/manage'; |
| 387 | jvandyk | 1.1 | } |
| 388 | |||
| 389 | /** | ||
| 390 | * Create the form for confirmation of deleting an action. | ||
| 391 | * | ||
| 392 | jvandyk | 1.31 | * @ingroup forms |
| 393 | * @see actions_delete_form_submit() | ||
| 394 | jvandyk | 1.1 | */ |
| 395 | jvandyk | 1.31 | function actions_delete_form($aid) {; |
| 396 | jvandyk | 1.1 | $action = actions_load($aid); |
| 397 | jvandyk | 1.15 | $form['aid'] = array( |
| 398 | '#type' => 'hidden', | ||
| 399 | jvandyk | 1.31 | '#value' => $action->aid, |
| 400 | jvandyk | 1.24 | ); |
| 401 | jvandyk | 1.31 | 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 | jvandyk | 1.24 | t('This cannot be undone.'), |
| 405 | jvandyk | 1.31 | t('Delete'), t('Cancel') |
| 406 | jvandyk | 1.24 | ); |
| 407 | jvandyk | 1.15 | } |
| 408 | |||
| 409 | jvandyk | 1.31 | /** |
| 410 | * Process actions_delete form submissions. | ||
| 411 | * | ||
| 412 | * Post-deletion operations for action deletion. | ||
| 413 | */ | ||
| 414 | jvandyk | 1.24 | 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 | jvandyk | 1.31 | 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 | jvandyk | 1.1 | } |
| 423 | |||
| 424 | /** | ||
| 425 | jvandyk | 1.31 | * Post-deletion operations for deleting action orphans. |
| 426 | jvandyk | 1.1 | * |
| 427 | jvandyk | 1.31 | * @param $orphaned |
| 428 | * An array of orphaned actions. | ||
| 429 | jvandyk | 1.1 | */ |
| 430 | jvandyk | 1.31 | 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 | jvandyk | 1.1 | } |
| 434 | } | ||
| 435 | |||
| 436 | /** | ||
| 437 | jvandyk | 1.31 | * Remove actions that are in the database but not supported by any enabled module. |
| 438 | jvandyk | 1.1 | */ |
| 439 | jvandyk | 1.31 | function actions_remove_orphans() { |
| 440 | actions_synchronize(actions_list(), TRUE); | ||
| 441 | drupal_goto('admin/settings/actions/manage'); | ||
| 442 | jvandyk | 1.1 | } |
| 443 | |||
| 444 | /** | ||
| 445 | jvandyk | 1.31 | * Admin page callbacks for the trigger module. |
| 446 | jvandyk | 1.1 | */ |
| 447 | |||
| 448 | /** | ||
| 449 | jvandyk | 1.31 | * Build the form that allows users to assign actions to hooks. |
| 450 | jvandyk | 1.1 | * |
| 451 | jvandyk | 1.31 | * @param $type |
| 452 | * Name of hook. | ||
| 453 | * @return | ||
| 454 | * HTML form. | ||
| 455 | jvandyk | 1.1 | */ |
| 456 | jvandyk | 1.31 | function actions_assign($type = NULL) { |
| 457 | jvandyk | 1.32 | |
| 458 | jvandyk | 1.31 | // 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 | jvandyk | 1.1 | } |
| 463 | jvandyk | 1.31 | if ($type == 'node') { |
| 464 | $type = 'nodeapi'; | ||
| 465 | jvandyk | 1.15 | } |
| 466 | |||
| 467 | jvandyk | 1.31 | $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 | jvandyk | 1.15 | } |
| 475 | jvandyk | 1.1 | } |
| 476 | } | ||
| 477 | jvandyk | 1.31 | return $output; |
| 478 | jvandyk | 1.26 | } |
| 479 | |||
| 480 | /** | ||
| 481 | jvandyk | 1.31 | * Confirm removal of an assigned action. |
| 482 | jvandyk | 1.26 | * |
| 483 | * @param $hook | ||
| 484 | * @param $op | ||
| 485 | jvandyk | 1.31 | * @param $aid |
| 486 | * The action ID. | ||
| 487 | * @ingroup forms | ||
| 488 | * @see actions_unassign_submit() | ||
| 489 | jvandyk | 1.26 | */ |
| 490 | jvandyk | 1.31 | 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 | jvandyk | 1.40 | $action = db_result(db_query("SELECT aid FROM {actions} WHERE MD5(aid) = '%s' AND parameters <> ''", $aid)); |
| 509 | jvandyk | 1.31 | $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 | jvandyk | 1.26 | } |
| 530 | else { | ||
| 531 | jvandyk | 1.31 | drupal_goto('admin/build/trigger'); |
| 532 | jvandyk | 1.26 | } |
| 533 | } | ||
| 534 | |||
| 535 | jvandyk | 1.28 | /** |
| 536 | jvandyk | 1.31 | * Create the form definition for assigning an action to a hook-op combination. |
| 537 | jvandyk | 1.28 | * |
| 538 | jvandyk | 1.31 | * @param $form_values |
| 539 | * Information about the current form. | ||
| 540 | jvandyk | 1.28 | * @param $hook |
| 541 | jvandyk | 1.31 | * The name of the hook, e.g., 'nodeapi'. |
| 542 | jvandyk | 1.28 | * @param $op |
| 543 | jvandyk | 1.31 | * The name of the hook operation, e.g., 'insert'. |
| 544 | * @param $description | ||
| 545 | * A plain English description of what this hook operation does. | ||
| 546 | jvandyk | 1.28 | * @return |
| 547 | jvandyk | 1.31 | * |
| 548 | * @ingoup forms | ||
| 549 | * @see actions_assign_form_validate() | ||
| 550 | * @see actions_assign_form_submit() | ||
| 551 | jvandyk | 1.28 | */ |
| 552 | jvandyk | 1.31 | function actions_assign_form($hook, $op, $description, $form_value = NULL) { |
| 553 | jvandyk | 1.26 | $form['hook'] = array( |
| 554 | '#type' => 'hidden', | ||
| 555 | jvandyk | 1.31 | '#value' => $hook, |
| 556 | jvandyk | 1.26 | ); |
| 557 | $form['operation'] = array( | ||
| 558 | '#type' => 'hidden', | ||
| 559 | jvandyk | 1.31 | '#value' => $op, |
| 560 | jvandyk | 1.26 | ); |
| 561 | jvandyk | 1.31 | // 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 | jvandyk | 1.27 | |
| 565 | jvandyk | 1.28 | $options = array(); |
| 566 | $functions = array(); | ||
| 567 | jvandyk | 1.31 | // Restrict the options list to actions that declare support for this hook-op |
| 568 | // combination. | ||
| 569 | jvandyk | 1.28 | foreach (actions_list() as $func => $metadata) { |
| 570 | jvandyk | 1.35 | 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 | jvandyk | 1.28 | } |
| 577 | } | ||
| 578 | foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) { | ||
| 579 | jvandyk | 1.31 | if (in_array($action['callback'], $functions)) { |
| 580 | jvandyk | 1.28 | $options[$action['type']][$aid] = $action['description']; |
| 581 | } | ||
| 582 | } | ||
| 583 | jvandyk | 1.27 | |
| 584 | jvandyk | 1.26 | $form[$op] = array( |
| 585 | '#type' => 'fieldset', | ||
| 586 | jvandyk | 1.31 | '#title' => t('Trigger: ') . $description, |
| 587 | jvandyk | 1.27 | '#theme' => 'actions_display' |
| 588 | jvandyk | 1.26 | ); |
| 589 | jvandyk | 1.31 | // Retrieve actions that are already assigned to this hook-op combination. |
| 590 | jvandyk | 1.26 | $actions = _actions_get_hook_actions($hook, $op); |
| 591 | jvandyk | 1.27 | $form[$op]['assigned']['#type'] = 'value'; |
| 592 | $form[$op]['assigned']['#value'] = array(); | ||
| 593 | jvandyk | 1.26 | foreach ($actions as $aid => $description) { |
| 594 | jvandyk | 1.27 | $form[$op]['assigned']['#value'][$aid] = array( |
| 595 | 'description' => $description, | ||
| 596 | jvandyk | 1.31 | 'link' => l(t('unassign'), "admin/build/trigger/unassign/$hook/$op/". md5($aid)) |
| 597 | ); | ||
| 598 | jvandyk | 1.26 | } |
| 599 | |||
| 600 | jvandyk | 1.31 | $form[$op]['parent'] = array( |
| 601 | '#prefix' => "<div class='container-inline'>", | ||
| 602 | '#suffix' => '</div>', | ||
| 603 | ); | ||
| 604 | jvandyk | 1.26 | // List possible actions that may be assigned. |
| 605 | jvandyk | 1.27 | if (count($options) != 0) { |
| 606 | jvandyk | 1.30 | array_unshift($options, t('Choose an action')); |
| 607 | jvandyk | 1.31 | $form[$op]['parent']['aid'] = array( |
| 608 | jvandyk | 1.26 | '#type' => 'select', |
| 609 | '#options' => $options, | ||
| 610 | ); | ||
| 611 | jvandyk | 1.31 | $form[$op]['parent']['submit'] = array( |
| 612 | jvandyk | 1.26 | '#type' => 'submit', |
| 613 | jvandyk | 1.31 | '#value' => t('Assign') |
| 614 | jvandyk | 1.26 | ); |
| 615 | } | ||
| 616 | jvandyk | 1.28 | else { |
| 617 | $form[$op]['none'] = array( | ||
| 618 | jvandyk | 1.31 | '#value' => t('No available actions for this trigger.') |
| 619 | jvandyk | 1.28 | ); |
| 620 | } | ||
| 621 | jvandyk | 1.26 | return $form; |
| 622 | } | ||
| 623 | |||
| 624 | jvandyk | 1.31 | /** |
| 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 | jvandyk | 1.30 | $aid = actions_function_lookup($form_values['aid']); |
| 632 | jvandyk | 1.31 | 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 | jvandyk | 1.30 | $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 | jvandyk | 1.31 | // 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 | jvandyk | 1.30 | } |
| 662 | jvandyk | 1.26 | } |
| 663 | |||
| 664 | jvandyk | 1.27 | /** |
| 665 | jvandyk | 1.31 | * Display actions assigned to this hook-op combination in a table. |
| 666 | jvandyk | 1.27 | * |
| 667 | * @param array $element | ||
| 668 | * The fieldset including all assigned actions. | ||
| 669 | * @return | ||
| 670 | * The rendered form with the table prepended. | ||
| 671 | jvandyk | 1.31 | * |
| 672 | * @ingroup themeable | ||
| 673 | jvandyk | 1.27 | */ |
| 674 | jvandyk | 1.26 | function theme_actions_display($element) { |
| 675 | jvandyk | 1.31 | $header = array(); |
| 676 | $rows = array(); | ||
| 677 | jvandyk | 1.27 | if (count($element['assigned']['#value'])) { |
| 678 | jvandyk | 1.31 | $header = array(array('data' => t('Name')), array('data' => t('Operation'))); |
| 679 | jvandyk | 1.27 | $rows = array(); |
| 680 | foreach ($element['assigned']['#value'] as $aid => $info) { | ||
| 681 | $rows[] = array( | ||
| 682 | $info['description'], | ||
| 683 | $info['link'] | ||
| 684 | ); | ||
| 685 | } | ||
| 686 | } | ||
| 687 | jvandyk | 1.28 | |
| 688 | jvandyk | 1.31 | 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 | jvandyk | 1.1 | } |
| 696 | |||
| 697 | jvandyk | 1.31 | |
| 698 | jvandyk | 1.10 | /** |
| 699 | jvandyk | 1.31 | * Get the actions that have already been defined for this |
| 700 | * type-hook-op combination. | ||
| 701 | jvandyk | 1.26 | * |
| 702 | jvandyk | 1.31 | * @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 | jvandyk | 1.10 | */ |
| 713 | jvandyk | 1.31 | 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 | jvandyk | 1.26 | } |
| 726 | jvandyk | 1.27 | |
| 727 | jvandyk | 1.31 | /** |
| 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 | jvandyk | 1.26 | } |
| 744 | jvandyk | 1.31 | return $aids; |
| 745 | jvandyk | 1.26 | } |
| 746 | |||
| 747 | jvandyk | 1.31 | /** |
| 748 | * Implementation of hook_theme(). | ||
| 749 | */ | ||
| 750 | function actions_theme() { | ||
| 751 | return array( | ||
| 752 | 'actions_display' => array( | ||
| 753 | 'arguments' => array('element'), | ||
| 754 | ), | ||
| 755 | jvandyk | 1.26 | ); |
| 756 | } | ||
| 757 | |||
| 758 | jvandyk | 1.31 | /** |
| 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 | jvandyk | 1.26 | } |
| 771 | jvandyk | 1.31 | |
| 772 | return $forms; | ||
| 773 | jvandyk | 1.26 | } |
| 774 | |||
| 775 | jvandyk | 1.31 | /** |
| 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 | jvandyk | 1.29 | function _actions_normalize_node_context($type, $node) { |
| 790 | jvandyk | 1.31 | 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 | jvandyk | 1.29 | |
| 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 | jvandyk | 1.27 | /** |
| 804 | * Implementation of hook_nodeapi(). | ||
| 805 | */ | ||
| 806 | jvandyk | 1.31 | 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 | jvandyk | 1.30 | static $recursion; |
| 811 | jvandyk | 1.31 | // Support a subset of operations. |
| 812 | jvandyk | 1.37 | if (!in_array($op, array('view', 'update', 'presave', 'insert', 'delete')) || isset($recursion[$op][$node->nid])) { |
| 813 | jvandyk | 1.30 | return; |
| 814 | } | ||
| 815 | jvandyk | 1.37 | $recursion[$op][$node->nid] = TRUE; |
| 816 | jvandyk | 1.30 | |
| 817 | jvandyk | 1.28 | $aids = _actions_get_hook_aids('nodeapi', $op); |
| 818 | jvandyk | 1.30 | if (!$aids) { |
| 819 | return; | ||
| 820 | } | ||
| 821 | jvandyk | 1.27 | $context = array( |
| 822 | 'hook' => 'nodeapi', | ||
| 823 | 'op' => $op, | ||
| 824 | ); | ||
| 825 | jvandyk | 1.28 | |
| 826 | jvandyk | 1.30 | // We need to get the expected object if the action's type is not 'node'. |
| 827 | jvandyk | 1.31 | // We keep the object in $objects so we can reuse it if we have multiple actions |
| 828 | // that make changes to an object. | ||
| 829 | jvandyk | 1.29 | foreach ($aids as $aid => $action_info) { |
| 830 | if ($action_info['type'] != 'node') { | ||
| 831 | jvandyk | 1.31 | if (!isset($objects[$action_info['type']])) { |
| 832 | $objects[$action_info['type']] = _actions_normalize_node_context($action_info['type'], $node); | ||
| 833 | } | ||
| 834 | jvandyk | 1.29 | // Since we know about the node, we pass that info along to the action. |
| 835 | $context['node'] = $node; | ||
| 836 | jvandyk | 1.31 | $result = actions_do($aid, $objects[$action_info['type']], $context, $a4, $a4); |
| 837 | jvandyk | 1.29 | } |
| 838 | else { | ||
| 839 | jvandyk |