Projet

Général

Profil

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

root / drupal7 / sites / all / modules / flag / includes / flag.pages.inc @ 4cfd8be6

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
 *   The action about to be performed. One of 'flag' or 'unflag'.
15
 * @param $flag
16
 *   The flag object.
17
 * @param $entity_id
18
 *   The ID of the entity to be acted upon. The type is implicit in the flag.
19
 */
20
function flag_page($action, $flag, $entity_id) {
21
  global $user;
22

    
23
  // Shorten up the variables that affect the behavior of this page.
24
  $js = isset($_REQUEST['js']);
25
  $token = $_REQUEST['token'];
26

    
27
  // Specifically $_GET to avoid getting the $_COOKIE variable by the same key.
28
  $has_js = isset($_GET['has_js']);
29

    
30
  // Check the flag token, and then javascript status.
31
  if (!flag_check_token($token, $entity_id)) {
32
    $flag->errors['token'] = t('Bad token. You seem to have followed an invalid link.');
33
  }
34
  elseif ($user->uid == 0 && !$has_js) {
35
    $flag->errors['javascript'] = t('You must have JavaScript and cookies enabled in your browser to flag content.');
36
  }
37

    
38
  // If no errors have been detected thus far, perform the flagging.
39
  // Further errors may still be detected during validation and prevent
40
  // the operation from succeeding.
41
  if (!$flag->errors) {
42
    $flag->flag($action, $entity_id);
43
  }
44

    
45
  // If successful, return data according to the request type.
46
  if ($js) {
47
    drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
48
    $flag->link_type = 'toggle';
49
    // Any errors that have been set will be output below
50
    // the flag link with javascript.
51
    print drupal_json_encode(flag_build_javascript_info($flag, $entity_id));
52
    drupal_exit();
53
  }
54
  else {
55
    $errors = $flag->get_errors();
56
    if ($errors) {
57
      // If an error was received, set a message and exit.
58
      foreach ($errors as $error) {
59
        drupal_set_message($error, 'error');
60
      }
61
      if (isset($errors['access-denied'])) {
62
        return MENU_ACCESS_DENIED;
63
      }
64
      else {
65
        drupal_goto();
66
      }
67
    }
68
    else {
69
      drupal_set_message($flag->get_label($action . '_message', $entity_id));
70
      drupal_goto();
71
    }
72
  }
73
}
74

    
75
/**
76
 * Form for confirming the (un)flagging of an entity.
77
 *
78
 * @param $action
79
 *   Either 'flag' or 'unflag'.
80
 * @param $flag
81
 *   A loaded flag object.
82
 * @param $entity_id
83
 *   The id of the entity to operate on. The type is implicit in the flag.
84
 *
85
 * @see flag_confirm_submit()
86
 */
87
function flag_confirm($form, &$form_state, $action, $flag, $entity_id) {
88
  $form['#flag'] = $flag;
89
  $form['action'] = array(
90
    '#type' => 'value',
91
    '#value' => $action,
92
  );
93
  $form['entity_id'] = array(
94
    '#type' => 'value',
95
    '#value' => $entity_id,
96
  );
97

    
98
  $question = $flag->get_label($action . '_confirmation', $entity_id);
99
  $path = isset($_GET['destination']) ? $_GET['destination'] : '<front>';
100
  $yes = strip_tags($flag->get_label($action . '_short', $entity_id));
101

    
102
  if ($action == 'flag') {
103
    // If the action 'flag', we're potentially about to create a new
104
    // flagging entity. We need an empty new entity to pass to FieldAPI.
105
    $flagging = $flag->new_flagging($entity_id);
106
    field_attach_form('flagging', $flagging, $form, $form_state);
107
    $form['#flagging'] = $flagging;
108

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

    
125
  return confirm_form($form, $question, $path, '', $yes);
126
}
127

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

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

    
156
  if ($action == 'flag') {
157
    // If the action 'flag', further build up the new entity from form values.
158
    $flagging = $form['#flagging'];
159
    entity_form_submit_build_entity('flagging', $flagging, $form, $form_state);
160

    
161
    $result = $flag->flag($action, $entity_id, NULL, FALSE, $flagging);
162
  }
163
  else {
164
    $result = $flag->flag($action, $entity_id, NULL, FALSE);
165
  }
166

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

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