From ca1fd4149814047f3f97bd92a6a33077d0fa9fd8 Mon Sep 17 00:00:00 2001 From: HerrHase Date: Sun, 7 Nov 2021 16:44:21 +0100 Subject: [PATCH] adding #10 --- app/Functions/Blade.php | 26 ++++++++++++++ app/Repositories/MenuRepository.php | 39 ++++++++++++++++----- resources/views/layout.blade.php | 3 ++ resources/views/partials/menu.blade.php | 46 +++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 resources/views/partials/menu.blade.php diff --git a/app/Functions/Blade.php b/app/Functions/Blade.php index 39d06ef..2a4e883 100644 --- a/app/Functions/Blade.php +++ b/app/Functions/Blade.php @@ -117,3 +117,29 @@ function assetsUrl(string $id, array $options = NULL) return $_ENV['DIRECTUS_API_URL'].'/assets/'.$id.$query; } + +/** + * + * + * + */ +function isCurrentPage($slug, $class = 'current') +{ + // parse current url + $url = parse_url($_SERVER['REQUEST_URI']); + + // getting path, remove first "/"" + $path = ltrim($url['path'], '/'); + + // parse empty in NULL + // @TODO bad solution, check for using parent + if (empty($path)) { + $path = NULL; + } + + if ($path !== $slug) { + $class = NULL; + } + + return $class; +} diff --git a/app/Repositories/MenuRepository.php b/app/Repositories/MenuRepository.php index 1c002fa..5f3756f 100644 --- a/app/Repositories/MenuRepository.php +++ b/app/Repositories/MenuRepository.php @@ -25,7 +25,7 @@ class MenuRepository extends RepositoryAbstract * @param string $slug * @return array */ - public function findOneByName($name) + public function findByName($name) { $results = $this->queryBuilder ->fields([ @@ -42,14 +42,35 @@ class MenuRepository extends RepositoryAbstract ->aliases('page[status]', 'page_status') ->aliases('page[slug]', 'page_slug') ->filter([ - 'page' => [ - 'status' => 'published' - ], - 'menus' => [ - 'menus_id' => [ - 'name' => $name, - 'status' => 'published' - ] + '_or' => [ + [ + '_and' => [ + [ 'page' => [ + 'id' => [ + '_null' => 'true' + ] + ]], + [ 'menus' => [ + 'menus_id' => [ + 'name' => $name, + 'status' => 'published' + ] + ]] + ] + ], + [ + '_and' => [ + [ 'page' => [ + 'status' => 'published' + ]], + [ 'menus' => [ + 'menus_id' => [ + 'name' => $name, + 'status' => 'published' + ] + ]] + ] + ], ] ]) ->find(); diff --git a/resources/views/layout.blade.php b/resources/views/layout.blade.php index b3384de..fa434d9 100644 --- a/resources/views/layout.blade.php +++ b/resources/views/layout.blade.php @@ -1,10 +1,12 @@ @inject('pageRepository', 'App\Repositories\PageRepository') @inject('siteRepository', 'App\Repositories\SiteRepository') +@inject('menuRepository', 'App\Repositories\MenuRepository') @inject('markdownHelper', 'App\Helpers\MarkdownHelper') @php $site = $siteRepository->findOne(); + $menuItems = $menuRepository->findByName('test'); @endphp @@ -32,6 +34,7 @@ Super Gear Directus + @include('partials.menu', [ 'menuItems' => $menuItems ])
diff --git a/resources/views/partials/menu.blade.php b/resources/views/partials/menu.blade.php new file mode 100644 index 0000000..a9c6186 --- /dev/null +++ b/resources/views/partials/menu.blade.php @@ -0,0 +1,46 @@ +@foreach($menuItems['data'] as $item) + @php + + // class if url is same as page + $current = NULL; + + // attribute target + $target = NULL; + + // if page is not null + if ($item['page']) { + $title = $item['page']['title']; + $url = '/'.$item['page']['slug']; + + // if page is current page + $current = isCurrentPage($item['page']['slug'], ' current'); + + // if title is set overwrite + if ($item['title']) { + $title = $item['title']; + } + + // if page empty and only title and url is set + } elseif ($item['title'] && $item['url']) { + $title = $item['title']; + $url = $item['url']; + + // if target set + if (isset($item['target'])) { + $target = 'target='.$item['target']; + + if ($item['target'] === '_blank') { + $target .= ' rel=noreferrer'; + } + } + + } else { + continue; + } + + @endphp + + + {{ $title }} + +@endforeach \ No newline at end of file