diff --git a/airtime_mvc/application/controllers/ScheduleController.php b/airtime_mvc/application/controllers/ScheduleController.php index e9a97b788..e86170863 100644 --- a/airtime_mvc/application/controllers/ScheduleController.php +++ b/airtime_mvc/application/controllers/ScheduleController.php @@ -439,7 +439,7 @@ class ScheduleController extends Zend_Controller_Action $service_showForm = new Application_Service_ShowFormService( $data["add_show_id"], $data["add_show_instance_id"]); - $service_show = new Application_Service_ShowService(); + $service_show = new Application_Service_ShowService(null, $data); $forms = $this->createShowFormAction(); @@ -479,7 +479,7 @@ class ScheduleController extends Zend_Controller_Action $service_showForm = new Application_Service_ShowFormService( $data["add_show_id"]); - $service_show = new Application_Service_ShowService(); + $service_show = new Application_Service_ShowService(null, $data, true); //TODO: move this to js $data['add_show_hosts'] = $this->_getParam('hosts'); @@ -497,8 +497,7 @@ class ScheduleController extends Zend_Controller_Action if ($service_showForm->validateShowForms($forms, $data, $validateStartDate, $originalShowStartDateTime, true, $data["add_show_instance_id"])) { - //pass in true to indicate we are updating a show - $service_show->addUpdateShow($data, true); + $service_show->addUpdateShow($data); $this->view->addNewShow = true; $this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); @@ -519,7 +518,7 @@ class ScheduleController extends Zend_Controller_Action public function addShowAction() { $service_showForm = new Application_Service_ShowFormService(null); - $service_show = new Application_Service_ShowService(); + //$service_show = new Application_Service_ShowService(); $js = $this->_getParam('data'); $data = array(); @@ -529,6 +528,8 @@ class ScheduleController extends Zend_Controller_Action $data[$j["name"]] = $j["value"]; } + $service_show = new Application_Service_ShowService(null, $data); + // TODO: move this to js $data['add_show_hosts'] = $this->_getParam('hosts'); $data['add_show_day_check'] = $this->_getParam('days'); diff --git a/airtime_mvc/application/models/Scheduler.php b/airtime_mvc/application/models/Scheduler.php index 3834499cc..63b1c5219 100644 --- a/airtime_mvc/application/models/Scheduler.php +++ b/airtime_mvc/application/models/Scheduler.php @@ -432,7 +432,7 @@ class Application_Model_Scheduler * array of schedule item info, what gets inserted into cc_schedule * @param $adjustSched */ - private function insertAfter($scheduleItems, $filesToInsert, $adjustSched = true) + private function insertAfter($scheduleItems, $mediaItems, $filesToInsert=null, $adjustSched=true) { try { $affectedShowInstances = array(); @@ -440,11 +440,6 @@ class Application_Model_Scheduler //dont want to recalculate times for moved items //only moved items have a sched_id $excludeIds = array(); - /*foreach ($filesToInsert as $file) { - if (isset($file["sched_id"])) { - $excludeIds[] = intval($file["sched_id"]); - } - }*/ $startProfile = microtime(true); @@ -517,9 +512,6 @@ class Application_Model_Scheduler } //selected empty row to add after else { - - //$instance = CcShowInstancesQuery::create()->findPK($schedule["instance"], $this->con); - $showStartDT = $instance->getDbStarts(null); $nextStartDT = $this->findNextStartTime($showStartDT, $instance); @@ -552,15 +544,17 @@ class Application_Model_Scheduler Logging::debug(floatval($pend) - floatval($pstart)); } + if (is_null($filesToInsert)) { + $filesToInsert = array(); + foreach ($mediaItems as $media) { + $filesToInsert = array_merge($filesToInsert, $this->retrieveMediaFiles($media["id"], $media["type"])); + } + } foreach ($filesToInsert as $file) { - Logging::info("INSERTING AT POSITION --- ".$pos); - //$endTimeDT = $this->findEndTime($nextStartDT, $file['cliplength']); - //item existed previously and is being moved. //need to keep same id for resources if we want REST. if (isset($file['sched_id'])) { - //$sched = CcScheduleQuery::create()->findPK($file['sched_id'], $this->con); $sched = CcScheduleQuery::create() ->filterByDbInstanceId($instance->getDbId()) ->filterByDbFileId($file["id"]) @@ -607,8 +601,20 @@ class Application_Model_Scheduler $nextStartDT = $endTimeDT; $pos++; + }//all files have been inserted/moved + // update is_scheduled flag for each cc_file + foreach ($filesToInsert as $file) { + $db_file = CcFilesQuery::create()->findPk($file['id'], $this->con); + $db_file->setDbIsScheduled(true); + $db_file->save($this->con); + } + /* Reset files to insert so we can get a new set of files. We have + * to do this in case we are inserting a dynamic block + */ + $filesToInsert = null; + /* If we are adjusting start and end times for items * after the insert location, we need to exclude the * schedule item we just inserted because it has correct @@ -661,11 +667,11 @@ class Application_Model_Scheduler } // update is_scheduled flag for each cc_file - foreach ($filesToInsert as $file) { + /*foreach ($filesToInsert as $file) { $db_file = CcFilesQuery::create()->findPk($file['id'], $this->con); $db_file->setDbIsScheduled(true); $db_file->save($this->con); - } + }*/ $endProfile = microtime(true); Logging::debug("updating show instances status."); @@ -706,8 +712,6 @@ class Application_Model_Scheduler { $this->con->beginTransaction(); - $filesToInsert = array(); - try { $this->validateRequest($scheduleItems, true); @@ -735,10 +739,7 @@ class Application_Model_Scheduler * sched_id => , * type => 0) */ - foreach ($mediaItems as $media) { - $filesToInsert = array_merge($filesToInsert, $this->retrieveMediaFiles($media["id"], $media["type"])); - } - $this->insertAfter($scheduleItems, $filesToInsert, $adjustSched); + $this->insertAfter($scheduleItems, $mediaItems, null, $adjustSched); $this->con->commit(); @@ -755,9 +756,6 @@ class Application_Model_Scheduler */ public function moveItem($selectedItems, $afterItems, $adjustSched = true) { - //Logging::info($selectedItems); - //Logging::info($afterItems); - $startProfile = microtime(true); $this->con->beginTransaction(); @@ -805,7 +803,7 @@ class Application_Model_Scheduler $modifiedMap[$showInstanceId] = array($schedId); } } -//Logging::info($movedData); + //calculate times excluding the to be moved items. foreach ($modifiedMap as $instance => $schedIds) { $startProfile = microtime(true); @@ -819,7 +817,7 @@ class Application_Model_Scheduler $startProfile = microtime(true); - $this->insertAfter($afterItems, $movedData, $adjustSched); + $this->insertAfter($afterItems, null, $movedData, $adjustSched); $endProfile = microtime(true); Logging::debug("inserting after removing gaps."); diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 5cd0d727d..121e0a3e1 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -1666,7 +1666,7 @@ SQL; if (is_null($showsPopUntil) || $showsPopUntil->getTimestamp() < $end_timestamp->getTimestamp()) { //Application_Model_Show::populateAllShowsInRange($showsPopUntil, $end_timestamp); $service_show = new Application_Service_ShowService(); - $service_show->delegateInstanceCreation(null, false, null, $end_timestamp); + $ccShow = $service_show->delegateInstanceCreation(null, $end_timestamp, true); Application_Model_Preference::SetShowsPopulatedUntil($end_timestamp); } diff --git a/airtime_mvc/application/models/ShowInstance.php b/airtime_mvc/application/models/ShowInstance.php index e1892d4c2..ec2a5ca01 100644 --- a/airtime_mvc/application/models/ShowInstance.php +++ b/airtime_mvc/application/models/ShowInstance.php @@ -391,7 +391,7 @@ SQL; * @param int $plId * Playlist ID. */ - public function addPlaylistToShow($pl_id, $checkUserPerm = true) + /*public function addPlaylistToShow($pl_id, $checkUserPerm = true) { $ts = intval($this->_showInstance->getDbLastScheduled("U")) ? : 0; $id = $this->_showInstance->getDbId(); @@ -401,7 +401,7 @@ SQL; array(array("id" => 0, "instance" => $id, "timestamp" => $ts)), array(array("id" => $pl_id, "type" => "playlist")) ); - } + }*/ /** * Add a media file as the last item in the show. @@ -427,12 +427,12 @@ SQL; * @param array $plIds * An array of playlist IDs. */ - public function scheduleShow($plIds) + /*public function scheduleShow($plIds) { foreach ($plIds as $plId) { $this->addPlaylistToShow($plId); } - } + }*/ public function clearShow() { diff --git a/airtime_mvc/application/models/airtime/CcShow.php b/airtime_mvc/application/models/airtime/CcShow.php index fa231be12..75489fee8 100644 --- a/airtime_mvc/application/models/airtime/CcShow.php +++ b/airtime_mvc/application/models/airtime/CcShow.php @@ -168,4 +168,12 @@ class CcShow extends BaseCcShow { } return $this->collCcShowInstancess; } + + public function getInstanceIds() { + $instanceIds = array(); + foreach ($this->getCcShowInstancess() as $ccShowInstance) { + $instanceIds[] = $ccShowInstance->getDbId(); + } + return $instanceIds; + } } // CcShow diff --git a/airtime_mvc/application/services/SchedulerService.php b/airtime_mvc/application/services/SchedulerService.php index a5d152680..718f402ba 100644 --- a/airtime_mvc/application/services/SchedulerService.php +++ b/airtime_mvc/application/services/SchedulerService.php @@ -117,7 +117,7 @@ class Application_Service_SchedulerService * @param DateTime $instanceStart * @param string $clipLength */ - private function findEndTime($instanceStart, $clipLength) + private static function findEndTime($instanceStart, $clipLength) { $startEpoch = $instanceStart->format("U.u"); $durationSeconds = Application_Common_DateHelper::playlistTimeToSeconds($clipLength); @@ -139,264 +139,68 @@ class Application_Service_SchedulerService /** * * Enter description here ... - * @param array $scheduleItems (schedule_id and instance_id it belongs to) - * @param array $mediaItems (file|block|playlist|webstream) - * @param $adjustSched + * @param $nextStartDT + * @param $showStamp array of ccSchedule objects */ - public function scheduleAdd($scheduleItems, $mediaItems, $adjustSched=true) + private static function insertItems($showStamp, $ccShow) { - $this->con->beginTransaction(); - - $filesToInsert = array(); - - try { - $this->validateRequest($scheduleItems); - - /* - * create array of arrays - * array of schedule item info - * (sched_id is the cc_schedule id and is set if an item is being - * moved because it is already in cc_schedule) - * [0] = Array( - * id => 1, - * cliplength => 00:04:32, - * cuein => 00:00:00, - * cueout => 00:04:32, - * fadein => 00.5, - * fadeout => 00.5, - * sched_id => , - * type => 0) - * [1] = Array( - * id => 2, - * cliplength => 00:05:07, - * cuein => 00:00:00, - * cueout => 00:05:07, - * fadein => 00.5, - * fadeout => 00.5, - * sched_id => , - * type => 0) - */ - foreach ($mediaItems as $media) { - $filesToInsert = array_merge($filesToInsert, $this->retrieveMediaFiles($media["id"], $media["type"])); - } - - //$this->insertAfter($scheduleItems, $filesToInsert, $adjustSched); - $ccStamp = $this->prepareStamp($scheduleItems, $mediaItems, $adjustSched); - - $this->insertStamp($ccStamp); - - //keep track of which shows had their schedule change - //dont forget about the linked shows - - $this->con->commit(); - - Application_Model_RabbitMq::PushSchedule(); - } catch (Exception $e) { - $this->con->rollback(); - throw $e; - } + } - private function setCcStamp($ccStamp, $instanceId) + public static function fillLinkedShows($ccShow) { - $ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId); - $ccShow = $ccShowInstance->getCcShow(); if ($ccShow->isLinked()) { - $ccStamp - ->setDbLinked(true) - ->setDbShowId($ccShow->getDbId()) - ->save(); - } else { - $ccStamp - ->setDbLinked(false) - ->setDbInstanceId($ccShowInstance->getDbId()) - ->save(); - } - } - - /** - * - * Enter description here ... - * @param $scheduleItems - * cc_schedule items, where the items get inserted after - * @param $filesToInsert - * array of schedule item info, what gets inserted into cc_schedule - * @param $adjustSched - */ - private function prepareStamp($scheduleItems, $itemsToInsert, $adjustSched = true) - { - try { - - foreach ($scheduleItems as $schedule) { - $id = intval($schedule["id"]); - - if ($id == 0) { - //if we get here, we know the show is empty and therefore - //need to create a new stamp - $pos = 0; - $ccStamp = new CcStamp(); - $this->setCcStamp($ccStamp, $schedule["instance"]); - } else { - $ccStamp = $this->getStamp($id); - //get the cc_stamp_contents item of the scheduleItem($schedule) - //this is where we are inserting after so we have to start the - //position counter after it - $ccStampContent = $this->getCurrentStampItem($id); - $pos = $ccStampContent->getDbPosition() + 1; - - //clear the positions of stamp items after the current - //item so we know we have to reassign their positions - //after inserting the new items - CcStampContentsQuery::create() - ->filterByDbStampId($ccStamp->getDbId()) - ->filterByDbPosition($pos, Criteria::GREATER_EQUAL) - ->setDbPosition(null) - ->save(); - } - - $stampId = $ccStamp->getDbId(); - foreach ($itemsToInsert as $item) { - $ccStampContent = new CcStampContents(); - $ccStampContent - ->setDbStampId($stampId) - ->setDbPosition($pos) - ->save(); - switch ($item["type"]) { - case "playlist": - $ccStampContent->setDbPlaylistId($item["id"])->save(); - break; - case "audioclip": - $ccStampContent->setDbFileId($item["id"])->save(); - - //update is_scheduled flag in cc_files - $ccFile = CcFilesQuery::create()->findPk($item['id']); - $ccFile->setDbIsScheduled(true)->save(); - break; - case "block": - $ccStampContent->setDbBlockId($item["id"])->save(); - break; - case "stream": - $ccStampContent->setDbStreamId($item["id"])->save(); - break; - } - $pos++; - } - - //reassign positions - $ccStampContents = CcStampContentsQuery::create() - ->filterByDbStampId($stampId) - ->filterByDbPosition(null) + /* First check if any linked instances have content + * If all instances are empty then we don't need to fill + * any other instances with content + */ + $instanceIds = $ccShow->getInstanceIds(); + $ccSchedules = CcScheduleQuery::create() + ->filterByDbInstanceId($instanceIds, Criteria::IN) + ->find(); + if (!$ccSchedules->isEmpty()) { + /* Find the show contents of just one of the instances. It doesn't + * matter which instance we use since all the content is the same + */ + $ccSchedule = $ccSchedules->getFirst(); + $showStamp = CcScheduleQuery::create() + ->filterByDbInstanceId($ccSchedule->getDbInstanceId()) + ->orderByDbStarts() ->find(); - foreach ($ccStampContents as $ccStampContent) { - $ccStampContent->setDbPosition($pos)->save(); - $pos++; + } else { + break; + } + + //need to find out which linked instances are empty + foreach ($ccShow->getCcShowInstancess() as $ccShowInstance) { + $ccSchedules = CcScheduleQuery::create() + ->filterByDbInstanceId($ccShowInstance->getDbId()) + ->find(); + if ($ccSchedules->isEmpty()) { + $nextStartDT = $ccShowInstance->getDbStarts(null); + + foreach ($showStamp as $item) { + $endTimeDT = self::findEndTime($nextStartDT, $item->getDbClipLength()); + + $ccSchedule = new CcSchedule(); + $ccSchedule + ->setDbStarts($nextStartDT) + ->setDbEnds($endTimeDT) + ->setDbFileId($item->getDbFileId()) + ->setDbStreamId($item->getDbStreamId()) + ->setDbClipLength($item->getDbClipLength()) + ->setDbFadeIn($item->getDbFadeIn()) + ->setDbFadeOut($item->getDbFadeOut()) + ->setDbCuein($item->getDbCueIn()) + ->setDbCueOut($item->getDbCueOut()) + ->setDbInstanceId($ccShowInstance->getDbId()) + ->setDbPosition($item->getDbPosition()) + ->save(); + + $nextStartDT = $endTimeDT; + } //foreach show item } - - return $ccStamp; - } - } catch (Exception $e) { - Logging::debug($e->getMessage()); - throw $e; - } - } - - private function insertStamp($ccStamp) - { - //delete cc_schedule entries - //CcScheduleQuery::create()->filterByDbStampId($ccStamp->getDbId())->delete(); - } - - private function getStamp($scheduleId) - { - $ccSchedule = CcScheduleQuery::create()->findPk($scheduleId); - return CcStamp::create()->findPk($ccSchedule->getDbStampId()); - } - - private function getCurrentStampItem($scheduleId) - { - $ccSchedule = CcScheduleQuery::create()->findPk($scheduleId); - return CcStampContents::create()->findPk($ccSchedule->getDbStampContentsId()); - } - - /** - * - * Enter description here ... - * @param array $items (schedule_id and instance_id it belongs to) - */ - private function validateRequest($items) - { - $nowEpoch = floatval($this->nowDT->format("U.u")); - - for ($i = 0; $i < count($items); $i++) { - $id = $items[$i]["id"]; - - //could be added to the beginning of a show, which sends id = 0; - if ($id > 0) { - //schedule_id of where we are inserting after? - $schedInfo[$id] = $items[$i]["instance"]; - } - - //what is timestamp for? - //format is instance_id => timestamp - $instanceInfo[$items[$i]["instance"]] = $items[$i]["timestamp"]; - } - - if (count($instanceInfo) === 0) { - throw new Exception("Invalid Request."); - } - - $schedIds = array(); - if (isset($schedInfo)) { - $schedIds = array_keys($schedInfo); - } - $schedItems = CcScheduleQuery::create()->findPKs($schedIds, $this->con); - $instanceIds = array_keys($instanceInfo); - $showInstances = CcShowInstancesQuery::create()->findPKs($instanceIds, $this->con); - - //an item has been deleted - if (count($schedIds) !== count($schedItems)) { - throw new OutDatedScheduleException(_("The schedule you're viewing is out of date! (sched mismatch)")); - } - - //a show has been deleted - if (count($instanceIds) !== count($showInstances)) { - throw new OutDatedScheduleException(_("The schedule you're viewing is out of date! (instance mismatch)")); - } - - foreach ($schedItems as $schedItem) { - $id = $schedItem->getDbId(); - $instance = $schedItem->getCcShowInstances($this->con); - - if (intval($schedInfo[$id]) !== $instance->getDbId()) { - throw new OutDatedScheduleException(_("The schedule you're viewing is out of date!")); - } - } - - foreach ($showInstances as $instance) { - - $id = $instance->getDbId(); - $show = $instance->getCcShow($this->con); - - if ($this->checkUserPermissions && $this->user->canSchedule($show->getDbId()) === false) { - throw new Exception(sprintf(_("You are not allowed to schedule show %s."), $show->getDbName())); - } - - if ($instance->getDbRecord()) { - throw new Exception(_("You cannot add files to recording shows.")); - } - - $showEndEpoch = floatval($instance->getDbEnds("U.u")); - - if ($showEndEpoch < $nowEpoch) { - throw new OutDatedScheduleException(sprintf(_("The show %s is over and cannot be scheduled."), $show->getDbName())); - } - - $ts = intval($instanceInfo[$id]); - $lastSchedTs = intval($instance->getDbLastScheduled("U")) ? : 0; - if ($ts < $lastSchedTs) { - Logging::info("ts {$ts} last sched {$lastSchedTs}"); - throw new OutDatedScheduleException(sprintf(_("The show %s has been previously updated!"), $show->getDbName())); - } + } //foreach linked instance } } } \ No newline at end of file diff --git a/airtime_mvc/application/services/ShowService.php b/airtime_mvc/application/services/ShowService.php index 146848aa4..82c74f031 100644 --- a/airtime_mvc/application/services/ShowService.php +++ b/airtime_mvc/application/services/ShowService.php @@ -9,12 +9,28 @@ define("REPEAT_MONTHLY_WEEKLY", 3); class Application_Service_ShowService { private $ccShow; + private $isRecorded; + private $isRebroadcast; + private $repeatType; + private $isUpdate; - public function __construct($showId=null) + public function __construct($showId=null, $showData=null, $isUpdate=false) { if (!is_null($showId)) { $this->ccShow = CcShowQuery::create()->findPk($showId); } + + if (isset($showData["add_show_repeats"]) && $showData["add_show_repeats"]) { + $this->repeatType = $showData["add_show_repeat_type"]; + if ($showData["add_show_repeat_type"] == 2) { + $this->repeatType = $showData["add_show_monthly_repeat_type"]; + } + } else { + $this->repeatType = -1; + } + $this->isRecorded = (isset($showData['add_show_record']) && $showData['add_show_record']) ? 1 : 0; + $this->isRebroadcast = (isset($showData['add_show_rebroadcast']) && $showData['add_show_rebroadcast']) ? 1 : 0; + $this->isUpdate = $isUpdate; } public function createShowFromRepeatingInstance($showData) { @@ -87,12 +103,12 @@ class Application_Service_ShowService } } - public function addUpdateShow($showData, $isUpdate=false) + public function addUpdateShow($showData) { $service_user = new Application_Service_UserService(); $currentUser = $service_user->getCurrentUser(); - if ($showData["add_show_repeats"]) { + /*if ($showData["add_show_repeats"]) { $repeatType = $showData["add_show_repeat_type"]; if ($showData["add_show_repeat_type"] == 2) { $repeatType = $showData["add_show_monthly_repeat_type"]; @@ -101,7 +117,7 @@ class Application_Service_ShowService $repeatType = -1; } $isRecorded = (isset($showData['add_show_record']) && $showData['add_show_record']) ? 1 : 0; - $isRebroadcast = (isset($showData['add_show_rebroadcast']) && $showData['add_show_rebroadcast']) ? 1 : 0; + $isRebroadcast = (isset($showData['add_show_rebroadcast']) && $showData['add_show_rebroadcast']) ? 1 : 0;*/ $showData["add_show_duration"] = $this->formatShowDuration( $showData["add_show_duration"]); @@ -113,36 +129,36 @@ class Application_Service_ShowService throw new Exception("Permission denied"); } //update ccShow - $this->setCcShow($showData, $isUpdate); + $this->setCcShow($showData); $daysAdded = array(); - if ($isUpdate) { - $daysAdded = $this->delegateInstanceCleanup($showData, $isRecorded, $repeatType); + if ($this->isUpdate) { + $daysAdded = $this->delegateInstanceCleanup($showData); // updates cc_show_instances start/end times, and updates // schedule start/end times $this->applyShowStartEndDifference($showData); $this->deleteRebroadcastInstances(); $this->deleteCcShowDays(); $this->deleteCcShowHosts(); - if ($isRebroadcast) { + if ($this->isRebroadcast) { //delete entry in cc_show_rebroadcast $this->deleteCcShowRebroadcasts(); } } //update ccShowDays - $this->setCcShowDays($showData, $repeatType, $isRecorded); + $this->setCcShowDays($showData); //update ccShowRebroadcasts - $this->setCcShowRebroadcasts($showData, $repeatType, $isRecorded); + $this->setCcShowRebroadcasts($showData); //update ccShowHosts $this->setCcShowHosts($showData); //create new ccShowInstances - $this->delegateInstanceCreation($isRebroadcast, $isUpdate, $daysAdded); + $this->delegateInstanceCreation($daysAdded); - if ($isUpdate) { + if ($this->isUpdate) { $service_scheduler = new Application_Service_SchedulerService(); $service_scheduler->removeGaps($this->ccShow->getDbId()); } @@ -151,7 +167,7 @@ class Application_Service_ShowService Application_Model_RabbitMq::PushSchedule(); } catch (Exception $e) { $con->rollback(); - $isUpdate ? $action = "update" : $action = "creation"; + $this->isUpdate ? $action = "update" : $action = "creation"; Logging::info("EXCEPTION: Show ".$action." failed."); Logging::info($e->getMessage()); } @@ -162,7 +178,7 @@ class Application_Service_ShowService * Receives a cc_show id and determines whether to create a * single show instance or repeating show instances */ - public function delegateInstanceCreation($isRebroadcast=null, $isUpdate=false, $daysAdded=null, $end=null) + public function delegateInstanceCreation($daysAdded=null, $end=null, $fillInstances=false) { $populateUntil = $this->getPopulateShowUntilDateTIme(); @@ -179,25 +195,27 @@ class Application_Service_ShowService foreach ($ccShowDays as $day) { switch ($day->getDbRepeatType()) { case NO_REPEAT: - $this->createNonRepeatingInstance($day, $populateUntil, $isRebroadcast, $isUpdate); + $this->createNonRepeatingInstance($day, $populateUntil); break; case REPEAT_WEEKLY: $this->createRepeatingInstances($day, $populateUntil, REPEAT_WEEKLY, - new DateInterval("P7D"), $isRebroadcast, $isUpdate, $daysAdded); + new DateInterval("P7D"), $daysAdded, $fillInstances); break; case REPEAT_BI_WEEKLY: $this->createRepeatingInstances($day, $populateUntil, REPEAT_BI_WEEKLY, - new DateInterval("P14D"), $isRebroadcast, $isUpdate, $daysAdded); + new DateInterval("P14D"), $daysAdded, $fillInstances); break; case REPEAT_MONTHLY_MONTHLY: - $this->createMonthlyMonthlyRepeatInstances($day, $populateUntil, $isRebroadcast, $isUpdate); + $this->createMonthlyMonthlyRepeatInstances($day, $populateUntil, $fillInstances); break; case REPEAT_MONTHLY_WEEKLY: $this->createRepeatingInstances($day, $populateUntil, REPEAT_MONTHLY_WEEKLY, - null, $isRebroadcast, $isUpdate); + null, $fillInstances); break; } } + + return $this->ccShow; } private function getShowDaysInRange($start, $end) @@ -273,7 +291,7 @@ SQL; * @param $isRecorded value computed from the edit show form * @param $repeatType value computed from the edit show form */ - private function delegateInstanceCleanup($showData, $isRecorded, $repeatType) + private function delegateInstanceCleanup($showData) { $showId = $this->ccShow->getDbId(); @@ -310,7 +328,7 @@ SQL; $deleteRepeatTypes = array(REPEAT_BI_WEEKLY, REPEAT_MONTHLY_MONTHLY, REPEAT_MONTHLY_WEEKLY); - if (in_array($repeatType, $deleteRepeatTypes) && + if (in_array($this->repeatType, $deleteRepeatTypes) && $showData["add_show_start_date"] != $localShowStart->format("Y-m-d")) { //Start date has changed when repeat type is bi-weekly or monthly. @@ -322,7 +340,7 @@ SQL; $currentRepeatType = $currentShowDay->getDbRepeatType(); //only delete instances if the show being edited was already repeating //and the repeat type changed - if ($currentRepeatType != -1 && $repeatType != $currentRepeatType) { + if ($currentRepeatType != -1 && $this->repeatType != $currentRepeatType) { $this->deleteAllInstances($showId); } else { //repeat type is the same, check if the days of the week are the same @@ -759,7 +777,7 @@ SQL; * @param $showDay * @param $populateUntil */ - private function createNonRepeatingInstance($showDay, $populateUntil, $isRebroadcast, $isUpdate) + private function createNonRepeatingInstance($showDay, $populateUntil) { //DateTime object $start = $showDay->getLocalStartDateAndTime(); @@ -769,7 +787,7 @@ SQL; if ($utcStartDateTime->getTimestamp() < $populateUntil->getTimestamp()) { $ccShowInstance = new CcShowInstances(); - if ($isUpdate) { + if ($this->isUpdate) { $ccShowInstance = $this->getInstance($utcStartDateTime); } $ccShowInstance->setDbShowId($this->ccShow->getDbId()); @@ -778,12 +796,12 @@ SQL; $ccShowInstance->setDbRecord($showDay->getDbRecord()); $ccShowInstance->save(); - if ($isUpdate) { + if ($this->isUpdate) { $con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME); $ccShowInstance->updateScheduleStatus($con); } - if ($isRebroadcast) { + if ($this->isRebroadcast) { $this->createRebroadcastInstances($showDay, $start, $ccShowInstance->getDbId()); } } @@ -798,7 +816,7 @@ SQL; * @param unknown_type $isRebroadcast */ private function createRepeatingInstances($showDay, $populateUntil, - $repeatType, $repeatInterval, $isRebroadcast, $isUpdate, $daysAdded=null) + $repeatType, $repeatInterval, $daysAdded=null, $fillInstances) { $show_id = $showDay->getDbShowId(); $first_show = $showDay->getDbFirstShow(); //non-UTC @@ -822,7 +840,6 @@ SQL; $utcLastShowDateTime = $last_show ? Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null; - $utcEndDateTime = null; foreach ($datePeriod as $date) { list($utcStartDateTime, $utcEndDateTime) = $this->createUTCStartEndDateTime( $date, $duration); @@ -838,7 +855,7 @@ SQL; * This will be the case when we are adding a new show day to * a repeating show */ - if ($isUpdate) { + if ($this->isUpdate) { if ($this->hasInstance($utcStartDateTime)) { $ccShowInstance = $this->getInstance($utcStartDateTime); $newInstance = false; @@ -874,17 +891,26 @@ SQL; } } - if ($isRebroadcast) { + if ($this->isRebroadcast) { $this->createRebroadcastInstances($showDay, $date, $ccShowInstance->getDbId()); } } } - $nextDate = $utcEndDateTime->add($repeatInterval); + + /* Set UTC to local time before setting the next repeat date. If we don't + * the next repeat date might be scheduled for the following day + */ + $utcStartDateTime->setTimezone(new DateTimeZone(Application_Model_Preference::GetTimezone())); + $nextDate = $utcStartDateTime->add($repeatInterval); $this->setNextRepeatingShowDate($nextDate->format("Y-m-d"), $day, $show_id); + + if ($fillInstances) { + Application_Service_SchedulerService::fillLinkedShows( + $showDay->getCcShow()); + } } - private function createMonthlyMonthlyRepeatInstances($showDay, $populateUntil, - $isRebroadcast, $isUpdate) + private function createMonthlyMonthlyRepeatInstances($showDay, $populateUntil) { $show_id = $showDay->getDbShowId(); $first_show = $showDay->getDbFirstShow(); //non-UTC @@ -920,7 +946,7 @@ SQL; * This will be the case when we are adding a new show day to * a repeating show */ - if ($isUpdate && $this->hasInstance($utcStartDateTime)) { + if ($this->isUpdate && $this->hasInstance($utcStartDateTime)) { $ccShowInstance = $this->getInstance($utcStartDateTime); $newInstance = false; $updateScheduleStatus = true; @@ -946,7 +972,7 @@ SQL; } } - if ($isRebroadcast) { + if ($this->isRebroadcast) { $this->createRebroadcastInstances($showDay, $date, $ccShowInstance->getDbId()); } } @@ -1105,9 +1131,9 @@ SQL; * @param $ccShow * @param $showData */ - private function setCcShow($showData, $isUpdate=false) + private function setCcShow($showData) { - if (!$isUpdate) { + if (!$this->isUpdate) { $ccShow = new CcShow(); } else { $ccShow = CcShowQuery::create()->findPk($showData["add_show_id"]); @@ -1139,7 +1165,7 @@ SQL; * @param $isRecorded * @param $showDay ccShowDay object we are setting values on */ - private function setCcShowDays($showData, $repeatType=-1, $isRecorded=0) + private function setCcShowDays($showData) { $showId = $this->ccShow->getDbId(); @@ -1172,9 +1198,9 @@ SQL; $showDay->setDbStartTime($startDateTime->format("H:i:s")); $showDay->setDbTimezone(Application_Model_Preference::GetTimezone()); $showDay->setDbDuration($showData['add_show_duration']); - $showDay->setDbRepeatType($repeatType); + $showDay->setDbRepeatType($this->repeatType); $showDay->setDbShowId($showId); - $showDay->setDbRecord($isRecorded); + $showDay->setDbRecord($this->isRecorded); //in case we are editing a show we need to set this to the first show //so when editing, the date period iterator will start from the beginning $showDay->setDbNextPopDate($startDateTime->format("Y-m-d")); @@ -1199,9 +1225,9 @@ SQL; $showDay->setDbTimezone(Application_Model_Preference::GetTimezone()); $showDay->setDbDuration($showData['add_show_duration']); $showDay->setDbDay($day); - $showDay->setDbRepeatType($repeatType); + $showDay->setDbRepeatType($this->repeatType); $showDay->setDbShowId($showId); - $showDay->setDbRecord($isRecorded); + $showDay->setDbRecord($this->isRecorded); //in case we are editing a show we need to set this to the first show //so when editing, the date period iterator will start from the beginning $showDay->setDbNextPopDate($startDateTimeClone->format("Y-m-d")); @@ -1230,11 +1256,11 @@ SQL; * @param $repeatType * @param $isRecorded */ - private function setCcShowRebroadcasts($showData, $repeatType, $isRecorded) + private function setCcShowRebroadcasts($showData) { $showId = $this->ccShow->getDbId(); - if (($isRecorded && $showData['add_show_rebroadcast']) && ($repeatType != -1)) { + if (($this->isRecorded && $showData['add_show_rebroadcast']) && ($this->repeatType != -1)) { for ($i = 1; $i <= MAX_REBROADCAST_DATES; $i++) { if ($showData['add_show_rebroadcast_date_'.$i]) { $showRebroad = new CcShowRebroadcast(); @@ -1244,7 +1270,7 @@ SQL; $showRebroad->save(); } } - } elseif ($isRecorded && $showData['add_show_rebroadcast'] && ($repeatType == -1)) { + } elseif ($this->isRecorded && $showData['add_show_rebroadcast'] && ($this->repeatType == -1)) { for ($i = 1; $i <= MAX_REBROADCAST_DATES; $i++) { if ($showData['add_show_rebroadcast_date_absolute_'.$i]) { $rebroadcastDate = new DateTime($showData["add_show_rebroadcast_date_absolute_$i"]);