]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/front/controller/visitor/ShaarliVisitorController.php
New basePath: fix officiel plugin paths and vintage template
[github/shaarli/Shaarli.git] / application / front / controller / visitor / ShaarliVisitorController.php
index 655b3baa294ce2f639163e6ab9a820b744ae4cf7..47057d97664920a08e25af6590491a3f2cbaa7a3 100644 (file)
@@ -9,6 +9,14 @@ use Shaarli\Container\ShaarliContainer;
 use Slim\Http\Request;
 use Slim\Http\Response;
 
+/**
+ * Class ShaarliVisitorController
+ *
+ * All controllers accessible by visitors (non logged in users) should extend this abstract class.
+ * Contains a few helper function for template rendering, plugins, etc.
+ *
+ * @package Shaarli\Front\Controller\Visitor
+ */
 abstract class ShaarliVisitorController
 {
     /** @var ShaarliContainer */
@@ -54,7 +62,7 @@ abstract class ShaarliVisitorController
 
         $this->executeDefaultHooks($template);
 
-        return $this->container->pageBuilder->render($template);
+        return $this->container->pageBuilder->render($template, $this->container->basePath);
     }
 
     /**
@@ -70,19 +78,48 @@ abstract class ShaarliVisitorController
         ];
 
         foreach ($common_hooks as $name) {
-            $plugin_data = [];
+            $pluginData = [];
             $this->container->pluginManager->executeHooks(
                 'render_' . $name,
-                $plugin_data,
+                $pluginData,
                 [
                     'target' => $template,
-                    'loggedin' => $this->container->loginManager->isLoggedIn()
+                    'loggedin' => $this->container->loginManager->isLoggedIn(),
+                    'basePath' => $this->container->basePath,
                 ]
             );
-            $this->assignView('plugins_' . $name, $plugin_data);
+            $this->assignView('plugins_' . $name, $pluginData);
         }
     }
 
+    protected function executePageHooks(string $hook, array &$data, string $template = null): void
+    {
+        $params = [
+            'target' => $template,
+            'loggedin' => $this->container->loginManager->isLoggedIn(),
+            'basePath' => $this->container->basePath,
+        ];
+
+        $this->container->pluginManager->executeHooks(
+            $hook,
+            $data,
+            $params
+        );
+    }
+
+    /**
+     * Simple helper which prepend the base path to redirect path.
+     *
+     * @param Response $response
+     * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory
+     *
+     * @return Response updated
+     */
+    protected function redirect(Response $response, string $path): Response
+    {
+        return $response->withRedirect($this->container->basePath . $path);
+    }
+
     /**
      * Generates a redirection to the previous page, based on the HTTP_REFERER.
      * It fails back to the home page.
@@ -94,9 +131,10 @@ abstract class ShaarliVisitorController
         Request $request,
         Response $response,
         array $loopTerms = [],
-        array $clearParams = []
+        array $clearParams = [],
+        string $anchor = null
     ): Response {
-        $defaultPath = $request->getUri()->getBasePath();
+        $defaultPath = $this->container->basePath . '/';
         $referer = $this->container->environment['HTTP_REFERER'] ?? null;
 
         if (null !== $referer) {
@@ -125,7 +163,8 @@ abstract class ShaarliVisitorController
         }
 
         $queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
+        $anchor = $anchor ? '#' . $anchor : '';
 
-        return $response->withRedirect($path . $queryString);
+        return $response->withRedirect($path . $queryString . $anchor);
     }
 }