function date_repeat_rrule_description in Date 8
Same name and namespace in other branches
- 5.2 date_repeat/date_repeat.module \date_repeat_rrule_description()
- 6.2 date_repeat/date_repeat.module \date_repeat_rrule_description()
- 6 date_repeat/date_repeat.module \date_repeat_rrule_description()
- 7.3 date_repeat/date_repeat.module \date_repeat_rrule_description()
- 7 date_repeat/date_repeat.module \date_repeat_rrule_description()
- 7.2 date_repeat/date_repeat.module \date_repeat_rrule_description()
Build a description of an iCal rule.
Constructs a human-readable description of the rule.
1 call to date_repeat_rrule_description()
- theme_date_repeat_display in date_repeat_field/
date_repeat_field.module - Theme the human-readable description for a Date Repeat rule.
File
- date_repeat/
date_repeat.module, line 193 - This module creates a form element that allows users to select repeat rules for a date, and reworks the result into an iCal RRULE string that can be stored in the database.
Code
function date_repeat_rrule_description($rrule, $format = 'D M d Y') {
// Empty or invalid value.
if (empty($rrule) || !strstr($rrule, 'RRULE')) {
return;
}
$calendar = system_calendar();
$parts = DateiCalParse::split_rrule($rrule);
$additions = $parts[2];
$exceptions = $parts[1];
$rrule = $parts[0];
if ($rrule['FREQ'] == 'NONE') {
return;
}
// Make sure there will be an empty description for any unused parts.
$description = array(
'!interval' => '',
'!byday' => '',
'!bymonth' => '',
'!count' => '',
'!until' => '',
'!except' => '',
'!additional' => '',
'!week_starts_on' => '',
);
$interval = date_repeat_interval_options();
switch ($rrule['FREQ']) {
case 'WEEKLY':
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every week', 'every @count weeks') . ' ';
break;
case 'MONTHLY':
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every month', 'every @count months') . ' ';
break;
case 'YEARLY':
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every year', 'every @count years') . ' ';
break;
default:
$description['!interval'] = format_plural($rrule['INTERVAL'], 'every day', 'every @count days') . ' ';
break;
}
if (!empty($rrule['BYDAY'])) {
$days = date_repeat_dow_day_options();
$counts = date_repeat_dow_count_options();
$results = array();
foreach ($rrule['BYDAY'] as $byday) {
// Get the numeric part of the BYDAY option, i.e. +3 from +3MO.
$day = substr($byday, -2);
$count = str_replace($day, '', $byday);
if (!empty($count)) {
// See if there is a 'pretty' option for this count, i.e. +1 => First.
$order = array_key_exists($count, $counts) ? strtolower($counts[$count]) : $count;
$results[] = trim(t('!repeats_every_interval on the !date_order !day_of_week', array(
'!repeats_every_interval ' => '',
'!date_order' => $order,
'!day_of_week' => $days[$day],
)));
}
else {
$results[] = trim(t('!repeats_every_interval every !day_of_week', array(
'!repeats_every_interval ' => '',
'!day_of_week' => $days[$day],
)));
}
}
$description['!byday'] = implode(' ' . t('and') . ' ', $results);
}
if (!empty($rrule['BYMONTH'])) {
if (sizeof($rrule['BYMONTH']) < 12) {
$results = array();
$months = $calendar
->month_names();
foreach ($rrule['BYMONTH'] as $month) {
$results[] = $months[$month];
}
if (!empty($rrule['BYMONTHDAY'])) {
$description['!bymonth'] = trim(t('!repeats_every_interval on the !month_days of !month_names', array(
'!repeats_every_interval ' => '',
'!month_days' => implode(', ', $rrule['BYMONTHDAY']),
'!month_names' => implode(', ', $results),
)));
}
else {
$description['!bymonth'] = trim(t('!repeats_every_interval on !month_names', array(
'!repeats_every_interval ' => '',
'!month_names' => implode(', ', $results),
)));
}
}
}
if ($rrule['INTERVAL'] < 1) {
$rrule['INTERVAL'] = 1;
}
if (!empty($rrule['COUNT'])) {
$description['!count'] = trim(t('!repeats_every_interval !count times', array(
'!repeats_every_interval ' => '',
'!count' => $rrule['COUNT'],
)));
}
if (!empty($rrule['UNTIL'])) {
$until = DateiCalParse::ical_date($rrule['UNTIL'], drupal_get_user_timezone());
$description['!until'] = trim(t('!repeats_every_interval until !until_date', array(
'!repeats_every_interval ' => '',
'!until_date' => $until
->format($format),
)));
}
if ($exceptions) {
$values = array();
foreach ($exceptions as $exception) {
$except = DateiCalParse::ical_date($exception, drupal_get_user_timezone());
$values[] = $except
->format($format);
}
$description['!except'] = trim(t('!repeats_every_interval except !except_dates', array(
'!repeats_every_interval ' => '',
'!except_dates' => implode(', ', $values),
)));
}
if (!empty($rrule['WKST'])) {
$day_names = date_repeat_dow_day_options();
$description['!week_starts_on'] = trim(t('!repeats_every_interval where the week start on !day_of_week', array(
'!repeats_every_interval ' => '',
'!day_of_week' => $day_names[trim($rrule['WKST'])],
)));
}
if ($additions) {
$values = array();
foreach ($additions as $addition) {
$add = DateiCalParse::ical_date($addition, drupal_get_user_timezone());
$values[] = $add
->format($format);
}
$description['!additional'] = trim(t('Also includes !additional_dates.', array(
'!additional_dates' => implode(', ', $values),
)));
}
return t('Repeats !interval !bymonth !byday !count !until !except. !additional', $description);
}