diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 7d54065d1..128782ec0 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -270,13 +270,8 @@ class ApiController extends Zend_Controller_Action $limit = "5"; } - $result = array("env"=>APPLICATION_ENV, - "schedulerTime"=>gmdate("Y-m-d H:i:s"), - "currentShow"=>Application_Model_Show::GetCurrentShow($utcTimeNow), - "nextShow"=>Application_Model_Show::GetNextShows($utcTimeNow, $limit, $utcTimeEnd), - "timezone"=> date("T"), - "timezoneOffset"=> date("Z"), - "AIRTIME_API_VERSION"=>AIRTIME_API_VERSION); //used by caller to determine if the airtime they are running or widgets in use is out of date. + $result = Application_Model_Schedule::GetPlayOrderRange(); + $result['AIRTIME_API_VERSION'] = AIRTIME_API_VERSION; //used by caller to determine if the airtime they are running or widgets in use is out of date. //Convert from UTC to localtime for user. Application_Model_Show::ConvertToLocalTimeZone($result["currentShow"], array("starts", "ends", "start_timestamp", "end_timestamp")); diff --git a/airtime_mvc/application/logging/Logging.php b/airtime_mvc/application/logging/Logging.php index 4b23de04f..d6fb070b3 100644 --- a/airtime_mvc/application/logging/Logging.php +++ b/airtime_mvc/application/logging/Logging.php @@ -23,7 +23,7 @@ class Logging { } public static function debug($p_msg){ - if (APPLICATION_ENV == "development"){ + if (defined('APPLICATION_ENV') && APPLICATION_ENV == "development"){ $logger = self::getLogger(); $logger->debug($p_msg); } diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index e0ebbc604..86d2986e5 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -45,7 +45,6 @@ class Application_Model_Schedule { } } - /** * Returns data related to the scheduled items. * @@ -60,23 +59,24 @@ class Application_Model_Schedule { return array(); } - global $CC_CONFIG; - $date = new Application_Model_DateHelper; $timeNow = $date->getTimestamp(); $utcTimeNow = $date->getUtcTimestamp(); - $currentShow = Application_Model_Show::GetCurrentShow($utcTimeNow); - $results = Application_Model_Schedule::GetPrevCurrentNext($utcTimeNow); + + $shows = Application_Model_Show::getPrevCurrentNext($utcTimeNow); + $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['id']:null; + $results = Application_Model_Schedule::GetPrevCurrentNext($currentShowID, $utcTimeNow); $range = array("env"=>APPLICATION_ENV, "schedulerTime"=>$timeNow, - "previous"=>$results['previous'], - "current"=>$results['current'], - "next"=>$results['next'], - "currentShow"=>$currentShow, - "nextShow"=>"",//Application_Model_Show::GetNextShows($utcTimeNow, 1), + "previous"=>isset($results['previous'])?$results['previous']:(count($shows['previousShow'])>0?$shows['previousShow'][0]:null), + "current"=>isset($results['current'])?$results['current']:null, + "next"=> isset($results['next'])?$results['next']:(count($shows['nextShow'])>0?$shows['nextShow'][0]:null), + "currentShow"=>$shows['currentShow'], + "nextShow"=>$shows['nextShow'], "timezone"=> date("T"), "timezoneOffset"=> date("Z")); + return $range; } @@ -88,54 +88,62 @@ class Application_Model_Schedule { * show types are not found through this mechanism a call is made to the old way of querying * the database to find the track info. **/ - public static function GetPrevCurrentNext($p_timeNow) + public static function GetPrevCurrentNext($currentShowID, $p_timeNow) { global $CC_CONFIG, $CC_DBC; - + if (!isset($currentShowID)) { + return array(); + } $sql = "Select ft.artist_name, ft.track_title, st.starts as starts, st.ends as ends, st.media_item_played as media_item_played - FROM cc_schedule st LEFT JOIN cc_files ft ON st.file_id = ft.id LEFT JOIN cc_show_instances sit ON st.instance_id = sit.id - WHERE st.starts > (TIMESTAMP '$p_timeNow'-INTERVAL '24 hours') AND st.starts < (TIMESTAMP '$p_timeNow'+INTERVAL '24 hours') AND st.starts < sit.ends + FROM cc_schedule st LEFT JOIN cc_files ft ON st.file_id = ft.id + WHERE st.instance_id = '$currentShowID' AND st.playout_status > 0 ORDER BY st.starts"; - $row = $CC_DBC->GetAll($sql); - $numberOfRows = count($row); + //Logging::log($sql); + $rows = $CC_DBC->GetAll($sql); + $numberOfRows = count($rows); $results; + $results['previous'] = null; + $results['current'] = null; + $results['next'] = null; $timeNowAsMillis = strtotime($p_timeNow); - for( $i = 0; $i < $numberOfRows && !isset($results); ++$i ){ - if ((strtotime($row[$i]['starts']) <= $timeNowAsMillis) && (strtotime($row[$i]['ends']) >= $timeNowAsMillis)){ + for( $i = 0; $i < $numberOfRows; ++$i ){ + if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) && (strtotime($rows[$i]['ends']) >= $timeNowAsMillis)){ if ( $i - 1 >= 0){ - $results['previous'] = array("name"=>$row[$i-1]["artist_name"]." - ".$row[$i-1]["track_title"], - "starts"=>$row[$i-1]["starts"], - "ends"=>$row[$i-1]["ends"]); + $results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"], + "starts"=>$rows[$i-1]["starts"], + "ends"=>$rows[$i-1]["ends"]); } - $results['current'] = array("name"=>$row[$i]["artist_name"]." - ".$row[$i]["track_title"], - "starts"=>$row[$i]["starts"], - "ends"=>$row[$i]["ends"], - "media_item_played"=>$row[$i]["media_item_played"], + $results['current'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], + "starts"=>$rows[$i]["starts"], + "ends"=>$rows[$i]["ends"], + "media_item_played"=>$rows[$i]["media_item_played"], "record"=>0); - if ( isset($row[$i+1])){ - $results['next'] = array("name"=>$row[$i+1]["artist_name"]." - ".$row[$i+1]["track_title"], - "starts"=>$row[$i+1]["starts"], - "ends"=>$row[$i+1]["ends"]); + + if ( isset($rows[$i+1])){ + $results['next'] = array("name"=>$rows[$i+1]["artist_name"]." - ".$rows[$i+1]["track_title"], + "starts"=>$rows[$i+1]["starts"], + "ends"=>$rows[$i+1]["ends"]); } break; } - } - - if (!isset($results['previous'])){ - $results['previous'] = Application_Model_Dashboard::GetPreviousItem($p_timeNow); - } - if (!isset($results['current'])){ - $results['current'] = Application_Model_Dashboard::GetCurrentItem($p_timeNow); - } - if (!isset($results['next'])) { - $results['next'] = Application_Model_Dashboard::GetNextItem($p_timeNow); + if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { + $results['previous'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], + "starts"=>$rows[$i]["starts"], + "ends"=>$rows[$i]["ends"]);; + } + if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { + $results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], + "starts"=>$rows[$i]["starts"], + "ends"=>$rows[$i]["ends"]); + break; + } } return $results; } - + public static function GetLastScheduleItem($p_timeNow){ global $CC_CONFIG, $CC_DBC; diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 2b61d7604..253c4c0c8 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -1693,6 +1693,71 @@ class Application_Model_Show { return $rows; } + /** + * Gets the current show, previous and next with an 2day window from the given timeNow, so timeNow-2days and timeNow+2days. + */ + public static function getPrevCurrentNext($timeNow) + { + global $CC_CONFIG, $CC_DBC; + //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin + $sql = "SELECT si.starts as start_timestamp, si.ends as end_timestamp, s.name, s.id, si.id as instance_id, si.record, s.url, starts, ends" + ." FROM $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s" + ." WHERE si.show_id = s.id" + ." AND si.starts > TIMESTAMP '$timeNow' - INTERVAL '2 days'" + ." AND si.ends < TIMESTAMP '$timeNow' + INTERVAL '2 days'" + ." AND modified_instance != TRUE"; + + //Logging::log($sql); + // Convert back to local timezone + $rows = $CC_DBC->GetAll($sql); + $numberOfRows = count($rows); + $results; + $results['previousShow'] = array(); + $results['currentShow'] = array(); + $results['nextShow'] = array(); + + $timeNowAsMillis = strtotime($timeNow); + for( $i = 0; $i < $numberOfRows; ++$i ){ + if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) && (strtotime($rows[$i]['ends']) >= $timeNowAsMillis)){ + if ( $i - 1 >= 0){ + $results['previousShow'][0] = array("name"=>$rows[$i-1]['name'], + "start_timestamp"=>$rows[$i-1]['start_timestamp'], + "end_timestamp"=>$rows[$i-1]['end_timestamp'], + "starts"=>$rows[$i-1]['starts'], + "ends"=>$rows[$i-1]['ends']); + } + + $results['currentShow'][0] = $rows[$i]; + + if ( isset($rows[$i+1])){ + $results['nextShow'][0] = array("name"=>$rows[$i+1]['name'], + "start_timestamp"=>$rows[$i+1]['start_timestamp'], + "end_timestamp"=>$rows[$i+1]['end_timestamp'], + "starts"=>$rows[$i+1]['starts'], + "ends"=>$rows[$i+1]['ends']); + + } + break; + } + if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { + $results['previousShow'][0] = array("name"=>$rows[$i]['name'], + "start_timestamp"=>$rows[$i]['start_timestamp'], + "end_timestamp"=>$rows[$i]['end_timestamp'], + "starts"=>$rows[$i]['starts'], + "ends"=>$rows[$i]['ends']); + } + if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { + $results['nextShow'][0] = array("name"=>$rows[$i]['name'], + "start_timestamp"=>$rows[$i]['start_timestamp'], + "end_timestamp"=>$rows[$i]['end_timestamp'], + "starts"=>$rows[$i]['starts'], + "ends"=>$rows[$i]['ends']); + break; + } + } + + return $results; + } /** * Given a start time $timeStart and end time $timeEnd, returns the next $limit * number of shows within the time interval;