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/Pages/Kms/PageModelService.php
<?php

declare(strict_types=1);

namespace App\Komma\Pages\Kms;

use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailInterface;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Pages\Models\Page;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;

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

    /**
     * Puts the values of attributes in an Eloquent model. And then saves that model.
     *
     * @param Model $model
     * @param Collection $attributes
     * @return Model
     */
    public function save(Model $model, Collection $attributes = null): Model
    {
        $model = parent::save($model, $attributes);

        // set code name if none is given
        // Only SuperAdmins can enter code_names
        if (empty($model->code_name)) {
            $model->code_name = 'page_'.$model->id;
        }
        $model->save();

        return $model;
    }

    /**
     * Gets the values of an Eloquent model and passes them to a collection of attributes
     *
     * @param Model $model
     * @param Collection $attributes
     * @return mixed
     */
    public function load(Model $model, Collection $attributes = null): Collection
    {
        if ($attributes == null) {
            return new Collection();
        }

        $this->checkContainsAttributes($attributes);
        if (! is_a($model, Page::class)) {
            return $attributes;
        }
        /** @var Page $model */
        $attributes = $attributes->map(function (Attribute $attribute) use (&$model) {
            $valueReference = $attribute->getsValueFromReference();
            if ($valueReference == 'site_name') {
                $site = $this->siteService->getCurrentSite();
                $attribute->setValue($site->name);
            }

            return $attribute;
        });

        return parent::load($model, $attributes);
    }

    /**
     * This method will create a new model instance.
     *
     * @return Model
     */
    public function newModel(): Model
    {
        //Create a new TranslatableModelInterface
        /** @var Page $model */
        $model = new $this->modelClassName;
        $model =
        $model->site()->associate($this->siteService->getCurrentSite());

        //Set the thumbnail
        /** @var HasThumbnailInterface|Model $model */
        if (! is_a($model, HasThumbnailInterface::class)) {
            throw new \RuntimeException('The class "'.get_class($model).'" is expected to implement the "'.HasThumbnailInterface::class.'" but did not');
        }
        $model = $model->generateThumbnail();

        return $model;
    }

    public function getProvincePages(): Collection
    {
        $pages = Page::where('code_name', 'like', 'locations.%')->with('translation')->get();

        $options = collect([
            (App::make(SelectOptionInterface::class))
                ->setContent(__('kms/global.none'))
                ->setHtmlContent(__('kms/global.none'))
                ->setValue(null),
        ]);

        foreach ($pages as $page) {
            $options->push(
                (App::make(SelectOptionInterface::class))
                ->setContent($page->translation->name)
                ->setHtmlContent($page->translation->name)
                ->setValue($page->id)
            );
        }

        return $options;
    }
}