CC-5594 : Remove all date_default_timezone_get()

DateTime here was relying on a timezone that wasn't what the user selects in the timezone drop down on the form.
This commit is contained in:
Naomi 2013-12-05 13:04:02 -05:00
parent ed74470803
commit eb4cefa3ff
1 changed files with 37 additions and 41 deletions

View File

@ -113,18 +113,20 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm
$start_time = $formData['add_show_start_date']." ".$formData['add_show_start_time']; $start_time = $formData['add_show_start_date']." ".$formData['add_show_start_time'];
$end_time = $formData['add_show_end_date_no_repeat']." ".$formData['add_show_end_time']; $end_time = $formData['add_show_end_date_no_repeat']." ".$formData['add_show_end_time'];
//DateTime stores $start_time in the current timezone //have to use the timezone the user has entered in the form to check past/present
$nowDateTime = new DateTime(); $showTimezone = new DateTimeZone($formData["add_show_timezone"]);
$showStartDateTime = new DateTime($start_time); $nowDateTime = new DateTime("now", $showTimezone);
$showEndDateTime = new DateTime($end_time); $showStartDateTime = new DateTime($start_time, $showTimezone);
$showEndDateTime = new DateTime($end_time, $showTimezone);
if ($validateStartDate) { if ($validateStartDate) {
if ($showStartDateTime->getTimestamp() < $nowDateTime->getTimestamp()) { if ($showStartDateTime < $nowDateTime) {
$this->getElement('add_show_start_time')->setErrors(array(_('Cannot create show in the past'))); $this->getElement('add_show_start_time')->setErrors(array(_('Cannot create show in the past')));
$valid = false; $valid = false;
} }
// if edit action, check if original show start time is in the past. CC-3864 // if edit action, check if original show start time is in the past. CC-3864
if ($originalStartDate) { if ($originalStartDate) {
if ($originalStartDate->getTimestamp() < $nowDateTime->getTimestamp()) { if ($originalStartDate < $nowDateTime) {
$this->getElement('add_show_start_time')->setValue($originalStartDate->format("H:i")); $this->getElement('add_show_start_time')->setValue($originalStartDate->format("H:i"));
$this->getElement('add_show_start_date')->setValue($originalStartDate->format("Y-m-d")); $this->getElement('add_show_start_date')->setValue($originalStartDate->format("Y-m-d"));
$this->getElement('add_show_start_time')->setErrors(array(_('Cannot modify start date/time of the show that is already started'))); $this->getElement('add_show_start_time')->setErrors(array(_('Cannot modify start date/time of the show that is already started')));
@ -135,45 +137,39 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm
} }
// if end time is in the past, return error // if end time is in the past, return error
if ($showEndDateTime->getTimestamp() < $nowDateTime->getTimestamp()) { if ($showEndDateTime < $nowDateTime) {
$this->getElement('add_show_end_time')->setErrors(array(_('End date/time cannot be in the past'))); $this->getElement('add_show_end_time')->setErrors(array(_('End date/time cannot be in the past')));
$valid = false; $valid = false;
} }
$pattern = '/([0-9][0-9])h ([0-9][0-9])m/'; //validate duration.
$duration = $showStartDateTime->diff($showEndDateTime);
if (preg_match($pattern, $formData['add_show_duration'], $matches) && count($matches) == 3) { if ($showStartDateTime > $showEndDateTime) {
$hours = $matches[1];
$minutes = $matches[2];
if ($formData["add_show_duration"] == "00h 00m") {
$this->getElement('add_show_duration')->setErrors(array(_('Cannot have duration 00h 00m')));
$valid = false;
} elseif (strpos($formData["add_show_duration"], 'h') !== false && $hours >= 24) {
if ($hours > 24 || ($hours == 24 && $minutes > 0)) {
$this->getElement('add_show_duration')->setErrors(array(_('Cannot have duration greater than 24h')));
$valid = false;
}
} elseif ( strstr($formData["add_show_duration"], '-') ) {
$this->getElement('add_show_duration')->setErrors(array(_('Cannot have duration < 0m'))); $this->getElement('add_show_duration')->setErrors(array(_('Cannot have duration < 0m')));
$valid = false; $valid = false;
} }
} else { else if ($showStartDateTime == $showEndDateTime) {
$this->getElement('add_show_duration')->setErrors(array(_('Cannot have duration 00h 00m')));
$valid = false; $valid = false;
} }
else if (intval($duration->format('%d')) > 0) {
$this->getElement('add_show_duration')->setErrors(array(_('Cannot have duration greater than 24h')));
$valid = false;
}
/* Check if show is overlapping /* Check if show is overlapping
* We will only do this check if the show is valid * We will only do this check if the show is valid
* upto this point * upto this point
*/ */
if ($valid) { if ($valid) {
$utc = new DateTimeZone('UTC');
$showTimezone = new DateTimeZone($formData["add_show_timezone"]);
$show_start = new DateTime($start_time, $showTimezone);
//we need to know the start day of the week in show's local timezome //we need to know the start day of the week in show's local timezome
$startDow = $show_start->format("w"); $startDow = $showStartDateTime->format("w");
$show_start->setTimezone($utc);
$show_end = new DateTime($end_time, $showTimezone); $utc = new DateTimeZone('UTC');
$show_end->setTimezone($utc); $showStartDateTime->setTimezone($utc);
$showEndDateTime->setTimezone($utc);
if ($formData["add_show_repeats"]) { if ($formData["add_show_repeats"]) {
@ -212,10 +208,10 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm
*/ */
if ($update) { if ($update) {
$overlapping = Application_Model_Schedule::checkOverlappingShows( $overlapping = Application_Model_Schedule::checkOverlappingShows(
$show_start, $show_end, $update, null, $formData["add_show_id"]); $showStartDateTime, $showEndDateTime, $update, null, $formData["add_show_id"]);
} else { } else {
$overlapping = Application_Model_Schedule::checkOverlappingShows( $overlapping = Application_Model_Schedule::checkOverlappingShows(
$show_start, $show_end); $showStartDateTime, $showEndDateTime);
} }
/* Check if repeats overlap with previously scheduled shows /* Check if repeats overlap with previously scheduled shows
@ -228,8 +224,8 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm
} }
foreach ($formData["add_show_day_check"] as $day) { foreach ($formData["add_show_day_check"] as $day) {
$repeatShowStart = clone $show_start; $repeatShowStart = clone $showStartDateTime;
$repeatShowEnd = clone $show_end; $repeatShowEnd = clone $showEndDateTime;
$daysAdd=0; $daysAdd=0;
if ($startDow !== $day) { if ($startDow !== $day) {
if ($startDow > $day) if ($startDow > $day)
@ -298,7 +294,7 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm
/* Check first show /* Check first show
* Continue if the first show does not overlap * Continue if the first show does not overlap
*/ */
$overlapping = Application_Model_Schedule::checkOverlappingShows($show_start, $show_end, $update, $instanceId); $overlapping = Application_Model_Schedule::checkOverlappingShows($showStartDateTime, $showEndDateTime, $update, $instanceId);
if (!$overlapping) { if (!$overlapping) {
$durationToAdd = "PT".$hours."H".$minutes."M"; $durationToAdd = "PT".$hours."H".$minutes."M";
@ -325,7 +321,7 @@ class Application_Form_AddShowWhen extends Zend_Form_SubForm
$this->getElement('add_show_duration')->setErrors(array(_('Cannot schedule overlapping shows'))); $this->getElement('add_show_duration')->setErrors(array(_('Cannot schedule overlapping shows')));
} }
} else { } else {
$overlapping = Application_Model_Schedule::checkOverlappingShows($show_start, $show_end, $update, $instanceId); $overlapping = Application_Model_Schedule::checkOverlappingShows($showStartDateTime, $showEndDateTime, $update, $instanceId);
if ($overlapping) { if ($overlapping) {
$this->getElement('add_show_duration')->setErrors(array(_('Cannot schedule overlapping shows'))); $this->getElement('add_show_duration')->setErrors(array(_('Cannot schedule overlapping shows')));
$valid = false; $valid = false;