1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Menu callbacks for the Flag module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Menu callback for (un)flagging a node.
|
10 |
|
|
*
|
11 |
|
|
* Used both for the regular callback as well as the JS version.
|
12 |
|
|
*
|
13 |
|
|
* @param $action
|
14 |
|
|
* Either 'flag' or 'unflag'.
|
15 |
|
|
*/
|
16 |
|
|
function flag_page($action, $flag, $entity_id) {
|
17 |
|
|
global $user;
|
18 |
|
|
|
19 |
|
|
// Shorten up the variables that affect the behavior of this page.
|
20 |
|
|
$js = isset($_REQUEST['js']);
|
21 |
|
|
$token = $_REQUEST['token'];
|
22 |
|
|
|
23 |
|
|
// Specifically $_GET to avoid getting the $_COOKIE variable by the same key.
|
24 |
|
|
$has_js = isset($_GET['has_js']);
|
25 |
|
|
|
26 |
|
|
// Check the flag token, and then javascript status.
|
27 |
|
|
if (!flag_check_token($token, $entity_id)) {
|
28 |
|
|
$flag->errors['token'] = t('Bad token. You seem to have followed an invalid link.');
|
29 |
|
|
}
|
30 |
|
|
elseif ($user->uid == 0 && !$has_js) {
|
31 |
|
|
$flag->errors['javascript'] = t('You must have JavaScript and cookies enabled in your browser to flag content.');
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
// If no errors have been detected thus far, perform the flagging.
|
35 |
|
|
// Further errors may still be detected during validation and prevent
|
36 |
|
|
// the operation from succeeding.
|
37 |
|
|
if (!$flag->errors) {
|
38 |
|
|
$flag->flag($action, $entity_id);
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
// If successful, return data according to the request type.
|
42 |
|
|
if ($js) {
|
43 |
|
|
drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
|
44 |
|
|
$flag->link_type = 'toggle';
|
45 |
|
|
// Any errors that have been set will be output below
|
46 |
|
|
// the flag link with javascript.
|
47 |
|
|
print drupal_json_encode(flag_build_javascript_info($flag, $entity_id));
|
48 |
|
|
drupal_exit();
|
49 |
|
|
}
|
50 |
|
|
else {
|
51 |
|
|
$errors = $flag->get_errors();
|
52 |
|
|
if ($errors) {
|
53 |
|
|
// If an error was received, set a message and exit.
|
54 |
|
|
foreach ($errors as $error) {
|
55 |
|
|
drupal_set_message($error, 'error');
|
56 |
|
|
}
|
57 |
|
|
if (isset($errors['access-denied'])) {
|
58 |
|
|
return MENU_ACCESS_DENIED;
|
59 |
|
|
}
|
60 |
|
|
else {
|
61 |
|
|
drupal_goto();
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
else {
|
65 |
|
|
drupal_set_message($flag->get_label($action . '_message', $entity_id));
|
66 |
|
|
drupal_goto();
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Form for confirming the (un)flagging of an entity.
|
73 |
|
|
*
|
74 |
|
|
* @param $action
|
75 |
|
|
* Either 'flag' or 'unflag'.
|
76 |
|
|
* @param $flag
|
77 |
|
|
* A loaded flag object.
|
78 |
|
|
* @param $entity_id
|
79 |
|
|
* The id of the entity to operate on. The type is implicit in the flag.
|
80 |
|
|
*
|
81 |
|
|
* @see flag_confirm_submit()
|
82 |
|
|
*/
|
83 |
|
|
function flag_confirm($form, &$form_state, $action, $flag, $entity_id) {
|
84 |
|
|
$form['#flag'] = $flag;
|
85 |
|
|
$form['action'] = array(
|
86 |
|
|
'#type' => 'value',
|
87 |
|
|
'#value' => $action,
|
88 |
|
|
);
|
89 |
|
|
$form['entity_id'] = array(
|
90 |
|
|
'#type' => 'value',
|
91 |
|
|
'#value' => $entity_id,
|
92 |
|
|
);
|
93 |
|
|
|
94 |
|
|
$question = $flag->get_label($action . '_confirmation', $entity_id);
|
95 |
|
|
$path = isset($_GET['destination']) ? $_GET['destination'] : '<front>';
|
96 |
|
|
$yes = $flag->get_label($action . '_short', $entity_id);
|
97 |
|
|
|
98 |
|
|
if ($action == 'flag') {
|
99 |
|
|
// If the action 'flag', we're potentially about to create a new
|
100 |
|
|
// flagging entity. We need an empty new entity to pass to FieldAPI.
|
101 |
|
|
$flagging = $flag->new_flagging($entity_id);
|
102 |
|
|
field_attach_form('flagging', $flagging, $form, $form_state);
|
103 |
|
|
$form['#flagging'] = $flagging;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
return confirm_form($form, $question, $path, '', $yes);
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Submit handler for the flag confirm form.
|
111 |
|
|
*
|
112 |
|
|
* Note that validating whether the user may perform the action is done here,
|
113 |
|
|
* rather than in a form validation handler.
|
114 |
|
|
*
|
115 |
|
|
* @see flag_confirm()
|
116 |
|
|
*/
|
117 |
|
|
function flag_confirm_submit(&$form, &$form_state) {
|
118 |
|
|
$flag = $form['#flag'];
|
119 |
|
|
$action = $form_state['values']['action'];
|
120 |
|
|
$entity_id = $form_state['values']['entity_id'];
|
121 |
|
|
|
122 |
|
|
if ($action == 'flag') {
|
123 |
|
|
// If the action 'flag', further build up the new entity from form values.
|
124 |
|
|
$flagging = $form['#flagging'];
|
125 |
|
|
entity_form_submit_build_entity('flagging', $flagging, $form, $form_state);
|
126 |
|
|
|
127 |
|
|
$result = $flag->flag($action, $entity_id, NULL, FALSE, $flagging);
|
128 |
|
|
}
|
129 |
|
|
else {
|
130 |
|
|
$result = $flag->flag($action, $entity_id, NULL, FALSE);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
if (!$result) {
|
134 |
|
|
if ($errors = $flag->get_errors()) {
|
135 |
|
|
foreach ($errors as $error) {
|
136 |
|
|
drupal_set_message($error, 'error');
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
else {
|
141 |
|
|
drupal_set_message($flag->get_label($action . '_message', $entity_id));
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/**
|
146 |
|
|
* Builds the JavaScript structure describing the flagging operation.
|
147 |
|
|
*/
|
148 |
|
|
function flag_build_javascript_info($flag, $entity_id) {
|
149 |
|
|
$errors = $flag->get_errors();
|
150 |
|
|
$info = array(
|
151 |
|
|
'status' => TRUE,
|
152 |
|
|
'newLink' => $flag->theme($flag->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id, array(
|
153 |
|
|
'after_flagging' => TRUE,
|
154 |
|
|
'errors' => $errors,
|
155 |
|
|
)),
|
156 |
|
|
// Further information for the benefit of custom JavaScript event handlers:
|
157 |
|
|
'flagSuccess' => !$errors,
|
158 |
|
|
'contentId' => $entity_id,
|
159 |
|
|
'entityType' => $flag->entity_type,
|
160 |
|
|
'flagName' => $flag->name,
|
161 |
|
|
'flagStatus' => $flag->is_flagged($entity_id) ? 'flagged' : 'unflagged',
|
162 |
|
|
);
|
163 |
018e218c
|
Assos Assos
|
drupal_alter('flag_javascript_info', $info, $flag);
|
164 |
85ad3d82
|
Assos Assos
|
return $info;
|
165 |
|
|
} |