]> _ Git - cubist_cms-back.git/commitdiff
#2810
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 11 Jun 2019 13:56:39 +0000 (15:56 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 11 Jun 2019 13:56:39 +0000 (15:56 +0200)
src/app/Magic/Fields/Field.php
src/app/Magic/Fields/Files.php
src/app/Magic/Fields/Images.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 25eead739302126401dc24aad79d0ebd2ba077b0..89517a7c5c9c26c376241c99b177f680bdb0176a 100644 (file)
@@ -26,6 +26,11 @@ class Field
     protected $_cast = false;
     protected $_translatable = false;
 
+    /**
+     * @var CubistMagicAbstractModel
+     */
+    protected $_modelInstance;
+
     protected $_databaseAttributes = [];
 
     /**
@@ -46,6 +51,25 @@ class Field
         return new $class($attributes);
     }
 
+    /**
+     * @return CubistMagicAbstractModel
+     */
+    public function getModelInstance(): CubistMagicAbstractModel
+    {
+        return $this->_modelInstance;
+    }
+
+    /**
+     * @param CubistMagicAbstractModel $modelInstance
+     * @return Field
+     */
+    public function setModelInstance(CubistMagicAbstractModel $modelInstance): Field
+    {
+        $this->_modelInstance = $modelInstance;
+        return $this;
+    }
+    
+
     public function getDatabaseType()
     {
         if ($this->_translatable) {
index 85466a17abebaa6d42b2f9385b3dca2600fcde82..ed1c2bda821ece8a9b829f5ee29af5c3072d049a 100644 (file)
@@ -13,7 +13,7 @@ class Files extends Field
 
     public function getDefaultAttributes()
     {
-        return array_merge(parent::getDefaultAttributes(), ['mime_types' => $this->_mimeTypes, 'multiple' => $this->_multiple]);
+        return array_merge(parent::getDefaultAttributes(), ['mime_types' => $this->_mimeTypes, 'multiple' => $this->_multiple, 'upload' => true, 'disk' => 'uploads']);
     }
 
     protected function _postSetAttributes()
@@ -27,4 +27,14 @@ class Files extends Field
         return parent::_postSetAttributes();
     }
 
+    public function setMutator($value)
+    {
+        $model = $this->getModelInstance();
+
+        $function = $this->_multiple ? 'uploadMultipleToDisk' : 'uploadToDisk';
+        $path = $model->getAttribute('name') . '/' . $model->getPrimaryKey();
+
+        $model->$function($value, $this->getAttribute('name'), $this->getAttribute('disk'), $path);
+    }
+
 }
index 580386b69f619f5f6fefbf2fa8c617d2688d27db..4c54636b8da776cfa01341f57dc8d608863af673 100644 (file)
@@ -7,4 +7,15 @@ namespace Cubist\Backpack\app\Magic\Fields;
 class Images extends Files
 {
     protected $_mimeTypes = ['image/png', 'image/jpeg', 'image/svg+xml', 'image/webp', 'image/gif'];
+
+    protected function _postSetAttributes()
+    {
+        if ($this->_multiple) {
+            $this->setAttribute('type', 'upload_multiple');
+        } else {
+            $this->setAttribute('type', 'image');
+        }
+
+        return parent::_postSetAttributes();
+    }
 }
index 67ab9a40a2c1914174d4c991d46b198970c40b67..53f9b4fb720da6b5d80505510d9219384985777f 100644 (file)
@@ -174,6 +174,7 @@ class CubistMagicAbstractModel extends Model
     {
         /** @var Field $field */
         $field = Field::getInstance($attributes);
+        $field->setModelInstance($this);
         if (is_callable([$field, 'getRelationship']) && null !== $field->getRelationship()) {
             $this->_addRelationship($field);
         }
@@ -348,6 +349,20 @@ class CubistMagicAbstractModel extends Model
 
     public function __call($method, $parameters)
     {
+
+        // Set mutators
+        if (preg_match('/^set([a-zA-Z0-9]+)Attribute$/', $method, $matches)) {
+            $attr = Str::snake($matches[1]);
+
+            if (isset($this->_fields[$attr])) {
+                $callback = [$this->_fields[$attr], 'setMutator'];
+                if (is_callable($callback)) {
+                    return call_user_func_array($callback, $parameters);
+                }
+            }
+        }
+
+        // magic call of relationships
         foreach ($this->_relationships as $relationship) {
             /** @var $relationship Field */
             if ($method == $relationship->getAttribute('entity')) {