TinyMCEJQ::init()   F
last analyzed

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 135
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 82
c 1
b 0
f 0
nc 24672
nop 0
dl 0
loc 135
rs 0

How to fix   Long Method    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
/**
13
 *  TinyMCE jQuery adapter for XOOPS
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @package         class
18
 * @subpackage      editor
19
 * @since           2.3.0
20
 * @author          Taiwen Jiang <[email protected]>
21
 * @author          Lucio Rota <[email protected]>
22
 * @author          Laurent JEN <[email protected]>
23
 * @version         $Id $
24
 */
25
class TinyMCEJQ
26
{
27
    public $rootpath;
28
    public $config  = [];
29
    public $setting = [];
30
31
    // PHP 5 Constructor
32
    public function __construct($config)
33
    {
34
        $this->setConfig($config);
35
        $this->rootpath     = $this->config['rootpath'] . '/tiny_mce';
36
        $this->xoopsPlugins = $this->get_xoopsPlugins();
0 ignored issues
show
Bug Best Practice introduced by
The property xoopsPlugins does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
    }
38
39
    // PHP 4 Contructor
40
    public function TinyMCEJQ($config)
41
    {
42
        $this->__construct($config);
43
    }
44
45
    public function &instance($config)
46
    {
47
        static $instance;
48
        if (!isset($instance)) {
49
            $instance = new TinyMCEJQ($config);
50
        } else {
51
            $instance->setConfig($config);
52
        }
53
        return $instance;
54
    }
55
56
    public function setConfig($config)
57
    {
58
        foreach ($config as $key => $val) {
59
            $this->config[$key] = $val;
60
        }
61
    }
62
63
    public function init()
64
    {
65
        // list of configured options
66
        $configured = [];
67
68
        // Load default settings
69
        if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) {
70
            $this->setting = include dirname(__FILE__) . '/settings.php';
71
        }
72
73
        // get editor language (from ...)
74
        if (is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) {
75
            $this->setting['language'] = $this->config['language'];
76
            $configured[]              = 'language';
77
        }
78
79
        $this->setting['content_css'] = implode(',', $this->loadCss());
80
        $configured[]                 = 'content_css';
81
82
        if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) {
83
            $this->setting['theme'] = $this->config['theme'];
84
            $configured[]           = 'theme';
85
        }
86
87
        if (!empty($this->config['mode'])) {
88
            $this->setting['mode'] = $this->config['mode'];
89
            $configured[]          = 'mode';
90
        }
91
92
        // load all plugins except the plugins in setting["exclude_plugins"]
93
        $this->setting['plugins'] = implode(',', $this->loadPlugins());
94
        $configured[]             = 'plugins';
95
96
        if ('simple' != $this->setting['theme']) {
97
            if (empty($this->config['buttons'])) {
98
                $this->config['buttons'][] = [
99
                    'before' => '',
100
                    'add'    => '',
101
                ];
102
                $this->config['buttons'][] = [
103
                    'before' => '',
104
                    'add'    => '',
105
                ];
106
                $this->config['buttons'][] = [
107
                    'before' => '',
108
                    'add'    => '',
109
                ];
110
            }
111
            $i = 0;
112
            foreach ($this->config['buttons'] as $button) {
113
                $i++;
114
                if (isset($button['before'])) {
115
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before'];
116
                }
117
                if (isset($button['add'])) {
118
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add'];
119
                }
120
                if (isset($button[''])) {
121
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button[''];
122
                }
123
            }
124
            $configured[] = 'buttons';
125
126
            if (isset($this->config['toolbar_location'])) {
127
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location'];
128
                $configured[]                                                            = 'toolbar_location';
129
            } else {
130
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top';
131
            }
132
133
            if (isset($this->config['toolbar_align'])) {
134
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align'];
135
                $configured[]                                                         = 'toolbar_align';
136
            } else {
137
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left';
138
            }
139
140
            if (isset($this->config['statusbar_location'])) {
141
                $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location'];
142
                $configured[]                                                              = 'statusbar_location';
143
            }
144
145
            if (isset($this->config['path_location'])) {
146
                $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location'];
147
                $configured[]                                                         = 'path_location';
148
            }
149
150
            if (isset($this->config['resize_horizontal'])) {
151
                $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal'];
152
                $configured[]                                                             = 'resize_horizontal';
153
            }
154
155
            if (isset($this->config['resizing'])) {
156
                $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing'];
157
                $configured[]                                                    = 'resizing';
158
            }
159
160
            if (!empty($this->config['fonts'])) {
161
                $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts'];
162
                $configured[]                                                 = 'fonts';
163
            }
164
165
            for ($i = 1; $i <= 4; $i++) {
166
                $buttons = [];
167
                if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) {
168
                    $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]);
169
                    foreach ($checklist as $plugin) {
170
                        if (false != strpos(strtolower($plugin), 'xoops')) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos(strtolower($plugin), 'xoops') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
171
                            if (in_array($plugin, $this->xoopsPlugins)) {
172
                                $buttons[] = $plugin;
173
                            }
174
                        } else {
175
                            $buttons[] = $plugin;
176
                        }
177
                    }
178
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons);
179
                }
180
            }
181
        }
182
183
        $configured = array_unique($configured);
184
        foreach ($this->config as $key => $val) {
185
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
186
                continue;
187
            }
188
            $this->setting[$key] = $val;
189
        }
190
191
        if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) {
192
            $this->setting['docs_language'] = 'en';
193
        }
194
195
        unset($this->config, $configured);
196
197
        return true;
198
    }
199
200
    // load all plugins execpt the plugins in setting["exclude_plugins"]
201
    public function loadPlugins()
202
    {
203
        $plugins      = [];
204
        $plugins_list = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
205
        if (empty($this->setting['plugins'])) {
206
            $plugins = $plugins_list;
207
        } else {
208
            $plugins = array_intersect(explode(',', $this->setting['plugins']), $plugins_list);
209
        }
210
        if (!empty($this->setting['exclude_plugins'])) {
211
            $plugins = array_diff($plugins, explode(',', $this->setting['exclude_plugins']));
212
        }
213
        if (!empty($this->config['plugins'])) {
214
            $plugins = array_merge($plugins, $this->config['plugins']);
215
        }
216
        return $plugins;
217
    }
218
219
    // return all xoops plugins
220
    public function get_xoopsPlugins()
221
    {
222
        $xoopsPlugins = [];
223
        $allplugins   = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
224
        foreach ($allplugins as $plugin) {
225
            if (false != strpos(strtolower($plugin), 'xoops') && file_exists(XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php")) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos(strtolower($plugin), 'xoops') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
226
                if ($right = @include XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php") {
227
                    $xoopsPlugins[$plugin] = $plugin;
228
                }
229
            }
230
        }
231
        return $xoopsPlugins;
232
    }
233
234
    public function loadCss($css_file = 'style.css')
235
    {
236
        static $css_url, $css_path;
237
238
        if (!isset($css_url)) {
239
            $css_url  = dirname(xoops_getcss($GLOBALS['xoopsConfig']['theme_set']));
240
            $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
241
        }
242
243
        $css         = [];
244
        $css[]       = $css_url . '/' . $css_file;
245
        $css_content = file_get_contents($css_path . '/' . $css_file);
246
247
        // get all import css files
248
        if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) {
249
            foreach ($matches[1] as $key => $css_import) {
250
                $css = array_merge($css, $this->loadCss($css_import));
251
            }
252
        }
253
        return $css;
254
    }
255
256
    public function render()
257
    {
258
        static $isTinyMceJsLoaded = false;
259
260
        $this->init();
261
262
        if (!empty($this->setting['callback'])) {
263
            $callback = $this->setting['callback'];
264
            unset($this->setting['callback']);
265
        } else {
266
            $callback = '';
267
        }
268
269
        // create returned string - start
270
        $ret = "\n";
271
272
        /*
273
        // IE BUG - start
274
        // more info here:
275
        // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
276
        // possible solution here:
277
        // http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
278
                $ret =<<<EOF
279
        <script language='javascript' type='text/javascript'>
280
            if (/msie/i.test (navigator.userAgent)) //only override IE
281
                {
282
                document.nativeGetElementById = document.getElementById;
283
                document.getElementById = function(id) {
284
                    var elem = document.nativeGetElementById(id);
285
                    if(elem) {
286
                        //make sure that it is a valid match on id
287
                        if(elem.attributes['id'].value == id)    {
288
                            return elem;
289
                        } else {
290
                            //otherwise find the correct element
291
                            for(var i=1;i<document.all[id].length;i++) {
292
                                if(document.all[id][i].attributes['id'].value == id) {
293
                                    return document.all[id][i];
294
                                }
295
                            }
296
                        }
297
                    }
298
                    return null;
299
                };
300
            }
301
        </script>
302
        \n
303
        EOF;
304
        // IE BUG - end
305
        */
306
307
        $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug
308
        $ret .= "<script language='javascript' type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/jquery.tinymce.js'></script>\n";
309
        if ($isTinyMceJsLoaded) {
310
            $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug
311
        } else {
312
            $ret               .= "<script language='javascript' type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/tiny_mce.js'></script>\n";
313
            $isTinyMceJsLoaded = true;
314
        }
315
        $ret .= "<script language='javascript' type='text/javascript'>\n";
316
        $ret .= "tinyMCE.init({\n";
317
        // set options - start
318
        foreach ($this->setting as $key => $val) {
319
            $ret .= $key . ':';
320
            if (true === $val) {
321
                $ret .= 'true,';
322
            } elseif (false === $val) {
323
                $ret .= 'false,';
324
            } else {
325
                $ret .= "'{$val}',";
326
            }
327
            $ret .= "\n";
328
        }
329
        // set options - end
330
        $ret .= "tinymceload: true\n";
331
        $ret .= "});\n";
332
        $ret .= $callback . "\n";
333
        //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n";
334
        $ret .= "</script>\n";
335
        $ret .= "<!-- End TinyMce Rendering -->\n";//debug
336
        // create returned string - end
337
        return $ret;
338
    }
339
}
340
341
342