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/netwerkbrabant.komma.pro/app/KommaApp/PastEvents/PastEventService.php
<?php


namespace App\KommaApp\PastEvents;

use App\KommaApp\Base\Service;
use App\KommaApp\PastEvents\Models\PastEvent;
use App\KommaApp\Regions\Models\Region;
use Carbon\Carbon;

class PastEventService extends Service
{

    const EVENTS_PER_PAGE = 6;
    const EVENTS_PER_REGION_PAGE = 9;
    const HIGHLIGHTED_EVENTS = 3;
    private $today;

    public function __construct()
    {
        $this->today = Carbon::now()->addHour();
        $this->today = $this->today->format('Y-m-d H:i:s');

        parent::__construct();
    }

    /**
     * Load the relations after or during query building
     *
     * @param $pastEvents
     * @param bool $queryBuilder
     */
    private function loadRelations(&$pastEvents, $queryBuilder = false){
        if($queryBuilder) $pastEvents->with('translation', 'images', 'type', 'type.translation');
        else $pastEvents->load('translation', 'images', 'type', 'type.translation');
    }

    /**
     * Get the events which are active and bound to a region and are newer then today ordered descending by date
     * with optional parameter to paginate, exclude given ids and get a given amount
     *
     * @param bool $pagination
     * @param array|null $excludeIds
     * @param int|null $amount
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
     */
    public function getPastEvents(bool $pagination = false, bool $loadRelations = false, array $excludeIds = null, int $amount = null)
    {
        $events = $this->site
            ->pastEvents()
            ->has('images')
            ->where('active', 1)
            ->whereNotNull('region_id')
            ->where('date', '<=', $this->today)
            ->orderBy('date','desc')
            ->orderBy('created_at', 'desc');

        if($loadRelations) $this->loadRelations($events, true);

        if($excludeIds) $events = $events->whereNotIn('id', $excludeIds);

        // Get the events paginated or by get with optional a given amount
        if($pagination){
            $events = $events->paginate(self::EVENTS_PER_PAGE);
        }
        else {
            if($amount) $events = $events->take($amount);
            $events = $events->get();
        }
        return $events;
    }

    /**
     * Get the past events which are active and where the region is $regionId and are newer then today ordered descending by date
     * with optional parameter to paginate, exclude given ids and get a given amount
     *
     * @param int $regionId
     * @param bool $pagination
     * @param array|null $excludeIds
     * @param int|null $amount
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
     */
    public function getPastEventsByRegion(int $regionId, bool $pagination = true, array $excludeIds = null, int $amount = null){

        $events = $this->site
            ->pastEvents()
            ->has('images')
            ->where('active', 1)
            ->where('region_id', $regionId)
            ->where('date', '<=', $this->today)
            ->orderBy('date','desc')
            ->orderBy('created_at', 'desc');

        // Exclude the following events
        if($excludeIds) $events = $events->whereNotIn('id', $excludeIds);

        $this->loadRelations($events, true);

        // Get the events paginated or by get with optional a given amount
        if($pagination){
            $events = $events->paginate(self::EVENTS_PER_REGION_PAGE);
        }
        else {
            if($amount) $events = $events->take($amount);
            $events = $events->get();
        }
        return $events;
    }

    /**
     * Get the region of the logged in user
     *
     * @return Region|null
     */
    private function getRegionThroughUser(): ?Region
    {
        if (!\Auth::guard('siteUser')->check()) return null;

        if (!$userCompany = \Auth::guard('siteUser')->user()->getCompany()) return null;

        if (!$region = $userCompany->region) return null;

        return $region;

    }

    /**
     * Get the highlighted past events
     * This can be based upon the bind company region of the user / or as guest the most recent events
     *
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    public function getHighlightedPastEvents(){

        if ($region = $this->getRegionThroughUser()) $highlightedPastEvents = $this->getHighlightedPastEventsForRegionId($region->id);
        else $highlightedPastEvents = $this->getPastEvents(false, false, null, 3);

        $this->loadRelations($highlightedPastEvents);

        return $highlightedPastEvents;

    }

    /**
     * Get the amount of needed highlighted events by region
     * and fill them out by the latest events if there aren't enough
     *
     * @param $regionId
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    private function getHighlightedPastEventsForRegionId($regionId)
    {

        $events = $this->site
            ->pastEvents()
            ->has('images')
            ->where('active', 1)
            ->where('region_id', $regionId)
            ->where('date', '<=', $this->today)
            ->orderBy('date','desc')
            ->orderBy('created_at', 'desc')
            ->take(self::HIGHLIGHTED_EVENTS)
            ->get();

        // If there are 3 events return them
        if($events->count() == self::HIGHLIGHTED_EVENTS) return $events;

        // Else fill out the events with the latest events (without region filter)

        // Get id's from the found event(s)
        $eventIds = $events->pluck('id')->all();

        // Determine the amount of events will still need
        $amountMoreNeeded = self::HIGHLIGHTED_EVENTS - $events->count();

        // Get the events and merge them with the current highlighted
        $otherRegionsPastEvents = $this->getPastEvents(false, false, $eventIds, $amountMoreNeeded);
        $events = $events->merge($otherRegionsPastEvents);

        return $events;
    }
}