### Eclipse Workspace Patch 1.0
#P date
Index: date_repeat/date_repeat.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/date/date_repeat/date_repeat.module,v
retrieving revision 1.30.4.12
diff -u -r1.30.4.12 date_repeat.module
--- date_repeat/date_repeat.module	12 May 2009 09:58:59 -0000	1.30.4.12
+++ date_repeat/date_repeat.module	1 Oct 2009 06:51:28 -0000
@@ -1,4 +1,5 @@
 <?php
+
 // $Id: date_repeat.module,v 1.30.4.12 2009/05/12 09:58:59 karens Exp $
 /**
  * @file
@@ -18,39 +19,23 @@
  * Implementation of hook_elements().
  */
 function date_repeat_elements() {
-  $type['date_repeat_rrule'] = array(
-    '#input' => TRUE,
-    '#process' => array('date_repeat_rrule_process'),
-    '#element_validate' => array('date_repeat_rrule_validate'),
-  );
+  $type['date_repeat_rrule'] = array('#input' => TRUE, '#process' => array('date_repeat_rrule_process'), '#element_validate' => array('date_repeat_rrule_validate'));
   return $type;
 }
 
 function date_repeat_theme() {
-  return array(
-    'date_repeat' => array('arguments' => array('element' => NULL)),
-    'date_repeat_current_exceptions' => array('arguments' => array('element' => NULL)),
-  );
+  return array('date_repeat' => array('arguments' => array('element' => NULL)), 'date_repeat_current_exceptions' => array('arguments' => array('element' => NULL)));
 }
 
 /**
  * Helper function for FREQ options.
  */
 function FREQ_options() {
-  return array(
-    'NONE' => t('-- Period'),
-    'DAILY' => date_t('Days', 'datetime_plural'),
-    'WEEKLY' => date_t('Weeks', 'datetime_plural'),
-    'MONTHLY' => date_t('Months', 'datetime_plural'),
-    'YEARLY' => date_t('Years', 'datetime_plural'),
-  );
+  return array('NONE' => t('-- Period'), 'DAILY' => date_t('Days', 'datetime_plural'), 'WEEKLY' => date_t('Weeks', 'datetime_plural'), 'MONTHLY' => date_t('Months', 'datetime_plural'), 'YEARLY' => date_t('Years', 'datetime_plural'));
 }
 
 function INTERVAL_options() {
-  $options = array(
-    0 => t('-- Frequency'),
-    1 => date_t('Every', 'date_order'),
-  );
+  $options = array(0 => t('-- Frequency'), 1 => date_t('Every', 'date_order'));
   for ($i = 2; $i < 367; $i++) {
     $options[$i] = t('Every @number', array('@number' => $i));
   }
@@ -65,15 +50,7 @@
  * values when displayed to user.
  */
 function date_repeat_dow_day_options($translated = TRUE) {
-  return array(
-    'SU' => $translated ? date_t('Sunday', 'day_name') : 'Sunday',
-    'MO' => $translated ? date_t('Monday', 'day_name') : 'Monday',
-    'TU' => $translated ? date_t('Tuesday', 'day_name') : 'Tuesday',
-    'WE' => $translated ? date_t('Wednesday', 'day_name') : 'Wednesday',
-    'TH' => $translated ? date_t('Thursday', 'day_name') : 'Thursday',
-    'FR' => $translated ? date_t('Friday', 'day_name') : 'Friday',
-    'SA' => $translated ? date_t('Saturday', 'day_name') : 'Saturday',
-  );
+  return array('SU' => $translated ? date_t('Sunday', 'day_name') : 'Sunday', 'MO' => $translated ? date_t('Monday', 'day_name') : 'Monday', 'TU' => $translated ? date_t('Tuesday', 'day_name') : 'Tuesday', 'WE' => $translated ? date_t('Wednesday', 'day_name') : 'Wednesday', 'TH' => $translated ? date_t('Thursday', 'day_name') : 'Thursday', 'FR' => $translated ? date_t('Friday', 'day_name') : 'Friday', 'SA' => $translated ? date_t('Saturday', 'day_name') : 'Saturday');
 }
 
 function date_repeat_dow_day_options_ordered($week_start) {
@@ -103,7 +80,7 @@
   $options = array();
   foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
     foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
-      $options[$count_key . $dow_key] = $count_value .' '. $dow_value;
+      $options[$count_key . $dow_key] = $count_value . ' ' . $dow_value;
     }
   }
   return $options;
@@ -150,91 +127,136 @@
     return;
   }
   
-  require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_ical.inc');
-  require_once('./'. drupal_get_path('module', 'date_repeat') .'/date_repeat_calc.inc');
+  require_once ('./' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc');
+  require_once ('./' . drupal_get_path('module', 'date_repeat') . '/date_repeat_calc.inc');
   
   // Make sure there will be an empty description for any unused parts.
-  $description = array(
-    '!interval' => '', 
-    '!byday' => '', 
-    '!bymonth' => '', 
-    '!count' => '',
-    '!until' => '', 
-    '!except' => '',
-    '!week_starts_on' => '',
-    );
+  $description = array('!interval' => '', '!byday' => '', '!bymonth' => '', '!count' => '', '!until' => '', '!except' => '', '!week_starts_on' => '');
   $parts = date_repeat_split_rrule($rrule);
   $exceptions = $parts[1];
   $rrule = $parts[0];
   $interval = 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 ($rrule['INTERVAL'] < 1) {
+    $rrule['INTERVAL'] = 1;
   }
   
+  if ((empty($rrule['BYDAY']) && empty($rrule['BYMONTHDAY']) && empty($rrule['BYMONTH']) || $rrule['FREQ'] == 'DAILY' || $rrule['FREQ'] == 'WEEKLY') || $rrule['INTERVAL'] > 1) {
+    switch ($rrule['FREQ']) {
+      case 'WEEKLY' :
+        $description['!interval'] = format_plural($rrule['INTERVAL'], 'weekly', 'every @count weeks') . ' ';
+        break;
+      case 'MONTHLY' :
+        $description['!interval'] = format_plural($rrule['INTERVAL'], 'monthly', 'every @count months') . ' ';
+        break;
+      case 'YEARLY' :
+        $description['!interval'] = format_plural($rrule['INTERVAL'], 'annually', 'every @count years') . ' ';
+        break;
+      default :
+        $description['!interval'] = format_plural($rrule['INTERVAL'], 'daily', 'every @count days') . ' ';
+        break;
+    }
+  }
+  
+  $days = date_repeat_dow_day_options();
+  $counts = date_repeat_dow_count_options();
+  
+  //Sort the by-day results if any into two categories, 
+  //those occurring weekly, and those occurring on the nth ordinal per month, grouped by ordinal
+  $ordinalResults = array();
+  $weeklyResults = array();
   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[$count]), '!day_of_week' => $days[$day])));
+      $strCount = substr($byday, 0, 2);
+      if ($count = intval($strCount)) {
+        $instance = trim(t($days[$day]));
+        if (empty($ordinalResults[$count])) {
+          $ordinalResults[$count] = array();
+        }
+        $ordinalResults[$count][] = $instance;
+      } else {
+        $weeklyResults[] = trim(t($days[$day]));
       }
-      else {
-        $results[] = trim(t('!repeats_every_interval every !day_of_week', array('!repeats_every_interval ' => '', '!day_of_week' => $days[$day])));
+    }
+    
+    $byDayResults = array();
+    if (!empty($weeklyResults)) {
+      $byDayResults[] = t('on') . ' ' . trim(t(implodeGrammar($weeklyResults)));
+    }
+    if (!empty($ordinalResults)) {
+      $resultsByOrdinal = array();
+      //TODO: Put this instances in the correct sequence. Currently they are 'fifth from last' to 'last', followed by 'first' to 'fifth'
+      //      They should probably be in order from earliest to latest so that 'first' comes before 'last'
+      for ($i = -5; $i <= 5; $i++) {
+        if (!empty($ordinalResults['' . $i])) {
+          $ordinal = $i < 0 ? $i : ($i > 0 ? '+' . $i : '');
+          $resultsByOrdinal[] = t('the') . ' ' . strtolower(trim(t($counts[$ordinal]))) . ' ' . implodeGrammar($ordinalResults[$i]);
+        }
       }
+      $byDayResults[] = t('on') . ' ' . trim(t(implodeGrammar($resultsByOrdinal))) . ' ' . t('of the month');
     }
-    $description['!byday'] = implode(' '. t('and') .' ', $results);
+    $description['!byday'] = implodeGrammar($byDayResults);
   }
-  if (!empty($rrule['BYMONTH'])) {
+  
+  if (!empty($rrule['BYMONTH']) || $rrule['FREQ'] == 'MONTHLY') {
     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))));
+      $monthdays = $rrule['BYMONTHDAY'];
+      if (!empty($monthdays)) {
+        if (empty($results)) {
+          $results[0] = "the month";
+        }
+        $description['!bymonth'] = trim(t('on day' . (count($monthdays) > 1 ? 's' : '') . '!month_days of !month_names', array('!month_days' => implodeGrammar($monthdays), '!month_names' => implodeGrammar($results))));
+      } elseif (!empty($rrule['BYMONTH'])) {
+        $description['!bymonth'] = trim(t('during !month_names', array('!month_names' => implodeGrammar($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'])));
+    $description['!count'] = trim(t('!count times', array('!count' => $rrule['COUNT'])));
   }
   if (!empty($rrule['UNTIL'])) {
     $until = date_ical_date($rrule['UNTIL']);
-    $description['!until'] = trim(t('!repeats_every_interval until !until_date', array('!repeats_every_interval ' => '', '!until_date' => date_format_date($until, 'custom', $format))));
+    $description['!until'] = trim(t('until !until_date', array('!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))));
+    $description['!except'] = trim(t('except on !except_dates', array('!except_dates' => implodeGrammar($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'])])));
+    $description['!week_starts_on'] = trim(t('where the week start on !day_of_week', array('!day_of_week' => $day_names[trim($rrule['WKST'])])));
   }
-  return t('Repeats !interval !bymonth !byday !count !until !except.', $description);
+  return trim(t('Repeats !interval !byday !count !bymonth !until !except', $description)) . '.';
+}
+
+/**
+ * Implodes an array of strings using punctuation and/or a conjunction, depending on the number of items in the array
+ */
+function implodeGrammar($terms, $punctuation = ',', $conjunction = 'and') {
+  $conjunction = trim(t($conjunction));
+  $count = count($terms);
+  if ($count == 1) {
+    return $terms[0];
+  } elseif ($count == 2) {
+    return implode(' ' . $conjunction . ' ', $terms);
+  } else {
+    $result = array();
+    for ($i = 0; $i < $count - 1; $i++) {
+      $result[] = $terms[$i];
+    }
+    $result[] = ' ' . $conjunction . ' ' . $terms[$count - 1];
+    return implode($punctuation . ' ', $result);
+  }
+  return '';
 }
 
 /**
@@ -247,11 +269,10 @@
   foreach ($parts as $part) {
     if (strstr($part, 'RRULE')) {
       $RRULE = str_replace('RRULE:', '', $part);
-      $rrule = (array) date_ical_parse_rrule('RRULE:', $RRULE);
-    }
-    elseif (strstr($part, 'EXDATE')) {
+      $rrule = (array)date_ical_parse_rrule('RRULE:', $RRULE);
+    } elseif (strstr($part, 'EXDATE')) {
       $EXDATE = str_replace('EXDATE:', '', $part);
-      $exceptions = (array) date_ical_parse_exceptions('EXDATE:', $EXDATE);
+      $exceptions = (array)date_ical_parse_exceptions('EXDATE:', $EXDATE);
       unset($exceptions['DATA']);
     }
   }
@@ -262,7 +283,7 @@
  * Analyze a RRULE and return dates that match it.
  */
 function date_repeat_calc($rrule, $start, $end, $exceptions = array(), $timezone = NULL) {
-  require_once('./'. drupal_get_path('module', 'date_repeat') .'/date_repeat_calc.inc');
+  require_once ('./' . drupal_get_path('module', 'date_repeat') . '/date_repeat_calc.inc');
   return _date_repeat_calc($rrule, $start, $end, $exceptions, $timezone);
 }
 
@@ -270,6 +291,6 @@
  * Generate the repeat rule setting form.
  */
 function date_repeat_rrule_process($element, $edit, $form_state, $form) {
-  require_once('./'. drupal_get_path('module', 'date_repeat') .'/date_repeat_form.inc');
+  require_once ('./' . drupal_get_path('module', 'date_repeat') . '/date_repeat_form.inc');
   return _date_repeat_rrule_process($element, $edit, $form_state, $form);
 }
