diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 31bd75d44..1f8170a5a 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -351,13 +351,15 @@ class Application_Model_Show { public function isRepeating() { $showDaysRow = CcShowDaysQuery::create() - ->filterByDbShowId($this->_showId) - ->findOne(); + ->filterByDbShowId($this->_showId) + ->findOne(); if (!is_null($showDaysRow)){ return ($showDaysRow->getDbRepeatType() != -1); - } else + } + else { return false; + } } /** @@ -1164,7 +1166,9 @@ class Application_Model_Show { Logging::log('$start time of non repeating record '.$start); - self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $start, $duration, $timezone); + if ($newInstance){ + self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $start, $duration, $timezone); + } } } @@ -1455,18 +1459,18 @@ class Application_Model_Show { $endTimeString = $p_endTimestamp->format("Y-m-d H:i:s"); if (!is_null($p_startTimestamp)) { $startTimeString = $p_startTimestamp->format("Y-m-d H:i:s"); - $sql = "SELECT * FROM cc_show_days - WHERE last_show IS NULL - OR first_show < '{$endTimeString}' AND last_show > '{$startTimeString}'"; } else { $today_timestamp = new DateTime("now", new DateTimeZone("UTC")); - $today_timestamp_string = $today_timestamp->format("Y-m-d H:i:s"); - $sql = "SELECT * FROM cc_show_days - WHERE last_show IS NULL - OR first_show < '{$endTimeString}' AND last_show > '{$today_timestamp_string}'"; + $startTimeString = $today_timestamp->format("Y-m-d H:i:s"); } + $sql = "SELECT * FROM cc_show_days + WHERE last_show IS NULL + OR first_show < '{$endTimeString}' AND last_show > '{$startTimeString}'"; + + Logging::log($sql); + $res = $CC_DBC->GetAll($sql); foreach ($res as $row) { diff --git a/airtime_mvc/application/models/ShowInstance.php b/airtime_mvc/application/models/ShowInstance.php index de62f7c83..b5dbfb55b 100644 --- a/airtime_mvc/application/models/ShowInstance.php +++ b/airtime_mvc/application/models/ShowInstance.php @@ -444,6 +444,81 @@ class Application_Model_ShowInstance { $this->updateScheduledTime(); } + private function checkToDeleteShow($showId) + { + //UTC DateTime object + $showsPopUntil = Application_Model_Preference::GetShowsPopulatedUntil(); + + $showDays = CcShowDaysQuery::create() + ->filterByDbShowId($showId) + ->findOne(); + + $showEnd = $showDays->getDbLastShow(); + + //there will always be more shows populated. + if (is_null($showEnd)) { + return false; + } + + $lastShowStartDateTime = new DateTime("{$showEnd} {$showDays->getDbStartTime()}", new DateTimeZone($showDays->getDbTimezone())); + //end dates were non inclusive. + $lastShowStartDateTime = self::addDeltas($lastShowStartDateTime, -1, 0); + + //there's still some shows left to be populated. + if ($lastShowStartDateTime->getTimestamp() > $showsPopUntil->getTimestamp()) { + return false; + } + + // check if there are any non deleted show instances remaining. + $showInstances = CcShowInstancesQuery::create() + ->filterByDbShowId($showId) + ->filterByDbModifiedInstance(false) + ->filterByDbRebroadcast(0) + ->find(); + + if (is_null($showInstances)){ + return true; + } + //only 1 show instance left of the show, make it non repeating. + else if (count($showInstances) === 1) { + $showInstance = $showInstances[0]; + + $showDaysOld = CcShowDaysQuery::create() + ->filterByDbShowId($showId) + ->find(); + + $tz = $showDaysOld[0]->getDbTimezone(); + + $startDate = new DateTime($showInstance->getDbStarts(), new DateTimeZone("UTC")); + $startDate->setTimeZone(new DateTimeZone($tz)); + $endDate = self::addDeltas($startDate, 1, 0); + + //make a new rule for a non repeating show. + $showDayNew = new CcShowDays(); + $showDayNew->setDbFirstShow($startDate->format("Y-m-d")); + $showDayNew->setDbLastShow($endDate->format("Y-m-d")); + $showDayNew->setDbStartTime($startDate->format("H:i:s")); + $showDayNew->setDbTimezone($tz); + $showDayNew->setDbDay($startDate->format('w')); + $showDayNew->setDbDuration($showDaysOld[0]->getDbDuration()); + $showDayNew->setDbRepeatType(-1); + $showDayNew->setDbShowId($showDaysOld[0]->getDbShowId()); + $showDayNew->setDbRecord($showDaysOld[0]->getDbRecord()); + $showDayNew->save(); + + //delete the old rules for repeating shows + $showDaysOld->delete(); + + //remove the old repeating deleted instances. + $showInstances = CcShowInstancesQuery::create() + ->filterByDbShowId($showId) + ->filterByDbModifiedInstance(true) + ->delete(); + } + + return false; + } + public function delete() { global $CC_DBC; @@ -465,6 +540,10 @@ class Application_Model_ShowInstance { ->setDbModifiedInstance(true) ->save(); + if ($this->isRebroadcast()) { + return; + } + //delete the rebroadcasts of the removed recorded show. if ($recording) { CcShowInstancesQuery::create() @@ -477,17 +556,8 @@ class Application_Model_ShowInstance { ->filterByDbInstanceId($this->_instanceId) ->delete(); - // check if we can safely delete the show - $showInstancesRow = CcShowInstancesQuery::create() - ->filterByDbShowId($showId) - ->filterByDbModifiedInstance(false) - ->findOne(); - /* If we didn't find any instances of the show that haven't - * been deleted, then just erase everything related to that show. - * We can just delete, the show and the foreign key-constraint should - * take care of deleting all of its instances. */ - if(is_null($showInstancesRow)){ + if ($this->checkToDeleteShow($showId)){ CcShowQuery::create() ->filterByDbId($showId) ->delete();