1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* This module creates a form element that allows users to select
|
6
|
* repeat rules for a date, and reworks the result into an iCal
|
7
|
* RRULE string that can be stored in the database.
|
8
|
*
|
9
|
* The module also parses iCal RRULEs to create an array of dates
|
10
|
* that meet their criteria.
|
11
|
*
|
12
|
* Other modules can use this API to add self-validating form elements
|
13
|
* to their dates, and identify dates that meet the RRULE criteria.
|
14
|
*/
|
15
|
|
16
|
/**
|
17
|
* Implements hook_element_info().
|
18
|
*/
|
19
|
function date_repeat_element_info() {
|
20
|
$type['date_repeat_rrule'] = array(
|
21
|
'#input' => TRUE,
|
22
|
'#process' => array('date_repeat_rrule_process'),
|
23
|
'#element_validate' => array('date_repeat_rrule_validate'),
|
24
|
'#theme_wrappers' => array('date_repeat_rrule'),
|
25
|
);
|
26
|
$type['date_repeat_form_element_radios'] = array(
|
27
|
'#input' => TRUE,
|
28
|
'#process' => array('date_repeat_form_element_radios_process'),
|
29
|
'#theme_wrappers' => array('radios'),
|
30
|
'#pre_render' => array('form_pre_render_conditional_form_element'),
|
31
|
);
|
32
|
if (module_exists('ctools')) {
|
33
|
$type['date_repeat_rrule']['#pre_render'] = array('ctools_dependent_pre_render');
|
34
|
}
|
35
|
return $type;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Implements hook_theme().
|
40
|
*/
|
41
|
function date_repeat_theme() {
|
42
|
return array(
|
43
|
'date_repeat_current_exceptions' => array('render element' => 'element'),
|
44
|
'date_repeat_current_additions' => array('render element' => 'element'),
|
45
|
'date_repeat_rrule' => array('render element' => 'element'),
|
46
|
);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Helper function for FREQ options.
|
51
|
*/
|
52
|
function date_repeat_freq_options() {
|
53
|
return array(
|
54
|
'DAILY' => t('Daily', array(), array('context' => 'datetime_singular')),
|
55
|
'WEEKLY' => t('Weekly', array(), array('context' => 'datetime_singular')),
|
56
|
'MONTHLY' => t('Monthly', array(), array('context' => 'datetime_singular')),
|
57
|
'YEARLY' => t('Yearly', array(), array('context' => 'datetime_singular')),
|
58
|
);
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Helper function for interval options.
|
63
|
*/
|
64
|
function date_repeat_interval_options() {
|
65
|
$options = range(0, 366);
|
66
|
unset($options[0]);
|
67
|
|
68
|
return $options;
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Helper function for FREQ options.
|
73
|
*
|
74
|
* Translated and untranslated arrays of the iCal day of week names. We need the
|
75
|
* untranslated values for date_modify(), translated values when displayed to
|
76
|
* user.
|
77
|
*/
|
78
|
function date_repeat_dow_day_options($translated = TRUE) {
|
79
|
return array(
|
80
|
'SU' => $translated ? t('Sunday', array(), array('context' => 'day_name')) : 'Sunday',
|
81
|
'MO' => $translated ? t('Monday', array(), array('context' => 'day_name')) : 'Monday',
|
82
|
'TU' => $translated ? t('Tuesday', array(), array('context' => 'day_name')) : 'Tuesday',
|
83
|
'WE' => $translated ? t('Wednesday', array(), array('context' => 'day_name')) : 'Wednesday',
|
84
|
'TH' => $translated ? t('Thursday', array(), array('context' => 'day_name')) : 'Thursday',
|
85
|
'FR' => $translated ? t('Friday', array(), array('context' => 'day_name')) : 'Friday',
|
86
|
'SA' => $translated ? t('Saturday', array(), array('context' => 'day_name')) : 'Saturday',
|
87
|
);
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Helper function for FREQ options.
|
92
|
*
|
93
|
* Translated and untranslated arrays of the iCal abbreviated day of week names.
|
94
|
*/
|
95
|
function date_repeat_dow_day_options_abbr($translated = TRUE, $length = 3) {
|
96
|
$return = array();
|
97
|
switch ($length) {
|
98
|
case 1:
|
99
|
$context = 'day_abbr1';
|
100
|
break;
|
101
|
|
102
|
case 2:
|
103
|
$context = 'day_abbr2';
|
104
|
break;
|
105
|
|
106
|
default:
|
107
|
$context = '';
|
108
|
}
|
109
|
foreach (date_repeat_dow_day_untranslated() as $key => $day) {
|
110
|
$return[$key] = $translated ? t(substr($day, 0, $length), array(), array('context' => $context)) : substr($day, 0, $length);
|
111
|
}
|
112
|
return $return;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Helper function for weekdays translated.
|
117
|
*/
|
118
|
function date_repeat_dow_day_untranslated() {
|
119
|
static $date_repeat_weekdays;
|
120
|
if (empty($date_repeat_weekdays)) {
|
121
|
$date_repeat_weekdays = array(
|
122
|
'SU' => 'Sunday',
|
123
|
'MO' => 'Monday',
|
124
|
'TU' => 'Tuesday',
|
125
|
'WE' => 'Wednesday',
|
126
|
'TH' => 'Thursday',
|
127
|
'FR' => 'Friday',
|
128
|
'SA' => 'Saturday',
|
129
|
);
|
130
|
}
|
131
|
return $date_repeat_weekdays;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Helper function for weekdays order.
|
136
|
*/
|
137
|
function date_repeat_dow_day_options_ordered($weekdays) {
|
138
|
$day_keys = array_keys($weekdays);
|
139
|
$day_values = array_values($weekdays);
|
140
|
for ($i = 1; $i <= variable_get('date_first_day', 0); $i++) {
|
141
|
$last_key = array_shift($day_keys);
|
142
|
array_push($day_keys, $last_key);
|
143
|
$last_value = array_shift($day_values);
|
144
|
array_push($day_values, $last_value);
|
145
|
}
|
146
|
$weekdays = array_combine($day_keys, $day_values);
|
147
|
return $weekdays;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* Helper function for BYDAY options.
|
152
|
*/
|
153
|
function date_repeat_dow_count_options() {
|
154
|
return array('' => t('Every', array(), array('context' => 'date_order'))) + date_order_translated();
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* Helper function for BYDAY options.
|
159
|
*
|
160
|
* Creates options like -1SU and 2TU.
|
161
|
*/
|
162
|
function date_repeat_dow_options() {
|
163
|
$options = array();
|
164
|
foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
|
165
|
foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
|
166
|
$options[$count_key . $dow_key] = $count_value . ' ' . $dow_value;
|
167
|
}
|
168
|
}
|
169
|
return $options;
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Translate a day of week position to the iCal day name.
|
174
|
*
|
175
|
* Used with date_format($date, 'w') or get_variable('date_first_day'), which
|
176
|
* return 0 for Sunday, 1 for Monday, etc.
|
177
|
*
|
178
|
* dow 2 becomes 'TU', dow 3 becomes 'WE', and so on.
|
179
|
*/
|
180
|
function date_repeat_dow2day($dow) {
|
181
|
$days_of_week = array_keys(date_repeat_dow_day_options(FALSE));
|
182
|
return $days_of_week[$dow];
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Shift the array of iCal day names into the right order.
|
187
|
*
|
188
|
* @param $week_start_day
|
189
|
*/
|
190
|
function date_repeat_days_ordered($week_start_day) {
|
191
|
$days = array_flip(array_keys(date_repeat_dow_day_options(FALSE)));
|
192
|
$start_position = $days[$week_start_day];
|
193
|
$keys = array_flip($days);
|
194
|
if ($start_position > 0) {
|
195
|
for ($i = 1; $i <= $start_position; $i++) {
|
196
|
$last = array_shift($keys);
|
197
|
array_push($keys, $last);
|
198
|
}
|
199
|
}
|
200
|
return $keys;
|
201
|
}
|
202
|
|
203
|
/**
|
204
|
* Build a description of an iCal rule.
|
205
|
*
|
206
|
* Constructs a human-readable description of the rule.
|
207
|
*/
|
208
|
function date_repeat_rrule_description($rrule, $format = 'D M d Y') {
|
209
|
// Empty or invalid value.
|
210
|
if (empty($rrule) || !strstr($rrule, 'RRULE')) {
|
211
|
return;
|
212
|
}
|
213
|
|
214
|
module_load_include('inc', 'date_api', 'date_api_ical');
|
215
|
module_load_include('inc', 'date_repeat', 'date_repeat_calc');
|
216
|
|
217
|
$parts = date_repeat_split_rrule($rrule);
|
218
|
$additions = $parts[2];
|
219
|
$exceptions = $parts[1];
|
220
|
$rrule = $parts[0];
|
221
|
if ($rrule['FREQ'] == 'NONE') {
|
222
|
return;
|
223
|
}
|
224
|
|
225
|
// Make sure there will be an empty description for any unused parts.
|
226
|
$description = array(
|
227
|
'!interval' => '',
|
228
|
'!byday' => '',
|
229
|
'!bymonth' => '',
|
230
|
'!count' => '',
|
231
|
'!until' => '',
|
232
|
'!except' => '',
|
233
|
'!additional' => '',
|
234
|
'!week_starts_on' => '',
|
235
|
);
|
236
|
$interval = date_repeat_interval_options();
|
237
|
switch ($rrule['FREQ']) {
|
238
|
case 'WEEKLY':
|
239
|
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every week', 'every @count weeks') . ' ';
|
240
|
break;
|
241
|
|
242
|
case 'MONTHLY':
|
243
|
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every month', 'every @count months') . ' ';
|
244
|
break;
|
245
|
|
246
|
case 'YEARLY':
|
247
|
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every year', 'every @count years') . ' ';
|
248
|
break;
|
249
|
|
250
|
default:
|
251
|
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every day', 'every @count days') . ' ';
|
252
|
}
|
253
|
|
254
|
if (!empty($rrule['BYDAY'])) {
|
255
|
$days = date_repeat_dow_day_options();
|
256
|
$counts = date_repeat_dow_count_options();
|
257
|
$results = array();
|
258
|
foreach ($rrule['BYDAY'] as $byday) {
|
259
|
// Get the numeric part of the BYDAY option, i.e. +3 from +3MO.
|
260
|
$day = substr($byday, -2);
|
261
|
$count = str_replace($day, '', $byday);
|
262
|
if (!empty($count)) {
|
263
|
// See if there is a 'pretty' option for this count, i.e. +1 => First.
|
264
|
$order = array_key_exists($count, $counts) ? strtolower($counts[$count]) : $count;
|
265
|
$results[] = trim(t('!repeats_every_interval on the !date_order !day_of_week',
|
266
|
array(
|
267
|
'!repeats_every_interval ' => '',
|
268
|
'!date_order' => $order,
|
269
|
'!day_of_week' => $days[$day],
|
270
|
)));
|
271
|
}
|
272
|
else {
|
273
|
$results[] = trim(t('!repeats_every_interval every !day_of_week',
|
274
|
array('!repeats_every_interval ' => '', '!day_of_week' => $days[$day])));
|
275
|
}
|
276
|
}
|
277
|
$description['!byday'] = implode(' ' . t('and') . ' ', $results);
|
278
|
}
|
279
|
if (!empty($rrule['BYMONTH'])) {
|
280
|
if (count($rrule['BYMONTH']) < 12) {
|
281
|
$results = array();
|
282
|
$months = date_month_names();
|
283
|
foreach ($rrule['BYMONTH'] as $month) {
|
284
|
$results[] = $months[$month];
|
285
|
}
|
286
|
if (!empty($rrule['BYMONTHDAY'])) {
|
287
|
$description['!bymonth'] = trim(t('!repeats_every_interval on the !month_days of !month_names',
|
288
|
array(
|
289
|
'!repeats_every_interval ' => '',
|
290
|
'!month_days' => implode(', ', $rrule['BYMONTHDAY']),
|
291
|
'!month_names' => implode(', ', $results),
|
292
|
)));
|
293
|
}
|
294
|
else {
|
295
|
$description['!bymonth'] = trim(t('!repeats_every_interval on !month_names',
|
296
|
array(
|
297
|
'!repeats_every_interval ' => '',
|
298
|
'!month_names' => implode(', ', $results),
|
299
|
)));
|
300
|
}
|
301
|
}
|
302
|
}
|
303
|
if ($rrule['INTERVAL'] < 1) {
|
304
|
$rrule['INTERVAL'] = 1;
|
305
|
}
|
306
|
if (!empty($rrule['COUNT'])) {
|
307
|
$description['!count'] = trim(t('!repeats_every_interval !count times',
|
308
|
array('!repeats_every_interval ' => '', '!count' => $rrule['COUNT'])));
|
309
|
}
|
310
|
if (!empty($rrule['UNTIL'])) {
|
311
|
$until = date_ical_date($rrule['UNTIL'], 'UTC');
|
312
|
date_timezone_set($until, date_default_timezone_object());
|
313
|
$description['!until'] = trim(t('!repeats_every_interval until !until_date',
|
314
|
array(
|
315
|
'!repeats_every_interval ' => '',
|
316
|
'!until_date' => date_format_date($until, 'custom', $format),
|
317
|
)));
|
318
|
}
|
319
|
if ($exceptions) {
|
320
|
$values = array();
|
321
|
foreach ($exceptions as $exception) {
|
322
|
$except = date_ical_date($exception, 'UTC');
|
323
|
date_timezone_set($except, date_default_timezone_object());
|
324
|
$values[] = date_format_date($except, 'custom', $format);
|
325
|
}
|
326
|
$description['!except'] = trim(t('!repeats_every_interval except !except_dates',
|
327
|
array(
|
328
|
'!repeats_every_interval ' => '',
|
329
|
'!except_dates' => implode(', ', $values),
|
330
|
)));
|
331
|
}
|
332
|
if (!empty($rrule['WKST'])) {
|
333
|
$day_names = date_repeat_dow_day_options();
|
334
|
$description['!week_starts_on'] = trim(t('!repeats_every_interval where the week start on !day_of_week',
|
335
|
array('!repeats_every_interval ' => '', '!day_of_week' => $day_names[trim($rrule['WKST'])])));
|
336
|
}
|
337
|
if ($additions) {
|
338
|
$values = array();
|
339
|
foreach ($additions as $addition) {
|
340
|
$add = date_ical_date($addition, 'UTC');
|
341
|
date_timezone_set($add, date_default_timezone_object());
|
342
|
$values[] = date_format_date($add, 'custom', $format);
|
343
|
}
|
344
|
$description['!additional'] = trim(t('Also includes !additional_dates.',
|
345
|
array('!additional_dates' => implode(', ', $values))));
|
346
|
}
|
347
|
$output = t('Repeats !interval !bymonth !byday !count !until !except. !additional', $description);
|
348
|
// Removes double whitespaces from Repeat tile.
|
349
|
$output = preg_replace('/\s+/', ' ', $output);
|
350
|
// Removes whitespace before full stop ".", at the end of the title.
|
351
|
$output = str_replace(' .', '.', $output);
|
352
|
return $output;
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* Parse an iCal rule into a parsed RRULE array and an EXDATE array.
|
357
|
*/
|
358
|
function date_repeat_split_rrule($rrule) {
|
359
|
$parts = explode("\n", str_replace("\r\n", "\n", $rrule));
|
360
|
$rrule = array();
|
361
|
$exceptions = array();
|
362
|
$additions = array();
|
363
|
$additions = array();
|
364
|
foreach ($parts as $part) {
|
365
|
if (strstr($part, 'RRULE')) {
|
366
|
$cleanded_part = str_replace('RRULE:', '', $part);
|
367
|
$rrule = (array) date_ical_parse_rrule('RRULE:', $cleanded_part);
|
368
|
}
|
369
|
elseif (strstr($part, 'EXDATE')) {
|
370
|
$exdate = str_replace('EXDATE:', '', $part);
|
371
|
$exceptions = (array) date_ical_parse_exceptions('EXDATE:', $exdate);
|
372
|
unset($exceptions['DATA']);
|
373
|
}
|
374
|
elseif (strstr($part, 'RDATE')) {
|
375
|
$rdate = str_replace('RDATE:', '', $part);
|
376
|
$additions = (array) date_ical_parse_exceptions('RDATE:', $rdate);
|
377
|
unset($additions['DATA']);
|
378
|
}
|
379
|
}
|
380
|
return array($rrule, $exceptions, $additions);
|
381
|
}
|
382
|
|
383
|
/**
|
384
|
* Analyze a RRULE and return dates that match it.
|
385
|
*/
|
386
|
function date_repeat_calc($rrule, $start, $end, $exceptions = array(), $timezone = NULL, $additions = array()) {
|
387
|
module_load_include('inc', 'date_repeat', 'date_repeat_calc');
|
388
|
return _date_repeat_calc($rrule, $start, $end, $exceptions, $timezone, $additions);
|
389
|
}
|
390
|
|
391
|
/**
|
392
|
* Generate the repeat rule setting form.
|
393
|
*/
|
394
|
function date_repeat_rrule_process($element, &$form_state, $form) {
|
395
|
module_load_include('inc', 'date_repeat', 'date_repeat_form');
|
396
|
return _date_repeat_rrule_process($element, $form_state, $form);
|
397
|
}
|
398
|
|
399
|
/**
|
400
|
* Process function for 'date_repeat_form_element_radios'.
|
401
|
*/
|
402
|
function date_repeat_form_element_radios_process($element) {
|
403
|
$childrenkeys = element_children($element);
|
404
|
|
405
|
if (count($element['#options']) &&
|
406
|
count($element['#options']) == count($childrenkeys)) {
|
407
|
$weight = 0;
|
408
|
$children = array();
|
409
|
$classes = isset($element['#div_classes']) ?
|
410
|
$element['#div_classes'] : array();
|
411
|
foreach ($childrenkeys as $childkey) {
|
412
|
$children[$childkey] = $element[$childkey];
|
413
|
unset($element[$childkey]);
|
414
|
}
|
415
|
foreach ($element['#options'] as $key => $choice) {
|
416
|
$currentchildkey = array_shift($childrenkeys);
|
417
|
$weight += 0.001;
|
418
|
$class = array_shift($classes);
|
419
|
$element += array($key => array());
|
420
|
$parents_for_id = array_merge($element['#parents'], array($key));
|
421
|
$element[$key] += array(
|
422
|
'#prefix' => '<div' . ($class ? " class=\"{$class}\"" : '') . '>',
|
423
|
'#type' => 'radio',
|
424
|
'#title' => $choice,
|
425
|
'#title_display' => 'invisible',
|
426
|
'#return_value' => $key,
|
427
|
'#default_value' => isset($element['#default_value']) ?
|
428
|
$element['#default_value'] : NULL,
|
429
|
'#attributes' => $element['#attributes'],
|
430
|
'#parents' => $element['#parents'],
|
431
|
'#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
|
432
|
'#ajax' => isset($element['#ajax']) ? $element['ajax'] : NULL,
|
433
|
'#weight' => $weight,
|
434
|
'#theme_wrappers' => array(),
|
435
|
'#suffix' => ' ',
|
436
|
);
|
437
|
|
438
|
$child = $children[$currentchildkey];
|
439
|
|
440
|
$weight += 0.001;
|
441
|
|
442
|
$child['#weight'] = $weight;
|
443
|
$child['#title_display'] = 'invisible';
|
444
|
$child['#suffix'] = (!empty($child['#suffix']) ? $child['#suffix'] : '') .
|
445
|
'</div>';
|
446
|
$child['#parents'] = $element['#parents'];
|
447
|
array_pop($child['#parents']);
|
448
|
array_push($child['#parents'], $currentchildkey);
|
449
|
|
450
|
$element_prototype = element_info($child['#type']);
|
451
|
$old_wrappers = array();
|
452
|
if (isset($child['#theme_wrappers'])) {
|
453
|
$old_wrappers += $child['#theme_wrappers'];
|
454
|
}
|
455
|
if (isset($element_prototype['#theme_wrappers'])) {
|
456
|
$old_wrappers += $element_prototype['#theme_wrappers'];
|
457
|
}
|
458
|
|
459
|
$child['#theme_wrappers'] = array();
|
460
|
|
461
|
foreach ($old_wrappers as $wrapper) {
|
462
|
if ($wrapper != 'form_element') {
|
463
|
$child['#theme_wrappers'][] = $wrapper;
|
464
|
}
|
465
|
}
|
466
|
|
467
|
$element[$currentchildkey] = $child;
|
468
|
}
|
469
|
}
|
470
|
|
471
|
return $element;
|
472
|
}
|