Projet

Général

Profil

Paste
Télécharger (2,34 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_sort_date.inc @ 5d12d676

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 5d12d676 Assos Assos
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public function option_definition() {
22 85ad3d82 Assos Assos
    $options = parent::option_definition();
23
24
    $options['granularity'] = array('default' => 'second');
25
26
    return $options;
27
  }
28
29 5d12d676 Assos Assos
  /**
30
   * {@inheritdoc}
31
   */
32
  public function options_form(&$form, &$form_state) {
33 85ad3d82 Assos Assos
    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 5d12d676 Assos Assos
   * {@inheritdoc}
53 85ad3d82 Assos Assos
   */
54 5d12d676 Assos Assos
  public function query() {
55 85ad3d82 Assos Assos
    $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 5d12d676 Assos Assos
62 85ad3d82 Assos Assos
      case 'minute':
63
        $formula = views_date_sql_format('YmdHi', "$this->table_alias.$this->real_field");
64
        break;
65 5d12d676 Assos Assos
66 85ad3d82 Assos Assos
      case 'hour':
67
        $formula = views_date_sql_format('YmdH', "$this->table_alias.$this->real_field");
68
        break;
69 5d12d676 Assos Assos
70 85ad3d82 Assos Assos
      case 'day':
71
        $formula = views_date_sql_format('Ymd', "$this->table_alias.$this->real_field");
72
        break;
73 5d12d676 Assos Assos
74 85ad3d82 Assos Assos
      case 'month':
75
        $formula = views_date_sql_format('Ym', "$this->table_alias.$this->real_field");
76
        break;
77 5d12d676 Assos Assos
78 85ad3d82 Assos Assos
      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 5d12d676 Assos Assos
87 85ad3d82 Assos Assos
}