Files
web_work/app/Models/Category.php
kirill.khorkov d2c15ec37f [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
2026-01-03 11:48:14 +03:00

160 lines
5.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use App\Core\Model;
/**
* Category - модель категории товаров
*/
class Category extends Model
{
protected string $table = 'categories';
protected string $primaryKey = 'category_id';
/**
* Получить активные категории
*/
public function getActive(): array
{
$sql = "SELECT * FROM {$this->table}
WHERE is_active = TRUE
ORDER BY sort_order, name";
return $this->query($sql);
}
/**
* Получить родительские категории (без parent_id)
*/
public function getParent(): array
{
$sql = "SELECT * FROM {$this->table}
WHERE parent_id IS NULL AND is_active = TRUE
ORDER BY sort_order, name";
return $this->query($sql);
}
/**
* Получить подкатегории
*/
public function getChildren(int $parentId): array
{
$sql = "SELECT * FROM {$this->table}
WHERE parent_id = ? AND is_active = TRUE
ORDER BY sort_order, name";
return $this->query($sql, [$parentId]);
}
/**
* Получить категорию по slug
*/
public function findBySlug(string $slug): ?array
{
return $this->findWhere(['slug' => $slug]);
}
/**
* Получить все категории с количеством товаров
*/
public function getAllWithProductCount(): array
{
$sql = "SELECT c1.*, c2.name as parent_name,
(SELECT COUNT(*) FROM products p WHERE p.category_id = c1.category_id) as product_count
FROM {$this->table} c1
LEFT JOIN {$this->table} c2 ON c1.parent_id = c2.category_id
ORDER BY c1.sort_order, c1.name";
return $this->query($sql);
}
/**
* Создать категорию
*/
public function createCategory(array $data): ?int
{
$slug = $this->generateSlug($data['name']);
return $this->create([
'name' => $data['name'],
'slug' => $slug,
'parent_id' => $data['parent_id'] ?? null,
'description' => $data['description'] ?? null,
'sort_order' => $data['sort_order'] ?? 0,
'is_active' => $data['is_active'] ?? true
]);
}
/**
* Обновить категорию
*/
public function updateCategory(int $id, array $data): bool
{
$updateData = [
'name' => $data['name'],
'slug' => $this->generateSlug($data['name']),
'parent_id' => $data['parent_id'] ?? null,
'description' => $data['description'] ?? null,
'sort_order' => $data['sort_order'] ?? 0,
'is_active' => $data['is_active'] ?? true,
'updated_at' => date('Y-m-d H:i:s')
];
return $this->update($id, $updateData);
}
/**
* Безопасное удаление категории
*/
public function safeDelete(int $id): array
{
// Проверяем наличие товаров
$sql = "SELECT COUNT(*) as cnt FROM products WHERE category_id = ?";
$result = $this->queryOne($sql, [$id]);
$productCount = (int) $result['cnt'];
// Проверяем наличие дочерних категорий
$sql = "SELECT COUNT(*) as cnt FROM {$this->table} WHERE parent_id = ?";
$result = $this->queryOne($sql, [$id]);
$childCount = (int) $result['cnt'];
if ($productCount > 0 || $childCount > 0) {
// Скрываем вместо удаления
$this->update($id, ['is_active' => false]);
return [
'deleted' => false,
'hidden' => true,
'reason' => $productCount > 0 ? 'has_products' : 'has_children'
];
}
// Удаляем полностью
$this->delete($id);
return ['deleted' => true, 'hidden' => false];
}
/**
* Генерация slug из названия
*/
private function generateSlug(string $name): string
{
$slug = mb_strtolower($name);
// Транслитерация
$transliteration = [
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'ts', 'ч' => 'ch',
'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '',
'э' => 'e', 'ю' => 'yu', 'я' => 'ya'
];
$slug = strtr($slug, $transliteration);
$slug = preg_replace('/[^a-z0-9]+/', '-', $slug);
$slug = trim($slug, '-');
return $slug;
}
}