Adds basic profile editing

main
Yasen Pramatarov 2024-09-08 13:48:21 +03:00
parent 06a534c2da
commit f0f7d5b2d3
5 changed files with 117 additions and 65 deletions

View File

@ -45,6 +45,7 @@ class User {
return $query->fetchAll(PDO::FETCH_ASSOC);
}
// get user details
public function getUserDetails($username) {
$sql = 'SELECT * FROM users_meta um
@ -60,6 +61,30 @@ class User {
}
// update an existing user
public function editUser($user_id, $updatedUser) {
try {
$sql = 'UPDATE users_meta SET
name = :name,
email = :email,
bio = :bio
WHERE user_id = :user_id';
$query = $this->db->prepare($sql);
$query->execute([
':user_id' => $user_id,
':name' => $updatedUser['name'],
':email' => $updatedUser['email'],
':bio' => $updatedUser['bio']
]);
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
}
?>

View File

@ -5,9 +5,33 @@ require '../app/classes/user.php';
$userObject = new User($dbWeb);
$userDetails = $userObject->getUserDetails($user);
// if a form is submitted, it's from the edit page
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
switch ($action) {
$user_id = $userObject->getUserId($user)[0]['id'];
// update the profile
$updatedUser = [
'name' => $_POST['name'] ?? '',
'email' => $_POST['email'] ?? '',
// 'avatar' => ,
'bio' => $_POST['bio'] ?? '',
];
$result = $userObject->editUser($user_id, $updatedUser);
if ($result === true) {
$_SESSION['notice'] = "User details for \"{$updatedUser['name']}\" are edited.";
} else {
$_SESSION['error'] = "Editing the user details failed. Error: $result";
}
header("Location: $app_root?page=profile");
exit();
// no form submitted, show the templates
} else {
$userDetails = $userObject->getUserDetails($user);
switch ($action) {
case 'edit':
include '../app/templates/profile-edit.php';
@ -15,6 +39,7 @@ switch ($action) {
default:
include '../app/templates/profile.php';
}
}
?>

View File

@ -1,5 +1,5 @@
<!-- widget "agents" -->
<!-- agents -->
<div class="card text-center w-50 mx-auto">
<p class="h4 card-header">Jilo Agent configuration for Jitsi platform <strong>"<?= htmlspecialchars($platformDetails[0]['name']) ?>"</strong></p>
<div class="card-body">
@ -47,4 +47,4 @@
</form>
</div>
</div>
<!-- /widget "agents" -->
<!-- /agents -->

View File

@ -5,6 +5,8 @@
<p class="h4 card-header">Profile of <?= $userDetails[0]['username'] ?></p>
<div class="card-body">
<form method="POST" action="<?= $app_root ?>?page=profile">
<div class="row">
<p class="border rounded bg-light mb-4"><small>edit the profile fields</small></p>
@ -14,7 +16,7 @@
<div class="col-md-8">
<div class="row mb-3">
<!--div class="row mb-3">
<div class="col-md-4 text-end">
<label for="username" class="form-label"><small>username:</small></label>
<span class="text-danger" style="margin-right: -12px;">*</span>
@ -22,7 +24,7 @@
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="username" value="<?= $userDetails[0]['username'] ?>" required />
</div>
</div>
</div-->
<div class="row mb-3">
<div class="col-md-4 text-end">
@ -63,12 +65,12 @@
</div>
<p>
<a href="<?= $app_root ?>?page=profile" class="btn btn-primary">Cancel</a>
<a href="<?= $app_root ?>?page=profile&action=edit" class="btn btn-danger">Save</a>
<a href="<?= $app_root ?>?page=profile" class="btn btn-secondary">Cancel</a>
<input type="submit" class="btn btn-primary" value="Save" />
</p>
</div>
</form>
</div>
</div>
<!-- /user profile -->

View File

@ -13,14 +13,14 @@
<div class="col-md-8">
<div class="row mb-3">
<!--div class="row mb-3">
<div class="col-md-4 text-end">
<label class="form-label"><small>username:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<?= $userDetails[0]['username'] ?>
</div>
</div>
</div-->
<div class="row mb-3">
<div class="col-md-4 text-end">