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

180 lines
5.3 KiB
PHP

<?php
$pageTitle = 'Welcome to CashTask';
require_once __DIR__ . '/shared/header.php';
require_once __DIR__ . '/db/config.php';
$message = $_GET['message'] ?? null;
$message_map = [
'already_applied' => [
'text' => 'You have already applied for this task.',
'type' => 'warning'
],
'owner_cannot_apply' => [
'text' => 'You cannot apply for your own task.',
'type' => 'warning'
],
'applied_successfully' => [
'text' => 'You have successfully applied for the task.',
'type' => 'success'
],
'application_failed' => [
'text' => 'There was an error submitting your application.',
'type' => 'danger'
]
];
// Fetch tasks from the database
try {
$search = $_GET['search'] ?? '';
$category = $_GET['category'] ?? '';
$location = $_GET['location'] ?? '';
$sql = "SELECT t.*, u.id as user_id_poster, u.full_name as user_name FROM tasks t JOIN users u ON t.user_id = u.id WHERE t.status = 'open'";
$params = [];
if (!empty($search)) {
$sql .= " AND (t.title LIKE :search OR t.description LIKE :search)";
$params[':search'] = '%' . $search . '%';
}
if (!empty($category)) {
$sql .= " AND t.category LIKE :category";
$params[':category'] = '%' . $category . '%';
}
if (!empty($location)) {
$sql .= " AND t.location LIKE :location";
$params[':location'] = '%' . $location . '%';
}
$sql .= " ORDER BY t.created_at DESC";
$stmt = db()->prepare($sql);
$stmt->execute($params);
$tasks = $stmt->fetchAll();
} catch (PDOException $e) {
// Handle database error gracefully
error_log($e->getMessage());
$tasks = [];
}
?>
<style>
.alert {
padding: 1rem;
margin-bottom: 1rem;
border-radius: 0.25rem;
}
.alert-success {
color: #155724;
background-color: #d4edda;
border-color: #c3e6cb;
}
.alert-warning {
color: #856404;
background-color: #fff3cd;
border-color: #ffeeba;
}
.alert-danger {
color: #721c24;
background-color: #f8d7da;
border-color: #f5c6cb;
}
.search-form {
display: flex;
gap: 1rem;
margin-bottom: 2rem;
}
.search-form input {
flex-grow: 1;
padding: 0.75rem;
border: 1px solid #ced4da;
border-radius: 0.25rem;
}
.task-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1.5rem;
}
.task-card {
background-color: #ffffff;
border-radius: 0.75rem;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
padding: 1.5rem;
transition: all 0.2s ease-in-out;
}
.task-card:hover {
transform: translateY(-4px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}
.task-card h3 {
margin-top: 0;
font-size: 1.25rem;
}
.task-meta {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 1rem;
color: #6c757d;
}
.task-payout {
font-size: 1.5rem;
font-weight: 700;
color: #14b8a6;
}
</style>
<?php if (isset($message_map[$message])): ?>
<div class="alert alert-<?= $message_map[$message]['type'] ?>">
<?= htmlspecialchars($message_map[$message]['text']) ?>
</div>
<?php endif; ?>
<div class="hero-section" style="text-align: center; padding: 4rem 0;">
<h1 style="font-size: 3rem; font-weight: 700;">Get anything done, today.</h1>
<p style="font-size: 1.25rem; color: #6c757d; margin-bottom: 2rem;">The best place to find quick help for your daily tasks.</p>
<a href="/signup.php" class="btn btn-primary">Post a Task</a>
</div>
<h2>Available Tasks</h2>
<form action="index.php" method="GET" class="search-form">
<input type="text" name="search" placeholder="Keywords..." value="<?= htmlspecialchars($_GET['search'] ?? '') ?>">
<input type="text" name="category" placeholder="Category..." value="<?= htmlspecialchars($_GET['category'] ?? '') ?>">
<input type="text" name="location" placeholder="Location..." value="<?= htmlspecialchars($_GET['location'] ?? '') ?>">
<button type="submit" class="btn">Search</button>
</form>
<div class="task-grid">
<?php if (empty($tasks)): ?>
<p>No tasks found matching your criteria. Try broadening your search!</p>
<?php else: ?>
<?php foreach ($tasks as $task): ?>
<div class="task-card">
<h3><a href="task-details.php?id=<?= $task['id'] ?>"><?= htmlspecialchars($task['title']) ?></a></h3>
<div class="task-meta">
<span><?= htmlspecialchars($task['category']) ?> &bull; <?= htmlspecialchars($task['location']) ?></span>
<span>Posted by: <a href="profile.php?id=<?= $task['user_id_poster'] ?>"><strong><?= htmlspecialchars($task['user_name']) ?></strong></a></span>
</div>
<div class="task-payout">$<?= htmlspecialchars((string)$task['payout']) ?></div>
<?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != $task['user_id']): ?>
<form action="apply.php" method="POST" style="margin-top: 1rem;">
<input type="hidden" name="task_id" value="<?= $task['id'] ?>">
<button type="submit" class="btn btn-primary">Apply</button>
</form>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php
require_once __DIR__ . '/shared/footer.php';
?>