From cbf026412c25c1a64ae4a39687404eaf9c891043 Mon Sep 17 00:00:00 2001 From: denise Date: Wed, 4 Jul 2012 11:34:18 -0400 Subject: [PATCH] CC-4056: Add Show -> Prevent shows from overlapping -done --- airtime_mvc/application/forms/AddShowWhen.php | 14 ++++++ airtime_mvc/application/models/Schedule.php | 43 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/airtime_mvc/application/forms/AddShowWhen.php b/airtime_mvc/application/forms/AddShowWhen.php index ba0a4df26..0bc94554f 100644 --- a/airtime_mvc/application/forms/AddShowWhen.php +++ b/airtime_mvc/application/forms/AddShowWhen.php @@ -138,6 +138,20 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm $valid = false; } + /* Check if show is overlapping + * We will only do this check if the show is valid + * upto this point + */ + if ($valid) { + $show_start = new DateTime($start_time); + $show_end = new DateTime($end_time); + $overlapping = Application_Model_Schedule::checkOverlappingShows($show_start, $show_end); + if ($overlapping) { + $this->getElement('add_show_duration')->setErrors(array('Cannot have overlapping shows')); + $valid = false; + } + } + return $valid; } diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 5c80013ef..5c85e10b6 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -912,4 +912,47 @@ class Application_Model_Schedule { return false; } } + + public static function checkOverlappingShows($show_start, $show_end) { + global $CC_CONFIG; + + $overlapping = false; + //Set timezone to UTC to be consisent with DB + $show_start->setTimezone(new DateTimeZone('UTC')); + + $con = Propel::getConnection(); + $sql = "SELECT id, starts, ends FROM ".$CC_CONFIG["showInstances"]." + where starts <= '{$show_start->format('Y-m-d H:i:s')}' order by ends"; + $rows = $con->query($sql); + + //Set back to local timezone to do comparison + $show_start->setTimezone(new DateTimeZone(Application_Model_Preference::GetTimezone())); + + foreach($rows as $row) { + $start = new DateTime($row["starts"]); + $end = new DateTime($row["ends"]); + + /* When data is grabbed from the DB, the time is in UTC + * but the timezone is set locally. Comparing timestamps + * on two DateTime objects with different timezones but + * same time yeild different results. + * + * Since we cannot change the timezone back to UTC without + * changing the time, we convert each show's instance + * end time to local time via offset and then do the comparison. + */ + $offset = $show_start->getOffset(); + if ($offset < 0) { + $end->sub(new DateInterval("PT".abs($offset)."S")); + } else { + $end->add(new DateInterval("PT".$offset."S")); + } + + if ($show_start->getTimestamp() < $end->getTimestamp()) { + $overlapping = true; + break; + } + } + return $overlapping; + } }