This commit is contained in:
Flatlogic Bot 2025-12-09 09:59:27 +00:00
parent 9748568c17
commit 6859a96e73
9 changed files with 371 additions and 5 deletions

View File

@ -1,22 +1,25 @@
<?php
$current_page = basename($_SERVER['REQUEST_URI']);
?>
<!-- Sidebar -->
<div class="bg-white" id="sidebar-wrapper">
<div class="sidebar-heading text-center py-4 fs-4 fw-bold text-uppercase border-bottom">
<i class="bi bi-shield-lock me-2"></i>Admin Panel
</div>
<div class="list-group list-group-flush my-3">
<a href="/admin/" class="list-group-item list-group-item-action active">
<a href="/admin/" class="list-group-item list-group-item-action <?php echo ($current_page == 'admin' || $current_page == 'index.php') ? 'active' : ''; ?>">
<i class="bi bi-speedometer2 me-2"></i>Dashboard
</a>
<a href="#" class="list-group-item list-group-item-action">
<a href="#" class="list-group-item list-group-item-action <?php echo ($current_page == 'users.php') ? 'active' : ''; ?>">
<i class="bi bi-people me-2"></i>Manajemen User
</a>
<a href="#" class="list-group-item list-group-item-action">
<a href="products.php" class="list-group-item list-group-item-action <?php echo ($current_page == 'products.php') ? 'active' : ''; ?>">
<i class="bi bi-box-seam me-2"></i>Manajemen Produk
</a>
<a href="#" class="list-group-item list-group-item-action">
<a href="#" class="list-group-item list-group-item-action <?php echo ($current_page == 'orders.php') ? 'active' : ''; ?>">
<i class="bi bi-receipt me-2"></i>Manajemen Order
</a>
<a href="#" class="list-group-item list-group-item-action">
<a href="#" class="list-group-item list-group-item-action <?php echo ($current_page == 'settings.php') ? 'active' : ''; ?>">
<i class="bi bi-gear me-2"></i>Pengaturan
</a>
<a href="/" class="list-group-item list-group-item-action bg-light">

62
admin/product_add.php Normal file
View File

@ -0,0 +1,62 @@
<?php
// This page contains the form to add a new product.
// Include header
include 'partials/header.php';
?>
<div id="page-content-wrapper">
<nav class="navbar navbar-expand-lg navbar-light bg-transparent py-4 px-4">
<div class="d-flex align-items-center">
<i class="bi bi-list fs-4 me-3" id="menu-toggle"></i>
<h2 class="fs-2 m-0">Tambah Produk Baru</h2>
</div>
</nav>
<div class="container-fluid px-4">
<div class="row my-5">
<div class="col">
<div class="card shadow-sm">
<div class="card-body">
<form action="product_create.php" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="name" class="form-label">Nama Produk</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="price" class="form-label">Harga</label>
<div class="input-group">
<span class="input-group-text">Rp</span>
<input type="number" class="form-control" id="price" name="price" min="0" step="any" required>
</div>
</div>
<div class="mb-3">
<label for="duration" class="form-label">Durasi Pengerjaan</label>
<input type="text" class="form-control" id="duration" name="duration" placeholder="Contoh: 3-5 hari kerja" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Deskripsi Singkat</label>
<textarea class="form-control" id="description" name="description" rows="3" required></textarea>
</div>
<div class="mb-3">
<label for="features" class="form-label">Fitur-fitur (pisahkan dengan koma)</label>
<textarea class="form-control" id="features" name="features" rows="3" placeholder="Contoh: Domain .com, Hosting 2GB, SSL Gratis"></textarea>
</div>
<div class="mb-3">
<label for="thumbnail" class="form-label">Thumbnail</label>
<input class="form-control" type="file" id="thumbnail" name="thumbnail" accept="image/png, image/jpeg, image/gif">
</div>
<button type="submit" class="btn btn-primary">Simpan Produk</button>
<a href="products.php" class="btn btn-secondary">Batal</a>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
// Include footer
include 'partials/footer.php';
?>

61
admin/product_create.php Normal file
View File

@ -0,0 +1,61 @@
<?php
require_once '../db/config.php';
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// --- Basic Form Data ---
$name = trim($_POST['name']);
$price = trim($_POST['price']);
$duration = trim($_POST['duration']);
$description = trim($_POST['description']);
$features = trim($_POST['features']);
$thumbnail_url = null; // Default to null
// --- Thumbnail Upload Handling ---
if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] == UPLOAD_ERR_OK) {
$upload_dir = '../assets/uploads/products/';
// Create directory if it doesn't exist
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
$file_info = pathinfo($_FILES['thumbnail']['name']);
$file_ext = $file_info['extension'];
$safe_filename = uniqid('product_', true) . '.' . $file_ext;
$target_path = $upload_dir . $safe_filename;
// Move the file
if (move_uploaded_file($_FILES['thumbnail']['tmp_name'], $target_path)) {
// Store relative path for DB
$thumbnail_url = 'assets/uploads/products/' . $safe_filename;
} else {
// Optional: Handle file move error
header("Location: products.php?status=error&message=Gagal memindahkan file thumbnail.");
exit;
}
}
// --- Database Insertion ---
try {
$pdo = db();
$sql = "INSERT INTO products (name, price, duration, description, features, thumbnail_url) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
// Bind parameters and execute
$stmt->execute([$name, $price, $duration, $description, $features, $thumbnail_url]);
// Redirect with success message
header("Location: products.php?status=success&message=Produk berhasil ditambahkan.");
exit;
} catch (PDOException $e) {
// Redirect with error message
// For development, you might want to log the error: error_log($e->getMessage());
header("Location: products.php?status=error&message=Database error: " . urlencode($e->getMessage()));
exit;
}
} else {
// If not a POST request, redirect to the add form
header("Location: product_add.php");
exit;
}

38
admin/product_delete.php Normal file
View File

@ -0,0 +1,38 @@
<?php
require_once '../db/config.php';
// Check if ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
header('Location: products.php?status=error&message=ID Produk tidak valid.');
exit;
}
$product_id = $_GET['id'];
try {
$pdo = db();
// First, get the thumbnail path to delete the file
$stmt = $pdo->prepare("SELECT thumbnail_url FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$thumbnail_url = $stmt->fetchColumn();
// Delete the product from the database
$stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
if ($stmt->execute([$product_id])) {
// If deletion from DB is successful, delete the thumbnail file
if ($thumbnail_url && file_exists('../' . $thumbnail_url)) {
unlink('../' . $thumbnail_url);
}
header('Location: products.php?status=success&message=Produk berhasil dihapus.');
} else {
throw new Exception("Gagal menghapus produk dari database.");
}
} catch (PDOException $e) {
header('Location: products.php?status=error&message=Database error: ' . $e->getMessage());
} catch (Exception $e) {
header('Location: products.php?status=error&message=' . $e->getMessage());
}
exit;

98
admin/product_edit.php Normal file
View File

@ -0,0 +1,98 @@
<?php
require_once '../db/config.php';
// Check if ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
header('Location: products.php?status=error&message=ID Produk tidak valid.');
exit;
}
$product_id = $_GET['id'];
$product = null;
$error_message = '';
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$product = $stmt->fetch();
if (!$product) {
throw new Exception("Produk tidak ditemukan.");
}
} catch (PDOException $e) {
$error_message = "Error fetching product: " . $e->getMessage();
} catch (Exception $e) {
$error_message = $e->getMessage();
}
// Include header
include 'partials/header.php';
?>
<div id="page-content-wrapper">
<nav class="navbar navbar-expand-lg navbar-light bg-transparent py-4 px-4">
<div class="d-flex align-items-center">
<i class="bi bi-list fs-4 me-3" id="menu-toggle"></i>
<h2 class="fs-2 m-0">Edit Produk</h2>
</div>
</nav>
<div class="container-fluid px-4">
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php elseif ($product): ?>
<div class="row my-5">
<div class="col">
<div class="card shadow-sm">
<div class="card-body">
<form action="product_update.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($product['id']); ?>">
<div class="mb-3">
<label for="name" class="form-label">Nama Produk</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($product['name']); ?>" required>
</div>
<div class="mb-3">
<label for="price" class="form-label">Harga</label>
<div class="input-group">
<span class="input-group-text">Rp</span>
<input type="number" class="form-control" id="price" name="price" min="0" step="any" value="<?php echo htmlspecialchars($product['price']); ?>" required>
</div>
</div>
<div class="mb-3">
<label for="duration" class="form-label">Durasi Pengerjaan</label>
<input type="text" class="form-control" id="duration" name="duration" placeholder="Contoh: 3-5 hari kerja" value="<?php echo htmlspecialchars($product['duration']); ?>" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Deskripsi Singkat</label>
<textarea class="form-control" id="description" name="description" rows="3" required><?php echo htmlspecialchars($product['description']); ?></textarea>
</div>
<div class="mb-3">
<label for="features" class="form-label">Fitur-fitur (pisahkan dengan koma)</label>
<textarea class="form-control" id="features" name="features" rows="3" placeholder="Contoh: Domain .com, Hosting 2GB, SSL Gratis"><?php echo htmlspecialchars($product['features']); ?></textarea>
</div>
<div class="mb-3">
<label for="thumbnail" class="form-label">Thumbnail</label>
<input class="form-control" type="file" id="thumbnail" name="thumbnail" accept="image/png, image/jpeg, image/gif">
<?php if ($product['thumbnail_url']): ?>
<div class="mt-2">
<small>Thumbnail saat ini:</small><br>
<img src="../<?php echo htmlspecialchars($product['thumbnail_url']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" width="150">
</div>
<?php endif; ?>
</div>
<button type="submit" class="btn btn-primary">Simpan Perubahan</button>
<a href="products.php" class="btn btn-secondary">Batal</a>
</form>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php
// Include footer
include 'partials/footer.php';
?>

88
admin/product_update.php Normal file
View File

@ -0,0 +1,88 @@
<?php
require_once '../db/config.php';
// Check if it's a POST request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: products.php');
exit;
}
// Basic validation
$required_fields = ['id', 'name', 'price', 'duration', 'description'];
foreach ($required_fields as $field) {
if (empty($_POST[$field])) {
header('Location: product_edit.php?id=' . $_POST['id'] . '&status=error&message=Semua field wajib diisi.');
exit;
}
}
$product_id = $_POST['id'];
$name = $_POST['name'];
$price = $_POST['price'];
$duration = $_POST['duration'];
$description = $_POST['description'];
$features = $_POST['features'] ?? '';
$thumbnail_path = null;
try {
$pdo = db();
// First, get the current thumbnail path
$stmt = $pdo->prepare("SELECT thumbnail_url FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$current_thumbnail = $stmt->fetchColumn();
// Handle file upload
if (isset($_FILES['thumbnail']) && $_FILES['thumbnail']['error'] == UPLOAD_ERR_OK) {
$upload_dir = '../assets/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
$filename = uniqid() . '-' . basename($_FILES['thumbnail']['name']);
$target_file = $upload_dir . $filename;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Basic validation for image
$check = getimagesize($_FILES['thumbnail']['tmp_name']);
if ($check === false) {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=File bukan gambar.');
exit;
}
if (!in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=Hanya format JPG, JPEG, PNG & GIF yang diperbolehkan.');
exit;
}
if (move_uploaded_file($_FILES['thumbnail']['tmp_name'], $target_file)) {
$thumbnail_path = 'assets/uploads/' . $filename;
// Delete the old thumbnail if a new one is uploaded
if ($current_thumbnail && file_exists('../' . $current_thumbnail)) {
unlink('../' . $current_thumbnail);
}
} else {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=Gagal mengunggah thumbnail.');
exit;
}
} else {
// Keep the old thumbnail if no new one is uploaded
$thumbnail_path = $current_thumbnail;
}
// Update data in the database
$sql = "UPDATE products SET name = ?, price = ?, duration = ?, description = ?, features = ?, thumbnail_url = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
if ($stmt->execute([$name, $price, $duration, $description, $features, $thumbnail_path, $product_id])) {
header('Location: products.php?status=success&message=Produk berhasil diperbarui.');
} else {
throw new Exception("Gagal memperbarui produk di database.");
}
} catch (PDOException $e) {
// Redirect with a generic error
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=Database error: ' . $e->getMessage());
} catch (Exception $e) {
header('Location: product_edit.php?id=' . $product_id . '&status=error&message=' . $e->getMessage());
}
exit;

1
admin/products.php Normal file
View File

@ -0,0 +1 @@
<?php\nrequire_once \'../db/config.php\';\n\n// Fetch all products from the database\ntry {\n $pdo = db();\n $stmt = $pdo->query(\"SELECT * FROM products ORDER BY created_at DESC\");\n $products = $stmt->fetchAll();\n} catch (PDOException $e) {\n // For now, just display a generic error\n // In a real app, you\'d want to log this error\n $products = [];\n $db_error = \"Error fetching products: \" . $e->getMessage();\n}\n\n// Include header\ninclude \'partials/header.php\';\n?>\n\n<div id=\"page-content-wrapper\">\n <nav class=\"navbar navbar-expand-lg navbar-light bg-transparent py-4 px-4\">\n <div class=\"d-flex align-items-center\">\n <i class=\"bi bi-list fs-4 me-3\" id=\"menu-toggle\"></i>\n <h2 class=\"fs-2 m-0\">Manajemen Produk</h2>\n </div>\n </nav>\n\n <div class=\"container-fluid px-4\">\n\n <?php if (isset($_GET[\'status\'])):\ ?>\n <div class=\"alert alert-<?php echo $_GET[\'status\'] == \'success\' ? \'success\' : \'danger\'; ?> alert-dismissible fade show\" role=\"alert\">\n <?php echo htmlspecialchars($_GET[\'message\']); ?>\n <button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" aria-label=\"Close\"></button>\n </div>\n <?php endif; ?>\n\n <?php if (isset($db_error)): ?>\n <div class=\"alert alert-danger\"> <?php echo $db_error; ?></div>\n <?php endif; ?>\n\n <div class=\"row my-5\">\n <div class=\"col\">\n <a href=\"product_add.php\" class=\"btn btn-primary mb-3\"><i class=\"bi bi-plus-lg\"></i> Tambah Produk</a>\n <div class=\"card shadow-sm\">\n <div class=\"card-body\">\n <h3 class=\"fs-4 mb-3\">Daftar Produk</h3>\n <div class=\"table-responsive\">\n <table class=\"table table-hover\">\n <thead>\n <tr>\n <th scope=\"col\">Thumbnail</th>\n <th scope=\"col\">Nama</th>\n <th scope=\"col\">Harga</th>\n <th scope=\"col\">Durasi</th>\n <th scope=\"col\">Aksi</th>\n </tr>\n </thead>\n <tbody>\n <?php if (empty($products)): ?>\n <tr>\n <td colspan=\"5\" class=\"text-center\">Belum ada produk.</td>\n </tr>\n <?php else: ?>\n <?php foreach ($products as $product): ?>\n <tr>\n <td>\n <?php if ($product[\'thumbnail_url\']): ?>\n <img src=\"../<?php echo htmlspecialchars($product[\'thumbnail_url\']); ?>\" alt=\"<?php echo htmlspecialchars($product[\'name\']); ?>\" width=\"100\">\n <?php else: ?>\n <span class=\"text-muted\">No Image</span>\n <?php endif; ?>\n </td>\n <td><?php echo htmlspecialchars($product[\'name\']); ?></td>\n <td>Rp <?php echo number_format($product[\'price\'], 0, \',\', \'.\'); ?></td>\n <td><?php echo htmlspecialchars($product[\'duration\']); ?></td>\n <td>\n <a href=\"product_edit.php?id=<?php echo $product[\'id\']; ?>\" class=\"btn btn-sm btn-outline-primary\"><i class=\"bi bi-pencil-square\"></i></a>\n <a href=\"product_delete.php?id=<?php echo $product[\'id\']; ?>\" class=\"btn btn-sm btn-outline-danger\" onclick=\"return confirm(\'Anda yakin ingin menghapus produk ini?\');\"><i class=\"bi bi-trash\"></i></a>\n </td>\n </tr>\n <?php endforeach; ?>\n <?php endif; ?>\n </tbody>\n </table>\n </div>\n </div>\n </div>\n </div>\n </div>\n </div>\n</div>\n\n<?php\n// Include footer\ninclude \'partials/footer.php\';\n?>\n

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

15
db/migration_products.php Normal file
View File

@ -0,0 +1,15 @@
<?php
require_once 'config.php';
try {
$pdo = db();
$sql = "CREATE TABLE IF NOT EXISTS `products` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, `duration` varchar(255) NOT NULL, `description` text NOT NULL, `features` text DEFAULT NULL, `thumbnail_url` varchar(255) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
$pdo->exec($sql);
echo "Table 'products' created successfully.";
} catch (PDOException $e) {
die("DB ERROR: ". $e->getMessage());
}