1 | <?php |
||
2 | |||
3 | namespace App\Service; |
||
4 | |||
5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
6 | |||
7 | class AdminControlPanel |
||
8 | { |
||
9 | public const DEFAULT_GROUP = [ |
||
10 | 'parent' => 'root', |
||
11 | 'id' => '', |
||
12 | 'title' => '', |
||
13 | 'icon' => '', |
||
14 | 'display' => 'true', |
||
15 | 'children' => [], |
||
16 | ]; |
||
17 | |||
18 | public const DEFAULT_MENU = [ |
||
19 | 'parent' => '', |
||
20 | 'id' => '', |
||
21 | 'title' => '', |
||
22 | 'href' => '', |
||
23 | 'icon' => '', |
||
24 | 'meta' => [ |
||
25 | 'tabindex' => -1, |
||
26 | ], |
||
27 | ]; |
||
28 | |||
29 | private static $list = []; |
||
30 | |||
31 | private static $tree = []; |
||
32 | |||
33 | /** |
||
34 | * https://stackoverflow.com/a/27360654/4156752 (Thanks to Thunderstriker, arthur and basil). |
||
35 | * |
||
36 | * @param array $flat |
||
37 | * @param string $pidKey |
||
38 | * @param string|null $idKey |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | public static function buildTree($flat, $pidKey, $idKey = null) |
||
43 | { |
||
44 | $grouped = []; |
||
45 | foreach ($flat as $sub) { |
||
46 | $grouped[$sub[$pidKey]][] = $sub; |
||
47 | } |
||
48 | |||
49 | $fnBuilder = function ($siblings) use (&$fnBuilder, $grouped, $idKey) { |
||
50 | foreach ($siblings as $k => $sibling) { |
||
51 | $id = $sibling[$idKey]; |
||
52 | if (isset($grouped[$id])) { |
||
53 | $sibling['children'] = $fnBuilder($grouped[$id]); |
||
54 | } |
||
55 | $siblings[$k] = $sibling; |
||
56 | } |
||
57 | |||
58 | return $siblings; |
||
59 | }; |
||
60 | |||
61 | return $fnBuilder($grouped['root']); |
||
62 | } |
||
63 | |||
64 | public static function loadLibs(string $rootDir, TokenStorageInterface $tokenStorage = null) |
||
65 | { |
||
66 | $libraryDir = $rootDir . '/src/Controller/Panel'; |
||
67 | |||
68 | $libsList = scandir($libraryDir); |
||
69 | foreach ($libsList as $lib) { |
||
70 | if ('.' === $lib || '..' === $lib) { |
||
71 | continue; |
||
72 | } |
||
73 | $file = pathinfo($libraryDir . '/' . $lib); |
||
74 | $class = '\\App\\Controller\\Panel\\' . $file['filename']; |
||
75 | |||
76 | self::$list[call_user_func($class . '::__callNumber')] = $class; |
||
77 | } |
||
78 | ksort(self::$list); |
||
79 | |||
80 | foreach (self::$list as $class) { |
||
81 | $subTree = call_user_func($class . '::__setupNavigation', $tokenStorage); |
||
82 | |||
83 | $isMulti = function ($arr) { |
||
84 | foreach ($arr as $v) { |
||
85 | if (is_array($v)) { |
||
86 | return true; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return false; |
||
91 | }; |
||
92 | |||
93 | if ($isMulti($subTree)) { |
||
94 | foreach ($subTree as $tree) { |
||
95 | self::$tree[] = $tree; |
||
96 | } |
||
97 | } else { |
||
98 | self::$tree[] = $subTree; |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public static function getTree() |
||
104 | { |
||
105 | return self::buildTree(self::$tree, 'parent', 'id'); |
||
106 | } |
||
107 | |||
108 | public static function getFlatTree() |
||
109 | { |
||
110 | return self::$tree; |
||
111 | } |
||
112 | |||
113 | public static function getList() |
||
114 | { |
||
115 | return self::$list; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * TODO: This is a new function, integrate it in HTML. |
||
120 | * |
||
121 | * @param string $page_name |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public static function changePage($page_name) |
||
126 | { |
||
127 | // Choose between 'href', 'hash', 'url' or 'url_js' |
||
128 | $page_changer = 'url_js'; |
||
129 | |||
130 | if ('js' === $page_changer) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
131 | return 'href="javascript:ControlPanel.changePage(\'' . $page_name . '\')" data-toggle="page"'; |
||
132 | } elseif ('url_js' === $page_changer) { |
||
0 ignored issues
–
show
|
|||
133 | return 'href="javascript:ControlPanel.changePage(\'' . $page_name . '\', true)" data-toggle="page"'; |
||
134 | } elseif ('hash' === $page_changer) { |
||
135 | return 'href="#/' . $page_name . '"'; |
||
136 | } elseif ('url' === $page_changer) { |
||
137 | return 'href="https://account.orbitrondev.org/panel/' . $page_name . '"'; |
||
138 | } |
||
139 | |||
140 | return ''; |
||
141 | } |
||
142 | } |
||
143 |