1 | <?php |
||
2 | |||
3 | namespace SleepingOwl\Admin; |
||
4 | |||
5 | use Illuminate\Support\Collection; |
||
6 | use Illuminate\Support\Facades\Route; |
||
7 | use Illuminate\Support\Str; |
||
8 | use SleepingOwl\Admin\Contracts\Navigation\NavigationInterface; |
||
9 | use SleepingOwl\Admin\Contracts\Navigation\PageInterface; |
||
10 | |||
11 | class Navigation extends \KodiComponents\Navigation\Navigation implements NavigationInterface |
||
12 | { |
||
13 | protected $currentPage; |
||
14 | protected $currentUrl; |
||
15 | |||
16 | /** |
||
17 | * Overload current page. |
||
18 | * @return \KodiComponents\Navigation\Contracts\PageInterface|null |
||
19 | */ |
||
20 | public function getCurrentPage() |
||
21 | { |
||
22 | $this->setAliasesId($this->getPages()); |
||
23 | $this->findActivePage(); |
||
24 | |||
25 | return $this->currentPage; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Set Alias Id to Page. |
||
30 | * @param Collection $pages |
||
31 | */ |
||
32 | public function setAliasesId(Collection $pages) |
||
33 | { |
||
34 | $pages->each(function (PageInterface $page) { |
||
35 | $page->setAliasId(); |
||
36 | |||
37 | if ($page->getPages()->count()) { |
||
38 | $this->setAliasesId($page->getPages()); |
||
39 | } |
||
40 | }); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string $url |
||
45 | * @param array $foundPages |
||
46 | */ |
||
47 | protected function findActive($url, array &$foundPages) |
||
48 | { |
||
49 | $this->findPageByAliasId($this->getPages(), $url); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param Collection $pages |
||
54 | * @param $url |
||
55 | */ |
||
56 | protected function findPageByAliasId(Collection $pages, $url) |
||
57 | { |
||
58 | $pages->each(function (PageInterface $page) use ($url) { |
||
59 | $urlPath = parse_url($url, PHP_URL_PATH); |
||
60 | |||
61 | if (Route::current()) { |
||
62 | $parameters = collect(Route::current()->parameters()); |
||
63 | |||
64 | if ($parameters->has('adminModel')) { |
||
65 | $routeUrl = route('admin.model', [ |
||
66 | 'adminModel' => Str::snake(class_basename($parameters->get('adminModel'))), |
||
67 | ]); |
||
68 | |||
69 | $urlPath = parse_url($routeUrl, PHP_URL_PATH); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | if ($urlPath) { |
||
74 | if (md5($urlPath) == $page->getAliasId()) { |
||
75 | $this->currentPage = $page; |
||
76 | |||
77 | return; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | $this->findPageByAliasId($page->getPages(), $url); |
||
82 | }); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return bool |
||
87 | */ |
||
88 | protected function findActivePage() |
||
89 | { |
||
90 | if (! is_null($this->currentPage)) { |
||
91 | return true; |
||
92 | } |
||
93 | |||
94 | $foundPages = []; |
||
95 | |||
96 | $url = $this->getCurrentUrl(); |
||
97 | |||
98 | $this->findActive($url, $foundPages); |
||
99 | |||
100 | if (! is_null($this->currentPage)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
101 | $this->currentPage->setActive(); |
||
102 | } |
||
103 | |||
104 | if (config('navigation.aliases')) { |
||
105 | $this->findActiveByAliases( |
||
106 | ltrim(parse_url($url, PHP_URL_PATH), '/') |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | return false; |
||
111 | } |
||
112 | } |
||
113 |