1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Actions support for the Flag module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_flag_flag(). Trigger actions if any are available.
|
10 |
|
|
*/
|
11 |
|
|
function flag_actions_flag_flag($flag, $entity_id, $account, $flagging) {
|
12 |
|
|
flag_actions_do('flag', $flag, $entity_id, $account);
|
13 |
|
|
}
|
14 |
|
|
|
15 |
|
|
/**
|
16 |
|
|
* Implements hook_flag_unflag(). Trigger actions if any are available.
|
17 |
|
|
*/
|
18 |
|
|
function flag_actions_flag_unflag($flag, $entity_id, $account, $flagging) {
|
19 |
|
|
flag_actions_do('unflag', $flag, $entity_id, $account);
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
/**
|
23 |
|
|
* Implements hook_menu().
|
24 |
|
|
*/
|
25 |
|
|
function flag_actions_menu() {
|
26 |
|
|
$items = array();
|
27 |
|
|
|
28 |
|
|
$items[FLAG_ADMIN_PATH . '/actions'] = array(
|
29 |
|
|
'title' => 'Actions',
|
30 |
|
|
'page callback' => 'flag_actions_page',
|
31 |
|
|
'access callback' => 'user_access',
|
32 |
|
|
'access arguments' => array('administer actions'),
|
33 |
|
|
'type' => MENU_LOCAL_TASK,
|
34 |
|
|
'weight' => 1,
|
35 |
|
|
);
|
36 |
|
|
$items[FLAG_ADMIN_PATH . '/actions/add'] = array(
|
37 |
|
|
'title' => 'Add action',
|
38 |
|
|
'page callback' => 'drupal_get_form',
|
39 |
|
|
'page arguments' => array('flag_actions_form', NULL, 5),
|
40 |
|
|
'access callback' => 'user_access',
|
41 |
|
|
'access arguments' => array('administer actions'),
|
42 |
|
|
'type' => MENU_CALLBACK,
|
43 |
|
|
);
|
44 |
|
|
$items[FLAG_ADMIN_PATH . '/actions/delete'] = array(
|
45 |
|
|
'title' => 'Delete action',
|
46 |
|
|
'page callback' => 'drupal_get_form',
|
47 |
|
|
'page arguments' => array('flag_actions_delete_form', 5),
|
48 |
|
|
'access callback' => 'user_access',
|
49 |
|
|
'access arguments' => array('administer actions'),
|
50 |
|
|
'type' => MENU_CALLBACK,
|
51 |
|
|
);
|
52 |
|
|
$items[FLAG_ADMIN_PATH . '/actions/configure'] = array(
|
53 |
|
|
'title' => 'Edit action',
|
54 |
|
|
'page callback' => 'drupal_get_form',
|
55 |
|
|
'page arguments' => array('flag_actions_form', 5),
|
56 |
|
|
'access callback' => 'user_access',
|
57 |
|
|
'access arguments' => array('administer actions'),
|
58 |
|
|
'type' => MENU_CALLBACK,
|
59 |
|
|
);
|
60 |
|
|
|
61 |
|
|
return $items;
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
/**
|
65 |
|
|
* Implements hook_theme().
|
66 |
|
|
*/
|
67 |
|
|
function flag_actions_theme() {
|
68 |
|
|
return array(
|
69 |
|
|
'flag_actions_page' => array(
|
70 |
|
|
'variables' => array('actions' => NULL, 'form' => NULL),
|
71 |
|
|
),
|
72 |
|
|
'flag_actions_add_form' => array(
|
73 |
|
|
'render element' => 'form',
|
74 |
|
|
),
|
75 |
|
|
'flag_actions_flag_form' => array(
|
76 |
|
|
'render element' => 'form',
|
77 |
|
|
),
|
78 |
|
|
);
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
function flag_actions_get_action($aid) {
|
82 |
|
|
$actions = flag_actions_get_actions();
|
83 |
|
|
return $actions[$aid];
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
function flag_actions_get_actions($flag_name = NULL, $reset = FALSE) {
|
87 |
|
|
$flag_actions = &drupal_static(__FUNCTION__);
|
88 |
|
|
module_load_include('inc', 'flag', 'includes/flag.actions');
|
89 |
|
|
|
90 |
|
|
// Get a list of all possible actions defined by modules.
|
91 |
|
|
$actions = module_invoke_all('action_info');
|
92 |
|
|
|
93 |
|
|
// Retrieve the list of user-defined flag actions.
|
94 |
|
|
if (!isset($flag_actions) || $reset) {
|
95 |
|
|
$flag_actions = array();
|
96 |
|
|
$query = db_select('flag_actions', 'a');
|
97 |
|
|
$query->innerJoin('flag', 'f', 'a.fid = f.fid');
|
98 |
|
|
$query->addField('f', 'name', 'flag');
|
99 |
|
|
$result = $query
|
100 |
|
|
->fields('a')
|
101 |
|
|
->execute();
|
102 |
|
|
foreach ($result as $action) {
|
103 |
|
|
if (!isset($actions[$action->callback])) {
|
104 |
|
|
$actions[$action->callback] = array(
|
105 |
|
|
'description' => t('Missing action "@action-callback". Module providing it was either uninstalled or disabled.', array('@action-callback' => $action->callback)),
|
106 |
|
|
'configurable' => FALSE,
|
107 |
|
|
'type' => 'node',
|
108 |
|
|
'missing' => TRUE,
|
109 |
|
|
);
|
110 |
|
|
}
|
111 |
|
|
$action->parameters = unserialize($action->parameters);
|
112 |
|
|
$action->label = $actions[$action->callback]['label'];
|
113 |
|
|
$action->configurable = $actions[$action->callback]['configurable'];
|
114 |
|
|
$action->behavior = isset($actions[$action->callback]['behavior']) ? $actions[$action->callback]['behavior'] : array();
|
115 |
|
|
$action->type = $actions[$action->callback]['type'];
|
116 |
|
|
$action->missing = !empty($actions[$action->callback]['missing']);
|
117 |
|
|
|
118 |
|
|
$flag_actions[$action->aid] = $action;
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
// Filter actions to a specified flag.
|
123 |
|
|
if (isset($flag_name)) {
|
124 |
|
|
$specific_flag_actions = array();
|
125 |
|
|
foreach ($flag_actions as $aid => $action) {
|
126 |
|
|
if ($action->flag == $flag_name) {
|
127 |
|
|
$specific_flag_actions[$aid] = $action;
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
return $specific_flag_actions;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
return $flag_actions;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
/**
|
137 |
|
|
* Insert a new flag action.
|
138 |
|
|
*
|
139 |
fd5a8e62
|
Assos Assos
|
* @param int $fid
|
140 |
85ad3d82
|
Assos Assos
|
* The flag object ID.
|
141 |
fd5a8e62
|
Assos Assos
|
* @param string $event
|
142 |
85ad3d82
|
Assos Assos
|
* The flag event, such as "flag" or "unflag".
|
143 |
fd5a8e62
|
Assos Assos
|
* @param int $threshold
|
144 |
85ad3d82
|
Assos Assos
|
* The flagging threshold at which this action will be executed.
|
145 |
fd5a8e62
|
Assos Assos
|
* @param int $repeat_threshold
|
146 |
85ad3d82
|
Assos Assos
|
* The number of additional flaggings after which the action will be repeated.
|
147 |
fd5a8e62
|
Assos Assos
|
* @param string $callback
|
148 |
85ad3d82
|
Assos Assos
|
* The action callback to be executed.
|
149 |
fd5a8e62
|
Assos Assos
|
* @param array $parameters
|
150 |
85ad3d82
|
Assos Assos
|
* The action parameters.
|
151 |
|
|
*/
|
152 |
|
|
function flag_actions_insert_action($fid, $event, $threshold, $repeat_threshold, $callback, $parameters) {
|
153 |
|
|
return db_insert('flag_actions')
|
154 |
|
|
->fields(array(
|
155 |
|
|
'fid' => $fid,
|
156 |
|
|
'event' => $event,
|
157 |
|
|
'threshold' => $threshold,
|
158 |
|
|
'repeat_threshold' => $repeat_threshold,
|
159 |
|
|
'callback' => $callback,
|
160 |
|
|
'parameters' => serialize($parameters),
|
161 |
|
|
))
|
162 |
|
|
->execute();
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
/**
|
166 |
|
|
* Update an existing flag action.
|
167 |
|
|
*
|
168 |
fd5a8e62
|
Assos Assos
|
* @param int $aid
|
169 |
85ad3d82
|
Assos Assos
|
* The flag action ID to update.
|
170 |
fd5a8e62
|
Assos Assos
|
* @param string $event
|
171 |
85ad3d82
|
Assos Assos
|
* The flag event, such as "flag" or "unflag".
|
172 |
fd5a8e62
|
Assos Assos
|
* @param int $threshold
|
173 |
85ad3d82
|
Assos Assos
|
* The flagging threshold at which this action will be executed.
|
174 |
fd5a8e62
|
Assos Assos
|
* @param int $repeat_threshold
|
175 |
85ad3d82
|
Assos Assos
|
* The number of additional flaggings after which the action will be repeated.
|
176 |
fd5a8e62
|
Assos Assos
|
* @param array $parameters
|
177 |
85ad3d82
|
Assos Assos
|
* The action parameters.
|
178 |
|
|
*/
|
179 |
|
|
function flag_actions_update_action($aid, $event, $threshold, $repeat_threshold, $parameters) {
|
180 |
|
|
return db_update('flag_actions')
|
181 |
|
|
->fields(array(
|
182 |
|
|
'event' => $event,
|
183 |
|
|
'threshold' => $threshold,
|
184 |
|
|
'repeat_threshold' => $repeat_threshold,
|
185 |
|
|
'parameters' => serialize($parameters),
|
186 |
|
|
))
|
187 |
|
|
->condition('aid', $aid)
|
188 |
|
|
->execute();
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
/**
|
192 |
|
|
* Delete a flag action.
|
193 |
|
|
*
|
194 |
fd5a8e62
|
Assos Assos
|
* @param int $aid
|
195 |
85ad3d82
|
Assos Assos
|
* The flag action ID to delete.
|
196 |
|
|
*/
|
197 |
|
|
function flag_actions_delete_action($aid) {
|
198 |
|
|
return db_delete('flag_actions', array('return' => Database::RETURN_AFFECTED))
|
199 |
|
|
->condition('aid', $aid)
|
200 |
|
|
->execute();
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
/**
|
204 |
|
|
* Perform flag actions.
|
205 |
|
|
*/
|
206 |
|
|
function flag_actions_do($event, $flag, $entity_id, $account) {
|
207 |
|
|
$actions = flag_actions_get_actions($flag->name);
|
208 |
|
|
if (!$actions) {
|
209 |
|
|
return;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
$flag_action = $flag->get_flag_action($entity_id);
|
213 |
|
|
$flag_action->action = $event;
|
214 |
|
|
$flag_action->count = $count = $flag->get_count($entity_id);
|
215 |
|
|
$relevant_objects = $flag->get_relevant_action_objects($entity_id);
|
216 |
|
|
$object_changed = FALSE;
|
217 |
|
|
foreach ($actions as $aid => $action) {
|
218 |
|
|
if ($action->event == 'flag') {
|
219 |
|
|
$at_threshold = ($count == $action->threshold);
|
220 |
|
|
$repeat = $action->repeat_threshold ? (($count > $action->threshold) && (($count - $action->threshold) % $action->repeat_threshold == 0)) : FALSE;
|
221 |
|
|
}
|
222 |
|
|
elseif ($action->event == 'unflag') {
|
223 |
|
|
$at_threshold = ($count == $action->threshold - 1);
|
224 |
|
|
$repeat = $action->repeat_threshold ? (($count < $action->threshold - 1) && (($count - $action->threshold - 1) % $action->repeat_threshold == 0)) : FALSE;
|
225 |
|
|
}
|
226 |
|
|
if (($at_threshold || $repeat) && $action->event == $event && !$action->missing) {
|
227 |
|
|
$context = $action->parameters;
|
228 |
|
|
$context['callback'] = $action->callback;
|
229 |
|
|
// We're setting 'hook' to something, to prevent PHP warnings by actions
|
230 |
|
|
// who read it. Maybe we should set it to nodeapi/comment/user, depending
|
231 |
|
|
// on the flag, because these three are among the only hooks some actions
|
232 |
|
|
// in system.module "know" to work with.
|
233 |
|
|
$context['hook'] = 'flag';
|
234 |
|
|
$context['type'] = $action->type;
|
235 |
|
|
$context['account'] = $account;
|
236 |
|
|
$context['flag'] = $flag;
|
237 |
|
|
$context['flag-action'] = $flag_action;
|
238 |
|
|
// We add to the $context all the objects we know about:
|
239 |
|
|
$context = array_merge($relevant_objects, $context);
|
240 |
|
|
$callback = $action->callback;
|
241 |
|
|
|
242 |
|
|
if (isset($relevant_objects[$action->type])) {
|
243 |
|
|
$callback($relevant_objects[$action->type], $context);
|
244 |
|
|
}
|
245 |
|
|
else {
|
246 |
|
|
// What object shall we send as last resort? Let's send a node, or
|
247 |
|
|
// the flag's object.
|
248 |
|
|
if (isset($relevant_objects['node'])) {
|
249 |
|
|
$callback($relevant_objects['node'], $context);
|
250 |
|
|
}
|
251 |
|
|
else {
|
252 |
|
|
$callback($relevant_objects[$flag->entity_type], $context);
|
253 |
|
|
}
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
if (is_array($action->behavior) && in_array('changes_property', $action->behavior)) {
|
257 |
|
|
$object_changed = TRUE;
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
// Actions by default do not save elements unless the save action is
|
263 |
|
|
// explicitly added. We run it automatically upon flagging.
|
264 |
|
|
if ($object_changed) {
|
265 |
|
|
$save_action = $action->type . '_save_action';
|
266 |
|
|
if (function_exists($save_action)) {
|
267 |
|
|
$save_action($relevant_objects[$action->type]);
|
268 |
|
|
}
|
269 |
|
|
}
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
/**
|
273 |
|
|
* Menu callback for FLAG_ADMIN_PATH/actions.
|
274 |
|
|
*/
|
275 |
|
|
function flag_actions_page() {
|
276 |
|
|
$actions = flag_actions_get_actions();
|
277 |
|
|
$add_action_form = drupal_get_form('flag_actions_add_form');
|
278 |
|
|
|
279 |
|
|
return theme('flag_actions_page', array('actions' => $actions, 'form' => $add_action_form));
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
/**
|
283 |
|
|
* Theme the list of actions currently in place for flags.
|
284 |
|
|
*/
|
285 |
|
|
function theme_flag_actions_page($variables) {
|
286 |
|
|
$actions = $variables['actions'];
|
287 |
|
|
$add_action_form = $variables['form'];
|
288 |
|
|
|
289 |
|
|
$rows = array();
|
290 |
|
|
foreach ($actions as $action) {
|
291 |
|
|
$flag = flag_get_flag($action->flag);
|
292 |
|
|
|
293 |
|
|
// Build a sample string representing repeating actions.
|
294 |
|
|
if ($action->repeat_threshold) {
|
295 |
|
|
$repeat_count = 3;
|
296 |
|
|
$repeat_subtract = ($action->event == 'flag') ? 1 : -1;
|
297 |
|
|
$repeat_samples = array();
|
298 |
|
|
for ($n = 1; $n < $repeat_count + 2; $n++) {
|
299 |
|
|
$sample = $action->threshold + (($n * $action->repeat_threshold) * $repeat_subtract);
|
300 |
|
|
if ($sample > 0) {
|
301 |
|
|
$repeat_samples[] = $sample;
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
if (count($repeat_samples) > $repeat_count) {
|
305 |
|
|
$repeat_samples[$repeat_count] = '…';
|
306 |
|
|
}
|
307 |
|
|
$repeat_string = implode(', ', $repeat_samples);
|
308 |
|
|
}
|
309 |
|
|
else {
|
310 |
|
|
$repeat_string = '-';
|
311 |
|
|
}
|
312 |
|
|
|
313 |
|
|
$row = array();
|
314 |
|
|
$row[] = $flag->get_title();
|
315 |
|
|
$row[] = ($action->event == 'flag' ? '≥ ' : '< ') . $action->threshold;
|
316 |
|
|
$row[] = $repeat_string;
|
317 |
|
|
$row[] = empty($action->missing) ? $action->label : '<div class="error">' . $action->label . '</div>';
|
318 |
|
|
$row[] = l(t('edit'), FLAG_ADMIN_PATH . '/actions/configure/' . $action->aid);
|
319 |
|
|
$row[] = l(t('delete'), FLAG_ADMIN_PATH . '/actions/delete/' . $action->aid);
|
320 |
|
|
$rows[] = $row;
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
if (empty($rows)) {
|
324 |
|
|
$rows[] = array(array('data' => t('Currently no flag actions. Use the <em>Add new flag action</em> form to add an action.'), 'colspan' => 6));
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
$header = array(
|
328 |
|
|
t('Flag'),
|
329 |
|
|
t('Threshold'),
|
330 |
|
|
t('Repeats'),
|
331 |
|
|
t('Action'),
|
332 |
|
|
array('data' => t('Operations'), 'colspan' => 2),
|
333 |
|
|
);
|
334 |
|
|
|
335 |
|
|
$output = '';
|
336 |
|
|
$output .= theme('table', array('header' => $header, 'rows' => $rows));
|
337 |
|
|
$output .= drupal_render($add_action_form);
|
338 |
|
|
return $output;
|
339 |
|
|
}
|
340 |
|
|
|
341 |
|
|
/**
|
342 |
|
|
* Modified version of the Add action form that redirects back to the flag list.
|
343 |
|
|
*/
|
344 |
|
|
function flag_actions_add_form($form, &$form_state) {
|
345 |
|
|
$flags = flag_get_flags();
|
346 |
|
|
$options = array();
|
347 |
|
|
foreach ($flags as $flag) {
|
348 |
|
|
$options[$flag->name] = $flag->get_title();
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
if (empty($options)) {
|
352 |
|
|
$options[] = t('No flag available');
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
$form['flag'] = array(
|
356 |
|
|
'#type' => 'select',
|
357 |
|
|
'#options' => empty($options) ? array(t('No flag available')) : $options,
|
358 |
|
|
'#disabled' => empty($options),
|
359 |
|
|
'#title' => t('Select a flag'),
|
360 |
|
|
);
|
361 |
|
|
|
362 |
|
|
$form['submit'] = array(
|
363 |
|
|
'#type' => 'submit',
|
364 |
|
|
'#value' => t('Add action'),
|
365 |
|
|
);
|
366 |
|
|
|
367 |
|
|
return $form;
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
function flag_actions_add_form_submit($form, &$form_state) {
|
371 |
|
|
if ($form_state['values']['flag']) {
|
372 |
|
|
$form_state['redirect'] = array(FLAG_ADMIN_PATH . '/actions/add/' . $form_state['values']['flag']);
|
373 |
|
|
}
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
function theme_flag_actions_add_form($variables) {
|
377 |
|
|
$form = $variables['form'];
|
378 |
|
|
|
379 |
|
|
$fieldset = array(
|
380 |
|
|
'#type' => 'fieldset',
|
381 |
|
|
'#title' => t('Add a new flag action'),
|
382 |
76e2e7c3
|
Assos Assos
|
'#children' => '<div class="container-inline">' . drupal_render($form['flag']) . drupal_render($form['submit']) . '</div>',
|
383 |
85ad3d82
|
Assos Assos
|
'#parents' => array('add_action'),
|
384 |
|
|
'#attributes' => array(),
|
385 |
|
|
'#groups' => array('add_action' => array()),
|
386 |
|
|
);
|
387 |
|
|
|
388 |
|
|
return drupal_render($fieldset) . drupal_render_children($form);
|
389 |
|
|
}
|
390 |
|
|
|
391 |
|
|
/**
|
392 |
|
|
* Generic configuration form for configuration of flag actions.
|
393 |
|
|
*
|
394 |
|
|
* @param $form_state
|
395 |
|
|
* The form state.
|
396 |
fd5a8e62
|
Assos Assos
|
* @param int|NULL $aid
|
397 |
85ad3d82
|
Assos Assos
|
* If editing an action, an action ID must be passed in.
|
398 |
fd5a8e62
|
Assos Assos
|
* @param string|NULL $flag_name
|
399 |
85ad3d82
|
Assos Assos
|
* If adding a new action to a flag, a flag name must be specified.
|
400 |
|
|
*/
|
401 |
|
|
function flag_actions_form($form, &$form_state, $aid = NULL, $flag_name = NULL) {
|
402 |
|
|
// This is a multistep form. Get the callback value if set and continue.
|
403 |
|
|
if (isset($form_state['storage']['callback'])) {
|
404 |
|
|
$callback = $form_state['storage']['callback'];
|
405 |
|
|
unset($form_state['storage']['callback']);
|
406 |
|
|
}
|
407 |
|
|
|
408 |
|
|
if (isset($aid)) {
|
409 |
|
|
$action = flag_actions_get_action($aid);
|
410 |
|
|
$callback = $action->callback;
|
411 |
|
|
$flag = flag_get_flag($action->flag);
|
412 |
|
|
drupal_set_title(t('Edit the "@action" action for the @title flag', array('@action' => $action->label, '@title' => $flag->get_title())));
|
413 |
|
|
}
|
414 |
|
|
elseif (isset($flag_name)) {
|
415 |
|
|
$flag = flag_get_flag($flag_name);
|
416 |
|
|
}
|
417 |
|
|
|
418 |
|
|
if (empty($flag)) {
|
419 |
|
|
drupal_not_found();
|
420 |
|
|
}
|
421 |
|
|
|
422 |
|
|
$form['new'] = array(
|
423 |
|
|
'#type' => 'value',
|
424 |
76e2e7c3
|
Assos Assos
|
'#value' => isset($callback) ? FALSE : TRUE,
|
425 |
85ad3d82
|
Assos Assos
|
);
|
426 |
|
|
|
427 |
|
|
if (!isset($callback)) {
|
428 |
|
|
drupal_set_title(t('Add an action to the @title flag', array('@title' => $flag->get_title())));
|
429 |
|
|
|
430 |
|
|
$actions = $flag->get_valid_actions();
|
431 |
|
|
$options = array();
|
432 |
76e2e7c3
|
Assos Assos
|
foreach ($actions as $key => $action) {
|
433 |
85ad3d82
|
Assos Assos
|
$options[$key] = $action['label'];
|
434 |
|
|
}
|
435 |
|
|
|
436 |
|
|
$form['callback'] = array(
|
437 |
|
|
'#title' => t('Select an action'),
|
438 |
|
|
'#type' => 'select',
|
439 |
|
|
'#options' => $options,
|
440 |
|
|
);
|
441 |
|
|
|
442 |
|
|
$form['submit'] = array(
|
443 |
|
|
'#type' => 'submit',
|
444 |
|
|
'#value' => t('Continue'),
|
445 |
|
|
);
|
446 |
|
|
|
447 |
|
|
return $form;
|
448 |
|
|
}
|
449 |
|
|
elseif (!isset($action)) {
|
450 |
|
|
$actions = $flag->get_valid_actions();
|
451 |
76e2e7c3
|
Assos Assos
|
$action = (object) $actions[$callback];
|
452 |
85ad3d82
|
Assos Assos
|
$action->parameters = array();
|
453 |
|
|
$action->event = 'flag';
|
454 |
|
|
$action->threshold = 10;
|
455 |
|
|
$action->repeat_threshold = 0;
|
456 |
|
|
drupal_set_title(t('Add "@action" action to the @title flag', array('@action' => $action->label, '@title' => $flag->get_title())));
|
457 |
|
|
}
|
458 |
|
|
|
459 |
|
|
$form['flag'] = array(
|
460 |
|
|
'#tree' => TRUE,
|
461 |
|
|
'#weight' => -9,
|
462 |
|
|
'#theme' => 'flag_actions_flag_form',
|
463 |
|
|
'#action' => $action,
|
464 |
|
|
'#flag' => $flag,
|
465 |
|
|
);
|
466 |
|
|
|
467 |
|
|
$form['flag']['flag'] = array(
|
468 |
|
|
'#type' => 'value',
|
469 |
|
|
'#value' => $flag,
|
470 |
|
|
);
|
471 |
|
|
|
472 |
|
|
$form['flag']['callback'] = array(
|
473 |
|
|
'#type' => 'value',
|
474 |
|
|
'#value' => $callback,
|
475 |
|
|
);
|
476 |
|
|
|
477 |
|
|
$form['flag']['aid'] = array(
|
478 |
|
|
'#type' => 'value',
|
479 |
|
|
'#value' => $aid,
|
480 |
|
|
);
|
481 |
|
|
|
482 |
|
|
$form['flag']['event'] = array(
|
483 |
|
|
'#type' => 'select',
|
484 |
|
|
'#options' => array(
|
485 |
|
|
'flag' => t('reaches'),
|
486 |
|
|
'unflag' => t('falls below'),
|
487 |
|
|
),
|
488 |
|
|
'#default_value' => $action->event,
|
489 |
|
|
);
|
490 |
|
|
|
491 |
|
|
$form['flag']['threshold'] = array(
|
492 |
|
|
'#type' => 'textfield',
|
493 |
|
|
'#size' => 6,
|
494 |
|
|
'#maxlength' => 6,
|
495 |
|
|
'#default_value' => $action->threshold,
|
496 |
|
|
'#required' => TRUE,
|
497 |
|
|
);
|
498 |
|
|
|
499 |
|
|
$form['flag']['repeat_threshold'] = array(
|
500 |
|
|
'#type' => 'textfield',
|
501 |
|
|
'#size' => 6,
|
502 |
|
|
'#maxlength' => 6,
|
503 |
|
|
'#default_value' => $action->repeat_threshold,
|
504 |
|
|
);
|
505 |
|
|
|
506 |
|
|
if ($flag->global) {
|
507 |
|
|
$form['flag']['threshold']['#disabled'] = 1;
|
508 |
|
|
$form['flag']['threshold']['#value'] = 1;
|
509 |
|
|
$form['flag']['repeat_threshold']['#access'] = FALSE;
|
510 |
|
|
$form['flag']['repeat_threshold']['#value'] = 0;
|
511 |
|
|
}
|
512 |
|
|
|
513 |
|
|
// Merge in the standard flag action form.
|
514 |
76e2e7c3
|
Assos Assos
|
$action_form = $callback . '_form';
|
515 |
85ad3d82
|
Assos Assos
|
$edit = array();
|
516 |
|
|
if (function_exists($action_form)) {
|
517 |
|
|
$edit += $action->parameters;
|
518 |
|
|
$edit['actions_label'] = $action->label;
|
519 |
|
|
$edit['actions_type'] = $action->type;
|
520 |
|
|
$edit['actions_flag'] = $flag->name;
|
521 |
|
|
$additions = flag_actions_form_additions($action_form, $edit);
|
522 |
|
|
$form = array_merge($form, $additions);
|
523 |
|
|
}
|
524 |
|
|
|
525 |
|
|
// Add a few customizations to existing flag actions.
|
526 |
76e2e7c3
|
Assos Assos
|
$flag_actions_form = 'flag_actions_' . $callback . '_form';
|
527 |
85ad3d82
|
Assos Assos
|
if (function_exists($flag_actions_form)) {
|
528 |
|
|
$flag_actions_form($form, $flag, $edit);
|
529 |
|
|
}
|
530 |
|
|
|
531 |
|
|
$form['submit'] = array(
|
532 |
|
|
'#type' => 'submit',
|
533 |
|
|
'#value' => t('Submit'),
|
534 |
|
|
);
|
535 |
|
|
|
536 |
|
|
return $form;
|
537 |
|
|
}
|
538 |
|
|
|
539 |
|
|
/**
|
540 |
|
|
* Execute an action form callback to retrieve form additions.
|
541 |
|
|
*
|
542 |
|
|
* This function prevents the form callback from modifying local variables.
|
543 |
|
|
*/
|
544 |
|
|
function flag_actions_form_additions($callback, $edit) {
|
545 |
|
|
return $callback($edit);
|
546 |
|
|
}
|
547 |
|
|
|
548 |
|
|
/**
|
549 |
|
|
* Generic submit handler for validating flag actions.
|
550 |
|
|
*/
|
551 |
|
|
function flag_actions_form_validate($form, &$form_state) {
|
552 |
|
|
// Special validation handlers may be needed to save this form properly.
|
553 |
|
|
// Try to load the action's validation routine if needed.
|
554 |
|
|
if (isset($form_state['values']['flag']['callback'])) {
|
555 |
|
|
$callback = $form_state['values']['flag']['callback'];
|
556 |
|
|
$validate_function = $callback . '_validate';
|
557 |
|
|
if (function_exists($validate_function)) {
|
558 |
|
|
$validate_function($form, $form_state);
|
559 |
|
|
}
|
560 |
|
|
}
|
561 |
|
|
}
|
562 |
|
|
|
563 |
|
|
/**
|
564 |
|
|
* Generic submit handler for saving flag actions.
|
565 |
|
|
*/
|
566 |
|
|
function flag_actions_form_submit($form, &$form_state) {
|
567 |
|
|
// If simply gathering the callback, save it to form state storage and
|
568 |
|
|
// rebuild the form to gather the complete information.
|
569 |
|
|
if ($form_state['values']['new']) {
|
570 |
|
|
$form_state['storage']['callback'] = $form_state['values']['callback'];
|
571 |
|
|
$form_state['rebuild'] = TRUE;
|
572 |
|
|
return;
|
573 |
|
|
}
|
574 |
|
|
|
575 |
|
|
$aid = $form_state['values']['flag']['aid'];
|
576 |
|
|
$flag = $form_state['values']['flag']['flag'];
|
577 |
|
|
$event = $form_state['values']['flag']['event'];
|
578 |
|
|
$threshold = $form_state['values']['flag']['threshold'];
|
579 |
|
|
$repeat_threshold = $form_state['values']['flag']['repeat_threshold'];
|
580 |
|
|
$callback = $form_state['values']['flag']['callback'];
|
581 |
|
|
|
582 |
|
|
// Specialized forms may need to execute their own submit handlers on save.
|
583 |
|
|
$submit_function = $callback . '_submit';
|
584 |
|
|
$parameters = function_exists($submit_function) ? $submit_function($form, $form_state) : array();
|
585 |
|
|
|
586 |
|
|
if (empty($aid)) {
|
587 |
|
|
$aid = flag_actions_insert_action($flag->fid, $event, $threshold, $repeat_threshold, $callback, $parameters);
|
588 |
|
|
$form_state['values']['flag']['aid'] = $aid;
|
589 |
|
|
$form_state['values']['flag']['is_new'] = TRUE;
|
590 |
|
|
}
|
591 |
|
|
else {
|
592 |
|
|
flag_actions_update_action($aid, $event, $threshold, $repeat_threshold, $parameters);
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
$action = flag_actions_get_action($aid);
|
596 |
|
|
|
597 |
|
|
drupal_set_message(t('The "@action" action for the @title flag has been saved.', array('@action' => $action->label, '@title' => $flag->get_title())));
|
598 |
|
|
$form_state['redirect'] = FLAG_ADMIN_PATH . '/actions';
|
599 |
|
|
}
|
600 |
|
|
|
601 |
|
|
function theme_flag_actions_flag_form($variables) {
|
602 |
|
|
$form = $variables['form'];
|
603 |
|
|
|
604 |
|
|
$event = drupal_render($form['event']);
|
605 |
|
|
$threshold = drupal_render($form['threshold']);
|
606 |
|
|
$repeat_threshold = drupal_render($form['repeat_threshold']);
|
607 |
|
|
$action = $form['#action']->label;
|
608 |
|
|
|
609 |
|
|
$output = '';
|
610 |
|
|
$output .= '<div class="container-inline">';
|
611 |
|
|
$output .= t('Perform action when content !event !threshold flags', array('!event' => $event, '!threshold' => $threshold));
|
612 |
|
|
if ($form['#flag']->global) {
|
613 |
|
|
$output .= ' ' . t('(global flags always have a threshold of 1)');
|
614 |
|
|
}
|
615 |
|
|
$output .= '</div>';
|
616 |
|
|
$output .= '<div class="container-inline">';
|
617 |
|
|
if (!$form['#flag']->global) {
|
618 |
|
|
$output .= t('Repeat this action every !repeat_threshold additional flags after the threshold is reached', array('!repeat_threshold' => $repeat_threshold));
|
619 |
|
|
}
|
620 |
|
|
$output .= '</div>';
|
621 |
|
|
|
622 |
|
|
$element = array(
|
623 |
|
|
'#title' => t('Flagging threshold'),
|
624 |
|
|
'#required' => TRUE,
|
625 |
|
|
);
|
626 |
|
|
|
627 |
|
|
return $output . drupal_render_children($form);
|
628 |
|
|
}
|
629 |
|
|
|
630 |
|
|
function flag_actions_delete_form($form, &$form_state, $aid) {
|
631 |
|
|
$action = flag_actions_get_action($aid);
|
632 |
|
|
$flag = flag_get_flag($action->flag);
|
633 |
|
|
|
634 |
|
|
$form['action'] = array(
|
635 |
|
|
'#type' => 'value',
|
636 |
|
|
'#value' => $action,
|
637 |
|
|
);
|
638 |
|
|
|
639 |
|
|
$form['flag'] = array(
|
640 |
|
|
'#type' => 'value',
|
641 |
|
|
'#value' => $flag,
|
642 |
|
|
);
|
643 |
|
|
|
644 |
|
|
$question = t('Delete the "@action" action for the @title flag?', array('@action' => $action->label, '@title' => $flag->get_title()));
|
645 |
|
|
$path = FLAG_ADMIN_PATH . '/actions';
|
646 |
|
|
|
647 |
|
|
return confirm_form($form, $question, $path, NULL, t('Delete'));
|
648 |
|
|
}
|
649 |
|
|
|
650 |
|
|
function flag_actions_delete_form_submit(&$form, &$form_state) {
|
651 |
|
|
flag_actions_delete_action($form_state['values']['action']->aid);
|
652 |
|
|
drupal_set_message(t('The "@action" action for the @title flag has been deleted.', array('@action' => $form_state['values']['action']->label, '@title' => $form_state['values']['flag']->get_title())));
|
653 |
|
|
$form_state['redirect'] = FLAG_ADMIN_PATH . '/actions';
|
654 |
|
|
}
|
655 |
|
|
|
656 |
|
|
/**
|
657 |
|
|
* Make modifications to the "Send e-mail" action form.
|
658 |
|
|
*/
|
659 |
|
|
function flag_actions_system_send_email_action_form(&$form, &$flag, $context) {
|
660 |
|
|
if (!isset($context['recipient'])) {
|
661 |
|
|
$form['recipient']['#default_value'] = '[site:mail]';
|
662 |
|
|
}
|
663 |
|
|
|
664 |
|
|
if (!isset($context['subject'])) {
|
665 |
|
|
$form['subject']['#default_value'] = t('Content Flagged @flag_title', array('@flag_title' => $flag->get_title()));
|
666 |
|
|
}
|
667 |
|
|
|
668 |
|
|
if (!isset($context['message'])) {
|
669 |
|
|
$form['message']['#default_value'] = t("The @flag_entity_type [flag-action:content-title] has been flagged [flag-action:count] times with the @flag_title flag.\n\nView this @flag_entity_type at [flag-action:content-url].", array('@flag_entity_type' => $flag->entity_type, '@flag_title' => $flag->get_title()));
|
670 |
|
|
}
|
671 |
|
|
|
672 |
|
|
$form['help'] = array(
|
673 |
|
|
'#type' => 'fieldset',
|
674 |
|
|
'#title' => t('Tokens'),
|
675 |
|
|
'#description' => t('The following tokens can be used in the recipient, subject, or message.'),
|
676 |
|
|
'#collapsible' => TRUE,
|
677 |
|
|
'#collapsed' => TRUE,
|
678 |
|
|
);
|
679 |
|
|
$form['help']['basic'] = array(
|
680 |
|
|
'#markup' => theme('flag_tokens_browser', array('types' => array('flag', 'flag-action'))),
|
681 |
|
|
);
|
682 |
|
|
|
683 |
|
|
$form['help']['tokens'] = array(
|
684 |
|
|
'#type' => 'fieldset',
|
685 |
|
|
'#title' => t('More tokens'),
|
686 |
|
|
'#description' => t("Depending on the type of the content being flagged, the following tokens can be used in the recipients, subject, or message. For example, if the content being flagged is a node, you can use any of the node tokens --but you can't use the comment tokens: they won't be recognized. Similarly, if the content being flagged is a user, you can use only the user tokens."),
|
687 |
|
|
'#value' => theme('flag_tokens_browser', array('types' => $flag->get_labels_token_types(), 'global_types' => FALSE)),
|
688 |
|
|
'#collapsible' => TRUE,
|
689 |
|
|
'#collapsed' => TRUE,
|
690 |
|
|
);
|
691 |
|
|
} |