Fix LESS import error and refactor project structure
This commit is contained in:
@@ -4,17 +4,11 @@ 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}
|
||||
@@ -23,9 +17,6 @@ class Category extends Model
|
||||
return $this->query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить родительские категории (без parent_id)
|
||||
*/
|
||||
public function getParent(): array
|
||||
{
|
||||
$sql = "SELECT * FROM {$this->table}
|
||||
@@ -34,9 +25,6 @@ class Category extends Model
|
||||
return $this->query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить подкатегории
|
||||
*/
|
||||
public function getChildren(int $parentId): array
|
||||
{
|
||||
$sql = "SELECT * FROM {$this->table}
|
||||
@@ -45,17 +33,11 @@ class Category extends Model
|
||||
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,
|
||||
@@ -66,9 +48,6 @@ class Category extends Model
|
||||
return $this->query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Создать категорию
|
||||
*/
|
||||
public function createCategory(array $data): ?int
|
||||
{
|
||||
$slug = $this->generateSlug($data['name']);
|
||||
@@ -83,9 +62,6 @@ class Category extends Model
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновить категорию
|
||||
*/
|
||||
public function updateCategory(int $id, array $data): bool
|
||||
{
|
||||
$updateData = [
|
||||
@@ -101,23 +77,17 @@ class Category extends Model
|
||||
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,
|
||||
@@ -126,19 +96,14 @@ class Category extends Model
|
||||
];
|
||||
}
|
||||
|
||||
// Удаляем полностью
|
||||
$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',
|
||||
@@ -156,4 +121,3 @@ class Category extends Model
|
||||
return $slug;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user