CC-5405: When editing a single show instance from a repeating series, should not create a new cc_show

- Fixed resizing repeating shows to not include edited instances
- Fixed context menu on repeating shows with edited instances
- Fixed populated the show form when editing a single instance
This commit is contained in:
drigato 2013-12-02 07:19:31 -05:00
parent a465b5a770
commit ef8af1b724
4 changed files with 146 additions and 27 deletions

View File

@ -184,16 +184,84 @@ SQL;
$nowDateTime = new DateTime("now", $utc);
$showInstances = CcShowInstancesQuery::create()
->filterByDbShowId($this->_showId)
->find($con);
//keep track of cc_show_day entries we need to update
$showDayIds = array();
/*
* If the resized show is an edited instance of a repeating show we
* need to treat it as a separate show and not resize the other instances
*
* Also, if the resized show has edited instances, we need to exclude
* those from the resize
*/
$ccShow = CcShowQuery::create()->findPk($this->_showId);
if ($ccShow->isRepeating()) {
//convert instance to local timezone
$ccShowInstance = CcShowInstancesQuery::create()->findPk($instanceId);
$startsDT = new DateTime($ccShowInstance->getDbStarts(),
new DateTimeZone("UTC"));
$timezone = $ccShow->getFirstCcShowDay()->getDbTimezone();
$startsDT->setTimezone(new DateTimeZone($timezone));
/* Get cc_show_day for the current instance. If we don't find one
* we know it is a repeat interval of one of cc_show_days first
* show and we can assume we aren't resizing a modified instance
*/
$ccShowDay = CcShowDaysQuery::create()
->filterByDbFirstShow($startsDT->format("Y-m-d"))
->filterByDbStartTime($startsDT->format("H:i:s"))
->filterByDbShowId($this->_showId)
->findOne();
/* Check if this cc_show_day rule is non-repeating. If it is, then
* we know this instance was edited out of the repeating sequence
*/
if (!$ccShowDay || $ccShowDay->getDbRepeatType() != -1) {
$ccShowDays = $ccShow->getRepeatingCcShowDays();
foreach ($ccShowDays as $day) {
array_push($showDayIds, $day->getDbId());
}
$excludeIds = $ccShow->getEditedRepeatingInstanceIds();
//exlcude edited instances from resize
$showInstances = CcShowInstancesQuery::create()
->filterByDbShowId($this->_showId)
->filterByDbModifiedInstance(false)
->filterByDbId($excludeIds, criteria::NOT_IN)
->find();
} elseif ($ccShowDay->getDbRepeatType() == -1) {
array_push($showDayIds, $ccShowDay->getDbId());
//treat edited instance as separate show for resize
$showInstances = CcShowInstancesQuery::create()
->filterByDbId($instanceId)
->find();
}
} else {
$ccShowDays = $ccShow->getCcShowDayss();
foreach ($ccShowDays as $day) {
array_push($showDayIds, $day->getDbId());
}
$showInstances = CcShowInstancesQuery::create()
->filterByDbShowId($this->_showId)
->find($con);
}
/* Check two things:
1. If the show being resized and any of its repeats end in the past
2. If the show being resized and any of its repeats overlap
with other scheduled shows */
//keep track of instance ids for update show instances start/end times
$instanceIds = array();
//check if new show time overlaps with any other shows
foreach ($showInstances as $si) {
array_push($instanceIds, $si->getDbId());
$startsDateTime = new DateTime($si->getDbStarts(), new DateTimeZone("UTC"));
$endsDateTime = new DateTime($si->getDbEnds(), new DateTimeZone("UTC"));
@ -204,19 +272,19 @@ SQL;
$startsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
$endsDateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
$newStartsDateTime = Application_Model_ShowInstance::addDeltas($startsDateTime, $deltaDay, $deltaMin);
//$newStartsDateTime = Application_Model_ShowInstance::addDeltas($startsDateTime, $deltaDay, $deltaMin);
$newEndsDateTime = Application_Model_ShowInstance::addDeltas($endsDateTime, $deltaDay, $deltaMin);
if ($newEndsDateTime->getTimestamp() < $nowDateTime->getTimestamp()) {
return _("End date/time cannot be in the past");
}
//convert our new starts/ends to UTC.
$newStartsDateTime->setTimezone($utc);
//$newStartsDateTime->setTimezone($utc);
$newEndsDateTime->setTimezone($utc);
$overlapping = Application_Model_Schedule::checkOverlappingShows(
$newStartsDateTime, $newEndsDateTime, true, $si->getDbId());
$startsDateTime, $newEndsDateTime, true, $si->getDbId());
if ($overlapping) {
return _("Cannot schedule overlapping shows.\nNote: Resizing a repeating show ".
@ -231,36 +299,30 @@ SQL;
//current timesamp in UTC.
$current_timestamp = gmdate("Y-m-d H:i:s");
$sql_gen = <<<SQL
UPDATE cc_show_instances
SET ends = (ends + :deltaDay1::INTERVAL + :interval1::INTERVAL)
WHERE (show_id = :show_id1
AND ends > :current_timestamp1)
AND ((ends + :deltaDay2::INTERVAL + :interval2::INTERVAL - starts) <= interval '24:00')
SQL;
$sql_gen = "UPDATE cc_show_instances ".
"SET ends = (ends + :deltaDay1::INTERVAL + :interval1::INTERVAL) ".
"WHERE (id IN (".implode($instanceIds, ",").") ".
"AND ends > :current_timestamp1) ".
"AND ((ends + :deltaDay2::INTERVAL + :interval2::INTERVAL - starts) <= interval '24:00')";
Application_Common_Database::prepareAndExecute($sql_gen,
array(
':deltaDay1' => "$deltaDay days",
':interval1' => "$hours:$mins",
':show_id1' => $this->_showId,
':current_timestamp1' => $current_timestamp,
':deltaDay2' => "$deltaDay days",
':interval2' => "$hours:$mins"
), "execute");
$sql_gen = <<<SQL
UPDATE cc_show_days
SET duration = (CAST(duration AS interval) + :deltaDay3::INTERVAL + :interval3::INTERVAL)
WHERE show_id = :show_id2
AND ((CAST(duration AS interval) + :deltaDay4::INTERVAL + :interval4::INTERVAL) <= interval '24:00')
SQL;
$sql_gen = "UPDATE cc_show_days ".
"SET duration = (CAST(duration AS interval) + :deltaDay3::INTERVAL + :interval3::INTERVAL) ".
"WHERE id IN (".implode($showDayIds, ",").") ".
"AND ((CAST(duration AS interval) + :deltaDay4::INTERVAL + :interval4::INTERVAL) <= interval '24:00')";
Application_Common_Database::prepareAndExecute($sql_gen,
array(
':deltaDay3' => "$deltaDay days",
':interval3' => "$hours:$mins",
':show_id2' => $this->_showId,
':deltaDay4' => "$deltaDay days",
':interval4' => "$hours:$mins"
), "execute");
@ -279,7 +341,7 @@ SQL;
$instances = CcShowInstancesQuery::create()
->filterByDbEnds($current_timestamp, Criteria::GREATER_THAN)
->filterByDbShowId($this->_showId)
->filterByDbId($instanceIds, Criteria::IN)
->find($con);
foreach ($instances as $instance) {

View File

@ -109,6 +109,44 @@ class CcShow extends BaseCcShow {
return false;
}
/**
* Returns all cc_show_instances that have been edited out of
* a repeating sequence
*/
public function getEditedRepeatingInstanceIds()
{
//get cc_show_days that have been edited (not repeating)
$ccShowDays = CcShowDaysQuery::create()
->filterByDbShowId($this->id)
->filterByDbRepeatType(-1)
->find();
$startsUTC = array();
$utc = new DateTimeZone("UTC");
foreach ($ccShowDays as $day) {
//convert to UTC
$starts = new DateTime(
$day->getDbFirstShow()." ".$day->getDbStartTime(),
new DateTimeZone($day->getDbTimezone())
);
$starts->setTimezone($utc);
array_push($startsUTC, $starts->format("Y-m-d H:i:s"));
}
$excludeInstances = CcShowInstancesQuery::create()
->filterByDbShowId($this->id)
->filterByDbStarts($startsUTC, criteria::IN)
->find();
$excludeIds = array();
foreach ($excludeInstances as $instance) {
array_push($excludeIds, $instance->getDbId());
}
return $excludeIds;
}
/**
* Gets an array of CcShowInstances objects which contain a foreign key that references this object.
*

View File

@ -126,7 +126,15 @@ class Application_Service_CalendarService
}
}
$isRepeating = $this->ccShow->getFirstCcShowDay()->isRepeating();
$excludeIds = $this->ccShow->getEditedRepeatingInstanceIds();
$isRepeating = true;
$populateInstance = false;
if (in_array($this->ccShowInstance->getDbId(), $excludeIds)) {
$populateInstance = true;
$isRepeating = false;
}
if (!$this->ccShowInstance->isRebroadcast() && $isAdminOrPM) {
if ($isRepeating) {
$menu["edit"] = array(
@ -143,6 +151,7 @@ class Application_Service_CalendarService
"name" => _("Edit This Instance"),
"icon" => "edit",
"url" => $baseUrl."Schedule/populate-repeating-show-instance-form");
} else {
$menu["edit"] = array(
"name"=> _("Edit Show"),

View File

@ -159,7 +159,11 @@ class Application_Service_ShowService
*/
private function storeOrigLocalShowInfo()
{
$this->origCcShowDay = $this->ccShow->getFirstRepeatingCcShowDay();
if ($this->ccShow->isRepeating()) {
$this->origCcShowDay = $this->ccShow->getFirstRepeatingCcShowDay();
} else {
$this->origCcShowDay = $this->ccShow->getFirstCcShowDay();
}
$this->oldShowTimezone = $this->origCcShowDay->getDbTimezone();
@ -404,7 +408,11 @@ SQL;
$daysAdded = array();
//CcShowDay object
$currentShowDay = $this->ccShow->getFirstRepeatingCcShowDay();
if ($this->ccShow->isRepeating()) {
$currentShowDay = $this->ccShow->getFirstRepeatingCcShowDay();
} else {
$currentShowDay = $this->ccShow->getFirstCcShowDay();
}
//new end date in users' local time
$endDateTime = $this->calculateEndDate($showData);
@ -1387,6 +1395,8 @@ SQL;
*/
private function setCcShowDays($showData)
{
Logging::info($showData);
Logging::info($this->repeatType);
$showId = $this->ccShow->getDbId();
$startDateTime = new DateTime($showData['add_show_start_date']." ".$showData['add_show_start_time']);
@ -1451,7 +1461,7 @@ SQL;
if ($this->isUpdate) {
$showDay = CcShowDaysQuery::create()
->filterByDbShowId($showId)
->filterByDbRepeatType($showData['add_show_repeat_type'])
->filterByDbRepeatType($this->repeatType)
->filterByDbDay($day)
->findOne();
if (!$showDay) {