From eae93008b4816b5b9ac76c1172755a12a19717d2 Mon Sep 17 00:00:00 2001 From: drigato Date: Mon, 2 Dec 2013 16:40:15 -0500 Subject: [PATCH] CC-5589: SQL error when adding/moving a scheduled item Error was occuring when a moved item didn't exist in a linked show instance Error was also occuring when trying to insert item into a schedule localtion that does not exist in a linked instance. Fixed by skipping the move action if the file does not exist in a linked instance, cleaning up the position numbers after deleting schedule items, and by setting the insert time to the show start time in a linked instance is empty --- airtime_mvc/application/models/Scheduler.php | 43 ++++++++++++++----- .../models/airtime/CcShowInstances.php | 23 ++++++++++ 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/airtime_mvc/application/models/Scheduler.php b/airtime_mvc/application/models/Scheduler.php index 8bb477bc5..9cb79927c 100644 --- a/airtime_mvc/application/models/Scheduler.php +++ b/airtime_mvc/application/models/Scheduler.php @@ -626,19 +626,32 @@ class Application_Model_Scheduler $pos = Application_Common_Database::prepareAndExecute( $maxPos_sql, array(), Application_Common_Database::COLUMN); - $linkedItem_sql = "SELECT ends FROM cc_schedule ". - "WHERE instance_id = {$instanceId} ". - "AND position = {$pos} ". - "AND playout_status != -1"; - $linkedItemEnds = Application_Common_Database::prepareAndExecute( - $linkedItem_sql, array(), Application_Common_Database::COLUMN); + //show instance has no scheduled tracks + if (empty($pos)) { + $pos = 0; + $nextStartDT = new DateTime($instance["starts"], new DateTimeZone("UTC")); + } else { + + $linkedItem_sql = "SELECT ends FROM cc_schedule ". + "WHERE instance_id = {$instanceId} ". + "AND position = {$pos} ". + "AND playout_status != -1"; + $linkedItemEnds = Application_Common_Database::prepareAndExecute( + $linkedItem_sql, array(), Application_Common_Database::COLUMN); + + $nextStartDT = $this->findNextStartTime( + new DateTime($linkedItemEnds, new DateTimeZone("UTC")), + $instanceId); + } + } else { + $nextStartDT = $this->findNextStartTime( + new DateTime($linkedItemEnds, new DateTimeZone("UTC")), + $instanceId); + + $pos++; } - $nextStartDT = $this->findNextStartTime( - new DateTime($linkedItemEnds, new DateTimeZone("UTC")), - $instanceId); - - $pos++; + //$pos++; } //selected empty row to add after else { @@ -722,6 +735,13 @@ class Application_Model_Scheduler $sched = Application_Common_Database::prepareAndExecute( $movedItem_sql, array(), Application_Common_Database::SINGLE); } + /* If we don't find a schedule item it means the linked + * shows have a different amount of items (dyanmic block) + * and we should skip the item move for this show instance + */ + if (!$sched) { + continue; + } $excludeIds[] = intval($sched["id"]); $file["cliplength"] = $sched["clip_length"]; @@ -1160,6 +1180,7 @@ class Application_Model_Scheduler foreach ($instances as $instance) { $instance->updateScheduleStatus($this->con); + $instance->correctSchedulePositions(); } //update the last scheduled timestamp. diff --git a/airtime_mvc/application/models/airtime/CcShowInstances.php b/airtime_mvc/application/models/airtime/CcShowInstances.php index 6fdaaf8a2..e7dace7b1 100644 --- a/airtime_mvc/application/models/airtime/CcShowInstances.php +++ b/airtime_mvc/application/models/airtime/CcShowInstances.php @@ -138,6 +138,29 @@ class CcShowInstances extends BaseCcShowInstances { $this->setDbLastScheduled(gmdate("Y-m-d H:i:s")); $this->save($con); } + + /** + * + * This function resets the cc_schedule table's position numbers so that + * tracks for each cc_show_instances start at position 1 + * + * The position numbers can become out of sync when the user deletes items + * from linekd shows filled with dyanmic smart blocks, where each instance + * has a different amount of scheduled items + */ + public function correctSchedulePositions() + { + $schedule = CcScheduleQuery::create() + ->filterByDbInstanceId($this->id) + ->orderByDbStarts() + ->find(); + + $pos = 0; + foreach ($schedule as $item) { + $item->setDbPosition($pos)->save(); + $pos++; + } + } /** * Computes the value of the aggregate column time_filled