From 22ab6cc95e12680bcde2fa07e4092e8608a8d91c Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Tue, 5 Dec 2023 16:47:47 -0300 Subject: [PATCH] Prevent error when user timezone is null Use server timezone when the use haven't a timezone Signed-off-by: Vitor Mattos --- Repository/LockdownRepository.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Repository/LockdownRepository.php b/Repository/LockdownRepository.php index 2f4aa21..7b1ec74 100644 --- a/Repository/LockdownRepository.php +++ b/Repository/LockdownRepository.php @@ -42,7 +42,11 @@ public function updateLockWeek(?Approval $approval, ApprovalRepository $approval $endDate = $this->getOldestNotSubmittedDate($allWeeks, $user); $endDate = $this->getOldestSubmittedApprovalFromTeam($user, $approvalRepository, $endDate); - $graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($user->getPreferenceValue('timezone'))); + $userTimezone = $user->getPreferenceValue('timezone'); + if (empty($userTimezone)) { + $userTimezone = $_SERVER['TIMEZONE']; + } + $graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($userTimezone)); $endDate->modify('midnight')->modify('+1 day')->modify('-1 second'); $this->updateLockPreference($user, $endDate, $graceDate); } @@ -66,7 +70,11 @@ private function updatePreference(User $user, string $preferenceName, string $va */ private function getOldestNotSubmittedDate(array $allWeeks, User $user): DateTime { - $timezone = new DateTimeZone($user->getPreferenceValue('timezone')); + $userTimezone = $user->getPreferenceValue('timezone'); + if (empty($userTimezone)) { + $userTimezone = $_SERVER['TIMEZONE']; + } + $timezone = new DateTimeZone($userTimezone); foreach ($allWeeks as $week) { if ($week['status'] === 'not_submitted') { @@ -87,9 +95,13 @@ private function getOldestNotSubmittedDate(array $allWeeks, User $user): DateTim private function updateLockPreference(User $user, $endDate, $graceDate): void { + $userTimezone = $user->getPreferenceValue('timezone'); + if (empty($userTimezone)) { + $userTimezone = $_SERVER['TIMEZONE']; + } $this->updatePreference($user, self::LOCKDOWN_PERIOD_START, '0000-01-01 00:00:01'); $this->updatePreference($user, self::LOCKDOWN_PERIOD_END, $endDate->format('Y-m-d H:i:s')); - $this->updatePreference($user, self::LOCKDOWN_PERIOD_TIMEZONE, $user->getPreferenceValue('timezone')); + $this->updatePreference($user, self::LOCKDOWN_PERIOD_TIMEZONE, $userTimezone); $this->updatePreference($user, self::LOCKDOWN_GRACE_PERIOD, $graceDate->format('Y-m-d H:i:s')); } @@ -123,7 +135,11 @@ private function updateTeamleadersLock(User $user, ApprovalRepository $approvalR $endDate = $this->getOldestNotSubmittedDate($weeks, $teamLeader); $endDate = $this->getOldestSubmittedApprovalFromTeam($teamLeader, $approvalRepository, $endDate); - $graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($user->getPreferenceValue('timezone'))); + $userTimezone = $user->getPreferenceValue('timezone'); + if (empty($userTimezone)) { + $userTimezone = $_SERVER['TIMEZONE']; + } + $graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($userTimezone)); $endDate->modify('midnight')->modify('+1 day')->modify('-1 second'); $this->updateLockPreference($teamLeader, $endDate, $graceDate); }