File: D:/HostingSpaces/SBogers10/ehboledensysteem.komma.pro/app/KommaApp/Kms/Core/Attributes/Select.php
<?php
/**
* Created by PhpStorm.
* User: julesgraus
* Date: 29/11/17
* Time: 11:50
*/
namespace App\KommaApp\Kms\Core\Attributes;
use App\KommaApp\Kms\Core\Attributes\Traits\LabelTrait;
use App\KommaApp\Kms\Core\Attributes\Traits\PlaceholderTextTrait;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\KommaApp\Kms\Core\Attributes\Traits\ReadOnlyTrait;
use Illuminate\Database\Eloquent\Model;
class Select extends Attribute
{
use LabelTrait;
use PlaceholderTextTrait;
use ReadOnlyTrait;
// private $data = [];
private $allowNull;
private $excludeCurrentModelItem;
/**
* @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;
public function __construct($value = null)
{
parent::__construct();
$this->excludeCurrentModelItem = 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)
{
$selectOptionJsonRepresentation = [];
if($item->getModel()) {
//Make sure that these keys match the ones in select.blade.php and ???
$jsonPreparementArray[] = $this->buildJsonRepresentation($item);
}
}
$jsonItems = implode(",", $jsonPreparementArray);
$json = "[".$jsonItems."]";
return $json;
}
/**
* Returns the selected model or a json string representing that model depending
* on the $asJson boolean you pass
*
* @param bool $asJson
* @return \Illuminate\Database\Eloquent\Model|string
*/
public function getSelected($asJson = false)
{
/** @var SelectOptionInterface $item */
foreach ($this->items as $item)
{
if($item->getModel()->id == $this->getValue()) {
if(!$asJson) return $item->getModel();
$selectOptionJsonRepresentation = $this->buildJsonRepresentation($item);
return $selectOptionJsonRepresentation;
}
}
return $this->buildJsonRepresentation($this->items[0]);
}
/**
* Sets the id of the model that needs to be displayed
*
* @param string $value
* @return $this
*/
public function setValue(string $value):Select
{
$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) {
$value = '';
//Return the value as a simple string containing the value
$sessionVar = \Form::getValueAttribute($this->key); //Note: this should not be here. Not the responsibility of this class
if ($sessionVar)
{
$value = $sessionVar."";
}
else if ($this->value) {
$value = $this->value."";
}
return $value;
} else {
//Return an object
$jsonItemPreparementArray = [];
$selectedItemId = $this->value;
if($selectedItemId) {
$jsonItemPreparementArray['id'] = $selectedItemId;
}
$json = json_encode($jsonItemPreparementArray);
return $json;
}
}
public function getExcludeCurrentModelItem():bool
{
return $this->excludeCurrentModelItem;
}
public function setExcludeCurrentModelItem($exclude):Select
{
$this->excludeCurrentModelItem = $exclude;
return $this;
}
/**
* Builds a json representation of a SelectOptionsInterface implementation.
* You should use these representation in the javascript code.
*/
private function buildJsonRepresentation(SelectOptionInterface $selectOption): string
{
$optionJsonPreparementData = [];
$optionJsonPreparementData['value'] = $selectOption->getModel()->id;
$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.select', [
'attribute' => $this,
'currentId' => $currentId
]);
}
}