Completed
Pull Request — master (#588)
by
unknown
14:32
created

Bootstrap3Renderer::renderXoopsFormDhtmlTextArea()   B

Complexity

Conditions 10
Paths 37

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 33
nc 37
nop 1
dl 0
loc 49
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 * This program is distributed in the hope that it will be useful,
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
 */
10
namespace Xoops\Form\Renderer;
11
12
use Xoops\Form\RendererInterface;
13
/**
14
 * Bootstrap3Renderer style form renderer
15
 *
16
 * @author    Grégory Mage
17
 * @copyright 2019 XOOPS Project (http://xoops.org)
18
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @link      http://xoops.org
20
 */
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
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormButton was not found. Did you mean XoopsFormButton? If so, make sure to prefix the type with \.
Loading history...
51
     *
52
     * @return string rendered form element
53
     */
54
	protected function renderXoopsFormButton(\Xoops\Form\Button $element):string
55
    {
56
		if (false == $element->hasClassLike('btn')) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $element->hasClassLike('btn') of type false|integer against false; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
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
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormButtonTray was not found. Did you mean XoopsFormButtonTray? If so, make sure to prefix the type with \.
Loading history...
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
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormColorPicker was not found. Did you mean XoopsFormColorPicker? If so, make sure to prefix the type with \.
Loading history...
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
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormDateSelect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
127
     *
128
     * @return string rendered form element
129
     */
130
	protected function renderXoopsFormDateSelect(\Xoops\Form\DateSelect $element):string
131
    {
132
        $xoops = \Xoops::getInstance();
133
134
        $display_value = \Xoops\Core\Locale\Time::formatDate($element->getValue(false));
135
136
        $dataList = $element->isDatalist();
137
        if (!empty($dataList)) {
138
            $element->add('list', 'list_' . $element->getName());
139
        }
140
141
		$element->add('class', 'form-control');
142
        $element->suppressRender(['value']);
143
        $attributes = $element->renderAttributeString();
144
145
        $xoops->theme()->addBaseStylesheetAssets('@jqueryuicss');
0 ignored issues
show
Bug introduced by
'@jqueryuicss' of type string is incompatible with the type array expected by parameter $assets of Xoops\Core\Theme\XoopsTh...dBaseStylesheetAssets(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
        $xoops->theme()->addBaseStylesheetAssets(/** @scrutinizer ignore-type */ '@jqueryuicss');
Loading history...
146
        $xoops->theme()->addBaseScriptAssets('@jqueryui');
0 ignored issues
show
Bug introduced by
'@jqueryui' of type string is incompatible with the type array expected by parameter $assets of Xoops\Core\Theme\XoopsTheme::addBaseScriptAssets(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

146
        $xoops->theme()->addBaseScriptAssets(/** @scrutinizer ignore-type */ '@jqueryui');
Loading history...
147
        \Xoops\Core\Locale\Time::localizeDatePicker();
148
149
        $xoops->theme()->addScript(
150
            '',
151
            '',
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type array expected by parameter $attributes of Xoops\Core\Theme\XoopsTheme::addScript(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
            /** @scrutinizer ignore-type */ '',
Loading history...
152
            ' $(function() { $( "#' . $element->get('id') . '" ).datepicker({' .
0 ignored issues
show
Bug introduced by
Are you sure $element->get('id') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
            ' $(function() { $( "#' . /** @scrutinizer ignore-type */ $element->get('id') . '" ).datepicker({' .
Loading history...
153
            'showOn: "focus", changeYear: true, constrainInput: false ' .
154
            ' }); }); '
155
        );
156
157
        $ret = '<div class="input-group">';
158
        $ret .= '<input ' . $attributes . ' value="' . $display_value . '" ' . $element->getExtra() .' >';
159
        $ret .= '<span class="input-group-btn">';
160
        $ret .= '<button class="btn btn-default" type="button" ';
161
        $ret .= 'data-toggle="tooltip" data-placement="left" title="' . \XoopsLocale::A_SELECT . '" ';
162
        $ret .= 'onclick="$( \'#' . $element->get('id') . '\' ).datepicker( \'show\' );"> ';
163
        $ret .= '<span class="glyphicon glyphicon-calendar" aria-hidden="true"></span></button>';
164
        $ret .= '</span></div>';
165
166
        return $ret;
167
	}
168
169
	/**
170
     * Render support for XoopsFormDhtmlTextArea
171
     *
172
     * @param XoopsFormDhtmlTextArea $element form element
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormDhtmlTextArea was not found. Did you mean XoopsFormDhtmlTextArea? If so, make sure to prefix the type with \.
Loading history...
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 .= $element->codeIcon() . "<br />\n";
191
        // fonts
192
        $ret .= $element->fontArray();
193
        // length checker
194
        $ret .= '<button type="button" class="btn btn-xs btn-default" onclick="XoopsCheckLength(\''
195
            . $element->getName() . '\', \'' . @$element->configs['maxlength'] . '\', \''
196
            . \XoopsLocale::F_CURRENT_TEXT_LENGTH . '\', \'' . \XoopsLocale::MAXIMUM_LENGTH . '\');"'
197
            . ' title="' . \XoopsLocale::CHECK_TEXT_LENGTH . '">'
198
            . '<span class="glyphicon glyphicon-check"></span></button>';
199
        $ret .= "\n";
200
        // the textarea box
201
		$element->add('class', 'form-control');
202
        $element->suppressRender(['value']);
203
        $attributes = $element->renderAttributeString();
204
205
        $ret .= '<textarea ' . $attributes . $extra . '>' . $element->getValue() . "</textarea>\n";
206
207
        if (empty($element->skipPreview)) {
208
            if (!$xoops->theme()) {
209
                $element->js .= implode("", file($xoops->path('media/xoops/image.js')));
0 ignored issues
show
Bug introduced by
It seems like file($xoops->path('media/xoops/image.js')) can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
                $element->js .= implode("", /** @scrutinizer ignore-type */ file($xoops->path('media/xoops/image.js')));
Loading history...
210
            } else {
211
                $xoops->theme()->addScript('media/xoops/image.js', array('type' => 'text/javascript'));
212
            }
213
            $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() . "')\"" . " />";
214
            $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>";
215
        }
216
        // Load javascript
217
        if (empty($js_loaded)) {
218
            $javascript = (($element->js)
219
                ? '<script type="text/javascript">' . $element->js . '</script>'
220
                : '') . '<script type="text/javascript" src="' . \XoopsBaseConfig::get('url') . '/include/formdhtmltextarea.js"></script>';
221
            $ret = $javascript . $ret;
222
            $js_loaded = true;
223
        }
224
        return $ret;
225
	}
226
227
	/**
228
     * Render support for XoopsFormPassword
229
     *
230
     * @param XoopsFormPassword $element form element
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormPassword was not found. Did you mean XoopsFormPassword? If so, make sure to prefix the type with \.
Loading history...
231
     *
232
     * @return string rendered form element
233
     */
234
	protected function renderXoopsFormPassword(\Xoops\Form\Password $element):string
235
    {
236
        $element->add('class', 'form-control');
237
		$attributes = $element->renderAttributeString();
238
        return '<input ' . $attributes . $element->getExtra() .' >';
239
	}
240
	
241
	/**
242
     * Render support for XoopsFormSelect
243
     *
244
     * @param XoopsFormSelect $element form element
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormSelect was not found. Did you mean XoopsFormSelect? If so, make sure to prefix the type with \.
Loading history...
245
     *
246
     * @return string rendered form element
247
     */
248
	protected function renderXoopsFormSelect(\Xoops\Form\Select $element):string
249
    {
250
        $selected = (array) $element->getValue();
251
252
        $ele_options = $element->getOptions();
253
254
        $extra = ($element->getExtra() != '' ? " " . $element->getExtra() : '');
255
        $element->add('class', 'form-control');
256
        $attributes = $element->renderAttributeString();
257
        $ret = '<select ' . $attributes . $extra .' >' . "\n";
258
259
        if (empty($ele_optgroup)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ele_optgroup does not exist. Did you maybe mean $ele_options?
Loading history...
260
            foreach ($ele_options as $value => $display) {
261
                if (is_array($display)) {
262
                    $ret .= '<optgroup label="' . $value . '">' . "\n";
263
                    foreach ($display as $optvalue => $optdisplay) {
264
                        $ret .= $element->renderOption($optvalue, $optdisplay, $selected);
265
                    }
266
                } else {
267
                    $ret .= $element->renderOption($value, $display, $selected);
268
                }
269
            }
270
        }
271
        $ret .= '</select>' . "\n";
272
273
        return $ret;
274
	}
275
	
276
	/**
277
     * Render support for XoopsFormText
278
     *
279
     * @param XoopsFormText $element form element
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormText was not found. Did you mean XoopsFormText? If so, make sure to prefix the type with \.
Loading history...
280
     *
281
     * @return string rendered form element
282
     */
283
	protected function renderXoopsFormText(\Xoops\Form\Text $element):string
284
    {
285
        $element->add('class', 'form-control');
286
        $dataList = $element->isDatalist();
287
        if (!empty($dataList)) {
288
            $element->add('list', 'list_' . $element->getName());
289
        }
290
291
        $attributes = $element->renderAttributeString();
292
        return '<input ' . $attributes . ' ' . $element->getExtra() .' >';
293
	}
294
	
295
	/**
296
     * Render support for XoopsFormTextArea
297
     *
298
     * @param XoopsFormTextArea $element form element
0 ignored issues
show
Bug introduced by
The type Xoops\Form\Renderer\XoopsFormTextArea was not found. Did you mean XoopsFormTextArea? If so, make sure to prefix the type with \.
Loading history...
299
     *
300
     * @return string rendered form element
301
     */
302
	protected function renderXoopsFormTextArea(\Xoops\Form\TextArea $element):string
303
    {
304
        $element->suppressRender(['value']);
305
		$element->add('class', 'form-control');
306
        $attributes = $element->renderAttributeString();
307
        return '<textarea ' . $attributes . ' ' . $element->getExtra() .' >'
308
            . $element->getValue() . '</textarea>';
309
	}
310
}
311