Projet

Général

Profil

Paste
Télécharger (5,65 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / includes / flag.pages.inc @ 76e2e7c3

1
<?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 = strip_tags($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
    // Take the same approach as Core entity forms: shove all the entity
106
    // properties into the form as values so that entity_form_field_validate()
107
    // can build a pseudoentity from $form_values in the validate handler.
108
    foreach (array(
109
      'flag_name',
110
      'entity_type',
111
      'entity_id',
112
      'uid',
113
    ) as $key) {
114
      $form[$key] = array(
115
        '#type' => 'value',
116
        '#value' => isset($flagging->$key) ? $flagging->$key : NULL,
117
      );
118
    }
119
  }
120

    
121
  return confirm_form($form, $question, $path, '', $yes);
122
}
123

    
124
/**
125
 * Validate handler for the flag confirm form.
126
 *
127
 * Validate any Field API fields on the Flagging.
128
 *
129
 * @see flag_confirm()
130
 */
131
function flag_confirm_validate($form, &$form_state) {
132
  // Only validate the entity fields when we're saving an entity.
133
  $action = $form_state['values']['action'];
134
  if ($action == 'flag') {
135
    entity_form_field_validate('flagging', $form, $form_state);
136
  }
137
}
138

    
139
/**
140
 * Submit handler for the flag confirm form.
141
 *
142
 * Note that validating whether the user may perform the action is done here,
143
 * rather than in a form validation handler.
144
 *
145
 * @see flag_confirm()
146
 */
147
function flag_confirm_submit(&$form, &$form_state) {
148
  $flag = $form['#flag'];
149
  $action = $form_state['values']['action'];
150
  $entity_id = $form_state['values']['entity_id'];
151

    
152
  if ($action == 'flag') {
153
    // If the action 'flag', further build up the new entity from form values.
154
    $flagging = $form['#flagging'];
155
    entity_form_submit_build_entity('flagging', $flagging, $form, $form_state);
156

    
157
    $result = $flag->flag($action, $entity_id, NULL, FALSE, $flagging);
158
  }
159
  else {
160
    $result = $flag->flag($action, $entity_id, NULL, FALSE);
161
  }
162

    
163
  if (!$result) {
164
    if ($errors = $flag->get_errors()) {
165
      foreach ($errors as $error) {
166
        drupal_set_message($error, 'error');
167
      }
168
    }
169
  }
170
  else {
171
    drupal_set_message($flag->get_label($action . '_message', $entity_id));
172
  }
173
}
174

    
175
/**
176
 * Builds the JavaScript structure describing the flagging operation.
177
 */
178
function flag_build_javascript_info($flag, $entity_id) {
179
  $errors = $flag->get_errors();
180
  $info = array(
181
    'status' => TRUE,
182
    'newLink' => $flag->theme($flag->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id, array(
183
      'after_flagging' => TRUE,
184
      'errors' => $errors,
185
    )),
186
    // Further information for the benefit of custom JavaScript event handlers:
187
    'flagSuccess' => !$errors,
188
    'contentId' => $entity_id,
189
    'entityType' => $flag->entity_type,
190
    'flagName' => $flag->name,
191
    'flagStatus' => $flag->is_flagged($entity_id) ? 'flagged' : 'unflagged',
192
  );
193
  drupal_alter('flag_javascript_info', $info, $flag);
194
  return $info;
195
}