annotate modules/trigger/trigger.admin.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: trigger.admin.inc,v 1.5 2008/01/08 10:35:43 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Admin page callbacks for the trigger module.
webmaster@1 7 */
webmaster@1 8
webmaster@1 9 /**
webmaster@1 10 * Build the form that allows users to assign actions to hooks.
webmaster@1 11 *
webmaster@1 12 * @param $type
webmaster@1 13 * Name of hook.
webmaster@1 14 * @return
webmaster@1 15 * HTML form.
webmaster@1 16 */
webmaster@1 17 function trigger_assign($type = NULL) {
webmaster@1 18 // If no type is specified we default to node actions, since they
webmaster@1 19 // are the most common.
webmaster@1 20 if (!isset($type)) {
webmaster@1 21 drupal_goto('admin/build/trigger/node');
webmaster@1 22 }
webmaster@1 23 if ($type == 'node') {
webmaster@1 24 $type = 'nodeapi';
webmaster@1 25 }
webmaster@1 26
webmaster@1 27 $output = '';
webmaster@1 28 $hooks = module_invoke_all('hook_info');
webmaster@1 29 foreach ($hooks as $module => $hook) {
webmaster@1 30 if (isset($hook[$type])) {
webmaster@1 31 foreach ($hook[$type] as $op => $description) {
webmaster@1 32 $form_id = 'trigger_'. $type .'_'. $op .'_assign_form';
webmaster@1 33 $output .= drupal_get_form($form_id, $type, $op, $description['runs when']);
webmaster@1 34 }
webmaster@1 35 }
webmaster@1 36 }
webmaster@1 37 return $output;
webmaster@1 38 }
webmaster@1 39
webmaster@1 40 /**
webmaster@1 41 * Confirm removal of an assigned action.
webmaster@1 42 *
webmaster@1 43 * @param $hook
webmaster@1 44 * @param $op
webmaster@1 45 * @param $aid
webmaster@1 46 * The action ID.
webmaster@1 47 * @ingroup forms
webmaster@1 48 * @see trigger_unassign_submit()
webmaster@1 49 */
webmaster@1 50 function trigger_unassign($form_state, $hook = NULL, $op = NULL, $aid = NULL) {
webmaster@1 51 if (!($hook && $op && $aid)) {
webmaster@1 52 drupal_goto('admin/build/trigger/assign');
webmaster@1 53 }
webmaster@1 54
webmaster@1 55 $form['hook'] = array(
webmaster@1 56 '#type' => 'value',
webmaster@1 57 '#value' => $hook,
webmaster@1 58 );
webmaster@1 59 $form['operation'] = array(
webmaster@1 60 '#type' => 'value',
webmaster@1 61 '#value' => $op,
webmaster@1 62 );
webmaster@1 63 $form['aid'] = array(
webmaster@1 64 '#type' => 'value',
webmaster@1 65 '#value' => $aid,
webmaster@1 66 );
webmaster@1 67
webmaster@1 68 $action = actions_function_lookup($aid);
webmaster@1 69 $actions = actions_get_all_actions();
webmaster@1 70
webmaster@1 71 $destination = 'admin/build/trigger/'. ($hook == 'nodeapi' ? 'node' : $hook);
webmaster@1 72
webmaster@1 73 return confirm_form($form,
webmaster@1 74 t('Are you sure you want to unassign the action %title?', array('%title' => $actions[$action]['description'])),
webmaster@1 75 $destination,
webmaster@1 76 t('You can assign it again later if you wish.'),
webmaster@1 77 t('Unassign'), t('Cancel')
webmaster@1 78 );
webmaster@1 79 }
webmaster@1 80
webmaster@1 81 function trigger_unassign_submit($form, &$form_state) {
webmaster@1 82 $form_values = $form_state['values'];
webmaster@1 83 if ($form_values['confirm'] == 1) {
webmaster@1 84 $aid = actions_function_lookup($form_values['aid']);
webmaster@1 85 db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid);
webmaster@1 86 $actions = actions_get_all_actions();
webmaster@1 87 watchdog('actions', 'Action %action has been unassigned.', array('%action' => check_plain($actions[$aid]['description'])));
webmaster@1 88 drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['description'])));
webmaster@1 89 $hook = $form_values['hook'] == 'nodeapi' ? 'node' : $form_values['hook'];
webmaster@1 90 $form_state['redirect'] = 'admin/build/trigger/'. $hook;
webmaster@1 91 }
webmaster@1 92 else {
webmaster@1 93 drupal_goto('admin/build/trigger');
webmaster@1 94 }
webmaster@1 95 }
webmaster@1 96
webmaster@1 97 /**
webmaster@1 98 * Create the form definition for assigning an action to a hook-op combination.
webmaster@1 99 *
webmaster@1 100 * @param $form_state
webmaster@1 101 * Information about the current form.
webmaster@1 102 * @param $hook
webmaster@1 103 * The name of the hook, e.g., 'nodeapi'.
webmaster@1 104 * @param $op
webmaster@1 105 * The name of the hook operation, e.g., 'insert'.
webmaster@1 106 * @param $description
webmaster@1 107 * A plain English description of what this hook operation does.
webmaster@1 108 * @return
webmaster@1 109 *
webmaster@1 110 * @ingoup forms
webmaster@1 111 * @see trigger_assign_form_validate()
webmaster@1 112 * @see trigger_assign_form_submit()
webmaster@1 113 */
webmaster@1 114 function trigger_assign_form($form_state, $hook, $op, $description) {
webmaster@1 115 $form['hook'] = array(
webmaster@1 116 '#type' => 'hidden',
webmaster@1 117 '#value' => $hook,
webmaster@1 118 );
webmaster@1 119 $form['operation'] = array(
webmaster@1 120 '#type' => 'hidden',
webmaster@1 121 '#value' => $op,
webmaster@1 122 );
webmaster@1 123 // All of these forms use the same validate and submit functions.
webmaster@1 124 $form['#validate'][] = 'trigger_assign_form_validate';
webmaster@1 125 $form['#submit'][] = 'trigger_assign_form_submit';
webmaster@1 126
webmaster@1 127 $options = array();
webmaster@1 128 $functions = array();
webmaster@1 129 // Restrict the options list to actions that declare support for this hook-op
webmaster@1 130 // combination.
webmaster@1 131 foreach (actions_list() as $func => $metadata) {
webmaster@1 132 if (isset($metadata['hooks']['any']) || (isset($metadata['hooks'][$hook]) && is_array($metadata['hooks'][$hook]) && (in_array($op, $metadata['hooks'][$hook])))) {
webmaster@1 133 $functions[] = $func;
webmaster@1 134 }
webmaster@1 135 }
webmaster@1 136 foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
webmaster@1 137 if (in_array($action['callback'], $functions)) {
webmaster@1 138 $options[$action['type']][$aid] = $action['description'];
webmaster@1 139 }
webmaster@1 140 }
webmaster@1 141
webmaster@1 142 $form[$op] = array(
webmaster@1 143 '#type' => 'fieldset',
webmaster@1 144 '#title' => t('Trigger: ') . $description,
webmaster@1 145 '#theme' => 'trigger_display'
webmaster@1 146 );
webmaster@1 147 // Retrieve actions that are already assigned to this hook-op combination.
webmaster@1 148 $actions = _trigger_get_hook_actions($hook, $op);
webmaster@1 149 $form[$op]['assigned']['#type'] = 'value';
webmaster@1 150 $form[$op]['assigned']['#value'] = array();
webmaster@1 151 foreach ($actions as $aid => $description) {
webmaster@1 152 $form[$op]['assigned']['#value'][$aid] = array(
webmaster@1 153 'description' => $description,
webmaster@1 154 'link' => l(t('unassign'), "admin/build/trigger/unassign/$hook/$op/". md5($aid))
webmaster@1 155 );
webmaster@1 156 }
webmaster@1 157
webmaster@1 158 $form[$op]['parent'] = array(
webmaster@1 159 '#prefix' => "<div class='container-inline'>",
webmaster@1 160 '#suffix' => '</div>',
webmaster@1 161 );
webmaster@1 162 // List possible actions that may be assigned.
webmaster@1 163 if (count($options) != 0) {
webmaster@1 164 array_unshift($options, t('Choose an action'));
webmaster@1 165 $form[$op]['parent']['aid'] = array(
webmaster@1 166 '#type' => 'select',
webmaster@1 167 '#options' => $options,
webmaster@1 168 );
webmaster@1 169 $form[$op]['parent']['submit'] = array(
webmaster@1 170 '#type' => 'submit',
webmaster@1 171 '#value' => t('Assign')
webmaster@1 172 );
webmaster@1 173 }
webmaster@1 174 else {
webmaster@1 175 $form[$op]['none'] = array(
webmaster@1 176 '#value' => t('No available actions for this trigger.')
webmaster@1 177 );
webmaster@1 178 }
webmaster@1 179 return $form;
webmaster@1 180 }
webmaster@1 181
webmaster@1 182 /**
webmaster@1 183 * Validation function for trigger_assign_form().
webmaster@1 184 *
webmaster@1 185 * Makes sure that the user is not re-assigning an action to an event.
webmaster@1 186 */
webmaster@1 187 function trigger_assign_form_validate($form, $form_state) {
webmaster@1 188 $form_values = $form_state['values'];
webmaster@1 189 if (!empty($form_values['aid'])) {
webmaster@1 190 $aid = actions_function_lookup($form_values['aid']);
webmaster@1 191 if (db_result(db_query("SELECT aid FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'], $aid))) {
webmaster@1 192 form_set_error($form_values['operation'], t('The action you chose is already assigned to that trigger.'));
webmaster@1 193 }
webmaster@1 194 }
webmaster@1 195 }
webmaster@1 196
webmaster@1 197 /**
webmaster@1 198 * Submit function for trigger_assign_form().
webmaster@1 199 */
webmaster@1 200 function trigger_assign_form_submit($form, $form_state) {
webmaster@1 201 $form_values = $form_state['values'];
webmaster@1 202
webmaster@1 203 if (!empty($form_values['aid'])) {
webmaster@1 204 $aid = actions_function_lookup($form_values['aid']);
webmaster@1 205 $weight = db_result(db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s'", $form_values['hook'], $form_values['operation']));
webmaster@1 206 db_query("INSERT INTO {trigger_assignments} values ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1);
webmaster@1 207 // If this action changes a node property, we need to save the node
webmaster@1 208 // so the change will persist.
webmaster@1 209 $actions = actions_list();
webmaster@1 210 if (isset($actions[$aid]['behavior']) && in_array('changes_node_property', $actions[$aid]['behavior']) && ($form_values['operation'] != 'presave')) {
webmaster@1 211 // Delete previous node_save_action if it exists, and re-add a new one at a higher weight.
webmaster@1 212 $save_post_action_assigned = db_result(db_query("SELECT aid FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation']));
webmaster@1 213 if ($save_post_action_assigned) {
webmaster@1 214 db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op = '%s' AND aid = 'node_save_action'", $form_values['hook'], $form_values['operation']);
webmaster@1 215 }
webmaster@1 216 db_query("INSERT INTO {trigger_assignments} VALUES ('%s', '%s', '%s', %d)", $form_values['hook'], $form_values['operation'], 'node_save_action', $weight + 2);
webmaster@1 217 if (!$save_post_action_assigned) {
webmaster@1 218 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.'));
webmaster@1 219 }
webmaster@1 220 }
webmaster@1 221 }
webmaster@1 222 }
webmaster@1 223
webmaster@1 224 /**
webmaster@1 225 * Display actions assigned to this hook-op combination in a table.
webmaster@1 226 *
webmaster@1 227 * @param array $element
webmaster@1 228 * The fieldset including all assigned actions.
webmaster@1 229 * @return
webmaster@1 230 * The rendered form with the table prepended.
webmaster@1 231 *
webmaster@1 232 * @ingroup themeable
webmaster@1 233 */
webmaster@1 234 function theme_trigger_display($element) {
webmaster@1 235 $header = array();
webmaster@1 236 $rows = array();
webmaster@1 237 if (count($element['assigned']['#value'])) {
webmaster@1 238 $header = array(array('data' => t('Name')), array('data' => t('Operation')));
webmaster@1 239 $rows = array();
webmaster@1 240 foreach ($element['assigned']['#value'] as $aid => $info) {
webmaster@1 241 $rows[] = array(
webmaster@1 242 $info['description'],
webmaster@1 243 $info['link']
webmaster@1 244 );
webmaster@1 245 }
webmaster@1 246 }
webmaster@1 247
webmaster@1 248 if (count($rows)) {
webmaster@1 249 $output = theme('table', $header, $rows) . drupal_render($element);
webmaster@1 250 }
webmaster@1 251 else {
webmaster@1 252 $output = drupal_render($element);
webmaster@1 253 }
webmaster@1 254 return $output;
webmaster@1 255 }
webmaster@1 256
webmaster@1 257
webmaster@1 258 /**
webmaster@1 259 * Get the actions that have already been defined for this
webmaster@1 260 * type-hook-op combination.
webmaster@1 261 *
webmaster@1 262 * @param $type
webmaster@1 263 * One of 'node', 'user', 'comment'.
webmaster@1 264 * @param $hook
webmaster@1 265 * The name of the hook for which actions have been assigned,
webmaster@1 266 * e.g. 'nodeapi'.
webmaster@1 267 * @param $op
webmaster@1 268 * The hook operation for which the actions have been assigned,
webmaster@1 269 * e.g., 'view'.
webmaster@1 270 * @return
webmaster@1 271 * An array of action descriptions keyed by action IDs.
webmaster@1 272 */
webmaster@1 273 function _trigger_get_hook_actions($hook, $op, $type = NULL) {
webmaster@1 274 $actions = array();
webmaster@1 275 if ($type) {
webmaster@1 276 $result = db_query("SELECT h.aid, a.description FROM {trigger_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);
webmaster@1 277 }
webmaster@1 278 else {
webmaster@1 279 $result = db_query("SELECT h.aid, a.description FROM {trigger_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);
webmaster@1 280 }
webmaster@1 281 while ($action = db_fetch_object($result)) {
webmaster@1 282 $actions[$action->aid] = $action->description;
webmaster@1 283 }
webmaster@1 284 return $actions;
webmaster@1 285 }