Project

General

Profile

Paste
Download (2.34 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_sort_date.inc @ 8be7bf84

1
<?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

    
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public function option_definition() {
22
    $options = parent::option_definition();
23

    
24
    $options['granularity'] = array('default' => 'second');
25

    
26
    return $options;
27
  }
28

    
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function options_form(&$form, &$form_state) {
33
    parent::options_form($form, $form_state);
34

    
35
    $form['granularity'] = array(
36
      '#type' => 'radios',
37
      '#title' => t('Granularity'),
38
      '#options' => array(
39
        'second' => t('Second'),
40
        'minute' => t('Minute'),
41
        'hour'   => t('Hour'),
42
        'day'    => t('Day'),
43
        'month'  => t('Month'),
44
        'year'   => t('Year'),
45
      ),
46
      '#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.'),
47
      '#default_value' => $this->options['granularity'],
48
    );
49
  }
50

    
51
  /**
52
   * {@inheritdoc}
53
   */
54
  public function query() {
55
    $this->ensure_my_table();
56
    switch ($this->options['granularity']) {
57
      case 'second':
58
      default:
59
        $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
60
        return;
61

    
62
      case 'minute':
63
        $formula = views_date_sql_format('YmdHi', "$this->table_alias.$this->real_field");
64
        break;
65

    
66
      case 'hour':
67
        $formula = views_date_sql_format('YmdH', "$this->table_alias.$this->real_field");
68
        break;
69

    
70
      case 'day':
71
        $formula = views_date_sql_format('Ymd', "$this->table_alias.$this->real_field");
72
        break;
73

    
74
      case 'month':
75
        $formula = views_date_sql_format('Ym', "$this->table_alias.$this->real_field");
76
        break;
77

    
78
      case 'year':
79
        $formula = views_date_sql_format('Y', "$this->table_alias.$this->real_field");
80
        break;
81
    }
82

    
83
    // Add the field.
84
    $this->query->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field . '_' . $this->options['granularity']);
85
  }
86

    
87
}