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/SBogers33/bbec.nl/workbench/komma/kms/src/Komma/Kms/Core/Attributes/KmsSelect.php
<?php
/**
 * Short description for the file.
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Kms\Core\Attributes;

use View;

class KmsSelect extends KmsAttribute{

    public $label;
    public $placeholder;
    public $data = [];
    public $allowNull;
    protected $exclude;

    function __construct($key, $value, array $options = [], array $errors = [], $languageId = null, $section = null)
    {
        parent::__construct($key, $value, $options, $errors, $languageId, $section);
        $this->label = $this->getOption('label', $key);
        $this->placeholder = $this->getOption('placeholder', $key);
        $this->exclude = $this->getOption('exclude');
        $this->setData($this->getOption('data', $key));
        $this->allowNull = $this->getOption('allowNull', false);
    }

    /**
     * Get data from a repository
     * @param $data
     * @return array
     */
    public function setData($data)
    {
        if(is_string($data))
        {
            $data = explode('@',$data);
            $method = array_pop($data);
            $class = $data[0];
            $this->data = \App::make($class)->{$method}($this->languageId, $this->exclude);
            return $this->data;
        }
        return $this->data = $data;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getSelected()
    {
        foreach($this->data as $dataElement)
        {
            if($dataElement['value'] == $this->getValue())
            {
                return '{value}';
            }
        }
    }

    /**
     * Set the value for displaying
     * @return mixed
     */
    public function setValue($value)
    {
        $this->value = $value;
        $this->setData($this->getOption('data', $this->getKey()));
        return $value;
    }

    public function getDefaultValue()
    {
        if($this->allowNull) return 0;
        if(count($this->data) > 0 and isset($this->data[0]['value'])) return $this->data[0]['value'];
        return '';
    }

    public function getValue()
    {
        $sessionVar = \Form::getValueAttribute($this->key);
        if($sessionVar) return $sessionVar;
        if($this->value) return $this->value;
        return $this->getDefaultValue();
    }

    public function render()
    {
        return View::make('kms::attributes.select', [
            'attribute' => $this
        ]);
    }

}