From 6766d208aa094f365b97ddbdff0bc057554c31cf Mon Sep 17 00:00:00 2001 From: denise Date: Thu, 21 Mar 2013 16:31:05 -0400 Subject: [PATCH] CC-4961: Show linking Removed logic out of edit-show action Created new SchedulerService Fixed overlapping show bug --- .../controllers/ScheduleController.php | 101 +----------------- airtime_mvc/application/forms/AddShowWhen.php | 3 +- airtime_mvc/application/models/Schedule.php | 14 +-- .../application/models/airtime/CcShow.php | 79 ++++++++------ .../application/models/airtime/CcShowDays.php | 5 +- .../application/services/ScheduleService.php | 26 ----- .../application/services/SchedulerService.php | 85 +++++++++++++++ .../application/services/ShowFormService.php | 21 +++- .../application/services/ShowService.php | 37 +++++-- 9 files changed, 189 insertions(+), 182 deletions(-) delete mode 100644 airtime_mvc/application/services/ScheduleService.php create mode 100644 airtime_mvc/application/services/SchedulerService.php diff --git a/airtime_mvc/application/controllers/ScheduleController.php b/airtime_mvc/application/controllers/ScheduleController.php index 7f53fe798..1d55f282e 100644 --- a/airtime_mvc/application/controllers/ScheduleController.php +++ b/airtime_mvc/application/controllers/ScheduleController.php @@ -550,105 +550,11 @@ class ScheduleController extends Zend_Controller_Action $service_showForm->delegateFormPopulation($forms); - /*$show = new Application_Model_Show($showInstance->getShowId()); - - $formWhat->populate(array('add_show_id' => $show->getId(), - 'add_show_instance_id' => $showInstanceId, - 'add_show_name' => $show->getName(), - 'add_show_url' => $show->getUrl(), - 'add_show_genre' => $show->getGenre(), - 'add_show_description' => $show->getDescription()));*/ - - /*$startsDateTime = new DateTime($show->getStartDate()." ".$show->getStartTime(), new DateTimeZone("UTC")); - $endsDateTime = new DateTime($show->getEndDate()." ".$show->getEndTime(), new DateTimeZone("UTC")); - - $startsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); - $endsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get())); - - $formWhen->populate(array('add_show_start_date' => $startsDateTime->format("Y-m-d"), - 'add_show_start_time' => $startsDateTime->format("H:i"), - 'add_show_end_date_no_repeat' => $endsDateTime->format("Y-m-d"), - 'add_show_end_time' => $endsDateTime->format("H:i"), - 'add_show_duration' => $show->getDuration(true), - 'add_show_repeats' => $show->isRepeating() ? 1 : 0)); - - if ($show->isStartDateTimeInPast()) { - // for a non-repeating show, we should never allow user to change the start time. - // for the repeating show, we should allow because the form works as repeating template form - if (!$showInstance->getShow()->isRepeating()) { - $formWhen->disableStartDateAndTime(); - } else { - $nextFutureRepeatShow = $show->getNextFutureRepeatShowTime(); - $formWhen->getElement('add_show_start_date')->setValue($nextFutureRepeatShow["starts"]->format("Y-m-d")); - $formWhen->getElement('add_show_start_time')->setValue($nextFutureRepeatShow["starts"]->format("H:i")); - $formWhen->getElement('add_show_end_date_no_repeat')->setValue($nextFutureRepeatShow["ends"]->format("Y-m-d")); - $formWhen->getElement('add_show_end_time')->setValue($nextFutureRepeatShow["ends"]->format("H:i")); - } - }*/ - - //need to get the days of the week in the php timezone (for the front end). - /*$days = array(); - $showDays = CcShowDaysQuery::create()->filterByDbShowId($showInstance->getShowId())->find(); - foreach ($showDays as $showDay) { - $showStartDay = new DateTime($showDay->getDbFirstShow(), new DateTimeZone($showDay->getDbTimezone())); - $showStartDay->setTimezone(new DateTimeZone(date_default_timezone_get())); - array_push($days, $showStartDay->format('w')); - } - - $displayedEndDate = new DateTime($show->getRepeatingEndDate(), new DateTimeZone($showDays[0]->getDbTimezone())); - $displayedEndDate->sub(new DateInterval("P1D"));//end dates are stored non-inclusively. - $displayedEndDate->setTimezone(new DateTimeZone(date_default_timezone_get())); - - $formRepeats->populate(array('add_show_repeat_type' => $show->getRepeatType(), - 'add_show_day_check' => $days, - 'add_show_end_date' => $displayedEndDate->format("Y-m-d"), - 'add_show_no_end' => ($show->getRepeatingEndDate() == '')));*/ - - /*$hosts = array(); - $showHosts = CcShowHostsQuery::create()->filterByDbShow($showInstance->getShowId())->find(); - foreach ($showHosts as $showHost) { - array_push($hosts, $showHost->getDbHost()); - } - $formWho->populate(array('add_show_hosts' => $hosts));*/ - - - /*$formStyle->populate(array('add_show_background_color' => $show->getBackgroundColor(), - 'add_show_color' => $show->getColor())); - - $formLive->populate($show->getLiveStreamInfo()); - - $formRecord->populate(array('add_show_record' => $show->isRecorded(), - 'add_show_rebroadcast' => $show->isRebroadcast())); - - $formRecord->getElement('add_show_record')->setOptions(array('disabled' => true));*/ - - - - /*$rebroadcastsRelative = $show->getRebroadcastsRelative(); - $rebroadcastFormValues = array(); - $i = 1; - foreach ($rebroadcastsRelative as $rebroadcast) { - $rebroadcastFormValues["add_show_rebroadcast_date_$i"] = $rebroadcast['day_offset']; - $rebroadcastFormValues["add_show_rebroadcast_time_$i"] = Application_Common_DateHelper::removeSecondsFromTime($rebroadcast['start_time']); - $i++; - } - $formRebroadcast->populate($rebroadcastFormValues);*/ -/* - $rebroadcastsAbsolute = $show->getRebroadcastsAbsolute(); - $rebroadcastAbsoluteFormValues = array(); - $i = 1; - foreach ($rebroadcastsAbsolute as $rebroadcast) { - $rebroadcastAbsoluteFormValues["add_show_rebroadcast_date_absolute_$i"] = $rebroadcast['start_date']; - $rebroadcastAbsoluteFormValues["add_show_rebroadcast_time_absolute_$i"] = $rebroadcast['start_time']; - $i++; - } - $formAbsoluteRebroadcast->populate($rebroadcastAbsoluteFormValues); - if (!$isAdminOrPM) { foreach ($forms as $form) { $form->disable(); } - }*/ + } $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); $this->view->entries = 5; @@ -734,11 +640,6 @@ class ScheduleController extends Zend_Controller_Action //pass in true to indicate we are updating a show $service_show->addUpdateShow($data, true); - $scheduler = new Application_Model_Scheduler(); - $showInstances = CcShowInstancesQuery::create()->filterByDbShowId($data['add_show_id'])->find(); - foreach ($showInstances as $si) { - $scheduler->removeGaps($si->getDbId()); - } $this->view->addNewShow = true; $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); } else { diff --git a/airtime_mvc/application/forms/AddShowWhen.php b/airtime_mvc/application/forms/AddShowWhen.php index ae9ffe5e3..007852ba4 100644 --- a/airtime_mvc/application/forms/AddShowWhen.php +++ b/airtime_mvc/application/forms/AddShowWhen.php @@ -295,7 +295,8 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm $rebroadcastShowStart->setTimezone(new DateTimeZone('UTC')); $rebroadcastShowEnd = clone $rebroadcastShowStart; $rebroadcastShowEnd->add(new DateInterval($durationToAdd)); - $overlapping = Application_Model_Schedule::checkOverlappingShows($rebroadcastShowStart, $rebroadcastShowEnd, $update, $instanceId); + $overlapping = Application_Model_Schedule::checkOverlappingShows($rebroadcastShowStart, + $rebroadcastShowEnd, $update, null, $formData["add_show_id"]); if ($overlapping) { $valid = false; $this->getElement('add_show_duration')->setErrors(array(_('Cannot schedule overlapping shows'))); diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 6b19c5e3d..0796740f3 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -1256,19 +1256,19 @@ WHERE (ends <= :show_end1 AND date(starts) >= (date(:show_end3) - INTERVAL '2 days') AND modified_instance = FALSE SQL; - if (is_null($showId)) { - $sql .= <<collCcShowDayss[0]; } -/** - * Gets an array of CcShowInstances objects which contain a foreign key that references this object. - * - * If the $criteria is not null, it is used to always fetch the results from the database. - * Otherwise the results are fetched from the database the first time, then cached. - * Next time the same method is called without $criteria, the cached collection is returned. - * If this CcShow is new, it will return - * an empty collection or the current collection; the criteria is ignored on a new object. - * - * @param Criteria $criteria optional Criteria object to narrow the query - * @param PropelPDO $con optional connection object - * @return PropelCollection|array CcShowInstances[] List of CcShowInstances objects - * @throws PropelException - */ - public function getFutureCcShowInstancess($criteria = null, PropelPDO $con = null) - { - if(null === $this->collCcShowInstancess || null !== $criteria) { - if ($this->isNew() && null === $this->collCcShowInstancess) { - // return empty collection - $this->initCcShowInstancess(); - } else { - - $collCcShowInstancess = CcShowInstancesQuery::create(null, $criteria) - ->filterByCcShow($this) - ->filterByDbStarts(gmdate("Y-m-d H:i:s"), Criteria::GREATER_THAN) - ->filterByDbModifiedInstance(false) - ->find($con); - if (null !== $criteria) { - return $collCcShowInstancess; - } - $this->collCcShowInstancess = $collCcShowInstancess; - } - } - return $this->collCcShowInstancess; - } + /** + * Gets an array of CcShowInstances objects which contain a foreign key that references this object. + * + * If the $criteria is not null, it is used to always fetch the results from the database. + * Otherwise the results are fetched from the database the first time, then cached. + * Next time the same method is called without $criteria, the cached collection is returned. + * If this CcShow is new, it will return + * an empty collection or the current collection; the criteria is ignored on a new object. + * + * @param Criteria $criteria optional Criteria object to narrow the query + * @param PropelPDO $con optional connection object + * @return PropelCollection|array CcShowInstances[] List of CcShowInstances objects + * @throws PropelException + */ + public function getFutureCcShowInstancess($criteria = null, PropelPDO $con = null) + { + if(null === $this->collCcShowInstancess || null !== $criteria) { + if ($this->isNew() && null === $this->collCcShowInstancess) { + // return empty collection + $this->initCcShowInstancess(); + } else { + $collCcShowInstancess = CcShowInstancesQuery::create(null, $criteria) + ->filterByCcShow($this) + ->filterByDbStarts(gmdate("Y-m-d H:i:s"), Criteria::GREATER_THAN) + ->filterByDbModifiedInstance(false) + ->find($con); + if (null !== $criteria) { + return $collCcShowInstancess; + } + $this->collCcShowInstancess = $collCcShowInstancess; + } + } + return $this->collCcShowInstancess; + } public function isRecorded() { @@ -119,4 +118,14 @@ class CcShow extends BaseCcShow { ->orderByDbDayOffset() ->find(); } + + public function getRebroadcastsAbsolute() + { + return CcShowInstancesQuery::create() + ->filterByDbShowId($this->getDbId()) + ->filterByDbRebroadcast(1) + ->filterByDbModifiedInstance(false) + ->orderByDbStarts() + ->find(); + } } // CcShow diff --git a/airtime_mvc/application/models/airtime/CcShowDays.php b/airtime_mvc/application/models/airtime/CcShowDays.php index 4ed9bd6bc..cef0b333f 100644 --- a/airtime_mvc/application/models/airtime/CcShowDays.php +++ b/airtime_mvc/application/models/airtime/CcShowDays.php @@ -46,8 +46,9 @@ class CcShowDays extends BaseCcShowDays { * Enter description here ... * @param DateTime $startDateTime first show in user's local time */ - public function getLocalEndDateAndTime($startDateTime) + public function getLocalEndDateAndTime($showStart) { + $startDateTime = clone $showStart; $duration = explode(":", $this->getDbDuration()); return $startDateTime->add(new DateInterval('PT'.$duration[0].'H'.$duration[1].'M')); @@ -55,7 +56,7 @@ class CcShowDays extends BaseCcShowDays { public function isShowStartInPast() { - return $this->getUTCStartDateAndTime() > gmdate("Y-m-d H:i:s"); + return $this->getUTCStartDateAndTime()->format("Y-m-d H:i:s") < gmdate("Y-m-d H:i:s"); } public function formatDuration() diff --git a/airtime_mvc/application/services/ScheduleService.php b/airtime_mvc/application/services/ScheduleService.php deleted file mode 100644 index 9ff581542..000000000 --- a/airtime_mvc/application/services/ScheduleService.php +++ /dev/null @@ -1,26 +0,0 @@ - 0 && $diff != 0) { - $showIdList = implode(",", $instanceIds); - $sql = << $diff, ':diff2' => $diff, - ':showIds' => $showIdList), - 'execute'); - } - } -} \ No newline at end of file diff --git a/airtime_mvc/application/services/SchedulerService.php b/airtime_mvc/application/services/SchedulerService.php new file mode 100644 index 000000000..4a4f9172b --- /dev/null +++ b/airtime_mvc/application/services/SchedulerService.php @@ -0,0 +1,85 @@ + 0 && $diff != 0) { + $showIdList = implode(",", $instanceIds); + $sql = << $diff, ':diff2' => $diff, + ':showIds' => $showIdList), + 'execute'); + } + } + + /** + * + * Removes any time gaps in shows + * + * @param array $schedIds schedule ids to exclude + */ + public function removeGaps($showId, $schedIds=null) + { + $ccShowInstances = CcShowInstancesQuery::create()->filterByDbShowId($showId)->find(); + + foreach ($ccShowInstances as $instance) { + Logging::info("Removing gaps from show instance #".$instance->getDbId()); + //DateTime object + $itemStart = $instance->getDbStarts(null); + + $ccScheduleItems = CcScheduleQuery::create() + ->filterByDbInstanceId($instance->getDbId()) + ->filterByDbId($schedIds, Criteria::NOT_IN) + ->orderByDbStarts() + ->find(); + + foreach ($ccScheduleItems as $ccSchedule) { + //DateTime object + $itemEnd = $this->findEndTime($itemStart, $ccSchedule->getDbClipLength()); + + $ccSchedule->setDbStarts($itemStart) + ->setDbEnds($itemEnd); + + $itemStart = $itemEnd; + } + $ccScheduleItems->save(); + } + } + + /** + * + * Enter description here ... + * @param DateTime $instanceStart + * @param string $clipLength + */ + private function findEndTime($instanceStart, $clipLength) + { + $startEpoch = $instanceStart->format("U.u"); + $durationSeconds = Application_Common_DateHelper::playlistTimeToSeconds($clipLength); + + //add two float numbers to 6 subsecond precision + //DateTime::createFromFormat("U.u") will have a problem if there is no decimal in the resulting number. + $endEpoch = bcadd($startEpoch , (string) $durationSeconds, 6); + + $dt = DateTime::createFromFormat("U.u", $endEpoch, new DateTimeZone("UTC")); + + if ($dt === false) { + //PHP 5.3.2 problem + $dt = DateTime::createFromFormat("U", intval($endEpoch), new DateTimeZone("UTC")); + } + + return $dt; + } +} \ No newline at end of file diff --git a/airtime_mvc/application/services/ShowFormService.php b/airtime_mvc/application/services/ShowFormService.php index 38f4129ff..6202f4862 100644 --- a/airtime_mvc/application/services/ShowFormService.php +++ b/airtime_mvc/application/services/ShowFormService.php @@ -89,6 +89,7 @@ class Application_Service_ShowFormService $this->populateFormStyle($forms["style"]); $this->populateFormRecord($forms["record"]); $this->populateFormRebroadcastRelative($forms["rebroadcast"]); + $this->populateFormRebroadcastAbsolute($forms["abs_rebroadcast"]); } private function populateFormWhat($form) @@ -186,7 +187,7 @@ class Application_Service_ShowFormService $form->getElement('add_show_record')->setOptions(array('disabled' => true)); } - public function populateFormRebroadcastRelative($form) + private function populateFormRebroadcastRelative($form) { $relativeRebroadcasts = $this->ccShow->getRebroadcastsRelative(); @@ -202,6 +203,24 @@ class Application_Service_ShowFormService $form->populate($formValues); } + private function populateFormRebroadcastAbsolute($form) + { + $absolutRebroadcasts = $this->ccShow->getRebroadcastsAbsolute(); + + $formValues = array(); + $i = 1; + foreach ($absolutRebroadcasts as $ar) { + //convert dates to user's local time + $start = new DateTime($ar->getDbStarts(), new DateTimeZone("UTC")); + $start->setTimezone(new DateTimeZone(Application_Model_Preference::GetTimezone())); + $formValues["add_show_rebroadcast_date_absolute_$i"] = $start->format("Y-m-d"); + $formValues["add_show_rebroadcast_time_absolute_$i"] = $start->format("H:i"); + $i++; + } + + $form->populate($formValues); + } + /** * * Before we send the form data in for validation, there diff --git a/airtime_mvc/application/services/ShowService.php b/airtime_mvc/application/services/ShowService.php index 746264bdf..982f17b35 100644 --- a/airtime_mvc/application/services/ShowService.php +++ b/airtime_mvc/application/services/ShowService.php @@ -65,6 +65,11 @@ class Application_Service_ShowService //create new ccShowInstances $this->delegateInstanceCreation($isRebroadcast, $isUpdate); + if ($isUpdate) { + $service_scheduler = new Application_Service_SchedulerService(); + $service_scheduler->removeGaps($this->ccShow->getDbId()); + } + $con->commit(); Application_Model_RabbitMq::PushSchedule(); } catch (Exception $e) { @@ -89,16 +94,16 @@ class Application_Service_ShowService foreach ($ccShowDays as $day) { switch ($day->getDbRepeatType()) { case NO_REPEAT: - $this->createNonRepeatingShowInstance($day, $populateUntil, $isRebroadcast, $isUpdate); + $this->createNonRepeatingInstance($day, $populateUntil, $isRebroadcast, $isUpdate); break; case REPEAT_WEEKLY: - $this->createWeeklyRepeatingShowInstances($day, $populateUntil, "P7D", $isRebroadcast, $isUpdate); + $this->createRepeatingInstances($day, $populateUntil, "P7D", $isRebroadcast, $isUpdate); break; case REPEAT_BI_WEEKLY: - $this->createWeeklyRepeatingShowInstances($day, $populateUntil, "P14D", $isRebroadcast, $isUpdate); + $this->createRepeatingInstances($day, $populateUntil, "P14D", $isRebroadcast, $isUpdate); break; case REPEAT_MONTHLY_MONTHLY: - $this->createMonthlyRepeatingShowInstances($day, $populateUntil, "P1M", $isRebroadcast); + $this->createRepeatingInstances($day, $populateUntil, "P1M", $isRebroadcast, $isUpdate); break; case REPEAT_MONTHLY_WEEKLY: // do something here @@ -426,7 +431,7 @@ SQL; foreach ($ccShowInstances as $ccShowInstance) { array_push($instanceIds, $ccShowInstance->getDbId()); } - Application_Service_ScheduleService::updateScheduleStartTime($instanceIds, $diff); + Application_Service_SchedulerService::updateScheduleStartTime($instanceIds, $diff); } /** @@ -507,7 +512,7 @@ SQL; * @param $showDay * @param $populateUntil */ - private function createNonRepeatingShowInstance($showDay, $populateUntil, $isRebroadcast, $isUpdate) + private function createNonRepeatingInstance($showDay, $populateUntil, $isRebroadcast, $isUpdate) { //DateTime object $start = $showDay->getLocalStartDateAndTime(); @@ -545,7 +550,7 @@ SQL; * @param unknown_type $repeatInterval * @param unknown_type $isRebroadcast */ - private function createWeeklyRepeatingShowInstances($showDay, $populateUntil, + private function createRepeatingInstances($showDay, $populateUntil, $repeatInterval, $isRebroadcast, $isUpdate) { $show_id = $showDay->getDbShowId(); @@ -617,6 +622,12 @@ SQL; $this->setNextRepeatingShowDate($nextDate->format("Y-m-d"), $day); } + private function createMonthlyRepeatingInstances($showDay, $populateUntil, + $repeatInterval, $isRebroadcast, $isUpdate) + { + + } + private function getNextRepeatingPopulateStartDateTime($showDay) { $nextPopDate = $showDay->getDbNextPopDate(); @@ -882,17 +893,23 @@ SQL; /** * * Enter description here ... - * @param DateTime $startDateTime user's local time + * @param DateTime $showStart user's local time * @param string $duration time interval (h)h:(m)m(:ss) * @param string $timezone "Europe/Prague" * @param array $offset (days, hours, mins) used for rebroadcast shows * * @return array of 2 DateTime objects, start/end time of the show in UTC */ - private function createUTCStartEndDateTime($startDateTime, $duration, $offset=null) + private function createUTCStartEndDateTime($showStart, $duration, $offset=null) { + $startDateTime = clone $showStart; + if (isset($offset)) { - $startDateTime->add(new DateInterval("P{$offset["days"]}DT{$offset["hours"]}H{$offset["mins"]}M")); + //$offset["hours"] and $offset["mins"] represents the start time + //of a rebroadcast show + $startDateTime = new DateTime($startDateTime->format("Y-m-d")." ". + $offset["hours"].":".$offset["mins"]); + $startDateTime->add(new DateInterval("P{$offset["days"]}D")); } //convert time to UTC $startDateTime->setTimezone(new DateTimeZone('UTC'));