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/SBogers95/rentman.io/app/Komma/Updates/Kms/UpdateService.php
<?php

namespace App\Komma\Updates\Kms;

use App\Helpers\KommaHelpers;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\SidebarListItem;
use App\Komma\Routes\Models\Route;
use App\Komma\Sites\HasSitesInterface;
use App\Komma\Updates\Models\Update;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Database\Eloquent\Model;

class UpdateService extends SectionService
{
    protected $sortable = false;

//    protected $orderBy = 'id';
    protected $orderByDisplayName = true;

    protected $orderReverse = true;

    public function __construct()
    {
        $this->forModelName = Update::class;

        parent::__construct();
    }

    /**
     * This method will save an model
     *
     * @param $model Model or null
     * @param Collection $sectionTabItems These must be filled with data. This is something you need to do yourself.
     *
     * @return mixed
     */
    public function saveModel(Model $model = null, Collection $sectionTabItems): Model
    {
        $model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
        $this->siteService->linkModelToCurrentSite($model);

        $sectionTabItems->each(function ($sectionTabItem, $key) use ($model) {
            /** @var SectionTabItem $sectionTabItem */
            $attribute = $sectionTabItem->getAttribute();

            $reference = $attribute->getsValueFromReference();
            switch ($attribute->getsValueFrom()) {
                case Attribute::ValueFromTranslationModel:

                    $language = $attribute->getAssociatedLanguage();
                    $translation = $this->getTranslationModelForModelByLanguage($model, $language);

                    if ($reference == 'slug') {
                        $value = $attribute->getValue();

                        // If slug isn't defined we use the name
                        if (empty($value)) {
                            $value = $translation->name;
                        }

                        if (! empty($value)) {
                            $slug = $this->createOrGetUniqueSlug($translation, $value);
                            $translation['slug'] = $slug;
                        }
                    }

                    break;
            }
        });

        $model->save(); //Save the page
        $this->saveModelTranslations($model);

        //Return the page
        return $model;
    }

    /**
     * Fills non-update specific attributes. Update specific attributes are processed in the parent
     *
     * @param DatabaseCollection $sectionTabItems A collection containing implementations AbstractSectionTabItem's
     * @param Model $model
     * @return DatabaseCollection
     */
    public function fillAttributesWithData(DatabaseCollection $sectionTabItems, Model $model)
    {
        $filledAttributesCollection = parent::fillAttributesWithData($sectionTabItems, $model);

        $sectionTabItems->each(
            function ($sectionTabItem, $key) use (
                $model,
                $filledAttributesCollection,
                &$quantityDiscountAttribute,
                &$quantityPriceAttribute
            ) {
                /** @var $sectionTabItem SectionTabItem */
                $attribute = $sectionTabItem->getAttribute();
                if (! is_a($attribute, Attribute::class)) {
                    throw new \InvalidArgumentException('One of the attributes in a AbstractSectionTabItem instance is not but must be an child instance of Attribute.');
                }

                $valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
                switch ($valueReference) {
                    case 'site_id':
                        /** @var HasSitesInterface $model */
                        $idString = $this->siteService->getSiteIdsForModel($model);
                        if ($idString) {
                            $attribute->setValue($idString);
                        }
                        break;

                    case 'link':

                        $link = '';

                        $language = $attribute->getAssociatedLanguage();
                        $translation = $this->getTranslationModelForModelByLanguage($model, $language);

                        $route = Route::where('route', 'updates')
                            ->where('language_id', $language->id)
                            ->first();

                        if (! empty($translation->slug) && isset($route)) {
                            $link = '/'.$route->alias.'/'.$translation->slug;
                        }

                        $attribute->setLink($link)->setLinkText($link);
                        break;
                }
            }
        );

        return $filledAttributesCollection;
    }

    /**
     * Returns all models for the sidebar menu in the backend
     *
     * @return array
     */
    public function getModelsForSideBar(): array
    {
        $models = $this->forModelName::orderBy('id');

        //Only load the models from the current site. Or load all when we currently have the default site
        $site = $this->siteService->getCurrentSite();
        if (new $this->forModelName instanceof HasSitesInterface && $site->exists) {
            $models->whereHas('Sites', function ($query) use ($site) {
                $query->where('site_id', '=', $site->id);
            });
        }

        // Get the models
        $models = $models->with('documents', 'translation', 'translations')->get();

        $sidebarList = [];
        foreach ($models as $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);

            if (! empty($model->translation)) {
                $key = $model->translation->date->format('Ymd').'-'.$model->translation->name;
                $title .= '<br/><sub>'.$model->translation->date->format('d-m-Y').'</sub>';
            } elseif ($model->translations->count() != 0) {
                $key = $model->translations->first()->date->format('Ymd').'-'.$model->translations->first()->name;
                $title .= '<br/><sub>'.$model->translations->first()->date->format('d-m-Y').'</sub>';
            } else {
                $key = $title;
            }

            $sidebarListItem->setName($title);
            $sidebarListItem->setThumbnail($model->getThumbnail());

            // Make sure there is always a unique key
            if (isset($sidebarList[$key])) {
                $key = $key.$model->id;
            }

            $sidebarList[$key] = $sidebarListItem;
        }

        if ($this->orderByDisplayName) {
            ksort($sidebarList);
            if ($this->orderReverse) {
                $sidebarList = array_reverse($sidebarList);
            }
        }

        return $sidebarList;
    }
}