Menu.php
1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace AC\Admin\Section\Partial;
use AC\Admin\MenuListFactory;
use AC\ListScreenGroupsFactory;
use AC\View;
class Menu
{
private $menu_factory;
public function __construct(MenuListFactory $menu_factory)
{
$this->menu_factory = $menu_factory;
}
public function render(string $current, string $url, bool $is_hidden = false): string
{
$menu = new View([
'items' => $this->get_menu_items(),
'current' => $current,
'screen_link' => $url,
'class' => $is_hidden ? 'hidden' : '',
]);
return $menu->set_template('admin/edit-menu')
->render();
}
private function get_menu_items(): array
{
$items = [];
foreach ($this->menu_factory->create()->all() as $item) {
$items[$item->get_group()][$item->get_key()] = $item->get_label();
}
$grouped = [];
foreach (ListScreenGroupsFactory::create()->get_all() as $group) {
$slug = $group['slug'];
if (empty($items[$slug])) {
continue;
}
if ( ! isset($grouped[$slug])) {
$grouped[$slug]['title'] = $group['label'];
}
natcasesort($items[$slug]);
$grouped[$slug]['options'] = $items[$slug];
unset($items[$slug]);
}
return $grouped;
}
}