[MVC] Полная миграция на MVC архитектуру

- Создано ядро MVC: App, Router, Controller, Model, View, Database
- Созданы модели: User, Product, Category, Cart, Order
- Созданы контроллеры: Home, Auth, Product, Cart, Order, Page, Admin
- Созданы layouts и partials для представлений
- Добавлены все views для страниц
- Настроена маршрутизация с чистыми URL
- Обновлена конфигурация Docker и Apache для mod_rewrite
- Добавлена единая точка входа public/index.php
This commit is contained in:
kirill.khorkov
2026-01-03 11:48:14 +03:00
parent 3f257120fa
commit d2c15ec37f
53 changed files with 8650 additions and 30 deletions

View File

@@ -0,0 +1,218 @@
<?php
namespace App\Controllers;
use App\Core\Controller;
use App\Models\Cart;
use App\Models\Product;
/**
* CartController - контроллер корзины
*/
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();
$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;
}
$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;
}
$productId = (int) $this->getPost('product_id', 0);
$quantity = (int) $this->getPost('quantity', 1);
$userId = $this->getCurrentUser()['id'];
if ($quantity <= 0) {
// Если количество 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;
}
$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
]);
}
}