✨ New Features: - Reviews system with 1-5 star ratings - User can add, edit, and delete their own reviews - One review per product per user (DB constraint) - Automatic average rating calculation - Review count tracking - Interactive star selection UI - AJAX-powered review submission - Responsive design for all devices 🗄️ Database: - New 'reviews' table with full structure - Added 'rating' and 'review_count' fields to products - PostgreSQL triggers for automatic rating updates - Database functions for rating calculations - Indexes for performance optimization 📦 Backend (PHP): - Review model with 15+ methods - ReviewController with 5 actions - Updated Product model to include ratings - Updated ProductController to load reviews - 5 new API endpoints 🎨 Frontend: - Reviews list component (_reviews_list.php) - Review form component (_review_form.php) - Reviews sechow page - Star ratings in catalog view - Interactive JavaScript (200+ lines) - Adaptive styles (400+ lines) 🔒 Security: - Server-side authorization checks - XSS protection (htmlspecialchars) - SQL injection protection (PDO prepared) - Input validation (client + server) - Access control for review editing 📝 Modified Files: - app/Models/Product.php - added rating fields to queries - app/Controllers/ProductController.php - loads reviews - app/Views/products/show.php - reviews section - app/Views/products/catalog.php - star ratings - config/routes.php - review endpoints - public/style_for_cite.less - rating styles 🆕 New Files: - app/Models/Review.php - app/Controllers/ReviewController.php - app/Views/products/_reviews_list.php - app/Views/products/_review_form.php
224 lines
6.1 KiB
PHP
224 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use App\Models\Cart;
|
|
use App\Models\Product;
|
|
|
|
class CartController extends Controller
|
|
{
|
|
private Cart $cartModel;
|
|
private Product $productModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->cartModel = new Cart();
|
|
$this->productModel = new Product();
|
|
}
|
|
|
|
public function index(): void
|
|
{
|
|
$this->requireAuth();
|
|
|
|
if ($this->isAdmin()) {
|
|
$this->redirect('/catalog');
|
|
return;
|
|
}
|
|
|
|
$user = $this->getCurrentUser();
|
|
$cartItems = $this->cartModel->getUserCart($user['id']);
|
|
$totals = $this->cartModel->getCartTotal($user['id']);
|
|
|
|
$this->view('cart/checkout', [
|
|
'user' => $user,
|
|
'cartItems' => $cartItems,
|
|
'totalQuantity' => $totals['quantity'],
|
|
'totalAmount' => $totals['amount']
|
|
]);
|
|
}
|
|
|
|
public function add(): void
|
|
{
|
|
if (!$this->isAuthenticated()) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Требуется авторизация'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if ($this->isAdmin()) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Доступ запрещен'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$productId = (int) $this->getPost('product_id', 0);
|
|
$quantity = (int) $this->getPost('quantity', 1);
|
|
$userId = $this->getCurrentUser()['id'];
|
|
|
|
if ($productId <= 0) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Неверный товар'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$product = $this->productModel->find($productId);
|
|
|
|
if (!$product || !$product['is_available']) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Товар не найден'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$cartItem = $this->cartModel->getItem($userId, $productId);
|
|
$currentQty = $cartItem ? $cartItem['quantity'] : 0;
|
|
$newQty = $currentQty + $quantity;
|
|
|
|
if ($newQty > $product['stock_quantity']) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Недостаточно товара на складе'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$result = $this->cartModel->addItem($userId, $productId, $quantity);
|
|
|
|
if ($result) {
|
|
$cartCount = $this->cartModel->getCount($userId);
|
|
$this->json([
|
|
'success' => true,
|
|
'cart_count' => $cartCount,
|
|
'message' => 'Товар добавлен в корзину'
|
|
]);
|
|
} else {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Ошибка при добавлении в корзину'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
if (!$this->isAuthenticated()) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Требуется авторизация'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if ($this->isAdmin()) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Доступ запрещен'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$productId = (int) $this->getPost('product_id', 0);
|
|
$quantity = (int) $this->getPost('quantity', 1);
|
|
$userId = $this->getCurrentUser()['id'];
|
|
|
|
if ($quantity <= 0) {
|
|
$this->cartModel->removeItem($userId, $productId);
|
|
$cartCount = $this->cartModel->getCount($userId);
|
|
$this->json([
|
|
'success' => true,
|
|
'cart_count' => $cartCount
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$product = $this->productModel->find($productId);
|
|
if (!$product || $quantity > $product['stock_quantity']) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Недостаточно товара на складе'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$result = $this->cartModel->updateQuantity($userId, $productId, $quantity);
|
|
|
|
if ($result) {
|
|
$totals = $this->cartModel->getCartTotal($userId);
|
|
$this->json([
|
|
'success' => true,
|
|
'cart_count' => $totals['quantity'],
|
|
'total_amount' => $totals['amount']
|
|
]);
|
|
} else {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Ошибка при обновлении'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function remove(): void
|
|
{
|
|
if (!$this->isAuthenticated()) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Требуется авторизация'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if ($this->isAdmin()) {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Доступ запрещен'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$productId = (int) $this->getPost('product_id', 0);
|
|
$userId = $this->getCurrentUser()['id'];
|
|
|
|
$result = $this->cartModel->removeItem($userId, $productId);
|
|
|
|
if ($result) {
|
|
$cartCount = $this->cartModel->getCount($userId);
|
|
$this->json([
|
|
'success' => true,
|
|
'cart_count' => $cartCount
|
|
]);
|
|
} else {
|
|
$this->json([
|
|
'success' => false,
|
|
'message' => 'Ошибка при удалении'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function count(): void
|
|
{
|
|
if (!$this->isAuthenticated()) {
|
|
$this->json([
|
|
'success' => true,
|
|
'cart_count' => 0
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$userId = $this->getCurrentUser()['id'];
|
|
$count = $this->cartModel->getCount($userId);
|
|
|
|
$this->json([
|
|
'success' => true,
|
|
'cart_count' => $count
|
|
]);
|
|
}
|
|
}
|