154 lines
5.0 KiB
PHP
154 lines
5.0 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../Models/Product.php';
|
|
require_once __DIR__ . '/../Models/Category.php';
|
|
require_once __DIR__ . '/../../helpers/slugify.php';
|
|
|
|
class ProductController {
|
|
|
|
public function index() {
|
|
return Product::getAll();
|
|
}
|
|
|
|
public function store() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$price = trim($_POST['price'] ?? '0');
|
|
$stock = trim($_POST['stock'] ?? '0');
|
|
|
|
if (empty($name) || empty($price) || empty($stock)) {
|
|
$_SESSION['error_message'] = 'Name, price and stock are required.';
|
|
return false;
|
|
}
|
|
|
|
$slug = slugify($name);
|
|
$image_filename = $this->handleImageUpload();
|
|
|
|
$data = [
|
|
'category_id' => $_POST['category_id'],
|
|
'name' => $name,
|
|
'slug' => $slug,
|
|
'description' => $_POST['description'] ?? '',
|
|
'price' => $price,
|
|
'stock' => $stock,
|
|
'active' => isset($_POST['active']) ? 1 : 0,
|
|
'image' => $image_filename
|
|
];
|
|
|
|
if (Product::create($data)) {
|
|
$_SESSION['success_message'] = 'Product created successfully.';
|
|
return true;
|
|
} else {
|
|
$_SESSION['error_message'] = 'Failed to create product.';
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function update() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['id'];
|
|
$name = trim($_POST['name'] ?? '');
|
|
$price = trim($_POST['price'] ?? '0');
|
|
$stock = trim($_POST['stock'] ?? '0');
|
|
|
|
if (empty($name) || empty($price) || empty($stock)) {
|
|
$_SESSION['error_message'] = 'Name, price and stock are required.';
|
|
return false;
|
|
}
|
|
|
|
$product = Product::find($id);
|
|
if (!$product) {
|
|
$_SESSION['error_message'] = 'Product not found.';
|
|
return false;
|
|
}
|
|
|
|
$slug = slugify($name);
|
|
$image_filename = $this->handleImageUpload($product['image']);
|
|
|
|
$data = [
|
|
'category_id' => $_POST['category_id'],
|
|
'name' => $name,
|
|
'slug' => $slug,
|
|
'description' => $_POST['description'] ?? '',
|
|
'price' => $price,
|
|
'stock' => $stock,
|
|
'active' => isset($_POST['active']) ? 1 : 0,
|
|
'image' => $image_filename
|
|
];
|
|
|
|
if (Product::update($id, $data)) {
|
|
$_SESSION['success_message'] = 'Product updated successfully.';
|
|
return true;
|
|
} else {
|
|
$_SESSION['error_message'] = 'Failed to update product.';
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function destroy() {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$id = $_POST['delete_id'];
|
|
$image = Product::getProductImage($id);
|
|
|
|
if (Product::delete($id)) {
|
|
if ($image) {
|
|
$this->deleteImage($image);
|
|
}
|
|
$_SESSION['success_message'] = 'Product deleted successfully.';
|
|
return true;
|
|
} else {
|
|
$_SESSION['error_message'] = 'Failed to delete product.';
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function handleImageUpload($existing_image = null) {
|
|
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
|
|
$upload_dir = __DIR__ . '/../../public_html/uploads/products/';
|
|
if (!is_dir($upload_dir)) {
|
|
mkdir($upload_dir, 0777, true);
|
|
}
|
|
|
|
$tmp_name = $_FILES['image']['tmp_name'];
|
|
$original_name = basename($_FILES['image']['name']);
|
|
$file_extension = pathinfo($original_name, PATHINFO_EXTENSION);
|
|
$safe_filename = uniqid('product_', true) . '.' . $file_extension;
|
|
$destination = $upload_dir . $safe_filename;
|
|
|
|
if (move_uploaded_file($tmp_name, $destination)) {
|
|
// Delete old image if a new one is uploaded
|
|
if ($existing_image) {
|
|
$this->deleteImage($existing_image);
|
|
}
|
|
return $safe_filename;
|
|
}
|
|
}
|
|
return $existing_image; // Return old image if no new one was uploaded
|
|
}
|
|
|
|
private function deleteImage($filename) {
|
|
$file_path = __DIR__ . '/../../public_html/uploads/products/' . $filename;
|
|
if (file_exists($file_path)) {
|
|
unlink($file_path);
|
|
}
|
|
}
|
|
|
|
public function getCategories() {
|
|
return Category::getAll();
|
|
}
|
|
|
|
public function show($slug) {
|
|
return Product::findBySlug($slug);
|
|
}
|
|
|
|
public function search($term) {
|
|
return Product::search($term);
|
|
}
|
|
}
|