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();
}
--- /dev/null
+<?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;
+ }
+}
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;
}
+ /**
+ * @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
*/
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',
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;
+ }
}
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';
}
}