token.pages.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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']['name'] = $token_info['name'];
  173. $row['data']['description'] = $token_info['description'];
  174. if ($is_group) {
  175. // This is a token type/group.
  176. $row['class'][] = 'token-group';
  177. }
  178. else {
  179. // This is a token.
  180. $row['data']['token']['data'] = $token;
  181. $row['data']['token']['class'][] = 'token-key';
  182. if (isset($token_info['value'])) {
  183. $row['data']['value'] = $token_info['value'];
  184. }
  185. if (!empty($token_info['parent'])) {
  186. $row['parent'] = _token_clean_css_identifier($token_info['parent']);
  187. }
  188. }
  189. return $row;
  190. }
  191. /**
  192. * Wrapper function for drupal_clean_css_identifier() for use with tokens.
  193. *
  194. * This trims any brackets from the token and also cleans the colon character
  195. * to a hyphen.
  196. *
  197. * @see drupal_clean_css_identifier()
  198. */
  199. function _token_clean_css_identifier($id) {
  200. static $replacements = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '--', '?' => '', '<' => '-', '>' => '-');
  201. return 'token-' . rtrim(strtr(trim($id, '[]'), $replacements), '-');
  202. }
  203. /**
  204. * Menu callback; prints the available tokens and values for an object.
  205. */
  206. function token_devel_token_object($entity_type, $entity, $token_type = NULL) {
  207. $header = array(
  208. t('Token'),
  209. t('Value'),
  210. );
  211. $rows = array();
  212. $options = array(
  213. 'flat' => TRUE,
  214. 'values' => TRUE,
  215. 'data' => array($entity_type => $entity),
  216. );
  217. if (!isset($token_type)) {
  218. $token_type = $entity_type;
  219. }
  220. $tree = token_build_tree($token_type, $options);
  221. foreach ($tree as $token => $token_info) {
  222. if (!empty($token_info['restricted'])) {
  223. continue;
  224. }
  225. if (!isset($token_info['value']) && !empty($token_info['parent']) && !isset($tree[$token_info['parent']]['value'])) {
  226. continue;
  227. }
  228. $row = _token_token_tree_format_row($token, $token_info);
  229. unset($row['data']['description']);
  230. unset($row['data']['name']);
  231. $rows[] = $row;
  232. }
  233. $build['tokens'] = array(
  234. '#theme' => 'tree_table',
  235. '#header' => $header,
  236. '#rows' => $rows,
  237. '#attributes' => array('class' => array('token-tree')),
  238. '#empty' => t('No tokens available.'),
  239. '#attached' => array(
  240. 'js' => array(drupal_get_path('module', 'token') . '/token.js'),
  241. 'css' => array(drupal_get_path('module', 'token') . '/token.css'),
  242. ),
  243. );
  244. return $build;
  245. }
  246. /**
  247. * Page callback to clear the token registry caches.
  248. */
  249. function token_flush_cache_callback() {
  250. if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], current_path())) {
  251. return MENU_NOT_FOUND;
  252. }
  253. token_clear_cache();
  254. drupal_set_message(t('Token registry caches cleared.'));
  255. drupal_goto();
  256. }
  257. function token_autocomplete() {
  258. $args = func_get_args();
  259. $string = implode('/', $args);
  260. $token_info = token_info();
  261. preg_match_all('/\[([^\s\]:]*):?([^\s\]]*)?\]?/', $string, $matches);
  262. $types = $matches[1];
  263. $tokens = $matches[2];
  264. foreach ($types as $index => $type) {
  265. if (!empty($tokens[$index]) || isset($token_info['types'][$type])) {
  266. token_autocomplete_token($type, $tokens[$index]);
  267. }
  268. else {
  269. token_autocomplete_type($type);
  270. }
  271. }
  272. }
  273. function token_autocomplete_type($string = '') {
  274. $token_info = token_info();
  275. $types = $token_info['types'];
  276. $matches = array();
  277. foreach ($types as $type => $info) {
  278. if (!$string || strpos($type, $string) === 0) {
  279. $type_key = "[{$type}:";
  280. $matches[$type_key] = levenshtein($type, $string);
  281. }
  282. }
  283. if ($string) {
  284. asort($matches);
  285. }
  286. else {
  287. ksort($matches);
  288. }
  289. $matches = drupal_map_assoc(array_keys($matches));
  290. drupal_json_output($matches);
  291. }
  292. function token_autocomplete_token($token_type) {
  293. $args = func_get_args();
  294. array_shift($args);
  295. $string = trim(implode('/', $args));
  296. $string = substr($string, strrpos($string, '['));
  297. $token_type = $token_type['type'];
  298. $matches = array();
  299. if (!drupal_strlen($string)) {
  300. $matches["[{$token_type}:"] = 0;
  301. }
  302. else {
  303. $depth = max(1, substr_count($string, ':'));
  304. $tree = token_build_tree($token_type, array('flat' => TRUE, 'depth' => $depth));
  305. foreach (array_keys($tree) as $token) {
  306. if (strpos($token, $string) === 0) {
  307. $matches[$token] = levenshtein($token, $string);
  308. if (isset($tree[$token]['children'])) {
  309. $token = rtrim($token, ':]') . ':';
  310. $matches[$token] = levenshtein($token, $string);
  311. }
  312. }
  313. }
  314. }
  315. asort($matches);
  316. $matches = drupal_map_assoc(array_keys($matches));
  317. drupal_json_output($matches);
  318. }