Projet

Général

Profil

Paste
Télécharger (4,71 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / views / handlers / views_handler_field_date.inc @ 4543c6c7

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
  function option_definition() {
15
    $options = parent::option_definition();
16

    
17
    $options['date_format'] = array('default' => 'small');
18
    $options['custom_date_format'] = array('default' => '');
19
    $options['timezone'] = array('default' => '');
20

    
21
    return $options;
22
  }
23

    
24
  function options_form(&$form, &$form_state) {
25

    
26
    $date_formats = array();
27
    $date_types = system_get_date_types();
28
    foreach ($date_types as $key => $value) {
29
      $date_formats[$value['type']] = t('@date_format format', array('@date_format' => $value['title'])) . ': ' . format_date(REQUEST_TIME, $value['type']);
30
    }
31

    
32
    $form['date_format'] = array(
33
      '#type' => 'select',
34
      '#title' => t('Date format'),
35
      '#options' => $date_formats + array(
36
        'custom' => t('Custom'),
37
        'raw time ago' => t('Time ago'),
38
        'time ago' => t('Time ago (with "ago" appended)'),
39
        'raw time hence' => t('Time hence'),
40
        'time hence' => t('Time hence (with "hence" appended)'),
41
        'raw time span' => t('Time span (future dates have "-" prepended)'),
42
        'inverse time span' => t('Time span (past dates have "-" prepended)'),
43
        'time span' => t('Time span (with "ago/hence" appended)'),
44
      ),
45
      '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
46
    );
47
    $form['custom_date_format'] = array(
48
      '#type' => 'textfield',
49
      '#title' => t('Custom date format'),
50
      '#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')),
51
      '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
52
      '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span')),
53
    );
54
    $form['timezone'] = array(
55
      '#type' => 'select',
56
      '#title' => t('Timezone'),
57
      '#description' => t('Timezone to be used for date output.'),
58
      '#options' => array('' => t('- Default site/user timezone -')) + system_time_zones(FALSE),
59
      '#default_value' => $this->options['timezone'],
60
      '#dependency' => array('edit-options-date-format' => array_merge(array('custom'), array_keys($date_formats))),
61
    );
62

    
63
    parent::options_form($form, $form_state);
64
  }
65

    
66
  function render($values) {
67
    $value = $this->get_value($values);
68
    $format = $this->options['date_format'];
69
    if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) {
70
      $custom_format = $this->options['custom_date_format'];
71
    }
72

    
73
    if ($value) {
74
      $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
75
      $time_diff = REQUEST_TIME - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
76
      switch ($format) {
77
        case 'raw time ago':
78
          return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
79
        case 'time ago':
80
          return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
81
        case 'raw time hence':
82
          return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
83
        case 'time hence':
84
          return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2)));
85
        case 'raw time span':
86
          return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
87
        case 'inverse time span':
88
          return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
89
        case 'time span':
90
          return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
91
        case 'custom':
92
          if ($custom_format == 'r') {
93
            return format_date($value, $format, $custom_format, $timezone, 'en');
94
          }
95
          return format_date($value, $format, $custom_format, $timezone);
96
        default:
97
          return format_date($value, $format, '', $timezone);
98
      }
99
    }
100
  }
101
}