Projet

Général

Profil

Révision 5d12d676

Ajouté par Assos Assos il y a environ 6 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/views/handlers/views_handler_field_date.inc
11 11
 * @ingroup views_field_handlers
12 12
 */
13 13
class views_handler_field_date extends views_handler_field {
14
  function option_definition() {
14

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

  
17 21
    $options['date_format'] = array('default' => 'small');
......
23 27
    return $options;
24 28
  }
25 29

  
26
  function options_form(&$form, &$form_state) {
27

  
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function options_form(&$form, &$form_state) {
28 34
    $date_formats = array();
29 35
    $date_types = system_get_date_types();
30 36
    foreach ($date_types as $key => $value) {
......
52 58
      '#title' => t('Custom date format'),
53 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')),
54 60
      '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
55
      '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'today time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span')),
61
      '#dependency' => array(
62
        'edit-options-date-format' => $this->supported_date_types(),
63
      ),
56 64
    );
57 65
    $form['second_date_format'] = array(
58
     '#type' => 'select',
59
     '#title' => t('Second date format'),
60
     '#options' => $date_formats + array(
61
       'custom' => t('Custom'),
62
     ),
63
     '#description' => t('The date format which will be used for rendering dates other than today.'),
64
     '#default_value' => isset($this->options['second_date_format']) ? $this->options['second_date_format'] : 'small',
65
     '#dependency' => array('edit-options-date-format' => array('today time ago')),
66
   );
67
   $form['second_date_format_custom'] = array(
68
     '#type' => 'textfield',
69
     '#title' => t('Custom date format of second date'),
70
     '#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')),
71
     '#default_value' => isset($this->options['second_date_format_custom']) ? $this->options['second_date_format_custom'] : '',
72
     // We have to use states instead of ctools dependency because dependency
73
     // doesn't handle multiple conditions.
74
     '#states' => array(
75
       'visible' => array(
76
         '#edit-options-date-format' => array('value' => 'today time ago'),
77
         '#edit-options-second-date-format' => array('value' => 'custom'),
78
       ),
79
     ),
80
     // We have to use ctools dependency too because states doesn't add the
81
     // correct left margin to the element's wrapper.
82
     '#dependency' => array(
83
       // This condition is handled by form API's states.
84
//        'edit-options-date-format' => array('today time ago'),
85
       'edit-options-second-date-format' => array('custom'),
86
     ),
87
   );
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
    );
88 98
    $form['timezone'] = array(
89 99
      '#type' => 'select',
90 100
      '#title' => t('Timezone'),
91 101
      '#description' => t('Timezone to be used for date output.'),
92
      '#options' => array('' => t('- Default site/user timezone -')) + system_time_zones(FALSE),
102
      '#options' => array(
103
        '' => t('- Default site/user timezone -'),
104
      ) + system_time_zones(FALSE),
93 105
      '#default_value' => $this->options['timezone'],
94
      '#dependency' => array('edit-options-date-format' => array_merge(array('custom'), array_keys($date_formats))),
106
      '#dependency' => array(
107
        'edit-options-date-format' => array_merge(array('custom'), array_keys($date_formats)),
108
      ),
95 109
    );
96 110

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

  
100
  function render($values) {
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) {
101 140
    $value = $this->get_value($values);
102 141
    $format = $this->options['date_format'];
103
    if (in_array($format, array('custom', 'raw time ago', 'time ago', 'today time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) {
142
    if (in_array($format, $this->supported_date_types())) {
104 143
      $custom_format = $this->options['custom_date_format'];
105 144
    }
106 145

  
107 146
    if ($value) {
108 147
      $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
109
      $time_diff = REQUEST_TIME - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
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;
110 151
      switch ($format) {
111 152
        case 'raw time ago':
112 153
          return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
154

  
113 155
        case 'time ago':
114
          return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
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

  
115 161
        case 'today time ago':
116 162
          $second_format = $this->options['second_date_format'];
117 163
          $second_custom_format = $this->options['second_date_format_custom'];
......
127 173
          else {
128 174
            return format_date($value, $this->options['second_date_format'], '', $timezone);
129 175
          }
176

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

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

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

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

  
138 189
        case 'time span':
139 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

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

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

  
150 204
}

Formats disponibles : Unified diff