From: Vincent Vanwaelscappel Date: Fri, 14 Jun 2019 15:52:49 +0000 (+0200) Subject: #2810 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=a015efdfff61066ce107cee95b3ad36ca8539734;p=cubist_cms-back.git #2810 --- diff --git a/src/app/Magic/CubistMagicAttribute.php b/src/app/Magic/CubistMagicAttribute.php index 04df712..8a107d7 100644 --- a/src/app/Magic/CubistMagicAttribute.php +++ b/src/app/Magic/CubistMagicAttribute.php @@ -4,31 +4,51 @@ 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 []; } } diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 6e9225b..787eeb8 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -18,6 +18,7 @@ class Field protected $_adminType = 'text'; protected $_viewNamespace = 'crud::fields'; + protected $_columnViewNamespace = 'crud::columns'; protected $_databaseType = 'text'; protected $_databaseUnique = false; @@ -95,22 +96,17 @@ class Field { 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(); } @@ -144,6 +140,7 @@ class Field $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')) ]; diff --git a/src/app/Magic/Fields/Files.php b/src/app/Magic/Fields/Files.php index 4f99f90..0ff1fab 100644 --- a/src/app/Magic/Fields/Files.php +++ b/src/app/Magic/Fields/Files.php @@ -15,7 +15,6 @@ class Files extends Field public function getDefaultAttributes() { - return array_merge(parent::getDefaultAttributes(), [ 'form' => 'update', 'mime_types' => $this->_mimeTypes, @@ -24,6 +23,17 @@ class Files extends Field '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(); diff --git a/src/app/Magic/Fields/SelectFromArray.php b/src/app/Magic/Fields/SelectFromArray.php index af51d80..ee4721b 100644 --- a/src/app/Magic/Fields/SelectFromArray.php +++ b/src/app/Magic/Fields/SelectFromArray.php @@ -4,10 +4,13 @@ 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; diff --git a/src/app/Magic/Fields/SelectFromModel.php b/src/app/Magic/Fields/SelectFromModel.php index 6885ea2..d6ea0b6 100644 --- a/src/app/Magic/Fields/SelectFromModel.php +++ b/src/app/Magic/Fields/SelectFromModel.php @@ -32,6 +32,13 @@ class SelectFromModel extends Model parent::_postSetAttributes(); } + protected function _getAttributesAliases() + { + return array_merge(parent::_getAttributesAliases(), [ + 'multiple' => 'allows_multiple' + ]); + } + public function getColumnData() { $res = parent::getColumnData(); diff --git a/src/resources/views/columns/select_from_array.blade.php b/src/resources/views/columns/select_from_array.blade.php new file mode 100644 index 0000000..a722fea --- /dev/null +++ b/src/resources/views/columns/select_from_array.blade.php @@ -0,0 +1,32 @@ +{{-- select_from_array column --}} +@php + $values = data_get($entry, $column['name']); +@endphp + + + $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 "-"; + } + ?> +