Completed
Push — master ( 247290...01d05f )
by Michael
08:30
created

XoopsFormDhtmlTextArea::fontArray()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 30
nc 8
nop 0
dl 0
loc 48
rs 8.7396
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Formatted textarea form
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2017 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @subpackage          form
16
 * @since               2.0.0
17
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
18
 * @author              Taiwen Jiang <[email protected]>
19
 * @author              Vinod <[email protected]>
20
 */
21
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
22
23
/**
24
 * base class
25
 */
26
xoops_load('XoopsFormTextArea');
27
28
/**
29
 *  A textarea with xoopsish formatting and smilie buttons
30
 *
31
 */
32
class XoopsFormDhtmlTextArea extends XoopsFormTextArea
33
{
34
    /**
35
     * Extended HTML editor
36
     *
37
     * <p>If an extended HTML editor is set, the renderer will be replaced by the specified editor, usually a visual or WYSIWYG editor.</p>
38
     *
39
     * <ul>Developer and user guide:
40
     *                         <li><ul>For run-time settings per call
41
     *                                 <li>To use an editor pre-configured by {@link XoopsEditor}, e.g. 'fckeditor': <code>$options['editor'] = 'fckeditor';</code></li>
42
     *                                 <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>
43
     *                             </ul></li>
44
     *                         <li><ul>For pre-configured settings, which will force to use a editor if no specific editor is set for call
45
     *                                 <li><ul>Set up custom configs: in XOOPS_VAR_PATH . '/configs/xoopsconfig.php' set a editor as default, e.g.
46
     *                                         <li>a pre-configured editor 'fckeditor': <code>return array('editor' => 'fckeditor');</code></li>
47
     *                                         <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>
48
     *                                     </ul></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></li>
52
     * </ul>
53
     */
54
    public $htmlEditor = array();
55
56
    /**
57
     * Hidden text
58
     *
59
     * @var string
60
     * @access private
61
     */
62
    public $_hiddenText;
63
64
    public $skipPreview = false;
65
    public $doHtml      = false;
66
    public $js          = '';
67
68
    /**
69
     * Constructor
70
     *
71
     * @param string $caption    Caption
72
     * @param string $name       "name" attribute
73
     * @param string $value      Initial text
74
     * @param int    $rows       Number of rows
75
     * @param int    $cols       Number of columns
76
     * @param string $hiddentext Identifier for hidden Text
77
     * @param array  $options    Extra options
78
     */
79
    public function __construct($caption, $name, $value = '', $rows = 5, $cols = 50, $hiddentext = 'xoopsHiddenText', $options = array())
80
    {
81
        global $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
82
        static $inLoop = 0;
83
84
        ++$inLoop;
85
        // Second loop, invalid, return directly
86
        if ($inLoop > 2) {
87
            return null;
88
        }
89
        // Else, initialize
90
        parent::__construct($caption, $name, $value, $rows, $cols);
91
        $this->_hiddenText = $hiddentext;
92
93
        if ($inLoop > 1) {
94
            return null;
95
        }
96
        if (!isset($options['editor'])) {
97
            if (isset($xoopsConfig['editor'])) {
98
                $options['editor'] = $xoopsConfig['editor'];
99
            }
100
        }
101
102
        if (!empty($this->htmlEditor) || !empty($options['editor'])) {
103
            $options['name']  = $this->getName();
104
            $options['value'] = $this->getValue();
105
            if (!empty($options['editor'])) {
106
                $this->htmlEditor = is_array($options['editor']) ? $options['editor'] : array($options['editor']);
107
            }
108
109
            if (count($this->htmlEditor) == 1) {
110
                xoops_load('XoopsEditorHandler');
111
                $editor_handler   = XoopsEditorHandler::getInstance();
112
                $this->htmlEditor = $editor_handler->get($this->htmlEditor[0], $options);
113
                if ($inLoop > 1) {
114
                    $this->htmlEditor = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array 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...
115
                }
116
            } else {
117
                list($class, $path) = $this->htmlEditor;
118
                include_once XOOPS_ROOT_PATH . $path;
119
                if (class_exists($class)) {
120
                    $this->htmlEditor = new $class($options);
0 ignored issues
show
Documentation Bug introduced by
It seems like new $class($options) of type object is incompatible with the declared type array 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...
121
                }
122
                if ($inLoop > 1) {
123
                    $this->htmlEditor = null;
124
                }
125
            }
126
        }
127
128
        $inLoop = 0;
129
    }
130
131
    /**
132
     * Prepare HTML for output
133
     *
134
     * @return string HTML
135
     */
136
    public function render()
137
    {
138 View Code Duplication
        if ($this->htmlEditor && is_object($this->htmlEditor)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->htmlEditor of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
139
            if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
140
                return $this->htmlEditor->render();
141
            }
142
        }
143
144
        return XoopsFormRenderer::getInstance()->get()->renderFormDhtmlTextArea($this);
145
    }
146
147
    /**
148
     * XoopsFormDhtmlTextArea::renderValidationJS()
149
     *
150
     * @return bool|string
151
     */
152
    public function renderValidationJS()
153
    {
154 View Code Duplication
        if ($this->htmlEditor && is_object($this->htmlEditor) && method_exists($this->htmlEditor, 'renderValidationJS')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->htmlEditor of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
155
            if (!isset($this->htmlEditor->isEnabled) || $this->htmlEditor->isEnabled) {
156
                return $this->htmlEditor->renderValidationJS();
157
            }
158
        }
159
160
        return parent::renderValidationJS();
161
    }
162
}
163