CC-3094 : Rebroadcast editing problems

This commit is contained in:
Naomi Aro 2011-12-02 16:24:11 +01:00
parent 198340ac67
commit 966dd49610
2 changed files with 76 additions and 35 deletions

View File

@ -287,7 +287,7 @@ class Application_Model_Show {
$showId = $this->getId();
$sql = "SELECT starts FROM cc_show_instances "
."WHERE show_id = $showId AND rebroadcast = 1"
."WHERE show_id = $showId AND rebroadcast = 1 "
."ORDER BY starts";
$rebroadcasts = $CC_DBC->GetAll($sql);

View File

@ -175,63 +175,104 @@ class Application_Model_ShowInstance {
Application_Model_RabbitMq::PushSchedule();
}
/*
* @param $dateTime
* php Datetime object to add deltas to
*
* @param $deltaDay
* php int, delta days show moved
*
* @param $deltaMin
* php int, delta mins show moved
*
* @return $newDateTime
* php DateTime, $dateTime with the added time deltas.
*/
private static function addDeltas($dateTime, $deltaDay, $deltaMin) {
$newDateTime = clone $dateTime;
Logging::log("deltaDay: {$deltaDay}");
Logging::log("deltaMin: {$deltaMin}");
Logging::log("addDeltas: original time {$newDateTime->format('Y-m-d H:i:s')}");
$days = abs($deltaDay);
$mins = abs($deltaMin);
$dayInterval = new DateInterval("P{$days}D");
$minInterval = new DateInterval("PT{$mins}M");
if ($deltaDay > 0) {
$newDateTime->add($dayInterval);
}
else if ($deltaDay < 0){
$newDateTime->sub($dayInterval);
}
if ($deltaMin > 0) {
$newDateTime->add($minInterval);
}
else if ($deltaMin < 0) {
$newDateTime->sub($minInterval);
}
Logging::log("addDeltas: modified time {$newDateTime->format('Y-m-d H:i:s')}");
return $newDateTime;
}
public function moveShow($deltaDay, $deltaMin)
{
global $CC_DBC;
if ($this->getShow()->isRepeating()){
return "Can't drag and drop repeating shows";
}
$hours = $deltaMin/60;
if($hours > 0)
$hours = floor($hours);
else
$hours = ceil($hours);
$mins = abs($deltaMin%60);
$today_timestamp = time();
$starts = $this->getShowInstanceStart();
$ends = $this->getShowInstanceEnd();
$startsDateTime = new DateTime($this->getShowInstanceStart(), new DateTimeZone("UTC"));
$endsDateTime = new DateTime($this->getShowInstanceEnd(), new DateTimeZone("UTC"));
$startsDateTime = new DateTime($starts, new DateTimeZone("UTC"));
if($today_timestamp > $startsDateTime->getTimestamp()) {
if ($today_timestamp > $startsDateTime->getTimestamp()) {
return "Can't move a past show";
}
$sql = "SELECT timestamp '{$starts}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'";
$new_starts = $CC_DBC->GetOne($sql);
$newStartsDateTime = new DateTime($new_starts, new DateTimeZone("UTC"));
//the user is moving the show on the calendar from the perspective of local time.
//incase a show is moved across a time change border offsets should be added to the localtime
//stamp and then converted back to UTC to avoid show time changes!
$startsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
$endsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
$sql = "SELECT timestamp '{$ends}' + interval '{$deltaDay} days' + interval '{$hours}:{$mins}'";
$new_ends = $CC_DBC->GetOne($sql);
$newEndsDateTime = new DateTime($new_ends, new DateTimeZone("UTC"));
$newStartsDateTime = self::addDeltas($startsDateTime, $deltaDay, $deltaMin);
$newEndsDateTime = self::addDeltas($endsDateTime, $deltaDay, $deltaMin);
//convert our new starts/ends to UTC.
$newStartsDateTime->setTimezone(new DateTimeZone("UTC"));
$newEndsDateTime->setTimezone(new DateTimeZone("UTC"));
if($today_timestamp > $newStartsDateTime->getTimestamp()) {
if ($today_timestamp > $newStartsDateTime->getTimestamp()) {
return "Can't move show into past";
}
$overlap = Application_Model_Show::getShows($newStartsDateTime, $newEndsDateTime, array($this->_instanceId));
if ($this->isRebroadcast()) {
if(count($overlap) > 0) {
return "Should not overlap shows";
}
try {
$recordedShow = new Application_Model_ShowInstance($this->_showInstance->getDbOriginalShow());
}
//recorded show doesn't exist.
catch (Exception $e) {
$this->_showInstance->delete();
return "Show was deleted because recorded show does not exist!";
}
$rebroadcast = $this->isRebroadcast();
if($rebroadcast) {
$sql = "SELECT timestamp '{$new_starts}' < (SELECT starts FROM cc_show_instances WHERE id = {$rebroadcast})";
$isBeforeRecordedOriginal = $CC_DBC->GetOne($sql);
$recordEndDateTime = new DateTime($recordedShow->getShowInstanceEnd(), new DateTimeZone("UTC"));
$newRecordEndDateTime = self::addDeltas($recordEndDateTime, 0, 60);
if($isBeforeRecordedOriginal === 't'){
return "Cannot move a rebroadcast show before its original show";
if ($newStartsDateTime->getTimestamp() < $newRecordEndDateTime->getTimestamp()) {
return "Must wait 1 hour to rebroadcast.";
}
}
$this->setShowStart($new_starts);
$this->setShowEnd($new_ends);
$this->setShowStart($newStartsDateTime);
$this->setShowEnd($newEndsDateTime);
$this->correctScheduleStartTimes();
$show = new Application_Model_Show($this->getShowId());