шаг 4 завершен

This commit is contained in:
Alisa
2026-06-04 01:30:45 +03:00
parent 62ce8c4d01
commit 238831ad3a
26 changed files with 1703 additions and 70 deletions
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Controllers\Admin;
use App\Repositories\CategoryRepository;
final class CategoryAdminController
{
public function index(): void
{
$repository = new CategoryRepository();
$message = null;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$repository->create([
'parent_id' => $_POST['parent_id'] !== '' ? (int) $_POST['parent_id'] : null,
'name' => trim((string) $_POST['name']),
'slug' => trim((string) $_POST['slug']),
'h1' => trim((string) ($_POST['h1'] ?? '')),
'seo_title' => trim((string) ($_POST['seo_title'] ?? '')),
'seo_description' => trim((string) ($_POST['seo_description'] ?? '')),
'seo_keywords' => trim((string) ($_POST['seo_keywords'] ?? '')),
'sort_order' => (int) ($_POST['sort_order'] ?? 100),
'is_active' => isset($_POST['is_active']) ? 1 : 0,
]);
$message = 'Категория добавлена.';
} catch (\Throwable $exception) {
$error = app_env('APP_DEBUG', 'false') === 'true' ? $exception->getMessage() : 'Не удалось добавить категорию.';
}
}
view('admin/categories', [
'title' => 'Категории - админка Рыбсток',
'breadcrumbs' => [
['title' => 'Главная', 'url' => '/'],
['title' => 'Админка', 'url' => '/admin/'],
['title' => 'Категории'],
],
'categories' => $repository->tree(),
'flatCategories' => $repository->flatActive(),
'message' => $message,
'error' => $error,
]);
}
}
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Controllers\Admin;
use App\Repositories\CategoryRepository;
use App\Repositories\ProductRepository;
final class ProductAdminController
{
public function index(): void
{
$categoryRepository = new CategoryRepository();
$productRepository = new ProductRepository();
$message = null;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$oldPackagePrice = $_POST['old_package_price'] !== '' ? (float) $_POST['old_package_price'] : null;
$discountPercent = $_POST['discount_percent'] !== '' ? (float) $_POST['discount_percent'] : null;
$packagePrice = (float) ($_POST['package_price'] ?? 0);
$discountType = 'none';
if ($oldPackagePrice !== null && $discountPercent !== null && $discountPercent > 0) {
$packagePrice = round($oldPackagePrice * (1 - ($discountPercent / 100)), 2);
$discountType = 'percent';
} elseif ($oldPackagePrice !== null) {
$discountType = 'manual_old_price';
}
$productRepository->createWithVariant([
'category_id' => $_POST['category_id'] !== '' ? (int) $_POST['category_id'] : null,
'name' => trim((string) $_POST['name']),
'slug' => trim((string) $_POST['slug']),
'short_description' => trim((string) ($_POST['short_description'] ?? '')),
'seo_title' => trim((string) ($_POST['seo_title'] ?? '')),
'seo_description' => trim((string) ($_POST['seo_description'] ?? '')),
'seo_keywords' => trim((string) ($_POST['seo_keywords'] ?? '')),
'base_unit' => (string) ($_POST['base_unit'] ?? 'kg'),
'is_published' => isset($_POST['is_published']) ? 1 : 0,
'variant_name' => trim((string) ($_POST['variant_name'] ?? '')),
'package_quantity' => (float) ($_POST['package_quantity'] ?? 1),
'step_quantity' => (float) ($_POST['step_quantity'] ?? 1),
'price_per_unit' => $_POST['price_per_unit'] !== '' ? (float) $_POST['price_per_unit'] : null,
'package_price' => $packagePrice,
'old_package_price' => $oldPackagePrice,
'discount_type' => $discountType,
'discount_percent' => $discountPercent,
]);
$message = 'Товар добавлен.';
} catch (\Throwable $exception) {
$error = app_env('APP_DEBUG', 'false') === 'true' ? $exception->getMessage() : 'Не удалось добавить товар.';
}
}
view('admin/products', [
'title' => 'Товары - админка Рыбсток',
'breadcrumbs' => [
['title' => 'Главная', 'url' => '/'],
['title' => 'Админка', 'url' => '/admin/'],
['title' => 'Товары'],
],
'categories' => $categoryRepository->flatActive(),
'products' => $productRepository->adminLatest(30),
'message' => $message,
'error' => $error,
]);
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Repositories\CategoryRepository;
use App\Repositories\ProductRepository;
final class CatalogController
{
public function index(): void
{
$categoryRepository = new CategoryRepository();
$productRepository = new ProductRepository();
view('catalog/index', [
'title' => 'Каталог продукции - Рыбсток',
'metaDescription' => 'Полный каталог Рыбсток: рыба, морепродукты, икра, мясо, сыры, полуфабрикаты и продукты с доставкой.',
'metaKeywords' => 'каталог Рыбсток, рыба, морепродукты, мясо, сыры, полуфабрикаты',
'breadcrumbs' => [
['title' => 'Главная', 'url' => '/'],
['title' => 'Каталог'],
],
'categories' => $categoryRepository->tree(),
'currentCategory' => null,
'products' => $productRepository->catalogPreview(48),
]);
}
public function category(string $slug): void
{
$categoryRepository = new CategoryRepository();
$productRepository = new ProductRepository();
$category = $categoryRepository->findBySlug($slug);
if ($category === null) {
http_response_code(404);
view('pages/404', [
'title' => 'Категория не найдена - Рыбсток',
'breadcrumbs' => [
['title' => 'Главная', 'url' => '/'],
['title' => 'Каталог', 'url' => '/catalog'],
['title' => 'Категория не найдена'],
],
]);
return;
}
view('catalog/index', [
'title' => ($category['seo_title'] ?: $category['name']) . ' - Рыбсток',
'metaDescription' => $category['seo_description'] ?: '',
'metaKeywords' => $category['seo_keywords'] ?: '',
'breadcrumbs' => $categoryRepository->breadcrumbs((int) $category['id']),
'categories' => $categoryRepository->tree(),
'currentCategory' => $category,
'products' => $productRepository->forCategory((int) $category['id'], 48),
]);
}
}