CC-3259 : change repeating state of show.

This commit is contained in:
Naomi Aro 2012-01-18 16:21:46 +01:00
parent 63b2fa94de
commit fa872894f3
2 changed files with 95 additions and 21 deletions

View File

@ -356,9 +356,11 @@ class Application_Model_Show {
if (!is_null($showDaysRow)){ if (!is_null($showDaysRow)){
return ($showDaysRow->getDbRepeatType() != -1); return ($showDaysRow->getDbRepeatType() != -1);
} else }
else {
return false; return false;
} }
}
/** /**
* Get the repeat type of the show. Show can have repeat * Get the repeat type of the show. Show can have repeat
@ -1164,9 +1166,11 @@ class Application_Model_Show {
Logging::log('$start time of non repeating record '.$start); Logging::log('$start time of non repeating record '.$start);
if ($newInstance){
self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $start, $duration, $timezone); self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $start, $duration, $timezone);
} }
} }
}
/** /**
* Creates a 1 or more than 1 show instances (user has stated this show repeats). If the show * Creates a 1 or more than 1 show instances (user has stated this show repeats). If the show
@ -1455,17 +1459,17 @@ class Application_Model_Show {
$endTimeString = $p_endTimestamp->format("Y-m-d H:i:s"); $endTimeString = $p_endTimestamp->format("Y-m-d H:i:s");
if (!is_null($p_startTimestamp)) { if (!is_null($p_startTimestamp)) {
$startTimeString = $p_startTimestamp->format("Y-m-d H:i:s"); $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 { else {
$today_timestamp = new DateTime("now", new DateTimeZone("UTC")); $today_timestamp = new DateTime("now", new DateTimeZone("UTC"));
$today_timestamp_string = $today_timestamp->format("Y-m-d H:i:s"); $startTimeString = $today_timestamp->format("Y-m-d H:i:s");
}
$sql = "SELECT * FROM cc_show_days $sql = "SELECT * FROM cc_show_days
WHERE last_show IS NULL WHERE last_show IS NULL
OR first_show < '{$endTimeString}' AND last_show > '{$today_timestamp_string}'"; OR first_show < '{$endTimeString}' AND last_show > '{$startTimeString}'";
}
Logging::log($sql);
$res = $CC_DBC->GetAll($sql); $res = $CC_DBC->GetAll($sql);

View File

@ -444,6 +444,81 @@ class Application_Model_ShowInstance {
$this->updateScheduledTime(); $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() public function delete()
{ {
global $CC_DBC; global $CC_DBC;
@ -465,6 +540,10 @@ class Application_Model_ShowInstance {
->setDbModifiedInstance(true) ->setDbModifiedInstance(true)
->save(); ->save();
if ($this->isRebroadcast()) {
return;
}
//delete the rebroadcasts of the removed recorded show. //delete the rebroadcasts of the removed recorded show.
if ($recording) { if ($recording) {
CcShowInstancesQuery::create() CcShowInstancesQuery::create()
@ -477,17 +556,8 @@ class Application_Model_ShowInstance {
->filterByDbInstanceId($this->_instanceId) ->filterByDbInstanceId($this->_instanceId)
->delete(); ->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 if ($this->checkToDeleteShow($showId)){
* 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)){
CcShowQuery::create() CcShowQuery::create()
->filterByDbId($showId) ->filterByDbId($showId)
->delete(); ->delete();