From: Vincent Vanwaelscappel Date: Thu, 9 Jan 2020 15:53:15 +0000 (+0100) Subject: wip #3318 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=6f1de2bc1c205d30b7e952c64d6e1757b6c6aec6;p=cubist_cms-back.git wip #3318 @3 --- diff --git a/src/app/Http/Controllers/CubistPageController.php b/src/app/Http/Controllers/CubistPageController.php index ba1999f..6c79881 100644 --- a/src/app/Http/Controllers/CubistPageController.php +++ b/src/app/Http/Controllers/CubistPageController.php @@ -16,7 +16,7 @@ class CubistPageController extends CubistFrontController { $class = CMSPage::getPageClass(); /** @var CMSPage $page */ - $page = $class::findBySlug($slug); + $page = Menu::getNavigation()->findOneBy('slug', $slug)->getEntity(); if (!$page) { $this->_404(); diff --git a/src/app/Magic/Menu/Item.php b/src/app/Magic/Menu/Item.php index 44caab3..6be2db1 100644 --- a/src/app/Magic/Menu/Item.php +++ b/src/app/Magic/Menu/Item.php @@ -126,11 +126,8 @@ class Item { $class = CMSPage::getPageClass(); /** @var $all CMSPage[] */ - $r = $class::orderBy('lft'); - if (App::hasVariant()) { - $r->whereRaw('variant IS NULL OR JSON_CONTAINS(variant, \'["' . App::getVariant() . '"]\')'); - } - $all = $r->get(); + /** @var Eloquent $r */ + $all = $class::whereVariant($this->getVariant())->orderBy('lft')->get(); $this->setId('#root'); $this->setRoot($this); diff --git a/src/app/Magic/Models/CMSPage.php b/src/app/Magic/Models/CMSPage.php index 77d4578..4017a4f 100644 --- a/src/app/Magic/Models/CMSPage.php +++ b/src/app/Magic/Models/CMSPage.php @@ -5,6 +5,7 @@ namespace Cubist\Backpack\app\Magic\Models; use Cubist\Backpack\app\Magic\Controllers\CubistMagicController; use Cubist\Backpack\app\Template\TemplateAbstract; +use Cubist\Backpack\Facades\App; use Cubist\Util\Json; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; @@ -16,7 +17,7 @@ class CMSPage extends CubistMagicNestedModel protected $_usedTemplate = null; protected static $_templatesById = null; - protected static $_pagesList = null; + protected static $_pagesList = []; protected static $_table = 'cubist_cms_pages'; protected $table = 'cubist_cms_pages'; @@ -220,16 +221,23 @@ class CMSPage extends CubistMagicNestedModel return $res; } - public static function getPagesList() + public static function getPagesList($variant = null) { - if (null === static::$_pagesList) { + if (null === $variant) { + $variant = App::getVariant(); + } + if (!isset(static::$_pagesList[$variant])) { try { - static::$_pagesList = DB::table(self::$_table)->orderBy('lft')->get()->pluck('name', 'id'); + $r = DB::table(self::$_table)->orderBy('lft'); + if (App::hasVariant()) { + $r->whereRaw('variant IS NULL OR JSON_CONTAINS(variant, \'["' . $variant . '"]\')'); + } + static::$_pagesList[$variant] = $r->get()->pluck('name', 'id'); } catch (Exception $e) { - static::$_pagesList = []; + static::$_pagesList[$variant] = []; } } - return static::$_pagesList; + return static::$_pagesList[$variant]; } protected function _prepareData($attributes) diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 70fca65..c671c9b 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -8,11 +8,14 @@ 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\QueryBuilder; use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; use Cubist\Backpack\app\Magic\Util; +use Cubist\Backpack\Facades\App; use Cubist\Util\Json; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; use Spatie\MediaLibrary\HasMedia\HasMedia; @@ -602,4 +605,13 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $new; } + + /** + * @param \Illuminate\Database\Query\Builder $query + * @return QueryBuilder|Builder|Model + */ + public function newEloquentBuilder($query) + { + return new QueryBuilder($query); + } } diff --git a/src/app/Magic/QueryBuilder.php b/src/app/Magic/QueryBuilder.php new file mode 100644 index 0000000..ea6c0b5 --- /dev/null +++ b/src/app/Magic/QueryBuilder.php @@ -0,0 +1,20 @@ +whereRaw('variant IS NULL OR JSON_CONTAINS(variant, \'["' . $variant . '"]\')'); + } + return $this; + } +}