File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Services/AdminCategoryService.php
Back
<?php /** * AdminCategoryService * Full CRUD for category hierarchy management */ require_once __DIR__ . '/../Models/Category.php'; require_once __DIR__ . '/../Models/AdminAuditLog.php'; class AdminCategoryService { private Category $categoryModel; private AdminAuditLog $auditLog; public function __construct() { $this->categoryModel = new Category(); $this->auditLog = new AdminAuditLog(); } public function getAllCategories(bool $includeDeleted = true): array { return $this->categoryModel->getAllCategories($includeDeleted); } public function getCategoryDetail(int $id): ?array { return $this->categoryModel->findAdmin($id); } public function createCategory(array $data, int $adminId): array { $name = trim($data['name'] ?? ''); $slug = trim($data['slug'] ?? ''); $parentId = !empty($data['parent_id']) ? (int)$data['parent_id'] : null; if (empty($name)) return ['success' => false, 'message' => 'กรุณาระบุชื่อหมวดหมู่']; if (empty($slug)) { $slug = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', $name)); $slug = trim($slug, '-'); } if ($this->categoryModel->slugExistsAdmin($slug)) { return ['success' => false, 'message' => 'Slug ซ้ำกับหมวดหมู่ที่มีอยู่แล้ว']; } $level = $parentId ? 2 : 1; if ($parentId) { $parent = $this->categoryModel->findAdmin($parentId); if (!$parent) return ['success' => false, 'message' => 'ไม่พบหมวดหมู่หลักที่เลือก']; $level = ((int)$parent['level']) + 1; } $id = $this->categoryModel->createCategory([ 'name' => $name, 'slug' => $slug, 'parent_id' => $parentId, 'level' => $level, 'description' => trim($data['description'] ?? ''), 'icon' => trim($data['icon'] ?? ''), 'status' => 'active', ]); $this->auditLog->record($adminId, 'category.create', 'categories', 'category', $id, "Created category: {$name}"); return ['success' => true, 'message' => 'สร้างหมวดหมู่เรียบร้อยแล้ว', 'id' => $id]; } public function updateCategory(int $id, array $data, int $adminId): array { $cat = $this->categoryModel->findAdmin($id); if (!$cat) return ['success' => false, 'message' => 'ไม่พบหมวดหมู่']; $name = trim($data['name'] ?? $cat['name']); $slug = trim($data['slug'] ?? $cat['slug']); if ($this->categoryModel->slugExistsAdmin($slug, $id)) { return ['success' => false, 'message' => 'Slug ซ้ำกับหมวดหมู่ที่มีอยู่แล้ว']; } $update = [ 'name' => $name, 'slug' => $slug, 'description' => trim($data['description'] ?? $cat['description'] ?? ''), 'icon' => trim($data['icon'] ?? $cat['icon'] ?? ''), ]; $ok = $this->categoryModel->updateCategory($id, $update); if ($ok) { $this->auditLog->record($adminId, 'category.update', 'categories', 'category', $id, "Updated category: {$name}"); } return ['success' => $ok, 'message' => $ok ? 'บันทึกการเปลี่ยนแปลงเรียบร้อยแล้ว' : 'ไม่มีการเปลี่ยนแปลง']; } public function disableCategory(int $id, int $adminId): array { $ok = $this->categoryModel->disableCategory($id); if ($ok) { $this->auditLog->record($adminId, 'category.disable', 'categories', 'category', $id); } return ['success' => $ok, 'message' => $ok ? 'ปิดใช้หมวดหมู่เรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } public function enableCategory(int $id, int $adminId): array { $ok = $this->categoryModel->enableCategory($id); if ($ok) { $this->auditLog->record($adminId, 'category.enable', 'categories', 'category', $id); } return ['success' => $ok, 'message' => $ok ? 'เปิดใช้หมวดหมู่เรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } public function deleteCategory(int $id, int $adminId): array { $cat = $this->categoryModel->findAdmin($id); if (!$cat) return ['success' => false, 'message' => 'ไม่พบหมวดหมู่']; $ok = $this->categoryModel->softDeleteCategory($id); if ($ok) { $this->auditLog->record($adminId, 'category.delete', 'categories', 'category', $id, "Soft-deleted category: {$cat['name']}"); } return ['success' => $ok, 'message' => $ok ? 'ลบหมวดหมู่เรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } public function restoreCategory(int $id, int $adminId): array { $ok = $this->categoryModel->restoreCategory($id); if ($ok) { $this->auditLog->record($adminId, 'category.restore', 'categories', 'category', $id); } return ['success' => $ok, 'message' => $ok ? 'กู้คืนหมวดหมู่เรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings