[TASK] Install Tool reset BE User with Ajax 56/52256/6
authorMarc Wessels <mwessels@me.com>
Thu, 30 Mar 2017 10:05:41 +0000 (12:05 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 14 Jul 2017 21:48:29 +0000 (23:48 +0200)
The 'Reset backend user preferences' button used a
controller action, but it can be improved by an ajax call.

Resolves: #80570
Releases: master
Change-Id: Idc379696d67a60b4c807abf7bb5abd6fd31f2422
Reviewed-on: https://review.typo3.org/52256
Tested-by: TYPO3com <no-reply@typo3.com>
Tested-by: Preben Rather Sørensen <preben@northchannel.dk>
Reviewed-by: Philipp Hamid <ph@supertrumpf.at>
Tested-by: Philipp Hamid <ph@supertrumpf.at>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/install/Classes/Controller/Action/Ajax/ResetBackendUserUc.php [new file with mode: 0644]
typo3/sysext/install/Classes/Controller/Action/Tool/CleanUp.php
typo3/sysext/install/Classes/Controller/AjaxController.php
typo3/sysext/install/Resources/Private/Partials/Action/Tool/CleanUp/ResetBackendUserUc.html
typo3/sysext/install/Resources/Public/JavaScript/Install.js

diff --git a/typo3/sysext/install/Classes/Controller/Action/Ajax/ResetBackendUserUc.php b/typo3/sysext/install/Classes/Controller/Action/Ajax/ResetBackendUserUc.php
new file mode 100644 (file)
index 0000000..1863892
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+declare(strict_types=1);
+
+namespace TYPO3\CMS\Install\Controller\Action\Ajax;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Install\Status\OkStatus;
+use TYPO3\CMS\Install\Status\StatusInterface;
+use TYPO3\CMS\Install\View\JsonView;
+
+/**
+ * ResetBackendUserUc
+ *
+ * This is an ajax wrapper for Reset backend user preferences
+ */
+class ResetBackendUserUc extends AbstractAjaxAction
+{
+
+    /**
+     * @param JsonView $view
+     * @throws \InvalidArgumentException
+     */
+    public function __construct(JsonView $view = null)
+    {
+        $this->view = $view ?: GeneralUtility::makeInstance(JsonView::class);
+    }
+
+    /**
+     * Initialize the handle action, sets up fluid stuff and assigns default variables.
+     * @ToDo Refactor View Initialization for all Ajax Controllers
+     */
+    protected function initializeHandle()
+    {
+        // empty on purpose because AbstractAjaxAction still overwrites $this->view with StandaloneView
+    }
+
+    /**
+     * Executes the action
+     *
+     * @return array Rendered content
+     * @throws \InvalidArgumentException
+     */
+    protected function executeAction(): array
+    {
+        $statusMessages[] = $this->resetBackendUserUc();
+
+        $this->view->assignMultiple([
+            'success' => true,
+            'status' => $statusMessages,
+        ]);
+        return $this->view->render();
+    }
+
+    /**
+     * Reset uc field of all be_users to empty string
+     *
+     * @return StatusInterface
+     * @throws \InvalidArgumentException
+     */
+    protected function resetBackendUserUc(): StatusInterface
+    {
+        GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('be_users')
+            ->update('be_users')
+            ->set('uc', '')
+            ->execute();
+        /** @var OkStatus $message */
+        $message = GeneralUtility::makeInstance(OkStatus::class);
+        $message->setTitle('Reset all backend users preferences');
+        return $message;
+    }
+}
index 594837a..1779b66 100644 (file)
@@ -46,10 +46,6 @@ class CleanUp extends Action\AbstractAction
             $this->actionMessages[] = $this->clearSelectedTables();
             $this->view->assign('postAction', 'clearTables');
         }
-        if (isset($this->postValues['set']['resetBackendUserUc'])) {
-            $this->actionMessages[] = $this->resetBackendUserUc();
-            $this->view->assign('postAction', 'resetBackendUserUc');
-        }
         if (isset($this->postValues['set']['clearProcessedFiles'])) {
             $this->actionMessages[] = $this->clearProcessedFiles();
             $this->view->assign('postAction', 'clearProcessedFiles');
@@ -161,24 +157,6 @@ class CleanUp extends Action\AbstractAction
     }
 
     /**
-     * Reset uc field of all be_users to empty string
-     *
-     * @return \TYPO3\CMS\Install\Status\StatusInterface
-     */
-    protected function resetBackendUserUc()
-    {
-        GeneralUtility::makeInstance(ConnectionPool::class)
-            ->getQueryBuilderForTable('be_users')
-            ->update('be_users')
-            ->set('uc', '')
-            ->execute();
-        /** @var OkStatus $message */
-        $message = GeneralUtility::makeInstance(OkStatus::class);
-        $message->setTitle('Reset all backend users preferences');
-        return $message;
-    }
-
-    /**
      * Data for the typo3temp/ deletion view
      *
      * @return array Data array
index aa0696f..cbd658c 100644 (file)
@@ -35,6 +35,7 @@ class AjaxController extends AbstractController
         'clearCache',
         'clearAllCache',
         'dumpAutoload',
+        'resetBackendUserUc',
         'coreUpdateUpdateVersionMatrix',
         'coreUpdateIsUpdateAvailable',
         'coreUpdateCheckPreConditions',
index b8f640d..a86e25b 100644 (file)
 
 <form method="post">
        <f:render partial="Action/Common/HiddenFormFields" arguments="{_all}" />
-
-       <f:render
-               partial="Action/Common/SubmitButton"
-               arguments="{name:'resetBackendUserUc', text:'Reset backend user preferences'}"
-       />
+       <div class="t3js-resetBackendUserUc">
+               <f:render
+                       partial="Action/Common/SubmitButton"
+                       arguments="{name:'resetBackendUserUc', text:'Reset backend user preferences'}"
+               />
+       </div>
 </form>
\ No newline at end of file
index 1461e7c..dc87186 100644 (file)
@@ -940,6 +940,134 @@ TYPO3.Install.coreUpdate = {
        }
 };
 
+TYPO3.Install.ResetBackendUserUc = {
+       /**
+        * output DOM Container
+        */
+       outputContainer: {},
+
+       /**
+        * Clone of the DOM object that contains the submit button
+        */
+       submitButton: {},
+
+       /**
+        * Default output messages
+        */
+       outputMessages: {
+               resetBackendUserUc: {
+                       fatalTitle: 'Something went wrong',
+                       fatalMessage: '',
+                       loadingTitle: 'Loading…',
+                       loadingMessage: ''
+               }
+       },
+
+       /**
+        * Fetching the templates out of the DOM
+        *
+        * @param resetBackendUserUcContainer DOM element id with all needed HTML in it
+        * @return boolean DOM container could be found and initialization finished
+        */
+       initialize: function (resetBackendUserUcContainer) {
+               var success = false;
+               this.outputContainer[resetBackendUserUcContainer] = $('.t3js-' + resetBackendUserUcContainer);
+
+               if (this.outputContainer[resetBackendUserUcContainer]) {
+                       // submit button: save and delete
+                       if (!this.submitButton[resetBackendUserUcContainer]) {
+                               var submitButton = this.outputContainer[resetBackendUserUcContainer].find('button[type="submit"]');
+                               this.submitButton[resetBackendUserUcContainer] = submitButton.clone();
+                       }
+
+                       // clear all messages from the run before
+                       this.outputContainer[resetBackendUserUcContainer].find('.typo3-message:visible ').remove();
+
+                       success = true;
+               }
+               return success;
+       },
+
+       resetBackendUserUc: function (actionName) {
+               var self = this;
+               var url = location.href + '&install[controller]=ajax&install[action]=' + actionName;
+               var isInitialized = self.initialize(actionName);
+               if (isInitialized) {
+                       self.addMessage(
+                               TYPO3.Install.Severity.loading,
+                               self.outputMessages[actionName].loadingTitle,
+                               self.outputMessages[actionName].loadingMessage,
+                               actionName
+                       );
+                       $.ajax({
+                               url: url,
+                               cache: false,
+                               success: function(data) {
+                                       if (data.success === true && Array.isArray(data.status)) {
+                                               if (data.status.length > 0) {
+                                                       self.outputContainer[actionName].find('.alert-loading').hide();
+                                                       data.status.forEach((function (element) {
+                                                               //noinspection JSUnresolvedVariable
+                                                               self.addMessage(
+                                                                       element.severity,
+                                                                       element.title,
+                                                                       element.message,
+                                                                       actionName
+                                                               );
+                                                       }));
+                                               } else {
+                                                       self.outputContainer[actionName].find('.alert-loading').hide();
+                                                       self.addMessage(
+                                                               TYPO3.Install.Severity.ok,
+                                                               self.outputMessages[actionName].successTitle,
+                                                               self.outputMessages[actionName].successMessage,
+                                                               actionName
+                                                       );
+                                               }
+                                       } else if (data === 'unauthorized') {
+                                               location.reload();
+                                       }
+                               },
+                               error: function() {
+                                       self.outputContainer[actionName].find('.alert-loading').hide();
+                                       self.addMessage(
+                                               TYPO3.Install.Severity.error,
+                                               self.outputMessages[actionName].fatalTitle,
+                                               self.outputMessages[actionName].fatalMessage,
+                                               actionName
+                                       );
+                               }
+                       });
+               }
+       },
+
+       /**
+        * Move the submit button to the end of the box
+        *
+        * @param resetBackendUserUcContainer DOM container name
+        */
+       moveSubmitButtonFurtherDown: function (resetBackendUserUcContainer) {
+               // first remove the currently visible button
+               this.outputContainer[resetBackendUserUcContainer].find('button[type="submit"]').remove();
+               // then append the cloned template to the end
+               this.outputContainer[resetBackendUserUcContainer].append(this.submitButton[resetBackendUserUcContainer]);
+       },
+
+       /**
+        * Show a status message
+        *
+        * @param severity
+        * @param title
+        * @param message
+        * @param resetBackendUserUcContainer DOM container name
+        */
+       addMessage: function (severity, title, message, resetBackendUserUcContainer) {
+               var domMessage = TYPO3.Install.FlashMessage.render(severity, title, message);
+               this.outputContainer[resetBackendUserUcContainer].append(domMessage);
+               this.moveSubmitButtonFurtherDown(resetBackendUserUcContainer);
+       }
+};
+
 $(function () {
        // Used in database compare section to select/deselect checkboxes
        $('.checkall').on('click', function () {
@@ -1077,6 +1205,16 @@ $(function () {
                }));
        }
 
+       // Handle Reset backend user preferences
+       var $resetBackendUserUcSection = $('.t3js-resetBackendUserUc');
+       if ($resetBackendUserUcSection) {
+               $resetBackendUserUcSection.on('click', 'button', (function(e) {
+                       TYPO3.Install.ResetBackendUserUc.resetBackendUserUc('resetBackendUserUc');
+                       e.preventDefault();
+                       return false;
+               }));
+       }
+
        var $installLeft = $('.t3js-list-group-wrapper');
        if ($installLeft.length > 0) {
                TYPO3.Install.Status.getFolderStatus();