fix active menu

This commit is contained in:
Билай Станислав 2024-09-04 16:47:05 +03:00
parent 68bf73e764
commit 9f132e7bc4
3 changed files with 52 additions and 27 deletions

View File

@ -37,30 +37,6 @@ class Menu extends Model
]; ];
} }
public static function getChild(int $id)
{
$collection = Menu::where("parent_id", $id)->get();
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;
}
}
}
return false;
}
/** /**
* @return string[] * @return string[]
*/ */

View File

@ -0,0 +1,49 @@
<?php
namespace kernel\services;
use app\helpers\Debug;
use kernel\models\Menu;
use kernel\Request;
class MenuService
{
public static function getChild(int $id)
{
$collection = Menu::where("parent_id", $id)->get();
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;
}
public static function getActiveStatus($url)
{
if ($url === Request::getUrlPath()){
return true;
} else {
if (strripos(\kernel\Request::getUrlPath(), ($url . "/page")) !== false) {
return true;
}
}
return false;
}
}

View File

@ -5,13 +5,13 @@
?> ?>
<ul class="list-unstyled components mb-5"> <ul class="list-unstyled components mb-5">
<?php foreach ($menu as $item): <?php foreach ($menu as $item):
$child = \kernel\models\Menu::getChild($item->id); $child = \kernel\services\MenuService::getChild($item->id);
if ($child): ?> if ($child): ?>
<li> <li>
<a href="#item<?=$item->id?>" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle"><?= $item->label ?></a> <a href="#item<?=$item->id?>" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle"><?= $item->label ?></a>
<ul class="collapse list-unstyled <?= \kernel\models\Menu::hasActiveChild($item->id) ? "show" : "" ?>" id="item<?=$item->id?>"> <ul class="collapse list-unstyled <?= \kernel\services\MenuService::hasActiveChild($item->id) ? "show" : "" ?>" id="item<?=$item->id?>">
<?php foreach ($child as $subitem): ?> <?php foreach ($child as $subitem): ?>
<li class="<?= $subitem->url === \kernel\Request::getUrlPath() ? "active" : "" ?>"> <li class="<?= \kernel\services\MenuService::getActiveStatus($subitem->url) ? "active" : "" ?>">
<a href="<?= $subitem->url ?>"><?= $subitem->label ?></a> <a href="<?= $subitem->url ?>"><?= $subitem->label ?></a>
</li> </li>
<?php endforeach; ?> <?php endforeach; ?>