/[drupal]/drupal/modules/trigger/trigger.admin.inc
ViewVC logotype

Contents of /drupal/modules/trigger/trigger.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.21 - (show annotations) (download) (as text)
Fri Nov 6 03:59:06 2009 UTC (3 weeks, 1 day ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.20: +37 -31 lines
File MIME type: text/x-php
#585868 by sun: Provide a generic way for actions to denote that they change a property.
1 <?php
2 // $Id: trigger.admin.inc,v 1.20 2009/11/02 04:36:25 webchick Exp $
3
4 /**
5 * @file
6 * Admin page callbacks for the trigger module.
7 */
8
9 /**
10 * Builds the form that allows users to assign actions to triggers.
11 *
12 * @param $module_to_display
13 * Which tab of triggers to display. E.g., 'node' for all
14 * node-related triggers.
15 * @return
16 * HTML form.
17 */
18 function trigger_assign($module_to_display = NULL) {
19 // If no type is specified we default to node actions, since they
20 // are the most common.
21 if (!isset($module_to_display)) {
22 drupal_goto('admin/structure/trigger/node');
23 }
24
25 $build = array();
26 $trigger_info = module_invoke_all('trigger_info');
27 drupal_alter('trigger_info', $trigger_info);
28 foreach ($trigger_info as $module => $hooks) {
29 if ($module == $module_to_display) {
30 foreach ($hooks as $hook => $description) {
31 $form_id = 'trigger_' . $hook . '_assign_form';
32 $build[$form_id] = drupal_get_form($form_id, $module, $hook, $description['label']);
33 }
34 }
35 }
36 return $build;
37 }
38
39 /**
40 * Confirm removal of an assigned action.
41 *
42 * @param $module
43 * The tab of triggers the user will be directed to after successful
44 * removal of the action, or if the confirmation form is cancelled.
45 * @param $hook
46 * @param $aid
47 * The action ID.
48 * @ingroup forms
49 * @see trigger_unassign_submit()
50 */
51 function trigger_unassign($form, $form_state, $module, $hook = NULL, $aid = NULL) {
52 if (!($hook && $aid)) {
53 drupal_goto('admin/structure/trigger');
54 }
55
56 $form['hook'] = array(
57 '#type' => 'value',
58 '#value' => $hook,
59 );
60 $form['module'] = array(
61 '#type' => 'value',
62 '#value' => $module,
63 );
64 $form['aid'] = array(
65 '#type' => 'value',
66 '#value' => $aid,
67 );
68
69 $action = actions_function_lookup($aid);
70 $actions = actions_get_all_actions();
71
72 $destination = 'admin/structure/trigger/' . $module;
73
74 return confirm_form($form,
75 t('Are you sure you want to unassign the action %title?', array('%title' => $actions[$action]['label'])),
76 $destination,
77 t('You can assign it again later if you wish.'),
78 t('Unassign'), t('Cancel')
79 );
80 }
81
82 /**
83 * Submit callback for trigger_unassign() form.
84 */
85 function trigger_unassign_submit($form, &$form_state) {
86 if ($form_state['values']['confirm'] == 1) {
87 $aid = actions_function_lookup($form_state['values']['aid']);
88 db_delete('trigger_assignments')
89 ->condition('hook', $form_state['values']['hook'])
90 ->condition('aid', $aid)
91 ->execute();
92 $actions = actions_get_all_actions();
93 watchdog('actions', 'Action %action has been unassigned.', array('%action' => check_plain($actions[$aid]['label'])));
94 drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['label'])));
95 $form_state['redirect'] = 'admin/structure/trigger/' . $form_state['values']['module'];
96 }
97 else {
98 drupal_goto('admin/structure/trigger');
99 }
100 }
101
102 /**
103 * Returns the form for assigning an action to a trigger.
104 *
105 * @param $module
106 * The name of the trigger group, e.g., 'node'.
107 * @param $hook
108 * The name of the trigger hook, e.g., 'node_insert'.
109 * @param $label
110 * A plain English description of what this trigger does.
111 *
112 * @ingoup forms
113 * @see trigger_assign_form_validate()
114 * @see trigger_assign_form_submit()
115 */
116 function trigger_assign_form($form, $form_state, $module, $hook, $label) {
117 $form['module'] = array(
118 '#type' => 'hidden',
119 '#value' => $module,
120 );
121 $form['hook'] = array(
122 '#type' => 'hidden',
123 '#value' => $hook,
124 );
125 // All of these forms use the same validate and submit functions.
126 $form['#validate'][] = 'trigger_assign_form_validate';
127 $form['#submit'][] = 'trigger_assign_form_submit';
128
129 $options = array();
130 $functions = array();
131 // Restrict the options list to actions that declare support for this hook.
132 foreach (actions_list() as $func => $metadata) {
133 if (in_array('any', $metadata['triggers']) || in_array($hook, $metadata['triggers'])) {
134 $functions[] = $func;
135 }
136 }
137 foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
138 if (in_array($action['callback'], $functions)) {
139 $options[$action['type']][$aid] = $action['label'];
140 }
141 }
142
143 $form[$hook] = array(
144 '#type' => 'fieldset',
145 // !description is correct, since these labels are passed through t() in
146 // hook_trigger_info().
147 '#title' => t('Trigger: !description', array('!description' => $label)),
148 '#theme' => 'trigger_display',
149 );
150
151 // Retrieve actions that are already assigned to this hook combination.
152 $actions = trigger_get_assigned_actions($hook);
153 $form[$hook]['assigned']['#type'] = 'value';
154 $form[$hook]['assigned']['#value'] = array();
155 foreach ($actions as $aid => $info) {
156 $form[$hook]['assigned']['#value'][$aid] = array(
157 'label' => $info['label'],
158 'link' => l(t('unassign'), "admin/structure/trigger/unassign/$module/$hook/" . md5($aid)),
159 );
160 }
161
162 $form[$hook]['parent'] = array(
163 '#prefix' => "<div class='container-inline'>",
164 '#suffix' => '</div>',
165 );
166 // List possible actions that may be assigned.
167 if (count($options) != 0) {
168 array_unshift($options, t('Choose an action'));
169 $form[$hook]['parent']['aid'] = array(
170 '#type' => 'select',
171 '#options' => $options,
172 );
173 $form[$hook]['parent']['submit'] = array(
174 '#type' => 'submit',
175 '#value' => t('Assign')
176 );
177 }
178 else {
179 $form[$hook]['none'] = array(
180 '#markup' => t('No actions available for this trigger. <a href="@link">Add action</a>.', array('@link' => url('admin/config/system/actions/manage')))
181 );
182 }
183 return $form;
184 }
185
186 /**
187 * Validation function for trigger_assign_form().
188 *
189 * Makes sure that the user is not re-assigning an action to an event.
190 */
191 function trigger_assign_form_validate($form, $form_state) {
192 $form_values = $form_state['values'];
193 if (!empty($form_values['aid'])) {
194 $aid = actions_function_lookup($form_values['aid']);
195 $aid_exists = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND aid = :aid", array(
196 ':hook' => $form_values['hook'],
197 ':aid' => $aid,
198 ))->fetchField();
199 if ($aid_exists) {
200 form_set_error($form_values['hook'], t('The action you chose is already assigned to that trigger.'));
201 }
202 }
203 }
204
205 /**
206 * Submit function for trigger_assign_form().
207 */
208 function trigger_assign_form_submit($form, &$form_state) {
209 if (!empty($form_state['values']['aid'])) {
210 $aid = actions_function_lookup($form_state['values']['aid']);
211 $weight = db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = :hook", array(':hook' => $form_state['values']['hook']))->fetchField();
212
213 // Insert the new action.
214 db_insert('trigger_assignments')
215 ->fields(array(
216 'hook' => $form_state['values']['hook'],
217 'aid' => $aid,
218 'weight' => $weight + 1,
219 ))
220 ->execute();
221
222 // If we are not configuring an action for a "presave" hook and this action
223 // changes an object property, then we need to save the object, so the
224 // property change will persist.
225 $actions = actions_list();
226 if (strpos($form_state['values']['hook'], 'presave') === FALSE && isset($actions[$aid]['behavior']) && in_array('changes_property', $actions[$aid]['behavior'])) {
227 // Determine the corresponding save action name for this action.
228 $save_action = strtok($aid, '_') . '_save_action';
229 // If no corresponding save action exists, we need to bail out.
230 if (!isset($actions[$save_action])) {
231 throw new Exception(t('Missing/undefined save action (%save_aid) for %aid action.', array('%save_aid' => $aid, '%aid' => $aid)));
232 }
233 // Delete previous save action if it exists, and re-add it using a higher
234 // weight.
235 $save_action_assigned = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND aid = :aid", array(':hook' => $form_state['values']['hook'], ':aid' => $save_action))->fetchField();
236
237 if ($save_action_assigned) {
238 db_delete('trigger_assignments')
239 ->condition('hook', $form_state['values']['hook'])
240 ->condition('aid', $save_action)
241 ->execute();
242 }
243 db_insert('trigger_assignments')
244 ->fields(array(
245 'hook' => $form_state['values']['hook'],
246 'aid' => $save_action,
247 'weight' => $weight + 2,
248 ))
249 ->execute();
250
251 // If no save action existed before, inform the user about it.
252 if (!$save_action_assigned) {
253 drupal_set_message(t('The %label action has been appended, which is required to save the property change.', array('%label' => $actions[$save_action]['label'])));
254 }
255 // Otherwise, just inform about the new weight.
256 else {
257 drupal_set_message(t('The %label action was moved to save the property change.', array('%label' => $actions[$save_action]['label'])));
258 }
259 }
260 }
261 }
262
263 /**
264 * Displays actions assigned to this hook in a table.
265 *
266 * @param $variables
267 * An associative array containing:
268 * - element: The fieldset including all assigned actions.
269 *
270 * @return
271 * The rendered form with the table prepended.
272 *
273 * @ingroup themeable
274 */
275 function theme_trigger_display($variables) {
276 $element = $variables['element'];
277
278 $header = array();
279 $rows = array();
280 if (isset($element['assigned']) && count($element['assigned']['#value'])) {
281 $header = array(array('data' => t('Name')), array('data' => t('Operation')));
282 $rows = array();
283 foreach ($element['assigned']['#value'] as $aid => $info) {
284 $rows[] = array(
285 $info['label'],
286 $info['link']
287 );
288 }
289 }
290
291 if (count($rows)) {
292 $output = theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($element);
293 }
294 else {
295 $output = drupal_render_children($element);
296 }
297 return $output;
298 }
299

  ViewVC Help
Powered by ViewVC 1.1.2