00001 <?php
00002
00011 class views_handler_sort_date extends views_handler_sort {
00012 function option_definition() {
00013 $options = parent::option_definition();
00014
00015 $options['granularity'] = array('default' => 'second');
00016
00017 return $options;
00018 }
00019
00020 function options_form(&$form, &$form_state) {
00021 parent::options_form($form, $form_state);
00022
00023 $form['granularity'] = array(
00024 '#type' => 'radios',
00025 '#title' => t('Granularity'),
00026 '#options' => array(
00027 'second' => t('Second'),
00028 'minute' => t('Minute'),
00029 'hour' => t('Hour'),
00030 'day' => t('Day'),
00031 'month' => t('Month'),
00032 'year' => t('Year'),
00033 ),
00034 '#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.'),
00035 '#default_value' => $this->options['granularity'],
00036 );
00037 }
00038
00042 function query() {
00043 $this->ensure_my_table();
00044 switch ($this->options['granularity']) {
00045 case 'second':
00046 default:
00047 $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
00048 return;
00049 case 'minute':
00050 $formula = views_date_sql_format('YmdHi', "$this->table_alias.$this->real_field");
00051 break;
00052 case 'hour':
00053 $formula = views_date_sql_format('YmdH', "$this->table_alias.$this->real_field");
00054 break;
00055 case 'day':
00056 $formula = views_date_sql_format('Ymd', "$this->table_alias.$this->real_field");
00057 break;
00058 case 'month':
00059 $formula = views_date_sql_format('Ym', "$this->table_alias.$this->real_field");
00060 break;
00061 case 'year':
00062 $formula = views_date_sql_format('Y', "$this->table_alias.$this->real_field");
00063 break;
00064 }
00065
00066
00067 $this->query->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
00068 }
00069 }