Total Complexity | 41 |
Total Lines | 421 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Bootstrap3Renderer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Bootstrap3Renderer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Bootstrap3Renderer implements RendererInterface |
||
22 | { |
||
23 | public function render(\Xoops\Form\Element $element):string |
||
24 | { |
||
25 | $methodName = 'render' . str_replace('\\' , '', get_class($element)); |
||
26 | if (true === method_exists($this, $methodName)) { |
||
27 | return $this->$methodName($element); |
||
28 | } |
||
29 | return $element->defaultRender(); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Example of an override render method for a specific element class. |
||
34 | * Each method will be specific to a concrete implementation of Xoops\Form\Element |
||
35 | * |
||
36 | * @param \Xoops\Form\Button $element Provides access to the element we are rendering. |
||
37 | * The strong type makes sure we get what we expect. |
||
38 | * |
||
39 | * @return string the rendering of $element |
||
40 | * |
||
41 | protected function renderXoopsFormButton(\Xoops\Form\Button $element):string |
||
42 | { |
||
43 | // do the rendering and return a string |
||
44 | return ''; |
||
45 | }*/ |
||
46 | |||
47 | /** |
||
48 | * Render support for XoopsFormButton |
||
49 | * |
||
50 | * @param XoopsFormButton $element form element |
||
|
|||
51 | * |
||
52 | * @return string rendered form element |
||
53 | */ |
||
54 | protected function renderXoopsFormButton(\Xoops\Form\Button $element):string |
||
55 | { |
||
56 | if (false == $element->hasClassLike('btn')) { |
||
57 | $element->add('class', 'btn btn-default'); |
||
58 | } |
||
59 | $attributes = $element->renderAttributeString(); |
||
60 | return '<input ' . $attributes . $element->getExtra() .' >'; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Render support for XoopsFormButtonTray |
||
65 | * |
||
66 | * @param XoopsFormButtonTray $element form element |
||
67 | * |
||
68 | * @return string rendered form element |
||
69 | */ |
||
70 | protected function renderXoopsFormButtonTray(\Xoops\Form\ButtonTray $element):string |
||
71 | { |
||
72 | $ret = ''; |
||
73 | $element->add('class', 'btn'); |
||
74 | $class = 'class="' . $element->getClass() . '"'; |
||
75 | |||
76 | $attributes = $element->renderAttributeString(); |
||
77 | |||
78 | if ((bool) $element->get(':showdelete', false)) { |
||
79 | $ret .= '<input type="submit"' . $class . ' name="delete" id="delete" value="' |
||
80 | . \XoopsLocale::A_DELETE . '" onclick="this.form.elements.op.value=\'delete\'">'; |
||
81 | } |
||
82 | $ret .= ' <input type="button" ' . $class . ' value="' . \XoopsLocale::A_CANCEL |
||
83 | . '" onclick="history.go(-1);return true;" />' |
||
84 | . ' <input type="reset"' . $class . ' name="reset" id="reset" value="' . \XoopsLocale::A_RESET . '" />' |
||
85 | . ' <input ' . $attributes . $element->getExtra() . ' />'; |
||
86 | return $ret; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Render support for XoopsFormColorPicker |
||
91 | * |
||
92 | * @param XoopsFormColorPicker $element form element |
||
93 | * |
||
94 | * @return string rendered form element |
||
95 | */ |
||
96 | protected function renderXoopsFormColorPicker(\Xoops\Form\ColorPicker $element):string |
||
97 | { |
||
98 | $xoops = \Xoops::getInstance(); |
||
99 | if ($xoops->theme()) { |
||
100 | $xoops->theme()->addScript('include/color-picker.js'); |
||
101 | } else { |
||
102 | echo '<script type="text/javascript" src="' . $xoops->url('/include/color-picker.js') . '"></script>'; |
||
103 | } |
||
104 | $temp = $element->get('value', ''); |
||
105 | if (!empty($temp)) { |
||
106 | $element->set('style', 'background-color:' . $temp . ';'); |
||
107 | } |
||
108 | $element->set('class', 'form-control'); |
||
109 | $ret = '<div class="input-group">'; |
||
110 | $attributes = $element->renderAttributeString(); |
||
111 | $ret .= '<input ' . $attributes . ' ' . $element->getExtra() .' >'; |
||
112 | $ret .= '<span class="input-group-btn">'; |
||
113 | $ret .= '<button class="btn btn-default" type="button" '; |
||
114 | $ret .= 'data-toggle="tooltip" data-placement="left" title="' . \XoopsLocale::A_SELECT . '" '; |
||
115 | $ret .= 'onclick="return TCP.popup(\''; |
||
116 | $ret .= $xoops->url('/include/') . '\',document.getElementById(\'' . $element->getName() . '\'));">'; |
||
117 | $ret .= '<span class="glyphicon glyphicon-option-horizontal" aria-hidden="true"></span></button>'; |
||
118 | $ret .= '</span></div>'; |
||
119 | |||
120 | return $ret; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Render support for XoopsFormDateSelect |
||
125 | * |
||
126 | * @param XoopsFormDateSelect $element form element |
||
127 | * |
||
128 | * @return string rendered form element |
||
129 | */ |
||
130 | protected function renderXoopsFormDateSelect(\Xoops\Form\DateSelect $element):string |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Render support for XoopsFormDhtmlTextArea |
||
171 | * |
||
172 | * @param XoopsFormDhtmlTextArea $element form element |
||
173 | * |
||
174 | * @return string rendered form element |
||
175 | */ |
||
176 | protected function renderXoopsFormDhtmlTextArea(\Xoops\Form\DhtmlTextArea $element):string |
||
177 | { |
||
178 | if ($element->htmlEditor && is_object($element->htmlEditor)) { |
||
179 | if (!isset($element->htmlEditor->isEnabled) || $element->htmlEditor->isEnabled) { |
||
180 | return $element->htmlEditor->render(); |
||
181 | } |
||
182 | } |
||
183 | static $js_loaded; |
||
184 | |||
185 | $xoops = \Xoops::getInstance(); |
||
186 | |||
187 | $extra = ($element->getExtra() != '' ? " " . $element->getExtra() : ''); |
||
188 | $ret = ""; |
||
189 | // actions |
||
190 | $ret .= $this->XoopsFormDhtmlTextAreaCodeIcon($element) . "<br>\n"; |
||
191 | // fonts |
||
192 | $ret .= $this->XoopsFormDhtmlTextAreaTypography($element); |
||
193 | $ret .= "<br>\n"; |
||
194 | // the textarea box |
||
195 | $element->add('class', 'form-control'); |
||
196 | $element->suppressRender(['value']); |
||
197 | $attributes = $element->renderAttributeString(); |
||
198 | |||
199 | $ret .= '<textarea ' . $attributes . $extra . '>' . $element->getValue() . "</textarea>\n"; |
||
200 | |||
201 | if (empty($element->skipPreview)) { |
||
202 | if (!$xoops->theme()) { |
||
203 | $element->js .= implode("", file($xoops->path('media/xoops/image.js'))); |
||
204 | } else { |
||
205 | $xoops->theme()->addScript('media/xoops/image.js', array('type' => 'text/javascript')); |
||
206 | } |
||
207 | $button = "<input id='" . $element->getName() . "_preview_button' " . "type='button' " . "class='btn btn-sm btn-default' value='" . \XoopsLocale::A_PREVIEW . "' " . "onclick=\"form_instantPreview('" . XOOPS_URL . "', '" . $element->getName() . "','" . XOOPS_URL . "/images', " . (int)($element->doHtml) . ", '" . $xoops->security()->createToken() . "')\"" . " />"; |
||
208 | $ret .= "<br />" . "<div id='" . $element->getName() . "_hidden' style='display: block;'> " . "<fieldset>" . "<legend>" . $button . "</legend>" . "<div id='" . $element->getName() . "_hidden_data'>" . \XoopsLocale::CLICK_PREVIEW_TO_SEE_CONTENT . "</div>" . "</fieldset>" . "</div>"; |
||
209 | } |
||
210 | // Load javascript |
||
211 | if (empty($js_loaded)) { |
||
212 | $javascript = (($element->js) |
||
213 | ? '<script type="text/javascript">' . $element->js . '</script>' |
||
214 | : '') . '<script type="text/javascript" src="' . \XoopsBaseConfig::get('url') . '/include/formdhtmltextarea.js"></script>'; |
||
215 | $ret = $javascript . $ret; |
||
216 | $js_loaded = true; |
||
217 | } |
||
218 | return $ret; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Render xoopscode buttons for editor, include calling text sanitizer extensions |
||
223 | * |
||
224 | * @param XoopsFormDhtmlTextArea $element form element |
||
225 | * |
||
226 | * @return string rendered buttons for xoopscode assistance |
||
227 | */ |
||
228 | |||
229 | public function XoopsFormDhtmlTextAreaCodeIcon($element) |
||
230 | { |
||
231 | $textarea_id = $element->getName(); |
||
232 | $xoops = \Xoops::getInstance(); |
||
233 | $myts = \Xoops\Core\Text\Sanitizer::getInstance(); |
||
234 | |||
235 | $code = ''; |
||
236 | $code .= "<div class='row'><div class='col-md-12'>"; |
||
237 | $code .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsCodeUrl(\"{$textarea_id}\", \"" . $myts->escapeForJavascript(\XoopsLocale::ENTER_LINK_URL) . "\", \"" . $myts->escapeForJavascript(\XoopsLocale::ENTER_WEBSITE_TITLE) . "\");' onmouseover='style.cursor=\"hand\"' title='" . \XoopsLocale::URL . "'><span class='fa fa-fw fa-link' aria-hidden='true'></span></button>"; |
||
238 | $code .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsCodeEmail(\"{$textarea_id}\", \"" . $myts->escapeForJavascript(\XoopsLocale::ENTER_EMAIL) . "\");' onmouseover='style.cursor=\"hand\"' title='" . \XoopsLocale::EMAIL . "'><span class='fa fa-fw fa-envelope-o' aria-hidden='true'></span></button>"; |
||
239 | $code .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsCodeImg(\"{$textarea_id}\", \"" . $myts->escapeForJavascript(\XoopsLocale::ENTER_IMAGE_URL) . "\", \"" . $myts->escapeForJavascript(\XoopsLocale::ENTER_IMAGE_POSITION) . "\", \"" . $myts->escapeForJavascript(\XoopsLocale::IMAGE_POSITION_DESCRIPTION) . "\", \"" . $myts->escapeForJavascript(\XoopsLocale::E_ENTER_IMAGE_POSITION) . "\", \"" . $myts->escapeForJavascript(\XoopsLocale::WIDTH) . "\");' onmouseover='style.cursor=\"hand\"' title='" . \XoopsLocale::IMAGES . "'><span class='fa fa-fw fa-file-image-o' aria-hidden='true'></span></button>"; |
||
240 | |||
241 | $response = \Xoops::getInstance()->service('emoji')->renderEmojiSelector($element->getName()); |
||
242 | if ($response->isSuccess()) { |
||
243 | $emojiSelector = $response->getValue(); |
||
244 | $code .= $emojiSelector; |
||
245 | } |
||
246 | |||
247 | $extensions = array_filter($myts->listExtensions()); |
||
248 | foreach ($extensions as $extension) { |
||
249 | list ($button, $js) = $myts->getDhtmlEditorSupport($extension, $textarea_id); |
||
250 | if (!empty($button)) { |
||
251 | $code .= $button; |
||
252 | } |
||
253 | if (!empty($js)) { |
||
254 | $element->js .= $js; |
||
255 | } |
||
256 | } |
||
257 | |||
258 | $code .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsCodeCode(\"{$textarea_id}\", \"" . $myts->escapeForJavascript(\XoopsLocale::ENTER_CODE) . "\");' onmouseover='style.cursor=\"hand\"' title='" . \XoopsLocale::SOURCE_CODE . "'><span class='fa fa-fw fa-code' aria-hidden='true'></span></button>"; |
||
259 | $code .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsCodeQuote(\"{$textarea_id}\", \"" . $myts->escapeForJavascript(\XoopsLocale::ENTER_QUOTE) . "\");' onmouseover='style.cursor=\"hand\"' title='" . \XoopsLocale::QUOTE . "'><span class='fa fa-fw fa-quote-right' aria-hidden='true'></span></button>"; |
||
260 | $code .= "</div></div>"; |
||
261 | |||
262 | return $code; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Render typography controls for editor (font, size, color) |
||
267 | * |
||
268 | * @param XoopsFormDhtmlTextArea $element form element |
||
269 | * |
||
270 | * @return string rendered typography controls |
||
271 | */ |
||
272 | protected function XoopsFormDhtmlTextAreaTypography($element) |
||
273 | { |
||
274 | $textarea_id = $element->getName(); |
||
275 | $hiddentext = $element->hiddenText; |
||
276 | |||
277 | $fontarray = !empty($GLOBALS['formtextdhtml_fonts']) ? $GLOBALS['formtextdhtml_fonts'] : array( |
||
278 | 'Arial', |
||
279 | 'Courier', |
||
280 | 'Georgia', |
||
281 | 'Helvetica', |
||
282 | 'Impact', |
||
283 | 'Verdana', |
||
284 | 'Haettenschweiler'); |
||
285 | |||
286 | $colorArray = array( |
||
287 | 'Black' => '000000', |
||
288 | 'Blue' => '38AAFF', |
||
289 | 'Brown' => '987857', |
||
290 | 'Green' => '79D271', |
||
291 | 'Grey' => '888888', |
||
292 | 'Orange' => 'FFA700', |
||
293 | 'Paper' => 'E0E0E0', |
||
294 | 'Purple' => '363E98', |
||
295 | 'Red' => 'FF211E', |
||
296 | 'White' => 'FEFEFE', |
||
297 | 'Yellow' => 'FFD628', |
||
298 | ); |
||
299 | |||
300 | $fontStr = '<div class="row"><div class="col-md-12"><div class="btn-group" role="toolbar">'; |
||
301 | $fontStr .= '<div class="btn-group">' |
||
302 | . '<button type="button" class="btn btn-default btn-sm dropdown-toggle" title="'. \XoopsLocale::SIZE .'"' |
||
303 | . ' data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' |
||
304 | . '<span class = "glyphicon glyphicon-text-height"></span><span class="caret"></span></button>' |
||
305 | . '<ul class="dropdown-menu">'; |
||
306 | foreach (\XoopsLocale::getFontSizes() as $value => $name) { |
||
307 | $fontStr .= '<li><a href="javascript:xoopsSetElementAttribute(\'size\', \'' . $value . '\', \'' |
||
308 | . $textarea_id . '\', \'' . $hiddentext . '\');">' . $name . '</a></li>'; |
||
309 | } |
||
310 | $fontStr .= '</ul></div>'; |
||
311 | |||
312 | $fontStr .= '<div class="btn-group">' |
||
313 | . '<button type="button" class="btn btn-default btn-sm dropdown-toggle" title="'. \XoopsLocale::FONT .'"' |
||
314 | . ' data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' |
||
315 | . '<span class = "glyphicon glyphicon-font"></span><span class="caret"></span></button>' |
||
316 | . '<ul class="dropdown-menu">'; |
||
317 | foreach ($fontarray as $font) { |
||
318 | $fontStr .= '<li><a href="javascript:xoopsSetElementAttribute(\'font\', \'' . $font . '\', \'' |
||
319 | . $textarea_id . '\', \'' . $hiddentext . '\');">' . $font . '</a></li>'; |
||
320 | } |
||
321 | $fontStr .= '</ul></div>'; |
||
322 | |||
323 | $fontStr .= '<div class="btn-group">' |
||
324 | . '<button type="button" class="btn btn-default btn-sm dropdown-toggle" title="'. \XoopsLocale::COLOR .'"' |
||
325 | . ' data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' |
||
326 | . '<span class = "glyphicon glyphicon-text-color"></span><span class="caret"></span></button>' |
||
327 | . '<ul class="dropdown-menu">'; |
||
328 | foreach ($colorArray as $color => $hex) { |
||
329 | $fontStr .= '<li><a href="javascript:xoopsSetElementAttribute(\'color\', \'' . $hex . '\', \'' |
||
330 | . $textarea_id . '\', \'' . $hiddentext . '\');">' |
||
331 | . '<span style="color:#' . $hex . ';">' . $color .'</span></a></li>'; |
||
332 | } |
||
333 | $fontStr .= '</ul></div>'; |
||
334 | $fontStr .= '</div>'; |
||
335 | |||
336 | $styleStr = "<div class='btn-group' role='group'>"; |
||
337 | $styleStr .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsMakeBold(\"{$hiddentext}\", \"{$textarea_id}\");' title='" . \XoopsLocale::BOLD . "' aria-label='Left Align'><span class='fa fa-bold' aria-hidden='true'></span></button>"; |
||
338 | $styleStr .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsMakeItalic(\"{$hiddentext}\", \"{$textarea_id}\");' title='" . \XoopsLocale::ITALIC . "' aria-label='Left Align'><span class='fa fa-italic' aria-hidden='true'></span></button>"; |
||
339 | $styleStr .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsMakeUnderline(\"{$hiddentext}\", \"{$textarea_id}\");' title='" . \XoopsLocale::UNDERLINE . "' aria-label='Left Align'>" . '<span class="fa fa-underline"></span></button>'; |
||
340 | $styleStr .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsMakeLineThrough(\"{$hiddentext}\", \"{$textarea_id}\");' title='" . \XoopsLocale::LINE_THROUGH . "' aria-label='Left Align'>" . '<span class="fa fa-strikethrough"></span></button>'; |
||
341 | $styleStr .= "</div>"; |
||
342 | |||
343 | $alignStr = "<div class='btn-group' role='group'>"; |
||
344 | $alignStr .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsMakeLeft(\"{$hiddentext}\", \"{$textarea_id}\");' title='" . \XoopsLocale::LEFT . "' aria-label='Left Align'><span class='fa fa-align-left' aria-hidden='true'></span></button>"; |
||
345 | $alignStr .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsMakeCenter(\"{$hiddentext}\", \"{$textarea_id}\");' title='" . \XoopsLocale::CENTER . "' aria-label='Left Align'><span class='fa fa-align-center' aria-hidden='true'></span></button>"; |
||
346 | $alignStr .= "<button type='button' class='btn btn-default btn-sm' onclick='xoopsMakeRight(\"{$hiddentext}\", \"{$textarea_id}\");' title='" . \XoopsLocale::RIGHT . "' aria-label='Left Align'><span class='fa fa-align-right' aria-hidden='true'></span></button>"; |
||
347 | $alignStr .= "</div>"; |
||
348 | |||
349 | $fontStr .= " {$styleStr} {$alignStr} \n"; |
||
350 | |||
351 | $fontStr .= "<button type='button' class='btn btn-default btn-sm' onclick=\"XoopsCheckLength('" |
||
352 | . $element->getName() . "', '" . @$element->configs['maxlength'] . "', '" |
||
353 | . \XoopsLocale::F_CURRENT_TEXT_LENGTH . "', '" . \XoopsLocale::MAXIMUM_LENGTH . "');\" title='" |
||
354 | . \XoopsLocale::CHECK_TEXT_LENGTH . "'><span class='fa fa-check-square-o' aria-hidden='true'></span></button>"; |
||
355 | $fontStr .= "</div></div>"; |
||
356 | |||
357 | return $fontStr; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Render support for XoopsFormPassword |
||
362 | * |
||
363 | * @param XoopsFormPassword $element form element |
||
364 | * |
||
365 | * @return string rendered form element |
||
366 | */ |
||
367 | protected function renderXoopsFormPassword(\Xoops\Form\Password $element):string |
||
368 | { |
||
369 | $element->add('class', 'form-control'); |
||
370 | $attributes = $element->renderAttributeString(); |
||
371 | return '<input ' . $attributes . $element->getExtra() .' >'; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Render support for XoopsFormSelect |
||
376 | * |
||
377 | * @param XoopsFormSelect $element form element |
||
378 | * |
||
379 | * @return string rendered form element |
||
380 | */ |
||
381 | protected function renderXoopsFormSelect(\Xoops\Form\Select $element):string |
||
382 | { |
||
383 | $selected = (array) $element->getValue(); |
||
384 | |||
385 | $ele_options = $element->getOptions(); |
||
386 | |||
387 | $extra = ($element->getExtra() != '' ? " " . $element->getExtra() : ''); |
||
388 | $element->add('class', 'form-control'); |
||
389 | $attributes = $element->renderAttributeString(); |
||
390 | $ret = '<select ' . $attributes . $extra .' >' . "\n"; |
||
391 | |||
392 | if (empty($ele_optgroup)) { |
||
393 | foreach ($ele_options as $value => $display) { |
||
394 | if (is_array($display)) { |
||
395 | $ret .= '<optgroup label="' . $value . '">' . "\n"; |
||
396 | foreach ($display as $optvalue => $optdisplay) { |
||
397 | $ret .= $element->renderOption($optvalue, $optdisplay, $selected); |
||
398 | } |
||
399 | } else { |
||
400 | $ret .= $element->renderOption($value, $display, $selected); |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | $ret .= '</select>' . "\n"; |
||
405 | |||
406 | return $ret; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Render support for XoopsFormText |
||
411 | * |
||
412 | * @param XoopsFormText $element form element |
||
413 | * |
||
414 | * @return string rendered form element |
||
415 | */ |
||
416 | protected function renderXoopsFormText(\Xoops\Form\Text $element):string |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Render support for XoopsFormTextArea |
||
430 | * |
||
431 | * @param XoopsFormTextArea $element form element |
||
432 | * |
||
433 | * @return string rendered form element |
||
434 | */ |
||
435 | protected function renderXoopsFormTextArea(\Xoops\Form\TextArea $element):string |
||
442 | } |
||
443 | } |
||
444 |