-
Notifications
You must be signed in to change notification settings - Fork 1
/
thai_date_util.module
executable file
·368 lines (335 loc) · 14.3 KB
/
thai_date_util.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
function thai_date_util_views_api() {
return array(
'api' => 2,
);
}
function thai_date_util_field_formatter_info() {
/* Buggy - leave it
$formatters = array(
'buddhist_default' => array('label' => t('(Buddhist) Default'),
'field types' => array('date', 'datestamp', 'datetime'),
'multiple values' => CONTENT_HANDLE_CORE),
);
*/
$formatters = array();
$format_types = date_get_format_types('', TRUE);
if (!empty($format_types)) {
foreach ($format_types as $type => $type_info) {
$formatters['buddhist_' . $type] = array(
'label' => '(Buddhist) ' . $type_info['title'],
'field types' => array('date', 'datestamp', 'datetime'),
'multiple values' => CONTENT_HANDLE_CORE,
);
}
}
return $formatters;
}
function thai_date_util_theme() {
$themes = array(
'thai_date_util_formatter_buddhist_default' => array(
'arguments' => array('element' => NULL),
'function' => 'theme_thai_date_util_display_combination',
),
);
if (db_table_exists('date_format_types')) {
$format_types = date_get_format_types('', TRUE);
if (!empty($format_types)) {
foreach ($format_types as $type => $type_info) {
$themes['thai_date_util_formatter_buddhist_' . $type] = array(
'arguments' => array('element' => NULL),
'function' => 'theme_thai_date_util_display_combination',
);
}
}
}
return $themes;
}
function thai_date_util_menu() {
return array(
'admin/settings/thai_date_util' => array(
'title' => t('Thai-buddhist Date Utility'),
'description' => t('Setup defaults and override how module work with system.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('thai_date_util_settings'),
'access arguments' => array('administer site configuration'),
));
}
function thai_date_util_settings(&$form_state) {
$form['thai_date_util_default_format'] = array(
'#title' => t('Default time format'),
'#description' => t('See !link for date formats.', array('!link' => l('the PHP docs', 'http://us.php.net/manual/en/function.date.php'))),
'#type' => 'textfield',
'#default_value' => variable_get('thai_date_util_default_format', 'j F Y H:i'),
);
$form['thai_date_util_node_era_type'] = array(
'#type' => 'select',
'#options' => array('buddhist' => t('Buddhist Era'), 'christmas' => t('Christmas Era')),
'#default_value' => variable_get('thai_date_util_node_era_type', 'buddhist'),
);
$form['thai_date_util_node_date_format'] = array(
'#title' => t('Override node date format'),
'#description' => t('By default this module override date format in node page with hook_preprocess_node(), for themes which have own custom format, you can disable this option.'),
'#type' => 'checkbox',
'#default_value' => variable_get('thai_date_util_node_date_format', true),
);
return system_settings_form($form);
}
function theme_thai_date_util_display_combination($element) {
static $repeating_ids = array();
$node = $element['#node'];
$field_name = $element['#field_name'];
$context = !empty($node->content) && !empty($node->content[$field_name]) ? $node->content[$field_name]['#context'] : 'full';
$type_name = $element['#type_name'];
$fields = content_fields();
$field = $fields[$field_name];
$item = $element['#item'];
// Get the formatter settings, either the default settings for this node
// type or the View settings stored in $node->date_info.
$options = date_formatter_get_settings($field_name, $type_name, $context);
if (!empty($node->date_info) && !empty($node->date_info->formatter_settings)) {
$options = $node->date_info->formatter_settings;
}
$output = '';
// If date_id is set for this field and the delta doesn't match, don't display it.
if (!empty($node->date_id)) {
foreach ((array) $node->date_id as $key => $id) {
list($module, $nid, $field_name, $delta, $other) = explode('.', $id);
if ($field_name == $field['field_name'] && isset($item['#delta']) && $delta != $item['#delta']) {
return $output;
}
}
}
// Check the formatter settings to see if the repeat rule should be
// displayed. Show it only with the first multiple value date.
if (!in_array($node->nid, $repeating_ids) && module_exists('date_repeat')
&& !empty($item['rrule']) && $options['repeat']['show_repeat_rule'] == 'show') {
require_once('./' . drupal_get_path('module', 'date') . '/date_repeat.inc');
$output .= theme('date_repeat_display', $field, $item, $node);
$repeating_ids[] = $node->nid;
}
// If this is a full node or a pseudo node created by grouping
// multiple values, see exactly which values are supposed to be visible.
if (isset($node->$field_name)) {
$node = date_prepare_node($node, $field, $type_name, $context, $options);
// Did the current value get removed by formatter settings?
if (empty($node->{$field_name}[$item['#delta']])) {
return $output;
}
// Adjust the $element values to match the changes.
$element['#node'] = $node;
}
// Call the right theme for this formatter.
// Update the element with values that might have been altered by
// date_prepare_node() and figure out which values to display.
$dates = thai_date_util_formatter_process($element);
switch ($options['fromto']['fromto']) {
case 'value':
$date1 = $dates['value']['formatted'];
$date2 = $date1;
break;
case 'value2':
$date2 = $dates['value2']['formatted'];
$date1 = $date2;
break;
default:
$date1 = $dates['value']['formatted'];
$date2 = $dates['value2']['formatted'];
break;
}
// Pull the timezone, if any, out of the formatted result and tack it
// back on at the end, if it is in the current formatted date.
$timezone = $dates['value']['formatted_timezone'];
if ($timezone) {
$timezone = ' ' . $timezone;
}
$date1 = str_replace($timezone, '', $date1);
$date2 = str_replace($timezone, '', $date2);
// No date values, display nothing.
if (empty($date1) && empty($date2)) {
$output .= '';
}
// From and To dates match or there is no To date, display a complete single date.
elseif ($date1 == $date2 || empty($date2)) {
$output .= theme('date_display_single', $date1, $timezone);
}
// Same day, different times, don't repeat the date but show both From and To times.
elseif (date_has_time($field['granularity']) && $dates['value']['formatted_date'] == $dates['value2']['formatted_date']) {
// Replace the original time with the from/to time in the formatted start date.
// Make sure that parentheses or brackets wrapping the time will be retained in the
// final result.
$time1 = preg_replace('`^([\(\[])`', '', $dates['value']['formatted_time']);
$time1 = preg_replace('([\)\]]$)', '', $time1);
$time2 = preg_replace('`^([\(\[])`', '', $dates['value2']['formatted_time']);
$time2 = preg_replace('([\)\]]$)', '', $time2);
$time = theme('date_display_range', $time1, $time2);
$replaced = str_replace($time1, $time, $date1);
$output .= theme('date_display_single', $replaced, $timezone);
}
// Different days, display both in their entirety.
else {
$output .= theme('date_display_range', $date1, $date2, $timezone);
}
return $output;
}
/**
* Replacement for date_formatter_process().
* It would be used only user select Buddhist era date.
*/
function thai_date_util_formatter_process($element) {
$node = $element['#node'];
$dates = array();
$timezone = date_default_timezone_name();
if (empty($timezone)) {
return $dates;
}
$field_name = $element['#field_name'];
$fields = content_fields();
$field = $fields[$field_name];
$formatter = $element['#formatter'];
$formatter = array_pop(explode('buddhist_', $formatter));
$format = date_formatter_format($formatter, $field_name);
$item = $element['#item'];
$timezone = isset($item['timezone']) ? $item['timezone'] : '';
$timezone = date_get_timezone($field['tz_handling'], $timezone);
$timezone_db = date_get_timezone_db($field['tz_handling']);
$process = date_process_values($field);
foreach ($process as $processed) {
if (empty($item[$processed])) {
$dates[$processed] = NULL;
}
else {
// create a date object with a gmt timezone from the database value
$value = $item[$processed];
if ($field['type'] == DATE_ISO) {
$value = str_replace(' ', 'T', date_fuzzy_datetime($value));
}
$date = date_make_date($value, $timezone_db, $field['type'], $field['granularity']);
$dates[$processed] = array();
$dates[$processed]['db']['object'] = $date;
$dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
date_timezone_set($date, timezone_open($timezone));
$dates[$processed]['local']['object'] = $date;
$dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
$dates[$processed]['local']['timezone'] = $timezone;
$dates[$processed]['local']['offset'] = date_offset_get($date);
//format the date, special casing the 'interval' format which doesn't need to be processed
$dates[$processed]['formatted'] = '';
if (is_object($date)) {
if ($format == 'format_interval') {
$dates[$processed]['interval'] = date_format_interval($date);
}
elseif ($format == 'format_calendar_day') {
$dates[$processed]['calendar_day'] = date_format_calendar_day($date);
}
elseif ($format == 'U') {
$dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
$dates[$processed]['formatted_date'] = date_format_date($date, 'custom', $format);
$dates[$processed]['formatted_time'] = '';
$dates[$processed]['formatted_timezone'] = '';
}
elseif (!empty($format)) {
$era_type = variable_get('thai_date_util_node_era_type', 'buddhist');
$dates[$processed]['formatted_date'] = thai_date_util_format($date, 'custom', date_limit_format($format, array('year', 'month', 'day')), $era_type);
$dates[$processed]['formatted_time'] = thai_date_util_format($date, 'custom', date_limit_format($format, array('hour', 'minute', 'second')), $era_type);
$dates[$processed]['formatted_timezone'] = thai_date_util_format($date, 'custom', date_limit_format($format, array('timezone')), $era_type);
$dates[$processed]['formatted'] = thai_date_util_format($date, 'custom', $format, $era_type);
}
}
}
}
if (empty($dates['value2'])) {
$dates['value2'] = $dates['value'];
}
$date1 = $dates['value']['local']['object'];
$date2 = $dates['value2']['local']['object'];
$all_day = '';
$all_day2 = '';
if ($format != 'format_interval') {
$all_day1 = theme('date_all_day', $field, 'date1', $date1, $date2, $format, $node);
$all_day2 = theme('date_all_day', $field, 'date2', $date1, $date2, $format, $node);
}
if ((!empty($all_day1) && $all_day1 != $dates['value']['formatted'])
|| (!empty($all_day2) && $all_day2 != $dates['value2']['formatted'])) {
$dates['value']['formatted_time'] = theme('date_all_day_label');
$dates['value2']['formatted_time'] = theme('date_all_day_label');
$dates['value']['formatted'] = $all_day1;
$dates['value2']['formatted'] = $all_day2;
}
$dates['format'] = $format;
return $dates;
}
/**
* Use theme_preprocess_node() to avoid conflict with themes.
*/
function thai_date_util_preprocess_node(&$vars) {
if (variable_get('thai_date_util_node_date_format', true)) {
$era_type = variable_get('thai_date_util_node_era_type', 'buddhist');
$format = 'j F Y H:i';
$vars['submitted'] = t('Submitted by !username on @datetime',
array(
'!username' => theme('username', $vars['node']),
'@datetime' => thai_date_util_format($vars['node']->created, 'custom', $format, $era_type),
));
}
}
/**
* API function to use overall module's features.
* Note: $custom_format use reference to forward updated format to formatter in CCK.
*/
function thai_date_util_format($value, $format, &$custom_format, $era_type = 'buddhist') {
$time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
switch ($format) {
case 'custom':
if ($custom_format == 'r') {
return format_date($value, $format, $custom_format, null, 'en');
//return format_date($value, $format, $custom_format);
}
$format = $custom_format;
break;
default:
switch ($format) {
case 'small':
$format = variable_get('date_format_short', 'm/d/Y - H:i');
break;
case 'large':
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
break;
case 'custom':
// No change to format.
break;
case 'raw time ago':
return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2);
case 'time ago':
return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2)));
case 'raw time span':
return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2);
case 'time span':
return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
case 'medium':
default:
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
}
}
// Thai date
if (is_object($value)) {
$date_object = date_make_date($value);
} else {
$value = date('Y-m-d H:i:s', $value);
$date_object = date_make_date($value); // $date_raw => date() or UTC formatted date
}
date_timezone_set($date_object, timezone_open(date_default_timezone_name(TRUE)));
$formatted_date = explode(' ',date_format_date($date_object,'custom', 'o Y'));
if ($era_type == 'buddhist') {
$formatted_date[0] = 543 + $formatted_date[0];
$formatted_date[1] = 543 + $formatted_date[1];
}
$formatted_date[2] = substr($formatted_date[1], 2);
$format = str_replace(array('o', 'Y', 'y'),
array($formatted_date[0], $formatted_date[1], $formatted_date[2]),
$format
);
// Forward updated format.
$custom_format = $format;
return date_format_date($date_object,'custom', $format);
}