From 02ca82f85e251402a40183c605944369048e3e5b Mon Sep 17 00:00:00 2001 From: denise Date: Fri, 7 Jun 2013 11:16:19 -0400 Subject: [PATCH] CC-5209: Calendar -> Repeat by 5th day of the week problem --- .../application/services/ShowService.php | 101 ++++++++++++------ 1 file changed, 66 insertions(+), 35 deletions(-) diff --git a/airtime_mvc/application/services/ShowService.php b/airtime_mvc/application/services/ShowService.php index 7430aa5e6..f0adcbfff 100644 --- a/airtime_mvc/application/services/ShowService.php +++ b/airtime_mvc/application/services/ShowService.php @@ -196,20 +196,18 @@ class Application_Service_ShowService $this->createNonRepeatingInstance($day, $populateUntil); break; case REPEAT_WEEKLY: - $this->createRepeatingInstances($day, $populateUntil, REPEAT_WEEKLY, + $this->createWeeklyRepeatInstances($day, $populateUntil, REPEAT_WEEKLY, new DateInterval("P7D"), $daysAdded); break; case REPEAT_BI_WEEKLY: - $this->createRepeatingInstances($day, $populateUntil, REPEAT_BI_WEEKLY, + $this->createWeeklyRepeatInstances($day, $populateUntil, REPEAT_BI_WEEKLY, new DateInterval("P14D"), $daysAdded); break; case REPEAT_MONTHLY_MONTHLY: - $this->createMonthlyMonthlyRepeatInstances($day, $populateUntil); + $this->createMonthlyRepeatInstances($day, $populateUntil); break; case REPEAT_MONTHLY_WEEKLY: - /* $this->createRepeatingInstances($day, $populateUntil, REPEAT_MONTHLY_WEEKLY, - null, $daysAdded); */ - $this->createMonthlyMonthlyRepeatInstances($day, $populateUntil); + $this->createMonthlyRepeatInstances($day, $populateUntil); break; } } @@ -323,7 +321,7 @@ SQL; $this->deleteAllRepeatInstances($currentShowDay, $showId); //if repeat option was checked we need to treat the current show day //as a new show day so the repeat instances get created properly - //in createRepeatingInstances() + //in createWeeklyRepeatInstances() if ($showData['add_show_repeats']) { array_push($daysAdded, $currentShowDay->getDbDay()); } @@ -847,7 +845,7 @@ SQL; * @param unknown_type $repeatInterval * @param unknown_type $isRebroadcast */ - private function createRepeatingInstances($showDay, $populateUntil, + private function createWeeklyRepeatInstances($showDay, $populateUntil, $repeatType, $repeatInterval, $daysAdded=null) { $show_id = $showDay->getDbShowId(); @@ -940,7 +938,7 @@ SQL; $this->setNextRepeatingShowDate($nextDate->format("Y-m-d"), $day, $show_id); } - private function createMonthlyMonthlyRepeatInstances($showDay, $populateUntil) + private function createMonthlyRepeatInstances($showDay, $populateUntil) { $show_id = $showDay->getDbShowId(); $first_show = $showDay->getDbFirstShow(); //non-UTC @@ -958,6 +956,13 @@ SQL; $end = $populateUntil; } + // We will only need this if the repeat type is MONTHLY_WEEKLY + list($weekNumberOfMonth, $dayOfWeek) = + $this->getMonthlyWeeklyRepeatInterval( + new DateTime($first_show, new DateTimeZone($timezone))); + + $this->repeatType = $showDay->getDbRepeatType(); + $utcLastShowDateTime = $last_show ? Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null; @@ -1006,7 +1011,20 @@ SQL; $this->createRebroadcastInstances($showDay, $start, $ccShowInstance->getDbId()); } } - $start = $this->getNextMonthlyMonthlyRepeatDate($start, $timezone, $showDay->getDbStartTime()); + if ($this->repeatType == REPEAT_MONTHLY_WEEKLY) { + $monthlyWeeklyStart = new DateTime($utcStartDateTime->format("Y-m"), + new DateTimeZone("UTC")); + $monthlyWeeklyStart->add(new DateInterval("P1M")); + $start = $this->getNextMonthlyWeeklyRepeatDate( + $monthlyWeeklyStart, + $timezone, + $showDay->getDbStartTime(), + $weekNumberOfMonth, + $dayOfWeek); + } else { + $start = $this->getNextMonthlyMonthlyRepeatDate( + $start, $timezone, $showDay->getDbStartTime()); + } } $this->setNextRepeatingShowDate($start->format("Y-m-d"), $day, $show_id); } @@ -1068,31 +1086,9 @@ SQL; { $dt = new DateTime($start->format("Y-m"), new DateTimeZone($timezone)); - if ($this->repeatType == REPEAT_MONTHLY_WEEKLY) { - list($weekNumberOfMonth, $dayOfWeek) = $this->getMonthlyWeeklyRepeatInterval($start); - $tempDT = clone $dt; - $keepGoing = true; - do { - $nextDT = date_create($weekNumberOfMonth." ".$dayOfWeek. - " of ".$tempDT->format("F")." ".$tempDT->format("Y")); - -Logging::info($weekNumberOfMonth." ".$dayOfWeek." of ".$tempDT->format("F")." ".$tempDT->format("Y")); -Logging::info($tempDT->format("Y-m-d")); -Logging::info($nextDT->format("Y-m-d")); -Logging::info("-----------"); - - if ($tempDT->format("F") == $nextDT->format("F")) { - $keepGoing = false; - } - $tempDT->add(new DateInterval("P1M")); - } while ($keepGoing); - $dt = $nextDT; -Logging::info($dt); - } else { - do { - $dt->add(new DateInterval("P1M")); - } while (!checkdate($dt->format("m"), $start->format("d"), $dt->format("Y"))); - } + do { + $dt->add(new DateInterval("P1M")); + } while (!checkdate($dt->format("m"), $start->format("d"), $dt->format("Y"))); $dt->setDate($dt->format("Y"), $dt->format("m"), $start->format("d")); @@ -1104,6 +1100,41 @@ Logging::info($dt); return $dt; } + private function getNextMonthlyWeeklyRepeatDate( + $start, + $timezone, + $startTime, + $weekNumberOfMonth, + $dayOfWeek) + { + $dt = new DateTime($start->format("Y-m"), new DateTimeZone($timezone)); + $tempDT = clone $dt; + $fifthWeekExists = false; + do { + $nextDT = date_create($weekNumberOfMonth." ".$dayOfWeek. + " of ".$tempDT->format("F")." ".$tempDT->format("Y")); + + /* We have to check if the next date is in the same month in case + * the repeat day is in the fifth week of the month. + * If it's not in the same month we know that a fifth week of + * the next month does not exist. So let's skip it. + */ + if ($tempDT->format("F") == $nextDT->format("F")) { + $fifthWeekExists = true; + } + $tempDT->add(new DateInterval("P1M")); + } while (!$fifthWeekExists); + + $dt = $nextDT; + + $startTime = explode(":", $startTime); + $hours = isset($startTime[0]) ? $startTime[0] : "00"; + $minutes = isset($startTime[1]) ? $startTime[1] : "00"; + $seconds = isset($startTime[2]) ? $startTime[2] : "00"; + $dt->setTime($hours, $minutes, $seconds); + return $dt; + } + private function getNextRepeatingPopulateStartDateTime($showDay) { $nextPopDate = $showDay->getDbNextPopDate();