Code coverage for /20081101/modules/trigger/trigger.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: trigger.admin.inc,v 1.7 2008/07/16 21:59:28 dries Exp $
3
4
/**
5
 * @file
6
 * Admin page callbacks for the trigger module.
7
 */
8
9
/**
10
 * Build the form that allows users to assign actions to hooks.
11
 *
12
 * @param $type
13
 *   Name of hook.
14
 * @return
15
 *   HTML form.
16
 */
1761
function trigger_assign($type = NULL) {
18
  // If no type is specified we default to node actions, since they
19
  // are the most common.
2048
  if (!isset($type)) {
210
    drupal_goto('admin/build/trigger/node');
220
  }
2348
  if ($type == 'node') {
2448
    $type = 'nodeapi';
2548
  }
26
2748
  $output = '';
2848
  $hooks = module_invoke_all('hook_info');
2948
  foreach ($hooks as $module => $hook) {
3048
    if (isset($hook[$type])) {
3148
      foreach ($hook[$type] as $op => $description) {
3248
        $form_id = 'trigger_' . $type . '_' . $op . '_assign_form';
3348
        $output .= drupal_get_form($form_id, $type, $op, $description['runs
when']);
3442
      }
3542
    }
3648
  }
3742
  return $output;
380
}
39
40
/**
41
 * Confirm removal of an assigned action.
42
 *
43
 * @param $hook
44
 * @param $op
45
 * @param $aid
46
 *   The action ID.
47
 * @ingroup forms
48
 * @see trigger_unassign_submit()
49
 */
5061
function trigger_unassign($form_state, $hook = NULL, $op = NULL, $aid =
NULL) {
5112
  if (!($hook && $op && $aid)) {
520
    drupal_goto('admin/build/trigger/assign');
530
  }
54
5512
  $form['hook'] = array(
5612
    '#type' => 'value',
5712
    '#value' => $hook,
58
  );
5912
  $form['operation'] = array(
6012
    '#type' => 'value',
6112
    '#value' => $op,
62
  );
6312
  $form['aid'] = array(
6412
    '#type' => 'value',
6512
    '#value' => $aid,
66
  );
67
6812
  $action = actions_function_lookup($aid);
6912
  $actions = actions_get_all_actions();
70
7112
  $destination = 'admin/build/trigger/' . ($hook == 'nodeapi' ? 'node' :
$hook);
72
7312
  return confirm_form($form,
7412
    t('Are you sure you want to unassign the action %title?',
array('%title' => $actions[$action]['description'])),
7512
    $destination,
7612
    t('You can assign it again later if you wish.'),
7712
    t('Unassign'), t('Cancel')
7812
  );
790
}
80
8161
function trigger_unassign_submit($form, &$form_state) {
826
  $form_values = $form_state['values'];
836
  if ($form_values['confirm'] == 1) {
846
    $aid = actions_function_lookup($form_values['aid']);
856
    db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND op =
'%s' AND aid = '%s'", $form_values['hook'], $form_values['operation'],
$aid);
866
    $actions = actions_get_all_actions();
876
    watchdog('actions', 'Action %action has been unassigned.', 
array('%action' => check_plain($actions[$aid]['description'])));
886
    drupal_set_message(t('Action %action has been unassigned.',
array('%action' => $actions[$aid]['description'])));
896
    $hook = $form_values['hook'] == 'nodeapi' ? 'node' :
$form_values['hook'];
906
    $form_state['redirect'] = 'admin/build/trigger/' . $hook;
916
  }
92
  else {
930
    drupal_goto('admin/build/trigger');
94
  }
956
}
96
97
/**
98
 * Create the form definition for assigning an action to a hook-op
combination.
99
 *
100
 * @param $form_state
101
 *   Information about the current form.
102
 * @param $hook
103
 *   The name of the hook, e.g., 'nodeapi'.
104
 * @param $op
105
 *   The name of the hook operation, e.g., 'insert'.
106
 * @param $description
107
 *   A plain English description of what this hook operation does.
108
 * @return
109
 *
110
 * @ingoup forms
111
 * @see trigger_assign_form_validate()
112
 * @see trigger_assign_form_submit()
113
 */
11461
function trigger_assign_form($form_state, $hook, $op, $description) {
11548
  $form['hook'] = array(
11648
    '#type' => 'hidden',
11748
    '#value' => $hook,
118
  );
11948
  $form['operation'] = array(
12048
    '#type' => 'hidden',
12148
    '#value' => $op,
122
  );
123
  // All of these forms use the same validate and submit functions.
12448
  $form['#validate'][] = 'trigger_assign_form_validate';
12548
  $form['#submit'][] = 'trigger_assign_form_submit';
126
12748
  $options = array();
12848
  $functions = array();
129
  // Restrict the options list to actions that declare support for this
hook-op
130
  // combination.
13148
  foreach (actions_list() as $func => $metadata) {
13248
    if (isset($metadata['hooks']['any']) ||
(isset($metadata['hooks'][$hook]) && is_array($metadata['hooks'][$hook]) &&
(in_array($op, $metadata['hooks'][$hook])))) {
13348
      $functions[] = $func;
13448
    }
13548
  }
13648
  foreach (actions_actions_map(actions_get_all_actions()) as $aid =>
$action) {
13748
    if (in_array($action['callback'], $functions)) {
13848
      $options[$action['type']][$aid] = $action['description'];
13948
    }
14048
  }
141
14248
  $form[$op] = array(
14348
    '#type' => 'fieldset',
14448
    '#title' => t('Trigger: ') . $description,
145
    '#theme' => 'trigger_display'
14648
    );
147
  // Retrieve actions that are already assigned to this hook-op
combination.
14848
  $actions = _trigger_get_hook_actions($hook, $op);
14948
  $form[$op]['assigned']['#type'] = 'value';
15048
  $form[$op]['assigned']['#value'] = array();
15148
  foreach ($actions as $aid => $description) {
15230
    $form[$op]['assigned']['#value'][$aid] = array(
15330
      'description' => $description,
15430
      'link' => l(t('unassign'), "admin/build/trigger/unassign/$hook/$op/"
. md5($aid))
15530
    );
15630
  }
157
15848
  $form[$op]['parent'] = array(
15948
    '#prefix' => "<div class='container-inline'>",
16048
    '#suffix' => '</div>',
161
  );
162
  // List possible actions that may be assigned.
16348
  if (count($options) != 0) {
16448
    array_unshift($options, t('Choose an action'));
16548
    $form[$op]['parent']['aid'] = array(
16648
      '#type' => 'select',
16748
      '#options' => $options,
168
    );
16948
    $form[$op]['parent']['submit'] = array(
17048
      '#type' => 'submit',
17148
      '#value' => t('Assign')
17248
    );
17348
  }
174
  else {
17542
    $form[$op]['none'] = array(
17642
      '#markup' => t('No available actions for this trigger.')
17742
    );
178
  }
17948
  return $form;
1800
}
181
182
/**
183
 * Validation function for trigger_assign_form().
184
 *
185
 * Makes sure that the user is not re-assigning an action to an event.
186
 */
18761
function trigger_assign_form_validate($form, $form_state) {
18818
  $form_values = $form_state['values'];
18918
  if (!empty($form_values['aid'])) {
19018
    $aid = actions_function_lookup($form_values['aid']);
19118
    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))) {
19212
      form_set_error($form_values['operation'], t('The action you chose is
already assigned to that trigger.'));
19312
    }
19418
  }
19518
}
196
197
/**
198
 * Submit function for trigger_assign_form().
199
 */
20061
function trigger_assign_form_submit($form, $form_state) {
2016
  $form_values = $form_state['values'];
202
2036
  if (!empty($form_values['aid'])) {
2046
    $aid = actions_function_lookup($form_values['aid']);
2056
    $weight = db_result(db_query("SELECT MAX(weight) FROM
{trigger_assignments} WHERE hook = '%s' AND op = '%s'",
$form_values['hook'], $form_values['operation']));
2066
    db_query("INSERT INTO {trigger_assignments} values ('%s', '%s', '%s',
%d)", $form_values['hook'], $form_values['operation'], $aid, $weight + 1);
207
    // If this action changes a node property, we need to save the node
208
    // so the change will persist.
2096
    $actions = actions_list();
2106
    if (isset($actions[$aid]['behavior']) &&
in_array('changes_node_property', $actions[$aid]['behavior']) &&
($form_values['operation'] != 'presave')) {
211
      // Delete previous node_save_action if it exists, and re-add a new
one at a higher weight.
2120
      $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']));
2130
      if ($save_post_action_assigned) {
2140
        db_query("DELETE FROM {trigger_assignments} WHERE hook = '%s' AND
op = '%s' AND aid = 'node_save_action'", $form_values['hook'],
$form_values['operation']);
2150
      }
2160
      db_query("INSERT INTO {trigger_assignments} VALUES ('%s', '%s', '%s',
%d)", $form_values['hook'], $form_values['operation'], 'node_save_action',
$weight + 2);
2170
      if (!$save_post_action_assigned) {
2180
        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.'));
2190
      }
2200
    }
2216
  }
2226
}
223
224
/**
225
 * Display actions assigned to this hook-op combination in a table.
226
 *
227
 * @param array $element
228
 *   The fieldset including all assigned actions.
229
 * @return
230
 *   The rendered form with the table prepended.
231
 *
232
 * @ingroup themeable
233
 */
23461
function theme_trigger_display($element) {
23542
  $header = array();
23642
  $rows = array();
23742
  if (count($element['assigned']['#value'])) {
23830
    $header = array(array('data' => t('Name')), array('data' =>
t('Operation')));
23930
    $rows = array();
24030
    foreach ($element['assigned']['#value'] as $aid => $info) {
24130
      $rows[] = array(
24230
        $info['description'],
24330
        $info['link']
24430
      );
24530
    }
24630
  }
247
24842
  if (count($rows)) {
24930
    $output = theme('table', $header, $rows) . drupal_render($element);
25030
  }
251
  else {
25242
    $output = drupal_render($element);
253
  }
25442
  return $output;
2550
}
256
257
258
/**
259
 * Get the actions that have already been defined for this
260
 * type-hook-op combination.
261
 *
262
 * @param $type
263
 *   One of 'node', 'user', 'comment'.
264
 * @param $hook
265
 *   The name of the hook for which actions have been assigned,
266
 *   e.g. 'nodeapi'.
267
 * @param $op
268
 *   The hook operation for which the actions have been assigned,
269
 *   e.g., 'view'.
270
 * @return
271
 *   An array of action descriptions keyed by action IDs.
272
 */
27361
function _trigger_get_hook_actions($hook, $op, $type = NULL) {
27448
  $actions = array();
27548
  if ($type) {
2760
    $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);
2770
  }
278
  else {
27948
    $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);
280
  }
28148
  while ($action = db_fetch_object($result)) {
28230
    $actions[$action->aid] = $action->description;
28330
  }
28448
  return $actions;
2850
}
28661