1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Definition of views_handler_sort_date.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Basic sort handler for dates.
|
10 |
|
|
*
|
11 |
|
|
* This handler enables granularity, which is the ability to make dates
|
12 |
|
|
* equivalent based upon nearness.
|
13 |
|
|
*
|
14 |
|
|
* @ingroup views_sort_handlers
|
15 |
|
|
*/
|
16 |
|
|
class views_handler_sort_date extends views_handler_sort {
|
17 |
|
|
function option_definition() {
|
18 |
|
|
$options = parent::option_definition();
|
19 |
|
|
|
20 |
|
|
$options['granularity'] = array('default' => 'second');
|
21 |
|
|
|
22 |
|
|
return $options;
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
function options_form(&$form, &$form_state) {
|
26 |
|
|
parent::options_form($form, $form_state);
|
27 |
|
|
|
28 |
|
|
$form['granularity'] = array(
|
29 |
|
|
'#type' => 'radios',
|
30 |
|
|
'#title' => t('Granularity'),
|
31 |
|
|
'#options' => array(
|
32 |
|
|
'second' => t('Second'),
|
33 |
|
|
'minute' => t('Minute'),
|
34 |
|
|
'hour' => t('Hour'),
|
35 |
|
|
'day' => t('Day'),
|
36 |
|
|
'month' => t('Month'),
|
37 |
|
|
'year' => t('Year'),
|
38 |
|
|
),
|
39 |
|
|
'#description' => t('The granularity is the smallest unit to use when determining whether two dates are the same; for example, if the granularity is "Year" then all dates in 1999, regardless of when they fall in 1999, will be considered the same date.'),
|
40 |
|
|
'#default_value' => $this->options['granularity'],
|
41 |
|
|
);
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* Called to add the sort to a query.
|
46 |
|
|
*/
|
47 |
|
|
function query() {
|
48 |
|
|
$this->ensure_my_table();
|
49 |
|
|
switch ($this->options['granularity']) {
|
50 |
|
|
case 'second':
|
51 |
|
|
default:
|
52 |
|
|
$this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
|
53 |
|
|
return;
|
54 |
|
|
case 'minute':
|
55 |
|
|
$formula = views_date_sql_format('YmdHi', "$this->table_alias.$this->real_field");
|
56 |
|
|
break;
|
57 |
|
|
case 'hour':
|
58 |
|
|
$formula = views_date_sql_format('YmdH', "$this->table_alias.$this->real_field");
|
59 |
|
|
break;
|
60 |
|
|
case 'day':
|
61 |
|
|
$formula = views_date_sql_format('Ymd', "$this->table_alias.$this->real_field");
|
62 |
|
|
break;
|
63 |
|
|
case 'month':
|
64 |
|
|
$formula = views_date_sql_format('Ym', "$this->table_alias.$this->real_field");
|
65 |
|
|
break;
|
66 |
|
|
case 'year':
|
67 |
|
|
$formula = views_date_sql_format('Y', "$this->table_alias.$this->real_field");
|
68 |
|
|
break;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
// Add the field.
|
72 |
|
|
$this->query->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
|
73 |
|
|
}
|
74 |
|
|
} |