[TASK] Introduce AbstractFormElement for FormEngine elements 15/33115/3
authorMarkus Klein <klein.t3@reelworx.at>
Thu, 2 Oct 2014 16:19:05 +0000 (18:19 +0200)
committerMarkus Klein <klein.t3@reelworx.at>
Thu, 2 Oct 2014 22:22:32 +0000 (00:22 +0200)
This is a follow up to #61958 which improves the code structure.

Resolves: #62011
Related: #61958
Releases: master
Change-Id: Ia89be0cfaa0779414652e5f5791b3920aa0d9927
Reviewed-on: http://review.typo3.org/33115
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Benjamin Mack <benni@typo3.org>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
12 files changed:
typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php [new file with mode: 0644]
typo3/sysext/backend/Classes/Form/Element/CheckboxElement.php
typo3/sysext/backend/Classes/Form/Element/FlexElement.php
typo3/sysext/backend/Classes/Form/Element/GroupElement.php
typo3/sysext/backend/Classes/Form/Element/InputElement.php
typo3/sysext/backend/Classes/Form/Element/NoneElement.php
typo3/sysext/backend/Classes/Form/Element/RadioElement.php
typo3/sysext/backend/Classes/Form/Element/SelectElement.php
typo3/sysext/backend/Classes/Form/Element/TextElement.php
typo3/sysext/backend/Classes/Form/Element/TreeElement.php
typo3/sysext/backend/Classes/Form/Element/UnknownElement.php
typo3/sysext/backend/Classes/Form/Element/UserElement.php

diff --git a/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php b/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
new file mode 100644 (file)
index 0000000..c82440c
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+namespace TYPO3\CMS\Backend\Form\Element;
+
+/**
+ * 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\Form\FormEngine;
+use TYPO3\CMS\Backend\Template\DocumentTemplate;
+use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
+use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Lang\LanguageService;
+
+/**
+ * Base class for form elements of FormEngine
+ */
+abstract class AbstractFormElement {
+
+       /**
+        * @var FormEngine
+        */
+       protected $formEngine;
+
+       /**
+        * Constructor function, setting the FormEngine
+        *
+        * @param FormEngine $formEngine
+        */
+       public function __construct(FormEngine $formEngine) {
+               $this->formEngine = $formEngine;
+       }
+
+       /**
+        * Handler for Flex Forms
+        *
+        * @param string $table The table name of the record
+        * @param string $field The field name which this element is supposed to edit
+        * @param array $row The record data array where the value(s) for the field can be found
+        * @param array $additionalInformation An array with additional configuration options.
+        * @return string The HTML code for the TCEform field
+        */
+       abstract public function render($table, $field, $row, &$additionalInformation);
+
+       /**
+        * @return LanguageService
+        */
+       protected function getLanguageService() {
+               return $GLOBALS['LANG'];
+       }
+
+       /**
+        * @return BackendUserAuthentication
+        */
+       protected function getBackendUserAuthentication() {
+               return $GLOBALS['BE_USER'];
+       }
+
+       /**
+        * @return DatabaseConnection
+        */
+       protected function getDatabaseConnection() {
+               return $GLOBALS['TYPO3_DB'];
+       }
+
+       /**
+        * @return DocumentTemplate
+        */
+       protected function getDocumentTemplate() {
+               return $GLOBALS['TBE_TEMPLATE'];
+       }
+
+       /**
+        * @return DocumentTemplate
+        */
+       protected function getControllerDocumentTemplate() {
+               // $GLOBALS['SOBE'] might be any kind of PHP class (controller most of the times)
+               // These classes do not inherit from any common class, but they all seem to have a "doc" member
+               return $GLOBALS['SOBE']->doc;
+       }
+}
index 301c559..7c58756 100644 (file)
@@ -17,21 +17,7 @@ namespace TYPO3\CMS\Backend\Form\Element;
 /**
  * Generation of TCEform elements of the type "check"
  */
-class CheckboxElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class CheckboxElement extends AbstractFormElement {
 
        /**
         * This will render a checkbox OR an array of checkboxes
@@ -70,8 +56,8 @@ class CheckboxElement {
                                $cBP = $this->checkBoxParams($additionalInformation['itemFormElName'], $thisValue, $c, count($selItems), implode('', $additionalInformation['fieldChangeFunc']));
                                $cBName = $additionalInformation['itemFormElName'] . '_' . $c;
                                $cBID = $additionalInformation['itemFormElID'] . '_' . $c;
-                               $item .= '<td nowrap="nowrap">' . '<input type="checkbox"' . $this->formEngine->insertDefStyle('check')
-                                       . ' value="1" name="' . $cBName . '"' . $cBP . $disabled . ' id="' . $cBID . '" />'
+                               $item .= '<td nowrap="nowrap">' . '<input type="checkbox" ' . $this->formEngine->insertDefStyle('check')
+                                       . 'value="1" name="' . $cBName . '" ' . $cBP . $disabled . 'id="' . $cBID . '" />'
                                        . '<label for="' . $cBID . '">' . htmlspecialchars($p[0]) . '</label>&nbsp;'
                                        . '</td>';
                                if ($c % $cols + 1 == $cols) {
@@ -121,4 +107,4 @@ class CheckboxElement {
                $str = ' onclick="' . htmlspecialchars($onClick) . '"' . ($thisValue & pow(2, $c) ? ' checked="checked"' : '');
                return $str;
        }
-}
\ No newline at end of file
+}
index 71fbdc2..f546102 100644 (file)
@@ -14,32 +14,14 @@ namespace TYPO3\CMS\Backend\Form\Element;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Backend\Template\DocumentTemplate;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Backend\Utility\IconUtility;
-use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
-use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Lang\LanguageService;
 
 /**
  * Generation of TCEform elements of the type "flexform"
  */
-class FlexElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class FlexElement extends AbstractFormElement {
 
        /**
         * Handler for Flex Forms
@@ -349,8 +331,7 @@ class FlexElement {
                                                        $toggleAll = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.toggleall', TRUE);
                                                        $output .= '
                                                        <div class="t3-form-field-toggle-flexsection">
-                                                               <a href="#" onclick="' . htmlspecialchars(('flexFormToggleSubs("' . $idTagPrefix
-                                                                       . '"); return false;')) . '">'
+                                                               <a href="#" onclick="flexFormToggleSubs(\'' . htmlspecialchars($idTagPrefix) . '\'); return false;">'
                                                                . IconUtility::getSpriteIcon('actions-move-right', array('title' => $toggleAll)) . $toggleAll . '
                                                                </a>
                                                        </div>
@@ -532,43 +513,4 @@ class FlexElement {
                }
                return $output;
        }
-
-       /**
-        * @return LanguageService
-        */
-       protected function getLanguageService() {
-               return $GLOBALS['LANG'];
-       }
-
-       /**
-        * @return BackendUserAuthentication
-        */
-       protected function getBackendUserAuthentication() {
-               return $GLOBALS['BE_USER'];
-       }
-
-       /**
-        * @return DatabaseConnection
-        */
-       protected function getDatabaseConnection() {
-               return $GLOBALS['TYPO3_DB'];
-       }
-
-       /**
-        * @return DocumentTemplate
-        */
-       protected function getDocumentTemplate() {
-               return $GLOBALS['TBE_TEMPLATE'];
-       }
-
-       /**
-        * @return DocumentTemplate
-        */
-       protected function getControllerDocumentTemplate() {
-               // $GLOBALS['SOBE'] might be any kind of PHP class (controller most of the times)
-               // These classes do not inherit from any common class, but they all seem to have a "doc" member
-               return $GLOBALS['SOBE']->doc;
-       }
-
-
-}
\ No newline at end of file
+}
index d3991ac..f230781 100644 (file)
@@ -16,7 +16,6 @@ namespace TYPO3\CMS\Backend\Form\Element;
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Backend\Utility\IconUtility;
-use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Resource\ProcessedFile;
 use TYPO3\CMS\Core\Resource\ResourceFactory;
@@ -26,21 +25,7 @@ use TYPO3\CMS\Core\Utility\MathUtility;
 /**
  * Generation of TCEform elements of the type "group"
  */
-class GroupElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class GroupElement extends AbstractFormElement {
 
        /**
         * This will render a selectorbox into which elements from either
@@ -93,7 +78,7 @@ class GroupElement {
                switch ((string) $config['internal_type']) {
                        case 'file_reference':
                                $config['uploadfolder'] = '';
-                       // Fall through
+                               // Fall through
                        case 'file':
                                // Creating string showing allowed types:
                                $tempFT = GeneralUtility::trimExplode(',', $allowed, TRUE);
@@ -303,11 +288,4 @@ class GroupElement {
                }
                return $item;
        }
-
-       /**
-        * @return BackendUserAuthentication
-        */
-       protected function getBackendUserAuthentication() {
-               return $GLOBALS['BE_USER'];
-       }
-}
\ No newline at end of file
+}
index 830cd39..ed61865 100644 (file)
@@ -21,21 +21,7 @@ use TYPO3\CMS\Core\Utility\MathUtility;
 /**
  * Generation of TCEform elements of the type "input"
  */
-class InputElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class InputElement extends AbstractFormElement {
 
        /**
         * This will render a single-line input form field, possibly with various control/validation features
@@ -187,4 +173,4 @@ TBE_EDITOR.customEvalFunctions[\'' . $evalData . '\'] = function(value) {
 
                return $item;
        }
-}
\ No newline at end of file
+}
index cde6b67..a1c44f3 100644 (file)
@@ -17,21 +17,7 @@ namespace TYPO3\CMS\Backend\Form\Element;
 /**
  * Generation of TCEform elements where no rendering could be found
  */
-class NoneElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class NoneElement extends AbstractFormElement {
 
        /**
         * This will render a non-editable display of the content of the field.
@@ -47,4 +33,4 @@ class NoneElement {
                $itemValue = $additionalInformation['itemFormElValue'];
                return $this->formEngine->getSingleField_typeNone_render($config, $itemValue);
        }
-}
\ No newline at end of file
+}
index dcf60d8..869030e 100644 (file)
@@ -17,21 +17,7 @@ namespace TYPO3\CMS\Backend\Form\Element;
 /**
  * Generation of TCEform elements of the type "radio"
  */
-class RadioElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class RadioElement extends AbstractFormElement {
 
        /**
         * This will render a series of radio buttons.
@@ -69,4 +55,4 @@ class RadioElement {
                }
                return $item;
        }
-}
\ No newline at end of file
+}
index 175466e..5a59455 100644 (file)
@@ -23,21 +23,7 @@ use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 /**
  * Generation of TCEform elements of the type "select"
  */
-class SelectElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class SelectElement extends AbstractFormElement {
 
        /**
         * This will render a selector box element, or possibly a special construction with two selector boxes.
@@ -641,7 +627,7 @@ class SelectElement {
                        $tableHead = '<thead>
                                        <tr class="c-header-checkbox-controls t3-row-header">
                                                <td class="c-checkbox">
-                                               <input type="checkbox" class="checkbox" onclick="if (checked) {' . htmlspecialchars(implode('', $setAll) . '} else {' . implode('', $unSetAll) . '}') . '">
+                                               <input type="checkbox" class="checkbox" onclick="if (checked) {' . htmlspecialchars(implode('', $setAll) . '} else {' . implode('', $unSetAll)) . '}">
                                                </td>
                                                <td colspan="2">
                                                </td>
@@ -759,11 +745,4 @@ class SelectElement {
                                ';
                return $item;
        }
-
-       /**
-        * @return BackendUserAuthentication
-        */
-       protected function getBackendUserAuthentication() {
-               return $GLOBALS['BE_USER'];
-       }
-}
\ No newline at end of file
+}
index 67c6e73..284566b 100644 (file)
@@ -18,26 +18,11 @@ use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Html\RteHtmlParser;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
-use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 
 /**
  * Generation of TCEform elements of the type "text"
  */
-class TextElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class TextElement extends AbstractFormElement {
 
        /**
         * This will render a <textarea> OR RTE area form field,
@@ -202,11 +187,4 @@ class TextElement {
                }
                return $wTags;
        }
-
-       /**
-        * @return BackendUserAuthentication
-        */
-       protected function getBackendUserAuthentication() {
-               return $GLOBALS['BE_USER'];
-       }
-}
\ No newline at end of file
+}
index 7781dd4..ab1ea5c 100644 (file)
@@ -22,23 +22,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  * @author Steffen Ritter <info@steffen-ritter.net>
  * @author Steffen Kamper <steffen@typo3.org>
  */
-class TreeElement {
-
-       /**
-        * Stores a reference to the original tceForms object
-        *
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $tceForms = NULL;
-
-       /**
-        * Constructor which sets the tceForms.
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $tceForms
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine &$tceForms) {
-               $this->tceForms = $tceForms;
-       }
+class TreeElement extends AbstractFormElement {
 
        /**
         * renders the tree as replacement for the selector
@@ -191,7 +175,7 @@ class TreeElement {
                                tcaExclusiveKeys: "' . ($PA['fieldConf']['config']['exclusiveKeys'] ? $PA['fieldConf']['config']['exclusiveKeys'] : '') . '",
                                ucId: "' . md5(($table . '|' . $field)) . '",
                                selModel: TYPO3.Components.Tree.EmptySelectionModel,
-                               disabled: ' . ($PA['fieldConf']['config']['readOnly'] || $this->tceForms->renderReadonly ? 'true' : 'false') . '
+                               disabled: ' . ($PA['fieldConf']['config']['readOnly'] || $this->formEngine->renderReadonly ? 'true' : 'false') . '
                        });' . LF .
                                ($autoSizeMax
                                        ? 'tree' . $id . '.bodyStyle = "max-height: ' . $autoSizeMax . 'px;min-height: ' . $height . 'px;";'
@@ -211,4 +195,17 @@ class TreeElement {
                return $formField;
        }
 
+       /**
+        * Dummy handler
+        *
+        * @param string $table The table name of the record
+        * @param string $field The field name which this element is supposed to edit
+        * @param array $row The record data array where the value(s) for the field can be found
+        * @param array $additionalInformation An array with additional configuration options.
+        * @return string The HTML code for the TCEform field
+        */
+       public function render($table, $field, $row, &$additionalInformation) {
+               // deliberately empty as this class is not used the same way
+               return '';
+       }
 }
index 51f5a98..4293cad 100644 (file)
@@ -17,21 +17,7 @@ namespace TYPO3\CMS\Backend\Form\Element;
 /**
  * Generation of TCEform elements of where the type is unknown
  */
-class UnknownElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class UnknownElement extends AbstractFormElement {
 
        /**
         * Handler for unknown types.
@@ -43,7 +29,7 @@ class UnknownElement {
         * @return string The HTML code for the TCEform field
         */
        public function render($table, $field, $row, &$additionalInformation) {
-               $item = 'Unknown type: ' . $additionalInformation['fieldConf']['config']['form_type'] . '<br />';
-               return $item;
+               return 'Unknown type: ' . $additionalInformation['fieldConf']['config']['form_type'] . '<br />';
+
        }
-}
\ No newline at end of file
+}
index b2c293e..3d29d53 100644 (file)
@@ -13,25 +13,12 @@ namespace TYPO3\CMS\Backend\Form\Element;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Generation of TCEform elements of the type "user"
  */
-class UserElement {
-
-       /**
-        * @var \TYPO3\CMS\Backend\Form\FormEngine
-        */
-       protected $formEngine;
-
-       /**
-        * Constructor function, setting the FormEngine
-        *
-        * @param \TYPO3\CMS\Backend\Form\FormEngine $formEngine
-        */
-       public function __construct(\TYPO3\CMS\Backend\Form\FormEngine $formEngine) {
-               $this->formEngine = $formEngine;
-       }
+class UserElement extends AbstractFormElement {
 
        /**
         * User defined field type
@@ -46,8 +33,10 @@ class UserElement {
                $additionalInformation['table'] = $table;
                $additionalInformation['field'] = $field;
                $additionalInformation['row'] = $row;
-               $additionalInformation['parameters'] = isset($additionalInformation['fieldConf']['config']['parameters']) ? $additionalInformation['fieldConf']['config']['parameters'] : array();
+               $additionalInformation['parameters'] = isset($additionalInformation['fieldConf']['config']['parameters'])
+                       ? $additionalInformation['fieldConf']['config']['parameters']
+                       : array();
                $additionalInformation['pObj'] = &$this;
-               return \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($additionalInformation['fieldConf']['config']['userFunc'], $additionalInformation, $this);
+               return GeneralUtility::callUserFunction($additionalInformation['fieldConf']['config']['userFunc'], $additionalInformation, $this);
        }
-}
\ No newline at end of file
+}