namespace Cubist\Backpack\app\Magic;
+use Illuminate\Support\Arr;
+
trait CubistMagicAttribute
{
public function getAttribute($key, $default = null)
{
- if ($this->hasAttribute($key)) {
- return $this->_attributes[$key];
- }
- return $default;
+ $key = $this->_attribute($key);
+ return Arr::get($this->_attributes, $key, $default);
}
public function hasAttribute($key)
{
- return isset($this->_attributes[$key]) && null !== $this->_attributes[$key];
+ $key = $this->_attribute($key);
+ return Arr::has($this->_attributes, $key);
}
public function setAttribute($key, $value)
{
- $this->_attributes[$key] = $value;
+ $key = $this->_attribute($key);
+ Arr::set($this->_attributes, $key, $value);
}
public function setAttributeIfNotSet($key, $value)
{
- if ($this->hasAttribute($key)) {
+ $k = $this->_attribute($key);
+ if ($this->hasAttribute($k)) {
return;
}
- $this->setAttribute($key, $value);
+ $this->setAttribute($k, $value);
+ }
+
+ public function _attribute($name)
+ {
+ $aliases = $this->_getAttributesAliases();
+ if (isset($aliases[$name])) {
+ $name = $aliases[$name];
+ }
+ if (is_array($name)) {
+ $name = implode('.', $name);
+ }
+ return $name;
+ }
+
+ protected function _getAttributesAliases()
+ {
+ return [];
}
}
protected $_adminType = 'text';
protected $_viewNamespace = 'crud::fields';
+ protected $_columnViewNamespace = 'crud::columns';
protected $_databaseType = 'text';
protected $_databaseUnique = false;
{
return ['type' => $this->_adminType, 'view_namespace' => $this->_viewNamespace, 'column' => false, 'form' => 'both', 'rules' => '',
'fillable' => true, 'guarded' => false, 'hidden' => false, 'translatable' => $this->_translatable,
- 'column_type' => $this->_columnType, 'default' => '', 'cast' => $this->_cast,
+ 'column_type' => $this->_columnType, 'default' => '', 'cast' => $this->_cast, 'column_view_namespace' => $this->_columnViewNamespace,
'allow_null' => true,
- 'fake' => false, 'store_in' => false, 'attributes' => []];
+ 'fake' => false, 'store_in' => false, 'attributes' => [],];
}
public function __construct($attributes)
{
- $aliases = ['multiple' => 'allows_multiple'];
- foreach ($aliases as $alias => $real) {
- if (isset($attributes[$alias])) {
- $attributes[$real] = $attributes[$alias];
- unset($attributes[$alias]);
- }
+ $this->_attributes = $this->getDefaultAttributes();
+ foreach ($attributes as $attribute => $value) {
+ $this->setAttribute($attribute, $value);
}
-
- $this->_attributes = array_merge($this->getDefaultAttributes(), $attributes);
$this->_postSetAttributes();
$this->init();
}
$res = [
'name' => $this->getAttribute('name'),
+ 'view_namespace' => $this->getAttribute('column_view_namespace'),
'type' => $this->getAttribute('column_type'),
'label' => $this->getAttribute('column_label', $this->getAttribute('label'))
];
public function getDefaultAttributes()
{
-
return array_merge(parent::getDefaultAttributes(), [
'form' => 'update',
'mime_types' => $this->_mimeTypes,
'options' => ['thumbnailHeight' => 368, 'thumbnailWidth' => 232, 'maxFilesize' => 1024, 'addRemoveLinks' => true, 'createImageThumbnails' => true, 'maxFiles' => $this->_maxFiles]]);
}
+ protected function _getAttributesAliases()
+ {
+ return array_merge(parent::_getAttributesAliases(), [
+ 'maxFiles' => 'options.maxFiles',
+ 'thumbnailHeight' => 'options.thumbnailHeight',
+ 'addRemoveLinks' => 'options.addRemoveLinks',
+ 'createImageThumbnails' => 'options.createImageThumbnails',
+ 'maxFilesize' => 'options.maxFilesize',
+ ]);
+ }
+
protected function _postSetAttributes()
{
parent::_postSetAttributes();
namespace Cubist\Backpack\app\Magic\Fields;
+use Cubist\Backpack\CubistBackpackServiceProvider;
+
class SelectFromArray extends Field
{
protected $_adminType = 'select2_from_array';
protected $_columnType = 'select_from_array';
+ protected $_columnViewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::columns';
protected $_databaseType = 'string';
protected $_multiple = false;
parent::_postSetAttributes();
}
+ protected function _getAttributesAliases()
+ {
+ return array_merge(parent::_getAttributesAliases(), [
+ 'multiple' => 'allows_multiple'
+ ]);
+ }
+
public function getColumnData()
{
$res = parent::getColumnData();
--- /dev/null
+{{-- select_from_array column --}}
+@php
+ $values = data_get($entry, $column['name']);
+@endphp
+
+<span>
+ <?php
+ if ($values !== null) {
+ if (is_array($values)) {
+ $array_of_values = [];
+
+ foreach ($values as $key => $value) {
+ $array_of_values[] = $column['options'][$value];
+ }
+
+ if (count($array_of_values) > 1) {
+ echo implode(', ', $array_of_values);
+ } else {
+ echo $array_of_values;
+ }
+ } else {
+ if (isset($column['options'][$values])) {
+ echo $column['options'][$values];
+ }else{
+ echo '-';
+ }
+ }
+ } else {
+ echo "-";
+ }
+ ?>
+</span>