MicroFrameWork/kernel/modules/menu/service/MenuService.php

131 lines
3.8 KiB
PHP
Raw Normal View History

2024-08-30 16:14:31 +03:00
<?php
2024-09-06 16:53:20 +03:00
namespace kernel\modules\menu\service;
2024-08-30 16:14:31 +03:00
use Exception;
2024-08-30 16:14:31 +03:00
use kernel\FormModel;
use kernel\helpers\Debug;
2024-08-30 16:14:31 +03:00
use kernel\models\Menu;
use kernel\modules\menu\models\forms\CreateMenuForm;
2024-09-06 16:53:20 +03:00
use kernel\Request;
2024-08-30 16:14:31 +03:00
class MenuService
{
public function create(FormModel $form_model): false|Menu
{
$model = new Menu();
$model->parent_id = $form_model->getItem('parent_id') ?? 0;
2024-08-30 16:14:31 +03:00
$model->icon_file = $form_model->getItem('icon_file');
$model->icon_font = $form_model->getItem('icon_font');
$model->label = $form_model->getItem('label');
$model->url = $form_model->getItem('url');
$model->status = $form_model->getItem('status') ?? 1;
$model->slug = $form_model->getItem('slug');
2024-09-24 14:57:25 +03:00
$model->priority = $form_model->getItem('priority') ?? 5;
2024-08-30 16:14:31 +03:00
if ($model->save()){
return $model;
}
return false;
}
/**
* @throws Exception
*/
public function createItem(array $item): false|Menu
{
$model = new CreateMenuForm();
$model->load($item);
if (!$model->validate()){
throw new Exception('Ошибка создания пункта меню');
}
if ($model->getItem("parent_slug")){
$parent = $this->getBySlug($model->getItem("parent_slug"));
if ($parent){
$model->setItem("parent_id", $parent->id);
}
else {
$model->setItem("parent_id", 0);
}
}
return $this->create($model);
}
public function removeItemBySlug(string $slug)
{
return Menu::where("slug", $slug)->delete();
}
public function getBySlug(string $slug)
{
return Menu::where("slug", $slug)->first();
}
2024-08-30 16:14:31 +03:00
public function update(FormModel $form_model, Menu $menuItem): false|Menu
{
$menuItem->parent_id = $form_model->getItem('parent_id');
$menuItem->icon_file = $form_model->getItem('icon_file');
$menuItem->icon_font = $form_model->getItem('icon_font');
$menuItem->label = $form_model->getItem('label');
$menuItem->url = $form_model->getItem('url');
$menuItem->status = $form_model->getItem('status');
$menuItem->slug = $form_model->getItem('slug');
2024-09-24 14:57:25 +03:00
$menuItem->priority = $form_model->getItem('priority') ?? 5;
2024-08-30 16:14:31 +03:00
if ($menuItem->save()){
return $menuItem;
}
return false;
}
public static function createLabelArr(): array
{
2024-09-03 16:29:44 +03:00
$labelArr[0] = "Корневой пункт меню";
foreach (Menu::where("parent_id", 0)->get()->toArray() as $menuItem) {
2024-08-30 16:14:31 +03:00
$labelArr[$menuItem['id']] = $menuItem['label'];
}
if (!empty($labelArr)) {
return $labelArr;
}
return [];
}
2024-09-06 16:53:20 +03:00
public static function getChild(int $id)
{
2024-09-24 14:57:25 +03:00
$collection = Menu::where("parent_id", $id)->get()->sortBy('priority');
2024-09-06 16:53:20 +03:00
if (!$collection->isEmpty()){
return $collection;
}
return false;
}
public static function hasActiveChild(int $id): bool
{
$child = self::getChild($id);
if (!$child->isEmpty()){
foreach ($child as $item){
// if ($item->url === \kernel\Request::getUrlPath()){
// return true;
// }
if (strripos(Request::getUrlPath(), $item->url) !== false) {
return true;
}
}
}
return false;
}
2024-08-30 16:14:31 +03:00
2024-09-06 16:53:20 +03:00
public static function isActive($url): bool
{
if ($url === Request::getUrlPath()){
return true;
} else {
if (strripos(\kernel\Request::getUrlPath(), ($url . "/page")) !== false) {
return true;
}
}
return false;
}
2024-08-30 16:14:31 +03:00
}