From 44b4faf6d9232d666da9c3c189cf8bcb412df8e7 Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Mon, 17 Nov 2014 21:53:31 +0100 Subject: [PATCH 1/8] added shows, show-schedules, show-preview, show-history-feed, item-history-feed endpoints to the HTTP api to be used via NewscoopAirtimePlugin --- .../application/controllers/ApiController.php | 215 +++++++++++++++++- airtime_mvc/application/models/Show.php | 71 +++++- .../application/models/ShowInstance.php | 13 +- .../application/services/HistoryService.php | 56 +++-- 4 files changed, 307 insertions(+), 48 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 6448342ae..5c3b54063 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -5,8 +5,16 @@ class ApiController extends Zend_Controller_Action public function init() { - $ignoreAuth = array("live-info", "live-info-v2", "week-info", - "station-metadata", "station-logo"); + $ignoreAuth = array("live-info", + "live-info-v2", + "week-info", + "station-metadata", + "station-logo", + "show-history-feed", + "item-history-feed", + "shows", + "show-schedules" + ); $params = $this->getRequest()->getParams(); if (!in_array($params['action'], $ignoreAuth)) { @@ -47,6 +55,7 @@ class ApiController extends Zend_Controller_Action ->addActionContext('update-stream-setting-table' , 'json') ->addActionContext('update-replay-gain-value' , 'json') ->addActionContext('update-cue-values-by-silan' , 'json') + ->addActionContext('show-preview' , 'json') ->initContext(); } @@ -274,10 +283,10 @@ class ApiController extends Zend_Controller_Action $utcTimeEnd = $end->format("Y-m-d H:i:s"); $result = array( - "env" => APPLICATION_ENV, - "schedulerTime" => $utcTimeNow, - "currentShow" => Application_Model_Show::getCurrentShow($utcTimeNow), - "nextShow" => Application_Model_Show::getNextShows($utcTimeNow, $limit, $utcTimeEnd) + "env" => APPLICATION_ENV, + "schedulerTime" => $utcTimeNow, + "currentShow" => Application_Model_Show::getCurrentShow($utcTimeNow), + "nextShow" => Application_Model_Show::getNextShows($utcTimeNow, $limit, $utcTimeEnd) ); } else { $result = Application_Model_Schedule::GetPlayOrderRangeOld($limit); @@ -484,9 +493,9 @@ class ApiController extends Zend_Controller_Action $shows, array("starts", "ends", "start_timestamp","end_timestamp"), $timezone - ); + ); - $result[$dow[$i]] = $shows; + $result[$dow[$i]] = $shows; } // XSS exploit prevention @@ -1320,4 +1329,194 @@ class ApiController extends Zend_Controller_Action Application_Model_StreamSetting::SetListenerStatError($k, $v); } } + + /** + * display played items for a given time range and show instance_id + * + * @return json array + */ + public function itemHistoryFeedAction() + { + try { + $request = $this->getRequest(); + $params = $request->getParams(); + $instance = $request->getParam("instance_id", null); + + list($startsDT, $endsDT) = $this->getStartEnd($request); + + $historyService = new Application_Service_HistoryService(); + $results = $historyService->getPlayedItemData($startsDT, $endsDT, $params, $instance); + + $this->_helper->json->sendJson($results['history']); + } + catch (Exception $e) { + Logging::info($e); + Logging::info($e->getMessage()); + } + } + + /** + * display show schedules for a given time range and show instance_id + * + * @return json array + */ + public function showHistoryFeedAction() + { + try { + $request = $this->getRequest(); + $params = $request->getParams(); + $userId = $request->getParam("user_id", null); + + list($startsDT, $endsDT) = $this->getStartEnd($request); + + $historyService = new Application_Service_HistoryService(); + $shows = $historyService->getShowList($startsDT, $endsDT, $userId); + + $this->_helper->json->sendJson($shows); + } + catch (Exception $e) { + Logging::info($e); + Logging::info($e->getMessage()); + } + } + + /** + * display show info (without schedule) for given show_id + * + * @return json array + */ + public function showsAction() + { + try { + $request = $this->getRequest(); + $params = $request->getParams(); + $showId = $request->getParam("show_id", null); + + if (empty($showId)) { + $shows = Application_Model_Show::getDistinctShows(); + } else { + $show = new Application_Model_Show($showId); + $shows = $show->getShowInfo(); + } + + $this->_helper->json->sendJson($shows); + } + catch (Exception $e) { + Logging::info($e); + Logging::info($e->getMessage()); + } + } + + /** + * display show schedule for given show_id + * + * @return json array + */ + public function showSchedulesAction() + { + try { + $request = $this->getRequest(); + $params = $request->getParams(); + $showId = $request->getParam("show_id", null); + + list($startsDT, $endsDT) = $this->getStartEnd($request); + + $shows = Application_Model_Show::getShows($startsDT, $endsDT, FALSE, $showId); + + $this->_helper->json->sendJson($shows); + } + catch (Exception $e) { + Logging::info($e); + Logging::info($e->getMessage()); + } + + } + + public function showPreviewAction() + { + $baseUrl = Application_Common_OsPath::getBaseDir(); + $prefTimezone = Application_Model_Preference::GetTimezone(); + + $instanceId = $this->_getParam('instance_id'); + $apiKey = $this->_getParam('api_key'); + + if (!isset($instanceId)) { + return; + } + + $showInstance = new Application_Model_ShowInstance($instanceId); + $result = array(); + $position = 0; + foreach ($showInstance->getShowListContent($prefTimezone) as $track) { + + $elementMap = array( + 'title' => isset($track['track_title']) ? $track['track_title'] : "", + 'artist' => isset($track['creator']) ? $track['creator'] : "", + 'position' => $position, + 'id' => ++$position, + 'mime' => isset($track['mime'])?$track['mime']:"", + 'starts' => isset($track['starts']) ? $track['starts'] : "", + 'length' => isset($track['length']) ? $track['length'] : "", + 'file_id' => ($track['type'] == 0) ? $track['item_id'] : $track['filepath'] + ); + + $result[] = $elementMap; + } + + $this->_helper->json($result); + + } + + /** + * sets start and end vars from given params, or defauls to + * yesterday - today using server timezone + */ + private function getStartEnd($request) + { + $prefTimezone = Application_Model_Preference::GetTimezone(); + $utcTimezone = new DateTimeZone("UTC"); + $utcNow = new DateTime("now", $utcTimezone); + + $start = $request->getParam("start"); + $end = $request->getParam("end"); + $timezone = $request->getParam("timezone"); + + if (empty($timezone)) { + $userTimezone = new DateTimeZone($prefTimezone); + } else { + $userTimezone = new DateTimeZone($timezone); + } + + if (empty($start) || empty($end)) { + $startsDT = clone $utcNow; + $startsDT->sub(new DateInterval("P1D")); + $endsDT = clone $utcNow; + } + else { + + try { + $startsDT = new DateTime($start, $userTimezone); + $startsDT->setTimezone($utcTimezone); + + $endsDT = new DateTime($end, $userTimezone); + $endsDT->setTimezone($utcTimezone); + + if ($startsDT > $endsDT) { + throw new Exception("start greater than end"); + } + } + catch (Exception $e) { + Logging::info($e); + Logging::info($e->getMessage()); + + $startsDT = clone $utcNow; + $startsDT->sub(new DateInterval("P1D")); + $endsDT = clone $utcNow; + } + + } + + return array($startsDT, $endsDT); + } + } diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 987b5a6c2..4d884a0f8 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -786,6 +786,34 @@ SQL; } } + /** + * returns show specific info (not related to isntance) + */ + public function getShowInfo() + { + $info = array(); + if ($this->getId() == null) { + return $info; + } else { + $ccShow = CcShowQuery::create()->findPK($this->_showId); + $info['name'] = $ccShow->getDbName(); + $info['id'] = $ccShow->getDbId(); + $info['url'] = $ccShow->getDbUrl(); + $info['genre'] = $ccShow->getDbGenre(); + $info['description'] = $ccShow->getDbDescription(); + $info['color'] = $ccShow->getDbColor(); + $info['background_color'] = $ccShow->getDbBackgroundColor(); + $info['custom_username'] = $ccShow->getDbLiveStreamUser(); + $info['cb_airtime_auth'] = $ccShow->getDbLiveStreamUsingAirtimeAuth(); + $info['cb_custom_auth'] = $ccShow->getDbLiveStreamUsingCustomAuth(); + $info['custom_username'] = $ccShow->getDbLiveStreamUser(); + $info['custom_password'] = $ccShow->getDbLiveStreamPass(); + $info['linked'] = $ccShow->getDbLinked(); + $info['is_linkable'] = $ccShow->getDbIsLinkable(); + return $info; + } + } + /* Only used for shows that are repeating. Note that this will return * true even for dates that only have a "modified" show instance (does not * check if the "modified_instance" column is set to true). This is intended @@ -862,9 +890,10 @@ SQL; * In UTC time. * @param unknown_type $excludeInstance * @param boolean $onlyRecord + * @param int $showId * @return array */ - public static function getShows($start_timestamp, $end_timestamp, $onlyRecord=FALSE) + public static function getShows($start_timestamp, $end_timestamp, $onlyRecord=FALSE, $showId=null) { self::createAndFillShowInstancesPastPopulatedUntilDate($end_timestamp); @@ -895,13 +924,21 @@ SQL; //only want shows that are starting at the time or later. $start_string = $start_timestamp->format("Y-m-d H:i:s"); $end_string = $end_timestamp->format("Y-m-d H:i:s"); + + $params = array(); + + if ($showId) { + $sql .= " AND (si1.show_id = :show_id)"; + $params[':show_id'] = $showId; + } + if ($onlyRecord) { $sql .= " AND (si1.starts >= :start::TIMESTAMP AND si1.starts < :end::TIMESTAMP)"; $sql .= " AND (si1.record = 1)"; - return Application_Common_Database::prepareAndExecute( $sql, - array( ':start' => $start_string, - ':end' => $end_string ), 'all'); + $params[':start'] = $start_string; + $params[':end'] = $end_string; + return Application_Common_Database::prepareAndExecute( $sql, $params, 'all'); } else { $sql .= " ". <<= :start1::TIMESTAMP AND si1.starts < :end1::TIMESTAMP) OR (si1.starts <= :start3::TIMESTAMP AND si1.ends >= :end3::TIMESTAMP)) ORDER BY si1.starts SQL; - return Application_Common_Database::prepareAndExecute( $sql, - array( + $params = array_merge($params, array( 'start1' => $start_string, 'start2' => $start_string, 'start3' => $start_string, 'end1' => $end_string, 'end2' => $end_string, 'end3' => $end_string - ), 'all'); + ) + ); + return Application_Common_Database::prepareAndExecute( $sql, $params, 'all'); } } @@ -1013,10 +1051,10 @@ SQL; //for putting the now playing icon on the show. if ($now > $startsDT && $now < $endsDT) { - $event["nowPlaying"] = true; + $event["nowPlaying"] = true; } else { - $event["nowPlaying"] = false; + $event["nowPlaying"] = false; } //event colouring @@ -1045,9 +1083,9 @@ SQL; **/ private static function getPercentScheduled($p_starts, $p_ends, $p_time_filled) { - $utcTimezone = new DatetimeZone("UTC"); - $startDt = new DateTime($p_starts, $utcTimezone); - $endDt = new DateTime($p_ends, $utcTimezone); + $utcTimezone = new DatetimeZone("UTC"); + $startDt = new DateTime($p_starts, $utcTimezone); + $endDt = new DateTime($p_ends, $utcTimezone); $durationSeconds = intval($endDt->format("U")) - intval($startDt->format("U")); $time_filled = Application_Common_DateHelper::playlistTimeToSeconds($p_time_filled); if ($durationSeconds != 0) { //Prevent division by zero if the show duration somehow becomes zero. @@ -1448,4 +1486,13 @@ SQL; return array($start, $end); } + + public static function getDistinctShows() { + $sql = << $this->_instanceId )); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); - - $userTimezone = Application_Model_Preference::GetUserTimezone(); - $displayTimezone = new DateTimeZone($userTimezone); + + if (isset($timezone)) { + $displayTimezone = new DateTimeZone($timezone); + } else { + $userTimezone = Application_Model_Preference::GetUserTimezone(); + $displayTimezone = new DateTimeZone($userTimezone); + } + $utcTimezone = new DateTimeZone("UTC"); foreach ($results as &$row) { diff --git a/airtime_mvc/application/services/HistoryService.php b/airtime_mvc/application/services/HistoryService.php index 73821ce92..d6af0a40b 100644 --- a/airtime_mvc/application/services/HistoryService.php +++ b/airtime_mvc/application/services/HistoryService.php @@ -204,30 +204,34 @@ class Application_Service_HistoryService //------------------------------------------------------------------------ //Using Datatables parameters to sort the data. - $numOrderColumns = $opts["iSortingCols"]; - $orderBys = array(); + if (empty($opts["iSortingCols"])) { + $orderBys = array(); + } else { + $numOrderColumns = $opts["iSortingCols"]; + $orderBys = array(); - for ($i = 0; $i < $numOrderColumns; $i++) { + for ($i = 0; $i < $numOrderColumns; $i++) { - $colNum = $opts["iSortCol_".$i]; - $key = $opts["mDataProp_".$colNum]; - $sortDir = $opts["sSortDir_".$i]; + $colNum = $opts["iSortCol_".$i]; + $key = $opts["mDataProp_".$colNum]; + $sortDir = $opts["sSortDir_".$i]; - if (in_array($key, $required)) { + if (in_array($key, $required)) { - $orderBys[] = "history_range.{$key} {$sortDir}"; - } - else if (in_array($key, $filemd_keys)) { + $orderBys[] = "history_range.{$key} {$sortDir}"; + } + else if (in_array($key, $filemd_keys)) { - $orderBys[] = "file_info.{$key} {$sortDir}"; - } - else if (in_array($key, $general_keys)) { + $orderBys[] = "file_info.{$key} {$sortDir}"; + } + else if (in_array($key, $general_keys)) { - $orderBys[] = "{$key}_filter.{$key} {$sortDir}"; - } - else { - //throw new Exception("Error: $key is not part of the template."); - } + $orderBys[] = "{$key}_filter.{$key} {$sortDir}"; + } + else { + //throw new Exception("Error: $key is not part of the template."); + } + } } if (count($orderBys) > 0) { @@ -241,7 +245,7 @@ class Application_Service_HistoryService //--------------------------------------------------------------- //using Datatables parameters to add limits/offsets - $displayLength = intval($opts["iDisplayLength"]); + $displayLength = empty($opts["iDisplayLength"]) ? -1 : intval($opts["iDisplayLength"]); //limit the results returned. if ($displayLength !== -1) { $mainSqlQuery.= @@ -311,7 +315,7 @@ class Application_Service_HistoryService } return array( - "sEcho" => intval($opts["sEcho"]), + "sEcho" => empty($opts["sEcho"]) ? null : intval($opts["sEcho"]), //"iTotalDisplayRecords" => intval($totalDisplayRows), "iTotalDisplayRecords" => intval($totalRows), "iTotalRecords" => intval($totalRows), @@ -445,9 +449,13 @@ class Application_Service_HistoryService ); } - public function getShowList($startDT, $endDT) + public function getShowList($startDT, $endDT, $userId = null) { - $user = Application_Model_User::getCurrentUser(); + if (empty($userId)) { + $user = Application_Model_User::getCurrentUser(); + } else { + $user = new Application_Model_User($userId); + } $shows = Application_Model_Show::getShows($startDT, $endDT); Logging::info($startDT->format("Y-m-d H:i:s")); @@ -456,7 +464,7 @@ class Application_Service_HistoryService Logging::info($shows); //need to filter the list to only their shows - if ($user->isHost()) { + if ((!empty($user)) && ($user->isHost())) { $showIds = array(); @@ -1524,4 +1532,4 @@ class Application_Service_HistoryService throw $e; } } -} \ No newline at end of file +} From 03dae5be2b7236aa27945c277ba0efd91b28027d Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Mon, 17 Nov 2014 21:59:50 +0100 Subject: [PATCH 2/8] fix spacing in ApiController --- airtime_mvc/application/controllers/ApiController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 5c3b54063..88f197116 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -55,7 +55,7 @@ class ApiController extends Zend_Controller_Action ->addActionContext('update-stream-setting-table' , 'json') ->addActionContext('update-replay-gain-value' , 'json') ->addActionContext('update-cue-values-by-silan' , 'json') - ->addActionContext('show-preview' , 'json') + ->addActionContext('show-preview' , 'json') ->initContext(); } From a27ca2f58326be222606d96a611b713bd29f96d0 Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Thu, 20 Nov 2014 17:22:53 +0100 Subject: [PATCH 3/8] cleanup new HTTP apiendpoints, added HTTPHelper and getStartEndFromRequest and updated all controllers that called their own versions of this function --- airtime_mvc/application/Bootstrap.php | 1 + airtime_mvc/application/common/DateHelper.php | 54 +++++++++++++ airtime_mvc/application/common/HTTPHelper.php | 20 +++++ .../application/controllers/ApiController.php | 77 ++++--------------- .../controllers/ListenerstatController.php | 47 +---------- .../controllers/PlayouthistoryController.php | 53 ++----------- .../controllers/ShowbuilderController.php | 47 +---------- airtime_mvc/application/models/Show.php | 13 +--- .../application/services/HistoryService.php | 6 +- 9 files changed, 106 insertions(+), 212 deletions(-) create mode 100644 airtime_mvc/application/common/HTTPHelper.php diff --git a/airtime_mvc/application/Bootstrap.php b/airtime_mvc/application/Bootstrap.php index 7ae9e7388..1e0b31cf4 100644 --- a/airtime_mvc/application/Bootstrap.php +++ b/airtime_mvc/application/Bootstrap.php @@ -11,6 +11,7 @@ require_once __DIR__."/configs/constants.php"; require_once 'Preference.php'; require_once 'Locale.php'; require_once "DateHelper.php"; +require_once "HTTPHelper.php"; require_once "OsPath.php"; require_once "Database.php"; require_once "Timezone.php"; diff --git a/airtime_mvc/application/common/DateHelper.php b/airtime_mvc/application/common/DateHelper.php index ba280d285..354b8f3dc 100644 --- a/airtime_mvc/application/common/DateHelper.php +++ b/airtime_mvc/application/common/DateHelper.php @@ -443,5 +443,59 @@ class Application_Common_DateHelper return $res; } + + /** + * Returns date fields from give start and end teimstamp strings + * if no start or end parameter is passed start will be set to 1 + * in the past and end to now + * + * @param string startTimestamp Y-m-d H:i:s + * @param string endTImestamp Y-m-d H:i:s + * @param string timezone (ex UTC) of the start and end parameters + * @return array (start DateTime, end DateTime) in UTC timezone + */ + public static function getStartEnd($startTimestamp, $endTimestamp, $timezone) + { + $prefTimezone = Application_Model_Preference::GetTimezone(); + $utcTimezone = new DateTimeZone("UTC"); + $utcNow = new DateTime("now", $utcTimezone); + + if (empty($timezone)) { + $userTimezone = new DateTimeZone($prefTimezone); + } else { + $userTimezone = new DateTimeZone($timezone); + } + + // default to 1 day + if (empty($startTimestamp) || empty($endTimestamp)) { + $startsDT = clone $utcNow; + $startsDT->sub(new DateInterval("P1D")); + $endsDT = clone $utcNow; + } else { + + try { + $startsDT = new DateTime($startTimestamp, $userTimezone); + $startsDT->setTimezone($utcTimezone); + + $endsDT = new DateTime($endTimestamp, $userTimezone); + $endsDT->setTimezone($utcTimezone); + + if ($startsDT > $endsDT) { + throw new Exception("start greater than end"); + } + } + catch (Exception $e) { + Logging::info($e); + Logging::info($e->getMessage()); + + $startsDT = clone $utcNow; + $startsDT->sub(new DateInterval("P1D")); + $endsDT = clone $utcNow; + } + + } + + return array($startsDT, $endsDT); + } } diff --git a/airtime_mvc/application/common/HTTPHelper.php b/airtime_mvc/application/common/HTTPHelper.php new file mode 100644 index 000000000..db314bb0b --- /dev/null +++ b/airtime_mvc/application/common/HTTPHelper.php @@ -0,0 +1,20 @@ +getParam("start", null), + $request->getParam("end", null), + $request->getParam("timezone", null) + ); + } +} diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 88f197116..240e562c2 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -13,6 +13,7 @@ class ApiController extends Zend_Controller_Action "show-history-feed", "item-history-feed", "shows", + "show-tracks", "show-schedules" ); @@ -55,7 +56,6 @@ class ApiController extends Zend_Controller_Action ->addActionContext('update-stream-setting-table' , 'json') ->addActionContext('update-replay-gain-value' , 'json') ->addActionContext('update-cue-values-by-silan' , 'json') - ->addActionContext('show-preview' , 'json') ->initContext(); } @@ -1342,7 +1342,7 @@ class ApiController extends Zend_Controller_Action $params = $request->getParams(); $instance = $request->getParam("instance_id", null); - list($startsDT, $endsDT) = $this->getStartEnd($request); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $historyService = new Application_Service_HistoryService(); $results = $historyService->getPlayedItemData($startsDT, $endsDT, $params, $instance); @@ -1367,7 +1367,7 @@ class ApiController extends Zend_Controller_Action $params = $request->getParams(); $userId = $request->getParam("user_id", null); - list($startsDT, $endsDT) = $this->getStartEnd($request); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $historyService = new Application_Service_HistoryService(); $shows = $historyService->getShowList($startsDT, $endsDT, $userId); @@ -1391,15 +1391,20 @@ class ApiController extends Zend_Controller_Action $request = $this->getRequest(); $params = $request->getParams(); $showId = $request->getParam("show_id", null); + $results = array(); if (empty($showId)) { $shows = Application_Model_Show::getDistinctShows(); + foreach($shows as $baseShow) { + $show = new Application_Model_Show($baseShow->getDbId()); + $results[] = $show->getShowInfo(); + } } else { $show = new Application_Model_Show($showId); - $shows = $show->getShowInfo(); + $results[] = $show->getShowInfo(); } - $this->_helper->json->sendJson($shows); + $this->_helper->json->sendJson($results); } catch (Exception $e) { Logging::info($e); @@ -1419,7 +1424,7 @@ class ApiController extends Zend_Controller_Action $params = $request->getParams(); $showId = $request->getParam("show_id", null); - list($startsDT, $endsDT) = $this->getStartEnd($request); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $shows = Application_Model_Show::getShows($startsDT, $endsDT, FALSE, $showId); @@ -1432,13 +1437,17 @@ class ApiController extends Zend_Controller_Action } - public function showPreviewAction() + /** + * displays track listing for given instance_id + * + * @return json array + */ + public function showTracksAction() { $baseUrl = Application_Common_OsPath::getBaseDir(); $prefTimezone = Application_Model_Preference::GetTimezone(); $instanceId = $this->_getParam('instance_id'); - $apiKey = $this->_getParam('api_key'); if (!isset($instanceId)) { return; @@ -1467,56 +1476,4 @@ class ApiController extends Zend_Controller_Action } - /** - * sets start and end vars from given params, or defauls to - * yesterday - today using server timezone - */ - private function getStartEnd($request) - { - $prefTimezone = Application_Model_Preference::GetTimezone(); - $utcTimezone = new DateTimeZone("UTC"); - $utcNow = new DateTime("now", $utcTimezone); - - $start = $request->getParam("start"); - $end = $request->getParam("end"); - $timezone = $request->getParam("timezone"); - - if (empty($timezone)) { - $userTimezone = new DateTimeZone($prefTimezone); - } else { - $userTimezone = new DateTimeZone($timezone); - } - - if (empty($start) || empty($end)) { - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - else { - - try { - $startsDT = new DateTime($start, $userTimezone); - $startsDT->setTimezone($utcTimezone); - - $endsDT = new DateTime($end, $userTimezone); - $endsDT->setTimezone($utcTimezone); - - if ($startsDT > $endsDT) { - throw new Exception("start greater than end"); - } - } - catch (Exception $e) { - Logging::info($e); - Logging::info($e->getMessage()); - - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - - } - - return array($startsDT, $endsDT); - } - } diff --git a/airtime_mvc/application/controllers/ListenerstatController.php b/airtime_mvc/application/controllers/ListenerstatController.php index 5f5250b9c..6e2b93aee 100644 --- a/airtime_mvc/application/controllers/ListenerstatController.php +++ b/airtime_mvc/application/controllers/ListenerstatController.php @@ -10,49 +10,6 @@ class ListenerstatController extends Zend_Controller_Action ->initContext(); } - private function getStartEnd() - { - $request = $this->getRequest(); - - $userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone()); - $utcTimezone = new DateTimeZone("UTC"); - $utcNow = new DateTime("now", $utcTimezone); - - $start = $request->getParam("start"); - $end = $request->getParam("end"); - - if (empty($start) || empty($end)) { - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - else { - - try { - $startsDT = new DateTime($start, $userTimezone); - $startsDT->setTimezone($utcTimezone); - - $endsDT = new DateTime($end, $userTimezone); - $endsDT->setTimezone($utcTimezone); - - if ($startsDT > $endsDT) { - throw new Exception("start greater than end"); - } - } - catch (Exception $e) { - Logging::info($e); - Logging::info($e->getMessage()); - - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - - } - - return array($startsDT, $endsDT); - } - public function indexAction() { $CC_CONFIG = Config::getConfig(); @@ -69,7 +26,7 @@ class ListenerstatController extends Zend_Controller_Action $this->view->headLink()->appendStylesheet($baseUrl.'css/jquery.ui.timepicker.css?'.$CC_CONFIG['airtime_version']); - list($startsDT, $endsDT) = $this->getStartEnd(); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone()); $startsDT->setTimezone($userTimezone); $endsDT->setTimezone($userTimezone); @@ -98,7 +55,7 @@ class ListenerstatController extends Zend_Controller_Action } public function getDataAction(){ - list($startsDT, $endsDT) = $this->getStartEnd(); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($this->getRequest()); $data = Application_Model_ListenerStat::getDataPointsWithinRange($startsDT->format("Y-m-d H:i:s"), $endsDT->format("Y-m-d H:i:s")); $this->_helper->json->sendJson($data); diff --git a/airtime_mvc/application/controllers/PlayouthistoryController.php b/airtime_mvc/application/controllers/PlayouthistoryController.php index 077cce0b2..7b82f7dfd 100644 --- a/airtime_mvc/application/controllers/PlayouthistoryController.php +++ b/airtime_mvc/application/controllers/PlayouthistoryController.php @@ -19,56 +19,13 @@ class PlayouthistoryController extends Zend_Controller_Action ->initContext(); } - private function getStartEnd() - { - $request = $this->getRequest(); - - $userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone()); - $utcTimezone = new DateTimeZone("UTC"); - $utcNow = new DateTime("now", $utcTimezone); - - $start = $request->getParam("start"); - $end = $request->getParam("end"); - - if (empty($start) || empty($end)) { - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - else { - - try { - $startsDT = new DateTime($start, $userTimezone); - $startsDT->setTimezone($utcTimezone); - - $endsDT = new DateTime($end, $userTimezone); - $endsDT->setTimezone($utcTimezone); - - if ($startsDT > $endsDT) { - throw new Exception("start greater than end"); - } - } - catch (Exception $e) { - Logging::info($e); - Logging::info($e->getMessage()); - - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - - } - - return array($startsDT, $endsDT); - } - public function indexAction() { $CC_CONFIG = Config::getConfig(); $baseUrl = Application_Common_OsPath::getBaseDir(); - list($startsDT, $endsDT) = $this->getStartEnd(); - + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($this->getRequest()); + $userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone()); $startsDT->setTimezone($userTimezone); $endsDT->setTimezone($userTimezone); @@ -123,7 +80,7 @@ class PlayouthistoryController extends Zend_Controller_Action $params = $request->getParams(); $instance = $request->getParam("instance_id", null); - list($startsDT, $endsDT) = $this->getStartEnd(); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $historyService = new Application_Service_HistoryService(); $r = $historyService->getFileSummaryData($startsDT, $endsDT, $params); @@ -146,7 +103,7 @@ class PlayouthistoryController extends Zend_Controller_Action $params = $request->getParams(); $instance = $request->getParam("instance_id", null); - list($startsDT, $endsDT) = $this->getStartEnd(); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $historyService = new Application_Service_HistoryService(); $r = $historyService->getPlayedItemData($startsDT, $endsDT, $params, $instance); @@ -169,7 +126,7 @@ class PlayouthistoryController extends Zend_Controller_Action $params = $request->getParams(); $instance = $request->getParam("instance_id", null); - list($startsDT, $endsDT) = $this->getStartEnd(); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $historyService = new Application_Service_HistoryService(); $shows = $historyService->getShowList($startsDT, $endsDT); diff --git a/airtime_mvc/application/controllers/ShowbuilderController.php b/airtime_mvc/application/controllers/ShowbuilderController.php index 7df1bb7ad..9abbeb167 100644 --- a/airtime_mvc/application/controllers/ShowbuilderController.php +++ b/airtime_mvc/application/controllers/ShowbuilderController.php @@ -236,49 +236,6 @@ class ShowbuilderController extends Zend_Controller_Action $this->view->dialog = $this->view->render('showbuilder/builderDialog.phtml'); } - private function getStartEnd() - { - $request = $this->getRequest(); - - $userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone()); - $utcTimezone = new DateTimeZone("UTC"); - $utcNow = new DateTime("now", $utcTimezone); - - $start = $request->getParam("start"); - $end = $request->getParam("end"); - - if (empty($start) || empty($end)) { - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - else { - - try { - $startsDT = new DateTime($start, $userTimezone); - $startsDT->setTimezone($utcTimezone); - - $endsDT = new DateTime($end, $userTimezone); - $endsDT->setTimezone($utcTimezone); - - if ($startsDT > $endsDT) { - throw new Exception("start greater than end"); - } - } - catch (Exception $e) { - Logging::info($e); - Logging::info($e->getMessage()); - - $startsDT = clone $utcNow; - $startsDT->sub(new DateInterval("P1D")); - $endsDT = clone $utcNow; - } - - } - - return array($startsDT, $endsDT); - } - public function checkBuilderFeedAction() { $request = $this->getRequest(); @@ -287,7 +244,7 @@ class ShowbuilderController extends Zend_Controller_Action $timestamp = intval($request->getParam("timestamp", -1)); $instances = $request->getParam("instances", array()); - list($startsDT, $endsDT) = $this->getStartEnd(); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $opts = array("myShows" => $my_shows, "showFilter" => $show_filter); $showBuilder = new Application_Model_ShowBuilder($startsDT, $endsDT, $opts); @@ -307,7 +264,7 @@ class ShowbuilderController extends Zend_Controller_Action $show_instance_filter = intval($request->getParam("showInstanceFilter", 0)); $my_shows = intval($request->getParam("myShows", 0)); - list($startsDT, $endsDT) = $this->getStartEnd(); + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); $opts = array("myShows" => $my_shows, "showFilter" => $show_filter, diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 4d884a0f8..4aebc3391 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -803,13 +803,7 @@ SQL; $info['description'] = $ccShow->getDbDescription(); $info['color'] = $ccShow->getDbColor(); $info['background_color'] = $ccShow->getDbBackgroundColor(); - $info['custom_username'] = $ccShow->getDbLiveStreamUser(); - $info['cb_airtime_auth'] = $ccShow->getDbLiveStreamUsingAirtimeAuth(); - $info['cb_custom_auth'] = $ccShow->getDbLiveStreamUsingCustomAuth(); - $info['custom_username'] = $ccShow->getDbLiveStreamUser(); - $info['custom_password'] = $ccShow->getDbLiveStreamPass(); $info['linked'] = $ccShow->getDbLinked(); - $info['is_linkable'] = $ccShow->getDbIsLinkable(); return $info; } } @@ -891,6 +885,7 @@ SQL; * @param unknown_type $excludeInstance * @param boolean $onlyRecord * @param int $showId + * limits the results to instances of a given showId only * @return array */ public static function getShows($start_timestamp, $end_timestamp, $onlyRecord=FALSE, $showId=null) @@ -1488,11 +1483,7 @@ SQL; } public static function getDistinctShows() { - $sql = <<find(); return $shows; - } } diff --git a/airtime_mvc/application/services/HistoryService.php b/airtime_mvc/application/services/HistoryService.php index d6af0a40b..181e55a67 100644 --- a/airtime_mvc/application/services/HistoryService.php +++ b/airtime_mvc/application/services/HistoryService.php @@ -279,14 +279,14 @@ class Application_Service_HistoryService foreach ($fields as $index=>$field) { if ($field["type"] == TEMPLATE_BOOLEAN) { - $boolCast[] = $field["name"]; + $boolCast[] = $field; } } foreach ($rows as $index => &$result) { - foreach ($boolCast as $name) { - $result[$name] = (bool) $result[$name]; + foreach ($boolCast as $field) { + $result[$field['label']] = (bool) $result[$field['name']]; } //need to display the results in the station's timezone. From 174cf79d84869d01489437193129bcddc0ca4169 Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Thu, 20 Nov 2014 18:16:52 +0100 Subject: [PATCH 4/8] clean up shows endpoint, remove dependency on Show model --- .../application/controllers/ApiController.php | 7 +++-- airtime_mvc/application/models/Show.php | 26 ------------------- .../application/models/airtime/CcShow.php | 19 ++++++++++++++ 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 240e562c2..6f96a1bf2 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -1394,13 +1394,12 @@ class ApiController extends Zend_Controller_Action $results = array(); if (empty($showId)) { - $shows = Application_Model_Show::getDistinctShows(); - foreach($shows as $baseShow) { - $show = new Application_Model_Show($baseShow->getDbId()); + $shows = CcShowQuery::create()->find(); + foreach($shows as $show) { $results[] = $show->getShowInfo(); } } else { - $show = new Application_Model_Show($showId); + $show = CcShowQuery::create()->findPK($showId); $results[] = $show->getShowInfo(); } diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 4aebc3391..46997e998 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -786,28 +786,6 @@ SQL; } } - /** - * returns show specific info (not related to isntance) - */ - public function getShowInfo() - { - $info = array(); - if ($this->getId() == null) { - return $info; - } else { - $ccShow = CcShowQuery::create()->findPK($this->_showId); - $info['name'] = $ccShow->getDbName(); - $info['id'] = $ccShow->getDbId(); - $info['url'] = $ccShow->getDbUrl(); - $info['genre'] = $ccShow->getDbGenre(); - $info['description'] = $ccShow->getDbDescription(); - $info['color'] = $ccShow->getDbColor(); - $info['background_color'] = $ccShow->getDbBackgroundColor(); - $info['linked'] = $ccShow->getDbLinked(); - return $info; - } - } - /* Only used for shows that are repeating. Note that this will return * true even for dates that only have a "modified" show instance (does not * check if the "modified_instance" column is set to true). This is intended @@ -1482,8 +1460,4 @@ SQL; return array($start, $end); } - public static function getDistinctShows() { - $shows = CcShowQuery::create()->find(); - return $shows; - } } diff --git a/airtime_mvc/application/models/airtime/CcShow.php b/airtime_mvc/application/models/airtime/CcShow.php index b9f140e3e..ee0c75454 100644 --- a/airtime_mvc/application/models/airtime/CcShow.php +++ b/airtime_mvc/application/models/airtime/CcShow.php @@ -304,4 +304,23 @@ class CcShow extends BaseCcShow { ->filterByDbId($instanceId, Criteria::NOT_EQUAL) ->find(); } + + public function getShowInfo() + { + $info = array(); + if ($this->getDbId() == null) { + return $info; + } else { + $info['name'] = $this->getDbName(); + $info['id'] = $this->getDbId(); + $info['url'] = $this->getDbUrl(); + $info['genre'] = $this->getDbGenre(); + $info['description'] = $this->getDbDescription(); + $info['color'] = $this->getDbColor(); + $info['background_color'] = $this->getDbBackgroundColor(); + $info['linked'] = $this->getDbLinked(); + return $info; + } + + } } // CcShow From 1cc1f8a3783bf2d4ad66f5ef1adcbb0e34cdacce Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Mon, 24 Nov 2014 12:22:34 +0100 Subject: [PATCH 5/8] return error for missing instance_id on show-tracks action --- airtime_mvc/application/controllers/ApiController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 6f96a1bf2..ee24bec67 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -1449,7 +1449,9 @@ class ApiController extends Zend_Controller_Action $instanceId = $this->_getParam('instance_id'); if (!isset($instanceId)) { - return; + $this->_helper->json->sendJson( + array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing required instance_id parameter")) + ); } $showInstance = new Application_Model_ShowInstance($instanceId); From fc8e65944ef9ce1a9770be626dfaa9c8a5488071 Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Thu, 8 Jan 2015 19:52:06 +0100 Subject: [PATCH 6/8] added error checked for required params --- .../application/controllers/ApiController.php | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index ee24bec67..e6f9b89df 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -1424,9 +1424,22 @@ class ApiController extends Zend_Controller_Action $showId = $request->getParam("show_id", null); list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); + + if ((!isset($showId)) || (!is_int($showId))) { + $this->_helper->json->sendJson( + array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing invalid type for required show_id parameter. use type int")) + ); + } $shows = Application_Model_Show::getShows($startsDT, $endsDT, FALSE, $showId); + // is this a valid show? + if (empty($shows)) { + $this->_helper->json->sendJson( + array("jsonrpc" => "2.0", "error" => array("code" => 204, "message" => "no content for requested show_id")) + ); + } + $this->_helper->json->sendJson($shows); } catch (Exception $e) { @@ -1448,13 +1461,22 @@ class ApiController extends Zend_Controller_Action $instanceId = $this->_getParam('instance_id'); - if (!isset($instanceId)) { + if ((!isset($instanceId)) || (!is_int($instanceI))) { $this->_helper->json->sendJson( - array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing required instance_id parameter")) + array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing invalid type for required instance_id parameter. use type int")) ); } $showInstance = new Application_Model_ShowInstance($instanceId); + $showInstanceContent = $showInstance->getShowListContent($prefTimezone); + + // is this a valid show instance with content? + if (empty($showInstanceContent)) { + $this->_helper->json->sendJson( + array("jsonrpc" => "2.0", "error" => array("code" => 204, "message" => "no content for requested instance_id")) + ); + } + $result = array(); $position = 0; foreach ($showInstance->getShowListContent($prefTimezone) as $track) { From 1b03bd7fbd3f7b1e7fe9ec387ce47f5d528947ce Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Thu, 8 Jan 2015 19:56:14 +0100 Subject: [PATCH 7/8] use new var instaead of calling getShowListContent again --- airtime_mvc/application/controllers/ApiController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index e6f9b89df..7c928487e 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -1479,7 +1479,7 @@ class ApiController extends Zend_Controller_Action $result = array(); $position = 0; - foreach ($showInstance->getShowListContent($prefTimezone) as $track) { + foreach ($showInstanceContent as $track) { $elementMap = array( 'title' => isset($track['track_title']) ? $track['track_title'] : "", From 17a577fbbb51dad437a9939395e530ebd1fd990a Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Thu, 8 Jan 2015 20:30:15 +0100 Subject: [PATCH 8/8] use is_numeric instead of is_int as values are strings --- airtime_mvc/application/controllers/ApiController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 7c928487e..2ee20b3e7 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -1425,9 +1425,10 @@ class ApiController extends Zend_Controller_Action list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($request); - if ((!isset($showId)) || (!is_int($showId))) { + if ((!isset($showId)) || (!is_numeric($showId))) { + //if (!isset($showId)) { $this->_helper->json->sendJson( - array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing invalid type for required show_id parameter. use type int")) + array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing invalid type for required show_id parameter. use type int.".$showId)) ); } @@ -1461,7 +1462,7 @@ class ApiController extends Zend_Controller_Action $instanceId = $this->_getParam('instance_id'); - if ((!isset($instanceId)) || (!is_int($instanceI))) { + if ((!isset($instanceId)) || (!is_numeric($instanceId))) { $this->_helper->json->sendJson( array("jsonrpc" => "2.0", "error" => array("code" => 400, "message" => "missing invalid type for required instance_id parameter. use type int")) );