📂 FileMgr
📍
/
home
/
u865173473
/
domains
/
aandaaloil.com
/
public_html
/
models
✏️ t.php
⬅ Kembali
<?php require_once __DIR__ . '/../db.php'; require_once __DIR__ . '/../includes/utils.php'; class Product { private $pdo; private array $columnCache = []; public function __construct($pdo) { $this->pdo = $pdo; } private function hasColumn(string $column): bool { if (array_key_exists($column, $this->columnCache)) { return $this->columnCache[$column]; } try { $stmt = $this->pdo->query('SHOW COLUMNS FROM products LIKE ' . $this->pdo->quote($column)); $this->columnCache[$column] = (bool) $stmt->fetch(); } catch (PDOException $e) { $this->columnCache[$column] = false; } return $this->columnCache[$column]; } public function getAll($filters = []) { $sql = 'SELECT p.*, b.brand_name, b.logo as brand_logo, c.category_name FROM products p JOIN brands b ON p.brand_id = b.id JOIN categories c ON p.category_id = c.id'; $params = []; $where = []; if (!empty($filters['brand_id'])) { $where[] = 'p.brand_id = ?'; $params[] = $filters['brand_id']; } if (!empty($filters['category_id'])) { $where[] = 'p.category_id = ?'; $params[] = $filters['category_id']; } if (!empty($filters['status'])) { $where[] = 'p.status = ?'; $params[] = $filters['status']; } if (!empty($filters['frontend'])) { $where[] = "b.status = 'active'"; $where[] = "c.status = 'active'"; } if (!empty($filters['search'])) { $where[] = 'p.product_name LIKE ?'; $params[] = '%' . $filters['search'] . '%'; } if (!empty($where)) { $sql .= ' WHERE ' . implode(' AND ', $where); } $sql .= ' ORDER BY p.created_at DESC'; $stmt = $this->pdo->prepare($sql); $stmt->execute($params); return $stmt->fetchAll(); } public function getById($id) { $stmt = $this->pdo->prepare('SELECT * FROM products WHERE id = ?'); $stmt->execute([$id]); return $stmt->fetch(); } public function create($data) { $slug = createSlug($data['product_name']); $stmtCheck = $this->pdo->prepare('SELECT COUNT(*) FROM products WHERE slug = ?'); $stmtCheck->execute([$slug]); if ($stmtCheck->fetchColumn() > 0) { $slug .= '-' . substr(md5(uniqid(mt_rand(), true)), 0, 5); } $sellingPrice = (float) $data['price']; $originalPrice = normalizeOriginalPrice($data['original_price'] ?? null, $sellingPrice); if ($this->hasColumn('product_name_en') && $this->hasColumn('original_price')) { $stmt = $this->pdo->prepare( 'INSERT INTO products (brand_id, category_id, product_name, product_name_en, slug, description, description_en, price, original_price, stock, image, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' ); return $stmt->execute([ $data['brand_id'], $data['category_id'], $data['product_name'], $data['product_name_en'] ?? null, $slug, $data['description'] ?? null, $data['description_en'] ?? null, $sellingPrice, $originalPrice, $data['stock'], $data['image'] ?? null, $data['status'] ?? 'active' ]); } if ($this->hasColumn('product_name_en')) { $stmt = $this->pdo->prepare( 'INSERT INTO products (brand_id, category_id, product_name, product_name_en, slug, description, description_en, price, stock, image, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' ); return $stmt->execute([ $data['brand_id'], $data['category_id'], $data['product_name'], $data['product_name_en'] ?? null, $slug, $data['description'] ?? null, $data['description_en'] ?? null, $sellingPrice, $data['stock'], $data['image'] ?? null, $data['status'] ?? 'active' ]); } $stmt = $this->pdo->prepare( 'INSERT INTO products (brand_id, category_id, product_name, slug, description, price, stock, image, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' ); return $stmt->execute([ $data['brand_id'], $data['category_id'], $data['product_name'], $slug, $data['description'] ?? null, $sellingPrice, $data['stock'], $data['image'] ?? null, $data['status'] ?? 'active' ]); } public function update($id, $data) { $slug = createSlug($data['product_name']); $stmtCheck = $this->pdo->prepare('SELECT COUNT(*) FROM products WHERE slug = ? AND id != ?'); $stmtCheck->execute([$slug, $id]); if ($stmtCheck->fetchColumn() > 0) { $slug .= '-' . substr(md5(uniqid(mt_rand(), true)), 0, 5); } $sellingPrice = (float) $data['price']; $originalPrice = normalizeOriginalPrice($data['original_price'] ?? null, $sellingPrice); if ($this->hasColumn('product_name_en') && $this->hasColumn('original_price')) { $stmt = $this->pdo->prepare( 'UPDATE products SET brand_id = ?, category_id = ?, product_name = ?, product_name_en = ?, slug = ?, description = ?, description_en = ?, price = ?, original_price = ?, stock = ?, image = ?, status = ? WHERE id = ?' ); return $stmt->execute([ $data['brand_id'], $data['category_id'], $data['product_name'], $data['product_name_en'] ?? null, $slug, $data['description'], $data['description_en'] ?? null, $sellingPrice, $originalPrice, $data['stock'], $data['image'], $data['status'], $id ]); } if ($this->hasColumn('product_name_en')) { $stmt = $this->pdo->prepare( 'UPDATE products SET brand_id = ?, category_id = ?, product_name = ?, product_name_en = ?, slug = ?, description = ?, description_en = ?, price = ?, stock = ?, image = ?, status = ? WHERE id = ?' ); return $stmt->execute([ $data['brand_id'], $data['category_id'], $data['product_name'], $data['product_name_en'] ?? null, $slug, $data['description'], $data['description_en'] ?? null, $sellingPrice, $data['stock'], $data['image'], $data['status'], $id ]); } $stmt = $this->pdo->prepare( 'UPDATE products SET brand_id = ?, category_id = ?, product_name = ?, slug = ?, description = ?, price = ?, stock = ?, image = ?, status = ? WHERE id = ?' ); return $stmt->execute([ $data['brand_id'], $data['category_id'], $data['product_name'], $slug, $data['description'], $sellingPrice, $data['stock'], $data['image'], $data['status'], $id ]); } public function delete($id) { $stmt = $this->pdo->prepare('DELETE FROM products WHERE id = ?'); return $stmt->execute([$id]); } public function toggleStatus($id) { $stmt = $this->pdo->prepare("UPDATE products SET status = IF(status = 'active', 'inactive', 'active') WHERE id = ?"); return $stmt->execute([$id]); } } ?>
💾 Simpan
Batal
⬅ public_html
3 item
Nama
Tipe
Ukuran
Diubah
Aksi
🐘
Brand.php
php
3 KB
2026-07-11 11:27
✏️
👁️
🔤
🗑
🐘
Category.php
php
3.3 KB
2026-07-11 11:27
✏️
👁️
🔤
🗑
🐘
Product.php
php
8.2 KB
2026-07-11 11:27
✏️
👁️
🔤
🗑
✏️ Rename
Nama Baru
Simpan
Batal