-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate_authored_facet.module
executable file
·375 lines (345 loc) · 12.4 KB
/
date_authored_facet.module
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* @file
* Provides a facet for searching content by date of creation.
*/
require_once('./'. drupal_get_path('module', 'faceted_search') .'/faceted_search.inc');
// TODO: add setting for granularity.
/**
* Implementation of hook_faceted_search_collect().
*/
function date_authored_facet_faceted_search_collect(&$facets, $domain, $env, $selection, $arg = NULL) {
switch ($domain) {
case 'facets':
// If the date authored facet is allowed.
if (!isset($selection) || isset($selection['date_authored'][1])) {
$facets[] = new date_authored_facet();
}
break;
case 'text':
// If the date authored facet is allowed.
if (!isset($selection) || isset($selection['date_authored'][1])) {
// Scan the given search text for a 'date_authored:date' token, and
// extract a category from it. Date is formatted as either YYYY,
// YYYY-MM, or YYYY-MM-DD.
if ($date = search_query_extract($arg, 'date_authored')) {
$matches = array();
// Match the three possible date formats.
if (preg_match('/^([12][0-9][0-9][0-9])(-([01][0-9])(-([0-3][0-9]))?)?$/', $date, $matches)) {
if (_date_authored_facet_check_date($matches[1], $matches[3], $matches[5])) {
$path = array(); // Array to build the path of categories.
if (isset($matches[1])) {
// Found year.
$path[] = new date_authored_facet_category($matches[1]);
}
if (isset($matches[3])) {
// Found month.
$path[] = new date_authored_facet_category($matches[1], $matches[3]);
}
if (isset($matches[5])) {
// Found day.
$path[] = new date_authored_facet_category($matches[1], $matches[3], $matches[5]);
}
if (!empty($path)) {
// Create a facet with the date found in the search text as the
// active category.
$facets[] = new date_authored_facet($path);
}
}
}
// Remove the parsed token from the search text.
$arg = search_query_insert($arg, 'date_authored');
}
}
return $arg;
case 'node':
// If the date authored facet is allowed.
if (!isset($selection) || isset($selection['date_authored'][1])) {
// Convert the node's creation date into the user's timezone.
$timestamp = $arg->created + _date_authored_facet_get_user_timezone();
$year = gmdate('Y', $timestamp);
$month = gmdate('n', $timestamp);
$day = gmdate('j', $timestamp);
// Build the path of categories.
$path = array(
new date_authored_facet_category($year),
new date_authored_facet_category($year, $month),
new date_authored_facet_category($year, $month, $day),
);
// Create a facet with the node's creation date as the active category.
$facets[] = new date_authored_facet($path);
}
break;
}
}
/**
* A facet for searching content by date of creation.
*/
class date_authored_facet extends faceted_search_facet {
/**
* Constructor.
*/
function date_authored_facet($active_path = array()) {
parent::faceted_search_facet('date_authored', $active_path);
}
function get_id() {
return 1; // This module provides only one facet.
}
function get_label() {
return t('Date authored');
}
/**
* Returns the available sort options for this facet.
*/
function get_sort_options() {
$options = parent::get_sort_options();
$options['oldest'] = t('Oldest first');
$options['latest'] = t('Latest first');
return $options;
}
/**
* Handler for the 'count' sort criteria.
*/
function build_sort_query_count(&$query) {
$query->add_orderby('count', 'DESC');
$query->add_orderby('n.created', 'ASC');
}
/**
* Handler for the 'oldest' sort criteria.
*/
function build_sort_query_oldest(&$query) {
$query->add_orderby('n.created', 'ASC');
}
/**
* Handler for the 'latest' sort criteria.
*/
function build_sort_query_latest(&$query) {
$query->add_orderby('n.created', 'DESC');
}
/**
* Returns the search text for this facet, taking into account this facet's
* active path.
*/
function get_text() {
if ($category = $this->get_active_category()) {
return $category->get_text();
}
return '';
}
/**
* Updates a query for retrieving the root categories of this facet and their
* associated nodes within the current search results.
*
* @param $query
* The query object to update.
*
* @return
* FALSE if this facet can't have root categories.
*/
function build_root_categories_query(&$query) {
$timezone = _date_authored_facet_get_timezone();
$query->add_field(NULL, "YEAR(FROM_UNIXTIME(n.created + $timezone))", 'node_created_year');
$query->add_groupby('node_created_year');
return TRUE;
}
/**
* This factory method creates categories given query results that include the
* fields selected in get_root_categories_query() or get_subcategories_query().
*
* @param $results
* $results A database query result resource.
*
* @return
* Array of categories.
*/
function build_categories($results) {
$categories = array();
while ($result = db_fetch_object($results)) {
$categories[] = new date_authored_facet_category($result->node_created_year, $result->node_created_month, $result->node_created_day, $result->count);
}
return $categories;
}
}
/**
* A category for node creation date.
*/
class date_authored_facet_category extends faceted_search_category {
var $_year = NULL;
var $_month = NULL;
var $_day = NULL;
/**
* Constructs a category for the specified date.
*
* @param $year
* Year corresponding to this category.
*
* @param $month
* Month corresponding to this category. Optional, but must be specified if
* $day is specified.
*
* @param $day
* Day corresponding to this category. Optional.
*
* @param $count
* The number of nodes associated to this category within the current
* search. Optional.
*
* Note: We consider the specified date as within the user's timezone.
*/
function date_authored_facet_category($year, $month = NULL, $day = NULL, $count = NULL) {
parent::faceted_search_category($count);
$this->_year = $year;
$this->_month = $month;
$this->_day = $day;
}
/**
* Return the label of this category.
*
* @param $html
* TRUE when HTML is allowed in the label, FALSE otherwise. Checking this
* flag allows implementors to provide a rich-text label if desired, and an
* alternate plain text version for cases where HTML cannot be used. The
* implementor is responsible to ensure adequate security filtering.
*/
function get_label($html = FALSE) {
if (isset($this->_day)) {
// Format date with YYYY-MM-DD.
$timestamp = gmmktime(0, 0, 0, $this->_month, $this->_day, $this->_year);
$format = variable_get('date_facets_format_ymd', 'm/d/Y');
}
elseif (isset($this->_month)) {
// Format date with YYYY-MM.
$timestamp = gmmktime(0, 0, 0, $this->_month, 1, $this->_year);
$format = variable_get('date_facets_format_ym', 'm/Y');
}
elseif (isset($this->_year)) {
// Format date with YYYY.
$timestamp = gmmktime(0, 0, 0, 1, 1, $this->_year);
$format = variable_get('date_facets_format_y', 'Y');
}
if ($timestamp) {
return format_date($timestamp, 'custom', $format, 0);
}
}
function get_text() {
$text = sprintf('%04d', $this->_year);
if (isset($this->_month)) {
$text .= sprintf('-%02d', $this->_month);
if (isset($this->_day)) {
$text .= sprintf('-%02d', $this->_day);
}
}
return $text;
}
/**
* Updates a query for retrieving the subcategories of this category and their
* associated nodes within the current search results.
*
* This only needs to be overridden for hierarchical facets.
*
* @param $query
* The query object to update.
*
* @return
* FALSE if this facet can't have subcategories.
*/
function build_subcategories_query(&$query) {
$timezone = _date_authored_facet_get_timezone();
if (isset($this->_day)) {
return FALSE; // No subcategories.
}
if (isset($this->_month)) {
$from = sprintf("'%04d-%02d-01 00:00:00'", $this->_year, $this->_month);
$query->add_field(NULL, "YEAR(FROM_UNIXTIME(n.created + $timezone))", 'node_created_year');
$query->add_field(NULL, "MONTH(FROM_UNIXTIME(n.created + $timezone))", 'node_created_month');
$query->add_field(NULL, "DAY(FROM_UNIXTIME(n.created + $timezone))", 'node_created_day');
$query->add_where("(n.created + $timezone >= UNIX_TIMESTAMP($from))");
$query->add_where("(n.created + $timezone < UNIX_TIMESTAMP($from + INTERVAL 1 MONTH))");
$query->add_groupby('node_created_day'); // Needed for counting matching nodes.
return TRUE;
}
if (isset($this->_year)) {
$from = sprintf("'%04d-01-01 00:00:00'", $this->_year);
$query->add_field(NULL, "YEAR(FROM_UNIXTIME(n.created + $timezone))", 'node_created_year');
$query->add_field(NULL, "MONTH(FROM_UNIXTIME(n.created + $timezone))", 'node_created_month');
$query->add_where("(n.created + $timezone >= UNIX_TIMESTAMP($from))");
$query->add_where("(n.created + $timezone < UNIX_TIMESTAMP($from + INTERVAL 1 YEAR))");
$query->add_groupby('node_created_month'); // Needed for counting matching nodes.
return TRUE;
}
return FALSE; // Unreachable, unless something is wrong...
}
/**
* Updates a query for selecting nodes matching this category.
*
* @param $query
* The query object to update.
*/
function build_results_query(&$query) {
$timezone = _date_authored_facet_get_timezone();
if (isset($this->_day)) {
$from = sprintf("'%04d-%02d-%02d 00:00:00'", $this->_year, $this->_month, $this->_day);
$query->add_where("(n.created + $timezone >= UNIX_TIMESTAMP($from))");
$query->add_where("(n.created + $timezone < UNIX_TIMESTAMP($from + INTERVAL 1 DAY))");
}
elseif (isset($this->_month)) {
$from = sprintf("'%04d-%02d-01 00:00:00'", $this->_year, $this->_month);
$query->add_where("(n.created + $timezone >= UNIX_TIMESTAMP($from))");
$query->add_where("(n.created + $timezone < UNIX_TIMESTAMP($from + INTERVAL 1 MONTH))");
}
elseif (isset($this->_year)) {
$from = sprintf("'%04d-01-01 00:00:00'", $this->_year);
$query->add_where("(n.created + $timezone >= UNIX_TIMESTAMP($from))");
$query->add_where("(n.created + $timezone < UNIX_TIMESTAMP($from + INTERVAL 1 YEAR))");
}
}
}
/**
* Returns the applicable timezone offset (in seconds) for date comparisons in
* database queries.
*/
function _date_authored_facet_get_timezone() {
return _date_authored_facet_get_user_timezone() - _date_authored_facet_get_system_timezone();
}
/**
* Returns current user's timezone offset (in seconds).
*/
function _date_authored_facet_get_user_timezone() {
global $user;
if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
return $user->timezone;
}
else {
return variable_get('date_default_timezone', 0);
}
}
/**
* Returns the database system's timezone offset (in seconds).
*
* This hack is used to cancel MySQL's timezone conversions when it converts
* dates to/from Unix timestamps. We could avoid this if MySQL had functions
* that worked directly in UTC (see http://bugs.mysql.com/bug.php?id=28961).
*/
function _date_authored_facet_get_system_timezone() {
static $offset = NULL;
if (!isset($offset)) {
$system = db_result(db_query("SELECT UNIX_TIMESTAMP('2007-01-01 00:00:00')"));
$offset = 1167609600 - $system; // Find offset from the timestamp for 2007-01-01 00:00:00 UTC.
}
return $offset;
}
/**
* Checks the validity of a date or partial date.
*/
function _date_authored_facet_check_date($year = NULL, $month = NULL, $day = NULL) {
if (!isset($year) || !is_numeric($year) || $year < 1) {
return FALSE;
}
if (isset($month) && (!is_numeric($month) || $month < 1 || $month > 12)) {
return FALSE;
}
if (isset($day) && (!isset($month) || !is_numeric($month) || !is_numeric($day) || !checkdate($month, $day, $year))) {
return FALSE;
}
return TRUE;
}