From bf146e7f85ecedc032c1cac7bdafa4dd84b6d5e9 Mon Sep 17 00:00:00 2001 From: martin Date: Thu, 14 Apr 2011 14:17:56 -0400 Subject: [PATCH 1/2] -commented some functions -removed usage of "current_timestamp" in postgresql queries in favour of php time. --- application/Bootstrap.php | 1 + application/controllers/ApiController.php | 4 +- application/models/DateHelper.php | 4 +- application/models/Nowplaying.php | 16 +-- application/models/Schedule.php | 4 +- application/models/Shows.php | 129 +++++++++++++++++----- 6 files changed, 117 insertions(+), 41 deletions(-) diff --git a/application/Bootstrap.php b/application/Bootstrap.php index 277bd9cab..511ab92e3 100644 --- a/application/Bootstrap.php +++ b/application/Bootstrap.php @@ -21,6 +21,7 @@ require_once 'Schedule.php'; require_once 'Shows.php'; require_once 'Users.php'; require_once 'RabbitMq.php'; +require_once 'DateHelper.php'; require_once __DIR__.'/controllers/plugins/RabbitMqPlugin.php'; global $CC_CONFIG, $CC_DBC; diff --git a/application/controllers/ApiController.php b/application/controllers/ApiController.php index 1b0e8ac51..4c6e344d2 100644 --- a/application/controllers/ApiController.php +++ b/application/controllers/ApiController.php @@ -112,8 +112,8 @@ class ApiController extends Zend_Controller_Action $result = Schedule::GetPlayOrderRange(0, 1); - $date = new Application_Model_DateHelper; - $timeNow = $date->getDate(); + $date = new DateHelper; + $timeNow = $date->getTimestamp(); $result = array("env"=>APPLICATION_ENV, "schedulerTime"=>gmdate("Y-m-d H:i:s"), "currentShow"=>Show_DAL::GetCurrentShow($timeNow), diff --git a/application/models/DateHelper.php b/application/models/DateHelper.php index d0f56f647..97b7d9ae9 100644 --- a/application/models/DateHelper.php +++ b/application/models/DateHelper.php @@ -1,6 +1,6 @@ _timestamp); } diff --git a/application/models/Nowplaying.php b/application/models/Nowplaying.php index 7536f1044..6df81c310 100644 --- a/application/models/Nowplaying.php +++ b/application/models/Nowplaying.php @@ -57,8 +57,8 @@ class Application_Model_Nowplaying } public static function FilterRowsByDate($rows, $date, $startCutoff, $endCutoff){ - $dateNow = new Application_Model_DateHelper; - $timeNow = $dateNow->getDate(); + $dateNow = new DateHelper; + $timeNow = $dateNow->getTimestamp(); $data = array(); //iterate over each show, and calculate information for it. @@ -117,16 +117,16 @@ class Application_Model_Nowplaying public static function GetDataGridData($viewType, $dateString){ if ($viewType == "now"){ - $date = new Application_Model_DateHelper; - $timeNow = $date->getDate(); + $date = new DateHelper; + $timeNow = $date->getTimestamp(); $startCutoff = 60; $endCutoff = 86400; //60*60*24 - seconds in a day } else { - $date = new Application_Model_DateHelper; + $date = new DateHelper; $time = $date->getTime(); $date->setDate($dateString." ".$time); - $timeNow = $date->getDate(); + $timeNow = $date->getTimestamp(); $startCutoff = $date->getNowDayStartDiff(); $endCutoff = $date->getNowDayEndDiff(); @@ -139,8 +139,8 @@ class Application_Model_Nowplaying //$rows = FindGapsBetweenShows() $data = Application_Model_Nowplaying::FilterRowsByDate($rows, $date, $startCutoff, $endCutoff); - $date = new Application_Model_DateHelper; - $timeNow = $date->getDate(); + $date = new DateHelper; + $timeNow = $date->getTimestamp(); return array("currentShow"=>Show_DAL::GetCurrentShow($timeNow), "rows"=>$data); } } diff --git a/application/models/Schedule.php b/application/models/Schedule.php index 8be208256..4f2b4cd8d 100644 --- a/application/models/Schedule.php +++ b/application/models/Schedule.php @@ -361,8 +361,8 @@ class Schedule { global $CC_CONFIG; - $date = new Application_Model_DateHelper; - $timeNow = $date->getDate(); + $date = new DateHelper; + $timeNow = $date->getTimestamp(); return array("env"=>APPLICATION_ENV, "schedulerTime"=>gmdate("Y-m-d H:i:s"), "previous"=>Schedule::GetScheduledItemData($timeNow, -1, $prev, "24 hours"), diff --git a/application/models/Shows.php b/application/models/Shows.php index 6be170dd9..a817e2738 100644 --- a/application/models/Shows.php +++ b/application/models/Shows.php @@ -136,9 +136,13 @@ class Show { $uncheckedDaysImploded = implode(",", $p_uncheckedDays); $showId = $this->getId(); + + $date = new DateHelper; + $timestamp = $date->getTimestamp(); + $sql = "DELETE FROM cc_show_instances" ." WHERE EXTRACT(DOW FROM starts) IN ($uncheckedDaysImploded)" - ." AND starts > current_timestamp " + ." AND starts > TIMESTAMP '$timestamp'" ." AND show_id = $showId"; $CC_DBC->query($sql); @@ -177,6 +181,14 @@ class Show { return !is_null($showInstancesRow); } + /** + * Get start time and absolute start date for a recorded + * shows rebroadcasts. For example start date format would be + * YYYY-MM-DD and time would HH:MM + * + * @return array + * array of associate arrays containing "start_date" and "start_time" + */ public function getRebroadcastsAbsolute() { global $CC_DBC; @@ -199,6 +211,14 @@ class Show { return $CC_DBC->GetAll($sql); } + /** + * Get start time and relative start date for a recorded + * shows rebroadcasts. For example start date format would be + * "x days" and time would HH:MM:SS + * + * @return array + * array of associate arrays containing "day_offset" and "start_time" + */ public function getRebroadcastsRelative() { global $CC_DBC; @@ -272,42 +292,76 @@ class Show { return $endDate; } } - + + /** + * Deletes all future instances of the current show object + * from the show_instances table. + * + */ public function deleteAllInstances(){ global $CC_DBC; + $date = new DateHelper; + $timestamp = $date->getTimestamp(); + $showId = $this->getId(); - $sql = "DELETE FROM cc_show_instances " - ."WHERE starts > current_timestamp " - ."AND show_id = $showId"; + $sql = "DELETE FROM cc_show_instances" + ." WHERE starts > TIMESTAMP '$timestamp'" + ." AND show_id = $showId"; $CC_DBC->query($sql); } - + + /** + * Deletes all show instances of current show after a + * certain date. + * + * @param string $p_date + * The date which to delete after + */ public function removeAllInstancesAfterDate($p_date){ global $CC_DBC; - $showId = $this->getId(); - $sql = "DELETE FROM cc_show_instances " - ."WHERE date(starts) > DATE '$p_date' " - ."AND starts > current_timestamp " - ."AND show_id = $showId"; - - $CC_DBC->query($sql); - } - - public function removeAllInstancesBeforeDate($p_date){ - global $CC_DBC; + $date = new DateHelper; + $timestamp = $date->getTimestamp(); $showId = $this->getId(); $sql = "DELETE FROM cc_show_instances " - ."WHERE date(starts) < DATE '$p_date' " - ."AND starts > current_timestamp " - ."AND show_id = $showId"; + ." WHERE date(starts) > DATE '$p_date'" + ." AND starts > TIMESTAMP '$timestamp'" + ." AND show_id = $showId"; + + $CC_DBC->query($sql); + } + + /** + * Deletes all show instances of current show before a + * certain date. + * + * @param string $p_date + * The date which to delete before + */ + public function removeAllInstancesBeforeDate($p_date){ + global $CC_DBC; + + $date = new DateHelper; + $timestamp = $date->getTimestamp(); + + $showId = $this->getId(); + $sql = "DELETE FROM cc_show_instances " + ." WHERE date(starts) < DATE '$p_date'" + ." AND starts > TIMESTAMP '$timestamp'" + ." AND show_id = $showId"; $CC_DBC->query($sql); } - + + /** + * Get the start date of the current show. + * + * @return string + * The start date in the format YYYY-MM-DD + */ public function getStartDate(){ global $CC_DBC; @@ -323,7 +377,13 @@ class Show { return $firstDate; } } - + + /** + * Get the start time of the current show. + * + * @return string + * The start time in the format HH:MM:SS + */ public function getStartTime(){ global $CC_DBC; @@ -339,14 +399,23 @@ class Show { return $startTime; } } - + + /** + * Get the ID's of future instance of the current show. + * + * @return array + * A simple array containing all future instance ID's + */ public function getAllFutureInstanceIds(){ global $CC_DBC; + + $date = new DateHelper; + $timestamp = $date->getTimestamp(); $showId = $this->getId(); - $sql = "SELECT id from cc_show_instances " + $sql = "SELECT id from cc_show_instances" ." WHERE show_id = $showId" - ." AND starts > current_timestamp"; + ." AND starts > TIMESTAMP '$timestamp'"; $rows = $CC_DBC->GetAll($sql); @@ -361,6 +430,9 @@ class Show { //need to update cc_show_instances, cc_show_days global $CC_DBC; + + $date = new DateHelper; + $timestamp = $date->getTimestamp(); $sql = "UPDATE cc_show_days " ."SET duration = '$p_data[add_show_duration]' " @@ -370,7 +442,7 @@ class Show { $sql = "UPDATE cc_show_instances " ."SET ends = starts + INTERVAL '$p_data[add_show_duration]' " ."WHERE show_id = $p_data[add_show_id] " - ."AND starts > current_timestamp"; + ."AND starts > TIMESTAMP '$timestamp'"; $CC_DBC->query($sql); } @@ -379,6 +451,9 @@ class Show { //need to update cc_schedule, cc_show_instances, cc_show_days global $CC_DBC; + + $date = new DateHelper; + $timestamp = $date->getTimestamp(); $sql = "UPDATE cc_show_days " ."SET start_time = TIME '$p_data[add_show_start_time]', " @@ -399,7 +474,7 @@ class Show { ."SET starts = starts + INTERVAL '$diff sec', " ."ends = ends + INTERVAL '$diff sec' " ."WHERE show_id = $p_data[add_show_id] " - ."AND starts > current_timestamp"; + ."AND starts > TIMESTAMP '$timestamp'"; $CC_DBC->query($sql); $showInstanceIds = $this->getAllFutureInstanceIds(); From 88362a2c5217d1c51cb7629d4713de8e508a51cc Mon Sep 17 00:00:00 2001 From: martin Date: Thu, 14 Apr 2011 14:57:52 -0400 Subject: [PATCH 2/2] CC-2179: Creating a record + rebroadcast show with no rebroadcast times causes DB error -fixed --- .../forms/AddShowAbsoluteRebroadcastDates.php | 18 ++++++++++++------ application/forms/AddShowRebroadcastDates.php | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/application/forms/AddShowAbsoluteRebroadcastDates.php b/application/forms/AddShowAbsoluteRebroadcastDates.php index ab6f9891b..bb3c79125 100644 --- a/application/forms/AddShowAbsoluteRebroadcastDates.php +++ b/application/forms/AddShowAbsoluteRebroadcastDates.php @@ -35,12 +35,18 @@ class Application_Form_AddShowAbsoluteRebroadcastDates extends Zend_Form_SubForm $valid = true; for($i=1; $i<=10; $i++) { - + $day = $formData['add_show_rebroadcast_date_absolute_'.$i]; - if($day == "") { + if(trim($day) == "") { continue; } + + $time = $formData['add_show_rebroadcast_time_absolute_'.$i]; + if (trim($time) == ""){ + $this->getElement('add_show_rebroadcast_time_absolute_'.$i)->setErrors(array("Time must be specified")); + $valid = false; + } $show_start_time = $formData['add_show_start_date']."".$formData['add_show_start_time']; $show_end = new DateTime($show_start_time); @@ -51,16 +57,16 @@ class Application_Form_AddShowAbsoluteRebroadcastDates extends Zend_Form_SubForm $show_end->add(new DateInterval("PT$duration[0]H")); $show_end->add(new DateInterval("PT$duration[1]M")); $show_end->add(new DateInterval("PT1H"));//min time to wait until a rebroadcast - + $rebroad_start = $day."".$formData['add_show_rebroadcast_time_absolute_'.$i]; $rebroad_start = new DateTime($rebroad_start); - + if($rebroad_start < $show_end) { $this->getElement('add_show_rebroadcast_time_absolute_'.$i)->setErrors(array("Must wait at least 1 hour to rebroadcast")); $valid = false; } - } - + } + return $valid; } } diff --git a/application/forms/AddShowRebroadcastDates.php b/application/forms/AddShowRebroadcastDates.php index 3a8cba562..e51bcf570 100644 --- a/application/forms/AddShowRebroadcastDates.php +++ b/application/forms/AddShowRebroadcastDates.php @@ -12,7 +12,7 @@ class Application_Form_AddShowRebroadcastDates extends Zend_Form_SubForm $relativeDates = array(); $relativeDates[""] = ""; for($i=0; $i<=30; $i++) { - $relativeDates["$i days"] = "+$i days"; + $relativeDates["$i days"] = "+$i days"; } for($i=1; $i<=10; $i++) { @@ -40,12 +40,18 @@ class Application_Form_AddShowRebroadcastDates extends Zend_Form_SubForm $valid = true; for($i=1; $i<=5; $i++) { - + $days = $formData['add_show_rebroadcast_date_'.$i]; - if($days == "") { + if(trim($days) == "") { continue; } + + $time = $formData['add_show_rebroadcast_time_'.$i]; + if (trim($time) == ""){ + $this->getElement('add_show_rebroadcast_time_'.$i)->setErrors(array("Time must be specified")); + $valid = false; + } $days = explode(" ", $days); $day = $days[0]; @@ -59,7 +65,7 @@ class Application_Form_AddShowRebroadcastDates extends Zend_Form_SubForm $show_end->add(new DateInterval("PT$duration[0]H")); $show_end->add(new DateInterval("PT$duration[1]M")); $show_end->add(new DateInterval("PT1H"));//min time to wait until a rebroadcast - + $rebroad_start = $formData['add_show_start_date']."".$formData['add_show_rebroadcast_time_'.$i]; $rebroad_start = new DateTime($rebroad_start); $rebroad_start->add(new DateInterval("P".$day."D")); @@ -68,8 +74,8 @@ class Application_Form_AddShowRebroadcastDates extends Zend_Form_SubForm $this->getElement('add_show_rebroadcast_time_'.$i)->setErrors(array("Must wait at least 1 hour to rebroadcast")); $valid = false; } - } - + } + return $valid; } }