File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Controllers/CatalogController.php
Back
<?php /** * CatalogController * Handles search, categories, product details, and store pages */ require_once __DIR__ . '/../Core/Controller.php'; require_once __DIR__ . '/../Services/ProductService.php'; require_once __DIR__ . '/../Services/WishlistService.php'; require_once __DIR__ . '/../Services/ReviewService.php'; require_once __DIR__ . '/../Helpers/Auth.php'; class CatalogController extends Controller { private ProductService $productService; private WishlistService $wishlistService; private ReviewService $reviewService; public function __construct() { $this->productService = new ProductService(); $this->wishlistService = new WishlistService(); $this->reviewService = new ReviewService(); } public function search(): void { $filters = [ 'q' => $_GET['q'] ?? '', 'category_id' => $_GET['category_id'] ?? '', 'category_slug' => $_GET['category_slug'] ?? $_GET['category'] ?? '', 'min_price' => $_GET['min_price'] ?? '', 'max_price' => $_GET['max_price'] ?? '', 'sort' => $_GET['sort'] ?? 'latest', 'in_stock_only' => !empty($_GET['in_stock']) ]; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $data = $this->productService->searchCatalog($filters, $page, 16); $this->render('catalog/search', array_merge($data, [ 'pageTitle' => (!empty($filters['q']) ? e($filters['q']) . ' - ' : '') . __('search_products') . ' - CARGOO' ]), 'main'); } public function category(string $slug): void { $categoryModel = new Category(); $category = $categoryModel->findBySlug($slug); if (!$category) { http_response_code(404); require_once __DIR__ . '/../../views/errors/404.php'; exit; } $filters = [ 'category_slug' => $slug, 'q' => $_GET['q'] ?? '', 'min_price' => $_GET['min_price'] ?? '', 'max_price' => $_GET['max_price'] ?? '', 'sort' => $_GET['sort'] ?? 'latest', 'in_stock_only' => !empty($_GET['in_stock']) ]; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $data = $this->productService->searchCatalog($filters, $page, 16); $this->render('catalog/category', array_merge($data, [ 'category' => $category, 'pageTitle' => e($category['name']) . ' - CARGOO' ]), 'main'); } public function product(string $idOrSlug): void { $product = $this->productService->getProductDetail($idOrSlug); if (!$product) { http_response_code(404); require_once __DIR__ . '/../../views/errors/404.php'; exit; } $isSaved = false; if (Auth::check()) { $isSaved = $this->wishlistService->isSaved(Auth::id(), (int)$product['id']); } // Get related products from same category $relatedProducts = []; if (!empty($product['category_id'])) { $search = $this->productService->searchCatalog([ 'category_id' => $product['category_id'], 'in_stock_only' => true ], 1, 6); $relatedProducts = array_filter($search['products'], fn($p) => (int)$p['id'] !== (int)$product['id']); } // Get product review and rating data $reviewData = $this->reviewService->getProductReviews((int)$product['id'], 1, 10); $this->render('catalog/product', [ 'product' => $product, 'isSaved' => $isSaved, 'relatedProducts' => array_slice($relatedProducts, 0, 4), 'reviews' => $reviewData['reviews'], 'reviewBreakdown' => $reviewData['breakdown'], 'totalReviews' => $reviewData['total_count'], 'pageTitle' => e($product['name']) . ' - CARGOO' ], 'main'); } public function store(string $slug): void { $filters = [ 'q' => $_GET['q'] ?? '', 'min_price' => $_GET['min_price'] ?? '', 'max_price' => $_GET['max_price'] ?? '', 'sort' => $_GET['sort'] ?? 'latest', 'in_stock_only' => !empty($_GET['in_stock']) ]; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $storeData = $this->productService->getStoreCatalog($slug, $filters, $page, 16); if (!$storeData) { http_response_code(404); require_once __DIR__ . '/../../views/errors/404.php'; exit; } $this->render('catalog/store', [ 'store' => $storeData['store'], 'catalog' => $storeData['catalog'], 'pageTitle' => e($storeData['store']['store_name']) . ' - ร้านค้า CARGOO' ], 'main'); } public function suggest(): void { $q = trim($_GET['q'] ?? ''); if (mb_strlen($q) < 1) { $this->json(['success' => true, 'products' => [], 'categories' => []]); return; } $search = $this->productService->searchCatalog(['q' => $q], 1, 5); $categoryModel = new Category(); $allCats = $categoryModel->getActiveCategories(); $matchedCats = []; foreach ($allCats as $cat) { if (mb_stripos($cat['name'], $q) !== false) { $matchedCats[] = [ 'id' => $cat['id'], 'name' => $cat['name'], 'slug' => $cat['slug'], 'url' => url('/category/' . $cat['slug']) ]; if (count($matchedCats) >= 3) break; } } $formattedProducts = array_map(function($p) { return [ 'id' => $p['id'], 'name' => $p['name'], 'slug' => $p['slug'], 'price' => (float)$p['price'], 'image' => !empty($p['gallery_image']) ? product_image_url($p['gallery_image']) : null, 'url' => url('/product/' . $p['slug']) ]; }, $search['products']); $this->json([ 'success' => true, 'products' => $formattedProducts, 'categories' => $matchedCats, 'total' => $search['total_count'] ]); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings