search_api_saved_searches.tokens.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for user-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. *
  9. * Provides the following tokens:
  10. * - [user:search-api-saved-searches-url]
  11. * - [search-api-saved-searches:quantity]
  12. * - [search-api-saved-searches:results]
  13. * - [search-api-saved-search:name]
  14. * - [search-api-saved-search:created]
  15. * - [search-api-saved-search:quantity]
  16. * - [search-api-saved-search:items]
  17. * - [search-api-saved-search:results-capped]
  18. * - [search-api-saved-search:view-url]
  19. * - [search-api-saved-search:activate-url]
  20. * - [search-api-saved-search:edit-url]
  21. * - [search-api-saved-search:delete-url]
  22. * - [search-api-saved-search-result:label]
  23. * - [search-api-saved-search-result:url]
  24. *
  25. * The used data types are the following:
  26. * - search_api_saved_searches: An array with values of type
  27. * search_api_saved_search_info.
  28. * - search_api_saved_search_info: An associative array containing:
  29. * - search: A SearchApiSavedSearch entity object.
  30. * - num_results: An integer telling how many new results there are for this
  31. * saved search.
  32. * - results: An array of items of the type that the index associated with
  33. * this saved search contains.
  34. * - search_api_saved_search_result_info: An associative array containing:
  35. * - search: A SearchApiSavedSearch entity object.
  36. * - entity: An item of the type corresponding to the saved search.
  37. *
  38. * @see search_api_saved_searches_tokens()
  39. */
  40. function search_api_saved_searches_token_info() {
  41. $info['types']['search-api-saved-searches'] = array(
  42. 'name' => t('Search API saved searches'),
  43. 'description' => t('Tokens related to several saved searches at once.'),
  44. 'needs-data' => 'search_api_saved_searches',
  45. );
  46. $info['types']['search-api-saved-search'] = array(
  47. 'name' => t('Search API saved search'),
  48. 'description' => t('Tokens related to a saved search.'),
  49. 'needs-data' => 'search_api_saved_search_info',
  50. );
  51. $info['types']['search-api-saved-search-result'] = array(
  52. 'name' => t('Search API saved search result'),
  53. 'description' => t('Tokens related to a result of a saved search.'),
  54. 'needs-data' => 'search_api_saved_search_result_info',
  55. );
  56. $tokens = &$info['tokens'];
  57. $tokens['user']['search-api-saved-searches-url'] = array(
  58. 'name' => t('Config URL'),
  59. 'description' => t('The URL under which a user can configure her saved searches.'),
  60. 'type' => 'url',
  61. );
  62. $tokens['search-api-saved-searches']['quantity'] = array(
  63. 'name' => t('Number of results'),
  64. 'description' => t('Number of results for all saved searches in this message.'),
  65. );
  66. $tokens['search-api-saved-searches']['results'] = array(
  67. 'name' => t('Results'),
  68. 'description' => t('Text listing new results for one or more saved searches.'),
  69. );
  70. $search = &$tokens['search-api-saved-search'];
  71. $search['name'] = array(
  72. 'name' => t('Name'),
  73. 'description' => t('The name of the saved search.'),
  74. );
  75. $search['created'] = array(
  76. 'name' => t('Date created'),
  77. 'description' => t('The date on which the saved search was created.'),
  78. 'type' => 'date',
  79. );
  80. $search['quantity'] = array(
  81. 'name' => t('Number of results'),
  82. 'description' => t('Number of results for the saved search.'),
  83. );
  84. $search['items'] = array(
  85. 'name' => t('Results'),
  86. 'description' => t('Text listing new results for the saved search.'),
  87. );
  88. $search['results-capped'] = array(
  89. 'name' => t('"Displayed results truncated" text'),
  90. 'description' => t('Text explaining that not all new results were displayed, if applicable.'),
  91. );
  92. $search['view-url'] = array(
  93. 'name' => t('View URL'),
  94. 'description' => t('The URL of the page displaying this search, if any.'),
  95. 'type' => 'url',
  96. );
  97. $search['activate-url'] = array(
  98. 'name' => t('Activate URL'),
  99. 'description' => t('The URL for activating the saved search, if necessary.'),
  100. 'type' => 'url',
  101. );
  102. $search['edit-url'] = array(
  103. 'name' => t('Edit URL'),
  104. 'description' => t("The URL for editing the saved search's notification interval."),
  105. 'type' => 'url',
  106. );
  107. $search['delete-url'] = array(
  108. 'name' => t('Delete URL'),
  109. 'description' => t('The URL for deleting the saved search.'),
  110. 'type' => 'url',
  111. );
  112. $tokens['search-api-saved-search-result']['id'] = array(
  113. 'name' => t('ID'),
  114. 'description' => t('The item id for the result.'),
  115. );
  116. $tokens['search-api-saved-search-result']['label'] = array(
  117. 'name' => t('Label'),
  118. 'description' => t('The human-readable label for the result.'),
  119. );
  120. $tokens['search-api-saved-search-result']['url'] = array(
  121. 'name' => t('URL'),
  122. 'description' => t('The URL at which the result can be viewed.'),
  123. 'type' => 'url',
  124. );
  125. return $info;
  126. }
  127. /**
  128. * Implements hook_tokens().
  129. *
  130. * @see search_api_saved_searches_token_info()
  131. */
  132. function search_api_saved_searches_tokens($type, array $tokens, array $data = array(), array $options = array()) {
  133. $url_options = array('absolute' => TRUE);
  134. if (isset($options['language'])) {
  135. $url_options['language'] = $options['language'];
  136. $language_code = $options['language']->language;
  137. }
  138. else {
  139. $language_code = NULL;
  140. }
  141. $sanitize = !empty($options['sanitize']);
  142. $replacements = array();
  143. switch ($type) {
  144. case 'user':
  145. if (isset($tokens['search-api-saved-searches-url'])) {
  146. $url_tokens = token_find_with_prefix($tokens, 'search-api-saved-searches-url');
  147. if (!empty($data['user']->uid)) {
  148. $path = 'user/' . $data['user']->uid . '/saved-searches';
  149. $replacements[$tokens['search-api-saved-searches-url']] = url($path, $url_options);
  150. if ($url_tokens) {
  151. $replacements += token_generate('url', $url_tokens, array(
  152. 'path' => $path,
  153. ), $options);
  154. }
  155. }
  156. else {
  157. $replacements[$tokens['search-api-saved-searches-url']] = '';
  158. foreach ($url_tokens as $token) {
  159. $replacements[$token] = '';
  160. }
  161. }
  162. }
  163. break;
  164. case 'search-api-saved-searches':
  165. if (!empty($data['search_api_saved_searches'])) {
  166. if (isset($tokens['results'])) {
  167. $results = array();
  168. foreach ($data['search_api_saved_searches'] as $info) {
  169. $search = $info['search'];
  170. $settings = $search->settings();
  171. if (empty($settings->options['mail']['notify']['results'])) {
  172. continue;
  173. }
  174. $text = $settings->getTranslatedOption('mail.notify.results', $language_code);
  175. $params['user'] = $search->user();
  176. $params['search_api_saved_search_info'] = $info;
  177. $results[] = token_replace($text, $params, $options);
  178. }
  179. $text = implode("\n\n", $results);
  180. $replacements[$tokens['results']] = $sanitize ? check_plain($text) : $text;
  181. }
  182. if (isset($tokens['quantity'])) {
  183. $quantity = 0;
  184. foreach ($data['search_api_saved_searches'] as $info) {
  185. if (isset($info['num_results'])) {
  186. $quantity += $info['num_results'];
  187. }
  188. }
  189. $replacements[$tokens['quantity']] = $quantity;
  190. }
  191. }
  192. break;
  193. case 'search-api-saved-search':
  194. if (!empty($data['search_api_saved_search_info']['search'])) {
  195. $search = $data['search_api_saved_search_info']['search'];
  196. foreach ($tokens as $name => $original) {
  197. switch ($name) {
  198. case 'name':
  199. $replacements[$original] = $sanitize ? check_plain($search->name) : $search->name;
  200. break;
  201. case 'created':
  202. $replacements[$original] = format_date($search->created, 'medium', '', NULL, $language_code);
  203. break;
  204. case 'quantity':
  205. if (empty($data['search_api_saved_search_info']['num_results'])) {
  206. $replacements[$original] = 0;
  207. break;
  208. }
  209. $replacements[$original] = $data['search_api_saved_search_info']['num_results'];
  210. break;
  211. case 'items':
  212. if (!empty($data['search_api_saved_search_info']['results'])) {
  213. $results = array();
  214. $settings = $search->settings();
  215. $text = $settings->getTranslatedOption('mail.notify.result', $language_code);
  216. if (empty($text)) {
  217. break;
  218. }
  219. $item_type = $search->index()->item_type;
  220. foreach ($data['search_api_saved_search_info']['results'] as $item) {
  221. $params = array(
  222. $item_type => $item,
  223. 'search_api_saved_search_result_info' => array(
  224. 'search' => $search,
  225. 'entity' => $item,
  226. ),
  227. );
  228. $results[] = token_replace($text, $params, $options);
  229. }
  230. $text = implode("\n", $results);
  231. $replacements[$original] = $sanitize ? check_plain($text) : $text;
  232. }
  233. break;
  234. case 'results-capped':
  235. if ($data['search_api_saved_search_info']['num_results'] == count($data['search_api_saved_search_info']['results'])) {
  236. $replacements[$original] = '';
  237. break;
  238. }
  239. $text = $settings->getTranslatedOption('mail.notify.results_capped', $language_code);
  240. $text = token_replace($text, $data, $options);
  241. $replacements[$original] = $sanitize ? check_plain($text) : $text;
  242. break;
  243. case 'view-url':
  244. $replacements[$original] = $search->url();
  245. break;
  246. case 'activate-url':
  247. $key = isset($search->options['key']) ? '/' . $search->options['key'] : '';
  248. $replacements[$original] = url('search-api/saved-search/' . $search->id . '/activate' . $key, $url_options);
  249. break;
  250. case 'edit-url':
  251. $key = isset($search->options['key']) ? '/' . $search->options['key'] : '';
  252. $replacements[$original] = url('search-api/saved-search/' . $search->id . '/edit' . $key, $url_options);
  253. break;
  254. case 'delete-url':
  255. $key = isset($search->options['key']) ? '/' . $search->options['key'] : '';
  256. $replacements[$original] = url('search-api/saved-search/' . $search->id . '/delete' . $key, $url_options);
  257. break;
  258. }
  259. }
  260. if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
  261. $replacements += token_generate('date', $created_tokens, array('date' => $search->created), $options);
  262. }
  263. if ($view_url_tokens = token_find_with_prefix($tokens, 'view-url')) {
  264. $replacements += token_generate('url', $view_url_tokens, array(
  265. 'path' => $search->url(),
  266. ), $options);
  267. }
  268. if ($activate_url_tokens = token_find_with_prefix($tokens, 'activate-url')) {
  269. $key = isset($search->options['key']) ? '/' . $search->options['key'] : '';
  270. $replacements += token_generate('url', $activate_url_tokens, array(
  271. 'path' => 'search-api/saved-search/' . $search->id . '/activate' . $key,
  272. ), $options);
  273. }
  274. if ($edit_url_tokens = token_find_with_prefix($tokens, 'edit-url')) {
  275. $key = isset($search->options['key']) ? '/' . $search->options['key'] : '';
  276. $replacements += token_generate('url', $edit_url_tokens, array(
  277. 'path' => 'search-api/saved-search/' . $search->id . '/edit' . $key,
  278. ), $options);
  279. }
  280. if ($delete_url_tokens = token_find_with_prefix($tokens, 'delete-url')) {
  281. $key = isset($search->options['key']) ? '/' . $search->options['key'] : '';
  282. $replacements += token_generate('url', $delete_url_tokens, array(
  283. 'path' => 'search-api/saved-search/' . $search->id . '/delete' . $key,
  284. ), $options);
  285. }
  286. }
  287. break;
  288. case 'search-api-saved-search-result':
  289. $d = $data['search_api_saved_search_result_info'];
  290. if (!empty($d['search']) && !empty($d['entity'])) {
  291. $search = $d['search'];
  292. $index = $search->index();
  293. $item = $d['entity'];
  294. foreach ($tokens as $name => $original) {
  295. if ($name == 'id' && ($id = $index->datasource()->getItemId($item))) {
  296. $replacements[$original] = $id;
  297. }
  298. elseif ($name == 'label' && ($label = $index->datasource()->getItemLabel($item))) {
  299. $replacements[$original] = $label;
  300. }
  301. elseif ($name == 'url' && ($url = $index->datasource()->getItemUrl($item))) {
  302. $url['options'] += $url_options;
  303. $replacements[$original] = url($url['path'], $url['options']);
  304. }
  305. }
  306. if (($url_tokens = token_find_with_prefix($tokens, 'url')) && ($url = $index->datasource()->getItemUrl($item))) {
  307. $replacements += token_generate('url', $url_tokens, $url, $options);
  308. }
  309. }
  310. break;
  311. }
  312. return $replacements;
  313. }