1 |
85ad3d82
|
Assos Assos
|
<?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 |
6eb8d15f
|
Assos Assos
|
$options['second_date_format_custom'] = array('default' => '');
|
20 |
|
|
$options['second_date_format'] = array('default' => 'small');
|
21 |
85ad3d82
|
Assos Assos
|
$options['timezone'] = array('default' => '');
|
22 |
|
|
|
23 |
|
|
return $options;
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
function options_form(&$form, &$form_state) {
|
27 |
|
|
|
28 |
|
|
$date_formats = array();
|
29 |
|
|
$date_types = system_get_date_types();
|
30 |
|
|
foreach ($date_types as $key => $value) {
|
31 |
|
|
$date_formats[$value['type']] = t('@date_format format', array('@date_format' => $value['title'])) . ': ' . format_date(REQUEST_TIME, $value['type']);
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
$form['date_format'] = array(
|
35 |
|
|
'#type' => 'select',
|
36 |
|
|
'#title' => t('Date format'),
|
37 |
|
|
'#options' => $date_formats + array(
|
38 |
|
|
'custom' => t('Custom'),
|
39 |
|
|
'raw time ago' => t('Time ago'),
|
40 |
|
|
'time ago' => t('Time ago (with "ago" appended)'),
|
41 |
6eb8d15f
|
Assos Assos
|
'today time ago' => t('Time ago (with "ago" appended) for today\'s date, but not for other dates'),
|
42 |
85ad3d82
|
Assos Assos
|
'raw time hence' => t('Time hence'),
|
43 |
|
|
'time hence' => t('Time hence (with "hence" appended)'),
|
44 |
|
|
'raw time span' => t('Time span (future dates have "-" prepended)'),
|
45 |
|
|
'inverse time span' => t('Time span (past dates have "-" prepended)'),
|
46 |
|
|
'time span' => t('Time span (with "ago/hence" appended)'),
|
47 |
|
|
),
|
48 |
|
|
'#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
|
49 |
|
|
);
|
50 |
|
|
$form['custom_date_format'] = array(
|
51 |
|
|
'#type' => 'textfield',
|
52 |
|
|
'#title' => t('Custom date format'),
|
53 |
|
|
'#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 |
|
|
'#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
|
55 |
6eb8d15f
|
Assos Assos
|
'#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')),
|
56 |
85ad3d82
|
Assos Assos
|
);
|
57 |
6eb8d15f
|
Assos Assos
|
$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 |
|
|
);
|
88 |
85ad3d82
|
Assos Assos
|
$form['timezone'] = array(
|
89 |
|
|
'#type' => 'select',
|
90 |
|
|
'#title' => t('Timezone'),
|
91 |
|
|
'#description' => t('Timezone to be used for date output.'),
|
92 |
|
|
'#options' => array('' => t('- Default site/user timezone -')) + system_time_zones(FALSE),
|
93 |
|
|
'#default_value' => $this->options['timezone'],
|
94 |
|
|
'#dependency' => array('edit-options-date-format' => array_merge(array('custom'), array_keys($date_formats))),
|
95 |
|
|
);
|
96 |
|
|
|
97 |
|
|
parent::options_form($form, $form_state);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
function render($values) {
|
101 |
|
|
$value = $this->get_value($values);
|
102 |
|
|
$format = $this->options['date_format'];
|
103 |
6eb8d15f
|
Assos Assos
|
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'))) {
|
104 |
85ad3d82
|
Assos Assos
|
$custom_format = $this->options['custom_date_format'];
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
if ($value) {
|
108 |
|
|
$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)
|
110 |
|
|
switch ($format) {
|
111 |
|
|
case 'raw time ago':
|
112 |
|
|
return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
|
113 |
|
|
case 'time ago':
|
114 |
|
|
return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
|
115 |
6eb8d15f
|
Assos Assos
|
case 'today time ago':
|
116 |
|
|
$second_format = $this->options['second_date_format'];
|
117 |
|
|
$second_custom_format = $this->options['second_date_format_custom'];
|
118 |
|
|
if (format_date(REQUEST_TIME, 'custom', 'Y-m-d', $timezone) == format_date($value, 'custom', 'Y-m-d', $timezone)) {
|
119 |
|
|
return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
|
120 |
|
|
}
|
121 |
|
|
elseif ($second_format == 'custom') {
|
122 |
|
|
if ($second_custom_format == 'r') {
|
123 |
|
|
return format_date($value, $second_format, $second_custom_format, $timezone, 'en');
|
124 |
|
|
}
|
125 |
|
|
return format_date($value, $second_format, $second_custom_format, $timezone);
|
126 |
|
|
}
|
127 |
|
|
else {
|
128 |
|
|
return format_date($value, $this->options['second_date_format'], '', $timezone);
|
129 |
|
|
}
|
130 |
85ad3d82
|
Assos Assos
|
case 'raw time hence':
|
131 |
|
|
return format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2);
|
132 |
|
|
case 'time hence':
|
133 |
|
|
return t('%time hence', array('%time' => format_interval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2)));
|
134 |
|
|
case 'raw time span':
|
135 |
|
|
return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
|
136 |
|
|
case 'inverse time span':
|
137 |
|
|
return ($time_diff > 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
|
138 |
|
|
case 'time span':
|
139 |
|
|
return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
|
140 |
|
|
case 'custom':
|
141 |
|
|
if ($custom_format == 'r') {
|
142 |
|
|
return format_date($value, $format, $custom_format, $timezone, 'en');
|
143 |
|
|
}
|
144 |
|
|
return format_date($value, $format, $custom_format, $timezone);
|
145 |
|
|
default:
|
146 |
|
|
return format_date($value, $format, '', $timezone);
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
|
|
}
|
150 |
|
|
} |