CC-4961: Show linking

This commit is contained in:
denise 2013-04-25 09:00:37 -04:00
parent a9b5d00d87
commit 0f1383d541
7 changed files with 169 additions and 332 deletions

View file

@ -439,7 +439,7 @@ class ScheduleController extends Zend_Controller_Action
$service_showForm = new Application_Service_ShowFormService( $service_showForm = new Application_Service_ShowFormService(
$data["add_show_id"], $data["add_show_instance_id"]); $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(); $forms = $this->createShowFormAction();
@ -479,7 +479,7 @@ class ScheduleController extends Zend_Controller_Action
$service_showForm = new Application_Service_ShowFormService( $service_showForm = new Application_Service_ShowFormService(
$data["add_show_id"]); $data["add_show_id"]);
$service_show = new Application_Service_ShowService(); $service_show = new Application_Service_ShowService(null, $data, true);
//TODO: move this to js //TODO: move this to js
$data['add_show_hosts'] = $this->_getParam('hosts'); $data['add_show_hosts'] = $this->_getParam('hosts');
@ -497,8 +497,7 @@ class ScheduleController extends Zend_Controller_Action
if ($service_showForm->validateShowForms($forms, $data, $validateStartDate, if ($service_showForm->validateShowForms($forms, $data, $validateStartDate,
$originalShowStartDateTime, true, $data["add_show_instance_id"])) { $originalShowStartDateTime, true, $data["add_show_instance_id"])) {
//pass in true to indicate we are updating a show $service_show->addUpdateShow($data);
$service_show->addUpdateShow($data, true);
$this->view->addNewShow = true; $this->view->addNewShow = true;
$this->view->newForm = $this->view->render('schedule/add-show-form.phtml'); $this->view->newForm = $this->view->render('schedule/add-show-form.phtml');
@ -519,7 +518,7 @@ class ScheduleController extends Zend_Controller_Action
public function addShowAction() public function addShowAction()
{ {
$service_showForm = new Application_Service_ShowFormService(null); $service_showForm = new Application_Service_ShowFormService(null);
$service_show = new Application_Service_ShowService(); //$service_show = new Application_Service_ShowService();
$js = $this->_getParam('data'); $js = $this->_getParam('data');
$data = array(); $data = array();
@ -529,6 +528,8 @@ class ScheduleController extends Zend_Controller_Action
$data[$j["name"]] = $j["value"]; $data[$j["name"]] = $j["value"];
} }
$service_show = new Application_Service_ShowService(null, $data);
// TODO: move this to js // TODO: move this to js
$data['add_show_hosts'] = $this->_getParam('hosts'); $data['add_show_hosts'] = $this->_getParam('hosts');
$data['add_show_day_check'] = $this->_getParam('days'); $data['add_show_day_check'] = $this->_getParam('days');

View file

@ -432,7 +432,7 @@ class Application_Model_Scheduler
* array of schedule item info, what gets inserted into cc_schedule * array of schedule item info, what gets inserted into cc_schedule
* @param $adjustSched * @param $adjustSched
*/ */
private function insertAfter($scheduleItems, $filesToInsert, $adjustSched = true) private function insertAfter($scheduleItems, $mediaItems, $filesToInsert=null, $adjustSched=true)
{ {
try { try {
$affectedShowInstances = array(); $affectedShowInstances = array();
@ -440,11 +440,6 @@ class Application_Model_Scheduler
//dont want to recalculate times for moved items //dont want to recalculate times for moved items
//only moved items have a sched_id //only moved items have a sched_id
$excludeIds = array(); $excludeIds = array();
/*foreach ($filesToInsert as $file) {
if (isset($file["sched_id"])) {
$excludeIds[] = intval($file["sched_id"]);
}
}*/
$startProfile = microtime(true); $startProfile = microtime(true);
@ -517,9 +512,6 @@ class Application_Model_Scheduler
} }
//selected empty row to add after //selected empty row to add after
else { else {
//$instance = CcShowInstancesQuery::create()->findPK($schedule["instance"], $this->con);
$showStartDT = $instance->getDbStarts(null); $showStartDT = $instance->getDbStarts(null);
$nextStartDT = $this->findNextStartTime($showStartDT, $instance); $nextStartDT = $this->findNextStartTime($showStartDT, $instance);
@ -552,15 +544,17 @@ class Application_Model_Scheduler
Logging::debug(floatval($pend) - floatval($pstart)); 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) { foreach ($filesToInsert as $file) {
Logging::info("INSERTING AT POSITION --- ".$pos);
//$endTimeDT = $this->findEndTime($nextStartDT, $file['cliplength']);
//item existed previously and is being moved. //item existed previously and is being moved.
//need to keep same id for resources if we want REST. //need to keep same id for resources if we want REST.
if (isset($file['sched_id'])) { if (isset($file['sched_id'])) {
//$sched = CcScheduleQuery::create()->findPK($file['sched_id'], $this->con);
$sched = CcScheduleQuery::create() $sched = CcScheduleQuery::create()
->filterByDbInstanceId($instance->getDbId()) ->filterByDbInstanceId($instance->getDbId())
->filterByDbFileId($file["id"]) ->filterByDbFileId($file["id"])
@ -607,8 +601,20 @@ class Application_Model_Scheduler
$nextStartDT = $endTimeDT; $nextStartDT = $endTimeDT;
$pos++; $pos++;
}//all files have been inserted/moved }//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 /* If we are adjusting start and end times for items
* after the insert location, we need to exclude the * after the insert location, we need to exclude the
* schedule item we just inserted because it has correct * 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 // 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 = CcFilesQuery::create()->findPk($file['id'], $this->con);
$db_file->setDbIsScheduled(true); $db_file->setDbIsScheduled(true);
$db_file->save($this->con); $db_file->save($this->con);
} }*/
$endProfile = microtime(true); $endProfile = microtime(true);
Logging::debug("updating show instances status."); Logging::debug("updating show instances status.");
@ -706,8 +712,6 @@ class Application_Model_Scheduler
{ {
$this->con->beginTransaction(); $this->con->beginTransaction();
$filesToInsert = array();
try { try {
$this->validateRequest($scheduleItems, true); $this->validateRequest($scheduleItems, true);
@ -735,10 +739,7 @@ class Application_Model_Scheduler
* sched_id => , * sched_id => ,
* type => 0) * type => 0)
*/ */
foreach ($mediaItems as $media) { $this->insertAfter($scheduleItems, $mediaItems, null, $adjustSched);
$filesToInsert = array_merge($filesToInsert, $this->retrieveMediaFiles($media["id"], $media["type"]));
}
$this->insertAfter($scheduleItems, $filesToInsert, $adjustSched);
$this->con->commit(); $this->con->commit();
@ -755,9 +756,6 @@ class Application_Model_Scheduler
*/ */
public function moveItem($selectedItems, $afterItems, $adjustSched = true) public function moveItem($selectedItems, $afterItems, $adjustSched = true)
{ {
//Logging::info($selectedItems);
//Logging::info($afterItems);
$startProfile = microtime(true); $startProfile = microtime(true);
$this->con->beginTransaction(); $this->con->beginTransaction();
@ -805,7 +803,7 @@ class Application_Model_Scheduler
$modifiedMap[$showInstanceId] = array($schedId); $modifiedMap[$showInstanceId] = array($schedId);
} }
} }
//Logging::info($movedData);
//calculate times excluding the to be moved items. //calculate times excluding the to be moved items.
foreach ($modifiedMap as $instance => $schedIds) { foreach ($modifiedMap as $instance => $schedIds) {
$startProfile = microtime(true); $startProfile = microtime(true);
@ -819,7 +817,7 @@ class Application_Model_Scheduler
$startProfile = microtime(true); $startProfile = microtime(true);
$this->insertAfter($afterItems, $movedData, $adjustSched); $this->insertAfter($afterItems, null, $movedData, $adjustSched);
$endProfile = microtime(true); $endProfile = microtime(true);
Logging::debug("inserting after removing gaps."); Logging::debug("inserting after removing gaps.");

View file

@ -1666,7 +1666,7 @@ SQL;
if (is_null($showsPopUntil) || $showsPopUntil->getTimestamp() < $end_timestamp->getTimestamp()) { if (is_null($showsPopUntil) || $showsPopUntil->getTimestamp() < $end_timestamp->getTimestamp()) {
//Application_Model_Show::populateAllShowsInRange($showsPopUntil, $end_timestamp); //Application_Model_Show::populateAllShowsInRange($showsPopUntil, $end_timestamp);
$service_show = new Application_Service_ShowService(); $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); Application_Model_Preference::SetShowsPopulatedUntil($end_timestamp);
} }

View file

@ -391,7 +391,7 @@ SQL;
* @param int $plId * @param int $plId
* Playlist ID. * Playlist ID.
*/ */
public function addPlaylistToShow($pl_id, $checkUserPerm = true) /*public function addPlaylistToShow($pl_id, $checkUserPerm = true)
{ {
$ts = intval($this->_showInstance->getDbLastScheduled("U")) ? : 0; $ts = intval($this->_showInstance->getDbLastScheduled("U")) ? : 0;
$id = $this->_showInstance->getDbId(); $id = $this->_showInstance->getDbId();
@ -401,7 +401,7 @@ SQL;
array(array("id" => 0, "instance" => $id, "timestamp" => $ts)), array(array("id" => 0, "instance" => $id, "timestamp" => $ts)),
array(array("id" => $pl_id, "type" => "playlist")) array(array("id" => $pl_id, "type" => "playlist"))
); );
} }*/
/** /**
* Add a media file as the last item in the show. * Add a media file as the last item in the show.
@ -427,12 +427,12 @@ SQL;
* @param array $plIds * @param array $plIds
* An array of playlist IDs. * An array of playlist IDs.
*/ */
public function scheduleShow($plIds) /*public function scheduleShow($plIds)
{ {
foreach ($plIds as $plId) { foreach ($plIds as $plId) {
$this->addPlaylistToShow($plId); $this->addPlaylistToShow($plId);
} }
} }*/
public function clearShow() public function clearShow()
{ {

View file

@ -168,4 +168,12 @@ class CcShow extends BaseCcShow {
} }
return $this->collCcShowInstancess; return $this->collCcShowInstancess;
} }
public function getInstanceIds() {
$instanceIds = array();
foreach ($this->getCcShowInstancess() as $ccShowInstance) {
$instanceIds[] = $ccShowInstance->getDbId();
}
return $instanceIds;
}
} // CcShow } // CcShow

View file

@ -117,7 +117,7 @@ class Application_Service_SchedulerService
* @param DateTime $instanceStart * @param DateTime $instanceStart
* @param string $clipLength * @param string $clipLength
*/ */
private function findEndTime($instanceStart, $clipLength) private static function findEndTime($instanceStart, $clipLength)
{ {
$startEpoch = $instanceStart->format("U.u"); $startEpoch = $instanceStart->format("U.u");
$durationSeconds = Application_Common_DateHelper::playlistTimeToSeconds($clipLength); $durationSeconds = Application_Common_DateHelper::playlistTimeToSeconds($clipLength);
@ -139,264 +139,68 @@ class Application_Service_SchedulerService
/** /**
* *
* Enter description here ... * Enter description here ...
* @param array $scheduleItems (schedule_id and instance_id it belongs to) * @param $nextStartDT
* @param array $mediaItems (file|block|playlist|webstream) * @param $showStamp array of ccSchedule objects
* @param $adjustSched
*/ */
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); public static function fillLinkedShows($ccShow)
$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)
{ {
$ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId);
$ccShow = $ccShowInstance->getCcShow();
if ($ccShow->isLinked()) { if ($ccShow->isLinked()) {
$ccStamp /* First check if any linked instances have content
->setDbLinked(true) * If all instances are empty then we don't need to fill
->setDbShowId($ccShow->getDbId()) * any other instances with content
->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) $instanceIds = $ccShow->getInstanceIds();
{ $ccSchedules = CcScheduleQuery::create()
try { ->filterByDbInstanceId($instanceIds, Criteria::IN)
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)
->find(); ->find();
foreach ($ccStampContents as $ccStampContent) { if (!$ccSchedules->isEmpty()) {
$ccStampContent->setDbPosition($pos)->save(); /* Find the show contents of just one of the instances. It doesn't
$pos++; * matter which instance we use since all the content is the same
}
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) $ccSchedule = $ccSchedules->getFirst();
{ $showStamp = CcScheduleQuery::create()
$nowEpoch = floatval($this->nowDT->format("U.u")); ->filterByDbInstanceId($ccSchedule->getDbInstanceId())
->orderByDbStarts()
for ($i = 0; $i < count($items); $i++) { ->find();
$id = $items[$i]["id"]; } else {
break;
//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? //need to find out which linked instances are empty
//format is instance_id => timestamp foreach ($ccShow->getCcShowInstancess() as $ccShowInstance) {
$instanceInfo[$items[$i]["instance"]] = $items[$i]["timestamp"]; $ccSchedules = CcScheduleQuery::create()
} ->filterByDbInstanceId($ccShowInstance->getDbId())
->find();
if (count($instanceInfo) === 0) { if ($ccSchedules->isEmpty()) {
throw new Exception("Invalid Request."); $nextStartDT = $ccShowInstance->getDbStarts(null);
}
foreach ($showStamp as $item) {
$schedIds = array(); $endTimeDT = self::findEndTime($nextStartDT, $item->getDbClipLength());
if (isset($schedInfo)) {
$schedIds = array_keys($schedInfo); $ccSchedule = new CcSchedule();
} $ccSchedule
$schedItems = CcScheduleQuery::create()->findPKs($schedIds, $this->con); ->setDbStarts($nextStartDT)
$instanceIds = array_keys($instanceInfo); ->setDbEnds($endTimeDT)
$showInstances = CcShowInstancesQuery::create()->findPKs($instanceIds, $this->con); ->setDbFileId($item->getDbFileId())
->setDbStreamId($item->getDbStreamId())
//an item has been deleted ->setDbClipLength($item->getDbClipLength())
if (count($schedIds) !== count($schedItems)) { ->setDbFadeIn($item->getDbFadeIn())
throw new OutDatedScheduleException(_("The schedule you're viewing is out of date! (sched mismatch)")); ->setDbFadeOut($item->getDbFadeOut())
} ->setDbCuein($item->getDbCueIn())
->setDbCueOut($item->getDbCueOut())
//a show has been deleted ->setDbInstanceId($ccShowInstance->getDbId())
if (count($instanceIds) !== count($showInstances)) { ->setDbPosition($item->getDbPosition())
throw new OutDatedScheduleException(_("The schedule you're viewing is out of date! (instance mismatch)")); ->save();
}
$nextStartDT = $endTimeDT;
foreach ($schedItems as $schedItem) { } //foreach show item
$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
} }
} }
} }

View file

@ -9,12 +9,28 @@ define("REPEAT_MONTHLY_WEEKLY", 3);
class Application_Service_ShowService class Application_Service_ShowService
{ {
private $ccShow; 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)) { if (!is_null($showId)) {
$this->ccShow = CcShowQuery::create()->findPk($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) { 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(); $service_user = new Application_Service_UserService();
$currentUser = $service_user->getCurrentUser(); $currentUser = $service_user->getCurrentUser();
if ($showData["add_show_repeats"]) { /*if ($showData["add_show_repeats"]) {
$repeatType = $showData["add_show_repeat_type"]; $repeatType = $showData["add_show_repeat_type"];
if ($showData["add_show_repeat_type"] == 2) { if ($showData["add_show_repeat_type"] == 2) {
$repeatType = $showData["add_show_monthly_repeat_type"]; $repeatType = $showData["add_show_monthly_repeat_type"];
@ -101,7 +117,7 @@ class Application_Service_ShowService
$repeatType = -1; $repeatType = -1;
} }
$isRecorded = (isset($showData['add_show_record']) && $showData['add_show_record']) ? 1 : 0; $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"] = $this->formatShowDuration(
$showData["add_show_duration"]); $showData["add_show_duration"]);
@ -113,36 +129,36 @@ class Application_Service_ShowService
throw new Exception("Permission denied"); throw new Exception("Permission denied");
} }
//update ccShow //update ccShow
$this->setCcShow($showData, $isUpdate); $this->setCcShow($showData);
$daysAdded = array(); $daysAdded = array();
if ($isUpdate) { if ($this->isUpdate) {
$daysAdded = $this->delegateInstanceCleanup($showData, $isRecorded, $repeatType); $daysAdded = $this->delegateInstanceCleanup($showData);
// updates cc_show_instances start/end times, and updates // updates cc_show_instances start/end times, and updates
// schedule start/end times // schedule start/end times
$this->applyShowStartEndDifference($showData); $this->applyShowStartEndDifference($showData);
$this->deleteRebroadcastInstances(); $this->deleteRebroadcastInstances();
$this->deleteCcShowDays(); $this->deleteCcShowDays();
$this->deleteCcShowHosts(); $this->deleteCcShowHosts();
if ($isRebroadcast) { if ($this->isRebroadcast) {
//delete entry in cc_show_rebroadcast //delete entry in cc_show_rebroadcast
$this->deleteCcShowRebroadcasts(); $this->deleteCcShowRebroadcasts();
} }
} }
//update ccShowDays //update ccShowDays
$this->setCcShowDays($showData, $repeatType, $isRecorded); $this->setCcShowDays($showData);
//update ccShowRebroadcasts //update ccShowRebroadcasts
$this->setCcShowRebroadcasts($showData, $repeatType, $isRecorded); $this->setCcShowRebroadcasts($showData);
//update ccShowHosts //update ccShowHosts
$this->setCcShowHosts($showData); $this->setCcShowHosts($showData);
//create new ccShowInstances //create new ccShowInstances
$this->delegateInstanceCreation($isRebroadcast, $isUpdate, $daysAdded); $this->delegateInstanceCreation($daysAdded);
if ($isUpdate) { if ($this->isUpdate) {
$service_scheduler = new Application_Service_SchedulerService(); $service_scheduler = new Application_Service_SchedulerService();
$service_scheduler->removeGaps($this->ccShow->getDbId()); $service_scheduler->removeGaps($this->ccShow->getDbId());
} }
@ -151,7 +167,7 @@ class Application_Service_ShowService
Application_Model_RabbitMq::PushSchedule(); Application_Model_RabbitMq::PushSchedule();
} catch (Exception $e) { } catch (Exception $e) {
$con->rollback(); $con->rollback();
$isUpdate ? $action = "update" : $action = "creation"; $this->isUpdate ? $action = "update" : $action = "creation";
Logging::info("EXCEPTION: Show ".$action." failed."); Logging::info("EXCEPTION: Show ".$action." failed.");
Logging::info($e->getMessage()); Logging::info($e->getMessage());
} }
@ -162,7 +178,7 @@ class Application_Service_ShowService
* Receives a cc_show id and determines whether to create a * Receives a cc_show id and determines whether to create a
* single show instance or repeating show instances * 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(); $populateUntil = $this->getPopulateShowUntilDateTIme();
@ -179,25 +195,27 @@ class Application_Service_ShowService
foreach ($ccShowDays as $day) { foreach ($ccShowDays as $day) {
switch ($day->getDbRepeatType()) { switch ($day->getDbRepeatType()) {
case NO_REPEAT: case NO_REPEAT:
$this->createNonRepeatingInstance($day, $populateUntil, $isRebroadcast, $isUpdate); $this->createNonRepeatingInstance($day, $populateUntil);
break; break;
case REPEAT_WEEKLY: case REPEAT_WEEKLY:
$this->createRepeatingInstances($day, $populateUntil, REPEAT_WEEKLY, $this->createRepeatingInstances($day, $populateUntil, REPEAT_WEEKLY,
new DateInterval("P7D"), $isRebroadcast, $isUpdate, $daysAdded); new DateInterval("P7D"), $daysAdded, $fillInstances);
break; break;
case REPEAT_BI_WEEKLY: case REPEAT_BI_WEEKLY:
$this->createRepeatingInstances($day, $populateUntil, REPEAT_BI_WEEKLY, $this->createRepeatingInstances($day, $populateUntil, REPEAT_BI_WEEKLY,
new DateInterval("P14D"), $isRebroadcast, $isUpdate, $daysAdded); new DateInterval("P14D"), $daysAdded, $fillInstances);
break; break;
case REPEAT_MONTHLY_MONTHLY: case REPEAT_MONTHLY_MONTHLY:
$this->createMonthlyMonthlyRepeatInstances($day, $populateUntil, $isRebroadcast, $isUpdate); $this->createMonthlyMonthlyRepeatInstances($day, $populateUntil, $fillInstances);
break; break;
case REPEAT_MONTHLY_WEEKLY: case REPEAT_MONTHLY_WEEKLY:
$this->createRepeatingInstances($day, $populateUntil, REPEAT_MONTHLY_WEEKLY, $this->createRepeatingInstances($day, $populateUntil, REPEAT_MONTHLY_WEEKLY,
null, $isRebroadcast, $isUpdate); null, $fillInstances);
break; break;
} }
} }
return $this->ccShow;
} }
private function getShowDaysInRange($start, $end) private function getShowDaysInRange($start, $end)
@ -273,7 +291,7 @@ SQL;
* @param $isRecorded value computed from the edit show form * @param $isRecorded value computed from the edit show form
* @param $repeatType 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(); $showId = $this->ccShow->getDbId();
@ -310,7 +328,7 @@ SQL;
$deleteRepeatTypes = array(REPEAT_BI_WEEKLY, REPEAT_MONTHLY_MONTHLY, $deleteRepeatTypes = array(REPEAT_BI_WEEKLY, REPEAT_MONTHLY_MONTHLY,
REPEAT_MONTHLY_WEEKLY); REPEAT_MONTHLY_WEEKLY);
if (in_array($repeatType, $deleteRepeatTypes) && if (in_array($this->repeatType, $deleteRepeatTypes) &&
$showData["add_show_start_date"] != $localShowStart->format("Y-m-d")) { $showData["add_show_start_date"] != $localShowStart->format("Y-m-d")) {
//Start date has changed when repeat type is bi-weekly or monthly. //Start date has changed when repeat type is bi-weekly or monthly.
@ -322,7 +340,7 @@ SQL;
$currentRepeatType = $currentShowDay->getDbRepeatType(); $currentRepeatType = $currentShowDay->getDbRepeatType();
//only delete instances if the show being edited was already repeating //only delete instances if the show being edited was already repeating
//and the repeat type changed //and the repeat type changed
if ($currentRepeatType != -1 && $repeatType != $currentRepeatType) { if ($currentRepeatType != -1 && $this->repeatType != $currentRepeatType) {
$this->deleteAllInstances($showId); $this->deleteAllInstances($showId);
} else { } else {
//repeat type is the same, check if the days of the week are the same //repeat type is the same, check if the days of the week are the same
@ -759,7 +777,7 @@ SQL;
* @param $showDay * @param $showDay
* @param $populateUntil * @param $populateUntil
*/ */
private function createNonRepeatingInstance($showDay, $populateUntil, $isRebroadcast, $isUpdate) private function createNonRepeatingInstance($showDay, $populateUntil)
{ {
//DateTime object //DateTime object
$start = $showDay->getLocalStartDateAndTime(); $start = $showDay->getLocalStartDateAndTime();
@ -769,7 +787,7 @@ SQL;
if ($utcStartDateTime->getTimestamp() < $populateUntil->getTimestamp()) { if ($utcStartDateTime->getTimestamp() < $populateUntil->getTimestamp()) {
$ccShowInstance = new CcShowInstances(); $ccShowInstance = new CcShowInstances();
if ($isUpdate) { if ($this->isUpdate) {
$ccShowInstance = $this->getInstance($utcStartDateTime); $ccShowInstance = $this->getInstance($utcStartDateTime);
} }
$ccShowInstance->setDbShowId($this->ccShow->getDbId()); $ccShowInstance->setDbShowId($this->ccShow->getDbId());
@ -778,12 +796,12 @@ SQL;
$ccShowInstance->setDbRecord($showDay->getDbRecord()); $ccShowInstance->setDbRecord($showDay->getDbRecord());
$ccShowInstance->save(); $ccShowInstance->save();
if ($isUpdate) { if ($this->isUpdate) {
$con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME); $con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME);
$ccShowInstance->updateScheduleStatus($con); $ccShowInstance->updateScheduleStatus($con);
} }
if ($isRebroadcast) { if ($this->isRebroadcast) {
$this->createRebroadcastInstances($showDay, $start, $ccShowInstance->getDbId()); $this->createRebroadcastInstances($showDay, $start, $ccShowInstance->getDbId());
} }
} }
@ -798,7 +816,7 @@ SQL;
* @param unknown_type $isRebroadcast * @param unknown_type $isRebroadcast
*/ */
private function createRepeatingInstances($showDay, $populateUntil, private function createRepeatingInstances($showDay, $populateUntil,
$repeatType, $repeatInterval, $isRebroadcast, $isUpdate, $daysAdded=null) $repeatType, $repeatInterval, $daysAdded=null, $fillInstances)
{ {
$show_id = $showDay->getDbShowId(); $show_id = $showDay->getDbShowId();
$first_show = $showDay->getDbFirstShow(); //non-UTC $first_show = $showDay->getDbFirstShow(); //non-UTC
@ -822,7 +840,6 @@ SQL;
$utcLastShowDateTime = $last_show ? $utcLastShowDateTime = $last_show ?
Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null; Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null;
$utcEndDateTime = null;
foreach ($datePeriod as $date) { foreach ($datePeriod as $date) {
list($utcStartDateTime, $utcEndDateTime) = $this->createUTCStartEndDateTime( list($utcStartDateTime, $utcEndDateTime) = $this->createUTCStartEndDateTime(
$date, $duration); $date, $duration);
@ -838,7 +855,7 @@ SQL;
* This will be the case when we are adding a new show day to * This will be the case when we are adding a new show day to
* a repeating show * a repeating show
*/ */
if ($isUpdate) { if ($this->isUpdate) {
if ($this->hasInstance($utcStartDateTime)) { if ($this->hasInstance($utcStartDateTime)) {
$ccShowInstance = $this->getInstance($utcStartDateTime); $ccShowInstance = $this->getInstance($utcStartDateTime);
$newInstance = false; $newInstance = false;
@ -874,17 +891,26 @@ SQL;
} }
} }
if ($isRebroadcast) { if ($this->isRebroadcast) {
$this->createRebroadcastInstances($showDay, $date, $ccShowInstance->getDbId()); $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); $this->setNextRepeatingShowDate($nextDate->format("Y-m-d"), $day, $show_id);
if ($fillInstances) {
Application_Service_SchedulerService::fillLinkedShows(
$showDay->getCcShow());
}
} }
private function createMonthlyMonthlyRepeatInstances($showDay, $populateUntil, private function createMonthlyMonthlyRepeatInstances($showDay, $populateUntil)
$isRebroadcast, $isUpdate)
{ {
$show_id = $showDay->getDbShowId(); $show_id = $showDay->getDbShowId();
$first_show = $showDay->getDbFirstShow(); //non-UTC $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 * This will be the case when we are adding a new show day to
* a repeating show * a repeating show
*/ */
if ($isUpdate && $this->hasInstance($utcStartDateTime)) { if ($this->isUpdate && $this->hasInstance($utcStartDateTime)) {
$ccShowInstance = $this->getInstance($utcStartDateTime); $ccShowInstance = $this->getInstance($utcStartDateTime);
$newInstance = false; $newInstance = false;
$updateScheduleStatus = true; $updateScheduleStatus = true;
@ -946,7 +972,7 @@ SQL;
} }
} }
if ($isRebroadcast) { if ($this->isRebroadcast) {
$this->createRebroadcastInstances($showDay, $date, $ccShowInstance->getDbId()); $this->createRebroadcastInstances($showDay, $date, $ccShowInstance->getDbId());
} }
} }
@ -1105,9 +1131,9 @@ SQL;
* @param $ccShow * @param $ccShow
* @param $showData * @param $showData
*/ */
private function setCcShow($showData, $isUpdate=false) private function setCcShow($showData)
{ {
if (!$isUpdate) { if (!$this->isUpdate) {
$ccShow = new CcShow(); $ccShow = new CcShow();
} else { } else {
$ccShow = CcShowQuery::create()->findPk($showData["add_show_id"]); $ccShow = CcShowQuery::create()->findPk($showData["add_show_id"]);
@ -1139,7 +1165,7 @@ SQL;
* @param $isRecorded * @param $isRecorded
* @param $showDay ccShowDay object we are setting values on * @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(); $showId = $this->ccShow->getDbId();
@ -1172,9 +1198,9 @@ SQL;
$showDay->setDbStartTime($startDateTime->format("H:i:s")); $showDay->setDbStartTime($startDateTime->format("H:i:s"));
$showDay->setDbTimezone(Application_Model_Preference::GetTimezone()); $showDay->setDbTimezone(Application_Model_Preference::GetTimezone());
$showDay->setDbDuration($showData['add_show_duration']); $showDay->setDbDuration($showData['add_show_duration']);
$showDay->setDbRepeatType($repeatType); $showDay->setDbRepeatType($this->repeatType);
$showDay->setDbShowId($showId); $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 //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 //so when editing, the date period iterator will start from the beginning
$showDay->setDbNextPopDate($startDateTime->format("Y-m-d")); $showDay->setDbNextPopDate($startDateTime->format("Y-m-d"));
@ -1199,9 +1225,9 @@ SQL;
$showDay->setDbTimezone(Application_Model_Preference::GetTimezone()); $showDay->setDbTimezone(Application_Model_Preference::GetTimezone());
$showDay->setDbDuration($showData['add_show_duration']); $showDay->setDbDuration($showData['add_show_duration']);
$showDay->setDbDay($day); $showDay->setDbDay($day);
$showDay->setDbRepeatType($repeatType); $showDay->setDbRepeatType($this->repeatType);
$showDay->setDbShowId($showId); $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 //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 //so when editing, the date period iterator will start from the beginning
$showDay->setDbNextPopDate($startDateTimeClone->format("Y-m-d")); $showDay->setDbNextPopDate($startDateTimeClone->format("Y-m-d"));
@ -1230,11 +1256,11 @@ SQL;
* @param $repeatType * @param $repeatType
* @param $isRecorded * @param $isRecorded
*/ */
private function setCcShowRebroadcasts($showData, $repeatType, $isRecorded) private function setCcShowRebroadcasts($showData)
{ {
$showId = $this->ccShow->getDbId(); $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++) { for ($i = 1; $i <= MAX_REBROADCAST_DATES; $i++) {
if ($showData['add_show_rebroadcast_date_'.$i]) { if ($showData['add_show_rebroadcast_date_'.$i]) {
$showRebroad = new CcShowRebroadcast(); $showRebroad = new CcShowRebroadcast();
@ -1244,7 +1270,7 @@ SQL;
$showRebroad->save(); $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++) { for ($i = 1; $i <= MAX_REBROADCAST_DATES; $i++) {
if ($showData['add_show_rebroadcast_date_absolute_'.$i]) { if ($showData['add_show_rebroadcast_date_absolute_'.$i]) {
$rebroadcastDate = new DateTime($showData["add_show_rebroadcast_date_absolute_$i"]); $rebroadcastDate = new DateTime($showData["add_show_rebroadcast_date_absolute_$i"]);