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/Neopoints/momsecurity.be/app/Komma/Kms/Core/Attributes/AutocompleteInput.php
<?php

namespace App\Komma\Kms\Core\Attributes;


use App\Komma\Kms\Core\Attributes\Traits\ExplanationTrait;
use App\Komma\Kms\Core\Attributes\Traits\LabelTrait;
use App\Komma\Kms\Core\Attributes\Traits\PlaceholderTextTrait;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Kms\Core\Attributes\Traits\ReadOnlyTrait;
use Illuminate\Database\Eloquent\Model;

class AutocompleteInput extends Attribute
{
    use LabelTrait;
    use PlaceholderTextTrait;
    use ReadOnlyTrait;
    use ExplanationTrait;

    /**
     * @var string $exclude The option (value) that needs to excluded from the list. Usually because it represents something that you currently are in or are using
     */
    private $exclude;

    /** @var SelectOptionInterface[] $items */
    private $items;

    protected $defaultValue;

    /** @var string $autoSaveUrl */
    protected $autoSaveUrl;

    /** @var int $maxItemsToSelect */
    protected $maxItemsToSelect;

    public function __construct($value = null)
    {
        parent::__construct();

        $this->maxItemsToSelect = 20;
        $this->autoSaveUrl = false;
        if($value) $this->setValue($value);
    }

    /**
     * Sets the items you can select
     *
     * @param SelectOptionInterface[] $items
     * @return $this
     */
    public function setItems(array $items)
    {
        $this->items = $items;
        return $this;
    }

    /**
     * Returns the items that you can select.
     * Normally as an array unless you specify the parameter as true.
     * In that latter case it will return a json string.
     *
     * @param bool $asJson
     * @return SelectOptionInterface[]|string
     */
    public function getItems(bool $asJson = false)
    {
        if(!$asJson) return $this->items;

        $jsonPreparementArray = [];
        /** @var SelectOptionInterface $item */
        foreach($this->items as $item)
        {
            //Make sure that these keys match the ones in select.blade.php and ???
            $jsonPreparementArray[] = $this->buildJsonRepresentation($item);
        }

        $jsonItems = implode(",", $jsonPreparementArray);
        $json = "[".$jsonItems."]";

        return $json;
    }

    /**
     * Sets the id of the model that needs to be displayed
     *
     * @param string $value
     * @return $this
     */
    public function setValue(string $value)
    {
        $this->value = $value;
        return $this;
    }

    /**
     * Return the value as a string or as a json object for the javascript code.
     *
     * @param bool $asJson
     * @return string
     */
    public function getValue($asJson = false): string
    {
        if($asJson == false) {

            if(!empty($this->value)) return $this->value;
            return '';

        } else {
            //Return an object
            $jsonItemPreparementArray = [];

            $selectedItemId = $this->value;

            if($selectedItemId) {
                $jsonItemPreparementArray['id'] = $selectedItemId;
            }

            $json = json_encode($jsonItemPreparementArray);
            return $json;
        }
    }

    /**
     * Builds a json representation of a SelectOptionsInterface implementation.
     * You should use these representation in the javascript code.
     * @param SelectOptionInterface $selectOption
     * @return string
     */
    private function buildJsonRepresentation(SelectOptionInterface $selectOption): string
    {
        $optionJsonPreparementData = [];
        $optionJsonPreparementData['value'] = $selectOption->getValue();
        $optionJsonPreparementData['content'] = $selectOption->getContent();
        $optionJsonPreparementData['htmlContent'] = $selectOption->getHtmlContent();

        $json = json_encode($optionJsonPreparementData);
        return $json;
    }

    /**
     * Returns a view that visually represents this attribute
     */
    public function render()
    {
        //get id from model
        $currentId = null;
        foreach(request()->route()->parameters as $parameter)
        {
            if(is_a($parameter, Model::class))
            {
                $currentId = $parameter->id;
            }
        }

        return \View::make('kms/attributes.autocompleteInput', [
            'attribute' => $this,
            'currentId' => $currentId,
            'dataset' => $this->buildAutoCompleteDataJson()
        ]);
    }

    private function buildAutoCompleteDataJson()
    {
        $dataSet = [];
        $items = $this->getItems();
        foreach($items as $selectOption)
        {
            /** @var SelectOptionInterface $selectOption */
            $dataSet[] = [
                'id' => $selectOption->getValue(),
                'value' => str_replace('&nbsp;', '.', $selectOption->getHtmlContent())
            ];
        }
        $dataSet = json_encode($dataSet);
        return $dataSet;
    }

    /**
     * @return mixed
     */
    public function getDefaultValue()
    {
        return $this->defaultValue;
    }

    /**
     * @param mixed $defaultValue
     * @return AutocompleteInput
     */
    public function setDefaultValue($defaultValue): AutocompleteInput
    {
        $this->defaultValue = $defaultValue;
        return $this;
    }

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

    /**
     * This url will receive a comma delimited list of item ids that need to be saved.
     *
     * @param string $autoSaveUrl
     * @return AutocompleteInput
     */
    public function setAutoSaveUrl(string $autoSaveUrl): AutocompleteInput
    {
        $this->autoSaveUrl = $autoSaveUrl;
        return $this;
    }

    /**
     * @return int
     */
    public function getMaxItemsToSelect(): int
    {
        return $this->maxItemsToSelect;
    }

    /**
     * @param int $maxItemsToSelect
     * @return AutocompleteInput
     */
    public function setMaxItemsToSelect(int $maxItemsToSelect)
    {
        $this->maxItemsToSelect = $maxItemsToSelect;
        return $this;
    }
}