-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_api_saved_searches.search_entity.inc
231 lines (203 loc) · 5.1 KB
/
search_api_saved_searches.search_entity.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
<?php
/**
* @file
* Contains the entity class for saved searches.
*/
/**
* Class representing "Saved searches" settings.
*/
class SearchApiSavedSearch extends Entity {
/**
* The user that owns this saved search.
*
* @var stdClass
*/
protected $user;
/**
* The settings this saved search uses.
*
* @var SearchApiSavedSearchesSettings
*/
protected $settings;
/**
* The search index this saved search uses.
*
* @var SearchApiIndex
*/
protected $index;
// Database values that will be set when object is loaded
/**
* @var integer
*/
public $id;
/**
* @var integer
*/
public $uid;
/**
* @var string
*/
public $settings_id;
/**
* @var boolean
*/
public $enabled;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $mail;
/**
* @var integer
*/
public $created;
/**
* @var integer
*/
public $last_queued;
/**
* @var integer
*/
public $last_execute;
/**
* @var integer
*/
public $notify_interval;
/**
* Array representing the search query to execute, containing:
* keys: The parsed fulltext keys.
* fields: The fields that will be fulltext-searched.
* filters: An array of filters, as used in SearchApiQueryFilterInterface.
* options: The query options.
*
* @var array
*/
public $query;
/**
* @var array
*/
public $options;
/**
* @var string
*/
public $results;
/**
* Constructor as a helper to the parent constructor.
*/
public function __construct(array $values = array()) {
parent::__construct($values, 'search_api_saved_search');
}
/**
* Permanently saves the entity.
*
* @see entity_save()
*/
public function save() {
$settings = $this->settings();
if ((!$this->enabled && empty($this->options['key'])) || !empty($settings->options['registered_user_delete_key'])) {
$this->options['key'] = drupal_hash_base64(drupal_random_bytes(12));
}
$date_field = isset($settings->options['date_field']) ? $settings->options['date_field'] : NULL;
if ($this->enabled && !isset($this->results) && !$date_field) {
$results = array();
$response = $this->query()->execute();
$this->results = implode(',', array_keys($response['results']));
}
$ret = parent::save();
if ($ret == SAVED_NEW && !$this->enabled) {
$params = array(
'user' => user_load($this->uid),
'search' => $this,
);
drupal_mail('search_api_saved_searches', 'activate', $this->mail, user_preferred_language($params['user']), $params);
}
return $ret;
}
/**
* @return
* The user that owns this saved search.
*/
public function user() {
if (!isset($this->user)) {
$this->user = user_load($this->uid);
}
return $this->user;
}
/**
* @return SearchApiSavedSearchesSettings
* The settings this saved search uses.
*
* @throws SearchApiException
* If the settings don't exist.
*/
public function settings() {
if (!isset($this->settings)) {
$this->settings = search_api_saved_searches_settings_load($this->settings_id);
}
if (!$this->settings) {
throw new SearchApiException(t("The saved search settings with the ID %id don't exist, but are used by an existing saved search.", array('%id' => $this->settings_id)));
}
return $this->settings;
}
/**
* @return SearchApiIndex
* The index this saved search uses.
*
* @throws SearchApiException
* If the index doesn't exist.
*/
public function index() {
if (!isset($this->index)) {
$this->index = search_api_index_load($this->settings()->index_id);
}
if (!$this->index) {
throw new SearchApiException(t("The index with the ID %id doesn't exist, but has saved search settings attached.", array('%id' => $this->settings()->index_id)));
}
return $this->index;
}
/**
* @return SearchApiQueryInterface
* A query for getting all new results for this saved search.
*
* @throws SearchApiException
* If the saved search's index is disabled.
*/
public function query() {
$index = $this->index();
$query = $index->query($this->query['options']);
if ($this->query['keys']) {
$query->keys($this->query['keys']);
}
if ($this->query['fields']) {
$fields = (array) $this->query['fields'];
$fields = array_intersect($fields, $index->getFulltextFields());
if ($fields) {
$query->fields($fields);
}
}
if ($this->query['filters']) {
$filters = &$query->getFilter()->getFilters();
$filters = $this->query['filters'];
}
return $query;
}
/**
* Return the URL where this search can be viewed, if any.
*/
public function url() {
if (isset($this->options['page']['path'])) {
return url($this->options['page']['path'], $this->options['page']);
}
}
/**
* Return a link to the URL where this search can be viewed, if any.
*/
public function l($text) {
if (isset($this->options['page']['path'])) {
return l($text, $this->options['page']['path'], $this->options['page']);
}
}
}