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 @@