Completed
Pull Request — master (#563)
by Richard
08:33
created

TinyMCE::init()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 139
Code Lines 89

Duplication

Lines 99
Ratio 71.22 %

Code Coverage

Tests 0
CRAP Score 812

Importance

Changes 0
Metric Value
cc 28
eloc 89
nc 24672
nop 0
dl 99
loc 139
rs 2
c 0
b 0
f 0
ccs 0
cts 115
cp 0
crap 812

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 adapter for XOOPS
14
 *
15
 * @copyright       XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
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
26
class TinyMCE
27
{
28
    var $rootpath;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $rootpath.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
29
    var $config = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
30
    var $setting = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $setting.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
31
    var $element = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $element.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
32
33
    // PHP 5 Constructor
34
    function __construct($config)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
     {
36
        $this->setConfig($config);
37
        $this->rootpath = $this->config["rootpath"] . "/tiny_mce";
38
        $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...
39
        $this->element = $config['elements'];
40
    }
41
42
    // PHP 4 Contructor
43
    function TinyMCE($config)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
44
    {
45
        $this->__construct($config) ;
46
    }
47
48
    function &instance( $config )
49
    {
50
        static $instance;
51
        if (!isset($instance)) {
52
            $instance = new TinyMCE($config);
53
        } else {
54
            $instance->setConfig($config);
55
        }
56
        return $instance;
57
    }
58
59
    function setConfig( $config )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
60
    {
61
        foreach ($config as $key => $val) {
62
            $this->config[$key] = $val;
63
        }
64
    }
65
66
    function init()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
    {
68
        $xoops = Xoops::getInstance();
69
        // list of configured options
70
        $configured = array();
71
72
		$xoops_root_path = \XoopsBaseConfig::get('root-path');
73
		
74
        // Load default settings
75
        if ( ! ($this->setting = @include( $xoops->path( "var/configs/tinymce.php" ) ) ) ) {
76
            $this->setting = include __DIR__ . "/settings.php";
77
        }
78
79
        // get editor language (from ...)
80
        if (is_readable($xoops_root_path . $this->rootpath . '/langs/' . $this->config["language"] . '.js')) {
81
            $this->setting["language"] = $this->config["language"];
82
            $configured[] = "language";
83
        }
84
85
        $this->setting["content_css"] = implode( ",", $this->loadCss()) . ',' . $xoops->url('xoops.css');
86
        $configured[] = "content_css";
87
88
        if ( !empty($this->config["theme"]) && is_dir($xoops_root_path . $this->rootpath . "/themes/" . $this->config["theme"]) ) {
89
            $this->setting["theme"] = $this->config["theme"];
90
            $configured[] = "theme";
91
        }
92
93
        if (!empty($this->config["mode"])) {
94
            $this->setting["mode"] = $this->config["mode"];
95
            $configured[] = "mode";
96
        }
97
98
        // load all plugins except the plugins in setting["exclude_plugins"]
99
        $this->setting["plugins"] = implode(",", $this->loadPlugins());
100
        $configured[] = "plugins";
101
102
        if ( $this->setting["theme"] !== "simple" ) {
103
            if (empty($this->config["buttons"])) {
104
                $this->config["buttons"][] = array(
105
                    "before"    => "",
106
                    "add"       => "",
107
                    );
108
                $this->config["buttons"][] = array(
109
                    "before"    => "",
110
                    "add"       => "",
111
                    );
112
                $this->config["buttons"][] = array(
113
                    "before"    => "",
114
                    "add"       => "",
115
                    );
116
            }
117
            $i = 0;
118
            foreach ($this->config["buttons"] as $button) {
119
                $i++;
120
                if (isset($button["before"])) {
121
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add_before"] = $button["before"];
122
                }
123
                if (isset($button["add"])) {
124
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add"] = $button["add"];
125
                }
126
                if (isset($button[""])) {
127
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = $button[""];
128
                }
129
            }
130
            $configured[] = "buttons";
131
132
            if (isset($this->config["toolbar_location"])) {
133
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = $this->config["toolbar_location"];
134
                $configured[] = "toolbar_location";
135
            } else {
136
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = "top";
137
            }
138
139
            if (isset($this->config["toolbar_align"])) {
140
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = $this->config["toolbar_align"];
141
                $configured[] = "toolbar_align";
142
            } else {
143
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = "left";
144
            }
145
146
            if (isset($this->config["statusbar_location"])) {
147
                $this->setting["theme_" . $this->setting["theme"] . "_statusbar_location"] = $this->config["statusbar_location"];
148
                $configured[] = "statusbar_location";
149
            }
150
151
            if (isset($this->config["path_location"])) {
152
                $this->setting["theme_" . $this->setting["theme"] . "_path_location"] = $this->config["path_location"];
153
                $configured[] = "path_location";
154
            }
155
156
            if (isset($this->config["resize_horizontal"])) {
157
                $this->setting["theme_" . $this->setting["theme"] . "_resize_horizontal"] = $this->config["resize_horizontal"];
158
                $configured[] = "resize_horizontal";
159
            }
160
161
            if (isset($this->config["resizing"])) {
162
                $this->setting["theme_" . $this->setting["theme"] . "_resizing"] = $this->config["resizing"];
163
                $configured[] = "resizing";
164
            }
165
166
            if (!empty($this->config["fonts"])) {
167
                $this->setting["theme_" . $this->setting["theme"] . "_fonts"] = $this->config["fonts"];
168
                $configured[] = "fonts";
169
            }
170
171
            for ($i=1 ; $i <= 4 ; $i++ ) {
172
                $buttons = array();
173
                if ( isset($this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"]) ) {
174
                    $checklist = explode(",", $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] );
175
                    foreach ( $checklist as $plugin ) {
176
                        if ( strpos( strtolower($plugin), "xoops") !== false ) {
177
                            if ( in_array( $plugin, $this->xoopsPlugins ) ) {
178
                                $buttons[] = $plugin;
179
                            }
180
                        } else {
181
                            $buttons[] = $plugin;
182
                        }
183
                    }
184
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = implode(",", $buttons);
185
                }
186
            }
187
        }
188
189
        $configured = array_unique($configured);
190
        foreach ($this->config as $key => $val) {
191
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
192
                continue;
193
            }
194
            $this->setting[$key] = $val;
195
        }
196
197
        if (!is_dir($xoops_root_path . $this->rootpath . "/themes/" . $this->setting["theme"] . '/docs/' . $this->setting["language"] . '/')) {
198
            $this->setting["docs_language"] = "en";
199
        }
200
201
        unset($this->config, $configured);
202
203
        return true;
204
    }
205
206
    // load all plugins execpt the plugins in setting["exclude_plugins"]
207
    function loadPlugins()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
208
    {
209
		$xoops_root_path = \XoopsBaseConfig::get('root-path');
210
        $plugins = array();
211
        $plugins_list = XoopsLists::getDirListAsArray( $xoops_root_path . $this->rootpath . "/plugins" );
212
        if (empty($this->setting["plugins"])) {
213
            $plugins = $plugins_list;
214
        } else {
215
            $plugins = array_intersect(explode(",", $this->setting["plugins"]), $plugins_list);
216
        }
217
        if (!empty($this->setting["exclude_plugins"])) {
218
            $plugins = array_diff($plugins, explode(",", $this->setting["exclude_plugins"]));
219
        }
220
        if (!empty($this->config["plugins"])) {
221
            $plugins = array_merge($plugins, $this->config["plugins"]);
222
        }
223
        return $plugins;
224
    }
225
226
    // return all xoops plugins
227
    function get_xoopsPlugins() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
228
		$xoops_root_path = \XoopsBaseConfig::get('root-path');
229
        $xoopsPlugins = array();
230
        $allplugins = XoopsLists::getDirListAsArray( $xoops_root_path . $this->rootpath . "/plugins" );
231
        foreach ( $allplugins as $plugin ) {
232
            if ( strpos( strtolower($plugin), "xoops") !== false && file_exists($xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php") ) {
233
                if ( $right = @include $xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php" ) {
234
                    $xoopsPlugins[$plugin] = $plugin;
235
                }
236
            }
237
        }
238
        return $xoopsPlugins;
239
    }
240
241
    function loadCss($css_file = 'style.css')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
242
    {
243
        static $css_url, $css_path;
244
245
        if (!isset($css_url)) {
246
            $xoops = Xoops::getInstance();
247
            $css_url = dirname( $xoops->getCss($xoops->getConfig('theme_set')) );
248
            $css_path = str_replace(\XoopsBaseConfig::get('themes-url'), \XoopsBaseConfig::get('themes-path'), $css_url);
249
        }
250
251
        $css = array();
252
        $css[] = $css_url . '/' . $css_file;
253
        $css_content = file_get_contents( $css_path . '/' . $css_file );
254
255
        // get all import css files
256
        if ( preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER) ) {
257
            foreach( $matches[1] as $key => $css_import ) {
258
                $css = array_merge( $css, $this->loadCss( $css_import) );
259
            }
260
        }
261
        return $css;
262
    }
263
264
    function render()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
265
    {
266
        static $isTinyMceJsLoaded = false;
267
		
268
		$xoops_url = \XoopsBaseConfig::get('url');
269
270
        $this->init();
271
272
        if ( !empty($this->setting["callback"]) ) {
273
            $callback = $this->setting["callback"];
274
            unset($this->setting["callback"]);
275
        } else {
276
            $callback = "";
277
        }
278
279
        // create returned string - start
280
        $ret = "\n";
281
        $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug
282
        if ($isTinyMceJsLoaded) {
283
            $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug
284
        } else {
285
            $ret .= "<script language='javascript' type='text/javascript' src='" . $xoops_url . $this->rootpath . "/jquery.tinymce.js'></script>\n";
286
            $isTinyMceJsLoaded = true;
287
        }
288
        $ret .= "<script language='javascript' type='text/javascript'>\n";
289
        $ret .= "   $().ready(function() {\n";
290
        $ret .= "$('textarea." . $this->element . "').tinymce({\n";
291
            // Location of TinyMCE script
292
        $ret .= "script_url : '" . $xoops_url . $this->rootpath . "/tiny_mce.js',\n";
293
294
        // set options - start
295
        foreach ($this->setting as $key => $val) {
296
            $ret .= $key . ":";
297
            if ($val === true) {
298
                $ret.= "true,";
299
            } elseif ($val === false) {
300
                $ret .= "false,";
301
            } else {
302
                $ret .= "'{$val}',";
303
            }
304
            $ret .= "\n";
305
        }
306
307
        // set options - end
308
        $ret .= "tinymceload: true\n";
309
        $ret .= "});\n";
310
        $ret .= "});\n";
311
        $ret .= $callback . "\n";
312
        //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n";
313
        $ret .= "</script>\n";
314
        $ret .= "<!-- End TinyMce Rendering -->\n";//debug
315
        // create returned string - end
316
        return $ret ;
317
    }
318
}
319