From 96bf448aebb8b05be3b368ca26819c3420659d47 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 10 Jul 2019 14:10:02 +0200 Subject: [PATCH] #2878 --- .../Controllers/CubistFrontController.php | 18 +------ .../Http/Controllers/CubistPageController.php | 25 ++++++++++ .../Magic/Models/CubistMagicAbstractModel.php | 21 +++++--- src/app/Magic/PageData.php | 49 ++++++++++++------- 4 files changed, 72 insertions(+), 41 deletions(-) create mode 100644 src/app/Http/Controllers/CubistPageController.php diff --git a/src/app/Http/Controllers/CubistFrontController.php b/src/app/Http/Controllers/CubistFrontController.php index 9fb83fa..b26cc3e 100644 --- a/src/app/Http/Controllers/CubistFrontController.php +++ b/src/app/Http/Controllers/CubistFrontController.php @@ -4,9 +4,7 @@ namespace Cubist\Backpack\app\Http\Controllers; use App\Models\Page; -use Cubist\Backpack\app\Magic\Models\CMSPage; use Cubist\Backpack\app\Magic\Models\Settings; -use Cubist\Backpack\app\Magic\PageData; use Illuminate\Routing\Controller as BaseController; class CubistFrontController extends BaseController @@ -15,23 +13,9 @@ class CubistFrontController extends BaseController public function __construct() { - $this->data['global'] = new PageData(Settings::find(1)->withFakes()->getDecodedAttributes()); + $this->data['global'] = Settings::find(1)->getPageData(); } - public function index($slug = 'home') - { - $class = CMSPage::getPageClass(); - $page = $class::findBySlug($slug); - - if (!$page) { - $this->_404(); - } - - $this->data['title'] = $page->title; - $this->data['page'] = new PageData($page->withFakes()->getDecodedAttributes()); - - return view('pages.' . $page->template, $this->data); - } protected function _404() { diff --git a/src/app/Http/Controllers/CubistPageController.php b/src/app/Http/Controllers/CubistPageController.php new file mode 100644 index 0000000..0317bb8 --- /dev/null +++ b/src/app/Http/Controllers/CubistPageController.php @@ -0,0 +1,25 @@ +_404(); + } + + $this->data['title'] = $page->title; + $this->data['page'] = $page->getPageData(); + + return view('pages.' . $page->template, $this->data); + } +} diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 37ff8d3..474ed8b 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -6,6 +6,7 @@ use Backpack\CRUD\CrudTrait; use Cubist\Backpack\app\Magic\BunchOfFields; use Cubist\Backpack\app\Magic\Controllers\CubistMagicController; use Cubist\Backpack\app\Magic\Fields\Field; +use Cubist\Backpack\app\Magic\PageData; use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; use Cubist\Backpack\app\Magic\Util; use Backpack\CRUD\ModelTraits\SpatieTranslatable\Sluggable; @@ -453,7 +454,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia protected function _prepareData($attributes) { - return $attributes; + return $attributes; } public function update(array $attributes = [], array $options = []) @@ -516,18 +517,24 @@ class CubistMagicAbstractModel extends Model implements HasMedia } - public function getDecodedAttributes() + /** + * @return PageData + */ + public function getPageData() { - $res = new \stdClass(); + $this->withFakes(); + + $res = new PageData(); + $res->setEntity($this); foreach ($this->attributes as $key => $value) { - $res->$key = Json::decodeRecursive($this->getAttribute($key), Json::TYPE_OBJECT); + $res->set($key, Json::decodeRecursive($value, Json::TYPE_ARRAY)); } - $res->entity = $this; return $res; } - public function getMediaInField($c){ - if(null===$c || !$c){ + public function getMediaInField($c) + { + if (null === $c || !$c) { return []; } return $this->getMedia($c); diff --git a/src/app/Magic/PageData.php b/src/app/Magic/PageData.php index c9d60da..ecf0184 100644 --- a/src/app/Magic/PageData.php +++ b/src/app/Magic/PageData.php @@ -4,44 +4,59 @@ namespace Cubist\Backpack\app\Magic; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; +use Cubist\Backpack\app\Magic\Models\CubistMagicModel; +use Illuminate\Support\Arr; + class PageData 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($data) + public function __construct() { - if (is_array($data)) { - $this->_data = $data; - return; - } - if (is_object($data)) { - foreach ($data as $k => $v) { - $this->set($k, $v); - } - } } public function set($offset, $value) { - $this->_data[$offset] = $value; + Arr::set($this->_data, $offset, $value); } public function exists($offset) { - return isset($this->_data[$offset]) && null !== $this->_data[$offset]; + return Arr::exists($this->_data, $offset); } public function unset($offset) { - unset($this->_data[$offset]); + Arr::set($this->_data, $offset, null); } public function get($offset, $default = null) { - if (!$this->exists($offset)) { - return $default; - } - return $this->_data[$offset]; + return Arr::get($this->_data, $offset, $default); } public function offsetSet($offset, $value) -- 2.39.5