From e0eee38726ded6d72b92caea2e3366cf0815a971 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Fri, 13 Sep 2024 13:04:15 +0300 Subject: [PATCH] Bugfixes to the latest changes --- app/classes/user.php | 2 ++ app/helpers/profile.php | 15 +++++++++----- app/pages/profile.php | 11 ++++------ app/templates/page-sidebar.php | 5 +---- app/templates/profile-edit.php | 38 ++++++++++++++++++++++++++++++++++ app/templates/profile.php | 11 ++++++++++ doc/jilo-web.schema | 1 + public_html/index.php | 1 + 8 files changed, 68 insertions(+), 16 deletions(-) diff --git a/app/classes/user.php b/app/classes/user.php index ed6120a..9c2e11e 100644 --- a/app/classes/user.php +++ b/app/classes/user.php @@ -191,6 +191,7 @@ class User { $sql = 'UPDATE users_meta SET name = :name, email = :email, + timezone = :timezone, bio = :bio WHERE user_id = :user_id'; $query = $this->db->prepare($sql); @@ -198,6 +199,7 @@ class User { ':user_id' => $user_id, ':name' => $updatedUser['name'], ':email' => $updatedUser['email'], + ':timezone' => $updatedUser['timezone'], ':bio' => $updatedUser['bio'] ]); diff --git a/app/helpers/profile.php b/app/helpers/profile.php index c6f4ba8..ad4f53c 100644 --- a/app/helpers/profile.php +++ b/app/helpers/profile.php @@ -2,14 +2,19 @@ // get the UTC offset of a specified timezone function getUTCOffset($timezone) { - $datetime = new DateTime("now", new DateTimeZone($timezone)); - $offsetInSeconds = $datetime->getOffset(); + $formattedOffset = ''; + if (isset($timezone)) { - $hours = intdiv($offsetInSeconds, 3600); - $minutes = ($offsetInSeconds % 3600) / 60; - $formattedOffset = sprintf("UTC%+03d:%02d", $hours, $minutes); // Format UTC+01:00 + $datetime = new DateTime("now", new DateTimeZone($timezone)); + $offsetInSeconds = $datetime->getOffset(); + + $hours = intdiv($offsetInSeconds, 3600); + $minutes = ($offsetInSeconds % 3600) / 60; + $formattedOffset = sprintf("UTC%+03d:%02d", $hours, $minutes); // Format UTC+01:00 + } return $formattedOffset; + } ?> diff --git a/app/pages/profile.php b/app/pages/profile.php index ef944e1..879cc57 100644 --- a/app/pages/profile.php +++ b/app/pages/profile.php @@ -1,13 +1,6 @@ getUserId($user)[0]['id']; -//$userDetails = $userObject->getUserDetails($user_id); -//$userRights = $userObject->getUserRights($user_id); // if a form is submitted, it's from the edit page if ($_SERVER['REQUEST_METHOD'] == 'POST') { @@ -31,6 +24,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $updatedUser = [ 'name' => $_POST['name'] ?? '', 'email' => $_POST['email'] ?? '', + 'timezone' => $_POST['timezone'] ?? '', 'bio' => $_POST['bio'] ?? '', ]; $result = $userObject->editUser($user_id, $updatedUser); @@ -76,6 +70,9 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { case 'edit': $allRights = $userObject->getAllRights(); + $allTimezones = timezone_identifiers_list(); + // if timezone is already set, we pass a flag for JS to not autodetect browser timezone + $isTimezoneSet = !empty($userDetails[0]['timezone']); include '../app/templates/profile-edit.php'; break; diff --git a/app/templates/page-sidebar.php b/app/templates/page-sidebar.php index cc40e0f..a9dc145 100644 --- a/app/templates/page-sidebar.php +++ b/app/templates/page-sidebar.php @@ -4,10 +4,7 @@ +
+
+ +
+
+ +
+
+
@@ -160,4 +175,27 @@ document.getElementById('confirm-delete').addEventListener('click', function(eve document.getElementById('remove-avatar-form').submit(); }); +// Function to detect user's timezone and select it in the dropdown +function setTimezone() { + const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; + const timezoneSelect = document.getElementById("timezone"); + timezoneSelect.className = 'form-control border border-danger'; + + // Loop through the options to find and select the user's timezone + for (let i = 0; i < timezoneSelect.options.length; i++) { + if (timezoneSelect.options[i].value === userTimezone) { + timezoneSelect.selectedIndex = i; + break; + } + } +} +// Run the function on page load +window.onload = function() { + const isTimezoneSet = ; // Pass PHP flag to JavaScript + // If timezone is not set, run setTimezone() + if (!isTimezoneSet) { + setTimezone(); + } +}; + diff --git a/app/templates/profile.php b/app/templates/profile.php index 7f84350..ad31f97 100644 --- a/app/templates/profile.php +++ b/app/templates/profile.php @@ -42,6 +42,17 @@
+
+
+ +
+
+ +   () + +
+
+
diff --git a/doc/jilo-web.schema b/doc/jilo-web.schema index 69073ad..351e74c 100644 --- a/doc/jilo-web.schema +++ b/doc/jilo-web.schema @@ -9,6 +9,7 @@ CREATE TABLE users_meta ( user_id INTEGER NOT NULL, name TEXT, email TEXT, + timezone TEXT, avatar TEXT, bio TEXT, FOREIGN KEY (user_id) REFERENCES users(id) diff --git a/public_html/index.php b/public_html/index.php index 857a16b..c35d815 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -138,6 +138,7 @@ if ($page == 'logout') { $user_id = $userObject->getUserId($user)[0]['id']; $userDetails = $userObject->getUserDetails($user_id); $userRights = $userObject->getUserRights($user_id); + $userTimezone = isset($userDetails[0]['timezone']) ? $userDetails[0]['timezone'] : 'UTC'; // Default to UTC if no timezone is set // page building if (in_array($page, $allowed_urls)) {