36018-vm/profile.php
2025-11-22 14:03:37 +00:00

140 lines
6.1 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$profileId = $_GET['id'] ?? null;
if (!$profileId) {
header("Location: index.php");
exit();
}
$pdo = db();
// Handle WhatsApp number update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user_id']) && $_SESSION['user_id'] == $profileId) {
$whatsapp_number = $_POST['whatsapp_number'] ?? '';
try {
$stmt = $pdo->prepare('UPDATE users SET whatsapp_number = :whatsapp_number WHERE id = :id');
$stmt->execute([':whatsapp_number' => $whatsapp_number, ':id' => $profileId]);
header("Location: profile.php?id=$profileId&message=whatsapp_updated");
exit();
} catch (PDOException $e) {
$update_error = "Failed to update WhatsApp number.";
}
}
// Fetch user information
try {
$stmt = $pdo->prepare('SELECT id, full_name, email, created_at, whatsapp_number FROM users WHERE id = :id');
$stmt->execute([':id' => $profileId]);
$profileUser = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: Could not retrieve user profile.");
}
if (!$profileUser) {
// Handle user not found
header("Location: index.php?message=user_not_found");
exit();
}
// Fetch tasks posted by the user
try {
$stmt = $pdo->prepare('SELECT * FROM tasks WHERE user_id = :user_id ORDER BY created_at DESC');
$stmt->execute([':user_id' => $profileId]);
$postedTasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: Could not retrieve posted tasks.");
}
// Fetch tasks the user has completed (i.e., their application was accepted)
try {
$stmt = $pdo->prepare(
'SELECT t.* FROM tasks t JOIN applications a ON t.id = a.task_id WHERE a.user_id = :user_id AND a.status = \'accepted\' ORDER BY t.created_at DESC'
);
$stmt->execute([':user_id' => $profileId]);
$completedTasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: Could not retrieve completed tasks.");
}
$pageTitle = htmlspecialchars($profileUser['full_name']) . "'s Profile";
include 'shared/header.php';
?>
<div class="container profile-page">
<div class="page-header">
<h1><?php echo htmlspecialchars($profileUser['full_name']); ?></h1>
<p><strong>Member Since:</strong> <?php echo date('F j, Y', strtotime($profileUser['created_at'])); ?></p>
</div>
<?php if (isset($_GET['message']) && $_GET['message'] === 'whatsapp_updated'): ?>
<div class="alert alert-success">Your WhatsApp number has been updated successfully.</div>
<?php endif; ?>
<?php if (isset($update_error)): ?>
<div class="alert alert-danger"><?= htmlspecialchars($update_error) ?></div>
<?php endif; ?>
<?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == $profileUser['id']): ?>
<div class="card mb-4" style="margin-bottom: 2rem; background-color: #fff; border: 1px solid #dee2e6; border-radius: 0.75rem; padding: 1.5rem; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
<div class="card-body">
<h2 class="card-title" style="font-size: 1.75rem; margin-bottom: 1.5rem; border-bottom: 2px solid #dee2e6; padding-bottom: 0.5rem; color: #0d6efd;">My Contact Information</h2>
<form action="profile.php?id=<?= $profileUser['id'] ?>" method="POST">
<div class="form-group">
<label for="whatsapp_number">WhatsApp Number</label>
<input type="text" class="form-control" id="whatsapp_number" name="whatsapp_number" value="<?= htmlspecialchars($profileUser['whatsapp_number'] ?? '') ?>" placeholder="Enter your WhatsApp number...">
<small class="form-text text-muted">This will only be shared with users whose applications you accept.</small>
</div>
<button type="submit" class="btn btn-primary" style="margin-top: 1rem;">Save</button>
</form>
</div>
</div>
<?php endif; ?>
<div class="profile-section">
<h2>Posted Tasks</h2>
<?php if (empty($postedTasks)): ?>
<p><?php echo htmlspecialchars($profileUser['full_name']); ?> has not posted any tasks yet.</p>
<?php else: ?>
<div class="task-list">
<?php foreach ($postedTasks as $task):
// Check if task status is not null before using it in strtolower
$taskStatus = $task['status'] ?? 'unknown';
?>
<div class="task-card-profile">
<h4><a href="task-details.php?id=<?php echo $task['id']; ?>"><?php echo htmlspecialchars($task['title']); ?></a></h4>
<p><strong>Status:</strong> <span class="status-badge status-<?php echo strtolower(htmlspecialchars($taskStatus)); ?>"><?php echo htmlspecialchars($taskStatus); ?></span></p>
<p><strong>Payout:</strong> $<?php echo htmlspecialchars($task['payout']); ?></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="profile-section">
<h2>Completed Tasks</h2>
<?php if (empty($completedTasks)): ?>
<p><?php echo htmlspecialchars($profileUser['full_name']); ?> has not completed any tasks yet.</p>
<?php else: ?>
<div class="task-list">
<?php foreach ($completedTasks as $task):
// Check if task status is not null before using it in strtolower
$taskStatus = $task['status'] ?? 'unknown';
?>
<div class="task-card-profile">
<h4><?php echo htmlspecialchars($task['title']); ?></h4>
<p><strong>Status:</strong> <span class="status-badge status-<?php echo strtolower(htmlspecialchars($taskStatus)); ?>"><?php echo htmlspecialchars($taskStatus); ?></span></p>
<p><strong>Payout:</strong> $<?php echo htmlspecialchars($task['payout']); ?></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php include 'shared/footer.php'; ?>