]> _ Git - cubist_cms-back.git/commitdiff
#2810
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 14 Jun 2019 15:52:49 +0000 (17:52 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 14 Jun 2019 15:52:49 +0000 (17:52 +0200)
src/app/Magic/CubistMagicAttribute.php
src/app/Magic/Fields/Field.php
src/app/Magic/Fields/Files.php
src/app/Magic/Fields/SelectFromArray.php
src/app/Magic/Fields/SelectFromModel.php
src/resources/views/columns/select_from_array.blade.php [new file with mode: 0644]

index 04df7124783b7689ccde555cd09d570cf567f08a..8a107d78a11d912487ceebe79b1a5a5370926fd8 100644 (file)
@@ -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 [];
     }
 }
index 6e9225b460abbf0200d082407dbaa9959475812e..787eeb8f449cbb402d1ffd533b9a25aa037f6a18 100644 (file)
@@ -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'))
         ];
index 4f99f902dbb5a8be589c06e2349b5911f11248a2..0ff1fabe063f36696f4d69e5f3285a57d4078ee1 100644 (file)
@@ -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();
index af51d80551efc80cd6936c6dc0cf8eb4576b8c59..ee4721b7b68eb974d2d3351dafe566f5774ef067 100644 (file)
@@ -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;
 
index 6885ea200f7bea74715ee9ddeb3718d22bfb5d92..d6ea0b67f3b4d2640bba07923b67a44640b79032 100644 (file)
@@ -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 (file)
index 0000000..a722fea
--- /dev/null
@@ -0,0 +1,32 @@
+{{-- 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>