Browse Source

refactor otcustomizer to use the new core controller

Olivier Massot 5 years ago
parent
commit
36fd043603

+ 9 - 58
ot_templating/Classes/Controller/OtCustomizerController.php

@@ -2,12 +2,11 @@
 
 namespace Opentalent\OtTemplating\Controller;
 
-use Opentalent\OtCore\Page\OtPageRepository;
+use Opentalent\OtCore\Controller\SelectedSiteController;
 use Opentalent\OtCore\Cache\OtCacheManager;
 use Opentalent\OtTemplating\Templating\TemplateRepository;
 use PDO;
 use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -15,34 +14,22 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  *
  * @author olivier.massot
  */
-class OtCustomizerController extends ActionController {
+class OtCustomizerController extends SelectedSiteController {
 
     /**
      * Index action (default action)
      * Displays the customizer page on the backend
      */
     public function indexAction() {
-
-        // Get the current root page's uid
-        $rootPageUid = $this->getCurrentRootPageUid();
-
-        // If the current page is not a root page or a subpage of one, abort
-        $pageSelected = ($rootPageUid !== null);
-        $this->view->assign('pageSelected', (int)$pageSelected);
-        if (!$pageSelected) {
-            return;
-        }
-
-        $this->view->assign('rootPage', $rootPageUid);
+        $this->view->assign('rootPage', $this->currentRootUid);
         $this->view->assign('templates', TemplateRepository::templates);
 
         $templateRepository = GeneralUtility::makeInstance(TemplateRepository::class);
-        $this->view->assign('currentTemplate', $templateRepository->getCurrentTemplate());
-        $this->view->assign('preferences', $templateRepository->getTemplatePreferences());
+        $this->view->assign('currentTemplate', $templateRepository->getTemplate($this->currentRootUid));
+        $this->view->assign('preferences', $templateRepository->getTemplatePreferences($this->currentRootUid));
 
         $args = $this->request->getArguments();
         $this->view->assign('preferencesUpdated', $args['preferencesUpdated']);
-
     }
 
     /**
@@ -52,16 +39,13 @@ class OtCustomizerController extends ActionController {
     public function selectTemplateAction() {
         $templateKey = $this->request->getArgument('template_key');
 
-        // Get the current root page's uid
-        $rootPageUid = $this->getCurrentRootPageUid();
-
         // applies the change in the database
         $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
         $queryBuilder->update('pages')
             ->where(
                 $queryBuilder->expr()->eq(
                     'uid',
-                    $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
+                    $queryBuilder->createNamedParameter($this->currentRootUid, PDO::PARAM_INT)
                 )
             )
             ->set('tx_opentalent_template', $templateKey)
@@ -69,7 +53,7 @@ class OtCustomizerController extends ActionController {
         ;
 
         // Clear the site's cache
-        OtCacheManager::clearSiteCache($rootPageUid);
+        OtCacheManager::clearSiteCache($this->currentRootUid);
 
         $this->forward('index');
     }
@@ -89,16 +73,13 @@ class OtCustomizerController extends ActionController {
             $prefs['displayCarousel'] = $args['displayCarousel'];
         }
 
-        // Get the current root page's uid
-        $rootPageUid = $this->getCurrentRootPageUid();
-
         // applies the change in the database
         $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
         $queryBuilder->update('pages')
             ->where(
                 $queryBuilder->expr()->eq(
                     'uid',
-                    $queryBuilder->createNamedParameter($rootPageUid, PDO::PARAM_INT)
+                    $queryBuilder->createNamedParameter($this->currentRootUid, PDO::PARAM_INT)
                 )
             )
             ->set('tx_opentalent_template_preferences', json_encode($prefs))
@@ -106,7 +87,7 @@ class OtCustomizerController extends ActionController {
         ;
 
         // Clear the site's cache
-        OtCacheManager::clearSiteCache($rootPageUid);
+        OtCacheManager::clearSiteCache($this->currentRootUid);
 
         $this->forward(
             'index',
@@ -115,34 +96,4 @@ class OtCustomizerController extends ActionController {
             ['preferencesUpdated'=>'1']
         );
     }
-
-    /**
-     * Return the uid of the root page of the currently selected
-     * site, or null if no site's page is selected
-     *
-     * @return int | null
-     */
-    protected function getCurrentRootPageUid() {
-        // Get the current page uid
-        $pageId = (int) GeneralUtility::_GP('id');
-
-        // Get the root page of the site
-        $otPageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
-        $rootPage = $otPageRepository->getRootPageFor($pageId);
-
-        if (!$rootPage['uid'] > 0) {
-            return null;
-        }
-
-        return (int)$rootPage['uid'];
-    }
-
-    /**
-     * Gets the backend user
-     *
-     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
-     */
-    protected function getBackendUser() {
-        return $GLOBALS['BE_USER'];
-    }
 }

+ 25 - 18
ot_templating/Classes/Templating/TemplateRepository.php

@@ -3,6 +3,7 @@
 namespace Opentalent\OtTemplating\Templating;
 
 use Opentalent\OtCore\Page\OtPageRepository;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -34,19 +35,25 @@ class TemplateRepository
     ];
 
     /**
-     * Returns the current site template's name
+     * Returns the site template's name
+     *
+     * @param int $rootUid
      * @return string
      */
-    public function getCurrentTemplate() {
-        $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
-
-        $rootPageUid = $pageRepository->getCurrentSiteRootPageId();
-        if (!($rootPageUid >= 0)) {
+    public function getTemplate(int $rootUid) {
+        if (!($rootUid >= 0)) {
             return self::defaultTemplate;
         }
 
-        $rootPage = $pageRepository->getPage($rootPageUid);
-        $templateName = $rootPage['tx_opentalent_template'];
+        $cnnPool = GeneralUtility::makeInstance(ConnectionPool::class);
+        $queryBuilder = $cnnPool->getQueryBuilderForTable('pages');
+        $templateName = $queryBuilder
+            ->select('tx_opentalent_template')
+            ->from('pages')
+            ->where($queryBuilder->expr()->eq('uid', $rootUid))
+            ->execute()
+            ->fetchColumn(0);
+
         if ($templateName==='') {
             return self::defaultTemplate;
         }
@@ -55,18 +62,18 @@ class TemplateRepository
 
     /**
      * Returns the current site template's name
+     * @param int $rootUid
      * @return array
      */
-    public function getTemplatePreferences() {
-        $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
-
-        $rootPageUid = $pageRepository->getCurrentSiteRootPageId();
-        if (!($rootPageUid >= 0)) {
-            return [];
-        }
-
-        $rootPage = $pageRepository->getPage($rootPageUid);
-        $templatePreferences = $rootPage['tx_opentalent_template_preferences'];
+    public function getTemplatePreferences(int $rootUid) {
+        $cnnPool = GeneralUtility::makeInstance(ConnectionPool::class);
+        $queryBuilder = $cnnPool->getQueryBuilderForTable('pages');
+        $templatePreferences = $queryBuilder
+            ->select('tx_opentalent_template_preferences')
+            ->from('pages')
+            ->where($queryBuilder->expr()->eq('uid', $rootUid))
+            ->execute()
+            ->fetchColumn(0);
 
         if ($templatePreferences==='') {
             return self::defaultPreferences;

+ 5 - 2
ot_templating/Classes/ViewHelpers/Template/CurrentViewHelper.php

@@ -4,6 +4,7 @@ namespace Opentalent\OtTemplating\ViewHelpers\Template;
 
 
 use Closure;
+use Opentalent\OtCore\Page\OtPageRepository;
 use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
 use Opentalent\OtTemplating\Templating\TemplateRepository;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -34,8 +35,10 @@ class CurrentViewHelper extends OtAbstractViewHelper
         Closure $renderChildrenClosure,
         RenderingContextInterface $renderingContext
     ) {
+        $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
+        $rootUid = $pageRepository->getCurrentSiteRootPageId();
+
         $templateRepository = GeneralUtility::makeInstance(TemplateRepository::class);
-        return $templateRepository->getCurrentTemplate();
+        return $templateRepository->getCurrentTemplate($rootUid);
     }
-
 }

+ 5 - 1
ot_templating/Classes/ViewHelpers/Template/GetPreferenceViewHelper.php

@@ -49,8 +49,12 @@ class GetPreferenceViewHelper extends OtAbstractViewHelper
         Closure $renderChildrenClosure,
         RenderingContextInterface $renderingContext
     ) {
+        $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
+        $rootUid = $pageRepository->getCurrentSiteRootPageId();
+
         $templateRepository = GeneralUtility::makeInstance(TemplateRepository::class);
-        $preferences = $templateRepository->getTemplatePreferences();
+        $preferences = $templateRepository->getTemplatePreferences($rootUid);
+
         $key = $arguments['key'];
         return $preferences[$key];
     }

+ 4 - 1
ot_templating/Classes/ViewHelpers/Template/GetPreferencesViewHelper.php

@@ -34,8 +34,11 @@ class PreferencesViewHelper extends OtAbstractViewHelper
         Closure $renderChildrenClosure,
         RenderingContextInterface $renderingContext
     ) {
+        $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
+        $rootUid = $pageRepository->getCurrentSiteRootPageId();
+
         $templateRepository = GeneralUtility::makeInstance(TemplateRepository::class);
-        return $templateRepository->getTemplatePreferences();
+        return $templateRepository->getTemplatePreferences($rootUid);
     }
 
 }

+ 69 - 80
ot_templating/Resources/Private/Templates/OtCustomizer/Index.html

@@ -8,95 +8,84 @@
 <f:section name="content">
 
     <div class="ot-be-module ot-customizer">
-        <f:if condition="{pageSelected}">
-            <f:then>
+        <div class="templates">
+            <h3>Thèmes disponibles</h3>
+            <div class="templates-gallery">
+                <f:for each="{templates}" as="template" key="template_key">
 
-                <div class="templates">
-                    <h3>Thèmes disponibles</h3>
-                    <div class="templates-gallery">
-                        <f:for each="{templates}" as="template" key="template_key">
-
-                            <div class="template-card">
-                                <div class="template-title">
-                                    {template.name}
-                                </div>
-                                <div class="template-poster">
-                                    <f:image src="{template.picture}" alt="poster" />
-                                </div>
-                                <div class="template-description">
-                                    {template.description}
-                                </div>
-                                <div class="template-controls">
+                    <div class="template-card">
+                        <div class="template-title">
+                            {template.name}
+                        </div>
+                        <div class="template-poster">
+                            <f:image src="{template.picture}" alt="poster" />
+                        </div>
+                        <div class="template-description">
+                            {template.description}
+                        </div>
+                        <div class="template-controls">
 
-                                    <f:if condition="{template_key}=={currentTemplate}">
-                                        <f:then>
-                                            <div class="active">Thème actif</div>
-                                        </f:then>
-                                        <f:else>
-                                            <f:link.action
-                                                    action="selectTemplate"
-                                                    arguments="{template_key: template_key}"
-                                                    title="select"
-                                                    class="ot-btn"
-                                            >
-                                                Utiliser ce thème
-                                            </f:link.action>
-                                        </f:else>
-                                    </f:if>
+                            <f:if condition="{template_key}=={currentTemplate}">
+                                <f:then>
+                                    <div class="active">Thème actif</div>
+                                </f:then>
+                                <f:else>
+                                    <f:link.action
+                                            action="selectTemplate"
+                                            arguments="{template_key: template_key}"
+                                            title="select"
+                                            class="ot-btn"
+                                    >
+                                        Utiliser ce thème
+                                    </f:link.action>
+                                </f:else>
+                            </f:if>
 
-                                </div>
-                            </div>
-                        </f:for>
+                        </div>
                     </div>
-                </div>
-
-                <div class="customizer">
+                </f:for>
+            </div>
+        </div>
 
-                    <h3>Autre options de personnalisation</h3>
+        <div class="customizer">
 
-                    <f:form action="updatePreferences">
-                        <div class="form-group">
-                            <label>Couleur du thème</label>
-                            <f:form.select class="form-control"
-                                           name="themeColor"
-                                           options="{light-blue: 'Bleu ciel',
-                                                       blue: 'Bleu',
-                                                       green: 'Vert',
-                                                       orange: 'Orange',
-                                                       grey: 'Gris',
-                                                       red: 'Rouge',
-                                                       light-red: 'Rouge cerise',
-                                                       purple: 'Violet'}"
-                                           value="{preferences.themeColor}">
-                            </f:form.select>
-                        </div>
-
-                        <div class="form-group">
-                            <label>Afficher le caroussel d'images</label>
-                            <f:form.checkbox name="displayCarousel"
-                                             value="1"
-                                             checked="{preferences.displayCarousel}"
-                            />
-                        </div>
+            <h3>Autre options de personnalisation</h3>
 
-                        <div class="actions">
-                            <f:form.button type="submit" class="ot-btn">
-                                Appliquer
-                            </f:form.button>
-                        </div>
+            <f:form action="updatePreferences">
+                <div class="form-group">
+                    <label>Couleur du thème</label>
+                    <f:form.select class="form-control"
+                                   name="themeColor"
+                                   options="{light-blue: 'Bleu ciel',
+                                               blue: 'Bleu',
+                                               green: 'Vert',
+                                               orange: 'Orange',
+                                               grey: 'Gris',
+                                               red: 'Rouge',
+                                               light-red: 'Rouge cerise',
+                                               purple: 'Violet'}"
+                                   value="{preferences.themeColor}">
+                    </f:form.select>
+                </div>
 
-                        <f:if condition="{preferencesUpdated}==1">
-                            <p class="success-msg">Préférences mises à jour</p>
-                        </f:if>
-                    </f:form>
+                <div class="form-group">
+                    <label>Afficher le caroussel d'images</label>
+                    <f:form.checkbox name="displayCarousel"
+                                     value="1"
+                                     checked="{preferences.displayCarousel}"
+                    />
                 </div>
-            </f:then>
-            <f:else>
-                <div class="no-page-warning">
-                    <f:comment><!-- No page selected --></f:comment>
-                    <f:translate key="noPageSelected" extensionName="ot_core" />
+
+                <div class="actions">
+                    <f:form.button type="submit" class="ot-btn">
+                        Appliquer
+                    </f:form.button>
                 </div>
-            </f:else>
-        </f:if>
+
+                <f:if condition="{preferencesUpdated}==1">
+                    <p class="success-msg">Préférences mises à jour</p>
+                </f:if>
+            </f:form>
+        </div>
     </div>
 </f:section>