]> _ Git - cubist_cms-back.git/commitdiff
wip #4211
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 7 Mar 2022 18:11:16 +0000 (19:11 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 7 Mar 2022 18:11:16 +0000 (19:11 +0100)
src/app/Magic/Models/CubistMagicAbstractModel.php

index 151162bdc7a9166d81bbb18a354a6d97f4360443..d4a8453ee88ac75c9b97ff2dcbf5f01adebc9bae 100644 (file)
@@ -28,6 +28,7 @@ use Doctrine\DBAL\Schema\Schema;
 use Doctrine\DBAL\Schema\Table;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Arr;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Str;
@@ -740,15 +741,6 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         }
     }
 
-    public function getAttribute($key)
-    {
-        $value = parent::getAttribute($key);
-        $f = $this->getField($key);
-        if (null === $f) {
-            return $value;
-        }
-        return $f->filterValue($value);
-    }
 
     protected function _randCollectionName($fieldname)
     {
@@ -797,6 +789,48 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         }
     }
 
+    public function setAttribute($key, $value)
+    {
+        $f = $this->getField($key);
+        if (null === $f || !$f->getAttribute('fake', false)) {
+            return parent::setAttribute($key, $value);
+        }
+        $k = $f->getAttribute('store_in', 'extras');
+        $fv = $this->_getFakeValues($k);
+        Arr::set($fv, $key, $value);
+        $this->$k = $fv;
+
+        return $this;
+    }
+
+    protected function _getFakeValues($storedIn)
+    {
+        $fv = $this->$storedIn;
+        if (is_string($fv) && Json::isJson($fv)) {
+            $fv = Json::decode($fv, true);
+        }
+        return $fv;
+    }
+
+    public function getAttribute($key)
+    {
+        $f = $this->getField($key);
+        if (null === $f || !$f->getAttribute('fake', false)) {
+            $value = parent::getAttribute($key);
+        } else {
+            $k = $f->getAttribute('store_in', 'extras');
+            $fv = $this->_getFakeValues($k);
+            $value = Arr::get($fv, $key);
+        }
+
+        if (null === $f) {
+            return $value;
+        }
+
+        return $f->filterValue($value);
+    }
+
+
     public function replaceMediaInField($fieldname, $filepath, $preserveOriginal = false)
     {
         $this->deleteMediaInField($fieldname);