From d8ced29dd04e3e348d46fb9b372fd765800c0108 Mon Sep 17 00:00:00 2001 From: denise Date: Fri, 8 Mar 2013 16:08:43 -0500 Subject: [PATCH] Refactored adding show forms to the view --- .../controllers/ScheduleController.php | 57 +++++++++++-------- .../services/ShowInstanceService.php | 40 +++++++++---- .../application/services/ShowService.php | 13 ++++- 3 files changed, 73 insertions(+), 37 deletions(-) diff --git a/airtime_mvc/application/controllers/ScheduleController.php b/airtime_mvc/application/controllers/ScheduleController.php index ea95d4bc7..50408ff85 100644 --- a/airtime_mvc/application/controllers/ScheduleController.php +++ b/airtime_mvc/application/controllers/ScheduleController.php @@ -94,20 +94,7 @@ class ScheduleController extends Zend_Controller_Action $this->view->headLink()->appendStylesheet($baseUrl.'css/showbuilder.css?'.$CC_CONFIG['airtime_version']); //End Show builder JS/CSS requirements - $forms = $this->service_schedule->createShowForms(); - // populate forms with default values - $this->service_schedule->populateNewShowForms( - $forms["what"], $forms["when"], $forms["repeats"]); - - $this->view->what = $forms["what"]; - $this->view->when = $forms["when"]; - $this->view->repeats = $forms["repeats"]; - $this->view->live = $forms["live"]; - $this->view->rr = $forms["record"]; - $this->view->absoluteRebroadcast = $forms["abs_rebroadcast"]; - $this->view->rebroadcast = $forms["rebroadcast"]; - $this->view->who = $forms["who"]; - $this->view->style = $forms["style"]; + $this->createShowFormAction(true); $user = Application_Model_User::getCurrentUser(); if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { @@ -746,7 +733,8 @@ class ScheduleController extends Zend_Controller_Action $user = Application_Model_User::getCurrentUser(); if ($user->isUserType(array(UTYPE_ADMIN, UTYPE_PROGRAM_MANAGER))) { - Application_Model_Schedule::createNewFormSections($this->view); + //Application_Model_Schedule::createNewFormSections($this->view); + $this->createShowFormAction(true); $this->view->form = $this->view->render('schedule/add-show-form.phtml'); } } @@ -879,8 +867,32 @@ class ScheduleController extends Zend_Controller_Action $data['add_show_day_check'] = null; } + $forms = $this->createShowFormAction(); + + $this->view->addNewShow = true; + + if ($this->service_schedule->validateShowForms($forms, $data)) { + $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); + $this->service_schedule->createShow($data); + + //send new show forms to the user + $this->createShowFormAction(true); + Logging::debug("Show creation succeeded"); + } else { + $this->view->form = $this->view->render('schedule/add-show-form.phtml'); + Logging::debug("Show creation failed"); + } + } + + public function createShowFormAction($populate=false) + { $forms = $this->service_schedule->createShowForms(); + // populate forms with default values + if ($populate) { + $this->populateNewShowFormsAction($forms); + } + $this->view->what = $forms["what"]; $this->view->when = $forms["when"]; $this->view->repeats = $forms["repeats"]; @@ -891,16 +903,13 @@ class ScheduleController extends Zend_Controller_Action $this->view->who = $forms["who"]; $this->view->style = $forms["style"]; - $this->view->addNewShow = true; + return $forms; + } - if ($this->service_schedule->validateShowForms($forms, $data)) { - $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); - $this->service_schedule->createShow($data); - Logging::debug("Show creation succeeded"); - } else { - $this->view->form = $this->view->render('schedule/add-show-form.phtml'); - Logging::debug("Show creation failed"); - } + public function populateNewShowFormsAction($forms) + { + $this->service_schedule->populateNewShowForms( + $forms["what"], $forms["when"], $forms["repeats"]); } public function cancelShowAction() diff --git a/airtime_mvc/application/services/ShowInstanceService.php b/airtime_mvc/application/services/ShowInstanceService.php index 1e1eeb0f4..09d70e9c3 100644 --- a/airtime_mvc/application/services/ShowInstanceService.php +++ b/airtime_mvc/application/services/ShowInstanceService.php @@ -67,7 +67,7 @@ class Application_Service_ShowInstanceService $ccShowInstance->save(); if ($isRebroadcast) { - self::createRebroadcastShowInstances($showDay, $start, $ccShowInstance->getDbId()); + $this->createRebroadcastShowInstances($showDay, $start, $ccShowInstance->getDbId()); } } } @@ -101,17 +101,14 @@ class Application_Service_ShowInstanceService $start = $first_show." ".$start_time; } - /* - * Create a DatePeriod object in the user's local time - * It will get converted to UTC before the show instance gets created - */ - $period = new DatePeriod(new DateTime($start, new DateTimeZone($timezone)), - new DateInterval($repeatInterval), $populateUntil); + $period = $this->getDatePeriod($start, $timezone, $last_show, + $repeatInterval, $populateUntil); $utcStartDateTime = Application_Common_DateHelper::ConvertToUtcDateTime($start, $timezone); - //convert $last_show into a UTC DateTime object, or null if there is no last show. - $utcLastShowDateTime = $last_show ? Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null; + $utcLastShowDateTime = $last_show ? + Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null; + $utcEndDateTime = null; foreach ($period as $date) { list($utcStartDateTime, $utcEndDateTime) = $this->service_show->createUTCStartEndDateTime( $date->format("Y-m-d H:i:s"), $duration, $timezone); @@ -120,7 +117,8 @@ class Application_Service_ShowInstanceService * last show date is null OR start date is less than last show date */ if ($utcStartDateTime->getTimestamp() <= $populateUntil->getTimestamp() && - is_null($utcLastShowDateTime) || $utcStartDateTime->getTimestamp() < $utcLastShowDateTime->getTimestamp()) { + ( is_null($utcLastShowDateTime) || + $utcStartDateTime->getTimestamp() < $utcLastShowDateTime->getTimestamp()) ) { $ccShowInstance = new CcShowInstances(); $ccShowInstance->setDbShowId($show_id); @@ -130,11 +128,12 @@ class Application_Service_ShowInstanceService $ccShowInstance->save(); if ($isRebroadcast) { - self::createRebroadcastShowInstances($showDay, $date->format("Y-m-d"), $ccShowInstance->getDbId()); + $this->createRebroadcastShowInstances($showDay, $date->format("Y-m-d"), $ccShowInstance->getDbId()); } } } - $this->service_show->setNextPopulateUntilDate($nextDate, $show_id, $day); + $nextDate = $utcEndDateTime->add(new DateInterval($repeatInterval)); + $this->service_show->setNextRepeatingShowDate($nextDate->format("Y-m-d"), $show_id, $day); } /** @@ -176,4 +175,21 @@ class Application_Service_ShowInstanceService { } + + /** + * + * Create a DatePeriod object in the user's local time + * It will get converted to UTC before the show instance gets created + */ + private function getDatePeriod($start, $timezone, $lastShow, $repeatInterval, $populateUntil) + { + if (isset($lastShow)) { + $endDatePeriod = new DateTime($lastShow, new DateTimeZone($timezone)); + } else { + $endDatePeriod = $populateUntil; + } + + return new DatePeriod(new DateTime($start, new DateTimeZone($timezone)), + new DateInterval($repeatInterval), $endDatePeriod); + } } \ No newline at end of file diff --git a/airtime_mvc/application/services/ShowService.php b/airtime_mvc/application/services/ShowService.php index 13605963d..d8a8e3b1f 100644 --- a/airtime_mvc/application/services/ShowService.php +++ b/airtime_mvc/application/services/ShowService.php @@ -227,7 +227,18 @@ class Application_Service_ShowService return array($startDateTime, $endDateTime); } - public function setNextPopulateUntilDate($nextDate, $showId, $day) + /** + * + * Show instances for repeating shows only get created up + * until what is visible on the calendar. We need to set the + * date for when the next repeating show instance should be created + * as the user browses the calendar further. + * + * @param $nextDate + * @param $showId + * @param $day + */ + public function setNextRepeatingShowDate($nextDate, $showId, $day) { $nextInfo = explode(" ", $nextDate);