Selaa lähdekoodia

add new common classes in ot_core

Olivier Massot 5 vuotta sitten
vanhempi
commit
35bcdfff6c

+ 44 - 0
ot_core/Classes/Controller/SelectedSiteController.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace Opentalent\OtCore\Controller;
+
+use Opentalent\OtCore\Exception\NoSiteSelected;
+use Opentalent\OtCore\Page\OtPageRepository;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
+
+/**
+ * Base class for all controllers of backend modules that need
+ * a typo3 site's page to be selected to execute **ALL** of its actions methods
+ *
+ * If no page is selected or if this page does not have any root page as an ancestor,
+ * the action is forwarded to a page asking the user to select one.
+ *
+ * Class BeOnSiteController
+ */
+class SelectedSiteController extends ActionController
+{
+    /**
+     * The current site root page uid
+     *
+     * @var int
+     */
+    protected $currentRootUid;
+
+    protected function callActionMethod() {
+        try {
+            $otPageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
+            $this->currentRootUid = $otPageRepository->getCurrentRootUid();
+        } catch (NoSiteSelected $e) {
+            $this->currentRootUid = null;
+        }
+
+        if ($this->actionMethodName != 'displayNoSelectedPageWarningAction' && $this->currentRootUid == null) {
+            $this->forward('displayNoSelectedPageWarning', 'SelectedSite', 'OtCore');
+        }
+
+        parent::callActionMethod();
+    }
+
+    protected function displayNoSelectedPageWarningAction() {}
+}

+ 53 - 0
ot_core/Classes/Logging/OtLogger.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace Opentalent\OtCore\Logging;
+
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerAwareTrait;
+use TYPO3\CMS\Core\Log\LogLevel;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+class OtLogger implements LoggerAwareInterface
+{
+    use LoggerAwareTrait;
+
+    public static function log(int $level, string $msg, array $context = []) {
+        $loggerService = GeneralUtility::makeInstance(self::class);
+        $loggerService->getLogger()->log($level, $msg, $context);
+    }
+
+    public function getLogger() {
+        return $this->logger;
+    }
+
+    public static function alert(string $msg, array $context = []) {
+        self::log(LogLevel::ALERT, $msg, $context);
+    }
+
+    public static function critical(string $msg, array $context = []) {
+        self::log(LogLevel::CRITICAL, $msg, $context);
+    }
+
+    public static function error(string $msg, array $context = []) {
+        self::log(LogLevel::ERROR, $msg, $context);
+    }
+
+    public static function warning(string $msg, array $context = []) {
+        self::log(LogLevel::WARNING, $msg, $context);
+    }
+
+    public static function notice(string $msg, array $context = []) {
+        self::log(LogLevel::NOTICE, $msg, $context);
+    }
+
+    public static function info(string $msg, array $context = []) {
+        self::log(LogLevel::INFO, $msg, $context);
+    }
+
+    public static function debug(string $msg, array $context = []) {
+        self::log(LogLevel::DEBUG, $msg, $context);
+    }
+
+
+
+}

+ 25 - 2
ot_core/Classes/Page/OtPageRepository.php

@@ -3,6 +3,7 @@
 namespace Opentalent\OtCore\Page;
 
 use FluidTYPO3\Vhs\Service\PageService;
+use Opentalent\OtCore\Exception\NoSiteSelected;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Exception\SiteNotFoundException;
 use TYPO3\CMS\Core\Site\Entity\Site;
@@ -122,7 +123,8 @@ class OtPageRepository extends PageRepository
     }
 
     /**
-     * Returns the current site's rootpage array (FE only)
+     * [Frontend Only]
+     * Returns the current site's rootpage array
      *
      * @return array
      */
@@ -132,12 +134,33 @@ class OtPageRepository extends PageRepository
     }
 
     /**
+     * [Backend Only]
      * Returns the page currently selected in the backend if any
      *
      * @return int|null
      */
-    public function getCurrentBackendPage() {
+    public function getCurrentPageId() {
         return (int)GeneralUtility::_GP('id');
     }
 
+    /**
+     * [Backend Only]
+     * Return the root uid of the currently selected website if any,
+     * or throw a NoSiteSelected exception
+     *
+     * @return int
+     * @throws NoSiteSelected
+     */
+    public function getCurrentRootUid() {
+        $pageUid = $this->getCurrentPageId();
+
+        $otPageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
+        $rootPage = $otPageRepository->getRootPageFor($pageUid);
+        $rootUid = $rootPage['uid'] ?? 0;
+        if (!$rootUid > 0) {
+            throw new NoSiteSelected();
+        }
+        return $rootUid;
+    }
+
 }

+ 7 - 0
ot_core/Resources/Private/Layouts/Backend/Default.html

@@ -0,0 +1,7 @@
+{namespace v=FluidTYPO3\Vhs\ViewHelpers}
+
+<f:be.container includeCssFiles="{ot_core: '{f:uri.resource(path:\'assets/Backend/style/ot_be_module.css\')}'}">
+    <div class="ot-be-module">
+        <f:render section="content" />
+    </div>
+</f:be.container>

+ 9 - 0
ot_core/Resources/Private/Templates/SelectedSite/DisplayNoSelectedPageWarning.html

@@ -0,0 +1,9 @@
+
+<f:layout name="Backend/Default" />
+
+<f:section name="content">
+    <div class="no-page-warning">
+        <f:comment><!-- No page selected --></f:comment>
+        <f:translate key="noPageSelected" />
+    </div>
+</f:section>