/**
  * Build a description of an iCal rule.
  *
  * Constructs a human-readable description of the rule.
  */
 function date_repeat_rrule_description($rrule, $format = 'D M d Y')
 {
     // Empty or invalid value.
     if (empty($rrule) || !strstr($rrule, 'RRULE')) {
         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' => '');
     $parts = self::date_repeat_split_rrule($rrule);
     $additions = $parts[2];
     $exceptions = $parts[1];
     $rrule = $parts[0];
     $interval = self::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) {
             $day = substr($byday, -2);
             $count = intval(str_replace(' ' . $day, '', $byday));
             if ($count = intval(str_replace(' ' . $day, '', $byday))) {
                 $results[] = trim(t('!repeats_every_interval on the !date_order !day_of_week', array('!repeats_every_interval ' => '', '!date_order' => strtolower($counts[substr($byday, 0, 2)]), '!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 = Yii::app()->getLocale()->getMonthNames();
             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 = date_ical_date($rrule['UNTIL'], 'UTC');
         date_timezone_set($until, date_default_timezone_object());
         $description['!until'] = trim(t('!repeats_every_interval until !until_date', array('!repeats_every_interval ' => '', '!until_date' => date_format_date($until, 'custom', $format))));
     }
     if ($exceptions) {
         $values = array();
         foreach ($exceptions as $exception) {
             $values[] = date_format_date(date_ical_date($exception), 'custom', $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) {
             $values[] = date_format_date(date_ical_date($addition), 'custom', $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);
 }
/**
 * Custom repeat rule theme function for dates.
 *
 * @see date_repeat_rrule_description().
 */
function ringstedtheme_repeat_rrule_description($rrule, $format = 'd.M')
{
    // Empty or invalid value.
    if (empty($rrule) || !strstr($rrule, 'RRULE')) {
        return;
    }
    module_load_include('inc', 'date_api', 'date_api_ical');
    module_load_include('inc', 'date_repeat', 'date_repeat_calc');
    $parts = date_repeat_split_rrule($rrule);
    $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':
            if (isset($rrule['BYDAY'])) {
                $description['!interval'] = format_plural($rrule['INTERVAL'], '', 'every @count weeks') . ' ';
            } else {
                $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();
        $first_day = $rrule['BYDAY'][0];
        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' => strtolower($days[$day]))));
            } else {
                if ($first_day === $byday) {
                    $results[] = trim(t('!repeats_every_interval every  !day_of_week', array('!repeats_every_interval ' => '', '!day_of_week' => strtolower($days[$day]))));
                } else {
                    $results[] = trim(t('!repeats_every_interval !day_of_week', array('!repeats_every_interval ' => '', '!day_of_week' => strtolower($days[$day]))));
                }
            }
        }
        $days_count = count($rrule['BYDAY']);
        if ($days_count > 2) {
            $last_day = array_pop($results);
            $description['!byday'] = implode(' , ', $results);
            $description['!byday'] .= ' ' . t('and') . ' ' . $last_day;
        } else {
            $description['!byday'] = implode(' ' . t('and') . ' ', $results);
        }
    }
    if (!empty($rrule['BYMONTH'])) {
        if (sizeof($rrule['BYMONTH']) < 12) {
            $results = array();
            $months = date_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 = date_ical_date($rrule['UNTIL'], 'UTC');
        date_timezone_set($until, date_default_timezone_object());
        $description['!until'] = trim(t('!repeats_every_interval until !until_date', array('!repeats_every_interval ' => '', '!until_date' => strtolower(date_format_date($until, 'custom', $format)))));
    }
    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'])])));
    }
    return preg_replace('/\\s*\\./', '.', t('Repeats !interval !bymonth !byday !count !until !except. !additional', $description));
}