Textarea::render()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 41
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 18
c 0
b 0
f 0
nc 1024
nop 0
dl 0
loc 41
rs 3.1764

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
/**
4
 * Koch Framework
5
 * Jens-André Koch © 2005 - onwards.
6
 *
7
 * This file is part of "Koch Framework".
8
 *
9
 * License: GNU/GPL v2 or any later version, see LICENSE file.
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace Koch\Form\Elements;
26
27
use Koch\Form\FormElement;
28
use Koch\Form\FormElementInterface;
29
use Koch\Functions\Functions;
30
31
/**
32
 * This class renders the formelement textarea.
33
 * It gives you the option to add a JavaScript WYSIWYG editor as textarea replacement.
34
 */
35
class Textarea extends FormElement implements FormElementInterface
36
{
37
    /**
38
     * Flag variable for the What-You-See-Is-What-You-Get Editor.
39
     *
40
     * There are several different wysiwyg editor formelements available:
41
     *
42
     * 1) Nicedit   -> wysiwygnicedit.php
43
     * 2) TinyMCE   -> wysiwygtinymce.php
44
     * 3) CKEditor  -> wysiwygckeditor.php
45
     * 4) markItUp  -> wysiwygmarkItUp.phg
46
     *
47
     * @var string
48
     */
49
    private $editor;
50
51
    /**
52
     * @var int width of textarea in letters
53
     */
54
    public $cols = 0;
55
56
    /**
57
     * @var int height of textarea in rows
58
     */
59
    public $rows = 0;
60
61
    /**
62
     * @var object of the wysiwyg editor
63
     */
64
    private $editorObject;
65
66
    public function __construct()
67
    {
68
        $this->type = 'textarea';
69
70
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
71
    }
72
73
    public function setEditor($editor = null)
74
    {
75
        /*
76
         *  if no editor is given, take the one definied in the general configuration.
77
         *  the expected config setting is [editor] type.
78
         *  if the configuration value is not given, use "ckeditor" as fallback.
79
         */
80
        if ($editor === null) {
81
            $config = Clansuite_CMS::getInjector()->instantiate('Koch\Config');
82
            $editor = isset($config['editor']['type']) ? $config['editor']['type'] : 'ckeditor';
83
            unset($config);
84
        }
85
86
        $this->editor = mb_strtolower($editor);
87
88
        return $this;
89
    }
90
91
    public function getEditor()
92
    {
93
        return $this->editor;
94
    }
95
96
    /**
97
     * defines width of textarea.
98
     *
99
     * @param int $cols
100
     */
101
    public function setCols($cols)
102
    {
103
        $this->cols = $cols;
104
105
        return $this;
106
    }
107
108
    /**
109
     * get defined width of textarea.
110
     */
111
    public function getCols()
112
    {
113
        return $this->cols;
114
    }
115
116
    /**
117
     * define height of textarea in rows.
118
     *
119
     * @param int $rows
120
     */
121
    public function setRows($rows)
122
    {
123
        $this->rows = $rows;
124
125
        return $this;
126
    }
127
128
    /**
129
     * get defined height of textarea in rows.
130
     */
131
    public function getRows()
132
    {
133
        return $this->rows;
134
    }
135
136
    public function setName($name)
137
    {
138
        $this->name = $name;
139
140
        return $this;
141
    }
142
143
    public function setEditorFormelement(FormelementInterface $editorObject)
144
    {
145
        $this->editorObject = $editorObject;
146
147
        return $this;
148
    }
149
150
    public function getEditorFormelement()
151
    {
152
        if (empty($this->editorObject)) {
153
            return $this->setEditorFormelement($this->editorFactory());
154
        } else {
155
            return $this->editorObject;
156
        }
157
    }
158
159
    /**
160
     * editorFactory
161
     * loads and instantiates an wysiwyg editor object.
162
     */
163
    private function editorFactory()
164
    {
165
        // build classname
166
        $name = 'Wysiwyg' . ucfirst($this->getEditor());
167
168
        // attach namespace
169
        $classname = 'Koch\Form\Elements\\' . $name;
170
171
        // load file
172
        if (class_exists($classname, false) === false) {
173
            include __DIR__ . '/' . $name . '.php';
174
        }
175
176
        // instantiate
177
        $editor_formelement = new $classname();
0 ignored issues
show
Coding Style introduced by
$editor_formelement does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
178
179
        return $editor_formelement;
0 ignored issues
show
Coding Style introduced by
$editor_formelement does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
180
    }
181
182
    /**
183
     * At some point in the lifetime of this object
184
     * you decided that this textarea should be a wysiwyg editor.
185
     * The editorFactory will load the file and instantiate the editor object.
186
     * But you already defined some properties like Cols or Rows for this textarea.
187
     * Therefore it's now time to transfer these properties to the editor object.
188
     * Because we don't render this textarea, but the requested wysiwyg editor object.
189
     */
190
    private function transferPropertiesToEditor()
191
    {
192
        // get editor formelement
193
        $formelement = $this->getEditorFormelement();
194
195
        // transfer props from $this to editor formelement
196
        $formelement->setRequired($this->required);
197
        $formelement->setRows($this->rows);
198
        $formelement->setCols($this->cols);
199
        $formelement->setLabel($this->label);
200
        $formelement->setName($this->name);
201
        $formelement->setValue($this->value);
202
203
        // return the editor formelement, to call e.g. render() on it
204
        return $formelement;
205
    }
206
207
    /**
208
     * Renders a normal html textarea representation when
209
     * no wysiwyg editor object is attached to this textarea object.
210
     * Otherwise the html content of the wysiwyg editor is prepended
211
     * to the textarea html.!
212
     *
213
     * @return $html HTML Representation of an textarea
0 ignored issues
show
Documentation introduced by
The doc-type $html could not be parsed: Unknown type name "$html" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
214
     */
215
    public function render()
216
    {
217
        $html = '';
218
219
        /*
220
         * Opening of textarea tag
221
         */
222
        $html .= '<textarea';
223
        $html .= (bool) $this->id ? ' id="' . $this->id . '"' : null;
224
        $html .= (bool) $this->name ? ' name="' . $this->name . '"' : null;
225
        $html .= (bool) $this->size ? ' size="' . $this->size . '"' : null;
226
        $html .= (bool) $this->cols ? ' cols="' . $this->cols . '"' : null;
227
        $html .= (bool) $this->rows ? ' rows="' . $this->rows . '"' : null;
228
        $html .= (bool) $this->class ? ' class="' . $this->class . '"' : null;
229
        $html .= (bool) $this->disabled ? ' disabled="disabled"' : null;
230
        $html .= (bool) $this->maxlength ? ' maxlength="' . $this->maxlength . '"' : null;
231
        $html .= (bool) $this->style ? ' style="' . $this->style . '"' : null;
232
        $html .= '>';
233
234
        /*
235
         * Content between tags (value)
236
         */
237
        $html .= Functions::UTF8_to_HTML($this->getValue());
238
239
        /*
240
         * Closing of textarea tag
241
         */
242
        $html .= '</textarea>';
243
244
        /*
245
         * Attach HTML content of WYSIWYG Editor
246
         *
247
         * Always after the textarea !
248
         * Because html elements are served first, before javascript dom selections are applied upon them!
249
         */
250
        if (empty($this->editor) === false) {
251
            $html .= $this->getEditorFormelement()->transferPropertiesToEditor()->render();
252
        }
253
254
        return $html;
255
    }
256
}
257