CC-5627 : Check all Application_Common_DateHelper calculations that use timezone

This commit is contained in:
Naomi 2013-12-12 17:18:03 -05:00
parent 974be86691
commit 87ac5dc294
2 changed files with 68 additions and 48 deletions

View File

@ -215,12 +215,13 @@ class Application_Service_ShowFormService
}
$service_show = new Application_Service_ShowService($this->ccShow->getDbId());
$repeatEndDate = new DateTime($service_show->getRepeatingEndDate(), new DateTimeZone(
$ccShowDays[0]->getDbTimezone()));
$repeatEndDate = $service_show->getRepeatingEndDate();
//end dates are stored non-inclusively so we need to
//subtract one day
$repeatEndDate->sub(new DateInterval("P1D"));
if (!is_null($repeatEndDate)) {
$repeatEndDate->sub(new DateInterval("P1D"));
}
//default monthly repeat type
$monthlyRepeatType = 2;
$repeatType = $ccShowDays[0]->getDbRepeatType();
@ -237,8 +238,8 @@ class Application_Service_ShowFormService
'add_show_linked' => $this->ccShow->getDbLinked(),
'add_show_repeat_type' => $repeatType,
'add_show_day_check' => $days,
'add_show_end_date' => $repeatEndDate->format("Y-m-d"),
'add_show_no_end' => (!$service_show->getRepeatingEndDate()),
'add_show_end_date' => (!is_null($repeatEndDate)) ? $repeatEndDate->format("Y-m-d"):null,
'add_show_no_end' => (is_null($repeatEndDate)),
'add_show_monthly_repeat_type' => $monthlyRepeatType));
if (!$this->ccShow->isLinkable() || $this->ccShow->isRecorded()) {

View File

@ -413,13 +413,8 @@ SQL;
$currentShowDay = $this->ccShow->getFirstCcShowDay();
}
//new end date in users' local time
//new end date in the show's timezone (from the select box)
$endDateTime = $this->calculateEndDate($showData);
if (!is_null($endDateTime)) {
$endDate = $endDateTime->format("Y-m-d");
} else {
$endDate = $endDateTime;
}
//repeat option was toggled
if ($showData['add_show_repeats'] != $currentShowDay->isRepeating()) {
@ -512,28 +507,27 @@ SQL;
}
}
$currentShowEndDate = $this->getRepeatingEndDate();
//check if "no end" option has changed
if ($currentShowEndDate != $showData['add_show_no_end']) {
//get the endate from the past for this show.
//check if this is null if "no end"
$currentShowEndDateTime = $this->getRepeatingEndDate();
if ($currentShowEndDateTime != $endDateTime) {
$endDate = clone $endDateTime;
$endDate->setTimezone(new DateTimeZone("UTC"));
//show "No End" option was toggled
if (!$showData['add_show_no_end']) {
//or the end date comes earlier
if (is_null($currentShowEndDateTime) || ($endDateTime < $currentShowEndDateTime)) {
//"No End" option was unchecked so we need to delete the
//repeat instances that are scheduled after the new end date
$this->deleteInstancesFromDate($endDate, $showId);
//OR
//end date was pushed back so we have to delete any
//instances of this show scheduled after the new end date
$this->deleteInstancesFromDate($endDate->format("Y-m-d"), $showId);
}
}
if ($currentShowEndDate != $showData['add_show_end_date']) {
//end date was changed
$newEndDate = strtotime($showData['add_show_end_date']);
$oldEndDate = strtotime($currentShowEndDate);
if ($newEndDate < $oldEndDate) {
//end date was pushed back so we have to delete any
//instances of this show scheduled after the new end date
$this->deleteInstancesFromDate($endDate, $showId);
}
}
}//if repeats
}
return $daysAdded;
}
@ -554,19 +548,34 @@ SQL;
}
}
/*
* returns a DateTime of the current show end date set to the timezone of the show.
*/
public function getRepeatingEndDate()
{
$sql = <<<SQL
SELECT last_show
SELECT last_show, timezone
FROM cc_show_days
WHERE show_id = :showId
ORDER BY last_show DESC
LIMIT 1
SQL;
$query = Application_Common_Database::prepareAndExecute( $sql,
array( 'showId' => $this->ccShow->getDbId() ), 'column' );
array( 'showId' => $this->ccShow->getDbId() ), 'single');
return ($query !== false) ? $query : false;
Logging::info($query);
$date = null;
if ($query !== false && isset($query["last_show"])) {
$date = new DateTime(
$query["last_show"],
new DateTimeZone($query["timezone"])
);
}
return $date;
}
private function deleteInstancesFromDate($endDate, $showId)
@ -823,13 +832,24 @@ SQL;
*/
private function calculateEndDate($showData)
{
//if no end return null
if ($showData['add_show_no_end']) {
$endDate = NULL;
} elseif ($showData['add_show_repeats']) {
$endDate = new DateTime($showData['add_show_end_date']);
$endDate = null;
}
//if the show is repeating & ends, then return the end date
elseif ($showData['add_show_repeats']) {
$endDate = new DateTime(
$showData['add_show_end_date'],
new DateTimeZone($showData["add_show_timezone"])
);
$endDate->add(new DateInterval("P1D"));
} else {
$endDate = new DateTime($showData['add_show_start_date']);
}
//the show doesn't repeat, so add one day to the start date.
else {
$endDate = new DateTime(
$showData['add_show_start_date'],
new DateTimeZone($showData["add_show_timezone"])
);
$endDate->add(new DateInterval("P1D"));
}
@ -1407,25 +1427,24 @@ SQL;
{
$showId = $this->ccShow->getDbId();
$startDateTime = new DateTime($showData['add_show_start_date']." ".$showData['add_show_start_time']);
$startDateTime = new DateTime(
$showData['add_show_start_date']." ".$showData['add_show_start_time'],
new DateTimeZone($showData['add_show_timezone'])
);
$endDateTime = $this->calculateEndDate($showData);
if (!is_null($endDateTime)) {
$endDate = $endDateTime->format("Y-m-d");
} else {
$endDate = $endDateTime;
}
else {
$endDate = null;
}
/* What we are doing here is checking if the show repeats or if
* any repeating days have been checked. If not, then by default
* the "selected" DOW is the initial day.
* DOW in local time.
*/
$startDow = date("w", $startDateTime->getTimestamp());
//Our calculated start DOW must be used for non repeating since a day has not been selected.
//For all repeating shows, only the selected days of the week will be repeated on.
$startDow = $startDateTime->format("w");
if (!$showData['add_show_repeats']) {
$showData['add_show_day_check'] = array($startDow);
} elseif ($showData['add_show_repeats'] && $showData['add_show_day_check'] == "") {
$showData['add_show_day_check'] = array($startDow);
}
// Don't set day for monthly repeat type, it's invalid