File: D:/HostingSpaces/zipwire/zipwire.nl/app/KommaApp/Kms/Core/Attributes/KmsAttribute.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Kms\Core\Attributes;
use App\KommaApp\Users\Models\User;
use Validator;
/**
* Class KmsAttribute
* @package App\KommaApp\Kms\Core\Attributes
*
* @deprecated Do not use it anymore. Use Attribute instead
*/
abstract class KmsAttribute
{
public $requiredSymbol = ' *';
public $key;
public $value;
public $errors;
public $options;
public $siteId;
public $languageId;
public $section;
public $class = '';
public $arr;
public $default_value;
public $canNotChange;
public $data_type = null;
public $onlyForSuperAdmin = false;
public $foreach;
public $width;
function __construct($key, $value, array $options = [], array $errors = [], $siteId = null, $languageId = null, $section = null, $foreach = null)
{
$this->key = $key;
$this->value = $value;
$this->options = $options;
$this->errors = $errors;
$this->siteId = $siteId;
$this->languageId = $languageId;
$this->section = $section;
$this->class = $this->getOption('class', '');
$this->data_type = $this->getOption('data_type', $this->data_type);
$this->onlyForSuperAdmin = $this->getOption('onlyForSuperAdmin', false);
$this->foreach = $foreach;
$this->canNotChange = $this->getOption('canNotChange', false);
$this->width = $this->getOption('width', false);
$this->arr = \App::make('Illuminate\Support\Arr');
$this->default_value = $this->getOption('default_value', '');
}
abstract public function render();
/**
* Get the value for saving
* If null is returned, the value will not be saved
* @return mixed
*/
public function getKey()
{
return $this->key;
}
/**
* Get the value for saving
* If null is returned, the value will not be saved
* @return mixed
*/
public function getValue($post = false)
{
if (!$this->value) $this->value = $this->default_value;
return $this->value;
}
/**
* Get the value for saving
* If null is returned, the value will not be saved
* @return mixed
*/
public function getRawValue()
{
return $this->value;
}
/**
* Set the value for displaying
* @return mixed
*/
public function setValue($value)
{
return $this->value = $value;
}
public function getValidation()
{
return $this->getOption('validation');
}
public function setErrors(array $errors)
{
$this->errors = $errors;
}
public function userIsEligible(User $user)
{
if ($user->role() != 1 && $this->onlyForSuperAdmin)
return false;
return true;
}
public function process()
{
}
public function delete()
{
}
protected function getCleanKey()
{
$output = explode('_', $this->key);
if ($this->siteId) {
if ($output[count($output) - 1] == $this->siteId)
array_pop($output);
}
if ($this->languageId) {
if ($output[count($output) - 1] == $this->languageId)
array_pop($output);
}
return implode('_', $output);
}
/**
* Helper function to get data from the options array by key
* @param $key
* @param null $default
* @return null
*/
public function getOption($key, $default = null)
{
return isset($this->options[$key]) ? $this->options[$key] : $default;
}
public function canNotChange()
{
return $this->section->getModelId() && $this->canNotChange;
}
public function getWidth($itemCount = 1)
{
$width = 100;
$width = $width / $itemCount;
if ($this->width) $width = $this->width;
if ($width == 100) return 'width: ' . $width . '%; clear:both;';
return 'width: ' . $width . '%; float: left;';
}
public function key()
{
return preg_replace('/(.*)\[(.*)\]/', '$1[]', $this->key);
}
public function isAttributeRequired()
{
/**
* This method checks if an Kms attribute is required
*
* @param $attribute
* @return bool
*/
if (!isset($this->options)) return false;
if (!isset($this->options['validation'])) return false;
if (!isset($this->options['validation']['rules'])) return false;
if (!preg_match('/required/', $this->options['validation']['rules'])) return false;
return true;
}
public function label()
{
if($this->isAttributeRequired()) return $this->label.$this->requiredSymbol;
return $this->label;
}
}