File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Kms/Core/Attributes/KmsSorter.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Kms\Core\Attributes;
use View;
class KmsSorter extends KmsAttribute
{
public $entityId;
public $label;
public $data = [];
function __construct($key, $value, array $options = [], array $errors = [], $siteId = null, $languageId = null, $section = null)
{
parent::__construct($key, $value, $options, $errors, $siteId, $languageId, $section);
$this->label = $this->getOption('label', $key);
$this->entityId = $this->getOption('entityId');
$this->setData($this->getOption('data', $key));
}
/**
* 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->siteId, $this->languageId, $this->entityId);
//dd($this->data);
return $this->data;
}
return $this->data = $data;
}
public function getData($type = null)
{
if ($type == 'json') return json_encode($this->data);
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 (count($this->data) > 0 and isset($this->data[0]['title']))
return $this->data[0]['title'];
return '';
}
public function getValue($post = false)
{
$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.sorter', [
'attribute' => $this
]);
}
}