]> _ Git - cubist_cms-back.git/commitdiff
wip #7118 @7
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 23 Oct 2024 16:58:09 +0000 (18:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 23 Oct 2024 16:58:09 +0000 (18:58 +0200)
src/app/Http/Controllers/CubistFrontController.php
src/app/Http/Controllers/CubistPageController.php
src/app/Magic/Menu/Item.php
src/app/Magic/Menu/Menu.php
src/app/Magic/Menu/PageItem.php
src/app/Middleware/FrontControllerInit.php [new file with mode: 0644]

index 0c503d1a130731da1e2d61b8df9cfe4733513a45..07be3450e84e750e1a2248be10421ecdcb9c2596 100644 (file)
@@ -12,20 +12,28 @@ use Illuminate\Routing\Controller as BaseController;
 class CubistFrontController extends BaseController
 {
     public $data = [];
+    protected $_inited = false;
 
-    public function __construct()
+    protected function _404()
+    {
+        abort(404, 'Please go back to our <a href="' . url('') . '">homepage</a>.');
+    }
+
+    public function init()
     {
+        if ($this->_inited) {
+            return;
+        }
+        $this->_inited = true;
         $this->data['variant'] = App::getVariant();
         $this->data['global'] = Settings::getData();
         $this->data['locales'] = Locale::getLocalesDataForFront();
         $this->data['nav'] = Menu::getNavigation();
     }
 
-
-    protected function _404()
+    public function callAction($method, $parameters)
     {
-        abort(404, 'Please go back to our <a href="' . url('') . '">homepage</a>.');
+        $this->init();
+        return parent::callAction($method, $parameters);
     }
-
-
 }
index ccf32ad9c5919da25c16a127460a74dd6853d01e..3fdd61225522db327ec07f3eb5561fab7da496cc 100644 (file)
@@ -14,6 +14,8 @@ class CubistPageController extends CubistFrontController
 
     public function index(Request $request, $id)
     {
+        $this->init();
+
         $class = CMSPage::getPageClass();
         /** @var CMSPage $page */
         $page = Menu::getNavigation()->findOneById($id)->getEntity();
@@ -49,7 +51,7 @@ class CubistPageController extends CubistFrontController
                 $path = 'home';
             }
             $nav = Menu::getNavigation();
-            $item = $nav->findItemWithURL($path,true);
+            $item = $nav->findItemWithURL($path, true);
 
             if (null === $item) {
                 $redirectItem = $nav->findOneWithAlias($path);
@@ -65,6 +67,6 @@ class CubistPageController extends CubistFrontController
             $c['params'] = ['request' => $request] + $c['params'];
         }
 
-        return call_user_func_array([app('App\\Http\\Controllers\\' . $c['controller']), $c['action']], $c['params']);
+        return call_user_func_array([app('App\\Http\\Controllers\\' . $c['controller']), 'callAction'], [$c['action'], $c['params']]);
     }
 }
index 678d0d3bce7f6d5ba7bca1b26ad3cd179fbf13db..ee400ff5fbac684f5fb7a73ffed1ec63d396ec4f 100644 (file)
@@ -111,6 +111,11 @@ class Item
      */
     protected $_variant = '';
 
+    /**
+     * @var bool
+     */
+    protected $_status = true;
+
     /**
      * @var array
      */
@@ -129,30 +134,30 @@ class Item
      * @param string $id
      */
 
-    public function initFromDatabase($id = '#root')
+    public function initFromDatabase($id = '#root', $viewOffline = false)
     {
         $class = CMSPage::getPageClass();
         /** @var $all CMSPage[] */
-        /** @var Eloquent $r */
-        $all = $class::whereVariant($this->getVariant())->orderBy('lft')->get();
-
+        /** @var CMSPage $r */
+        $r = $class::whereVariant($this->getVariant())->orderBy('lft');
+        $all = $r->get();
         $this->setId('#root');
         $this->setBreadcrumbs(false);
         $this->setName($id);
-        $this->setChildrenFromData($all, null);
+        $this->setChildrenFromData($all, null, $viewOffline);
     }
 
     /**
      * @param $data CMSPage[]
      * @param $filter null|string
      */
-    public function setChildrenFromData($data, $filter = null)
+    public function setChildrenFromData($data, $filter = null, $viewOffline = false)
     {
         foreach ($data as $item) {
             if ($item->parent_id != $filter) {
                 continue;
             }
-            if (!$item->status) {
+            if (!$viewOffline && !$item->status) {
                 continue;
             }
             $this->addChildFromData($item, $data);
@@ -793,6 +798,22 @@ class Item
         return $this->getParent()->getRoot();
     }
 
+    /**
+     * @return bool
+     */
+    public function getStatus(): bool
+    {
+        return $this->_status;
+    }
+
+    /**
+     * @param bool $status
+     */
+    public function setStatus(bool $status): void
+    {
+        $this->_status = $status;
+    }
+
     /**
      * @param CubistMagicPageModel $entity
      */
index 5e0ede9be06d5a1fe95b3f878bf020ee67ce2883..580cef6beaa77bd63ad951cdab8575f580d2180f 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace Cubist\Backpack\app\Magic\Menu;
 
-use Cubist\Backpack\app\Template\Navigation;
-use Cubist\Backpack\app\Template\Redirection;
 use Cubist\Backpack\Facades\App;
 use Lavary\Menu\Menu as BaseMenu;
 use Illuminate\Support\Facades\Cache;
@@ -52,9 +50,12 @@ class Menu extends BaseMenu
      */
     public static function getNavigation($locale = null, $variant = null)
     {
+        $viewOffline = backpack_user() !== null && backpack_user();
+
         if (null === $locale) {
             $locale = App::getLocale();
         }
+
         if (null === $variant) {
             $variant = App::getVariant();
         }
@@ -65,12 +66,12 @@ class Menu extends BaseMenu
 
         if (!isset(self::$_nav[$variant][$locale])) {
             \Barryvdh\Debugbar\Facade::startMeasure('nav', 'Init Navigation object ' . $variant . ', ' . $locale);
-            self::$_nav[$variant][$locale] = Cache::tags(self::CACHE_TAG)->remember('navigation_' . $variant . '_' . $locale, 43200, function () use ($locale, $variant) {
+            self::$_nav[$variant][$locale] = Cache::tags(self::CACHE_TAG)->remember('navigation_' . $variant . '_' . $locale . '_' . $viewOffline, 43200, function () use ($locale, $variant, $viewOffline) {
                 \Barryvdh\Debugbar\Facade::startMeasure('donav', 'Do Navigation object ' . $variant . ', ' . $locale);
                 $nav = new Item();
                 $nav->setLocale($locale);
                 $nav->setVariant($variant);
-                $nav->initFromDatabase();
+                $nav->initFromDatabase('#root', $viewOffline);
                 \Barryvdh\Debugbar\Facade::stopMeasure('donav');
                 return $nav;
             });
index 5f6b34499b32004fd8f22ed6f51398ec092c9ecf..71dd277d7eacf07b452b440274912c4247d403e9 100644 (file)
@@ -53,6 +53,7 @@ class PageItem extends Item
         $this->setSlug($data->get('slug'));
         $this->setTitle($data->get('title'));
         $this->setRobots($data->get('robots', true));
+        $this->setStatus($data->get('status', true));
         $this->setAvailableVariants($data->get('variant', App::getVariants()));
 
         $desktop = $entity->menu_desktop == '' ? 'children' : $entity->menu_desktop;
diff --git a/src/app/Middleware/FrontControllerInit.php b/src/app/Middleware/FrontControllerInit.php
new file mode 100644 (file)
index 0000000..3947ada
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+namespace Cubist\Backpack\app\Middleware;
+
+use Closure;
+use Cubist\Backpack\app\Http\Controllers\CubistFrontController;
+use Illuminate\Http\Request;
+
+class FrontControllerInit extends CubistMiddleware
+{
+    public function handle(Request $request, Closure $next)
+    {
+        if (!$request->route()) {
+            return $this->getResponse();
+        }
+        if ($c = $request->route()->getController()) {
+            if ($c instanceof CubistFrontController && method_exists($c, 'init')) {
+                $c->init();
+            }
+        }
+        parent::handle($request, $next);
+        return $this->getResponse();
+    }
+}