From a27ca2f58326be222606d96a611b713bd29f96d0 Mon Sep 17 00:00:00 2001 From: Mark Lewis Date: Thu, 20 Nov 2014 17:22:53 +0100 Subject: [PATCH] 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.