Projet

Général

Profil

Paste
Télécharger (8,22 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_date.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_date.
6
 */
7

    
8
/**
9
 * A handler to provide proper displays for dates.
10
 *
11
 * This may be used on table fields that hold either UNIX timestamps or SQL
12
 * datetime strings.
13
 *
14
 * @ingroup views_field_handlers
15
 */
16
class views_handler_field_date extends views_handler_field {
17

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

    
24
    $options['date_format'] = array('default' => 'small');
25
    $options['custom_date_format'] = array('default' => '');
26
    $options['second_date_format_custom'] = array('default' => '');
27
    $options['second_date_format'] = array('default' => 'small');
28
    $options['timezone'] = array('default' => '');
29

    
30
    return $options;
31
  }
32

    
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public function options_form(&$form, &$form_state) {
37
    $date_formats = array();
38
    $date_types = system_get_date_types();
39
    foreach ($date_types as $key => $value) {
40
      $date_formats[$value['type']] = t('@date_format format', array('@date_format' => $value['title'])) . ': ' . format_date(REQUEST_TIME, $value['type']);
41
    }
42

    
43
    $form['date_format'] = array(
44
      '#type' => 'select',
45
      '#title' => t('Date format'),
46
      '#options' => $date_formats + array(
47
        'custom' => t('Custom'),
48
        'raw time ago' => t('Time ago'),
49
        'time ago' => t('Time ago (with "ago" appended)'),
50
        'today time ago' => t('Time ago (with "ago" appended) for today\'s date, but not for other dates'),
51
        'raw time hence' => t('Time hence'),
52
        'time hence' => t('Time hence (with "hence" appended)'),
53
        'raw time span' => t('Time span (future dates have "-" prepended)'),
54
        'inverse time span' => t('Time span (past dates have "-" prepended)'),
55
        'time span' => t('Time span (with "ago/hence" appended)'),
56
      ),
57
      '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
58
    );
59
    $form['custom_date_format'] = array(
60
      '#type' => 'textfield',
61
      '#title' => t('Custom date format'),
62
      '#description' => t('If "Custom", see the <a href="@url" target="_blank">PHP manual</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.', array('@url' => 'http://php.net/manual/function.date.php')),
63
      '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
64
      '#dependency' => array(
65
        'edit-options-date-format' => $this->supported_date_types(),
66
      ),
67
    );
68
    $form['second_date_format'] = array(
69
      '#type' => 'select',
70
      '#title' => t('Second date format'),
71
      '#options' => $date_formats + array(
72
        'custom' => t('Custom'),
73
      ),
74
      '#description' => t('The date format which will be used for rendering dates other than today.'),
75
      '#default_value' => isset($this->options['second_date_format']) ? $this->options['second_date_format'] : 'small',
76
      '#dependency' => array(
77
        'edit-options-date-format' => array('today time ago'),
78
      ),
79
    );
80
    $form['second_date_format_custom'] = array(
81
      '#type' => 'textfield',
82
      '#title' => t('Custom date format of second date'),
83
      '#description' => t('If "Custom" is selected in "Second date format", see the <a href="@url" target="_blank">PHP manual</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.', array('@url' => 'http://php.net/manual/function.date.php')),
84
      '#default_value' => isset($this->options['second_date_format_custom']) ? $this->options['second_date_format_custom'] : '',
85
      // We have to use states instead of ctools dependency because dependency
86
      // doesn't handle multiple conditions.
87
      '#states' => array(
88
        'visible' => array(
89
          '#edit-options-date-format' => array('value' => 'today time ago'),
90
          '#edit-options-second-date-format' => array('value' => 'custom'),
91
        ),
92
      ),
93
      // We have to use ctools dependency too because states doesn't add the
94
      // correct left margin to the element's wrapper.
95
      '#dependency' => array(
96
        // This condition is handled by form API's states.
97
        // 'edit-options-date-format' => array('today time ago'),
98
        'edit-options-second-date-format' => array('custom'),
99
      ),
100
    );
101
    $form['timezone'] = array(
102
      '#type' => 'select',
103
      '#title' => t('Timezone'),
104
      '#description' => t('Timezone to be used for date output.'),
105
      '#options' => array(
106
        '' => t('- Default site/user timezone -'),
107
      ) + system_time_zones(FALSE),
108
      '#default_value' => $this->options['timezone'],
109
      '#dependency' => array(
110
        'edit-options-date-format' => array_merge(array('custom'), array_keys($date_formats)),
111
      ),
112
    );
113

    
114
    parent::options_form($form, $form_state);
115
  }
116

    
117
  /**
118
   * Provide a list of all of the supported standard date types.
119
   *
120
   * @return array
121
   *   The list of supported formats.
122
   */
123
  private function supported_date_types() {
124
    return array(
125
      'custom',
126
      'raw time ago',
127
      'time ago',
128
      'today time ago',
129
      'raw time hence',
130
      'time hence',
131
      'raw time span',
132
      'time span',
133
      'raw time span',
134
      'inverse time span',
135
      'time span',
136
    );
137
  }
138

    
139
  /**
140
   * {@inheritdoc}
141
   */
142
  public function render($values) {
143
    $value = $this->get_value($values);
144

    
145
    if (!is_numeric($value)) {
146
      // If the value isn't numeric, assume it's an SQL DATETIME.
147
      $value = strtotime($value);
148
    }
149

    
150
    $format = $this->options['date_format'];
151
    if (in_array($format, $this->supported_date_types())) {
152
      $custom_format = $this->options['custom_date_format'];
153
    }
154

    
155
    if ($value) {
156
      $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
157
      // Will be positive for a datetime in the past (ago), and negative for a
158
      // datetime in the future (hence).
159
      $time_diff = REQUEST_TIME - $value;
160
      switch ($format) {
161
        case 'raw time ago':
162
          return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
163

    
164
        case 'time ago':
165
          $t_args = array(
166
            '%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2),
167
          );
168
          return t('%time ago', $t_args);
169

    
170
        case 'today time ago':
171
          $second_format = $this->options['second_date_format'];
172
          $second_custom_format = $this->options['second_date_format_custom'];
173
          if (format_date(REQUEST_TIME, 'custom', 'Y-m-d', $timezone) == format_date($value, 'custom', 'Y-m-d', $timezone)) {
174
            return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
175
          }
176
          elseif ($second_format == 'custom') {
177
            if ($second_custom_format == 'r') {
178
              return format_date($value, $second_format, $second_custom_format, $timezone, 'en');
179
            }
180
            return format_date($value, $second_format, $second_custom_format, $timezone);
181
          }
182
          else {
183
            return format_date($value, $this->options['second_date_format'], '', $timezone);
184
          }
185

    
186
        case 'raw time hence':
187
          return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
188

    
189
        case 'time hence':
190
          return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2)));
191

    
192
        case 'raw time span':
193
          return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
194

    
195
        case 'inverse time span':
196
          return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
197

    
198
        case 'time span':
199
          return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
200

    
201
        case 'custom':
202
          if ($custom_format == 'r') {
203
            return format_date($value, $format, $custom_format, $timezone, 'en');
204
          }
205
          return format_date($value, $format, $custom_format, $timezone);
206

    
207
        default:
208
          return format_date($value, $format, '', $timezone);
209
      }
210
    }
211
  }
212

    
213
}