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