Projet

Général

Profil

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

root / drupal7 / sites / all / modules / token / token.pages.inc @ 1f142f4f

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'] = $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']['data'] = $token;
211
    $row['data']['token']['class'][] = 'token-key';
212
    if (isset($token_info['value'])) {
213
      $row['data']['value'] = $token_info['value'];
214
    }
215
    if (!empty($token_info['parent'])) {
216
      $row['parent'] = _token_clean_css_identifier($token_info['parent']);
217
    }
218
  }
219

    
220
  return $row;
221
}
222

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

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

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

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

    
280
  return $build;
281
}
282

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

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

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

    
300
  $token_info = token_info();
301

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

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

    
315
}
316

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

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

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

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

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

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

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

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