From e9627bca075ddb5f86b4e2bc2bb69b1ef50c02e6 Mon Sep 17 00:00:00 2001 From: Naomi Aro Date: Wed, 7 Mar 2012 12:20:30 +0100 Subject: [PATCH] CC-3174 : showbuilder only updating the timeline view if it is needed. --- .../controllers/ShowbuilderController.php | 36 +++++++++++++++- airtime_mvc/application/forms/ShowBuilder.php | 6 +++ airtime_mvc/application/models/Show.php | 6 ++- .../application/models/ShowBuilder.php | 43 +++++++++++++++++-- .../views/scripts/form/showbuilder.phtml | 1 + .../public/js/airtime/showbuilder/builder.js | 38 +++++++++++++++- .../js/airtime/showbuilder/main_builder.js | 42 +++++++++++++++--- 7 files changed, 160 insertions(+), 12 deletions(-) diff --git a/airtime_mvc/application/controllers/ShowbuilderController.php b/airtime_mvc/application/controllers/ShowbuilderController.php index a32d15665..28a9ba804 100644 --- a/airtime_mvc/application/controllers/ShowbuilderController.php +++ b/airtime_mvc/application/controllers/ShowbuilderController.php @@ -10,6 +10,7 @@ class ShowbuilderController extends Zend_Controller_Action ->addActionContext('schedule-add', 'json') ->addActionContext('schedule-remove', 'json') ->addActionContext('builder-dialog', 'json') + ->addActionContext('check-builder-feed', 'json') ->addActionContext('builder-feed', 'json') ->initContext(); } @@ -19,7 +20,7 @@ class ShowbuilderController extends Zend_Controller_Action $this->_helper->layout->setLayout('builder'); $this->view->headScript()->appendFile($this->view->baseUrl('/js/airtime/library/events/library_showbuilder.js'),'text/javascript'); - + $this->_helper->actionStack('library', 'library'); $this->_helper->actionStack('builder', 'showbuilder'); } @@ -88,6 +89,37 @@ class ShowbuilderController extends Zend_Controller_Action $this->view->dialog = $this->view->render('showbuilder/builderDialog.phtml'); } + public function checkBuilderFeedAction() { + + $request = $this->getRequest(); + $current_time = time(); + + $starts_epoch = $request->getParam("start", $current_time); + //default ends is 24 hours after starts. + $ends_epoch = $request->getParam("end", $current_time + (60*60*24)); + $show_filter = intval($request->getParam("showFilter", 0)); + $my_shows = intval($request->getParam("myShows", 0)); + $timestamp = intval($request->getParam("timestamp", -1)); + + $startsDT = DateTime::createFromFormat("U", $starts_epoch, new DateTimeZone("UTC")); + $endsDT = DateTime::createFromFormat("U", $ends_epoch, new DateTimeZone("UTC")); + + Logging::log("showbuilder starts {$startsDT->format("Y-m-d H:i:s")}"); + Logging::log("showbuilder ends {$endsDT->format("Y-m-d H:i:s")}"); + + $opts = array("myShows" => $my_shows, "showFilter" => $show_filter); + $showBuilder = new Application_Model_ShowBuilder($startsDT, $endsDT, $opts); + + //only send the schedule back if updates have been made. + // -1 default will always call the schedule to be sent back if no timestamp is defined. + if ($showBuilder->hasBeenUpdatedSince($timestamp)) { + $this->view->update = true; + } + else { + $this->view->update = false; + } + } + public function builderFeedAction() { $request = $this->getRequest(); @@ -98,6 +130,7 @@ class ShowbuilderController extends Zend_Controller_Action $ends_epoch = $request->getParam("end", $current_time + (60*60*24)); $show_filter = intval($request->getParam("showFilter", 0)); $my_shows = intval($request->getParam("myShows", 0)); + $timestamp = intval($request->getParam("timestamp", -1)); $startsDT = DateTime::createFromFormat("U", $starts_epoch, new DateTimeZone("UTC")); $endsDT = DateTime::createFromFormat("U", $ends_epoch, new DateTimeZone("UTC")); @@ -109,6 +142,7 @@ class ShowbuilderController extends Zend_Controller_Action $showBuilder = new Application_Model_ShowBuilder($startsDT, $endsDT, $opts); $this->view->schedule = $showBuilder->GetItems(); + $this->view->timestamp = $current_time; } public function scheduleAddAction() { diff --git a/airtime_mvc/application/forms/ShowBuilder.php b/airtime_mvc/application/forms/ShowBuilder.php index dddc91ffa..0865a51a2 100644 --- a/airtime_mvc/application/forms/ShowBuilder.php +++ b/airtime_mvc/application/forms/ShowBuilder.php @@ -11,6 +11,12 @@ class Application_Form_ShowBuilder extends Zend_Form_SubForm array('ViewScript', array('viewScript' => 'form/showbuilder.phtml')) )); + //set value to -1 originally to ensure we grab the schedule on first call. + $timestamp = new Zend_Form_Element_Hidden('sb_timestamp'); + $timestamp->setValue(-1) + ->setDecorators(array('ViewHelper')); + $this->addElement($timestamp); + // Add start date element $startDate = new Zend_Form_Element_Text('sb_date_start'); $startDate->class = 'input_text'; diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 6fa4fd132..e29aa71cb 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -1448,7 +1448,8 @@ class Application_Model_Show { } $sql = "SELECT starts, ends, record, rebroadcast, instance_id, show_id, name, - color, background_color, file_id, cc_show_instances.id AS instance_id + color, background_color, file_id, cc_show_instances.id AS instance_id, + last_scheduled FROM cc_show_instances LEFT JOIN cc_show ON cc_show.id = cc_show_instances.show_id WHERE cc_show_instances.modified_instance = FALSE"; @@ -1479,6 +1480,9 @@ class Application_Model_Show { $sql = $sql." AND ({$exclude})"; } + Logging::log("getShows"); + Logging::log($sql); + return $CC_DBC->GetAll($sql); } diff --git a/airtime_mvc/application/models/ShowBuilder.php b/airtime_mvc/application/models/ShowBuilder.php index 231293468..355621872 100644 --- a/airtime_mvc/application/models/ShowBuilder.php +++ b/airtime_mvc/application/models/ShowBuilder.php @@ -6,8 +6,12 @@ require_once 'formatters/TimeFilledFormatter.php'; class Application_Model_ShowBuilder { private $timezone; + + //in UTC timezone private $startDT; + //in UTC timezone private $endDT; + private $user; private $opts; @@ -59,12 +63,12 @@ class Application_Model_ShowBuilder { $showStartDT = new DateTime($p_item["si_starts"], new DateTimeZone("UTC")); $schedStartDT = new DateTime($p_item["sched_starts"], new DateTimeZone("UTC")); - + $showStartEpoch = intval($showStartDT->format('U')); $schedStartEpoch = intval($schedStartDT->format('U')); //can only schedule the show if item hasn't started and you are allowed. - if ($this->epoch_now < max($showStartEpoch, $schedStartEpoch) + if ($this->epoch_now < max($showStartEpoch, $schedStartEpoch) && $this->user->canSchedule($p_item["show_id"]) == true) { $row["allowed"] = true; } @@ -89,7 +93,7 @@ class Application_Model_ShowBuilder { } private function isCurrent($p_epochItemStart, $p_epochItemEnd, &$row) { - + if ($this->epoch_now >= $p_epochItemStart && $this->epoch_now < $p_epochItemEnd) { $row["current"] = true; //how many seconds the view should wait to redraw itself. @@ -194,6 +198,39 @@ class Application_Model_ShowBuilder { return $row; } + /* + * @param int $timestamp Unix timestamp in seconds. + * + * @return boolean whether the schedule in the show builder's range has been updated. + * + */ + public function hasBeenUpdatedSince($timestamp) { + $outdated = false; + + Logging::log("checking if show builder has been updated since {$timestamp}"); + + $shows = Application_Model_Show::getShows($this->startDT, $this->endDT); + + foreach ($shows as $show) { + + if (isset($show["last_scheduled"])) { + $dt = new DateTime($show["last_scheduled"], new DateTimeZone("UTC")); + + //check if any of the shows have a more recent timestamp. + if ($timestamp < intval($dt->format("U"))) { + $outdated = true; + break; + } + } + } + + if (count($shows) == 0) { + $outdated = true; + } + + return $outdated; + } + public function GetItems() { $current_id = -1; diff --git a/airtime_mvc/application/views/scripts/form/showbuilder.phtml b/airtime_mvc/application/views/scripts/form/showbuilder.phtml index fb4c3b135..e2b67262e 100644 --- a/airtime_mvc/application/views/scripts/form/showbuilder.phtml +++ b/airtime_mvc/application/views/scripts/form/showbuilder.phtml @@ -1,4 +1,5 @@
+ element->getElement('sb_timestamp') ?> element->getElement('sb_date_start') ?> element->getElement('sb_time_start') ?> element->getElement('sb_date_end') ?> diff --git a/airtime_mvc/public/js/airtime/showbuilder/builder.js b/airtime_mvc/public/js/airtime/showbuilder/builder.js index 74b3b18b3..e82fe625d 100644 --- a/airtime_mvc/public/js/airtime/showbuilder/builder.js +++ b/airtime_mvc/public/js/airtime/showbuilder/builder.js @@ -14,6 +14,32 @@ var AIRTIME = (function(AIRTIME){ } } + mod.resetTimestamp = function() { + var timestamp = $("#sb_timestamp"); + //reset timestamp value since input values could have changed. + timestamp.val(-1); + }; + + mod.setTimestamp = function(timestamp) { + $("#sb_timestamp").val(timestamp); + }; + + mod.getTimestamp = function() { + var timestamp = $("#sb_timestamp"), + val; + + //if the timestamp field is on the page return it, or give the default of -1 + //to ensure a page refresh. + if (timestamp.length === 1) { + val = timestamp.val(); + } + else { + val = -1; + } + + return val; + }; + mod.fnAdd = function(aMediaIds, aSchedIds) { var oLibTT = TableTools.fnGetInstance('library_display'); @@ -47,6 +73,8 @@ var AIRTIME = (function(AIRTIME){ }; fnServerData = function ( sSource, aoData, fnCallback ) { + + aoData.push( { name: "timestamp", value: AIRTIME.showbuilder.getTimestamp()} ); aoData.push( { name: "format", value: "json"} ); if (fnServerData.hasOwnProperty("start")) { @@ -65,7 +93,10 @@ var AIRTIME = (function(AIRTIME){ "type": "GET", "url": sSource, "data": aoData, - "success": fnCallback + "success": function(json) { + AIRTIME.showbuilder.setTimestamp(json.timestamp); + fnCallback(json); + } } ); }; @@ -302,6 +333,7 @@ var AIRTIME = (function(AIRTIME){ aData = tr.data("aData"); setTimeout(function(){ + AIRTIME.showbuilder.resetTimestamp(); oTable.fnDraw(); }, aData.refresh * 1000); //need refresh in milliseconds } @@ -311,7 +343,9 @@ var AIRTIME = (function(AIRTIME){ }, //remove any selected nodes before the draw. "fnPreDrawCallback": function( oSettings ) { - var oTT = TableTools.fnGetInstance('show_builder_table'); + var oTT; + + oTT = TableTools.fnGetInstance('show_builder_table'); oTT.fnSelectNone(); }, diff --git a/airtime_mvc/public/js/airtime/showbuilder/main_builder.js b/airtime_mvc/public/js/airtime/showbuilder/main_builder.js index 827ac6ef9..2000f6a34 100644 --- a/airtime_mvc/public/js/airtime/showbuilder/main_builder.js +++ b/airtime_mvc/public/js/airtime/showbuilder/main_builder.js @@ -109,6 +109,9 @@ $(document).ready(function(){ op, oTable = $('#show_builder_table').dataTable(); + //reset timestamp value since input values could have changed. + AIRTIME.showbuilder.resetTimestamp(); + oRange = fnGetScheduleRange(); fn = oTable.fnSettings().fnServerData; @@ -136,6 +139,9 @@ $(document).ready(function(){ if ($button.hasClass("sb-edit")) { + //reset timestamp to redraw the cursors. + AIRTIME.showbuilder.resetTimestamp(); + $lib.show(); $lib.width(Math.floor(screenWidth * 0.5)); $builder.width(Math.floor(screenWidth * 0.5)); @@ -154,8 +160,7 @@ $(document).ready(function(){ $button.val("Add Files"); } - oTable.fnDraw(); - + oTable.fnDraw(); }); oRange = fnGetScheduleRange(); @@ -165,8 +170,35 @@ $(document).ready(function(){ AIRTIME.library.libraryInit(); AIRTIME.showbuilder.builderDataTable(); + //check if the timeline viewed needs updating. setInterval(function(){ - var oTable = $('#show_builder_table').dataTable(); - oTable.fnDraw(); - }, 20 * 1000); //need refresh in milliseconds + var data = {}, + oTable = $('#show_builder_table').dataTable(), + fn = oTable.fnSettings().fnServerData, + start = fn.start, + end = fn.end; + + data["format"] = "json"; + data["start"] = start; + data["end"] = end; + data["timestamp"] = AIRTIME.showbuilder.getTimestamp(); + + if (fn.hasOwnProperty("ops")) { + data["myShows"] = fn.ops.myShows; + data["showFilter"] = fn.ops.showFilter; + } + + $.ajax( { + "dataType": "json", + "type": "GET", + "url": "/showbuilder/check-builder-feed", + "data": data, + "success": function(json) { + if (json.update === true) { + oTable.fnDraw(); + } + } + } ); + + }, 5 * 1000); //need refresh in milliseconds }); \ No newline at end of file