Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 35 | class tx_popup_wiz extends \TYPO3\CMS\Backend\Module\BaseScriptClass |
||
| 36 | { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Main function of the module. Write the content to $this->content |
||
| 40 | * |
||
| 41 | */ |
||
| 42 | function main() |
||
| 43 | { |
||
| 44 | global $BE_USER, $LANG, $BACK_PATH; |
||
| 45 | |||
| 46 | // popup Object |
||
| 47 | $this->popup = GeneralUtility::makeInstance('FRUIT\\Popup\\Popup'); |
||
| 48 | |||
| 49 | // Draw the header. |
||
| 50 | $this->doc = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate'); |
||
| 51 | $this->doc->backPath = $BACK_PATH; |
||
| 52 | $this->doc->form = '<form action="" method="post" name="wiz_form" style="margin: 5px;">'; |
||
| 53 | |||
| 54 | // JavaScript |
||
| 55 | $this->doc->JScode = ' |
||
| 56 | <script language="javascript" type="text/javascript"> |
||
| 57 | function setElementValue(elName,elValue) { |
||
| 58 | if (parent.opener && parent.opener.setFormValueFromBrowseWin) { |
||
| 59 | var checkbox = true; |
||
| 60 | if(elValue == "") checkbox = false; |
||
| 61 | |||
| 62 | var selector = \'[data-formengine-input-name="\'+elName+\'"]\'; |
||
| 63 | parent.opener.TYPO3.jQuery(selector).value = elValue; |
||
| 64 | parent.opener.TBE_EDITOR.fieldChanged("pages",' . $_GET['P']['uid'] . ',elName,"data[pages][' . $_GET['P']['uid'] . ']["+elName+"]"); |
||
| 65 | |||
| 66 | // setFormValueFromBrowseWin?? |
||
| 67 | parent.opener.focus(); |
||
| 68 | parent.close(); |
||
| 69 | } else { |
||
| 70 | alert("Error - reference to main window is not set properly!"); |
||
| 71 | parent.opener.focus(); |
||
| 72 | parent.close(); |
||
| 73 | } |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | function removePopup(){ |
||
| 78 | return setElementValue(\'' . $_GET['P']['field'] . '\',""); |
||
| 79 | } |
||
| 80 | |||
| 81 | function sendForm(){ |
||
| 82 | var value = ""; |
||
| 83 | |||
| 84 | value += document.forms["wiz_form"].elements["width"].value; |
||
| 85 | value += "x"; |
||
| 86 | value += document.forms["wiz_form"].elements["height"].value; |
||
| 87 | value += ":"; |
||
| 88 | |||
| 89 | value += "dependent="+document.forms["wiz_form"].elements["dependent"].value; |
||
| 90 | |||
| 91 | value += ","; |
||
| 92 | value += "location="+document.forms["wiz_form"].elements["location"].value; |
||
| 93 | |||
| 94 | value += ","; |
||
| 95 | value += "menubar="+document.forms["wiz_form"].elements["menubar"].value; |
||
| 96 | |||
| 97 | value += ","; |
||
| 98 | value += "resizable="+document.forms["wiz_form"].elements["resizable"].value; |
||
| 99 | |||
| 100 | value += ","; |
||
| 101 | value += "scrollbars="+document.forms["wiz_form"].elements["scrollbars"].value; |
||
| 102 | |||
| 103 | value += ","; |
||
| 104 | value += "status="+document.forms["wiz_form"].elements["status"].value; |
||
| 105 | |||
| 106 | value += ","; |
||
| 107 | value += "toolbar="+document.forms["wiz_form"].elements["toolbar"].value; |
||
| 108 | |||
| 109 | value += ","; |
||
| 110 | value += "left="+document.forms["wiz_form"].elements["left"].value; |
||
| 111 | |||
| 112 | value += ","; |
||
| 113 | value += "top="+document.forms["wiz_form"].elements["top"].value; |
||
| 114 | |||
| 115 | if(document.forms["wiz_form"].elements["once_per_session"] != undefined) { |
||
| 116 | value += ","; |
||
| 117 | value += "once_per_session="+document.forms["wiz_form"].elements["once_per_session"].value; |
||
| 118 | |||
| 119 | value += ","; |
||
| 120 | value += "once_per_link="+document.forms["wiz_form"].elements["once_per_link"].value; |
||
| 121 | |||
| 122 | value += ","; |
||
| 123 | value += "center="+document.forms["wiz_form"].elements["center"].value; |
||
| 124 | |||
| 125 | value += ","; |
||
| 126 | value += "maximize="+document.forms["wiz_form"].elements["maximize"].value; |
||
| 127 | |||
| 128 | value += ","; |
||
| 129 | value += "popunder="+document.forms["wiz_form"].elements["popunder"].value; |
||
| 130 | } |
||
| 131 | |||
| 132 | return setElementValue(\'' . $_GET['P']['field'] . '\',value); |
||
| 133 | } |
||
| 134 | </script>'; |
||
| 135 | |||
| 136 | |||
| 137 | $this->pageinfo = \TYPO3\CMS\Backend\Utility\BackendUtility::readPageAccess($this->id, $this->perms_clause); |
||
| 138 | $access = is_array($this->pageinfo) ? 1 : 0; |
||
| 139 | if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id)) { |
||
| 140 | if ($BE_USER->user['admin'] && !$this->id) { |
||
| 141 | $this->pageinfo = [ |
||
| 142 | 'title' => '[root-level]', |
||
| 143 | 'uid' => 0, |
||
| 144 | 'pid' => 0 |
||
| 145 | ]; |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->content .= $this->doc->startPage($this->getLabel('title')); |
||
| 149 | $this->content .= $this->doc->header($this->getLabel('title')); |
||
| 150 | $this->content .= $this->doc->spacer(5); |
||
| 151 | $this->content .= $this->doc->divider(5); |
||
| 152 | |||
| 153 | // CSS |
||
| 154 | $this->content .= '<style type="text/css"> table td { padding: 2px; width: 100px; } table td input, table td select { width: 90px; } </style>'; |
||
| 155 | |||
| 156 | // Render content: |
||
| 157 | $this->moduleContent(); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | protected function getLabel($key) |
||
| 162 | { |
||
| 163 | /** @var \TYPO3\CMS\Lang\LanguageService $languageService */ |
||
| 164 | $languageService = $GLOBALS['LANG']; |
||
| 165 | $languageService->includeLLFile('EXT:popup/wizard/locallang.xml'); |
||
| 166 | return $languageService->getLL($key); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Output the content |
||
| 171 | * |
||
| 172 | */ |
||
| 173 | function printContent() |
||
| 174 | { |
||
| 175 | $this->content .= $this->doc->endPage(); |
||
| 176 | echo $this->content; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Generate the content |
||
| 181 | * |
||
| 182 | */ |
||
| 183 | function moduleContent($content = '') |
||
| 184 | { |
||
| 185 | |||
| 186 | // Configuration |
||
| 187 | $pageID = MathUtility::canBeInterpretedAsInteger($_GET['P']['uid']) ? $_GET['P']['uid'] : $_GET['P']['pid']; |
||
| 188 | $advanced = (isset($_GET['advanced']) && intval($_GET['advanced']) === 1) ? true : false; |
||
| 189 | $current = (isset($_GET['P']['currentValue']) && trim($_GET['P']['currentValue']) != '') ? $_GET['P']['currentValue'] : $this->popup->convertArray2Cfg($this->popup->getDefaultConfiguration($pageID, |
||
| 190 | $advanced), $advanced); |
||
| 191 | |||
| 192 | $config = $this->popup->convertCfg2Array($current, $advanced); |
||
| 193 | |||
| 194 | // Basic configuration |
||
| 195 | $content .= '<table>'; |
||
| 196 | foreach ($this->popup->allowedParams as $key => $value) { |
||
| 197 | switch ($value) { |
||
| 198 | case 'integer': |
||
| 199 | $content .= '<tr><td>' . $this->getLabel($key) . '</td><td><input type="text" name="' . $key . '" value="' . intval($config[$key] ? $config[$key] : 0) . '" /></td></tr>'; |
||
| 200 | break; |
||
| 201 | case 'boolean': |
||
| 202 | $content .= '<tr><td>' . $this->getLabel($key) . '</td><td><select name="' . $key . '"><option' . ($config[$key] ? ' selected="selected"' : '') . ' value="yes">' . $this->getLabel('yes') . '</option><option' . (!$config[$key] ? ' selected="selected"' : '') . ' value="no">' . $this->getLabel('no') . '</option></select></td></tr>'; |
||
| 203 | break; |
||
| 204 | } # switch |
||
| 205 | } # foreach |
||
| 206 | $this->content .= $this->doc->section($this->getLabel('basic_configuration') . ':', $content . '</table>', 0, 1); |
||
| 207 | |||
| 208 | // Advanced configuration |
||
| 209 | if ($advanced) { |
||
| 210 | $content = '<table>'; |
||
| 211 | |||
| 212 | foreach ($this->popup->advancedParams as $key => $value) { |
||
| 213 | switch ($value) { |
||
| 214 | case 'integer': |
||
| 215 | $content .= '<tr><td>' . $this->getLabel($key) . '</td><td><input type="text" name="' . $key . '" value="' . intval($config[$key] ? $config[$key] : 0) . '" /></td></tr>'; |
||
| 216 | break; |
||
| 217 | case 'boolean': |
||
| 218 | $content .= '<tr><td>' . $this->getLabel($key) . '</td><td><select name="' . $key . '"><option' . ($config[$key] ? ' selected="selected"' : '') . ' value="yes">' . $this->getLabel('yes') . '</option><option' . (!$config[$key] ? ' selected="selected"' : '') . ' value="no">' . $this->getLabel('no') . '</option></select></td></tr>'; |
||
| 219 | break; |
||
| 220 | } # switch |
||
| 221 | } # foreach |
||
| 222 | $this->content .= $this->doc->section($this->getLabel('advanced_configuration') . ':', $content . '</table>', 0, 1); |
||
| 223 | } # if |
||
| 224 | |||
| 225 | // Options |
||
| 226 | $content = '<table><tr><td><input style="background-color: #ff9e9e;" type="button" value="remove Popup" onclick="return removePopup();" /></td><td><input style="background-color: #9eff9e;" type="button" value="set Popup" onclick="return sendForm();" /></td></tr></table>'; |
||
| 227 | $this->content .= $this->doc->section($this->getLabel('options') . ':', $content, 0, 1); |
||
| 228 | } |
||
| 229 | |||
| 230 | } # class - tx_popup_wiz |
||
| 231 | |||
| 237 | $SOBE->printContent(); |