/[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.20 - (show annotations) (download) (as text)
Mon Nov 2 04:36:25 2009 UTC (3 weeks, 6 days ago) by webchick
Branch: MAIN
Changes since 1.19: +5 -3 lines
File MIME type: text/x-php
#505942 by andypost and hass: Added label context to Trigger description's t() string.
1 <?php
2 // $Id: trigger.admin.inc,v 1.19 2009/10/09 01:00:06 dries 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 $form_values = $form_state['values'];
87 if ($form_values['confirm'] == 1) {
88 $aid = actions_function_lookup($form_values['aid']);
89 db_delete('trigger_assignments')
90 ->condition('hook', $form_values['hook'])
91 ->condition('aid', $aid)
92 ->execute();
93 $actions = actions_get_all_actions();
94 watchdog('actions', 'Action %action has been unassigned.', array('%action' => check_plain($actions[$aid]['label'])));
95 drupal_set_message(t('Action %action has been unassigned.', array('%action' => $actions[$aid]['label'])));
96 $form_state['redirect'] = 'admin/structure/trigger/' . $form_values['module'];
97 }
98 else {
99 drupal_goto('admin/structure/trigger');
100 }
101 }
102
103 /**
104 * Returns the form for assigning an action to a trigger.
105 *
106 * @param $module
107 * The name of the trigger group, e.g., 'node'.
108 * @param $hook
109 * The name of the trigger hook, e.g., 'node_insert'.
110 * @param $label
111 * A plain English description of what this trigger does.
112 *
113 * @ingoup forms
114 * @see trigger_assign_form_validate()
115 * @see trigger_assign_form_submit()
116 */
117 function trigger_assign_form($form, $form_state, $module, $hook, $label) {
118 $form['module'] = array(
119 '#type' => 'hidden',
120 '#value' => $module,
121 );
122 $form['hook'] = array(
123 '#type' => 'hidden',
124 '#value' => $hook,
125 );
126 // All of these forms use the same validate and submit functions.
127 $form['#validate'][] = 'trigger_assign_form_validate';
128 $form['#submit'][] = 'trigger_assign_form_submit';
129
130 $options = array();
131 $functions = array();
132 // Restrict the options list to actions that declare support for this hook.
133 foreach (actions_list() as $func => $metadata) {
134 if (in_array('any', $metadata['triggers']) || in_array($hook, $metadata['triggers'])) {
135 $functions[] = $func;
136 }
137 }
138 foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
139 if (in_array($action['callback'], $functions)) {
140 $options[$action['type']][$aid] = $action['label'];
141 }
142 }
143
144 $form[$hook] = array(
145 '#type' => 'fieldset',
146 // !description is correct, since these labels are passed through t() in
147 // hook_trigger_info().
148 '#title' => t('Trigger: !description', array('!description' => $label)),
149 '#theme' => 'trigger_display',
150 );
151
152 // Retrieve actions that are already assigned to this hook combination.
153 $actions = trigger_get_assigned_actions($hook);
154 $form[$hook]['assigned']['#type'] = 'value';
155 $form[$hook]['assigned']['#value'] = array();
156 foreach ($actions as $aid => $info) {
157 $form[$hook]['assigned']['#value'][$aid] = array(
158 'label' => $info['label'],
159 'link' => l(t('unassign'), "admin/structure/trigger/unassign/$module/$hook/" . md5($aid)),
160 );
161 }
162
163 $form[$hook]['parent'] = array(
164 '#prefix' => "<div class='container-inline'>",
165 '#suffix' => '</div>',
166 );
167 // List possible actions that may be assigned.
168 if (count($options) != 0) {
169 array_unshift($options, t('Choose an action'));
170 $form[$hook]['parent']['aid'] = array(
171 '#type' => 'select',
172 '#options' => $options,
173 );
174 $form[$hook]['parent']['submit'] = array(
175 '#type' => 'submit',
176 '#value' => t('Assign')
177 );
178 }
179 else {
180 $form[$hook]['none'] = array(
181 '#markup' => t('No actions available for this trigger. <a href="@link">Add action</a>.', array('@link' => url('admin/config/system/actions/manage')))
182 );
183 }
184 return $form;
185 }
186
187 /**
188 * Validation function for trigger_assign_form().
189 *
190 * Makes sure that the user is not re-assigning an action to an event.
191 */
192 function trigger_assign_form_validate($form, $form_state) {
193 $form_values = $form_state['values'];
194 if (!empty($form_values['aid'])) {
195 $aid = actions_function_lookup($form_values['aid']);
196 $aid_exists = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND aid = :aid", array(
197 ':hook' => $form_values['hook'],
198 ':aid' => $aid,
199 ))->fetchField();
200 if ($aid_exists) {
201 form_set_error($form_values['hook'], t('The action you chose is already assigned to that trigger.'));
202 }
203 }
204 }
205
206 /**
207 * Submit function for trigger_assign_form().
208 */
209 function trigger_assign_form_submit($form, $form_state) {
210 $form_values = $form_state['values'];
211
212 if (!empty($form_values['aid'])) {
213 $aid = actions_function_lookup($form_values['aid']);
214 $weight = db_query("SELECT MAX(weight) FROM {trigger_assignments} WHERE hook = :hook", array(
215 ':hook' => $form_values['hook'],
216 ))->fetchField();
217
218 db_insert('trigger_assignments')
219 ->fields(array(
220 'hook' => $form_values['hook'],
221 'aid' => $aid,
222 'weight' => $weight + 1,
223 ))
224 ->execute();
225 // If this action changes a node property, we need to save the node
226 // so the change will persist.
227
228 $actions = actions_list();
229 if (isset($actions[$aid]['behavior']) && in_array('changes_node_property', $actions[$aid]['behavior']) && ($form_values['hook'] != 'node_presave') && ($form_values['hook'] != 'comment_presave')) {
230 // Delete previous node_save_action if it exists, and re-add a new one at
231 // a higher weight.
232 $save_post_action_assigned = db_query("SELECT aid FROM {trigger_assignments} WHERE hook = :hook AND aid = :aid", array(
233 ':hook' => $form_values['hook'],
234 ':aid' => 'node_save_action',
235 ))->fetchField();
236
237 if ($save_post_action_assigned) {
238 db_delete('trigger_assignments')
239 ->condition('hook', $form_values['hook'])
240 ->condition('aid', 'node_save_action')
241 ->execute();
242 }
243 db_insert('trigger_assignments')
244 ->fields(array(
245 'hook' => $form_values['hook'],
246 'aid' => 'node_save_action',
247 'weight' => $weight + 2,
248 ))
249 ->execute();
250 if (!$save_post_action_assigned) {
251 drupal_set_message(t("You have added an action that changes a the property of some content. Your 'Save content' action has been moved later in the list so that the property change will be saved."));
252 }
253 }
254 }
255 }
256
257 /**
258 * Displays actions assigned to this hook in a table.
259 *
260 * @param $variables
261 * An associative array containing:
262 * - element: The fieldset including all assigned actions.
263 *
264 * @return
265 * The rendered form with the table prepended.
266 *
267 * @ingroup themeable
268 */
269 function theme_trigger_display($variables) {
270 $element = $variables['element'];
271
272 $header = array();
273 $rows = array();
274 if (isset($element['assigned']) && count($element['assigned']['#value'])) {
275 $header = array(array('data' => t('Name')), array('data' => t('Operation')));
276 $rows = array();
277 foreach ($element['assigned']['#value'] as $aid => $info) {
278 $rows[] = array(
279 $info['label'],
280 $info['link']
281 );
282 }
283 }
284
285 if (count($rows)) {
286 $output = theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($element);
287 }
288 else {
289 $output = drupal_render_children($element);
290 }
291 return $output;
292 }
293

  ViewVC Help
Powered by ViewVC 1.1.2