Projet

Général

Profil

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

root / htmltest / sites / all / modules / token / token.pages.inc @ a5572547

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
  $variables['options']['query']['options'] = array_intersect_key($variables, $info['token_tree']['variables']);
23

    
24
  // We should never pass the dialog option to theme_token_tree(). It is only
25
  // used for this function.
26
  unset($variables['options']['query']['options']['dialog']);
27

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

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

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

    
40
  return l($variables['text'], 'token/tree', $variables['options']);
41
}
42

    
43
/**
44
 * Page callback to output a token tree as an empty page.
45
 */
46
function token_page_output_tree() {
47
  $options = isset($_GET['options']) ? drupal_json_decode($_GET['options']) : array();
48

    
49
  // Check the token against the serialized options to prevent random access to
50
  // the token browser page.
51
  if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'token-tree:' . serialize($options))) {
52
    return MENU_ACCESS_DENIED;
53
  }
54

    
55
  // Force the dialog option to be false so we're not creating a dialog within
56
  // a dialog.
57
  $options['dialog'] = FALSE;
58

    
59
  $output = theme('token_tree', $options);
60
  print '<html><head>' . drupal_get_css() . drupal_get_js() . '</head>';
61
  print '<body class="token-tree">' . $output . '</body></html>';
62
  drupal_exit();
63
}
64

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

    
79
  if (!empty($variables['rows'])) {
80
    drupal_add_library('token', 'treeTable');
81
  }
82

    
83
  return theme('table', $variables);
84
}
85

    
86
/**
87
 * Provide a 'tree' display of nested tokens.
88
 *
89
 * @ingroup themeable
90
 */
91
function theme_token_tree($variables) {
92
  if (!empty($variables['dialog'])) {
93
    return theme_token_tree_link($variables);
94
  }
95

    
96
  $token_types = $variables['token_types'];
97
  $info = token_get_info();
98

    
99
  if ($token_types == 'all') {
100
    $token_types = array_keys($info['types']);
101
  }
102
  elseif ($variables['global_types']) {
103
    $token_types = array_merge($token_types, token_get_global_token_types());
104
  }
105

    
106
  $element = array(
107
    '#cache' => array(
108
      'cid' => 'tree-rendered:' . hash('sha256', serialize(array('token_types' => $token_types, 'global_types' => NULL) + $variables)),
109
      'bin' => 'cache_token',
110
    ),
111
  );
112
  if ($cached_output = token_render_cache_get($element)) {
113
    return $cached_output;
114
  }
115

    
116
  $options = array(
117
    'flat' => TRUE,
118
    'restricted' => $variables['show_restricted'],
119
    'depth' => $variables['recursion_limit'],
120
  );
121
  $multiple_token_types = (count($token_types) > 1);
122
  $rows = array();
123

    
124
  foreach ($info['types'] as $type => $type_info) {
125
    if (!in_array($type, $token_types)) {
126
      continue;
127
    }
128

    
129
    if ($multiple_token_types) {
130
      $row = _token_token_tree_format_row($type, $type_info, TRUE);
131
      unset($row['data']['value']);
132
      $rows[] = $row;
133
    }
134

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

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

    
166
  if ($variables['click_insert']) {
167
    $element['#caption'] = t('Click a token to insert it into the field you\'ve last clicked.');
168
    $element['#attributes']['class'][] = 'token-click-insert';
169
  }
170

    
171
  $output = drupal_render($element);
172
  token_render_cache_set($output, $element);
173
  return $output;
174
}
175

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

    
194
  $row = $defaults;
195
  $row['id'] = _token_clean_css_identifier($token);
196
  $row['data']['name'] = $token_info['name'];
197
  $row['data']['description'] = $token_info['description'];
198

    
199
  if ($is_group) {
200
    // This is a token type/group.
201
    $row['class'][] = 'token-group';
202
  }
203
  else {
204
    // This is a token.
205
    $row['data']['token']['data'] = $token;
206
    $row['data']['token']['class'][] = 'token-key';
207
    if (isset($token_info['value'])) {
208
      $row['data']['value'] = $token_info['value'];
209
    }
210
    if (!empty($token_info['parent'])) {
211
      $row['parent'] = _token_clean_css_identifier($token_info['parent']);
212
    }
213
  }
214

    
215
  return $row;
216
}
217

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

    
231
/**
232
 * Menu callback; prints the available tokens and values for an object.
233
 */
234
function token_devel_token_object($entity_type, $entity, $token_type = NULL) {
235
  $header = array(
236
    t('Token'),
237
    t('Value'),
238
  );
239
  $rows = array();
240

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

    
263
  $build['tokens'] = array(
264
    '#theme' => 'tree_table',
265
    '#header' => $header,
266
    '#rows' => $rows,
267
    '#attributes' => array('class' => array('token-tree')),
268
    '#empty' => t('No tokens available.'),
269
    '#attached' => array(
270
      'js' => array(drupal_get_path('module', 'token') . '/token.js'),
271
      'css' => array(drupal_get_path('module', 'token') . '/token.css'),
272
    ),
273
  );
274

    
275
  return $build;
276
}
277

    
278
/**
279
 * Page callback to clear the token registry caches.
280
 */
281
function token_flush_cache_callback() {
282
  if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], current_path())) {
283
    return MENU_NOT_FOUND;
284
  }
285

    
286
  token_clear_cache();
287
  drupal_set_message(t('Token registry caches cleared.'));
288
  drupal_goto();
289
}
290

    
291
function token_autocomplete() {
292
  $args = func_get_args();
293
  $string = implode('/', $args);
294

    
295
  $token_info = token_info();
296

    
297
  preg_match_all('/\[([^\s\]:]*):?([^\s\]]*)?\]?/', $string, $matches);
298
  $types = $matches[1];
299
  $tokens = $matches[2];
300

    
301
  foreach ($types as $index => $type) {
302
    if (!empty($tokens[$index]) || isset($token_info['types'][$type])) {
303
      token_autocomplete_token($type, $tokens[$index]);
304
    }
305
    else {
306
      token_autocomplete_type($type);
307
    }
308
  }
309

    
310
}
311

    
312
function token_autocomplete_type($string = '') {
313
  $token_info = token_info();
314
  $types = $token_info['types'];
315
  $matches = array();
316

    
317
  foreach ($types as $type => $info) {
318
    if (!$string || strpos($type, $string) === 0) {
319
      $type_key = "[{$type}:";
320
      $matches[$type_key] = levenshtein($type, $string);
321
    }
322
  }
323

    
324
  if ($string) {
325
    asort($matches);
326
  }
327
  else {
328
    ksort($matches);
329
  }
330

    
331
  $matches = drupal_map_assoc(array_keys($matches));
332
  drupal_json_output($matches);
333
}
334

    
335
function token_autocomplete_token($token_type) {
336
  $args = func_get_args();
337
  array_shift($args);
338
  $string = trim(implode('/', $args));
339
  $string = substr($string, strrpos($string, '['));
340

    
341
  $token_type = $token_type['type'];
342
  $matches = array();
343

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

    
361
  asort($matches);
362
  $matches = drupal_map_assoc(array_keys($matches));
363
  drupal_json_output($matches);
364
}