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/CalendarNotes/Kms/CalendarNoteService.php
<?php

namespace App\Komma\CalendarNotes\Kms;

use App\Helpers\KommaHelpers;
use App\Komma\CalendarNotes\Models\CalendarNote;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailInterface;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Kms\Core\Sections\SidebarListItem;
use App\Komma\Users\Models\KmsUserRole;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

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

    /**
     * Extends the save model, for saving the price excluding vat
     *
     * @param Model $model
     * @param Collection|null $attributes
     * @return Model
     */
    public function save(Model $model, Collection $attributes = null): Model
    {
        $model = parent::save($model, $attributes);

        if ($model->all_day) {
            $model->start_date = $model->start_date->startOfDay();
            $model->end_date = $model->end_date->endOfDay();
            $model->save();
        }

        return $model;
    }

    /**
     * Returns all models for the sidebar menu in the backend
     *
     * @return array
     */
    public function getModelsForSideBar():array
    {
        if (! is_a($this->modelClassName, DisplayNameInterface::class, true)) {
            throw new \RuntimeException('Please let class "'.$this->modelClassName.'" implement '.DisplayNameInterface::class.'. it is needed to build a sidebar.');
        }
        if (! is_a($this->modelClassName, HasThumbnailInterface::class, true)) {
            throw new \RuntimeException('Please let class "'.$this->modelClassName.'" implement '.HasThumbnailInterface::class.'. It is needed to build a sidebar');
        }

        // Get the models
        $modelQuery = $this->modelClassName::orderBy('start_date')
            ->where('start_date', '>=', Carbon::now()->subWeek()->startOfDay());

        if (! auth()->user()->isAtLeast(KmsUserRole::Admin)) {
            $modelQuery = $modelQuery->where('location_id', auth()->user()->location_id);
        }

        $models = $modelQuery->get();

        $sidebarList = [];
        foreach ($models as $model) {
            /** @var Model|HasThumbnailInterface|DisplayNameInterface $model */
            if ($model->getAttribute('lft') == 1) {
                continue;
            } //Skip the root model if it is one

            $sidebarListItem = new SidebarListItem();
            $sidebarListItem->setThumbnail($model);
            $sidebarListItem->alsoSearchInAttributesOfModel($model);
            $model->generateThumbnail();

            //Set the values for the sidebar
            $sidebarListItem->setId($model->id);

            $sidebarListItem->setStatus($model->active);
            $title = KommaHelpers::str_limit_full_word($model->getSidebarName(), 75);
            $sidebarListItem->setName($title);
            $sidebarListItem->setThumbnail($model->getThumbnail());

            $sidebarList[] = $sidebarListItem;
        }

        return $sidebarList;
    }
}