-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepeating_dates.module
706 lines (643 loc) · 23 KB
/
repeating_dates.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
<?php
/**
* @file
* Repeating dates main module code.
*/
require_once __DIR__ . '/libraries/php-rrule/src/RfcParser.php';
require_once __DIR__ . '/libraries/php-rrule/src/RRuleInterface.php';
require_once __DIR__ . '/libraries/php-rrule/src/RRuleTrait.php';
require_once __DIR__ . '/libraries/php-rrule/src/RRule.php';
require_once __DIR__ . '/libraries/php-rrule/src/RSet.php';
use RRule\RRule;
use RRule\RSet;
define('REPEATING_DATES_MAX_VALUES', 730);
/**
* Implements hook_field_info().
*/
function repeating_dates_field_info() {
return array(
'repeating_date' => array(
'label' => t('Date (repeating)'),
'description' => t('This field stores start and end datetime and a rule to repeat.'),
'settings' => array(),
'instance_settings' => array(),
'default_widget' => 'repeating_date_widget',
'default_formatter' => 'repeating_date_list',
),
);
}
/**
* Implements hook_field_is_empty().
*/
function repeating_dates_field_is_empty($item, $field) {
// Nothing at all.
if (empty($item['dtstart'])) {
return TRUE;
}
// Start date is empty.
if (isset($item['dtstart']['date']) && empty($item['dtstart']['date'])) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_widget_info().
*/
function repeating_dates_field_widget_info() {
return array(
'repeating_date_widget' => array(
'label' => t('Repeating date widget'),
'field types' => array('repeating_date'),
'settings' => array(
'hide_to_date' => FALSE,
'default_repeat_freq' => 'weekly',
'default_stop_mode' => 'count',
'exdate_helptext' => t('Exclude dates from this series of recurrences.'),
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
'default value' => FIELD_BEHAVIOR_NONE,
),
),
);
}
/**
* Implements hook_field_widget_settings_form().
*/
function repeating_dates_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
if ($widget['type'] == 'repeating_date_widget') {
$form = array();
$form['hide_to_date'] = array(
'#type' => 'checkbox',
'#title' => t('Hide to-date input'),
'#default_value' => $settings['hide_to_date'],
'#description' => t('When to-date is hidden, only a single datetime input is available for start date, no duration.'),
);
$form['default_repeat_freq'] = array(
'#type' => 'select',
'#title' => t('Default repeat frequency'),
'#options' => _repeating_dates_frequency_options(),
'#default_value' => $settings['default_repeat_freq'],
);
$form['default_stop_mode'] = array(
'#type' => 'radios',
'#title' => t('By default stop repeating by'),
'#options' => array(
'count' => t('Count'),
'date' => t('Date'),
),
'#default_value' => isset($settings['default_stop_mode']) ? $settings['default_stop_mode'] : 'count',
);
$form['exdate_helptext'] = array(
'#type' => 'textfield',
'#title' => t('Help text for excluded dates'),
'#default_value' => $settings['exdate_helptext'],
);
return $form;
}
}
/**
* Implements hook_field_widget_form().
*/
function repeating_dates_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'repeating_date_widget':
// The first item contains all the info we need (dtstart and rrule).
$values = !empty($items[0]) ? $items[0] : array();
require_once __DIR__ . '/repeating_dates.widget_form.inc';
$element += _repeating_dates_widget_form($form_state, $instance, $values, $field['cardinality']);
if (!empty($element['#description'])) {
$element['rrule']['#description'] = $element['#description'];
}
$element['#attached']['css'][] = backdrop_get_path('module', 'repeating_dates') . '/css/repeating-dates.css';
break;
}
return $element;
}
/**
* Custom validation callback for the widget form.
*/
function _repeating_dates_widget_validate($element, &$form_state, $form) {
$field_name = $element['#field_name'];
$rrule = $form_state['values'][$field_name][LANGUAGE_NONE]['rrule'];
$parents = implode('][', $element['#parents']);
if (!$element['#required'] && empty($rrule['dtstart']['date'])) {
// Nothing to do.
return;
}
if ($element['#required'] == TRUE && empty($rrule['dtstart']['date'])) {
$selector = $parents . '][rrule][dtstart';
form_set_error($selector, t('Start date is required.'));
}
$tz = date_default_timezone();
$dtstart = _repeating_dates_array_to_timestamp($rrule['dtstart'], $tz);
$type = $form[$field_name][LANGUAGE_NONE]['rrule']['dtend']['#type'];
if ($type != 'value' && !empty($rrule['dtend']['date'])) {
$dtend = _repeating_dates_array_to_timestamp($rrule['dtend'], $tz);
}
else {
// Either dtend is empty, or the form item is hidden, in both cases we set
// the value automatically.
$form_state['values'][$field_name][LANGUAGE_NONE]['rrule']['dtend'] = $rrule['dtstart'];
$dtend = $dtstart;
}
if ($dtstart > $dtend) {
$selector = $parents . '][rrule][dtstart';
form_set_error($selector, t('Start can not be after end.'));
}
if ($rrule['freq'] == 'weekly') {
$weekdays = array_filter($rrule['byday_week']);
if (empty($weekdays)) {
$selector = $parents . '][rrule][byday_week';
form_set_error($selector, t('Weekly frequencies need at least one weekday selected.'));
}
}
if ($rrule['stopby'] == 'date' && empty($rrule['until'])) {
$selector = $parents . '][rrule][until';
form_set_error($selector, t('If you stop based on date, the date may not be empty.'));
}
if ($rrule['stopby'] == 'count' && empty($rrule['count'])) {
$selector = $parents . '][rrule][count';
form_set_error($selector, t('If you stop based on number, the number may not be empty.'));
}
}
/**
* Implements hook_field_formatter_info().
*/
function repeating_dates_field_formatter_info() {
return array(
'repeating_date_list' => array(
'label' => t('Repeating date list'),
'field types' => array('repeating_date'),
'settings' => array(
'hide_description' => FALSE,
'datetime_format' => 'short',
'dateonly_format' => 'html_date',
'timeonly_format' => 'html_time',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function repeating_dates_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'repeating_date_list') {
$element['hide_description'] = array(
'#type' => 'checkbox',
'#title' => t('Hide the rule summary (description)'),
'#default_value' => $settings['hide_description'],
);
$date_formats = system_get_date_formats();
$options = array();
foreach ($date_formats as $name => $values) {
$options[$name] = $values['label'];
}
$element['datetime_format'] = array(
'#type' => 'select',
'#title' => t('Full date and time format'),
'#options' => $options,
'#default_value' => $settings['datetime_format'],
);
$element['dateonly_format'] = array(
'#type' => 'select',
'#title' => t('Date only format'),
'#options' => $options,
'#default_value' => $settings['dateonly_format'],
);
$element['timeonly_format'] = array(
'#type' => 'select',
'#title' => t('Time only format'),
'#options' => $options,
'#default_value' => $settings['timeonly_format'],
'#description' => t('You might want to create a custom format for date only or time only.'),
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function repeating_dates_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'repeating_date_list') {
$list = array();
if ($settings['hide_description']) {
$list[] = t('The repeat rule summary is hidden.');
}
else {
$list[] = t('The repeat rule summary is shown.');
}
$formats = system_get_date_formats();
$list[] = t('Full datetime format is %format', array(
'%format' => $formats[$settings['datetime_format']]['label'],
));
$list[] = t('Date only format is %format', array(
'%format' => $formats[$settings['dateonly_format']]['label'],
));
$list[] = t('Time only format is %format', array(
'%format' => $formats[$settings['timeonly_format']]['label'],
));
$summary = theme('item_list', array('items' => $list));
}
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function repeating_dates_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'repeating_date_list':
if (empty($items)) {
return $element;
}
// @todo is there a way to fiddle a preview and is it worth it?
$args = arg();
if (count($args) == 4 && $args[1] == 'preview') {
$output = t('Field %label does not support dynamic preview.', array(
'%label' => $instance['label'],
));
$element[0] = array('#markup' => $output);
return $element;
}
$settings = $display['settings'];
$prefix = '';
if (!$settings['hide_description']) {
$prefix = '<div class="rrule-description">' . _repeating_dates_rule_description($items[0], $settings) . '</div>';
}
$item_list = array();
$dtstart_date = format_date($items[0]['dtstart'], 'custom', 'Y-m-d');
$dtend_date = format_date($items[0]['dtend'], 'custom', 'Y-m-d');
foreach ($items as $delta => $item) {
$item_list[$delta] = format_date($item['dtstart'], $settings['datetime_format']);
// Skip "To" datetime, if it's the same as start.
if ($item['dtend'] != $item['dtstart']) {
// Skip redundant date info, if both dates are the same.
if ($dtstart_date == $dtend_date) {
$item_list[$delta] .= ' – ' . format_date($item['dtend'], $settings['timeonly_format']);
}
else {
$item_list[$delta] .= ' – ' . format_date($item['dtend'], $settings['datetime_format']);
}
}
}
$element[0] = array(
'#type' => 'markup',
'#markup' => $prefix . theme('item_list', array('items' => $item_list)),
);
break;
}
return $element;
}
/**
* Implements hook_field_presave().
*/
function repeating_dates_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
if (empty($items)) {
return;
}
// Already processed, like in VBO.
if (!is_array($items['0']['dtstart'])) {
return;
}
// Remove wrapping delta.
$rrule = $items['0'];
$items_processed = array();
$tz = date_default_timezone();
$dtstart_str = trim(implode(' ', $rrule['dtstart']));
$dtstart_obj = new DateTime($dtstart_str, new DateTimeZone($tz));
$dtstart_stamp = $dtstart_obj->format('U');
$duration = 0;
$ical_rrule = _repeating_dates_build_rrule($rrule, $dtstart_obj);
if (is_array($rrule['dtend']) && !empty($rrule['dtend']['date'])) {
$dtend_stamp = _repeating_dates_array_to_timestamp($rrule['dtend'], $tz);
$duration = $dtend_stamp - $dtstart_stamp;
}
if (!empty($ical_rrule)) {
$max = $field['cardinality'];
$exceeded = FALSE;
if ($max == FIELD_CARDINALITY_UNLIMITED) {
$max = REPEATING_DATES_MAX_VALUES;
}
try {
$string = "DTSTART;TZID=$tz:" . $dtstart_obj->format('Ymd\THis') . "\r\n" . $ical_rrule;
$rrule_obj = RRule::createFromRfcString($string);
foreach ($rrule_obj as $delta => $recurrence) {
if ($delta > $max) {
$exceeded = TRUE;
break;
}
$items_processed[$delta]['dtstart'] = $recurrence->format('U');
$items_processed[$delta]['dtend'] = $recurrence->format('U') + $duration;
$items_processed[$delta]['rrule'] = $ical_rrule;
}
}
catch (Exception $e) {
watchdog_exception('repeating_dates', $e);
$items = array();
return;
}
if ($exceeded) {
$message = t('The repeat setup created more items than field %field_label allows. Truncated to @max recurrences.', array(
'%field_label' => $instance['label'],
'@max' => $max,
));
backdrop_set_message($message, 'warning');
}
}
else {
// Single datetime pair, no repeat rule.
$items_processed[0]['dtstart'] = $dtstart_stamp;
$items_processed[0]['dtend'] = $dtstart_stamp + $duration;
$items_processed[0]['rrule'] = '';
}
// Replace rrule with processed items.
$items = $items_processed;
}
/**
* Implements hook_field_views_data_views_data_alter().
*/
function repeating_dates_field_views_data_views_data_alter(&$data, $field) {
if ($field['type'] == 'repeating_date') {
$field_name = $field['field_name'];
$data_table_name = 'field_data_' . $field_name;
$revision_table_name = 'field_revision_' . $field_name;
// Inform views that our field columns are dates (timestamps).
$data[$data_table_name][$field_name . '_dtstart']['field']['handler'] = 'views_handler_field_date';
$data[$data_table_name][$field_name . '_dtend']['field']['handler'] = 'views_handler_field_date';
$data[$data_table_name][$field_name . '_dtstart']['filter']['handler'] = 'views_handler_filter_date';
$data[$data_table_name][$field_name . '_dtend']['filter']['handler'] = 'views_handler_filter_date';
$data[$data_table_name][$field_name . '_dtstart']['sort']['handler'] = 'views_handler_sort_date';
$data[$data_table_name][$field_name . '_dtend']['sort']['handler'] = 'views_handler_sort_date';
}
}
/**
* Custom callback to add another date item to exdates.
*/
function repeating_dates_add_exdate($form, &$form_state) {
$button = $form_state['triggering_element'];
// Go one level up in the form, to the exdates element.
array_pop($button['#array_parents']);
$element = backdrop_array_get_nested_value($form, $button['#array_parents']);
$defaults = $element['dates']['#default_value'];
$field_name = $element['#array_parents'][0];// @todo sure?
$exdates = array();
if (!empty($form_state['input'][$field_name])) {
if (!empty($form_state['input'][$field_name]['und']['rrule']['exdates']['dates'])) {
$datelist = $form_state['input'][$field_name]['und']['rrule']['exdates']['dates'];
foreach ($datelist as $input) {
// Filter out invalid input.
if (!is_string($input)) {
continue;
}
// Filter out strings that are no dates as we expect them.
$pattern = '/[0-9]{4}-[0-9]{2}-[0-9]{2}/';
if (preg_replace($pattern, '', $input) !== '') {
continue;
}
$exdates[] = $input;
}
}
}
$element['dates']['#default_value'] = $exdates;
$element['dates']['#default_value'][] = '';
$weight = 0;
foreach ($element['dates']['#default_value'] as $index => $date) {
$weight += 0.001;
$element['dates'] += array($index => array());
$parents_for_id = array_merge($element['dates']['#parents'], array($index));
$element['dates'][$index] += array(
'#type' => 'html_date',
'#title' => t('Date !num', array('!num' => ($index + 1))),
'#title_display' => 'invisible',
'#default_value' => $element['dates']['#default_value'][$index],
'#value' => $element['dates']['#default_value'][$index],
'#attributes' => $element['dates']['#attributes'],
'#parents' => array_merge($element['dates']['#parents'], array($index)),
'#id' => backdrop_html_id('edit-' . implode('-', $parents_for_id)),
'#name' => $field_name . '[und][rrule][exdates][dates][' . $index . ']',
'#weight' => $weight,
);
}
return $element['dates'];
}
/**
* Custom ajax callback.
*/
function repeating_dates_add_exdate_callback($form, &$form_state) {
return repeating_dates_add_exdate($form, $form_state);
}
/**
* Implements hook_element_info().
*/
function repeating_dates_element_info() {
$types['repeating_dates_datelist'] = array(
'#input' => TRUE,
'#process' => array('repeating_dates_process_datelist'),
);
return $types;
}
/**
* Process callback for custom form item "datelist".
*/
function repeating_dates_process_datelist($element) {
if (count($element['#default_value']) > 0) {
$weight = 0;
$field_name = $element['#parents'][0];
// Back from preview the value got messed.
if (isset($element['#default_value']['add_exception'])) {
$element['#default_value'] = $element['#default_value']['dates'];
}
foreach ($element['#default_value'] as $index => $date) {
$weight += 0.001;
$element += array($index => array());
$parents_for_id = array_merge($element['#parents'], array($index));
$element[$index] += array(
'#type' => 'html_date',
'#title' => t('Date !num', array('!num' => ($index + 1))),
'#title_display' => 'invisible',
'#default_value' => $element['#default_value'][$index],
'#value' => $element['#default_value'][$index],
'#attributes' => $element['#attributes'],
'#parents' => array_merge($element['#parents'], array($index)),
'#id' => backdrop_html_id('edit-' . implode('-', $parents_for_id)),
'#name' => $field_name . '[und][rrule][exdates][dates][' . $index . ']',
'#weight' => $weight,
);
}
}
return $element;
}
/**
* Helper function.
*
* @param array $values
* Keyed array of date and time.
* @param string $timezone
* Timezone name to use for conversion.
*
* @return string
* Unix timestamp.
*/
function _repeating_dates_array_to_timestamp($values, $timezone = 'UTC') {
if (is_array($values)) {
$string = trim(implode(' ', $values));
$datetime = new DateTime($string, new DateTimeZone($timezone));
if (is_object($datetime)) {
return $datetime->format('U');
}
}
return '0';
}
/**
* Helper function to build an ical rule from form submissions.
*
* @param array $rrule
* Array of rrule components as provided by the widget form.
* @param object $start_date
* Datetime object of the start date.
*/
function _repeating_dates_build_rrule($rrule, $start_date) {
$components = array();
switch ($rrule['freq']) {
case 'daily':
$components[] = 'FREQ=DAILY';
$components[] = 'INTERVAL=' . $rrule['interval_day'];
break;
case 'weekly':
$dow = array_filter($rrule['byday_week']);
$days_string = implode(',', array_keys($dow));
$components[] = 'FREQ=WEEKLY';
$components[] = 'BYDAY=' . $days_string;
$components[] = 'INTERVAL=' . $rrule['interval_week'];
break;
case 'monthly':
$components[] = 'FREQ=MONTHLY';
if ($rrule['monthday_setpos'] === '0') {
$components[] = 'BYMONTHDAY=' . $rrule['bymonthday_month'];
}
else {
if ($rrule['byday_month'] != 'any') {
$components[] = 'BYSETPOS=' . $rrule['bysetpos_month'];
$components[] = 'BYDAY=' . $rrule['byday_month'];
}
else {
// @todo is there an easier way to provide "last day of month"?
$components[] = 'BYMONTHDAY=' . $rrule['bysetpos_month'];
}
}
$components[] = 'INTERVAL=' . $rrule['interval_month'];
break;
case 'yearly':
$components[] = 'FREQ=YEARLY';
if ($rrule['yearly_setpos'] === '0') {
$components[] = 'BYMONTHDAY=' . $rrule['bymonthday_year'];
}
else {
$components[] = 'BYSETPOS=' . $rrule['bysetpos_year'];
$components[] = 'BYDAY=' . $rrule['byday_year'];
}
$components[] = 'BYMONTH=' . $rrule['bymonth_year'];
break;
default:
return '';
}
if ($rrule['stopby'] == 'date') {
// Apply start date time offset to date only 'until' string.
$full_until_string = $rrule['until'] . ' ' . $start_date->format('H:i:s');
$until = new DateTime($full_until_string);
// Convert 'until' to UTC.
$until->setTimezone(new DateTimeZone('UTC'));
$components[] = 'UNTIL=' . $until->format('Ymd\THis\Z');
}
else {
$components[] = 'COUNT=' . max($rrule['count'], 1);
}
$first_day = config_get('system.date', 'first_day');
$keys = _repeating_dates_get_ical_daykeys();
$components[] = 'WKST=' . $keys[$first_day];
$rrule_string = 'RRULE:' . implode(';', $components);
if (!empty($rrule['exdates']['dates'])) {
$exdates = array_filter($rrule['exdates']['dates']);
$exdate_list = array();
foreach ($exdates as $date_string) {
$date = new DateTime($date_string . ' ' . $start_date->format('H:i:s'));
$exdate_list[] = $date->format('Ymd\THis');
}
if (!empty($exdate_list)) {
$timezone = $start_date->getTimeZone()->getName();
$rrule_string .= "\r\nEXDATE;TZID=$timezone:" . implode(',', $exdate_list);
}
}
return $rrule_string;
}
/**
* Helper function to return a list of weekdays as used by ical.
*/
function _repeating_dates_get_ical_daykeys() {
return array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
}
/**
* Helper function to return the frequency options.
*/
function _repeating_dates_frequency_options() {
return array(
'daily' => t('Daily'),
'weekly' => t('Weekly'),
'monthly' => t('Monthly'),
'yearly' => t('Yearly'),
'' => t('Do not repeat'),
);
}
/**
* Helper function to return a descriptive repesentation of the rrule.
*
* @param array $values
* Keyed array with field values from the database.
* @param array $settings
* Formatter settings for this field.
*/
function _repeating_dates_rule_description($values, $settings) {
$tz = date_default_timezone();
$dtstart = new DateTime('@' . $values['dtstart']);
$dtstart->setTimezone(new DateTimeZone($tz));
if (!empty($values['rrule'])) {
try {
global $language;
$description = '';
// @todo outcome is at least as odd as with date_repeat. :(
// @todo and it's relatively slow.
$rule_parts = explode("\r\n", $values['rrule']);
$rrule_obj = new RRule("DTSTART;TZID=$tz:" . $dtstart->format('Ymd\THis') . "\r\n" . $rule_parts[0]);
$description = $rrule_obj->humanReadable(array('locale' => $language->langcode));
if (!empty($rule_parts[1])) {
$dates = array();
$datelist_parts = explode(':', $rule_parts[1]);
// The dates' timezone might be different from the current one.
$timezone = str_replace('EXDATE;TZID=', '', $datelist_parts[0]);
$tz_obj = new DateTimeZone($timezone);
$date_strings = explode(',', $datelist_parts[1]);
foreach ($date_strings as $string) {
$date = new DateTime($string, $tz_obj);
$dates[] = format_date($date->format('U'), $settings['dateonly_format']);
}
$description .= '<br>' . t('Except for: !dates', array(
'!dates' => implode(', ', $dates),
));
}
return $description;
}
catch (Exception $e) {
// This is for node display. No dblog to prevent flooding.
return;
}
}
}