Merge branch '2.5.x' of github.com:sourcefabric/Airtime into 2.5.x

This commit is contained in:
Naomi 2013-12-11 18:56:29 -05:00
commit 37ea29380b
2 changed files with 36 additions and 18 deletions

View File

@ -78,11 +78,11 @@ SQL;
$utcNow = clone $displayNow; $utcNow = clone $displayNow;
$utcNow->setTimezone(new DateTimeZone("UTC")); $utcNow->setTimezone(new DateTimeZone("UTC"));
$shows = Application_Model_Show::getPrevCurrentNext($utcNow->format("Y-m-d H:i:s")); $shows = Application_Model_Show::getPrevCurrentNext($utcNow);
$previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null; $previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null;
$currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null; $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null;
$nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null; $nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null;
$results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow->format("Y-m-d H:i:s")); $results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow);
$range = array("env"=>APPLICATION_ENV, $range = array("env"=>APPLICATION_ENV,
"schedulerTime"=> $displayNow->format("Y-m-d H:i:s"), "schedulerTime"=> $displayNow->format("Y-m-d H:i:s"),
@ -108,8 +108,12 @@ SQL;
* through this mechanism a call is made to the old way of querying * through this mechanism a call is made to the old way of querying
* the database to find the track info. * the database to find the track info.
**/ **/
public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $p_timeNow) public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $utcNow)
{ {
$timeZone = new DateTimeZone("UTC"); //This function works entirely in UTC.
assert(get_class($utcNow) === "DateTime");
assert($utcNow->getTimeZone() == $timeZone);
if ($p_previousShowID == null && $p_currentShowID == null && $p_nextShowID == null) { if ($p_previousShowID == null && $p_currentShowID == null && $p_nextShowID == null) {
return; return;
} }
@ -167,14 +171,17 @@ SQL;
$results['current'] = null; $results['current'] = null;
$results['next'] = null; $results['next'] = null;
$timeNowAsMillis = strtotime($p_timeNow);
for ($i = 0; $i < $numberOfRows; ++$i) { for ($i = 0; $i < $numberOfRows; ++$i) {
// if the show is overbooked, then update the track end time to the end of the show time. // if the show is overbooked, then update the track end time to the end of the show time.
if ($rows[$i]['ends'] > $rows[$i]["show_ends"]) { if ($rows[$i]['ends'] > $rows[$i]["show_ends"]) {
$rows[$i]['ends'] = $rows[$i]["show_ends"]; $rows[$i]['ends'] = $rows[$i]["show_ends"];
} }
if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) && (strtotime($rows[$i]['ends']) >= $timeNowAsMillis)) { $curShowStartTime = new DateTime($rows[$i]['starts'], $timeZone);
$curShowEndTime = new DateTime($rows[$i]['ends'], $timeZone);
if (($curShowStartTime <= $utcNow) && ($curShowEndTime >= $utcNow)) {
if ($i - 1 >= 0) { if ($i - 1 >= 0) {
$results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"], $results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"],
"starts"=>$rows[$i-1]["starts"], "starts"=>$rows[$i-1]["starts"],
@ -195,10 +202,10 @@ SQL;
} }
break; break;
} }
if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { if ($curShowEndTime < $utcNow ) {
$previousIndex = $i; $previousIndex = $i;
} }
if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { if ($curShowStartTime > $utcNow) {
$results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], $results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"],
"starts"=>$rows[$i]["starts"], "starts"=>$rows[$i]["starts"],
"ends"=>$rows[$i]["ends"], "ends"=>$rows[$i]["ends"],

View File

@ -1116,9 +1116,17 @@ SQL;
/** /**
* Gets the current show, previous and next with an 2day window from * Gets the current show, previous and next with an 2day window from
* the given timeNow, so timeNow-2days and timeNow+2days. * the given timeNow, so timeNow-2days and timeNow+2days.
*
* @param $utcNow A DateTime object containing the current time in UTC.
* @return An array (with stupid sub-arrays) containing the previous show id,
* current show id, and next show id.
*/ */
public static function getPrevCurrentNext($p_timeNow) public static function getPrevCurrentNext($utcNow)
{ {
$timeZone = new DateTimeZone("UTC"); //This function works entirely in UTC.
assert(get_class($utcNow) === "DateTime");
assert($utcNow->getTimeZone() == $timeZone);
$CC_CONFIG = Config::getConfig(); $CC_CONFIG = Config::getConfig();
$con = Propel::getConnection(); $con = Propel::getConnection();
// //
@ -1144,9 +1152,10 @@ ORDER BY si.starts
SQL; SQL;
$stmt = $con->prepare($sql); $stmt = $con->prepare($sql);
$stmt->bindValue(':timeNow1', $p_timeNow); $utcNowStr = $utcNow->format("Y-m-d H:i:s");
$stmt->bindValue(':timeNow2', $p_timeNow); $stmt->bindValue(':timeNow1', $utcNowStr);
$stmt->bindValue(':timeNow2', $utcNowStr);
if ($stmt->execute()) { if ($stmt->execute()) {
$rows = $stmt->fetchAll(); $rows = $stmt->fetchAll();
@ -1161,12 +1170,14 @@ SQL;
$results['currentShow'] = array(); $results['currentShow'] = array();
$results['nextShow'] = array(); $results['nextShow'] = array();
$timeNowAsMillis = strtotime($p_timeNow);
for ($i = 0; $i < $numberOfRows; ++$i) { for ($i = 0; $i < $numberOfRows; ++$i) {
//All shows start/end times are stored in the database as UTC.
$showStartTime = new DateTime($rows[$i]['starts'], $timeZone);
$showEndTime = new DateTime($rows[$i]['ends'], $timeZone);
//Find the show that is within the current time. //Find the show that is within the current time.
if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) if (($showStartTime <= $utcNow) && ($showEndTime > $utcNow))
&& (strtotime($rows[$i]['ends']) > $timeNowAsMillis)) { {
if ($i-1 >= 0) { if ($i-1 >= 0) {
$results['previousShow'][0] = array( $results['previousShow'][0] = array(
"id" => $rows[$i-1]['id'], "id" => $rows[$i-1]['id'],
@ -1199,11 +1210,11 @@ SQL;
break; break;
} }
//Previous is any row that ends after time now capture it in case we need it later. //Previous is any row that ends after time now capture it in case we need it later.
if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { if ($showEndTime < $utcNow ) {
$previousShowIndex = $i; $previousShowIndex = $i;
} }
//if we hit this we know we've gone to far and can stop looping. //if we hit this we know we've gone to far and can stop looping.
if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { if ($showStartTime > $utcNow) {
$results['nextShow'][0] = array( $results['nextShow'][0] = array(
"id" => $rows[$i]['id'], "id" => $rows[$i]['id'],
"instance_id" => $rows[$i]['instance_id'], "instance_id" => $rows[$i]['instance_id'],