File manager - Edit - /home/webapp69.cm.in.th/u69319090039/Shop39/app/Controllers/ReviewController.php
Back
<?php class ReviewController extends Controller { public function store(): void { Auth::requireAuth(); $this->validateCsrf(); $user = Auth::user(); $db = Database::getInstance(); $orderId = (int)($_POST['order_id'] ?? 0); $orderItemId = (int)($_POST['order_item_id'] ?? 0); $rating = (int)($_POST['rating'] ?? 5); $comment = trim($_POST['comment'] ?? ''); if ($orderId <= 0 || $orderItemId <= 0) { Session::setFlash('danger', 'ข้อมูลคำสั่งซื้อไม่ถูกต้อง'); $this->redirect('orders'); return; } if ($rating < 1 || $rating > 5) { Session::setFlash('danger', 'คะแนนต้องอยู่ระหว่าง 1 ถึง 5 ดาว'); $this->redirect("orders/{$orderId}"); return; } // Verify order belongs to current user $stmt = $db->prepare("SELECT * FROM orders WHERE id = :oid AND user_id = :uid LIMIT 1"); $stmt->execute(['oid' => $orderId, 'uid' => $user['id']]); $order = $stmt->fetch(); if (!$order) { Session::setFlash('danger', 'ไม่พบรายการคำสั่งซื้อ หรือไม่มีสิทธิ์เข้าถึง'); $this->redirect('orders'); return; } // Verify item belongs to this order $itemStmt = $db->prepare("SELECT * FROM order_items WHERE id = :item_id AND order_id = :oid LIMIT 1"); $itemStmt->execute(['item_id' => $orderItemId, 'oid' => $orderId]); $item = $itemStmt->fetch(); if (!$item) { Session::setFlash('danger', 'ไม่พบรายการสินค้าในคำสั่งซื้อนี้'); $this->redirect("orders/{$orderId}"); return; } // Check if item has already been reviewed $reviewModel = new Review(); $existing = $reviewModel->getByOrderItemId($orderItemId); if ($existing) { // Update existing review $stmt = $db->prepare("UPDATE reviews SET rating = :rating, comment = :comment, status = 'approved' WHERE id = :rid"); $stmt->execute([ 'rating' => $rating, 'comment' => $comment, 'rid' => $existing['id'] ]); $reviewId = (int)$existing['id']; } else { // Create new review $reviewId = $reviewModel->create([ 'order_id' => $orderId, 'order_item_id' => $orderItemId, 'product_id' => $item['product_id'], 'user_id' => $user['id'], 'rating' => $rating, 'comment' => Security::sanitizeString($comment), 'status' => 'approved' ]); } // Handle Image Uploads if any if (isset($_FILES['images']) && !empty($_FILES['images']['name'][0])) { $uploadDir = SITE_ROOT . '/public/uploads/reviews/'; if (!file_exists($uploadDir)) { mkdir($uploadDir, 0777, true); } $savedImages = []; $totalFiles = count($_FILES['images']['name']); for ($i = 0; $i < min($totalFiles, 5); $i++) { if ($_FILES['images']['error'][$i] === UPLOAD_ERR_OK) { $tmpName = $_FILES['images']['tmp_name'][$i]; $ext = strtolower(pathinfo($_FILES['images']['name'][$i], PATHINFO_EXTENSION)); $allowed = ['jpg', 'jpeg', 'png', 'webp', 'gif']; if (in_array($ext, $allowed)) { $filename = 'rev_' . $reviewId . '_' . uniqid() . '.' . $ext; $destination = $uploadDir . $filename; if (move_uploaded_file($tmpName, $destination)) { $savedImages[] = '/uploads/reviews/' . $filename; } } } } if (!empty($savedImages)) { $reviewModel->addReviewImages($reviewId, $savedImages); } } Session::setFlash('success', 'บันทึกการรีวิวสินค้าเรียบร้อยแล้ว ขอบคุณสำหรับความคิดเห็น!'); $this->redirect("orders/{$orderId}"); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.25 |
proxy
|
phpinfo
|
Settings