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/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/Kms/Core/Attributes/Dynamic.php
<?php
namespace App\KommaApp\Kms\Core\Attributes;

use App\KommaApp\Kms\Core\Attributes\Models\AbstractBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Traits\LabelTrait;
use App\KommaApp\Kms\Core\Sections\KmsSiteSection;
use App\KommaApp\Kms\Core\Sections\Section;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Dynamic
 * @package App\KommaApp\Kms\Core\Attributes
 */
class Dynamic extends Attribute
{
    use LabelTrait;

    /** @var string $subFolder This is the subfolder of the path public/uploads/images where the images for this attribute wil be placed in*/
    private $subFolder;

    /** @var AbstractBlockSettings[] $blockSettings */
    private $blockSettings;

    private $dynamicLock = false;

    /**
     * Dynamic constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function lockDynamicBlocks(): Dynamic
    {
        $this->dynamicLock = true;
        return $this;
    }

    public function isLocked(){
        return $this->dynamicLock;
    }

    /**
     * Returns a view that visually represents this attribute
     *
     * @return \Illuminate\View\View
     */
    public function render()
    {
        //retrieve model and model id.
        $model = null;
        $request = request();
        $route = $request->route();

        if(method_exists($route->controller, 'getSection') == false) throw new \RuntimeException("The controller: '".get_class($route->controller)."' did not have a public  'getSection' method while it should have one.");

        /** @var Section $section */
        $section = $route->controller->getSection();
        $model = $section->getSectionService()->getModel();
//        dd($model);

        return \View::make('kms/attributes.dynamic', [
            'attribute' => $this,
            'forModel' => $model
        ]);
    }

    /**
     * @return string
     */
    public function getSubFolder(): string
    {
        return $this->subFolder;
    }

    /**
     * @param string $subFolder
     * @return Dynamic
     */
    public function setSubFolder(string $subFolder): Dynamic
    {
        $this->subFolder = $subFolder;
        return $this;
    }

    /**
     * @param bool $asJson
     * @return AbstractBlockSettings[]|string
     */
    public function getBlockSettings($asJson = true)
    {
        if($asJson == false) return $this->blockSettings;

        $toEncode = [];
        foreach($this->blockSettings as $blockSetting) {
            $toEncode[$blockSetting->getBlockType()] = $blockSetting->jsonSerialize();
        }
        $blockSettings = json_encode($toEncode);
        return $blockSettings;
    }

    /**
     * @param AbstractBlockSettings[] $blockSettings
     * @return Dynamic
     */
    public function setBlockSettings(array $blockSettings): Dynamic
    {
        $this->blockSettings = $blockSettings;
        return $this;
    }
}