Projet

Général

Profil

Paste
Télécharger (12,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / webform.tokens.inc @ 8d02775b

1
<?php
2

    
3
/**
4
 * @file
5
 * Builds placeholder replacement tokens for webform-related data.
6
 */
7

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function webform_token_info() {
12
  // Webform submission tokens.
13
  $info['types']['submission'] = array(
14
    'name' => t('Submission'),
15
    'description' => t('Tokens related to webform submissions.'),
16
    'needs-data' => 'webform-submission',
17
  );
18

    
19
  $info['tokens']['submission']['serial'] = array(
20
    'name' => t('Serial number'),
21
    'description' => t('The serial number of this webform submission.'),
22
  );
23
  $info['tokens']['submission']['sid'] = array(
24
    'name' => t('Submission ID'),
25
    'description' => t('The unique indentifier for the webform submission.'),
26
  );
27
  $info['tokens']['submission']['access-token'] = array(
28
    'name' => t('Access token'),
29
    'description' => t('The security token used to gain access to this webform submission.'),
30
  );
31
  $info['tokens']['submission']['date'] = array(
32
    'name' => t('Date submitted'),
33
    'description' => t('The date the webform was first save as draft or completed.'),
34
    'type' => 'date',
35
  );
36
  $info['tokens']['submission']['completed_date'] = array(
37
    'name' => t('Date completed'),
38
    'description' => t('The date the webform was first completed (not draft).'),
39
    'type' => 'date',
40
  );
41
  $info['tokens']['submission']['modified_date'] = array(
42
    'name' => t('Date modified'),
43
    'description' => t('The date the webform was last saved (draft or completed).'),
44
    'type' => 'date',
45
  );
46
  $info['tokens']['submission']['ip-address'] = array(
47
    'name' => t('IP address'),
48
    'description' => t('The IP address that was used when submitting the webform.'),
49
  );
50
  $info['tokens']['submission']['user'] = array(
51
    'name' => t('Submitter'),
52
    'description' => t('The user that submitted the webform result.'),
53
    'type' => 'user',
54
  );
55
  $info['tokens']['submission']['url'] = array(
56
    'name' => t('URL'),
57
    'description' => t("Webform tokens related to the submission's URL."),
58
    'type' => 'url',
59
  );
60
  $info['tokens']['submission']['edit-url'] = array(
61
    'name' => t('Edit URL'),
62
    'description' => t("Webform tokens related to the submission's Edit URL."),
63
    'type' => 'url',
64
  );
65
  $info['tokens']['submission']['values'] = array(
66
    'name' => t('Webform submission values'),
67
    'description' => '<div>' . t('Webform tokens from submitted data. Replace the "?" with the "form key", including any parent form keys separated by colons. You can append:') . '</div><ul>' .
68
    '<li>' . t('the question key for just that one question (grid components).') . '</li>' .
69
    '<li>' . t('the option key for just that one option (grid and select components).') . '</li>' .
70
    '<li>' . t('<code>@token</code> for the value without the label (the default).', array('@token' => ':nolabel')) . '</li>' .
71
    '<li>' . t('<code>@token</code> for just the label.', array('@token' => ':label')) . '</li>' .
72
    '<li>' . t('<code>@token</code> for both the label and value together.', array('@token' => ':withlabel')) . '</li>' .
73
    '<li>' . t('<code>@token</code> for just the key in a key|label pair (grid and select components).', array('@token' => ':key')) . '</li></ul>',
74
    'dynamic' => TRUE,
75
  );
76

    
77
  return $info;
78
}
79

    
80
/**
81
 * Implements hook_tokens().
82
 */
83
function webform_tokens($type, $tokens, array $data = array(), array $options = array()) {
84
  static $recursion_level = 0;
85

    
86
  // Return early unless submission tokens are needed and there is a submission.
87
  if ($type != 'submission' || empty($data['webform-submission']) || !webform_variable_get('webform_token_access')) {
88
    return array();
89
  }
90

    
91
  // Generate Webform tokens.
92
  $replacements = array();
93

    
94
  // Prepare all the data that we will likely need more than once.
95
  $submission = $data['webform-submission'];
96
  $node = isset($data['node']) ? $data['node'] : node_load($submission->nid);
97
  $email = isset($data['webform-email']) ? $data['webform-email'] : NULL;
98
  $sanitize = !empty($options['sanitize']);
99
  $format = $sanitize ? 'html' : 'text';
100
  $url_options = isset($options['language']) ? $options['language'] : array('absolute' => TRUE);
101
  $language_code = isset($options['language']) ? $options['language']->language : NULL;
102
  $markup_components = array();
103

    
104
  // Markup components may use tokens when displayed. Displaying the tokens
105
  // requires rendering the components. Rendering a markup component can then
106
  // cause infinite recursion. To prevent this, markup components are omitted
107
  // from the rendered submission if recursion has been detected.
108
  if ($recursion_level) {
109
    $markup_components = array_keys(array_filter($node->webform['components'],
110
      function ($component) {
111
        return $component['type'] == 'markup';
112
      }));
113
  }
114

    
115
  $recursion_level++;
116

    
117
  // Replace individual tokens that have an exact replacement.
118
  foreach ($tokens as $name => $original) {
119
    switch ($name) {
120
      case 'serial':
121
        $replacements[$original] = $submission->serial ? $submission->serial : '';
122
        break;
123

    
124
      case 'sid':
125
        $replacements[$original] = $submission->sid ? $submission->sid : '';
126
        break;
127

    
128
      case 'access-token':
129
        $replacements[$original] = webform_get_submission_access_token($submission);
130
        break;
131

    
132
      case 'date':
133
        $replacements[$original] = format_date($submission->submitted, webform_variable_get('webform_date_type'), '', NULL, $language_code);
134
        break;
135

    
136
      case 'completed_date':
137
        if ($submission->completed) {
138
          $replacements[$original] = format_date($submission->completed, webform_variable_get('webform_date_type'), '', NULL, $language_code);
139
        }
140
        break;
141

    
142
      case 'modified_date':
143
        $replacements[$original] = format_date($submission->modified, webform_variable_get('webform_date_type'), '', NULL, $language_code);
144
        break;
145

    
146
      case 'ip-address':
147
        $replacements[$original] = $sanitize ? check_plain($submission->remote_addr) : $submission->remote_addr;
148
        break;
149

    
150
      case 'user':
151
        $account = user_load($submission->uid);
152
        $name = format_username($account);
153
        $replacements[$original] = $sanitize ? check_plain($name) : $name;
154
        break;
155

    
156
      case 'url':
157
        $replacements[$original] = $submission->sid ? url("node/{$node->nid}/submission/{$submission->sid}", $url_options) : '';
158
        break;
159

    
160
      case 'edit-url':
161
        $replacements[$original] = $submission->sid ? url("node/{$node->nid}/submission/{$submission->sid}/edit", $url_options) : '';
162
        break;
163

    
164
      case 'values':
165
        $excluded_components = isset($email['excluded_components']) ? $email['excluded_components'] : array();
166
        $excluded_components = array_merge($excluded_components, $markup_components);
167
        $submission_renderable = webform_submission_render($node, $submission, $email, $format, $excluded_components);
168
        $replacements[$original] = drupal_render($submission_renderable);
169
        break;
170
    }
171
  }
172

    
173
  // Webform submission tokens for individual components.
174
  if ($value_tokens = token_find_with_prefix($tokens, 'values')) {
175
    // Get the full submission renderable without $excluded_components so that
176
    // individually referenced values are available.
177
    $submission_renderable = webform_submission_render($node, $submission, $email, $format, $markup_components);
178
    $available_modifiers = array(
179
      'label',
180
      'withlabel',
181
      'nolabel',
182
      'key',
183
    );
184

    
185
    foreach ($node->webform['components'] as $cid => $component) {
186
      // Build the list of parents for this component.
187
      $parents = ($component['pid'] == 0) ? array($component['form_key']) : webform_component_parent_keys($node, $component);
188
      $parent_token = implode(':', $parents);
189
      foreach ($value_tokens as $name => $original) {
190
        if (strpos($name, $parent_token) !== 0) {
191
          // Token not found as a prefix or exact match for this component.
192
          // Token loop continue.
193
          continue;
194
        }
195
        // Drill down into the renderable to find the element.
196
        $display_element = $submission_renderable;
197
        foreach ($parents as $parent) {
198
          if (!isset($display_element[$parent])) {
199
            // Sometimes an element won't exist in the submission renderable
200
            // due to conditional logic. If not found, skip that element.
201
            // Token loop continue.
202
            continue 2;
203
          }
204
          $display_element = $display_element[$parent];
205
        }
206
        // Individual tokens always have access granted even if they're
207
        // not displayed when printing the whole renderable.
208
        $display_element['#access'] = TRUE;
209

    
210
        // For grid components, see if optional question key is present.
211
        $matched_token = $parent_token;
212
        if ($display_element['#webform_component']['type'] === 'grid') {
213
          list($question_key) = explode(':', substr($name, strlen($matched_token) + 1));
214
          if (strlen($question_key) && isset($display_element[$question_key]['#value'])) {
215
            // Generate a faux select component for this grid question.
216
            $select_component = _webform_defaults_select();
217
            $select_component['type'] = 'select';
218
            $select_component['nid'] = $display_element['#webform_component']['nid'];
219
            $select_component['name'] = $display_element['#grid_questions'][$question_key];
220
            $select_component['extra']['items'] = $display_element['#webform_component']['extra']['options'];
221
            $display_element = _webform_display_select($select_component, $display_element[$question_key]['#value'], $format);
222
            $display_element['#webform_component'] = $select_component;
223
            $matched_token .= ':' . $question_key;
224
          }
225
        }
226

    
227
        // For select components, see if the optional option key is present.
228
        if ($display_element['#webform_component']['type'] === 'select') {
229
          list($option_key) = explode(':', substr($name, strlen($matched_token) + 1));
230
          if (strlen($option_key) && strpos("\n" . $display_element['#webform_component']['extra']['items'], "\n" . $option_key . '|') !== FALSE) {
231
            // Return only this specified option and no other values.
232
            $display_element['#value'] = array_intersect($display_element['#value'], array($option_key));
233
            $matched_token .= ':' . $option_key;
234
          }
235
        }
236

    
237
        // Assume no modifier (implied 'nolabel').
238
        $modifier = NULL;
239
        if (strcmp($name, $matched_token) !== 0) {
240
          // Check if this matches the key plus a modifier.
241
          $modifier = substr($name, strrpos($name, ':') + 1);
242
          // @todo: Allow components to provide additional modifiers per
243
          // type, i.e. key, day, hour, minute, etc.
244
          if (strcmp($name, $matched_token . ':' . $modifier) !== 0 || !in_array($modifier, $available_modifiers)) {
245
            // No match.
246
            // Token loop continue.
247
            continue;
248
          }
249
        }
250

    
251
        if ($modifier === 'label') {
252
          $replacements[$original] = webform_filter_xss($display_element['#title']);
253
        }
254
        elseif ($modifier === 'key' && $display_element['#webform_component']['type'] === 'select') {
255
          $values = array();
256
          foreach ($display_element['#value'] as $value) {
257
            $values[] = webform_filter_xss($value);
258
          }
259
          $replacements[$original] = implode(' ', $values);
260
        }
261
        else {
262
          // Remove theme wrappers for the nolabel modifier.
263
          if ($modifier === 'nolabel' || empty($modifier)) {
264
            $display_element['#theme_wrappers'] = array();
265
          }
266
          $replacements[$original] = render($display_element);
267
        }
268
        // Continue processing tokens in case another modifier is used.
269
      }
270
    }
271

    
272
  }
273

    
274
  // Chained token relationships.
275
  if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
276
    $replacements += token_generate('date', $date_tokens, array('date' => $submission->submitted), $options);
277
  }
278
  if ($submission->completed && ($date_tokens = token_find_with_prefix($tokens, 'completed_date'))) {
279
    $replacements += token_generate('date', $date_tokens, array('date' => $submission->completed), $options);
280
  }
281
  if ($date_tokens = token_find_with_prefix($tokens, 'modified_date')) {
282
    $replacements += token_generate('date', $date_tokens, array('date' => $submission->modified), $options);
283
  }
284
  if (($user_tokens = token_find_with_prefix($tokens, 'user')) && $account = user_load($submission->uid)) {
285
    $replacements += token_generate('user', $user_tokens, array('user' => $account), $options);
286
  }
287
  if ($submission->sid) {
288
    if ($url_tokens = token_find_with_prefix($tokens, 'url')) {
289
      $replacements += token_generate('url', $url_tokens, array('path' => "node/{$node->nid}/submission/{$submission->sid}"), $options);
290
    }
291
    if ($url_tokens = token_find_with_prefix($tokens, 'edit-url')) {
292
      $replacements += token_generate('url', $url_tokens, array('path' => "node/{$node->nid}/submission/{$submission->sid}/edit"), $options);
293
    }
294
  }
295

    
296
  $recursion_level--;
297

    
298
  return $replacements;
299
}