[TASK] Integrate DocHeader action buttons 38/43838/6
authorOliver Hader <oliver@typo3.org>
Mon, 5 Oct 2015 21:28:29 +0000 (23:28 +0200)
committerMathias Schreiber <mathias.schreiber@wmdb.de>
Fri, 9 Oct 2015 08:28:29 +0000 (10:28 +0200)
To avoid duplication of logic and code, for recurring
buttons in the decoupled DocHeader, new action buttons
are introduced.

Resolves: #70422
Releases: master
Change-Id: I3cdf662607d861510e311fa6ab0594528574f271
Reviewed-on: http://review.typo3.org/43838
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
typo3/sysext/backend/Classes/Template/Components/ButtonBar.php
typo3/sysext/backend/Classes/Template/Components/Buttons/Action/HelpButton.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Template/Components/Buttons/Action/ShortcutButton.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Template/Components/Buttons/PositionInterface.php [new file with mode: 0644]

index 4628737..b134091 100644 (file)
@@ -15,10 +15,13 @@ namespace TYPO3\CMS\Backend\Template\Components;
  */
 
 use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
+use TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface;
 use TYPO3\CMS\Backend\Template\Components\Buttons\FullyRenderedButton;
 use TYPO3\CMS\Backend\Template\Components\Buttons\InputButton;
 use TYPO3\CMS\Backend\Template\Components\Buttons\LinkButton;
 use TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton;
+use TYPO3\CMS\Backend\Template\Components\Buttons\Action\HelpButton;
+use TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -59,9 +62,14 @@ class ButtonBar
         $buttonPosition = self::BUTTON_POSITION_LEFT,
         $buttonGroup = 1
     ) {
-        if (!$button->isValid($button)) {
+        if (!$button->isValid()) {
             throw new \InvalidArgumentException('Button "' . $button->getType() . '" is not valid', 1441706370);
         }
+        // Determine the default button position
+        if ($button instanceof PositionInterface) {
+            $buttonPosition = $button->getPosition();
+            $buttonGroup = $button->getGroup();
+        }
         // We make the button immutable here
         $this->buttons[$buttonPosition][$buttonGroup][] = clone $button;
         return $this;
@@ -126,6 +134,26 @@ class ButtonBar
     }
 
     /**
+     * Creates a new ShortcutButton
+     *
+     * @return ShortcutButton
+     */
+    public function makeShortcutButton()
+    {
+        return GeneralUtility::makeInstance(ShortcutButton::class);
+    }
+
+    /**
+     * Creates a new HelpButton
+     *
+     * @return HelpButton
+     */
+    public function makeHelpButton()
+    {
+        return GeneralUtility::makeInstance(HelpButton::class);
+    }
+
+    /**
      * Returns an associative array of all buttons in the form of
      * ButtonPosition > ButtonGroup > Button
      *
@@ -134,7 +162,7 @@ class ButtonBar
     public function getButtons()
     {
         // here we need to call the sorting methods and stuff.
-        foreach ($this->buttons as  $position => $_) {
+        foreach ($this->buttons as $position => $_) {
             ksort($this->buttons[$position]);
         }
         // @todo do we want to provide a hook here?
diff --git a/typo3/sysext/backend/Classes/Template/Components/Buttons/Action/HelpButton.php b/typo3/sysext/backend/Classes/Template/Components/Buttons/Action/HelpButton.php
new file mode 100644 (file)
index 0000000..11c2a7c
--- /dev/null
@@ -0,0 +1,156 @@
+<?php
+namespace TYPO3\CMS\Backend\Template\Components\Buttons\Action;
+
+/*
+ * 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\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Backend\Template\Components\ButtonBar;
+use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
+use TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface;
+
+/**
+ * HelpButton
+ *
+ * Renders a help button in the DocHeader which will be rendered
+ * to the right position using button group "99".
+ *
+ * EXAMPLE USAGE TO ADD A HELP BUTTON:
+ *
+ * $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
+ * $myButton = $buttonBar->makeHelpButton()
+ *       ->setModuleName('xMOD_csh_corebe')
+ *       ->setFieldName('list_module');
+ * $buttonBar->addButton($myButton);
+ */
+class HelpButton implements ButtonInterface, PositionInterface
+{
+    /**
+     * @var string
+     */
+    protected $moduleName;
+
+    /**
+     * @var string
+     */
+    protected $fieldName;
+
+    /**
+     * Gets the name of the module.
+     *
+     * @return string
+     */
+    public function getModuleName()
+    {
+        return $this->moduleName;
+    }
+
+    /**
+     * Sets the name of the module.
+     *
+     * @param string $moduleName
+     * @return HelpButton
+     */
+    public function setModuleName($moduleName)
+    {
+        $this->moduleName = $moduleName;
+        return $this;
+    }
+
+    /**
+     * Gets the name of the field.
+     *
+     * @return string
+     */
+    public function getFieldName()
+    {
+        return $this->fieldName;
+    }
+
+    /**
+     * Sets the name of the field.
+     *
+     * @param string $fieldName
+     * @return HelpButton
+     */
+    public function setFieldName($fieldName)
+    {
+        $this->fieldName = $fieldName;
+        return $this;
+    }
+
+    /**
+     * Gets the button position.
+     *
+     * @return string
+     */
+    public function getPosition()
+    {
+        return ButtonBar::BUTTON_POSITION_RIGHT;
+    }
+
+    /**
+     * Gets the button group.
+     *
+     * @return int
+     */
+    public function getGroup()
+    {
+        return 99;
+    }
+
+    /**
+     * Gets the type of the button
+     *
+     * @return string
+     */
+    public function getType()
+    {
+        return get_class($this);
+    }
+
+    /**
+     * Determines whether the button shall be rendered.
+     * Depends on the defined module and field name.
+     *
+     * @return bool
+     */
+    public function isValid()
+    {
+        return (
+            !empty($this->moduleName)
+            && !empty($this->fieldName)
+        );
+    }
+
+    /**
+     * Renders the button
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        return $this->render();
+    }
+
+    /**
+     * Renders the button
+     *
+     * @return string
+     */
+    public function render()
+    {
+        $helpMarkup = BackendUtility::cshItem($this->moduleName, $this->fieldName);
+        return '<span class="btn btn-sm btn-default">' . $helpMarkup . '</span>';
+    }
+}
diff --git a/typo3/sysext/backend/Classes/Template/Components/Buttons/Action/ShortcutButton.php b/typo3/sysext/backend/Classes/Template/Components/Buttons/Action/ShortcutButton.php
new file mode 100644 (file)
index 0000000..f60910e
--- /dev/null
@@ -0,0 +1,236 @@
+<?php
+namespace TYPO3\CMS\Backend\Template\Components\Buttons\Action;
+
+/*
+ * 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\Backend\Template\DocumentTemplate;
+use TYPO3\CMS\Backend\Template\Components\ButtonBar;
+use TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface;
+use TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface;
+use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
+
+/**
+ * ShortcutButton
+ *
+ * Renders a shortcut button in the DocHeader which will be rendered
+ * to the right position using button group "91".
+ *
+ * EXAMPLE USAGE TO ADD A SHORTCUT BUTTON:
+ *
+ * $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
+ * $myButton = $buttonBar->makeShortcutButton()
+ *       ->setModuleName('my_info');
+ * $extbaseButton = $buttonBar->makeShortcutButton()
+ *      ->setControllerContext($this->controllerContext);
+ * $buttonBar->addButton($myButton);
+ * $buttonBar->addButton($extbaseButton);
+ */
+class ShortcutButton implements ButtonInterface, PositionInterface
+{
+    /**
+     * @var string
+     */
+    protected $moduleName;
+
+    /**
+     * @var array
+     */
+    protected $setVariables = array();
+
+    /**
+     * @var array
+     */
+    protected $getVariables = array();
+
+    /**
+     * @var ControllerContext
+     */
+    protected $controllerContext;
+
+    /**
+     * Gets the name of the module.
+     *
+     * @return string
+     */
+    public function getModuleName()
+    {
+        return $this->moduleName;
+    }
+
+    /**
+     * Sets the name of the module.
+     *
+     * @param string $moduleName
+     * @return ShortcutButton
+     */
+    public function setModuleName($moduleName)
+    {
+        $this->moduleName = $moduleName;
+        return $this;
+    }
+
+    /**
+     * Gets the SET variables.
+     *
+     * @return array
+     */
+    public function getSetVariables()
+    {
+        return $this->setVariables;
+    }
+
+    /**
+     * Sets the SET variables.
+     *
+     * @param array $setVariables
+     * @return ShortcutButton
+     */
+    public function setSetVariables(array $setVariables)
+    {
+        $this->setVariables = $setVariables;
+        return $this;
+    }
+
+    /**
+     * Gets the GET variables.
+     *
+     * @return array
+     */
+    public function getGetVariables()
+    {
+        return $this->getVariables;
+    }
+
+    /**
+     * Sets the GET variables.
+     *
+     * @param array $getVariables
+     * @return ShortcutButton
+     */
+    public function setGetVariables(array $getVariables)
+    {
+        $this->getVariables = $getVariables;
+        return $this;
+    }
+
+    /**
+     * Gets the button position.
+     *
+     * @return string
+     */
+    public function getPosition()
+    {
+        return ButtonBar::BUTTON_POSITION_RIGHT;
+    }
+
+    /**
+     * Gets the button group.
+     *
+     * @return int
+     */
+    public function getGroup()
+    {
+        return 91;
+    }
+
+    /**
+     * Gets the type of the button
+     *
+     * @return string
+     */
+    public function getType()
+    {
+        return get_class($this);
+    }
+
+    /**
+     * Determines whether the button shall be rendered.
+     * Depends on the backend user permission to create
+     * shortcuts and the defined module name.
+     *
+     * @return bool
+     */
+    public function isValid()
+    {
+        $this->preProcess();
+
+        return (
+            $this->getBackendUser()->mayMakeShortcut()
+            && !empty($this->moduleName)
+        );
+    }
+
+    /**
+     * Renders the button
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        return $this->render();
+    }
+
+    /**
+     * Renders the button
+     *
+     * @return string
+     */
+    public function render()
+    {
+        /** @var DocumentTemplate $documentTemplate */
+        $documentTemplate = GeneralUtility::makeInstance(DocumentTemplate::class);
+        $shortcutMarkup = $documentTemplate->makeShortcutIcon(
+            implode(',', $this->getVariables),
+            implode(',', $this->setVariables),
+            $this->moduleName
+        );
+
+        return '<span class="btn btn-sm btn-default">' . $shortcutMarkup . '</span>';
+    }
+
+    /**
+     * Pre-processes class member values.
+     */
+    protected function preProcess()
+    {
+        $emptyGetVariables = (count($this->getVariables) === 0);
+
+        // Set default GET parameters
+        if ($emptyGetVariables) {
+            $this->getVariables = array('id', 'M');
+        }
+
+        // Automatically determine module name in Extbase context
+        if ($this->controllerContext !== null) {
+            $currentRequest = $this->controllerContext->getRequest();
+            $extensionName = $currentRequest->getControllerExtensionName();
+            $this->moduleName = $currentRequest->getPluginName();
+            // Extend default GET parameters
+            if ($emptyGetVariables) {
+                $modulePrefix = strtolower('tx_' . $extensionName . '_' . $this->moduleName);
+                $this->getVariables[] = $modulePrefix;
+            }
+        }
+    }
+
+    /**
+     * @return BackendUserAuthentication
+     */
+    protected function getBackendUser()
+    {
+        return $GLOBALS['BE_USER'];
+    }
+}
diff --git a/typo3/sysext/backend/Classes/Template/Components/Buttons/PositionInterface.php b/typo3/sysext/backend/Classes/Template/Components/Buttons/PositionInterface.php
new file mode 100644 (file)
index 0000000..9de35da
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+namespace TYPO3\CMS\Backend\Template\Components\Buttons;
+
+/*
+ * 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!
+ */
+
+/**
+ * Interface for buttons
+ */
+interface PositionInterface
+{
+    /**
+     * Gets the button position.
+     *
+     * @return string
+     */
+    public function getPosition();
+
+    /**
+     * Gets the button group.
+     *
+     * @return int
+     */
+    public function getGroup();
+}