token.pages.inc 10 KB

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