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

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

View File

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

View File

@ -0,0 +1,20 @@
<?php
class Application_Common_HTTPHelper
{
/**
* Returns start and end DateTime vars from given
* HTTP Request object
*
* @param Request
* @return array(start DateTime, end DateTime)
*/
public static function getStartEndFromRequest($request)
{
return Application_Common_DateHelper::getStartEnd(
$request->getParam("start", null),
$request->getParam("end", null),
$request->getParam("timezone", null)
);
}
}

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,

View File

@ -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 = <<<SQL
SELECT * FROM cc_show
SQL;
$shows = Application_Common_Database::prepareAndExecute($sql);
$shows = CcShowQuery::create()->find();
return $shows;
}
}

View File

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