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

View File

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