116 lines
3.4 KiB
PHP
116 lines
3.4 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../../config/config.php';
|
|
require_once __DIR__ . '/../../db/config.php';
|
|
|
|
class Product {
|
|
|
|
/**
|
|
* Get all products with their category names.
|
|
*/
|
|
public static function getAll() {
|
|
$pdo = db();
|
|
$stmt = $pdo->query('SELECT p.*, c.name as category_name FROM products p JOIN categories c ON p.category_id = c.id ORDER BY p.created_at DESC');
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
/**
|
|
* Get a single product by its ID.
|
|
*/
|
|
public static function find($id) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
/**
|
|
* Get a single product by its slug.
|
|
*/
|
|
public static function findBySlug($slug) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT p.*, c.name as category_name FROM products p JOIN categories c ON p.category_id = c.id WHERE p.slug = ? AND p.active = 1');
|
|
$stmt->execute([$slug]);
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
/**
|
|
* Create a new product.
|
|
*/
|
|
public static function create($data) {
|
|
$pdo = db();
|
|
$sql = "INSERT INTO products (category_id, name, slug, description, price, stock, active, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
|
$stmt = $pdo->prepare($sql);
|
|
return $stmt->execute([
|
|
$data['category_id'],
|
|
$data['name'],
|
|
$data['slug'],
|
|
$data['description'],
|
|
$data['price'],
|
|
$data['stock'],
|
|
$data['active'],
|
|
$data['image']
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update an existing product.
|
|
*/
|
|
public static function update($id, $data) {
|
|
$pdo = db();
|
|
$sql = "UPDATE products SET category_id = ?, name = ?, slug = ?, description = ?, price = ?, stock = ?, active = ?, image = ? WHERE id = ?";
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
$params = [
|
|
$data['category_id'],
|
|
$data['name'],
|
|
$data['slug'],
|
|
$data['description'],
|
|
$data['price'],
|
|
$data['stock'],
|
|
$data['active'],
|
|
$data['image'],
|
|
$id
|
|
];
|
|
|
|
return $stmt->execute($params);
|
|
}
|
|
|
|
|
|
/**
|
|
* Delete a product by its ID.
|
|
*/
|
|
public static function delete($id) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('DELETE FROM products WHERE id = ?');
|
|
return $stmt->execute([$id]);
|
|
}
|
|
|
|
/**
|
|
* Get the image filename for a product.
|
|
*/
|
|
public static function getProductImage($id) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT image FROM products WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
return $result ? $result['image'] : null;
|
|
}
|
|
|
|
/**
|
|
* Search for products by a given term.
|
|
*/
|
|
public static function search($term) {
|
|
$pdo = db();
|
|
$searchTerm = '%' . $term . '%';
|
|
$stmt = $pdo->prepare(
|
|
'SELECT p.*, c.name as category_name
|
|
FROM products p
|
|
JOIN categories c ON p.category_id = c.id
|
|
WHERE (p.name LIKE ? OR p.description LIKE ?) AND p.active = 1
|
|
ORDER BY p.created_at DESC'
|
|
);
|
|
$stmt->execute([$searchTerm, $searchTerm]);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
} |