CC-4961: Show linking

Made some progress on show editing
This commit is contained in:
denise 2013-03-12 18:01:12 -04:00
parent 438200425a
commit f9f4e4f1fb
4 changed files with 281 additions and 28 deletions

View File

@ -15,4 +15,29 @@
*/
class CcShowDays extends BaseCcShowDays {
public function isRepeating()
{
return $this->getDbRepeatType() != -1;
}
public function getUTCStartDateAndTime()
{
$dt = new DateTime(
"{$this->getDbFirstShow()} {$this->getDbStartTime()}",
new DateTimeZone($this->getDbTimezone())
);
$dt->setTimezone(new DateTimeZone("UTC"));
return $dt;
}
public function getLocalStartDateAndTime()
{
$dt = new DateTime(
"{$this->getDbFirstShow()} {$this->getDbStartTime()}",
new DateTimeZone($this->getDbTimezone())
);
return $dt;
}
} // CcShowDays

View File

@ -223,6 +223,8 @@ class Application_Service_ScheduleService
if ($currentUser->isAdminOrPM()) {
$ccShow = $this->service_show->setShow($showData, false);
$this->service_showInstances->updateShowInstances($showData, $isRecorded, $repeatType);
}
}
@ -259,7 +261,7 @@ class Application_Service_ScheduleService
//if the show is repeating, set the start date to the next
//repeating instance in the future
if ($currentShowDay->getDbRepeatType() != -1) {
if ($currentShowDay->isRepeating()) {
$nextFutureRepeatShow = $this->service_showInstances
->getNextFutureRepeatShowTime($formData["add_show_id"]);
$originalShowStartDateTime = $nextFutureRepeatShow["starts"];

View File

@ -8,6 +8,24 @@ class Application_Service_ShowDaysService
{
$this->showId = $id;
}
public function calculateEndDate($showData)
{
if ($showData['add_show_no_end']) {
$endDate = NULL;
} elseif ($showData['add_show_repeats']) {
$endDateTime = new DateTime($showData['add_show_end_date']);
$endDateTime->add(new DateInterval("P1D"));
$endDate = $endDateTime->format("Y-m-d");
} else {
$endDateTime = new DateTime($showData['add_show_start_date']);
$endDateTime->add(new DateInterval("P1D"));
$endDate = $endDateTime->format("Y-m-d");
}
return $endDate;
}
/**
*
* Sets the fields for a cc_show_days table row
@ -21,17 +39,7 @@ class Application_Service_ShowDaysService
{
$startDateTime = new DateTime($showData['add_show_start_date']." ".$showData['add_show_start_time']);
if ($showData['add_show_no_end']) {
$endDate = NULL;
} elseif ($showData['add_show_repeats']) {
$endDateTime = new DateTime($showData['add_show_end_date']);
$endDateTime->add(new DateInterval("P1D"));
$endDate = $endDateTime->format("Y-m-d");
} else {
$endDateTime = new DateTime($showData['add_show_start_date']);
$endDateTime->add(new DateInterval("P1D"));
$endDate = $endDateTime->format("Y-m-d");
}
$endDate = $this->calculateEndDate($showData);
/* What we are doing here is checking if the show repeats or if
* any repeating days have been checked. If not, then by default
@ -94,10 +102,12 @@ class Application_Service_ShowDaysService
*/
public function getShowDays()
{
$sql = "SELECT * FROM cc_show_days WHERE show_id = :show_id";
/*$sql = "SELECT * FROM cc_show_days WHERE show_id = :show_id";
return Application_Common_Database::prepareAndExecute(
$sql, array(":show_id" => $this->showId), 'all');
$sql, array(":show_id" => $this->showId), 'all');*/
return CcShowDaysQuery::create()->filterByDbShowId(
$this->showId)->find();
}
public function getStartDateAndTime()

View File

@ -30,7 +30,7 @@ class Application_Service_ShowInstanceService
$showDays = $this->service_showDays->getShowDays();
foreach ($showDays as $day) {
switch ($day["repeat_type"]) {
switch ($day->getDbRepeatType()) {
case NO_REPEAT:
$this->createNonRepeatingShowInstance($day, $populateUntil, $isRebroadcast);
break;
@ -59,17 +59,17 @@ class Application_Service_ShowInstanceService
*/
private function createNonRepeatingShowInstance($showDay, $populateUntil, $isRebroadcast)
{
$start = $showDay["first_show"]." ".$showDay["start_time"];
$start = $showDay->getDbFirstShow()." ".$showDay->getDbStartTime();
list($utcStartDateTime, $utcEndDateTime) = $this->service_show->createUTCStartEndDateTime(
$start, $showDay["duration"], $showDay["timezone"]);
$start, $showDay->getDbDuration(), $showDay->getDbTimezone());
if ($utcStartDateTime->getTimestamp() < $populateUntil->getTimestamp()) {
$ccShowInstance = new CcShowInstances();
$ccShowInstance->setDbShowId($showDay["show_id"]);
$ccShowInstance->setDbShowId($showDay->getDbShowId());
$ccShowInstance->setDbStarts($utcStartDateTime);
$ccShowInstance->setDbEnds($utcEndDateTime);
$ccShowInstance->setDbRecord($showDay["record"]);
$ccShowInstance->setDbRecord($showDay->getDbRecord());
$ccShowInstance->save();
if ($isRebroadcast) {
@ -89,15 +89,15 @@ class Application_Service_ShowInstanceService
private function createWeeklyRepeatingShowInstances($showDay, $populateUntil,
$repeatInterval, $isRebroadcast)
{
$show_id = $showDay["show_id"];
$next_pop_date = $showDay["next_pop_date"];
$first_show = $showDay["first_show"]; //non-UTC
$last_show = $showDay["last_show"]; //non-UTC
$start_time = $showDay["start_time"]; //non-UTC
$duration = $showDay["duration"];
$day = $showDay["day"];
$record = $showDay["record"];
$timezone = $showDay["timezone"];
$show_id = $showDay->getDbShowId();
$next_pop_date = $showDay->getDbNextPopDate();
$first_show = $showDay->getDbFirstShow(); //non-UTC
$last_show = $showDay->getDbLastShow(); //non-UTC
$start_time = $showDay->getDbStartTime(); //non-UTC
$duration = $showDay->getDbDuration();
$day = $showDay->getDbDay();
$record = $showDay->getDbRecord();
$timezone = $showDay->getDbTimezone();
$currentUtcTimestamp = gmdate("Y-m-d H:i:s");
@ -229,4 +229,220 @@ SQL;
return $show;
}
/**
* This function is messy. But sometimes there is no easy way to do it.
*
* When editing a show we may need to perform some actions to reflect the new specs:
* - Delete some show instances
* - Update duration
* - Update start and end time
*
* @param $showData edit show form values in raw form
* @param $isRecorded value computed from the edit show form
* @param $repeatType value computed from the edit show form
*/
public function updateShowInstances($showData, $isRecorded, $repeatType)
{
$showId = $showData["add_show_id"];
$this->service_showDays = new Application_Service_ShowDaysService($showId);
//ccShowDays object of the show being edited
$currentShowDay = $this->service_showDays->getCurrentShowDay();
$endDate = $this->service_showDays->calculateEndDate($showData);
//repeat option was toggled
if ($showData['add_show_repeats'] != $currentShowDay->isRepeating()) {
$this->deleteAllRepeatInstances($currentShowDay, $showId);
}
//duration has changed
if ($showData['add_show_duration'] != $currentShowDay->getDbDuration()) {
$this->updateDuration($showData);
}
if ($showData['add_show_repeats']) {
$localShowStart = $currentShowDay->getLocalStartDateAndTime();
//if the start date changes, these are the repeat types
//that require show instance deletion
$deleteRepeatTypes = array(REPEAT_BI_WEEKLY, REPEAT_MONTHLY_MONTHLY,
REPEAT_MONTHLY_WEEKLY);
if (in_array($repeatType, $deleteRepeatTypes) &&
$showData["add_show_start_date"] != $localShowStart->format("Y-m-d")) {
//Start date has changed when repeat type is bi-weekly or monthly.
//This screws up the repeating positions of show instances, so lets
//we need to delete them (CC-2351)
$this->deleteAllInstances($showId);
}
if ($repeatType != $currentShowDay->getDbRepeatType()) {
//repeat type changed
$this->deleteAllInstances($showId);
} else {
//repeat type is the same, check if the days of the week are the same
$repeatingDaysChanged = false;
$ccShowDays = $this->service_showDays->getShowDays();
$showDays = array();
foreach ($ccShowDays as $day) {
$showDays[] = $day->getDbDay();
}
if (count($showData['add_show_day_check']) == count($showDays)) {
//same number of days checked, lets see if they are the same numbers
$intersect = array_intersect($showData['add_show_day_check'], $showDays);
if (count($intersect) != count($showData['add_show_day_check'])) {
$repeatingDaysChanged = true;
}
} else {
$repeatingDaysChanged = true;
}
if ($repeatingDaysChanged) {
$daysRemoved = array_diff($showDays, $showData['add_show_day_check']);
if (count($daysRemoved) > 0) {
//delete repeating show instances for the repeating
//days that were removed
$this->deleteRemovedShowDayInstances($daysRemoved,
$ccShowDays, $showId);
}
}
if ($showData['add_show_start_date'] != $localShowStart->format("Y-m-d")
|| $showData['add_show_start_time'] != $localShowStart->format("H:i:s")){
//start date/time has changed
if ($showData['add_show_start_date'] > $localShowStart->format("Y-m-d")) {
$this->deleteInstancesBeforeDate($showData['add_show_start_date'], $showId);
}
/*$this->updateStartDateTime($showData, $p_endDate);*/
}
}
/*
//Check if end date for the repeat option has changed. If so, need to take care
//of deleting possible invalid Show Instances.
if ((strlen($this->getRepeatingEndDate()) == 0) == $showData['add_show_no_end']) {
//show "Never Ends" option was toggled.
if ($showData['add_show_no_end']) {
} else {
$this->removeAllInstancesFromDate($p_endDate);
}
}
if ($this->getRepeatingEndDate() != $showData['add_show_end_date']) {
//end date was changed.
$newDate = strtotime($showData['add_show_end_date']);
$oldDate = strtotime($this->getRepeatingEndDate());
if ($newDate < $oldDate) {
$this->removeAllInstancesFromDate($p_endDate);
}
}*/
}
}
public function deleteAllRepeatInstances($currentShowDay, $showId)
{
$firstShow = $currentShowDay->getUTCStartDateAndTime();
$sql = <<<SQL
DELETE
FROM cc_show_instances
WHERE starts > :timestamp::TIMESTAMP
AND show_id = :showId
AND date(starts) != :firstShow
SQL;
Application_Common_Database::prepareAndExecute( $sql,
array( ':timestamp' => gmdate("Y-m-d H:i:s"),
':showId' => $showId,
':firstShow' => $firstShow->format("Y-m-d")), 'execute');
}
public function deleteAllInstances($showId)
{
$sql = <<<SQL
DELETE
FROM cc_show_instances
WHERE starts > :timestamp::TIMESTAMP
AND show_id = :showId
SQL;
Application_Common_Database::prepareAndExecute( $sql,
array( ':timestamp' => gmdate("Y-m-d H:i:s"),
':showId' => $showId), 'execute');
}
/**
*
* Enter description here ...
* @param $daysRemoved array of days removed
* (
* @param $showDays
* @param $showId
*/
public function deleteRemovedShowDayInstances($daysRemoved, $showDays, $showId)
{
$daysRemovedUTC = array();
//convert the start day of the week to UTC
foreach ($showDays as $showDay) {
if (in_array($showDay->getDbDay(), $daysRemoved)) {
$showDay->reload();
$startDay = $showDay->getUTCStartDateAndTime();
$daysRemovedUTC[] = $startDay->format('w');
}
}
$uncheckedDays = pg_escape_string(implode(",", $daysRemovedUTC));
$sql = <<<SQL
DELETE
FROM cc_show_instances
WHERE EXTRACT(DOW FROM starts) IN ($uncheckedDays)
AND starts > :timestamp::TIMESTAMP
AND show_id = :showId
SQL;
Application_Common_Database::prepareAndExecute( $sql,
array(
":timestamp" => gmdate("Y-m-d H:i:s"),
":showId" => $showId,
), "execute");
}
public function deleteInstancesBeforeDate($newStartDate, $showId)
{
$sql = <<<SQL
DELETE
FROM cc_show_instances
WHERE date(starts) < :newStartDate::DATE
AND starts > :timestamp::TIMESTAMP
AND show_id = :showId
SQL;
Application_Common_Database::prepareAndExecute($sql, array(
":newStartDate" => $newStartDate, ":timestamp" => gmdate("Y-m-d H:i:s"),
":showId" => $showId), "execute");
}
public function updateDuration($showData)
{
$date = new Application_Common_DateHelper;
$timestamp = $date->getUtcTimestamp();
$sql = <<<SQL
UPDATE cc_show_instances
SET ends = starts + :add_show_duration::INTERVAL
WHERE show_id = :show_id
AND ends > :timestamp::TIMESTAMP
SQL;
Application_Common_Database::prepareAndExecute( $sql, array(
':add_show_duration' => $showData['add_show_duration'],
':show_id' => $showData['add_show_id'],
':timestamp' => $timestamp), "execute");
}
}