mirror of
https://github.com/myronblair/tomsjavajive-app
synced 2026-06-30 17:50:56 -05:00
294 lines
12 KiB
PHP
294 lines
12 KiB
PHP
<?php
|
|
ob_start();
|
|
/**
|
|
* Tom's Java Jive - Admin Product Edit/Add
|
|
*/
|
|
|
|
$pageTitle = 'Product';
|
|
require_once __DIR__ . '/includes/header.php';
|
|
|
|
$productId = $_GET['id'] ?? '';
|
|
$product = null;
|
|
$isEdit = false;
|
|
$errors = [];
|
|
|
|
if ($productId) {
|
|
$product = db()->fetch("SELECT * FROM products WHERE product_id = :id", ['id' => $productId]);
|
|
if ($product) {
|
|
$isEdit = true;
|
|
$pageTitle = 'Edit Product';
|
|
$product['images'] = json_decode($product['images'] ?? '[]', true);
|
|
$product['tags'] = json_decode($product['tags'] ?? '[]', true);
|
|
}
|
|
}
|
|
|
|
if (!$product) {
|
|
$product = [
|
|
'product_id' => '',
|
|
'name' => '',
|
|
'description' => '',
|
|
'price' => '',
|
|
'sale_price' => '',
|
|
'cost_price' => '',
|
|
'sku' => '',
|
|
'barcode' => '',
|
|
'category' => '',
|
|
'tags' => [],
|
|
'images' => [],
|
|
'stock' => 100,
|
|
'low_stock_threshold' => 10,
|
|
'weight' => '',
|
|
'is_active' => 1,
|
|
'is_featured' => 0
|
|
];
|
|
$pageTitle = 'Add Product';
|
|
}
|
|
|
|
// Handle form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
$price = floatval($_POST['price'] ?? 0);
|
|
$salePrice = !empty($_POST['sale_price']) ? floatval($_POST['sale_price']) : null;
|
|
$costPrice = !empty($_POST['cost_price']) ? floatval($_POST['cost_price']) : null;
|
|
$sku = trim($_POST['sku'] ?? '');
|
|
$barcode = trim($_POST['barcode'] ?? '');
|
|
$category = trim($_POST['category'] ?? '');
|
|
$stock = intval($_POST['stock'] ?? 0);
|
|
$lowStockThreshold = intval($_POST['low_stock_threshold'] ?? 10);
|
|
$weight = !empty($_POST['weight']) ? floatval($_POST['weight']) : null;
|
|
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
|
$isFeatured = isset($_POST['is_featured']) ? 1 : 0;
|
|
$imageUrls = array_filter(array_map('trim', explode("\n", $_POST['image_urls'] ?? '')));
|
|
|
|
// Validate
|
|
if (empty($name)) $errors['name'] = 'Product name is required';
|
|
if ($price <= 0) $errors['price'] = 'Price must be greater than 0';
|
|
|
|
if (empty($errors)) {
|
|
$data = [
|
|
'name' => $name,
|
|
'description' => $description,
|
|
'price' => $price,
|
|
'sale_price' => $salePrice,
|
|
'cost_price' => $costPrice,
|
|
'sku' => $sku ?: null,
|
|
'barcode' => $barcode ?: null,
|
|
'category' => $category ?: null,
|
|
'images' => json_encode($imageUrls),
|
|
'stock' => $stock,
|
|
'low_stock_threshold' => $lowStockThreshold,
|
|
'weight' => $weight,
|
|
'is_active' => $isActive,
|
|
'is_featured' => $isFeatured
|
|
];
|
|
|
|
if ($isEdit) {
|
|
db()->update('products', $data, 'product_id = :id', ['id' => $productId]);
|
|
setFlash('success', 'Product updated successfully');
|
|
} else {
|
|
$data['product_id'] = generateId('prod_');
|
|
db()->insert('products', $data);
|
|
setFlash('success', 'Product created successfully');
|
|
}
|
|
|
|
header('Location: /admin/products.php');
|
|
exit;
|
|
}
|
|
|
|
// Keep form values on error
|
|
$product = array_merge($product, $_POST);
|
|
$product['images'] = $imageUrls;
|
|
}
|
|
|
|
// Get categories for dropdown
|
|
$categories = db()->fetchAll(
|
|
"SELECT DISTINCT category FROM products WHERE category IS NOT NULL AND category != '' ORDER BY category"
|
|
);
|
|
?>
|
|
|
|
<div class="page-header">
|
|
<h1 class="page-title"><?= $isEdit ? 'Edit' : 'Add' ?> Product</h1>
|
|
<a href="/admin/products.php" class="btn btn-secondary">
|
|
<i class="fas fa-arrow-left"></i> Back to Products
|
|
</a>
|
|
</div>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-error">
|
|
<i class="fas fa-exclamation-circle"></i>
|
|
Please fix the errors below
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST" action="">
|
|
<div style="display: grid; grid-template-columns: 2fr 1fr; gap: 1.5rem; align-items: start;">
|
|
<!-- Main Info -->
|
|
<div>
|
|
<div class="admin-card">
|
|
<div class="admin-card-header">
|
|
<h3 class="admin-card-title">Basic Information</h3>
|
|
</div>
|
|
<div class="admin-card-body">
|
|
<div class="form-group">
|
|
<label class="form-label">Product Name *</label>
|
|
<input type="text" name="name" class="form-input"
|
|
value="<?= htmlspecialchars($product['name']) ?>" required>
|
|
<?php if (isset($errors['name'])): ?>
|
|
<span class="form-error"><?= $errors['name'] ?></span>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Description</label>
|
|
<textarea name="description" class="form-textarea" rows="4"><?= htmlspecialchars($product['description']) ?></textarea>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label class="form-label">Category</label>
|
|
<input type="text" name="category" class="form-input"
|
|
value="<?= htmlspecialchars($product['category']) ?>"
|
|
list="category-list">
|
|
<datalist id="category-list">
|
|
<?php foreach ($categories as $cat): ?>
|
|
<option value="<?= htmlspecialchars($cat['category']) ?>">
|
|
<?php endforeach; ?>
|
|
</datalist>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Weight (oz)</label>
|
|
<input type="number" name="weight" class="form-input" step="0.01"
|
|
value="<?= htmlspecialchars($product['weight']) ?>">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-card">
|
|
<div class="admin-card-header">
|
|
<h3 class="admin-card-title">Pricing</h3>
|
|
</div>
|
|
<div class="admin-card-body">
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label class="form-label">Price *</label>
|
|
<input type="number" name="price" class="form-input" step="0.01" min="0"
|
|
value="<?= htmlspecialchars($product['price']) ?>" required>
|
|
<?php if (isset($errors['price'])): ?>
|
|
<span class="form-error"><?= $errors['price'] ?></span>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Sale Price</label>
|
|
<input type="number" name="sale_price" class="form-input" step="0.01" min="0"
|
|
value="<?= htmlspecialchars($product['sale_price'] ?? '') ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Cost Price (for profit tracking)</label>
|
|
<input type="number" name="cost_price" class="form-input" step="0.01" min="0"
|
|
value="<?= htmlspecialchars($product['cost_price'] ?? '') ?>">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-card">
|
|
<div class="admin-card-header">
|
|
<h3 class="admin-card-title">Images</h3>
|
|
</div>
|
|
<div class="admin-card-body">
|
|
<div class="form-group">
|
|
<label class="form-label">Image URLs (one per line)</label>
|
|
<textarea name="image_urls" class="form-textarea" rows="3"
|
|
placeholder="https://example.com/image1.jpg"><?= htmlspecialchars(implode("\n", $product['images'])) ?></textarea>
|
|
<small class="text-muted">Enter full URLs to product images</small>
|
|
</div>
|
|
|
|
<?php if (!empty($product['images'])): ?>
|
|
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 1rem;">
|
|
<?php foreach ($product['images'] as $img): ?>
|
|
<img src="<?= htmlspecialchars($img) ?>" alt="Product image"
|
|
style="width: 80px; height: 80px; object-fit: cover; border-radius: var(--admin-radius);">
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Sidebar -->
|
|
<div>
|
|
<div class="admin-card">
|
|
<div class="admin-card-header">
|
|
<h3 class="admin-card-title">Status</h3>
|
|
</div>
|
|
<div class="admin-card-body">
|
|
<div class="form-group">
|
|
<label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
|
|
<input type="checkbox" name="is_active" value="1"
|
|
<?= $product['is_active'] ? 'checked' : '' ?>>
|
|
Active (visible in store)
|
|
</label>
|
|
</div>
|
|
|
|
<div class="form-group mb-0">
|
|
<label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
|
|
<input type="checkbox" name="is_featured" value="1"
|
|
<?= $product['is_featured'] ? 'checked' : '' ?>>
|
|
Featured product
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-card">
|
|
<div class="admin-card-header">
|
|
<h3 class="admin-card-title">Inventory</h3>
|
|
</div>
|
|
<div class="admin-card-body">
|
|
<div class="form-group">
|
|
<label class="form-label">Stock Quantity</label>
|
|
<input type="number" name="stock" class="form-input" min="0"
|
|
value="<?= htmlspecialchars($product['stock']) ?>">
|
|
</div>
|
|
|
|
<div class="form-group mb-0">
|
|
<label class="form-label">Low Stock Alert Threshold</label>
|
|
<input type="number" name="low_stock_threshold" class="form-input" min="0"
|
|
value="<?= htmlspecialchars($product['low_stock_threshold']) ?>">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="admin-card">
|
|
<div class="admin-card-header">
|
|
<h3 class="admin-card-title">Identifiers</h3>
|
|
</div>
|
|
<div class="admin-card-body">
|
|
<div class="form-group">
|
|
<label class="form-label">SKU</label>
|
|
<input type="text" name="sku" class="form-input"
|
|
value="<?= htmlspecialchars($product['sku'] ?? '') ?>">
|
|
</div>
|
|
|
|
<div class="form-group mb-0">
|
|
<label class="form-label">Barcode</label>
|
|
<input type="text" name="barcode" class="form-input"
|
|
value="<?= htmlspecialchars($product['barcode'] ?? '') ?>">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary btn-lg btn-block">
|
|
<i class="fas fa-save"></i> <?= $isEdit ? 'Update' : 'Create' ?> Product
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|