Projet

Général

Profil

Paste
Télécharger (7,99 ko) Statistiques
| Branche: | Révision:

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

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
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_date extends views_handler_field {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    $options['date_format'] = array('default' => 'small');
22
    $options['custom_date_format'] = array('default' => '');
23
    $options['second_date_format_custom'] = array('default' => '');
24
    $options['second_date_format'] = array('default' => 'small');
25
    $options['timezone'] = array('default' => '');
26

    
27
    return $options;
28
  }
29

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

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

    
111
    parent::options_form($form, $form_state);
112
  }
113

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

    
136
  /**
137
   * {@inheritdoc}
138
   */
139
  public function render($values) {
140
    $value = $this->get_value($values);
141
    $format = $this->options['date_format'];
142
    if (in_array($format, $this->supported_date_types())) {
143
      $custom_format = $this->options['custom_date_format'];
144
    }
145

    
146
    if ($value) {
147
      $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
148
      // Will be positive for a datetime in the past (ago), and negative for a
149
      // datetime in the future (hence).
150
      $time_diff = REQUEST_TIME - $value;
151
      switch ($format) {
152
        case 'raw time ago':
153
          return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
154

    
155
        case 'time ago':
156
          $t_args = array(
157
            '%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2),
158
          );
159
          return t('%time ago', $t_args);
160

    
161
        case 'today time ago':
162
          $second_format = $this->options['second_date_format'];
163
          $second_custom_format = $this->options['second_date_format_custom'];
164
          if (format_date(REQUEST_TIME, 'custom', 'Y-m-d', $timezone) == format_date($value, 'custom', 'Y-m-d', $timezone)) {
165
            return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
166
          }
167
          elseif ($second_format == 'custom') {
168
            if ($second_custom_format == 'r') {
169
              return format_date($value, $second_format, $second_custom_format, $timezone, 'en');
170
            }
171
            return format_date($value, $second_format, $second_custom_format, $timezone);
172
          }
173
          else {
174
            return format_date($value, $this->options['second_date_format'], '', $timezone);
175
          }
176

    
177
        case 'raw time hence':
178
          return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
179

    
180
        case 'time hence':
181
          return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2)));
182

    
183
        case 'raw time span':
184
          return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
185

    
186
        case 'inverse time span':
187
          return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
188

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

    
192
        case 'custom':
193
          if ($custom_format == 'r') {
194
            return format_date($value, $format, $custom_format, $timezone, 'en');
195
          }
196
          return format_date($value, $format, $custom_format, $timezone);
197

    
198
        default:
199
          return format_date($value, $format, '', $timezone);
200
      }
201
    }
202
  }
203

    
204
}