Files
BiveEngine/engine/Main/trait.admin.php
2025-12-24 19:19:01 +03:00

110 lines
4.1 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
defined('ROOT_DIR') || exit;
trait Admin {
public array $admin_pages = array();
public array $admin_pages_list = array();
public function admin_page_set($parent_slug, $page_slug, $page_title, $params, $function, $hide = false, $icon = false)
{
if(!isset($params)) $params = array();
$params["page"] = $page_slug;
if($parent_slug && isset($this->admin_pages_list[$parent_slug])) {
$parent = $this->admin_pages_list[$parent_slug];
$link = &$parent["link"];
$parent_level = $link["level"];
$page_info = array(
"level" => $parent_level + 1,
"title" => $page_title,
"function" => $function,
"pages" => array(),
"params" => $params,
"hide" => $hide,
"icon" => $icon,
"slug" => $page_slug
);
$link["pages"][$page_slug] = $page_info;
$page_info["link"] = &$link["pages"][$page_slug];
} else {
$page_info = array(
"level" => 0,
"title" => $page_title,
"function" => $function,
"pages" => array(),
"params" => $params,
"hide" => $hide,
"icon" => $icon,
"slug" => $page_slug
);
$this->admin_pages[$page_slug] = $page_info;
$page_info["link"] = &$this->admin_pages[$page_slug];
}
$this->admin_pages_list[$page_slug] = $page_info;
}
// Проверить есть ли у страницы дочерний элемент по с нужной подстрокой
public function admin_page_has_search($parent_slug, $search): bool
{
if(!$search) return true;
$parent_page = $this->admin_pages_list[$parent_slug];
$parent_page_link = &$parent_page["link"];
if(mb_strpos(mb_strtolower($parent_page["title"]), mb_strtolower($search)) !== false) return true;
return $this->admin_page_has_search_recurse($parent_page_link["pages"], $search);
}
// Рекурсивная функция проверки дочерних элементов для поиска подстроки
private function admin_page_has_search_recurse($pages, $search): bool
{
foreach ($pages as $key => $page) {
if(mb_strpos(mb_strtolower($page["title"]), mb_strtolower($search)) !== false) return true;
$result = $this->admin_page_has_child_recurse($page["pages"], $search);
if($result === true) return true;
}
return false;
}
// Проверить есть ли у страницы дочерний элемент
public function admin_page_has_child($parent_slug, $page_slug): bool
{
$parent_page = &$this->admin_pages_list[$parent_slug]["link"];
if($parent_slug == $page_slug) return true;
return $this->admin_page_has_child_recurse($parent_page["pages"], $page_slug);
}
// Рекурсивная функция проверки дочерних элементов
private function admin_page_has_child_recurse($pages, $page_slug): bool
{
foreach ($pages as $key => $page) {
if($key === $page_slug) return true;
$result = $this->admin_page_has_child_recurse($page["pages"], $page_slug);
if($result === true) return true;
}
return false;
}
// Отрисовка страницы в админ панели
public function admin_page_render($slug, $dive = false): bool
{
$page = $this->admin_pages_list[$slug];
if(!isset($page)) return false;
$link = &$page["link"];
if($page["level"] == 0 && $dive === true)
return $this->admin_page_render(array_shift($link["pages"])["slug"]);
if(is_callable($page["function"])) {
$page["function"]();
return true;
}
$func_name = $page["function"];
$func_body = $GLOBALS[$func_name];
$func_body();
return true;
}
}