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,16 +5,41 @@ 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'];
case 'edit':
include '../app/templates/profile-edit.php';
break;
// 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";
}
default:
include '../app/templates/profile.php';
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';
break;
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,70 +5,72 @@
<p class="h4 card-header">Profile of <?= $userDetails[0]['username'] ?></p>
<div class="card-body">
<div class="row">
<p class="border rounded bg-light mb-4"><small>edit the profile fields</small></p>
<form method="POST" action="<?= $app_root ?>?page=profile">
<div class="col-md-4">
<div class="border" style="width: 200px; height: 200px;"><img src="" alt="avatar" /></div>
</div>
<div class="row">
<p class="border rounded bg-light mb-4"><small>edit the profile fields</small></p>
<div class="col-md-8">
<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>
</div>
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="username" value="<?= $userDetails[0]['username'] ?>" required />
</div>
<div class="col-md-4">
<div class="border" style="width: 200px; height: 200px;"><img src="" alt="avatar" /></div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="name" class="form-label"><small>name:</small></label>
<div class="col-md-8">
<!--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>
</div>
<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 class="row mb-3">
<div class="col-md-4 text-end">
<label for="name" class="form-label"><small>name:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="name" value="<?= $userDetails[0]['name'] ?>" />
</div>
</div>
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="name" value="<?= $userDetails[0]['name'] ?>" />
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="email" class="form-label"><small>email:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="email" value="<?= $userDetails[0]['email'] ?>" />
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="bio" class="form-label"><small>bio:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<textarea class="form-control" name="bio" rows="10"><?= $userDetails[0]['bio'] ?? '' ?></textarea>
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="rights" class="form-label"><small>rights:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="rights" value="<?= $userDetails[0]['rights'] ?? '' ?>" />
</div>
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="email" class="form-label"><small>email:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="email" value="<?= $userDetails[0]['email'] ?>" />
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="bio" class="form-label"><small>bio:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<textarea class="form-control" name="bio" rows="10"><?= $userDetails[0]['bio'] ?? '' ?></textarea>
</div>
</div>
<div class="row mb-3">
<div class="col-md-4 text-end">
<label for="rights" class="form-label"><small>rights:</small></label>
</div>
<div class="col-md-8 text-start bg-light">
<input class="form-control" type="text" name="rights" value="<?= $userDetails[0]['rights'] ?? '' ?>" />
</div>
</div>
<p>
<a href="<?= $app_root ?>?page=profile" class="btn btn-secondary">Cancel</a>
<input type="submit" class="btn btn-primary" value="Save" />
</p>
</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>
</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">