]> _ Git - cubist_cms-back.git/commitdiff
fix #2967 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 30 Aug 2019 15:43:12 +0000 (17:43 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 30 Aug 2019 15:43:12 +0000 (17:43 +0200)
src/app/Http/Controllers/CubistFrontController.php
src/app/Magic/EntityData.php [new file with mode: 0644]
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Magic/Models/Settings.php
src/app/Magic/PageData.php

index 2db50e7d4f00c6c3b1bc341f18aa3047cab26aa1..6ca1c9287ae483277969a98e8c0feea7efe8dc7b 100644 (file)
@@ -14,8 +14,7 @@ class CubistFrontController extends BaseController
 
     public function __construct()
     {
-        $settings_class = Settings::getSettingsClass();
-        $this->data['global'] = $settings_class::find(1)->getPageData();
+        $this->data['global'] = Settings::getData();
         $this->data['nav'] = Menu::getNavigation();
     }
 
diff --git a/src/app/Magic/EntityData.php b/src/app/Magic/EntityData.php
new file mode 100644 (file)
index 0000000..1e623e0
--- /dev/null
@@ -0,0 +1,368 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic;
+
+
+use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
+use Cubist\Backpack\app\Magic\Models\CubistMagicModel;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\App;
+use Spatie\MediaLibrary\Models\Media;
+
+class EntityData implements \ArrayAccess
+{
+    /**
+     * @var array
+     */
+    protected $_data = [];
+    /**
+     * @var CubistMagicAbstractModel
+     */
+    protected $_entity;
+
+    /**
+     * @return CubistMagicModel
+     */
+    public function getEntity(): CubistMagicAbstractModel
+    {
+        return $this->_entity;
+    }
+
+    /**
+     * @param CubistMagicModel $entity
+     */
+    public function setEntity(CubistMagicAbstractModel $entity): void
+    {
+        $this->_entity = $entity;
+    }
+
+    public function __construct()
+    {
+    }
+
+    /**
+     * @param string $offset
+     * @param $value
+     */
+    public function set($offset, $value)
+    {
+        $offset = square_brackets_to_dots($offset);
+        Arr::set($this->_data, $offset, $this->_fixValue($value));
+    }
+
+    protected function _fixValue($value)
+    {
+        if (!is_array($value)) {
+            return $value;
+        }
+        return $this->_fixArray($value);
+    }
+
+    protected function _fixArray($value)
+    {
+        $dot = Arr::dot($value);
+        $res = [];
+        foreach ($dot as $k => $v) {
+            Arr::set($res, square_brackets_to_dots($k), $v);
+        }
+        return $res;
+    }
+
+    /**
+     * @param string $offset
+     * @return bool
+     */
+    public function exists($offset)
+    {
+        return Arr::has($this->_data, $offset);
+    }
+
+    /**
+     * @param string $offset
+     * @return bool
+     */
+    public function has($offset)
+    {
+        return $this->exists($offset);
+    }
+
+    /**
+     * @param string $offset
+     */
+    public function unset($offset)
+    {
+        Arr::set($this->_data, $offset, null);
+    }
+
+    /**
+     * @param string|array $offset
+     * @param mixed $default
+     * @return mixed
+     */
+    public function get($offset, $default = null)
+    {
+        if (!is_array($offset)) {
+            $offset = [$offset];
+        }
+
+        foreach ($offset as $key) {
+            if (!Arr::has($this->_data, $key)) {
+                continue;
+            }
+
+            $res = Arr::get($this->_data, $key, $default);
+
+            // If an array value is saved without any items, it might be returned
+            // as a string "[]" when instead it should be an empty array...
+            if ($res === '[]') {
+                $res = [];
+            }
+
+            // Get the translated value if the array contains a key matching the locale.
+            // We have to check if the key exists because the actual value may be null.
+            // Using isset() on the locale key would give a false negative if the value is null.
+            if (is_array($res) && array_key_exists(App::getLocale(), $res)) {
+                $res = $res[App::getLocale()];
+            }
+
+            if (!$res) {
+                return $default;
+            }
+            return $res;
+
+        }
+        return $default;
+    }
+
+    /**
+     * @param mixed $offset
+     * @param mixed $value
+     */
+    public function offsetSet($offset, $value)
+    {
+        $this->set($offset, $value);
+    }
+
+    /**
+     * @param mixed $offset
+     * @return bool
+     */
+    public function offsetExists($offset)
+    {
+        return $this->exists($offset);
+    }
+
+    /**
+     * @param mixed $offset
+     */
+    public function offsetUnset($offset)
+    {
+        return $this->unset($offset);
+    }
+
+    /**
+     * @param mixed $offset
+     * @return mixed
+     */
+    public function offsetGet($offset)
+    {
+        return $this->get($offset);
+    }
+
+    /**
+     * @param $name string
+     * @return mixed
+     */
+    public function __get($name)
+    {
+        return $this->get($name);
+    }
+
+    /**
+     * @param string $name
+     * @param mixed $value
+     */
+    public function __set($name, $value)
+    {
+        return $this->set($name, $value);
+    }
+
+    /**
+     * @param string $name
+     */
+    public function __unset($name)
+    {
+        $this->unset($name);
+    }
+
+    public function __isset($name)
+    {
+        return $this->has($name);
+    }
+
+    public function getMediaCollection($offset)
+    {
+        if (!$this->exists($offset)) {
+            return false;
+        }
+        if (null === $this->getEntity()) {
+            return false;
+        }
+        $collection = $this->get($offset);
+        if (null === $collection || !$collection) {
+            return false;
+        }
+
+        return $collection;
+    }
+
+    /**
+     * @param $offset
+     * @return array|\Illuminate\Support\Collection
+     */
+    public function getMedia($offset)
+    {
+        $collection = $this->getMediaCollection($offset);
+        if (!$collection || !is_string($collection)) {
+            return [];
+        }
+        $media = $this->getEntity()->getMedia($collection);
+        return $media;
+    }
+
+    /**
+     * @param $offset
+     * @return bool
+     */
+    public function hasMedia($offset)
+    {
+        return count($this->getMedia($offset)) > 0;
+    }
+
+    public function getImageFile($offset, $conversionName = '')
+    {
+        /** @var Media $media */
+        $media = $this->getMedia($offset)->get(0);
+        return $media->getPath($conversionName);
+    }
+
+    public function getMediaURL($offset, $default = null)
+    {
+        return $this->getImageURL($offset, '', $default);
+    }
+
+    public function getMediaURLList($offset, $default = [])
+    {
+        return $this->getImageURLList($offset, '', $default);
+    }
+
+    public function getMediaURLAt($offset, $at = 0, $default = null)
+    {
+        return $this->getImageURLAt($offset, $at, '', $default);
+    }
+
+    /**
+     * @param string $offset
+     * @param string $conversionName
+     * @param mixed $default
+     * @return string|null
+     */
+    public function getImageURL($offset, $conversionName = '', $default = null)
+    {
+        $media = $this->getMedia($offset);
+        if (!$media) {
+            return $default;
+        }
+
+        foreach ($media as $m) {
+            /** @var Media $m */
+            $res = $m->getUrl($conversionName);
+            if (!$res) {
+                continue;
+            }
+            return $res;
+        }
+
+        return $default;
+    }
+
+    /**
+     * @param string $collectionID Name of the collection
+     * @param string $conversionName
+     * @param mixed $default
+     * @return string|null
+     */
+    public function getImageURLbyCollection($collectionID, $conversionName = '', $default = null)
+    {
+        $collectionID = $collectionID ?? '';
+
+        $media = $this->getEntity()->getMedia($collectionID);
+        if (!$media) {
+            return $default;
+        }
+
+        foreach ($media as $m) {
+            /** @var Media $m */
+            $res = $m->getUrl($conversionName);
+            if (!$res) {
+                continue;
+            }
+            return $res;
+        }
+
+        return $default;
+    }
+
+    public function getImageURLAt($offset, $at = 0, $conversionName = '', $default = null)
+    {
+        $media = $this->getMedia($offset);
+        if (!$media) {
+            return $default;
+        }
+        if ($at + 1 > count($media)) {
+            return $default;
+        }
+
+        $res = $media[$at]->getUrl($conversionName);
+        if (!$res) {
+            return $default;
+        }
+        return $res;
+    }
+
+    public function getImageURLList($offset, $conversionName = '', $default = [])
+    {
+        $media = $this->getMedia($offset);
+        if (!$media) {
+            return $default;
+        }
+
+        $res = [];
+
+        foreach ($media as $m) {
+            /** @var Media $m */
+            $url = $m->getUrl($conversionName);
+            if (!$url) {
+                continue;
+            }
+
+            $res[] = $url;
+        }
+
+        return empty($res) ? $default : $res;
+    }
+
+    /**
+     * @param $entities CubistMagicAbstractModel[]
+     * @return PageData[]
+     */
+    public static function fromEntities($entities)
+    {
+        $res = [];
+        foreach ($entities as $key => $entity) {
+            $res[$entity->id] = $entity->getPageData();
+        }
+        return $res;
+    }
+}
index 779d8658ba6919ff631f9ccbbec2a9cec2c705bc..359405d03096ca37d69e7509ccf5af2d7b27c5a9 100644 (file)
@@ -5,6 +5,7 @@ namespace Cubist\Backpack\app\Magic\Models;
 use Backpack\CRUD\CrudTrait;
 use Cubist\Backpack\app\Magic\BunchOfFields;
 use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
+use Cubist\Backpack\app\Magic\EntityData;
 use Cubist\Backpack\app\Magic\Fields\Field;
 use Cubist\Backpack\app\Magic\PageData;
 use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest;
@@ -547,6 +548,21 @@ class CubistMagicAbstractModel extends Model implements HasMedia
 
     }
 
+    /**
+     * @return EntityData
+     */
+    public function getEntityData()
+    {
+        $this->withFakes();
+
+        $res = new EntityData();
+        $res->setEntity($this);
+        foreach ($this->attributes as $key => $value) {
+            $res->set($key, Json::decodeRecursive($this->getAttributeValue($key), Json::TYPE_ARRAY));
+        }
+        return $res;
+    }
+
     /**
      * @return PageData
      */
index 9e5e44673a09a32ce0fd2bc11b777366b5b12d40..dbd649c48724eed9489a5e10acaddf3d932c3fd5 100644 (file)
@@ -4,10 +4,14 @@
 namespace Cubist\Backpack\app\Magic\Models;
 
 use Cubist\Backpack\app\Http\Controllers\CubistPWAController;
+use Cubist\Backpack\app\Magic\PageData;
 use Spatie\MediaLibrary\Models\Media;
 
 class Settings extends CubistMagicModel
 {
+    /** @var PageData|null */
+    protected static $_data = null;
+
     protected $table = 'cubist_settings';
 
     protected $_options = ['name' => 'settings',
@@ -110,11 +114,23 @@ class Settings extends CubistMagicModel
 
     public static function getSettingsClass()
     {
-        $class = Settings::class;
+        $class = self::class;
         $config = config('cubist.settings_model', 'Cubist\Backpack\app\Magic\Models\Settings');
         if (class_exists($config)) {
             $class = $config;
         }
         return $class;
     }
+
+    /**
+     * @return PageData
+     */
+    public static function getData()
+    {
+        if (null === self::$_data) {
+            $settings_class = self::getSettingsClass();
+            self::$_data = $settings_class::find(1)->getPageData();
+        }
+        return self::$_data;
+    }
 }
index 8d534348f2549e283ad9a8c3060362eba3fb3c24..a907ae5e6b4b076455d373a70e1ce0f0bb89c24d 100644 (file)
 
 namespace Cubist\Backpack\app\Magic;
 
+use Cubist\Backpack\app\Magic\Models\Settings;
 
-use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
-use Cubist\Backpack\app\Magic\Models\CubistMagicModel;
-use Illuminate\Support\Arr;
-use Illuminate\Support\Facades\App;
-use Spatie\MediaLibrary\Models\Media;
-
-class PageData implements \ArrayAccess
+class PageData extends EntityData
 {
-    /**
-     * @var array
-     */
-    protected $_data = [];
-    /**
-     * @var CubistMagicAbstractModel
-     */
-    protected $_entity;
-
-    /**
-     * @return CubistMagicModel
-     */
-    public function getEntity(): CubistMagicAbstractModel
-    {
-        return $this->_entity;
-    }
-
-    /**
-     * @param CubistMagicModel $entity
-     */
-    public function setEntity(CubistMagicAbstractModel $entity): void
-    {
-        $this->_entity = $entity;
-    }
-
-    public function __construct()
-    {
-    }
-
-    /**
-     * @param string $offset
-     * @param $value
-     */
-    public function set($offset, $value)
-    {
-        $offset = square_brackets_to_dots($offset);
-        Arr::set($this->_data, $offset, $this->_fixValue($value));
-    }
-
-    protected function _fixValue($value)
-    {
-        if (!is_array($value)) {
-            return $value;
-        }
-        return $this->_fixArray($value);
-    }
-
-    protected function _fixArray($value)
-    {
-        $dot = Arr::dot($value);
-        $res = [];
-        foreach ($dot as $k => $v) {
-            Arr::set($res, square_brackets_to_dots($k), $v);
-        }
-        return $res;
-    }
-
-    /**
-     * @param string $offset
-     * @return bool
-     */
-    public function exists($offset)
-    {
-        return Arr::has($this->_data, $offset);
-    }
-
-    /**
-     * @param string $offset
-     * @return bool
-     */
-    public function has($offset)
-    {
-        return $this->exists($offset);
-    }
-
-    /**
-     * @param string $offset
-     */
-    public function unset($offset)
-    {
-        Arr::set($this->_data, $offset, null);
-    }
-
-    /**
-     * @param string|array $offset
-     * @param mixed $default
-     * @return mixed
-     */
-    public function get($offset, $default = null)
-    {
-        if (!is_array($offset)) {
-            $offset = [$offset];
-        }
-
-        foreach ($offset as $key) {
-            if (!Arr::has($this->_data, $key)) {
-                continue;
-            }
-
-            $res = Arr::get($this->_data, $key, $default);
-
-            // If an array value is saved without any items, it might be returned
-            // as a string "[]" when instead it should be an empty array...
-            if ($res === '[]') {
-                $res = [];
-            }
-
-            // Get the translated value if the array contains a key matching the locale.
-            // We have to check if the key exists because the actual value may be null.
-            // Using isset() on the locale key would give a false negative if the value is null.
-            if (is_array($res) && array_key_exists(App::getLocale(), $res)) {
-                $res = $res[App::getLocale()];
-            }
-
-            if (!$res) {
-                return $default;
-            }
-            return $res;
-
-        }
-        return $default;
-    }
-
-    /**
-     * @param mixed $offset
-     * @param mixed $value
-     */
-    public function offsetSet($offset, $value)
-    {
-        $this->set($offset, $value);
-    }
-
-    /**
-     * @param mixed $offset
-     * @return bool
-     */
-    public function offsetExists($offset)
-    {
-        return $this->exists($offset);
-    }
-
-    /**
-     * @param mixed $offset
-     */
-    public function offsetUnset($offset)
-    {
-        return $this->unset($offset);
-    }
-
-    /**
-     * @param mixed $offset
-     * @return mixed
-     */
-    public function offsetGet($offset)
-    {
-        return $this->get($offset);
-    }
-
-    /**
-     * @param $name string
-     * @return mixed
-     */
-    public function __get($name)
-    {
-        return $this->get($name);
-    }
-
-    /**
-     * @param string $name
-     * @param mixed $value
-     */
-    public function __set($name, $value)
-    {
-        return $this->set($name, $value);
-    }
-
-    /**
-     * @param string $name
-     */
-    public function __unset($name)
+    public function getMetaLongTitle()
     {
-        $this->unset($name);
+        $settings = Settings::getData();
+        return $this->get('meta_title', $this->get('title', $this->get('name')) . ' - ' . $settings->meta_title);
     }
 
-    public function __isset($name)
+    public function getMetaDescription()
     {
-        return $this->has($name);
+        $settings = Settings::getData();
+        return $this->get('meta_description', $settings->meta_description);
     }
 
-    public function getMediaCollection($offset)
+    public function getSocialTitle()
     {
-        if (!$this->exists($offset)) {
-            return false;
-        }
-        if (null === $this->getEntity()) {
-            return false;
-        }
-        $collection = $this->get($offset);
-        if (null === $collection || !$collection) {
-            return false;
-        }
-
-        return $collection;
+        return $this->get('social_title', $this->getMetaLongTitle());
     }
 
-    /**
-     * @param $offset
-     * @return array|\Illuminate\Support\Collection
-     */
-    public function getMedia($offset)
+    public function getSocialDescription()
     {
-        $collection = $this->getMediaCollection($offset);
-        if (!$collection || !is_string($collection)) {
-            return [];
-        }
-        $media = $this->getEntity()->getMedia($collection);
-        return $media;
+        return $this->get('social_description', $this->getMetaDescription());
     }
 
-    /**
-     * @param $offset
-     * @return bool
-     */
-    public function hasMedia($offset)
+    public function getSocialImage()
     {
-        return count($this->getMedia($offset)) > 0;
-    }
-
-    public function getImageFile($offset, $conversionName = '')
-    {
-        /** @var Media $media */
-        $media = $this->getMedia($offset)->get(0);
-        return $media->getPath($conversionName);
-    }
-
-    public function getMediaURL($offset, $default = null)
-    {
-        return $this->getImageURL($offset, '', $default);
-    }
-
-    public function getMediaURLList($offset, $default = [])
-    {
-        return $this->getImageURLList($offset, '', $default);
-    }
-
-    public function getMediaURLAt($offset, $at = 0, $default = null)
-    {
-        return $this->getImageURLAt($offset, $at, '', $default);
-    }
-
-    /**
-     * @param string $offset
-     * @param string $conversionName
-     * @param mixed $default
-     * @return string|null
-     */
-    public function getImageURL($offset, $conversionName = '', $default = null)
-    {
-        $media = $this->getMedia($offset);
-        if (!$media) {
-            return $default;
-        }
-
-        foreach ($media as $m) {
-            /** @var Media $m */
-            $res = $m->getUrl($conversionName);
-            if (!$res) {
-                continue;
-            }
-            return $res;
-        }
-
-        return $default;
-    }
-
-    /**
-     * @param string $collectionID Name of the collection
-     * @param string $conversionName
-     * @param mixed $default
-     * @return string|null
-     */
-    public function getImageURLbyCollection($collectionID, $conversionName = '', $default = null)
-    {
-        $collectionID = $collectionID ?? '';
-
-        $media = $this->getEntity()->getMedia($collectionID);
-        if (!$media) {
-            return $default;
-        }
-
-        foreach ($media as $m) {
-            /** @var Media $m */
-            $res = $m->getUrl($conversionName);
-            if (!$res) {
-                continue;
-            }
-            return $res;
-        }
-
-        return $default;
-    }
-
-    public function getImageURLAt($offset, $at = 0, $conversionName = '', $default = null)
-    {
-        $media = $this->getMedia($offset);
-        if (!$media) {
-            return $default;
-        }
-        if ($at + 1 > count($media)) {
-            return $default;
-        }
-
-        $res = $media[$at]->getUrl($conversionName);
-        if (!$res) {
-            return $default;
-        }
-        return $res;
-    }
-
-    public function getImageURLList($offset, $conversionName = '', $default = [])
-    {
-        $media = $this->getMedia($offset);
-        if (!$media) {
-            return $default;
-        }
-
-        $res = [];
-
-        foreach ($media as $m) {
-            /** @var Media $m */
-            $url = $m->getUrl($conversionName);
-            if (!$url) {
-                continue;
-            }
-
-            $res[] = $url;
-        }
 
-        return empty($res) ? $default : $res;
     }
 
-    /**
-     * @param $entities CubistMagicAbstractModel[]
-     * @return PageData[]
-     */
-    public static function fromEntities($entities)
+    public function getRobots()
     {
-        $res = [];
-        foreach ($entities as $key => $entity) {
-            $res[$entity->id] = $entity->getPageData();
-        }
-        return $res;
+        return ($this->get('robots', true) && config('cubist.seo_robots', true))
+            ? 'index,follow' : 'noindex,nofollow';
     }
 }