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/farmfun.komma.pro/app/Komma/Locations/Kms/LocationService.php
<?php

namespace App\Komma\Locations\Kms;

use App\Komma\Kms\Core\ModelService;
use App\Komma\Kms\Menu\Item;
use App\Komma\Locations\Models\Location;
use App\Komma\Users\Models\KmsUserRole;

class LocationService extends ModelService
{
    public function __construct()
    {
        parent::__construct();
        $this->setModelClassName(Location::class);
    }

    public function getSidebarPlanningItems(Location $viewLocation = null)
    {
        $items = [];
        $locations = Location::where('active', 1)->get();

        if (auth()->user()->role == KmsUserRole::Editor && isset(auth()->user()->location_id)) {
            $locations = $locations->where('id', '=', auth()->user()->location_id);
        }

        foreach ($locations as $location) {
            $locationItem = (new Item())
                ->setName($location->city)
                ->setUrl(route('planning', ['location' => $location->id]));

            if ($location->is($viewLocation)) {
                $locationItem->setIsActive(true);
            }

            $items[] = $locationItem;
        }

        return $items;
    }

    public function getModelsForSideBar(): array
    {
        $modelsForSidebar = parent::getModelsForSideBar();
        if (auth()->user()->isAtLeast(KmsUserRole::Admin)) {
            return $modelsForSidebar;
        }

        foreach ($modelsForSidebar as $modelForSidebar) {
            if ($modelForSidebar->getId() == auth()->user()->location_id) {
                return [$modelForSidebar];
            }
        }

        return [];
    }
}