From f9f4e4f1fb13114f785c6b281df0f61c5eeb4e39 Mon Sep 17 00:00:00 2001 From: denise Date: Tue, 12 Mar 2013 18:01:12 -0400 Subject: [PATCH] CC-4961: Show linking Made some progress on show editing --- .../application/models/airtime/CcShowDays.php | 25 ++ .../application/services/ScheduleService.php | 4 +- .../application/services/ShowDaysService.php | 36 ++- .../services/ShowInstanceService.php | 244 +++++++++++++++++- 4 files changed, 281 insertions(+), 28 deletions(-) diff --git a/airtime_mvc/application/models/airtime/CcShowDays.php b/airtime_mvc/application/models/airtime/CcShowDays.php index 72df6047e..7dc9d98bd 100644 --- a/airtime_mvc/application/models/airtime/CcShowDays.php +++ b/airtime_mvc/application/models/airtime/CcShowDays.php @@ -15,4 +15,29 @@ */ class CcShowDays extends BaseCcShowDays { + public function isRepeating() + { + return $this->getDbRepeatType() != -1; + } + + public function getUTCStartDateAndTime() + { + $dt = new DateTime( + "{$this->getDbFirstShow()} {$this->getDbStartTime()}", + new DateTimeZone($this->getDbTimezone()) + ); + $dt->setTimezone(new DateTimeZone("UTC")); + + return $dt; + } + + public function getLocalStartDateAndTime() + { + $dt = new DateTime( + "{$this->getDbFirstShow()} {$this->getDbStartTime()}", + new DateTimeZone($this->getDbTimezone()) + ); + + return $dt; + } } // CcShowDays diff --git a/airtime_mvc/application/services/ScheduleService.php b/airtime_mvc/application/services/ScheduleService.php index bf649212d..72304cdb7 100644 --- a/airtime_mvc/application/services/ScheduleService.php +++ b/airtime_mvc/application/services/ScheduleService.php @@ -223,6 +223,8 @@ class Application_Service_ScheduleService if ($currentUser->isAdminOrPM()) { $ccShow = $this->service_show->setShow($showData, false); + + $this->service_showInstances->updateShowInstances($showData, $isRecorded, $repeatType); } } @@ -259,7 +261,7 @@ class Application_Service_ScheduleService //if the show is repeating, set the start date to the next //repeating instance in the future - if ($currentShowDay->getDbRepeatType() != -1) { + if ($currentShowDay->isRepeating()) { $nextFutureRepeatShow = $this->service_showInstances ->getNextFutureRepeatShowTime($formData["add_show_id"]); $originalShowStartDateTime = $nextFutureRepeatShow["starts"]; diff --git a/airtime_mvc/application/services/ShowDaysService.php b/airtime_mvc/application/services/ShowDaysService.php index 9876fdce5..f5cd6c906 100644 --- a/airtime_mvc/application/services/ShowDaysService.php +++ b/airtime_mvc/application/services/ShowDaysService.php @@ -8,6 +8,24 @@ class Application_Service_ShowDaysService { $this->showId = $id; } + + public function calculateEndDate($showData) + { + if ($showData['add_show_no_end']) { + $endDate = NULL; + } elseif ($showData['add_show_repeats']) { + $endDateTime = new DateTime($showData['add_show_end_date']); + $endDateTime->add(new DateInterval("P1D")); + $endDate = $endDateTime->format("Y-m-d"); + } else { + $endDateTime = new DateTime($showData['add_show_start_date']); + $endDateTime->add(new DateInterval("P1D")); + $endDate = $endDateTime->format("Y-m-d"); + } + + return $endDate; + } + /** * * Sets the fields for a cc_show_days table row @@ -21,17 +39,7 @@ class Application_Service_ShowDaysService { $startDateTime = new DateTime($showData['add_show_start_date']." ".$showData['add_show_start_time']); - if ($showData['add_show_no_end']) { - $endDate = NULL; - } elseif ($showData['add_show_repeats']) { - $endDateTime = new DateTime($showData['add_show_end_date']); - $endDateTime->add(new DateInterval("P1D")); - $endDate = $endDateTime->format("Y-m-d"); - } else { - $endDateTime = new DateTime($showData['add_show_start_date']); - $endDateTime->add(new DateInterval("P1D")); - $endDate = $endDateTime->format("Y-m-d"); - } + $endDate = $this->calculateEndDate($showData); /* What we are doing here is checking if the show repeats or if * any repeating days have been checked. If not, then by default @@ -94,10 +102,12 @@ class Application_Service_ShowDaysService */ public function getShowDays() { - $sql = "SELECT * FROM cc_show_days WHERE show_id = :show_id"; + /*$sql = "SELECT * FROM cc_show_days WHERE show_id = :show_id"; return Application_Common_Database::prepareAndExecute( - $sql, array(":show_id" => $this->showId), 'all'); + $sql, array(":show_id" => $this->showId), 'all');*/ + return CcShowDaysQuery::create()->filterByDbShowId( + $this->showId)->find(); } public function getStartDateAndTime() diff --git a/airtime_mvc/application/services/ShowInstanceService.php b/airtime_mvc/application/services/ShowInstanceService.php index fcf9a6cfa..e3de1f766 100644 --- a/airtime_mvc/application/services/ShowInstanceService.php +++ b/airtime_mvc/application/services/ShowInstanceService.php @@ -30,7 +30,7 @@ class Application_Service_ShowInstanceService $showDays = $this->service_showDays->getShowDays(); foreach ($showDays as $day) { - switch ($day["repeat_type"]) { + switch ($day->getDbRepeatType()) { case NO_REPEAT: $this->createNonRepeatingShowInstance($day, $populateUntil, $isRebroadcast); break; @@ -59,17 +59,17 @@ class Application_Service_ShowInstanceService */ private function createNonRepeatingShowInstance($showDay, $populateUntil, $isRebroadcast) { - $start = $showDay["first_show"]." ".$showDay["start_time"]; + $start = $showDay->getDbFirstShow()." ".$showDay->getDbStartTime(); list($utcStartDateTime, $utcEndDateTime) = $this->service_show->createUTCStartEndDateTime( - $start, $showDay["duration"], $showDay["timezone"]); + $start, $showDay->getDbDuration(), $showDay->getDbTimezone()); if ($utcStartDateTime->getTimestamp() < $populateUntil->getTimestamp()) { $ccShowInstance = new CcShowInstances(); - $ccShowInstance->setDbShowId($showDay["show_id"]); + $ccShowInstance->setDbShowId($showDay->getDbShowId()); $ccShowInstance->setDbStarts($utcStartDateTime); $ccShowInstance->setDbEnds($utcEndDateTime); - $ccShowInstance->setDbRecord($showDay["record"]); + $ccShowInstance->setDbRecord($showDay->getDbRecord()); $ccShowInstance->save(); if ($isRebroadcast) { @@ -89,15 +89,15 @@ class Application_Service_ShowInstanceService private function createWeeklyRepeatingShowInstances($showDay, $populateUntil, $repeatInterval, $isRebroadcast) { - $show_id = $showDay["show_id"]; - $next_pop_date = $showDay["next_pop_date"]; - $first_show = $showDay["first_show"]; //non-UTC - $last_show = $showDay["last_show"]; //non-UTC - $start_time = $showDay["start_time"]; //non-UTC - $duration = $showDay["duration"]; - $day = $showDay["day"]; - $record = $showDay["record"]; - $timezone = $showDay["timezone"]; + $show_id = $showDay->getDbShowId(); + $next_pop_date = $showDay->getDbNextPopDate(); + $first_show = $showDay->getDbFirstShow(); //non-UTC + $last_show = $showDay->getDbLastShow(); //non-UTC + $start_time = $showDay->getDbStartTime(); //non-UTC + $duration = $showDay->getDbDuration(); + $day = $showDay->getDbDay(); + $record = $showDay->getDbRecord(); + $timezone = $showDay->getDbTimezone(); $currentUtcTimestamp = gmdate("Y-m-d H:i:s"); @@ -229,4 +229,220 @@ SQL; return $show; } + + /** + * This function is messy. But sometimes there is no easy way to do it. + * + * When editing a show we may need to perform some actions to reflect the new specs: + * - Delete some show instances + * - Update duration + * - Update start and end time + * + * @param $showData edit show form values in raw form + * @param $isRecorded value computed from the edit show form + * @param $repeatType value computed from the edit show form + */ + public function updateShowInstances($showData, $isRecorded, $repeatType) + { + $showId = $showData["add_show_id"]; + + $this->service_showDays = new Application_Service_ShowDaysService($showId); + //ccShowDays object of the show being edited + $currentShowDay = $this->service_showDays->getCurrentShowDay(); + + $endDate = $this->service_showDays->calculateEndDate($showData); + + //repeat option was toggled + if ($showData['add_show_repeats'] != $currentShowDay->isRepeating()) { + $this->deleteAllRepeatInstances($currentShowDay, $showId); + } + + //duration has changed + if ($showData['add_show_duration'] != $currentShowDay->getDbDuration()) { + $this->updateDuration($showData); + } + + if ($showData['add_show_repeats']) { + + $localShowStart = $currentShowDay->getLocalStartDateAndTime(); + + //if the start date changes, these are the repeat types + //that require show instance deletion + $deleteRepeatTypes = array(REPEAT_BI_WEEKLY, REPEAT_MONTHLY_MONTHLY, + REPEAT_MONTHLY_WEEKLY); + + if (in_array($repeatType, $deleteRepeatTypes) && + $showData["add_show_start_date"] != $localShowStart->format("Y-m-d")) { + + //Start date has changed when repeat type is bi-weekly or monthly. + //This screws up the repeating positions of show instances, so lets + //we need to delete them (CC-2351) + $this->deleteAllInstances($showId); + } + + if ($repeatType != $currentShowDay->getDbRepeatType()) { + //repeat type changed + $this->deleteAllInstances($showId); + } else { + //repeat type is the same, check if the days of the week are the same + $repeatingDaysChanged = false; + $ccShowDays = $this->service_showDays->getShowDays(); + $showDays = array(); + foreach ($ccShowDays as $day) { + $showDays[] = $day->getDbDay(); + } + if (count($showData['add_show_day_check']) == count($showDays)) { + //same number of days checked, lets see if they are the same numbers + $intersect = array_intersect($showData['add_show_day_check'], $showDays); + if (count($intersect) != count($showData['add_show_day_check'])) { + $repeatingDaysChanged = true; + } + } else { + $repeatingDaysChanged = true; + } + + if ($repeatingDaysChanged) { + $daysRemoved = array_diff($showDays, $showData['add_show_day_check']); + + if (count($daysRemoved) > 0) { + //delete repeating show instances for the repeating + //days that were removed + $this->deleteRemovedShowDayInstances($daysRemoved, + $ccShowDays, $showId); + } + } + + if ($showData['add_show_start_date'] != $localShowStart->format("Y-m-d") + || $showData['add_show_start_time'] != $localShowStart->format("H:i:s")){ + + //start date/time has changed + if ($showData['add_show_start_date'] > $localShowStart->format("Y-m-d")) { + $this->deleteInstancesBeforeDate($showData['add_show_start_date'], $showId); + } + + /*$this->updateStartDateTime($showData, $p_endDate);*/ + } + } +/* + //Check if end date for the repeat option has changed. If so, need to take care + //of deleting possible invalid Show Instances. + if ((strlen($this->getRepeatingEndDate()) == 0) == $showData['add_show_no_end']) { + //show "Never Ends" option was toggled. + if ($showData['add_show_no_end']) { + } else { + $this->removeAllInstancesFromDate($p_endDate); + } + } + if ($this->getRepeatingEndDate() != $showData['add_show_end_date']) { + //end date was changed. + + $newDate = strtotime($showData['add_show_end_date']); + $oldDate = strtotime($this->getRepeatingEndDate()); + if ($newDate < $oldDate) { + $this->removeAllInstancesFromDate($p_endDate); + } + }*/ + } + } + + public function deleteAllRepeatInstances($currentShowDay, $showId) + { + $firstShow = $currentShowDay->getUTCStartDateAndTime(); + + $sql = << :timestamp::TIMESTAMP + AND show_id = :showId + AND date(starts) != :firstShow +SQL; + Application_Common_Database::prepareAndExecute( $sql, + array( ':timestamp' => gmdate("Y-m-d H:i:s"), + ':showId' => $showId, + ':firstShow' => $firstShow->format("Y-m-d")), 'execute'); + } + + public function deleteAllInstances($showId) + { + $sql = << :timestamp::TIMESTAMP + AND show_id = :showId +SQL; + Application_Common_Database::prepareAndExecute( $sql, + array( ':timestamp' => gmdate("Y-m-d H:i:s"), + ':showId' => $showId), 'execute'); + } + + /** + * + * Enter description here ... + * @param $daysRemoved array of days removed + * ( + * @param $showDays + * @param $showId + */ + public function deleteRemovedShowDayInstances($daysRemoved, $showDays, $showId) + { + $daysRemovedUTC = array(); + + //convert the start day of the week to UTC + foreach ($showDays as $showDay) { + if (in_array($showDay->getDbDay(), $daysRemoved)) { + $showDay->reload(); + $startDay = $showDay->getUTCStartDateAndTime(); + $daysRemovedUTC[] = $startDay->format('w'); + } + } + + $uncheckedDays = pg_escape_string(implode(",", $daysRemovedUTC)); + + $sql = << :timestamp::TIMESTAMP + AND show_id = :showId +SQL; + + Application_Common_Database::prepareAndExecute( $sql, + array( + ":timestamp" => gmdate("Y-m-d H:i:s"), + ":showId" => $showId, + ), "execute"); + } + + public function deleteInstancesBeforeDate($newStartDate, $showId) + { + $sql = << :timestamp::TIMESTAMP + AND show_id = :showId +SQL; + + Application_Common_Database::prepareAndExecute($sql, array( + ":newStartDate" => $newStartDate, ":timestamp" => gmdate("Y-m-d H:i:s"), + ":showId" => $showId), "execute"); + } + + public function updateDuration($showData) + { + $date = new Application_Common_DateHelper; + $timestamp = $date->getUtcTimestamp(); + + $sql = << :timestamp::TIMESTAMP +SQL; + + Application_Common_Database::prepareAndExecute( $sql, array( + ':add_show_duration' => $showData['add_show_duration'], + ':show_id' => $showData['add_show_id'], + ':timestamp' => $timestamp), "execute"); + } } \ No newline at end of file