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