[TASK] Improve readability of TextElement code 69/33169/3
authorWouter Wolters <typo3@wouterwolters.nl>
Sun, 5 Oct 2014 15:38:42 +0000 (17:38 +0200)
committerMarkus Klein <klein.t3@reelworx.at>
Mon, 13 Oct 2014 23:07:30 +0000 (01:07 +0200)
FormEngine type="text" in class TextElements gets some
code beautification to improve readability.

Resolves: #62040
Releases: master
Change-Id: I219dc1fcabc656aff6eec81066fa7d51ca950d59
Reviewed-on: http://review.typo3.org/33169
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/backend/Classes/Form/Element/TextElement.php

index 284566b..5207522 100644 (file)
@@ -35,62 +35,93 @@ class TextElement extends AbstractFormElement {
         * @return string The HTML code for the TCEform field
         */
        public function render($table, $field, $row, &$additionalInformation) {
-               // Init config:
                $config = $additionalInformation['fieldConf']['config'];
-               $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
                if ($this->formEngine->renderReadonly || $config['readOnly']) {
                        return $this->formEngine->getSingleField_typeNone_render($config, $additionalInformation['itemFormElValue']);
                }
-               // Setting columns number:
-               $cols = MathUtility::forceIntegerInRange($config['cols'] ? $config['cols'] : 30, 5, $this->formEngine->maxTextareaWidth);
-               // Setting number of rows:
-               $origRows = ($rows = MathUtility::forceIntegerInRange($config['rows'] ? $config['rows'] : 5, 1, 20));
-               if (strlen($additionalInformation['itemFormElValue']) > $this->formEngine->charsPerRow * 2) {
+
+               // Setting columns number
+               $cols = MathUtility::forceIntegerInRange($config['cols'] ?: 30, 5, $this->formEngine->maxTextareaWidth);
+
+               // Setting number of rows
+               $rows = MathUtility::forceIntegerInRange($config['rows'] ?: 5, 1, 20);
+               $originalRows = $rows;
+
+               $itemFormElementValueLength = strlen($additionalInformation['itemFormElValue']);
+               if ($itemFormElementValueLength > $this->formEngine->charsPerRow * 2) {
                        $cols = $this->formEngine->maxTextareaWidth;
-                       $rows = MathUtility::forceIntegerInRange(round(strlen($additionalInformation['itemFormElValue']) / $this->formEngine->charsPerRow), count(explode(LF, $additionalInformation['itemFormElValue'])), 20);
-                       if ($rows < $origRows) {
-                               $rows = $origRows;
+                       $rows = MathUtility::forceIntegerInRange(
+                               round($itemFormElementValueLength / $this->formEngine->charsPerRow),
+                               count(explode(LF, $additionalInformation['itemFormElValue'])),
+                               20
+                       );
+                       if ($rows < $originalRows) {
+                               $rows = $originalRows;
                        }
                }
-               if (in_array('required', $evalList)) {
+
+               $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
+               if (in_array('required', $evalList, TRUE)) {
                        $this->formEngine->requiredFields[$table . '_' . $row['uid'] . '_' . $field] = $additionalInformation['itemFormElName'];
                }
-               // Init RTE vars:
+               // Init RTE vars
                // Set TRUE, if the RTE is loaded; If not a normal textarea is shown.
-               $RTEwasLoaded = 0;
+               $rteWasLoaded = FALSE;
                // Set TRUE, if the RTE would have been loaded if it wasn't for the disable-RTE flag in the bottom of the page...
-               $RTEwouldHaveBeenLoaded = 0;
+               $rteWouldHaveBeenLoaded = FALSE;
                // "Extra" configuration; Returns configuration for the field based on settings found in the "types" fieldlist. Traditionally, this is where RTE configuration has been found.
-               $specConf = $this->formEngine->getSpecConfFromString($additionalInformation['extra'], $additionalInformation['fieldConf']['defaultExtras']);
+               $specialConfiguration = $this->formEngine->getSpecConfFromString($additionalInformation['extra'], $additionalInformation['fieldConf']['defaultExtras']);
                // Setting up the altItem form field, which is a hidden field containing the value
                $altItem = '<input type="hidden" name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '" value="' . htmlspecialchars($additionalInformation['itemFormElValue']) . '" />';
                $item = '';
                // If RTE is generally enabled (TYPO3_CONF_VARS and user settings)
                if ($this->formEngine->RTEenabled) {
-                       $p = BackendUtility::getSpecConfParametersFromArray($specConf['rte_transform']['parameters']);
+                       $parameters = BackendUtility::getSpecConfParametersFromArray($specialConfiguration['rte_transform']['parameters']);
                        // If the field is configured for RTE and if any flag-field is not set to disable it.
-                       if (isset($specConf['richtext']) && (!$p['flag'] || !$row[$p['flag']])) {
+                       if (isset($specialConfiguration['richtext']) && (!$parameters['flag'] || !$row[$parameters['flag']])) {
                                BackendUtility::fixVersioningPid($table, $row);
-                               list($tscPID, $thePidValue) = $this->formEngine->getTSCpid($table, $row['uid'], $row['pid']);
+                               list($recordPid, $tsConfigPid) = $this->formEngine->getTSCpid($table, $row['uid'], $row['pid']);
                                // If the pid-value is not negative (that is, a pid could NOT be fetched)
-                               if ($thePidValue >= 0) {
-                                       $RTEsetup = $this->getBackendUserAuthentication()->getTSConfig('RTE', BackendUtility::getPagesTSconfig($tscPID));
-                                       $RTEtypeVal = BackendUtility::getTCAtypeValue($table, $row);
-                                       $thisConfig = BackendUtility::RTEsetup($RTEsetup['properties'], $table, $field, $RTEtypeVal);
-                                       if (!$thisConfig['disabled']) {
+                               if ($tsConfigPid >= 0) {
+                                       $rteSetup = $this->getBackendUserAuthentication()->getTSConfig('RTE', BackendUtility::getPagesTSconfig($recordPid));
+                                       $rteTcaTypeValue = BackendUtility::getTCAtypeValue($table, $row);
+                                       $rteSetupConfiguration = BackendUtility::RTEsetup($rteSetup['properties'], $table, $field, $rteTcaTypeValue);
+                                       if (!$rteSetupConfiguration['disabled']) {
                                                if (!$this->formEngine->disableRTE) {
                                                        $this->formEngine->RTEcounter++;
                                                        // Find alternative relative path for RTE images/links:
-                                                       $eFile = RteHtmlParser::evalWriteFile($specConf['static_write'], $row);
-                                                       $RTErelPath = is_array($eFile) ? dirname($eFile['relEditFile']) : '';
+                                                       $evalWriteFile = RteHtmlParser::evalWriteFile($specialConfiguration['static_write'], $row);
+                                                       $rteRelativePath = is_array($evalWriteFile) ? dirname($evalWriteFile['relEditFile']) : '';
                                                        // Get RTE object, draw form and set flag:
-                                                       $RTEobj = BackendUtility::RTEgetObj();
-                                                       $item = $RTEobj->drawRTE($this->formEngine, $table, $field, $row, $additionalInformation, $specConf, $thisConfig, $RTEtypeVal, $RTErelPath, $thePidValue);
-                                                       // Wizard:
-                                                       $item = $this->formEngine->renderWizards(array($item, $altItem), $config['wizards'], $table, $row, $field, $additionalInformation, $additionalInformation['itemFormElName'], $specConf, 1);
-                                                       $RTEwasLoaded = 1;
+                                                       $rteObject = BackendUtility::RTEgetObj();
+                                                       $item = $rteObject->drawRTE(
+                                                               $this->formEngine,
+                                                               $table,
+                                                               $field,
+                                                               $row,
+                                                               $additionalInformation,
+                                                               $specialConfiguration,
+                                                               $rteSetupConfiguration,
+                                                               $rteTcaTypeValue,
+                                                               $rteRelativePath,
+                                                               $tsConfigPid
+                                                       );
+
+                                                       // Wizard
+                                                       $item = $this->formEngine->renderWizards(
+                                                               array($item, $altItem),
+                                                               $config['wizards'],
+                                                               $table,
+                                                               $row,
+                                                               $field,
+                                                               $additionalInformation,
+                                                               $additionalInformation['itemFormElName'],
+                                                               $specialConfiguration,
+                                                               TRUE
+                                                       );
+                                                       $rteWasLoaded = TRUE;
                                                } else {
-                                                       $RTEwouldHaveBeenLoaded = 1;
+                                                       $rteWouldHaveBeenLoaded = TRUE;
                                                        $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': RTE is disabled by the on-page RTE-flag (probably you can enable it by the check-box in the bottom of this page!)';
                                                }
                                        } else {
@@ -100,30 +131,30 @@ class TextElement extends AbstractFormElement {
                                        $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': PID value could NOT be fetched. Rare error, normally with new records.';
                                }
                        } else {
-                               if (!isset($specConf['richtext'])) {
+                               if (!isset($specialConfiguration['richtext'])) {
                                        $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': RTE was not configured for this field in TCA-types';
                                }
-                               if (!(!$p['flag'] || !$row[$p['flag']])) {
+                               if (!(!$parameters['flag'] || !$row[$parameters['flag']])) {
                                        $this->formEngine->commentMessages[] = $additionalInformation['itemFormElName'] . ': Field-flag (' . $additionalInformation['flag'] . ') has been set to disable RTE!';
                                }
                        }
                }
                // Display ordinary field if RTE was not loaded.
-               if (!$RTEwasLoaded) {
+               if (!$rteWasLoaded) {
                        // Show message, if no RTE (field can only be edited with RTE!)
-                       if ($specConf['rte_only']) {
+                       if ($specialConfiguration['rte_only']) {
                                $item = '<p><em>' . htmlspecialchars($this->formEngine->getLL('l_noRTEfound')) . '</em></p>';
                        } else {
-                               if ($specConf['nowrap']) {
+                               if ($specialConfiguration['nowrap']) {
                                        $wrap = 'off';
                                } else {
                                        $wrap = $config['wrap'] ?: 'virtual';
                                }
                                $classes = array();
-                               if ($specConf['fixed-font']) {
+                               if ($specialConfiguration['fixed-font']) {
                                        $classes[] = 'fixed-font';
                                }
-                               if ($specConf['enable-tab']) {
+                               if ($specialConfiguration['enable-tab']) {
                                        $classes[] = 'enable-tab';
                                }
                                $formWidthText = $this->formWidthText($cols, $wrap);
@@ -133,40 +164,47 @@ class TextElement extends AbstractFormElement {
                                        $formWidthText = str_replace(' class="' . $res[1] . '"', '', $formWidthText);
                                        $classes = array_merge($classes, explode(' ', $res[1]));
                                }
-                               if (count($classes)) {
+
+                               if (!empty($classes)) {
                                        $class = ' class="tceforms-textarea ' . implode(' ', $classes) . '"';
                                } else {
-                                       $class = 'tceforms-textarea';
+                                       $class = ' class="tceforms-textarea"';
                                }
-                               $evalList = GeneralUtility::trimExplode(',', $config['eval'], TRUE);
+
                                foreach ($evalList as $func) {
-                                       switch ($func) {
-                                               case 'required':
-                                                       $this->formEngine->registerRequiredProperty('field', $table . '_' . $row['uid'] . '_' . $field, $additionalInformation['itemFormElName']);
-                                                       break;
-                                               default:
-                                                       // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
-                                                       // and \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_text_Eval()
-                                                       $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func] . ':&' . $func);
-                                                       if (is_object($evalObj) && method_exists($evalObj, 'deevaluateFieldValue')) {
-                                                               $_params = array(
-                                                                       'value' => $additionalInformation['itemFormElValue']
-                                                               );
-                                                               $additionalInformation['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
-                                                       }
+                                       if ($func === 'required') {
+                                               $this->formEngine->registerRequiredProperty('field', $table . '_' . $row['uid'] . '_' . $field, $additionalInformation['itemFormElName']);
+                                       } else {
+                                               // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
+                                               // and \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_text_Eval()
+                                               $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func] . ':&' . $func);
+                                               if (is_object($evalObj) && method_exists($evalObj, 'deevaluateFieldValue')) {
+                                                       $_params = array(
+                                                               'value' => $additionalInformation['itemFormElValue']
+                                                       );
+                                                       $additionalInformation['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
+                                               }
                                        }
                                }
-                               $iOnChange = implode('', $additionalInformation['fieldChangeFunc']);
+                               $textOnChange = implode('', $additionalInformation['fieldChangeFunc']);
                                $item .= '
                                                        <textarea ' . 'id="' . uniqid('tceforms-textarea-') . '" ' . 'name="' . $additionalInformation['itemFormElName']
                                        . '"' . $formWidthText . $class . ' ' . 'rows="' . $rows . '" ' . 'wrap="' . $wrap . '" ' . 'onchange="'
-                                       . htmlspecialchars($iOnChange) . '"' . $this->formEngine->getPlaceholderAttribute($table, $field, $config, $row)
+                                       . htmlspecialchars($textOnChange) . '"' . $this->formEngine->getPlaceholderAttribute($table, $field, $config, $row)
                                        . $additionalInformation['onFocus'] . '>' . GeneralUtility::formatForTextarea($additionalInformation['itemFormElValue']) . '</textarea>';
-                               $item = $this->formEngine->renderWizards(array($item, $altItem), $config['wizards'], $table, $row, $field, $additionalInformation,
-                                       $additionalInformation['itemFormElName'], $specConf, $RTEwouldHaveBeenLoaded);
+                               $item = $this->formEngine->renderWizards(
+                                       array($item, $altItem),
+                                       $config['wizards'],
+                                       $table,
+                                       $row,
+                                       $field,
+                                       $additionalInformation,
+                                       $additionalInformation['itemFormElName'],
+                                       $specialConfiguration,
+                                       $rteWouldHaveBeenLoaded
+                               );
                        }
                }
-               // Return field HTML:
                return $item;
        }