diff --git a/airtime_mvc/application/controllers/ShowbuilderController.php b/airtime_mvc/application/controllers/ShowbuilderController.php index 823bb15f2..dd6134f68 100644 --- a/airtime_mvc/application/controllers/ShowbuilderController.php +++ b/airtime_mvc/application/controllers/ShowbuilderController.php @@ -6,7 +6,7 @@ class ShowbuilderController extends Zend_Controller_Action public function init() { $ajaxContext = $this->_helper->getHelper('AjaxContext'); - $ajaxContext->addActionContext('schedule-update', 'json') + $ajaxContext->addActionContext('schedule-move', 'json') ->addActionContext('schedule-add', 'json') ->addActionContext('schedule-remove', 'json') ->addActionContext('builder-feed', 'json') @@ -103,48 +103,26 @@ class ShowbuilderController extends Zend_Controller_Action $this->view->data = $json; } - public function scheduleAction() { + public function scheduleMoveAction() { $request = $this->getRequest(); - $instance = $request->getParam("instance", null); - $id = $request->getParam("id", null); - $starts_epoch = $request->getParam("start", null); - $file_id = $request->getParam("file", null); + $selectedItem = $request->getParam("selectedItem"); + $afterItem = $request->getParam("afterItem"); - $startDT = DateTime::createFromFormat("U", $starts_epoch, new DateTimeZone("UTC")); + $json = array(); - //invalid request - if (is_null($start)) { - return; + try { + $scheduler = new Application_Model_Scheduler(); + $scheduler->moveItem($selectedItem, $afterItem); + + $json["message"]="success... maybe"; + } + catch (Exception $e) { + $json["message"]=$e->getMessage(); + Logging::log($e->getMessage()); } - //updating a scheduled item. - if (isset($id)) { - $schedItem = CcScheduleQuery::create()->findPK($id); - $duration = $schedItem->getDbEnds('U') - $schedItem->getDbStarts('U'); - - $endDT = DateTime::createFromFormat("U", $starts_epoch + $duration, new DateTimeZone("UTC")); - - $oldInstance = $schedItem->getDbInstanceId(); - - if ($instance === $oldInstance) { - CcScheduleQuery::create() - ->filterByDbInstanceId($oldInstance) - ->find(); - } - //file was dragged out of the show into another show or scheduled not within a show. - else { - } - - - } - else { - $schedItem = new CcSchedule(); - } - - $schedItem->setDbStarts($startDT); - $schedItem->setDbEnds($endDT); - $schedItem->save(); + $this->view->data = $json; } } \ No newline at end of file diff --git a/airtime_mvc/application/models/Scheduler.php b/airtime_mvc/application/models/Scheduler.php index 724ef8ba0..61debe89c 100644 --- a/airtime_mvc/application/models/Scheduler.php +++ b/airtime_mvc/application/models/Scheduler.php @@ -3,6 +3,15 @@ class Application_Model_Scheduler { private $con; + private $fileInfo = array( + "id" => "", + "cliplength" => "", + "cuein" => "00:00:00", + "cueout" => "00:00:00", + "fadein" => "00:00:00", + "fadeout" => "00:00:00", + "sched_id" => null, + ); public function __construct($id = null) { @@ -16,21 +25,12 @@ class Application_Model_Scheduler { */ private function retrieveMediaFiles($id, $type) { - $fileInfo = array( - "id" => "", - "cliplength" => "", - "cuein" => "00:00:00", - "cueout" => "00:00:00", - "fadein" => "00:00:00", - "fadeout" => "00:00:00" - ); - $files = array(); if ($type === "audioclip") { $file = CcFilesQuery::create()->findPK($id); - $data = $fileInfo; + $data = $this->fileInfo; $data["id"] = $id; $data["cliplength"] = $file->getDbLength(); @@ -60,7 +60,6 @@ class Application_Model_Scheduler { public function scheduleAfter($scheduleItems, $mediaItems, $adjustSched = true) { $this->con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME); - $this->con->beginTransaction(); $schedFiles = array(); @@ -73,6 +72,24 @@ class Application_Model_Scheduler { $schedFiles = array_merge($schedFiles, $this->retrieveMediaFiles($media["id"], $media["type"])); } + $this->insertAfter($scheduleItems, $schedFiles, $adjustSched); + + $this->con->commit(); + } + catch (Exception $e) { + $this->con->rollback(); + throw $e; + } + } + + /* + * @param array $scheduledIds + * @param array $fileIds + * @param array $playlistIds + */ + private function insertAfter($scheduleItems, $schedFiles, $adjustSched = true) { + + try { foreach ($scheduleItems as $schedule) { $id = intval($schedule["id"]); @@ -85,6 +102,7 @@ class Application_Model_Scheduler { //user has an old copy of the time line opened. if ($instance !== intval($schedule["instance"])) { + Logging::log("items have been since updated"); return; } @@ -113,17 +131,25 @@ class Application_Model_Scheduler { $endTimeEpoch = $nextStartDT->format("U") + $durationDT->format("U"); $endTimeDT = DateTime::createFromFormat("U", $endTimeEpoch, new DateTimeZone("UTC")); - $newItem = new CcSchedule(); - $newItem->setDbStarts($nextStartDT); - $newItem->setDbEnds($endTimeDT); - $newItem->setDbFileId($file['id']); - $newItem->setDbCueIn($file['cuein']); - $newItem->setDbCueOut($file['cueout']); - $newItem->setDbFadeIn($file['fadein']); - $newItem->setDbFadeOut($file['fadeout']); - $newItem->setDbClipLength($durationDT->format("H:i:s.u")); - $newItem->setDbInstanceId($instance); - $newItem->save($this->con); + $sched = new CcSchedule(); + + //item existed previously and is being moved. + //TODO make it possible to insert the primary key of the item + //need to keep same id for resources if we want REST. + if (isset($file['sched_id'])) { + //$sched->setDbId($file['sched_id']); + } + + $sched->setDbStarts($nextStartDT); + $sched->setDbEnds($endTimeDT); + $sched->setDbFileId($file['id']); + $sched->setDbCueIn($file['cuein']); + $sched->setDbCueOut($file['cueout']); + $sched->setDbFadeIn($file['fadein']); + $sched->setDbFadeOut($file['fadeout']); + $sched->setDbClipLength($durationDT->format("H:i:s.u")); + $sched->setDbInstanceId($instance); + $sched->save($this->con); $nextStartDT = $endTimeDT; } @@ -149,6 +175,59 @@ class Application_Model_Scheduler { } } + } + catch (Exception $e) { + throw $e; + } + } + + /* + * @param array $selectedItem + * @param array $afterItem + */ + public function moveItem($selectedItem, $afterItem, $adjustSched = true) { + + $this->con = Propel::getConnection(CcSchedulePeer::DATABASE_NAME); + + $this->con->beginTransaction(); + + try { + + $origSelIns = intval($selectedItem[0]["instance"]); + $origAfterIns = intval($afterItem[0]["instance"]); + + Logging::log("Moving item {$selectedItem[0]["id"]}"); + Logging::log("After {$afterItem[0]["id"]}"); + + $selected = CcScheduleQuery::create()->findPk($selectedItem[0]["id"]); + $after = CcScheduleQuery::create()->findPk($afterItem[0]["id"]); + + if ($origSelIns !== $selected->getDBInstanceId() + || $origAfterIns !== $after->getDBInstanceId()) { + + Logging::log("items have been since updated"); + return; + } + + $selected->delete($this->con); + $this->removeGaps($origSelIns); + + //moved to another show, remove gaps from original show. + if ($adjustSched === true && $origSelIns !== $origAfterIns) { + + } + + $data = $this->fileInfo; + $data["id"] = $selected->getDbFileId(); + $data["cliplength"] = $selected->getDbClipLength(); + $data["cuein"] = $selected->getDbCueIn(); + $data["cueout"] = $selected->getDbCueOut(); + $data["fadein"] = $selected->getDbFadeIn(); + $data["fadeout"] = $selected->getDbFadeOut(); + $data["sched_id"] = $selected->getDbId(); + + $this->insertAfter($afterItem, array($data), $adjustSched); + $this->con->commit(); } catch (Exception $e) { @@ -219,7 +298,6 @@ class Application_Model_Scheduler { Logging::log($itemStartDT->format("Y-m-d H:i:s")); Logging::log("duration"); - Logging::log($durationDT->format("Y-m-d H:i:s")); Logging::log($durationDT->format("U"). "seconds"); $endEpoch = $itemStartDT->format("U") + $durationDT->format("U"); diff --git a/airtime_mvc/public/js/airtime/showbuilder/builder.js b/airtime_mvc/public/js/airtime/showbuilder/builder.js index 2292d32b5..e9bd96d7b 100644 --- a/airtime_mvc/public/js/airtime/showbuilder/builder.js +++ b/airtime_mvc/public/js/airtime/showbuilder/builder.js @@ -325,8 +325,8 @@ $(document).ready(function() { fnUpdate; fnAdd = function() { - var aSchedIds = [], - aMediaIds = []; + var aMediaIds = [], + aSchedIds = []; aSchedIds.push({"id": oPrevData.id, "instance": oPrevData.instance}); aMediaIds.push({"id": oItemData.id, "type": oItemData.ftype}); @@ -338,6 +338,20 @@ $(document).ready(function() { }); }; + fnMove = function() { + var aSelect = [], + aAfter = []; + + aSelect.push({"id": oItemData.id, "instance": oItemData.instance}); + aAfter.push({"id": oPrevData.id, "instance": oPrevData.instance}); + + $.post("/showbuilder/schedule-move", + {"format": "json", "selectedItem": aSelect, "afterItem": aAfter}, + function(json){ + oTable.fnDraw(); + }); + }; + fnReceive = function(event, ui) { origRow = ui.item; }; @@ -353,6 +367,7 @@ $(document).ready(function() { //item was reordered. else { oItemData = ui.item.data("aData"); + fnMove(); } origRow = undefined;