示例#1
0
 /**
  * this methode calculate the next date
  */
 function calculate_recurrence($recurrence_row)
 {
     // get the recurrence information
     $recurrence_number = $recurrence_row['recurrence_number'];
     $recurrence_type = $recurrence_row['recurrence_type'];
     $day_time = 86400;
     // 60 sec. * 60 min. * 24 h
     $week_time = 604800;
     // $day_time * 7days
     $date_array = ELHelper::generate_date($recurrence_row['dates'], $recurrence_row['enddates']);
     switch ($recurrence_type) {
         case "1":
             // +1 hour for the Summer to Winter clock change
             $start_day = mktime(1, 0, 0, $date_array["month"], $date_array["day"], $date_array["year"]);
             $start_day = $start_day + $recurrence_number * $day_time;
             break;
         case "2":
             // +1 hour for the Summer to Winter clock change
             $start_day = mktime(1, 0, 0, $date_array["month"], $date_array["day"], $date_array["year"]);
             $start_day = $start_day + $recurrence_number * $week_time;
             break;
         case "3":
             // month recurrence
             /*
              * warning here, we have to make sure the date exists: 31 of october + 1 month = 31 of november, which doesn't exists => skip the date!
              */
             $start_day = mktime(1, 0, 0, $date_array["month"] + $recurrence_number, $date_array["day"], $date_array["year"]);
             $interval = $recurrence_number;
             while ((date('m', $start_day) + 12 - $date_array["month"]) % 12 != $interval) {
                 $interval += $recurrence_number;
                 $start_day = mktime(1, 0, 0, $date_array["month"] + $interval, $date_array["day"], $date_array["year"]);
             }
             break;
         case "4":
             $selected = ELHelper::convert2CharsDaysToInt(explode(',', $recurrence_row['recurrence_byday']), $recurrence_row['weekstart']);
             // the selected weekdays
             $current_weekday = (int) $date_array["weekday"];
             if ($recurrence_row['weekstart'] == 1) {
                 // O for monday, not sunday
                 $current_weekday = ($current_weekday + 6) % 7;
             }
             if (count($selected) == 0) {
                 // this shouldn't happen, but if it does, to prevent problem use the current weekday for the repetition.
                 JError::raiseWarning(500, JText::_('Empty weekday recurrence'));
                 $selected = array($current_weekday);
             }
             sort($selected);
             if ($recurrence_number < 5) {
                 // 1. - 4. week in a month
                 // look for the position of current start date in 'selected' days.
                 if ($current_weekday >= max($selected)) {
                     // last selected day of the week => next occurence is first selected day of next (interval) week
                     $start_day = $date_array["unixtime"] - ($current_weekday - $selected[0]) * $day_time + $week_time * $recurrence_number;
                 } else {
                     // next weekday
                     foreach ($selected as $k => $day) {
                         if ($current_weekday < $day) {
                             $next_weekday = $day;
                             break;
                         }
                     }
                     $start_day = $date_array["unixtime"] + ($next_weekday - $current_weekday) * $day_time;
                 }
             } else {
                 if ($current_weekday >= max($selected)) {
                     // next occurence is next month
                     // the last or the before last week in a month
                     // the last day in the new month
                     $start_day = mktime(1, 0, 0, $date_array["month"] + 2, 1, $date_array["year"]) - $day_time;
                     $weekday_is = date("w", $start_day);
                     // first selected day of the week
                     $weekday_must = $selected[0];
                     // calculate the day difference between these days
                     if ($weekday_is >= $weekday_must) {
                         $day_diff = $weekday_is - $weekday_must;
                     } else {
                         $day_diff = $weekday_is - $weekday_must + 7;
                     }
                     $start_day = $start_day - $day_diff * $day_time;
                     if ($recurrence_number == 6) {
                         // before last?
                         $start_day = $start_day - $week_time;
                     }
                 } else {
                     // next weekday
                     foreach ($selected as $k => $day) {
                         if ($current_weekday < $day) {
                             $next_weekday = $day;
                             break;
                         }
                     }
                     // next occurence is next selected weekday
                     $start_day = $date_array["unixtime"] + ($next_weekday - $current_weekday) * $day_time;
                 }
             }
             break;
     }
     $recurrence_row['dates'] = date("Y-m-d", $start_day);
     if ($recurrence_row['enddates']) {
         $recurrence_row['enddates'] = date("Y-m-d", $start_day + $date_array["day_diff"]);
     }
     if ($start_day < $date_array["unixtime"]) {
         JError::raiseError(500, JText::_('Recurrence date generation error'));
     }
     return $recurrence_row;
 }