fix active menu
This commit is contained in:
		| @@ -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[] | ||||
|      */ | ||||
|   | ||||
							
								
								
									
										49
									
								
								kernel/services/MenuService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								kernel/services/MenuService.php
									
									
									
									
									
										Normal 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; | ||||
|     } | ||||
| } | ||||
| @@ -5,13 +5,13 @@ | ||||
| ?> | ||||
| <ul class="list-unstyled components mb-5"> | ||||
|     <?php foreach ($menu as $item): | ||||
|         $child = \kernel\models\Menu::getChild($item->id); | ||||
|         $child = \kernel\services\MenuService::getChild($item->id); | ||||
|         if ($child): ?> | ||||
|             <li> | ||||
|                 <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): ?> | ||||
|                     <li class="<?= $subitem->url === \kernel\Request::getUrlPath() ? "active" : "" ?>"> | ||||
|                     <li class="<?= \kernel\services\MenuService::getActiveStatus($subitem->url) ? "active" : "" ?>"> | ||||
|                         <a href="<?= $subitem->url ?>"><?= $subitem->label ?></a> | ||||
|                     </li> | ||||
|                     <?php endforeach; ?> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user