Projet

Général

Profil

Paste
Télécharger (13 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / webform.tokens.inc @ 7547bb19

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>' .
68
                     '<ul>' .
69
                     '<li>' . t('the question key for just that one question (grid components).') . '</li>' .
70
                     '<li>' . t('the option key for just that one option (grid and select components).') . '</li>' .
71
                     '<li>' . t('<code>@token</code> for the value without the label (the default).', array('@token' => ':nolabel')) . '</li>' .
72
                     '<li>' . t('<code>@token</code> for just the label.', array('@token' => ':label')) . '</li>' .
73
                     '<li>' . t('<code>@token</code> for both the label and value together.', array('@token' => ':withlabel')) . '</li>' .
74
                     '<li>' . t('<code>@token</code> for just the key in a key|label pair (grid and select components).', array('@token' => ':key')) . '</li>' .
75
                     '</ul>',
76
    'dynamic' => TRUE,
77
  );
78

    
79
  return $info;
80
}
81

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

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

    
93
  // Generate Webform tokens.
94
  $replacements = array();
95

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

    
106
  // Markup components may use tokens when displayed. Displaying the tokens
107
  // requires rendering the components. Rendering a markup component can then
108
  // cause infinite recursion. To prevent this, markup components are omitted
109
  // from the rendered submission if recursion has been detected.
110
  if ($recursion_level) {
111
    $markup_components = array_keys(array_filter($node->webform['components'],
112
                                                 function ($component) { return $component['type'] == 'markup'; }));
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
      case 'sid':
124
        $replacements[$original] = $submission->sid ? $submission->sid : '';
125
        break;
126
      case 'access-token':
127
        $replacements[$original] = webform_get_submission_access_token($submission);
128
        break;
129
      case 'date':
130
        $replacements[$original] = format_date($submission->submitted, webform_variable_get('webform_date_type'), '', NULL, $language_code);
131
        break;
132
      case 'completed_date':
133
        if ($submission->completed) {
134
          $replacements[$original] = format_date($submission->completed, webform_variable_get('webform_date_type'), '', NULL, $language_code);
135
        }
136
        break;
137
      case 'modified_date':
138
        $replacements[$original] = format_date($submission->modified, webform_variable_get('webform_date_type'), '', NULL, $language_code);
139
        break;
140
      case 'ip-address':
141
        $replacements[$original] = $sanitize ? check_plain($submission->remote_addr) : $submission->remote_addr;
142
        break;
143
      case 'user':
144
        $account = user_load($submission->uid);
145
        $name = format_username($account);
146
        $replacements[$original] = $sanitize ? check_plain($name) : $name;
147
        break;
148
      case 'url':
149
        $replacements[$original] = $submission->sid ? url("node/{$node->nid}/submission/{$submission->sid}", $url_options) : '';
150
        break;
151
      case 'edit-url':
152
        $replacements[$original] = $submission->sid ? url("node/{$node->nid}/submission/{$submission->sid}/edit", $url_options) : '';
153
        break;
154
      case 'values':
155
        $excluded_components = isset($email['excluded_components']) ? $email['excluded_components'] : array();
156
        $excluded_components = array_merge($excluded_components, $markup_components);
157
        $submission_renderable = webform_submission_render($node, $submission, $email, $format, $excluded_components);
158
        $replacements[$original] = drupal_render($submission_renderable);
159
        break;
160
    }
161
  }
162

    
163
  // Webform submission tokens for individual components.
164
  if ($value_tokens = token_find_with_prefix($tokens, 'values')) {
165
    // Get the full submission renderable without $excluded_components so that
166
    // individually referenced values are available.
167
    $submission_renderable = webform_submission_render($node, $submission, $email, $format, $markup_components);
168
    $available_modifiers = array(
169
      'label',
170
      'withlabel',
171
      'nolabel',
172
      'key',
173
    );
174

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

    
198
        // For grid components, see if optional question key is present.
199
        $matched_token = $parent_token;
200
        if ($display_element['#webform_component']['type'] === 'grid') {
201
          list($question_key) = explode(':', substr($name, strlen($matched_token) + 1));
202
          if (strlen($question_key) && isset($display_element[$question_key]['#value'])) {
203
            // Generate a faux select component for this grid question.
204
            $select_component = _webform_defaults_select();
205
            $select_component['type'] = 'select';
206
            $select_component['nid'] = $display_element['#webform_component']['nid'];
207
            $select_component['name'] = $display_element['#grid_questions'][$question_key];
208
            $select_component['extra']['items'] = $display_element['#webform_component']['extra']['options'];
209
            $display_element = _webform_display_select($select_component, $display_element[$question_key]['#value'], $format);
210
            $display_element['#webform_component'] = $select_component;
211
            $matched_token .= ':' . $question_key;
212
          }
213
        }
214

    
215
        // For select components, see if the optional option key is present.
216
        if ($display_element['#webform_component']['type'] === 'select') {
217
          list($option_key) = explode(':', substr($name, strlen($matched_token) + 1));
218
          if (strlen($option_key) && strpos("\n" . $display_element['#webform_component']['extra']['items'], "\n" . $option_key . '|') !== FALSE) {
219
            // Return only this specified option and no other values.
220
            $display_element['#value'] = array_intersect($display_element['#value'], array($option_key));
221
            $matched_token .= ':' . $option_key;
222
          }
223
        }
224

    
225
        // Assume no modifier (implied 'nolabel').
226
        $modifier = NULL;
227
        if (strcmp($name, $matched_token) !== 0) {
228
          // Check if this matches the key plus a modifier.
229
          $modifier = substr($name, strrpos($name, ':') + 1);
230
          // TODO: Allow components to provide additional modifiers per
231
          // type, i.e. key, day, hour, minute, etc.
232
          if (strcmp($name, $matched_token . ':' . $modifier) !== 0 || !in_array($modifier, $available_modifiers)) {
233
            // No match.
234
            continue;   // Token loop continue.
235
          }
236
        }
237

    
238
        if ($modifier === 'label') {
239
          $replacements[$original] = webform_filter_xss($display_element['#title']);
240
        }
241
        elseif ($modifier === 'key' && $display_element['#webform_component']['type'] === 'select') {
242
          $values = array();
243
          foreach ($display_element['#value'] as $value) {
244
            $values[] = webform_filter_xss($value);
245
          }
246
          $replacements[$original] = implode(' ', $values);
247
        }
248
        else {
249
          // Remove theme wrappers for the nolabel modifier.
250
          if ($modifier === 'nolabel' || empty($modifier)) {
251
            $display_element['#theme_wrappers'] = array();
252
          }
253
          $replacements[$original] = render($display_element);
254
        }
255
        // Continue processing tokens in case another modifier is used.
256
      }
257
    }
258

    
259
  }
260

    
261
  // Chained token relationships.
262
  if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
263
    $replacements += token_generate('date', $date_tokens, array('date' => $submission->submitted), $options);
264
  }
265
  if ($submission->completed && ($date_tokens = token_find_with_prefix($tokens, 'completed_date'))) {
266
    $replacements += token_generate('date', $date_tokens, array('date' => $submission->completed), $options);
267
  }
268
  if ($date_tokens = token_find_with_prefix($tokens, 'modified_date')) {
269
    $replacements += token_generate('date', $date_tokens, array('date' => $submission->modified), $options);
270
  }
271
  if (($user_tokens = token_find_with_prefix($tokens, 'user')) && $account = user_load($submission->uid)) {
272
    $replacements += token_generate('user', $user_tokens, array('user' => $account), $options);
273
  }
274
  if ($submission->sid) {
275
    if ($url_tokens = token_find_with_prefix($tokens, 'url')) {
276
      $replacements += token_generate('url', $url_tokens, array('path' => "node/{$node->nid}/submission/{$submission->sid}"), $options);
277
    }
278
    if ($url_tokens = token_find_with_prefix($tokens, 'edit-url')) {
279
      $replacements += token_generate('url', $url_tokens, array('path' => "node/{$node->nid}/submission/{$submission->sid}/edit"), $options);
280
    }
281
  }
282

    
283
  $recursion_level--;
284

    
285
  return $replacements;
286
}