Projet

Général

Profil

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

root / drupal7 / sites / all / modules / token / token.pages.inc @ f9d4b898

1
<?php
2

    
3
/**
4
 * @file
5
 * User page callbacks for the token module.
6
 */
7

    
8
/**
9
 * Theme a link to a token tree either as a regular link or a dialog.
10
 */
11
function theme_token_tree_link($variables) {
12
  if (empty($variables['text'])) {
13
    $variables['text'] = t('Browse available tokens.');
14
  }
15

    
16
  if (!empty($variables['dialog'])) {
17
    drupal_add_library('token', 'dialog');
18
    $variables['options']['attributes']['class'][] = 'token-dialog';
19
  }
20

    
21
  $info = token_theme();
22
  $tree_variables = array_intersect_key($variables, $info['token_tree']['variables']);
23
  $tree_variables = drupal_array_diff_assoc_recursive($tree_variables, $info['token_tree']['variables']);
24
  if (!isset($variables['options']['query']['options'])) {
25
    $variables['options']['query']['options'] = array();
26
  }
27
  $variables['options']['query']['options'] += $tree_variables;
28

    
29
  // We should never pass the dialog option to theme_token_tree(). It is only
30
  // used for this function.
31
  unset($variables['options']['query']['options']['dialog']);
32

    
33
  // Add a security token so that the tree page should only work when used
34
  // when the dialog link is output with theme('token_tree_link').
35
  $variables['options']['query']['token'] = drupal_get_token('token-tree:' . serialize($variables['options']['query']['options']));
36

    
37
  // Because PHP converts query strings with arrays into a different syntax on
38
  // the next request, the options have to be encoded with JSON in the query
39
  // string so that we can reliably decode it for token comparison.
40
  $variables['options']['query']['options'] = drupal_json_encode($variables['options']['query']['options']);
41

    
42
  // Set the token tree to open in a separate window.
43
  $variables['options']['attributes'] + array('target' => '_blank');
44

    
45
  return l($variables['text'], 'token/tree', $variables['options']);
46
}
47

    
48
/**
49
 * Page callback to output a token tree as an empty page.
50
 */
51
function token_page_output_tree() {
52
  $options = isset($_GET['options']) ? drupal_json_decode($_GET['options']) : array();
53

    
54
  // Check the token against the serialized options to prevent random access to
55
  // the token browser page.
56
  if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'token-tree:' . serialize($options))) {
57
    return MENU_ACCESS_DENIED;
58
  }
59

    
60
  // Force the dialog option to be false so we're not creating a dialog within
61
  // a dialog.
62
  $options['dialog'] = FALSE;
63

    
64
  $output = theme('token_tree', $options);
65
  print '<html><head>' . drupal_get_css() . drupal_get_js() . '</head>';
66
  print '<body class="token-tree">' . $output . '</body></html>';
67
  drupal_exit();
68
}
69

    
70
/**
71
 * Theme a tree table.
72
 *
73
 * @ingroup themeable
74
 */
75
function theme_tree_table($variables) {
76
  foreach ($variables['rows'] as &$row) {
77
    $row += array('class' => array());
78
    if (!empty($row['parent'])) {
79
      $row['class'][] = 'child-of-' . $row['parent'];
80
      unset($row['parent']);
81
    }
82
  }
83

    
84
  if (!empty($variables['rows'])) {
85
    drupal_add_library('token', 'treeTable');
86
  }
87

    
88
  return theme('table', $variables);
89
}
90

    
91
/**
92
 * Provide a 'tree' display of nested tokens.
93
 *
94
 * @ingroup themeable
95
 */
96
function theme_token_tree($variables) {
97
  if (!empty($variables['dialog'])) {
98
    return theme_token_tree_link($variables);
99
  }
100

    
101
  $token_types = $variables['token_types'];
102
  $info = token_get_info();
103

    
104
  if ($token_types == 'all') {
105
    $token_types = array_keys($info['types']);
106
  }
107
  elseif ($variables['global_types']) {
108
    $token_types = array_merge($token_types, token_get_global_token_types());
109
  }
110

    
111
  $element = array(
112
    '#cache' => array(
113
      'cid' => 'tree-rendered:' . hash('sha256', serialize(array('token_types' => $token_types, 'global_types' => NULL) + $variables)),
114
      'bin' => 'cache_token',
115
    ),
116
  );
117
  if ($cached_output = token_render_cache_get($element)) {
118
    return $cached_output;
119
  }
120

    
121
  $options = array(
122
    'flat' => TRUE,
123
    'restricted' => $variables['show_restricted'],
124
    'depth' => $variables['recursion_limit'],
125
  );
126
  $multiple_token_types = (count($token_types) > 1);
127
  $rows = array();
128

    
129
  foreach ($info['types'] as $type => $type_info) {
130
    if (!in_array($type, $token_types)) {
131
      continue;
132
    }
133

    
134
    if ($multiple_token_types) {
135
      $row = _token_token_tree_format_row($type, $type_info, TRUE);
136
      unset($row['data']['value']);
137
      $rows[] = $row;
138
    }
139

    
140
    $tree = token_build_tree($type, $options);
141
    foreach ($tree as $token => $token_info) {
142
      if (!empty($token_info['restricted']) && empty($variables['show_restricted'])) {
143
        continue;
144
      }
145
      if ($multiple_token_types && !isset($token_info['parent'])) {
146
        $token_info['parent'] = $type;
147
      }
148
      $row = _token_token_tree_format_row($token, $token_info);
149
      unset($row['data']['value']);
150
      $rows[] = $row;
151
    }
152
  }
153

    
154
  $element += array(
155
    '#theme' => 'tree_table',
156
    '#header' => array(
157
      t('Name'),
158
      t('Token'),
159
      t('Description'),
160
    ),
161
    '#rows' => $rows,
162
    '#attributes' => array('class' => array('token-tree')),
163
    '#empty' => t('No tokens available'),
164
    '#attached' => array(
165
      'js' => array(drupal_get_path('module', 'token') . '/token.js'),
166
      'css' => array(drupal_get_path('module', 'token') . '/token.css'),
167
      'library' => array(array('token', 'treeTable')),
168
    ),
169
  );
170

    
171
  if ($variables['click_insert']) {
172
    $element['#caption'] = t('Click a token to insert it into the field you\'ve last clicked.');
173
    $element['#attributes']['class'][] = 'token-click-insert';
174
  }
175

    
176
  $output = drupal_render($element);
177
  token_render_cache_set($output, $element);
178
  return $output;
179
}
180

    
181
/**
182
 * Build a row in the token tree.
183
 */
184
function _token_token_tree_format_row($token, array $token_info, $is_group = FALSE) {
185
  // Build a statically cached array of default values. This is around four
186
  // times as efficient as building the base array from scratch each time this
187
  // function is called.
188
  static $defaults = array(
189
    'id' => '',
190
    'class' => array(),
191
    'data' => array(
192
      'name' => '',
193
      'token' => '',
194
      'value' => '',
195
      'description' => '',
196
    ),
197
  );
198

    
199
  $row = $defaults;
200
  $row['id'] = _token_clean_css_identifier($token);
201
  $row['data']['name'] = $token_info['name'];
202
  $row['data']['description'] = isset($token_info['description']) ? $token_info['description'] : '';
203

    
204
  if ($is_group) {
205
    // This is a token type/group.
206
    $row['class'][] = 'token-group';
207
  }
208
  else {
209
    // This is a token.
210
    $row['data']['token'] = array();
211
    $row['data']['token']['data'] = $token;
212
    $row['data']['token']['class'][] = 'token-key';
213
    if (isset($token_info['value'])) {
214
      $row['data']['value'] = $token_info['value'];
215
    }
216
    if (!empty($token_info['parent'])) {
217
      $row['parent'] = _token_clean_css_identifier($token_info['parent']);
218
    }
219
  }
220

    
221
  return $row;
222
}
223

    
224
/**
225
 * Wrapper function for drupal_clean_css_identifier() for use with tokens.
226
 *
227
 * This trims any brackets from the token and also cleans the colon character
228
 * to a hyphen.
229
 *
230
 * @see drupal_clean_css_identifier()
231
 */
232
function _token_clean_css_identifier($id) {
233
  static $replacements = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '--', '?' => '', '<' => '-', '>' => '-');
234
  return 'token-' . rtrim(strtr(trim($id, '[]'), $replacements), '-');
235
}
236

    
237
/**
238
 * Menu callback; prints the available tokens and values for an object.
239
 */
240
function token_devel_token_object($entity_type, $entity, $token_type = NULL) {
241
  $header = array(
242
    t('Token'),
243
    t('Value'),
244
  );
245
  $rows = array();
246

    
247
  $options = array(
248
    'flat' => TRUE,
249
    'values' => TRUE,
250
    'data' => array($entity_type => $entity),
251
  );
252
  if (!isset($token_type)) {
253
    $token_type = $entity_type;
254
  }
255
  $tree = token_build_tree($token_type, $options);
256
  foreach ($tree as $token => $token_info) {
257
    if (!empty($token_info['restricted'])) {
258
      continue;
259
    }
260
    if (!isset($token_info['value']) && !empty($token_info['parent']) && !isset($tree[$token_info['parent']]['value'])) {
261
      continue;
262
    }
263
    $row = _token_token_tree_format_row($token, $token_info);
264
    unset($row['data']['description']);
265
    unset($row['data']['name']);
266
    $rows[] = $row;
267
  }
268

    
269
  $build['tokens'] = array(
270
    '#theme' => 'tree_table',
271
    '#header' => $header,
272
    '#rows' => $rows,
273
    '#attributes' => array('class' => array('token-tree')),
274
    '#empty' => t('No tokens available.'),
275
    '#attached' => array(
276
      'js' => array(drupal_get_path('module', 'token') . '/token.js'),
277
      'css' => array(drupal_get_path('module', 'token') . '/token.css'),
278
    ),
279
  );
280

    
281
  return $build;
282
}
283

    
284
/**
285
 * Page callback to clear the token registry caches.
286
 */
287
function token_flush_cache_callback() {
288
  if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], current_path())) {
289
    return MENU_NOT_FOUND;
290
  }
291

    
292
  token_clear_cache();
293
  drupal_set_message(t('Token registry caches cleared.'));
294
  drupal_goto();
295
}
296

    
297
function token_autocomplete() {
298
  $args = func_get_args();
299
  $string = implode('/', $args);
300

    
301
  $token_info = token_info();
302

    
303
  preg_match_all('/\[([^\s\]:]*):?([^\s\]]*)?\]?/', $string, $matches);
304
  $types = $matches[1];
305
  $tokens = $matches[2];
306

    
307
  foreach ($types as $index => $type) {
308
    if (!empty($tokens[$index]) || isset($token_info['types'][$type])) {
309
      token_autocomplete_token($type, $tokens[$index]);
310
    }
311
    else {
312
      token_autocomplete_type($type);
313
    }
314
  }
315

    
316
}
317

    
318
function token_autocomplete_type($string = '') {
319
  $token_info = token_info();
320
  $types = $token_info['types'];
321
  $matches = array();
322

    
323
  foreach ($types as $type => $info) {
324
    if (!$string || strpos($type, $string) === 0) {
325
      $type_key = "[{$type}:";
326
      $matches[$type_key] = levenshtein($type, $string);
327
    }
328
  }
329

    
330
  if ($string) {
331
    asort($matches);
332
  }
333
  else {
334
    ksort($matches);
335
  }
336

    
337
  $matches = drupal_map_assoc(array_keys($matches));
338
  drupal_json_output($matches);
339
}
340

    
341
function token_autocomplete_token($token_type) {
342
  $args = func_get_args();
343
  array_shift($args);
344
  $string = trim(implode('/', $args));
345
  $string = substr($string, strrpos($string, '['));
346

    
347
  $token_type = $token_type['type'];
348
  $matches = array();
349

    
350
  if (!drupal_strlen($string)) {
351
    $matches["[{$token_type}:"] = 0;
352
  }
353
  else {
354
    $depth = max(1, substr_count($string, ':'));
355
    $tree = token_build_tree($token_type, array('flat' => TRUE, 'depth' => $depth));
356
    foreach (array_keys($tree) as $token) {
357
      if (strpos($token, $string) === 0) {
358
        $matches[$token] = levenshtein($token, $string);
359
        if (isset($tree[$token]['children'])) {
360
          $token = rtrim($token, ':]') . ':';
361
          $matches[$token] = levenshtein($token, $string);
362
        }
363
      }
364
    }
365
  }
366

    
367
  asort($matches);
368
  $matches = drupal_map_assoc(array_keys($matches));
369
  drupal_json_output($matches);
370
}