161 lines
6.9 KiB
PHP
161 lines
6.9 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
$errors = [];
|
|
$success = false;
|
|
|
|
// Fetch categories and subcategories for the form
|
|
$categories = $pdo->query("SELECT * FROM categories WHERE visibility = 1 ORDER BY display_order ASC, name ASC")->fetchAll();
|
|
$subcategories = [];
|
|
if (!empty($categories)) {
|
|
$stmt = $pdo->query("SELECT * FROM subcategories ORDER BY name ASC");
|
|
while ($row = $stmt->fetch()) {
|
|
$subcategories[$row['category_id']][] = $row;
|
|
}
|
|
}
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$title = trim($_POST['title'] ?? '');
|
|
$url = trim($_POST['url'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
$subcategory_id = $_POST['subcategory_id'] ?? null;
|
|
|
|
if (empty($title)) $errors[] = 'Title is required.';
|
|
if (empty($url)) $errors[] = 'URL is required.';
|
|
if (!filter_var($url, FILTER_VALIDATE_URL)) $errors[] = 'Invalid URL.';
|
|
if (empty($subcategory_id)) $errors[] = 'Subcategory is required.';
|
|
|
|
if (empty($errors)) {
|
|
// Determine status based on user role
|
|
$status = ($_SESSION['user_role'] === 'admin' || $_SESSION['user_role'] === 'power_user') ? 'approved' : 'pending';
|
|
|
|
// For now, thumbnail is not implemented
|
|
$thumbnail_url = null;
|
|
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO links (user_id, subcategory_id, title, url, description, thumbnail_url, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$_SESSION['user_id'], $subcategory_id, $title, $url, $description, $thumbnail_url, $status]);
|
|
$success = true;
|
|
} catch (PDOException $e) {
|
|
$errors[] = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Submit a Link - <?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Web Directory'); ?></title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<header class="header">
|
|
<h1><a href="/" style="text-decoration: none; color: inherit;"><?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Web Directory'); ?></a></h1>
|
|
<div class="auth-links">
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<a href="submit.php">Submit Link</a>
|
|
<a href="logout.php">Logout</a>
|
|
<?php else: ?>
|
|
<a href="register.php">Register</a>
|
|
<a href="login.php">Login</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="container my-4">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<main class="content p-4">
|
|
<h2>Submit a New Link</h2>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php foreach ($errors as $error): ?><p class="mb-0"><?php echo $error; ?></p><?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success">
|
|
<p class="mb-0">Thank you for your submission! It will be reviewed shortly.</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<form action="submit.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="title" class="form-label">Title</label>
|
|
<input type="text" class="form-control" id="title" name="title" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="url" class="form-label">URL</label>
|
|
<input type="url" class="form-control" id="url" name="url" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="category" class="form-label">Category</label>
|
|
<select class="form-select" id="category" name="category">
|
|
<option selected disabled>-- Select a Category --</option>
|
|
<?php foreach($categories as $cat): ?>
|
|
<option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="subcategory_id" class="form-label">Subcategory</label>
|
|
<select class="form-select" id="subcategory_id" name="subcategory_id" required>
|
|
<option selected disabled>-- Select a Subcategory --</option>
|
|
<?php foreach($subcategories as $cat_id => $subs): ?>
|
|
<?php foreach($subs as $sub): ?>
|
|
<option class="d-none" data-category="<?php echo $cat_id; ?>" value="<?php echo $sub['id']; ?>"><?php echo htmlspecialchars($sub['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Submit Link</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer class="footer">
|
|
<p>© <?php echo date("Y"); ?> <?php echo htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Web Directory'); ?>. All Rights Reserved.</p>
|
|
</footer>
|
|
|
|
<script>
|
|
document.getElementById('category').addEventListener('change', function() {
|
|
const categoryId = this.value;
|
|
const subcategorySelect = document.getElementById('subcategory_id');
|
|
|
|
// Reset and show the default option
|
|
subcategorySelect.value = '-- Select a Subcategory --';
|
|
|
|
// Hide all subcategory options
|
|
Array.from(subcategorySelect.options).forEach(opt => {
|
|
if (opt.dataset.category) { // Skip the default disabled option
|
|
opt.classList.add('d-none');
|
|
}
|
|
});
|
|
|
|
// Show subcategories for the selected category
|
|
const relevantOptions = subcategorySelect.querySelectorAll(`[data-category="${categoryId}"]`);
|
|
relevantOptions.forEach(opt => opt.classList.remove('d-none'));
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|