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

DhtmlTextArea::defaultRender()   B

Complexity

Conditions 10
Paths 37

Size

Total Lines 49
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 10.0907

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 37
nop 0
dl 0
loc 49
ccs 28
cts 31
cp 0.9032
crap 10.0907
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
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Form;
13
14
/**
15
 * DhtmlTextArea - A textarea with xoopsish formatting and smilie buttons
16
 *
17
 * @category  Xoops\Form\DhtmlTextArea
18
 * @package   Xoops\Form
19
 * @author    Kazumi Ono <[email protected]>
20
 * @author    Taiwen Jiang <[email protected]>
21
 * @author    Vinod <[email protected]>
22
 * @copyright 2001-2016 XOOPS Project (http://xoops.org)
23
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @link      http://xoops.org
25
 */
26
class DhtmlTextArea extends \XoopsEditor
27
{
28
    /**
29
     * Extended HTML editor
30
     *
31
     * <p>If an extended HTML editor is set, the renderer will be replaced by the specified editor,
32
     * usually a visual or WYSIWYG editor.</p>
33
     *
34
     * <ul>Developer and user guide:
35
     *      <li>
36
     *          <ul>For run-time settings per call
37
     *              <li>To use an editor pre-configured by {@link XoopsEditor}, e.g. 'fckeditor': <code>$options['editor'] = 'fckeditor';</code></li>
38
     *              <li>To use a custom editor, e.g. 'MyEditor' class located in "/modules/myeditor/myeditor.php": <code>$options['editor'] = array('MyEditor', XOOPS_ROOT_PATH . "/modules/myeditor/myeditor.php");</code></li>
39
     *          </ul>
40
     *      </li>
41
     *      <li>
42
     *          <ul>For pre-configured settings, which will force to use a editor if no specific editor is set for call
43
     *              <li>
44
     *                  <ul>Set up custom configs: in XOOPS_VAR_PATH . '/configs/xoopsconfig.php' set a editor as default, e.g.
45
     *                      <li>a pre-configured editor 'fckeditor': <code>return array('editor' => 'fckeditor');</code></li>
46
     *                      <li>a custom editor 'MyEditor' class located in "/modules/myeditor/myeditor.php": <code>return array('editor' => array('MyEditor', XOOPS_ROOT_PATH . "/modules/myeditor/myeditor.php");</code></li>
47
     *                  </ul>
48
     *              </li>
49
     *              <li>To disable the default editor, in XOOPS_VAR_PATH . '/configs/xoopsconfig.php': <code>return array();</code></li>
50
     *              <li>To disable the default editor for a specific call: <code>$options['editor'] = 'dhtmltextarea';</code></li>
51
     *          </ul>
52
     *      </li>
53
     * </ul>
54
     */
55
    /**
56
     * @var \XoopsEditor
57
     */
58
    public $htmlEditor;
59
60
    /**
61
     * Hidden text
62
     *
63
     * @var string
64
     */
65
    private $hiddenText;
66
67
    /**
68
     * @var bool
69
     */
70
    public $skipPreview = false;
71
72
    /**
73
     * @var bool
74
     */
75
    public $doHtml = false;
76
77
    /**
78
     * @var string
79
     */
80
    public $js = '';
81
82
    /**
83
     * @var array
84
     */
85
    public $configs = array();
86
87
    /**
88
     * Constructor
89
     *
90
     * @param string  $caption    Caption
91
     * @param string  $name       name attribute
92
     * @param string  $value      Initial text
93
     * @param integer $rows       Number of rows
94
     * @param integer $cols       Number of columns
95
     * @param string  $hiddentext Identifier for hidden Text
96
     * @param array   $options    Extra options
97
     */
98 6
    public function __construct(
99
        $caption,
100
        $name,
101
        $value = "",
102
        $rows = 5,
103
        $cols = 50,
104
        $hiddentext = "xoopsHiddenText",
105
        $options = array()
106
    ) {
107 6
        static $inLoop = 0;
108
109 6
        ++$inLoop;
110
        // Second loop, invalid, return directly
111 6
        if ($inLoop > 2) {
112
            return;
113
        }
114
115
        // Else, initialize
116 6
        parent::__construct($caption, $name, $value, $rows, $cols);
117 6
        $this->hiddenText = $hiddentext;
118
119 6
        if ($inLoop > 1) {
120
            return;
121
        }
122
123 6
        $xoops = \Xoops::getInstance();
124 6
        if (!isset($options['editor'])) {
125 6
            if ($editor = $xoops->getConfig('editor')) {
126
                $options['editor'] = $editor;
127
            }
128
        }
129
130 6
        if (!empty($this->htmlEditor) || !empty($options['editor'])) {
131
            $options['name'] = $this->getName();
132
            $options['value'] = $this->getValue();
133
            if (!empty($options['editor'])) {
134
                $this->htmlEditor = is_array($options['editor']) ? $options['editor'] : array($options['editor']);
0 ignored issues
show
Documentation Bug introduced by
It seems like is_array($options['edito...ray($options['editor']) of type array or array is incompatible with the declared type XoopsEditor of property $htmlEditor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
135
            }
136
137
            if (count($this->htmlEditor) == 1) {
138
                $editor_handler = \XoopsEditorHandler::getInstance();
139
                $this->htmlEditor = $editor_handler->get($this->htmlEditor[0], $options);
140
                if ($inLoop > 1) {
141
                    $this->htmlEditor = null;
142
                }
143
            } else {
144
                list ($class, $path) = $this->htmlEditor;
145
                include_once \XoopsBaseConfig::get('root-path') . $path;
146
                if (class_exists($class)) {
147
                    $this->htmlEditor = new $class($options);
148
                }
149
                if ($inLoop > 1) {
150
                    $this->htmlEditor = null;
151
                }
152
            }
153
        }
154
155 6
        $inLoop = 0;
156 6
    }
157
158
    /**
159
     * defaultRender
160
     *
161
     * @return string rendered form element
162
     */
163 1
    public function defaultRender()
164
    {
165 1
        if ($this->htmlEditor && is_object($this->htmlEditor)) {
166
            if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
167
                return $this->htmlEditor->render();
168
            }
169
        }
170 1
        static $js_loaded;
171
172 1
        $xoops = \Xoops::getInstance();
173
174 1
        $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
175 1
        $ret = "";
176
        // actions
177 1
        $ret .= $this->codeIcon() . "<br />\n";
178
        // fonts
179 1
        $ret .= $this->fontArray();
180
        // length checker
181
        $ret .= '<button type="button" onclick="XoopsCheckLength(\''
182 1
            . $this->getName() . '\', \'' . @$this->configs['maxlength'] . '\', \''
183 1
            . \XoopsLocale::F_CURRENT_TEXT_LENGTH . '\', \'' . \XoopsLocale::MAXIMUM_LENGTH . '\');"'
184 1
            . ' title="' . \XoopsLocale::CHECK_TEXT_LENGTH . '">'
185 1
            . '<span>' . \XoopsLocale::CHECK_TEXT_LENGTH . '</span></button>';
186 1
        $ret .= "\n";
187
        // the textarea box
188
189 1
        $this->suppressRender(['value']);
190 1
        $attributes = $this->renderAttributeString();
191 1
192
        $ret .= '<textarea ' . $attributes . $extra . '>' . $this->getValue() . "</textarea>\n";
193 1
194
        if (empty($this->skipPreview)) {
195 1
            if (!$xoops->theme()) {
196 1
                $this->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

196
                $this->js .= implode("", /** @scrutinizer ignore-type */ file($xoops->path('media/xoops/image.js')));
Loading history...
197
            } else {
198
                $xoops->theme()->addScript('media/xoops/image.js', array('type' => 'text/javascript'));
199 1
            }
200
            $button = "<input id='" . $this->getName() . "_preview_button' " . "type='button' " . " value='" . \XoopsLocale::A_PREVIEW . "' " . "onclick=\"form_instantPreview('" . XOOPS_URL . "', '" . $this->getName() . "','" . XOOPS_URL . "/images', " . (int)($this->doHtml) . ", '" . $xoops->security()->createToken() . "')\"" . " />";
201 1
            $ret .= "<br />" . "<div id='" . $this->getName() . "_hidden' style='display: block;'> " . "<fieldset>" . "<legend>" . $button . "</legend>" . "<div id='" . $this->getName() . "_hidden_data'>" . \XoopsLocale::CLICK_PREVIEW_TO_SEE_CONTENT . "</div>" . "</fieldset>" . "</div>";
202 1
        }
203
        // Load javascript
204
        if (empty($js_loaded)) {
205 1
            $javascript = (($this->js)
206 1
                ? '<script type="text/javascript">' . $this->js . '</script>'
207 1
                : '') . '<script type="text/javascript" src="' . \XoopsBaseConfig::get('url') . '/include/formdhtmltextarea.js"></script>';
208 1
            $ret = $javascript . $ret;
209 1
            $js_loaded = true;
210 1
        }
211
        return $ret;
212 1
    }
213
214
    /**
215
     * codeIcon
216
     *
217
     * @return string
218
     */
219
    public function codeIcon()
220 2
    {
221
        $textarea_id = $this->getName();
222 2
        $xoops = \Xoops::getInstance();
223 2
        $myts = \Xoops\Core\Text\Sanitizer::getInstance();
224 2
225
        $code = '';
226 2
        $code .= '<img src="' . $xoops->url('images/form/url.gif') . '" alt="' . \XoopsLocale::URL
227 2
            . '" title="' . \XoopsLocale::URL . '" onclick="xoopsCodeUrl(\'' . $textarea_id . '\', \''
228 2
            . $myts->escapeForJavascript(\XoopsLocale::ENTER_LINK_URL) . '\', \''
229 2
            . $myts->escapeForJavascript(\XoopsLocale::ENTER_WEBSITE_TITLE)
230 2
            . '\')" onmouseover="style.cursor=\'hand\'" />&nbsp;';
231 2
        $code .= '<img src="' . $xoops->url('images/form/email.gif') . '" alt="' . \XoopsLocale::EMAIL
232 2
            . '" title="' . \XoopsLocale::EMAIL . '" onclick="xoopsCodeEmail(\'' . $textarea_id . '\', \''
233 2
            . $myts->escapeForJavascript(\XoopsLocale::ENTER_EMAIL)
234 2
            . '\');"  onmouseover="style.cursor=\'hand\'" />&nbsp;';
235 2
        $code .= '<img src="' . $xoops->url('images/form/imgsrc.gif') . '" alt="' . \XoopsLocale::IMAGES
236 2
            . '" title="' . \XoopsLocale::IMAGES . '" onclick="xoopsCodeImg(\'' . $textarea_id . '\', \''
237 2
            . $myts->escapeForJavascript(\XoopsLocale::ENTER_IMAGE_URL) . '\', \''
238 2
            . $myts->escapeForJavascript(\XoopsLocale::ENTER_IMAGE_POSITION) . '\', \''
239 2
            . $myts->escapeForJavascript(\XoopsLocale::IMAGE_POSITION_DESCRIPTION) . '\', \''
240 2
            . $myts->escapeForJavascript(\XoopsLocale::E_ENTER_IMAGE_POSITION) . '\', \''
241 2
            . $myts->escapeForJavascript(\XoopsLocale::WIDTH) . '\');" onmouseover="style.cursor=\'hand\'" />&nbsp;';
242 2
243
        $extensions = array_filter($myts->listExtensions());
244 2
        foreach ($extensions as $extension) {
245 2
            list ($button, $js) = $myts->getDhtmlEditorSupport($extension, $textarea_id);
246 2
            if (!empty($button)) {
247 2
                $code .= $button;
248 2
            }
249
            if (!empty($js)) {
250 2
                $this->js .= $js;
251 2
            }
252
        }
253
        $code .= '<img src="' . $xoops->url('images/form/code.gif') .'" alt="' . \XoopsLocale::SOURCE_CODE . '" title="'
254 2
            . \XoopsLocale::SOURCE_CODE . '" onclick="xoopsCodeCode(\'' . $textarea_id . '\', \''
255 2
            . $myts->escapeForJavascript(\XoopsLocale::ENTER_CODE) . '\');" onmouseover="style.cursor=\'hand\'" />&nbsp;';
256 2
257
        $code .= '<img src="' . $xoops->url('images/form/quote.gif') .'" alt="' . \XoopsLocale::QUOTE . '" title="'
258 2
            . \XoopsLocale::QUOTE . '" onclick="xoopsCodeQuote(\'' . $textarea_id . '\', \''
259 2
            . $myts->escapeForJavascript(\XoopsLocale::ENTER_QUOTE) . '\');" onmouseover="style.cursor=\'hand\'" />&nbsp;';
260 2
261
        $response = \Xoops::getInstance()->service('emoji')->renderEmojiSelector($this->getName());
262 2
        if ($response->isSuccess()) {
263 2
            $emojiSelector = $response->getValue();
264
            $code .= $emojiSelector;
265
        }
266
267
        return $code;
268 2
    }
269
270
    /**
271
     * fontArray
272
     *
273
     * @return string
274
     */
275
    public function fontArray()
276 2
    {
277
        $textarea_id = $this->getName();
278 2
        $hiddentext = $this->hiddenText;
279 2
280
        $fontStr = "<script type=\"text/javascript\" language=\"JavaScript\">";
281 2
        $fontStr .= "var _editor_dialog = ''" . "+ '<select class=\"span2\" id=\'{$textarea_id}Size\' onchange=\'xoopsSetElementAttribute(\"size\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
282 2
        $fontStr .= "+ '<option value=\'SIZE\'>" . \XoopsLocale::SIZE . "</option>'";
283 2
        $localeFontSizes = \XoopsLocale::getFontSizes();
284 2
        foreach ($localeFontSizes as $_val => $_name) {
285 2
            $fontStr .= " + '<option value=\'{$_val}\'>{$_name}</option>'";
286 2
        }
287
        $fontStr .= " + '</select> '";
288 2
        $fontStr .= "+ '<select class=\"span2\" id=\'{$textarea_id}Font\' onchange=\'xoopsSetElementAttribute(\"font\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
289 2
        $fontStr .= "+ '<option value=\'FONT\'>" . \XoopsLocale::FONT . "</option>'";
290 2
        $localeFonts = \XoopsLocale::getFonts();
291 2
        $fontarray = !empty($localeFonts) ? $localeFonts :
292 2
            array("Arial", "Courier", "Georgia", "Helvetica", "Impact", "Verdana", "Haettenschweiler");
293 2
        foreach ($fontarray as $font) {
294 2
            $fontStr .= " + '<option value=\'{$font}\'>{$font}</option>'";
295 2
        }
296
        $fontStr .= " + '</select> '";
297 2
        $fontStr .= "+ '<select class=\"span2\" id=\'{$textarea_id}Color\' onchange=\'xoopsSetElementAttribute(\"color\", this.options[this.selectedIndex].value, \"{$textarea_id}\", \"{$hiddentext}\");\'>'";
298 2
        $fontStr .= "+ '<option value=\'COLOR\'>" . \XoopsLocale::COLOR . "</option>';";
299 2
        $fontStr .= "var _color_array = new Array('00', '33', '66', '99', 'CC', 'FF');
300 2
            for(var i = 0; i < _color_array.length; i ++) {
301
                for(var j = 0; j < _color_array.length; j ++) {
302
                    for(var k = 0; k < _color_array.length; k ++) {
303
                        var _color_ele = _color_array[i] + _color_array[j] + _color_array[k];
304
                        _editor_dialog += '<option value=\''+_color_ele+'\' style=\'background-color:#'+_color_ele+';color:#'+_color_ele+';\'>#'+_color_ele+'</option>';
305
                    }
306
                }
307
            }
308
            _editor_dialog += '</select>';";
309
310
        $fontStr .= "document.write(_editor_dialog); </script>";
311 2
312
        $styleStr = "<img src='" . \XoopsBaseConfig::get('url') . "/images/bold.gif' alt='" . \XoopsLocale::BOLD . "' title='" . \XoopsLocale::BOLD . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeBold(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
313 2
        $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/italic.gif' alt='" . \XoopsLocale::ITALIC . "' title='" . \XoopsLocale::ITALIC . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeItalic(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
314 2
        $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/underline.gif' alt='" . \XoopsLocale::UNDERLINE . "' title='" . \XoopsLocale::UNDERLINE . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeUnderline(\"{$hiddentext}\", \"{$textarea_id}\");'/>&nbsp;";
315 2
        $styleStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/linethrough.gif' alt='" . \XoopsLocale::LINE_THROUGH . "' title='" . \XoopsLocale::LINE_THROUGH . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeLineThrough(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
316 2
317
        $alignStr = "<img src='" . \XoopsBaseConfig::get('url') . "/images/alignleft.gif' alt='" . \XoopsLocale::LEFT . "' title='" . \XoopsLocale::LEFT . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeLeft(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
318 2
        $alignStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/aligncenter.gif' alt='" . \XoopsLocale::CENTER . "' title='" . \XoopsLocale::CENTER . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeCenter(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
319 2
        $alignStr .= "<img src='" . \XoopsBaseConfig::get('url') . "/images/alignright.gif' alt='" . \XoopsLocale::RIGHT . "' title='" . \XoopsLocale::RIGHT . "' onmouseover='style.cursor=\"hand\"' onclick='xoopsMakeRight(\"{$hiddentext}\", \"{$textarea_id}\");' />&nbsp;";
320 2
        $fontStr = $fontStr . "<br />\n{$styleStr}&nbsp;{$alignStr}&nbsp;\n";
321 2
        return $fontStr;
322 2
    }
323
324
    /**
325
     * renderValidationJS
326
     *
327
     * @return bool|string
328
     */
329
    public function renderValidationJS()
330 1
    {
331
        if ($this->htmlEditor && is_object($this->htmlEditor) && method_exists($this->htmlEditor, 'renderValidationJS')) {
332 1
            if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
333
                return $this->htmlEditor->renderValidationJS();
334
            }
335
        }
336
        return parent::renderValidationJS();
337 1
    }
338
}
339