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