jilo-web/app/pages/profile.php

88 lines
2.7 KiB
PHP
Raw Normal View History

2024-08-12 11:12:24 +00:00
<?php
2024-09-07 22:36:57 +00:00
$action = $_REQUEST['action'] ?? '';
2024-09-07 20:05:22 +00:00
require '../app/classes/user.php';
$userObject = new User($dbWeb);
2024-09-11 13:21:05 +00:00
$user_id = $userObject->getUserId($user)[0]['id'];
$userDetails = $userObject->getUserDetails($user_id);
$userRights = $userObject->getUserRights($user_id);
2024-09-09 12:20:21 +00:00
2024-09-08 10:48:21 +00:00
// if a form is submitted, it's from the edit page
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
2024-09-07 20:05:22 +00:00
2024-09-09 12:20:21 +00:00
$item = $_REQUEST['item'] ?? '';
2024-09-10 11:05:38 +00:00
// avatar removal
if ($item === 'avatar' && $action === 'remove') {
$result = $userObject->removeAvatar($user_id, $config['avatars_path'].$userDetails[0]['avatar']);
if ($result === true) {
2024-09-11 13:21:05 +00:00
$_SESSION['notice'] .= "Avatar for user \"{$userDetails[0]['username']}\" is removed. ";
2024-09-10 11:05:38 +00:00
} else {
$_SESSION['error'] .= "Removing the avatar failed. Error: $result ";
2024-09-09 12:20:21 +00:00
}
header("Location: $app_root?page=profile");
exit();
}
2024-09-08 10:48:21 +00:00
// update the profile
$updatedUser = [
'name' => $_POST['name'] ?? '',
'email' => $_POST['email'] ?? '',
'bio' => $_POST['bio'] ?? '',
];
2024-09-09 12:20:21 +00:00
$result = $userObject->editUser($user_id, $updatedUser);
if ($result === true) {
2024-09-10 11:05:38 +00:00
$_SESSION['notice'] .= "User details for \"{$updatedUser['name']}\" are edited. ";
2024-09-09 12:20:21 +00:00
} else {
2024-09-10 11:05:38 +00:00
$_SESSION['error'] .= "Editing the user details failed. Error: $result ";
}
2024-09-11 19:51:46 +00:00
// update the rights
$newRights = $_POST['rights'] ?? array();
// extract the new right_ids
$userRightsIds = array_column($userRights, 'right_id');
// what rights we need to add
$rightsToAdd = array_diff($newRights, $userRightsIds);
if (!empty($rightsToAdd)) {
foreach ($rightsToAdd as $rightId) {
$userObject->addUserRight($user_id, $rightId);
}
}
// what rights we need to remove
$rightsToRemove = array_diff($userRightsIds, $newRights);
if (!empty($rightsToRemove)) {
foreach ($rightsToRemove as $rightId) {
$userObject->removeUserRight($user_id, $rightId);
}
}
2024-09-10 11:05:38 +00:00
// update the avatar
if (!empty($_FILES['avatar_file']['tmp_name'])) {
$result = $userObject->changeAvatar($user_id, $_FILES['avatar_file'], $config['avatars_path']);
2024-09-09 12:20:21 +00:00
}
2024-09-07 22:36:57 +00:00
2024-09-08 10:48:21 +00:00
header("Location: $app_root?page=profile");
exit();
// no form submitted, show the templates
} else {
2024-09-09 12:20:21 +00:00
$avatar = !empty($userDetails[0]['avatar']) ? $config['avatars_path'] . $userDetails[0]['avatar'] : $config['default_avatar'];
2024-09-09 12:54:32 +00:00
$default_avatar = empty($userDetails[0]['avatar']) ? true : false;
2024-09-08 10:48:21 +00:00
switch ($action) {
case 'edit':
2024-09-11 19:51:46 +00:00
$allRights = $userObject->getAllRights();
2024-09-08 10:48:21 +00:00
include '../app/templates/profile-edit.php';
break;
default:
include '../app/templates/profile.php';
}
2024-09-07 22:36:57 +00:00
}
2024-08-12 11:12:24 +00:00
?>