token.pages.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the token module.
  5. */
  6. /**
  7. * Theme a link to a token tree either as a regular link or a dialog.
  8. */
  9. function theme_token_tree_link($variables) {
  10. if (empty($variables['text'])) {
  11. $variables['text'] = t('Browse available tokens.');
  12. }
  13. if (!empty($variables['dialog'])) {
  14. drupal_add_library('token', 'dialog');
  15. $variables['options']['attributes']['class'][] = 'token-dialog';
  16. }
  17. $info = token_theme();
  18. $tree_variables = array_intersect_key($variables, $info['token_tree']['variables']);
  19. $tree_variables = drupal_array_diff_assoc_recursive($tree_variables, $info['token_tree']['variables']);
  20. if (!isset($variables['options']['query']['options'])) {
  21. $variables['options']['query']['options'] = array();
  22. }
  23. $variables['options']['query']['options'] += $tree_variables;
  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. // Add a security token so that the tree page should only work when used
  28. // when the dialog link is output with theme('token_tree_link').
  29. $variables['options']['query']['token'] = drupal_get_token('token-tree:' . serialize($variables['options']['query']['options']));
  30. // Because PHP converts query strings with arrays into a different syntax on
  31. // the next request, the options have to be encoded with JSON in the query
  32. // string so that we can reliably decode it for token comparison.
  33. $variables['options']['query']['options'] = drupal_json_encode($variables['options']['query']['options']);
  34. // Set the token tree to open in a separate window.
  35. $variables['options']['attributes'] + array('target' => '_blank');
  36. return l($variables['text'], 'token/tree', $variables['options']);
  37. }
  38. /**
  39. * Page callback to output a token tree as an empty page.
  40. */
  41. function token_page_output_tree() {
  42. $options = isset($_GET['options']) ? drupal_json_decode($_GET['options']) : array();
  43. // Check the token against the serialized options to prevent random access to
  44. // the token browser page.
  45. if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'token-tree:' . serialize($options))) {
  46. return MENU_ACCESS_DENIED;
  47. }
  48. // Force the dialog option to be false so we're not creating a dialog within
  49. // a dialog.
  50. $options['dialog'] = FALSE;
  51. $output = theme('token_tree', $options);
  52. print '<html><head>' . drupal_get_css() . drupal_get_js() . '</head>';
  53. print '<body class="token-tree">' . $output . '</body></html>';
  54. drupal_exit();
  55. }
  56. /**
  57. * Theme a tree table.
  58. *
  59. * @ingroup themeable
  60. */
  61. function theme_tree_table($variables) {
  62. foreach ($variables['rows'] as &$row) {
  63. $row += array('class' => array());
  64. if (!empty($row['parent'])) {
  65. $row['class'][] = 'child-of-' . $row['parent'];
  66. unset($row['parent']);
  67. }
  68. }
  69. if (!empty($variables['rows'])) {
  70. drupal_add_library('token', 'treeTable');
  71. }
  72. return theme('table', $variables);
  73. }
  74. /**
  75. * Provide a 'tree' display of nested tokens.
  76. *
  77. * @ingroup themeable
  78. */
  79. function theme_token_tree($variables) {
  80. if (!empty($variables['dialog'])) {
  81. return theme_token_tree_link($variables);
  82. }
  83. $token_types = $variables['token_types'];
  84. $info = token_get_info();
  85. if ($token_types == 'all') {
  86. $token_types = array_keys($info['types']);
  87. }
  88. elseif ($variables['global_types']) {
  89. $token_types = array_merge($token_types, token_get_global_token_types());
  90. }
  91. $element = array(
  92. '#cache' => array(
  93. 'cid' => 'tree-rendered:' . hash('sha256', serialize(array('token_types' => $token_types, 'global_types' => NULL) + $variables)),
  94. 'bin' => 'cache_token',
  95. ),
  96. );
  97. if ($cached_output = token_render_cache_get($element)) {
  98. return $cached_output;
  99. }
  100. $options = array(
  101. 'flat' => TRUE,
  102. 'restricted' => $variables['show_restricted'],
  103. 'depth' => $variables['recursion_limit'],
  104. );
  105. $multiple_token_types = (count($token_types) > 1);
  106. $rows = array();
  107. foreach ($info['types'] as $type => $type_info) {
  108. if (!in_array($type, $token_types)) {
  109. continue;
  110. }
  111. if ($multiple_token_types) {
  112. $row = _token_token_tree_format_row($type, $type_info, TRUE);
  113. unset($row['data']['value']);
  114. $rows[] = $row;
  115. }
  116. $tree = token_build_tree($type, $options);
  117. foreach ($tree as $token => $token_info) {
  118. if (!empty($token_info['restricted']) && empty($variables['show_restricted'])) {
  119. continue;
  120. }
  121. if ($multiple_token_types && !isset($token_info['parent'])) {
  122. $token_info['parent'] = $type;
  123. }
  124. $row = _token_token_tree_format_row($token, $token_info);
  125. unset($row['data']['value']);
  126. $rows[] = $row;
  127. }
  128. }
  129. $element += array(
  130. '#theme' => 'tree_table',
  131. '#header' => array(
  132. t('Name'),
  133. t('Token'),
  134. t('Description'),
  135. ),
  136. '#rows' => $rows,
  137. '#attributes' => array('class' => array('token-tree')),
  138. '#empty' => t('No tokens available'),
  139. '#attached' => array(
  140. 'js' => array(drupal_get_path('module', 'token') . '/token.js'),
  141. 'css' => array(drupal_get_path('module', 'token') . '/token.css'),
  142. 'library' => array(array('token', 'treeTable')),
  143. ),
  144. );
  145. if ($variables['click_insert']) {
  146. $element['#caption'] = t('Click a token to insert it into the field you\'ve last clicked.');
  147. $element['#attributes']['class'][] = 'token-click-insert';
  148. }
  149. $output = drupal_render($element);
  150. token_render_cache_set($output, $element);
  151. return $output;
  152. }
  153. /**
  154. * Build a row in the token tree.
  155. */
  156. function _token_token_tree_format_row($token, array $token_info, $is_group = FALSE) {
  157. // Build a statically cached array of default values. This is around four
  158. // times as efficient as building the base array from scratch each time this
  159. // function is called.
  160. static $defaults = array(
  161. 'id' => '',
  162. 'class' => array(),
  163. 'data' => array(
  164. 'name' => '',
  165. 'token' => '',
  166. 'value' => '',
  167. 'description' => '',
  168. ),
  169. );
  170. $row = $defaults;
  171. $row['id'] = _token_clean_css_identifier($token);
  172. $row['data']['token'] = array();
  173. $row['data']['name'] = $token_info['name'];
  174. $row['data']['description'] = $token_info['description'];
  175. if ($is_group) {
  176. // This is a token type/group.
  177. $row['class'][] = 'token-group';
  178. }
  179. else {
  180. // This is a token.
  181. $row['data']['token']['data'] = $token;
  182. $row['data']['token']['class'][] = 'token-key';
  183. if (isset($token_info['value'])) {
  184. $row['data']['value'] = $token_info['value'];
  185. }
  186. if (!empty($token_info['parent'])) {
  187. $row['parent'] = _token_clean_css_identifier($token_info['parent']);
  188. }
  189. }
  190. return $row;
  191. }
  192. /**
  193. * Wrapper function for drupal_clean_css_identifier() for use with tokens.
  194. *
  195. * This trims any brackets from the token and also cleans the colon character
  196. * to a hyphen.
  197. *
  198. * @see drupal_clean_css_identifier()
  199. */
  200. function _token_clean_css_identifier($id) {
  201. static $replacements = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '--', '?' => '', '<' => '-', '>' => '-');
  202. return 'token-' . rtrim(strtr(trim($id, '[]'), $replacements), '-');
  203. }
  204. /**
  205. * Menu callback; prints the available tokens and values for an object.
  206. */
  207. function token_devel_token_object($entity_type, $entity, $token_type = NULL) {
  208. $header = array(
  209. t('Token'),
  210. t('Value'),
  211. );
  212. $rows = array();
  213. $options = array(
  214. 'flat' => TRUE,
  215. 'values' => TRUE,
  216. 'data' => array($entity_type => $entity),
  217. );
  218. if (!isset($token_type)) {
  219. $token_type = $entity_type;
  220. }
  221. $tree = token_build_tree($token_type, $options);
  222. foreach ($tree as $token => $token_info) {
  223. if (!empty($token_info['restricted'])) {
  224. continue;
  225. }
  226. if (!isset($token_info['value']) && !empty($token_info['parent']) && !isset($tree[$token_info['parent']]['value'])) {
  227. continue;
  228. }
  229. $row = _token_token_tree_format_row($token, $token_info);
  230. unset($row['data']['description']);
  231. unset($row['data']['name']);
  232. $rows[] = $row;
  233. }
  234. $build['tokens'] = array(
  235. '#theme' => 'tree_table',
  236. '#header' => $header,
  237. '#rows' => $rows,
  238. '#attributes' => array('class' => array('token-tree')),
  239. '#empty' => t('No tokens available.'),
  240. '#attached' => array(
  241. 'js' => array(drupal_get_path('module', 'token') . '/token.js'),
  242. 'css' => array(drupal_get_path('module', 'token') . '/token.css'),
  243. ),
  244. );
  245. return $build;
  246. }
  247. /**
  248. * Page callback to clear the token registry caches.
  249. */
  250. function token_flush_cache_callback() {
  251. if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], current_path())) {
  252. return MENU_NOT_FOUND;
  253. }
  254. token_clear_cache();
  255. drupal_set_message(t('Token registry caches cleared.'));
  256. drupal_goto();
  257. }
  258. function token_autocomplete() {
  259. $args = func_get_args();
  260. $string = implode('/', $args);
  261. $token_info = token_info();
  262. preg_match_all('/\[([^\s\]:]*):?([^\s\]]*)?\]?/', $string, $matches);
  263. $types = $matches[1];
  264. $tokens = $matches[2];
  265. foreach ($types as $index => $type) {
  266. if (!empty($tokens[$index]) || isset($token_info['types'][$type])) {
  267. token_autocomplete_token($type, $tokens[$index]);
  268. }
  269. else {
  270. token_autocomplete_type($type);
  271. }
  272. }
  273. }
  274. function token_autocomplete_type($string = '') {
  275. $token_info = token_info();
  276. $types = $token_info['types'];
  277. $matches = array();
  278. foreach ($types as $type => $info) {
  279. if (!$string || strpos($type, $string) === 0) {
  280. $type_key = "[{$type}:";
  281. $matches[$type_key] = levenshtein($type, $string);
  282. }
  283. }
  284. if ($string) {
  285. asort($matches);
  286. }
  287. else {
  288. ksort($matches);
  289. }
  290. $matches = drupal_map_assoc(array_keys($matches));
  291. drupal_json_output($matches);
  292. }
  293. function token_autocomplete_token($token_type) {
  294. $args = func_get_args();
  295. array_shift($args);
  296. $string = trim(implode('/', $args));
  297. $string = substr($string, strrpos($string, '['));
  298. $token_type = $token_type['type'];
  299. $matches = array();
  300. if (!drupal_strlen($string)) {
  301. $matches["[{$token_type}:"] = 0;
  302. }
  303. else {
  304. $depth = max(1, substr_count($string, ':'));
  305. $tree = token_build_tree($token_type, array('flat' => TRUE, 'depth' => $depth));
  306. foreach (array_keys($tree) as $token) {
  307. if (strpos($token, $string) === 0) {
  308. $matches[$token] = levenshtein($token, $string);
  309. if (isset($tree[$token]['children'])) {
  310. $token = rtrim($token, ':]') . ':';
  311. $matches[$token] = levenshtein($token, $string);
  312. }
  313. }
  314. }
  315. }
  316. asort($matches);
  317. $matches = drupal_map_assoc(array_keys($matches));
  318. drupal_json_output($matches);
  319. }