HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/anvil.komma.pro/app/KommaApp/Kms/Composers/KmsSidebarMenuComposer.php
<?php

namespace App\KommaApp\Kms\Composers;

/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

use App\KommaApp\Kms\Core\KmsInterface;
use App\KommaApp\Sites\Models\Site;
use App\KommaApp\Users\Models\User;

class KmsSidebarMenuComposer
{
    protected $kms;

    public function __construct()
    {
        $this->kms = \App::make(KmsInterface::class);
    }

    public function compose($view)
    {
        $viewData = $view->getData();

        $currentSiteSlug = $this->kms->getSiteSlug();

        //$currentSectionSlug = $this->getCurrentSectionSlug();
        $currentSectionSlug = isset($viewData['section']) ? $viewData['section']->getSlug() : $this->getCurrentSectionSlug();

        $currentSectionSubSlug = $this->getCurrentSectionSubSlug();

        //Generate the base menu structure
        $menuStructure = $this->getBaseItems($currentSectionSlug, $currentSectionSubSlug);

        //Load all the available sites
        $sites = User::with('sites')->where('id', '=', \Auth::user()->id)->first()->sites;

        //If there is only one, merge the siteSub items, sot it is not a sub menu
        if ($sites->count() == 1) {
            $menuStructure = array_merge($menuStructure, $this->getSiteSubItems($sites->first(), $currentSiteSlug, $currentSectionSlug));
        } //If there are multiple sites, loop and ad these as an sub menu
        else {
            foreach ($sites as $site) {
                // Only show site that have translations defined
                if ($site->languages->isEmpty()) {
                    continue;
                }

                $menuStructure = array_merge($menuStructure,
                    [
                        [
                            'name' => $site->name,
                            'subItems' => $this->getSiteSubItems($site, $currentSiteSlug, $currentSectionSlug),
                            'active' => ($site->slug == $currentSiteSlug),
                        ],
                    ]
                );
            }
        }

        $view->with('menuStructure', $menuStructure);
    }

    protected function getCurrentSectionSlug()
    {
        $route = explode('/', \Route::current()->uri());
        if (\Route::current()->parameter('site')) {
            return $route[2];
        }

        return isset($route[1]) ? $route[1] : null;
    }

    protected function getCurrentSectionSubSlug()
    {
        $route = explode('/', \Route::current()->uri());
        if (\Route::current()->parameter('site')) {
            if (isset($route[3])) {
                return $route[3];
            }
        }

        return isset($route[2]) ? $route[2] : null;
    }

    /**
     * This functions will return the basic menu items
     *
     * @param $currentSectionSlug
     * @param $currentSectionSubSlug
     * @return array
     */
    protected function getBaseItems($currentSectionSlug, $currentSectionSubSlug)
    {
        $items = [];

//        $items[] = [
//            'name' => \Lang::get('kms/SidebarMenu.dashboard'),
        ////            'url' => route('kms.dashboard.index'),
//            'url' => route('dashboard.index'),
//            'active' => ('' == $currentSectionSlug)
//        ];

        $items[] = [
            'name' => \Lang::get('kms/SidebarMenu.users'),
            'url' => route('users.index'),
            'active' => ('users' == $currentSectionSlug),
        ];

//        $items[] = [
//            'name' => \Lang::get('kms/SidebarMenu.posts'),
//            'url' => route('posts.index'),
//            'active' => ('posts' == $currentSectionSlug)
//        ];

        // Allow site(s) editing only for SuperAdmin and when projects has multiple sites
        if (\Auth::user()->role->id == 1 && \Config::get('app.multipleSites')) {
            $items[] = [
                'name' => 'Sites',
                'url' => route('sites.index'),
                'active' => ('sites' == $currentSectionSlug),
            ];
        }

        //  niet nodig want er zijn werken-bij-... sites ???
//        $items[] = [
//            'name' => \Lang::get('kms/SidebarMenu.vacancies'),
//            'url' => route('vacancies.index'),
//            'active' => ('vacancies' == $currentSectionSlug)
//        ];

        return $items;
    }

    /**
     * This will return the site specific items
     *
     * @param $site
     * @param $currentSiteSlug
     * @param $currentSectionSlug
     * @return array
     */
    protected function getSiteSubItems(Site $site, $currentSiteSlug, $currentSectionSlug)
    {
        $items = [
            [
                'name' => 'Pagina\'s',
                'url' => route('pages.index', ['site' => $site->slug]),
                'active' => ('pages' == $currentSectionSlug && $site->slug == $currentSiteSlug),
            ],
            [
                'name' => 'Werknemers',
                'url' => route('employees.index', ['site' => $site->slug]),
                'active' => ('employees' == $currentSectionSlug && $site->slug == $currentSiteSlug),
            ],
            [
                'name' => 'Specialismen',
                'url' => route('specialisms.index', ['site' => $site->slug]),
                'active' => ('specialisms' == $currentSectionSlug && $site->slug == $currentSiteSlug),
            ],
            [
                'name' => 'Referenties',
                'url' => route('references.index', ['site' => $site->slug]),
                'active' => ('references' == $currentSectionSlug && $site->slug == $currentSiteSlug),
            ],
        ];

        if ($site->slug === 'job-precision') {
            $items[] = [
                'name' => 'Marktsegmenten',
                'url' => route('segments.index', ['site' => $site->slug]),
                'active' => ('segments' == $currentSectionSlug && $site->slug == $currentSiteSlug),
            ];
        }

        if ($site->slug === 'machinefabriek-de-valk') {
            $items[] = [
                'name' => 'Marktsegmenten',
                'url' => route('segments.index', ['site' => $site->slug]),
                'active' => ('segments' == $currentSectionSlug && $site->slug == $currentSiteSlug),
            ];
        }

        return $items;
    }
}