Bugfixes to the latest changes

main
Yasen Pramatarov 2024-09-13 13:04:15 +03:00
parent 7cc8da562d
commit e0eee38726
8 changed files with 68 additions and 16 deletions

View File

@ -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']
]);

View File

@ -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;
}
?>

View File

@ -1,13 +1,6 @@
<?php
$action = $_REQUEST['action'] ?? '';
//require '../app/classes/user.php';
//
//$userObject = new User($dbWeb);
//
//$user_id = $userObject->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;

View File

@ -4,10 +4,7 @@
<div class="col-md-3 sidebar-wrapper bg-light" id="sidebar">
<div class="text-center" style="border: 1px solid #0dcaf0; height: 22px;" id="time_now">
<?php
$userTimezone = 'Europe/Sofia';
//$userTimezone = 'UTC';
$timezone = isset($userTimezone) ? $userTimezone : 'UTC'; // Default to UTC if no timezone is set
$timeNow = new DateTime('now', new DateTimeZone($timezone));
$timeNow = new DateTime('now', new DateTimeZone($userTimezone));
?>
<!--span style="vertical-align: top; font-size: 12px;"><?= $timeNow->format('d M Y H:i'); ?> <?= $userTimezone ?></span-->
<span style="vertical-align: top; font-size: 12px;"><?= $timeNow->format('H:i'); ?>&nbsp;&nbsp;<?= $userTimezone ?></span>

View File

@ -58,6 +58,21 @@
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="timezone" class="form-label"><small>timezone:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<select class="form-control" name="timezone" id="timezone">
<?php foreach ($allTimezones as $timezone) { ?>
<option value="<?= $timezone ?>" <?= $timezone === $userTimezone ? 'selected' : '' ?>>
<?= $timezone ?>&nbsp;&nbsp;(<?= getUTCOffset($timezone) ?>)
</option>
<?php } ?>
</select>
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="bio" class="form-label"><small>bio:</small></label>
@ -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 = <?php echo json_encode($isTimezoneSet); ?>; // Pass PHP flag to JavaScript
// If timezone is not set, run setTimezone()
if (!isTimezoneSet) {
setTimezone();
}
};
</script>

View File

@ -42,6 +42,17 @@
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label class="form-label"><small>timezone:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<?php if (isset($userDetails[0]['timezone'])) { ?>
<?= $userDetails[0]['timezone'] ?>&nbsp;&nbsp;<span style="font-size: 0.66em;">(<?= getUTCOffset($userDetails[0]['timezone']) ?>)</span>
<?php } ?>
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label class="form-label"><small>bio:</small></label>

View File

@ -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)

View File

@ -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)) {