]> _ Git - cubist_cms-back.git/commitdiff
wait #3056 @6
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 30 Sep 2019 12:07:57 +0000 (14:07 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 30 Sep 2019 12:07:57 +0000 (14:07 +0200)
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Magic/Models/CubistMagicTranslatableModel.php

index 35931c9f15d64f8fd1631da31b87d5283379df30..fc5dc45c7ae79eb264af2ff77c3de269234dd02a 100644 (file)
@@ -14,6 +14,7 @@ use Cubist\Util\Json;
 use Doctrine\DBAL\Schema\Schema;
 use Doctrine\DBAL\Schema\Table;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\App;
 use Illuminate\Support\Str;
 use Spatie\MediaLibrary\HasMedia\HasMedia;
 use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
@@ -135,7 +136,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
      *
      * @return array
      */
-    public function sluggable():array
+    public function sluggable(): array
     {
         return [
             'slug' => [
@@ -152,8 +153,10 @@ class CubistMagicAbstractModel extends Model implements HasMedia
 
             $slug = [];
             foreach ($components as $component) {
-                if (isset($this->$component) && $this->$component != '') {
-                    $slug[] = $this->$component;
+                $v = $this->getAttribute($component);
+
+                if ($v) {
+                    $slug[] = $v;
                 }
             }
             if (count($slug) > 0) {
index 0e0559da3254119eb2818bbe9eda8ef7909b41bd..db96702cc37924dea343562e1cab5dd19eb56880 100644 (file)
@@ -46,6 +46,29 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
         return $field;
     }
 
+    public function getTranslations(string $key = null): array
+    {
+        if ($key !== null) {
+            $this->guardAgainstNonTranslatableAttribute($key);
+
+            if ($key === 'slug') {
+                return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) {
+                    return $value !== null;
+                });
+            } else {
+                return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) {
+                    return $value !== null && $value !== '';
+                });
+            }
+        }
+
+        return array_reduce($this->getTranslatableAttributes(), function ($result, $item) {
+            $result[$item] = $this->getTranslations($item);
+
+            return $result;
+        });
+    }
+
     public function update(array $attributes = [], array $options = [])
     {
         return $this->updateTranslations($this->_prepareData($attributes), $options);