MicroFrameWork/app/services/MenuService.php

55 lines
1.6 KiB
PHP
Raw Normal View History

2024-08-30 16:14:31 +03:00
<?php
namespace app\services;
use app\helpers\Debug;
use kernel\FormModel;
use kernel\models\Menu;
class MenuService
{
public function create(FormModel $form_model): false|Menu
{
$model = new Menu();
$model->parent_id = $form_model->getItem('parent_id');
$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');
if ($model->save()){
return $model;
}
return false;
}
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');
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 [];
}
}