File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Controllers/SellerProductController.php
Back
<?php /** * SellerProductController * Handles Seller Product Management, CRUD, Variations, and Images */ require_once __DIR__ . '/../Core/Controller.php'; require_once __DIR__ . '/../Services/SellerProductService.php'; require_once __DIR__ . '/../Services/StoreService.php'; require_once __DIR__ . '/../Models/Category.php'; require_once __DIR__ . '/../Helpers/Auth.php'; require_once __DIR__ . '/../Helpers/Session.php'; require_once __DIR__ . '/../Helpers/Url.php'; class SellerProductController extends Controller { private SellerProductService $productService; private StoreService $storeService; private Category $categoryModel; public function __construct() { $this->productService = new SellerProductService(); $this->storeService = new StoreService(); $this->categoryModel = new Category(); } public function index(): void { $userId = Auth::id(); $store = $this->storeService->getStoreByUserId($userId); if (!$store) { $this->redirect('/seller/register'); } $filters = [ 'q' => $_GET['q'] ?? '', 'status' => $_GET['status'] ?? 'all', 'category_id' => $_GET['category_id'] ?? '', 'stock_state' => $_GET['stock_state'] ?? '' ]; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $data = $this->productService->getSellerProducts($userId, $filters, $page, 20); $categories = $this->categoryModel->getActiveCategories(); $this->render('seller/products/index', array_merge($data, [ 'store' => $store, 'categories' => $categories, 'pageTitle' => 'จัดการสินค้า - CARGOO Seller Center', 'activeNav' => 'products' ]), 'seller'); } public function create(): void { $userId = Auth::id(); $store = $this->storeService->getStoreByUserId($userId); if (!$store) { $this->redirect('/seller/register'); } $categories = $this->categoryModel->getActiveCategories(); $this->render('seller/products/create', [ 'store' => $store, 'categories' => $categories, 'pageTitle' => 'เพิ่มสินค้าใหม่ - CARGOO Seller Center', 'activeNav' => 'product_create' ], 'seller'); } public function store(): void { $userId = Auth::id(); $result = $this->productService->createProduct($userId, $_POST, $_FILES); if (!$result['success']) { Session::setFlash('danger', $result['message']); $this->redirect('/seller/products/create'); } Session::setFlash('success', $result['message']); $this->redirect('/seller/products'); } public function edit(string $id): void { $userId = Auth::id(); $store = $this->storeService->getStoreByUserId($userId); if (!$store) { $this->redirect('/seller/register'); } $product = $this->productService->getSellerProductDetail((int)$id, $userId); if (!$product) { Session::setFlash('danger', 'ไม่พบสินค้า หรือคุณไม่มีสิทธิ์แก้ไขสินค้านี้'); $this->redirect('/seller/products'); } $categories = $this->categoryModel->getActiveCategories(); $this->render('seller/products/edit', [ 'store' => $store, 'product' => $product, 'categories' => $categories, 'pageTitle' => 'แก้ไขสินค้า: ' . e($product['name']) . ' - CARGOO', 'activeNav' => 'products' ], 'seller'); } public function update(string $id): void { $userId = Auth::id(); $result = $this->productService->updateProduct((int)$id, $userId, $_POST, $_FILES); if (!$result['success']) { Session::setFlash('danger', $result['message']); $this->redirect('/seller/products/' . $id . '/edit'); } Session::setFlash('success', $result['message']); $this->redirect('/seller/products'); } public function delete(string $id): void { $userId = Auth::id(); $result = $this->productService->deleteProduct((int)$id, $userId); if (!$result['success']) { Session::setFlash('danger', $result['message']); } else { Session::setFlash('success', $result['message']); } $this->redirect('/seller/products'); } public function deleteImage(string $id): void { $userId = Auth::id(); $productId = (int)($_POST['product_id'] ?? 0); $result = $this->productService->deleteProductImage((int)$id, $productId, $userId); if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json($result); } if (!$result['success']) { Session::setFlash('danger', $result['message']); } else { Session::setFlash('success', $result['message']); } $this->redirect('/seller/products/' . $productId . '/edit'); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings