cleanup new HTTP apiendpoints, added HTTPHelper and getStartEndFromRequest and updated all controllers that called their own versions of this function

This commit is contained in:
Mark Lewis 2014-11-20 17:22:53 +01:00 committed by Albert Santoni
parent 03dae5be2b
commit a27ca2f583
9 changed files with 106 additions and 212 deletions

View file

@ -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);
}
}

View file

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

View file

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

View file

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