Passed
Branch master (f2d2e3)
by Michael
18:45
created

TinyMCE::init()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 137
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 28
eloc 84
c 6
b 0
f 0
nc 24672
nop 0
dl 0
loc 137
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
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
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
13
/**
14
 *  TinyMCE 4.x adapter for XOOPS
15
 *
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @package         class
19
 * @subpackage      editor
20
 * @since           2.3.0
21
 * @author          Taiwen Jiang <[email protected]>
22
 * @author          Lucio Rota <[email protected]>
23
 * @author          Laurent JEN <[email protected]>
24
 * @version         $Id $
25
 */
26
27
class TinyMCE
28
{
29
    public $rootpath;
30
    public $config  = [];
31
    public $setting = [];
32
33
  /**
34
  * PHP 5 Constructor
35
  *
36
  * @param    string    $config   The configuration
37
  **/
38
39
    public function __construct($config)
40
     {
41
        $this->setConfig($config);
42
        $this->rootpath = $this->config['rootpath'] . '/tinymce/js/tinymce';
43
    }
44
45
  /**
46
  * Creates one instance of the tinyMCE object
47
  *
48
  * @param    array     $config     The configuration
49
  * @return   object    $instance   The instance of tinyMCE object
50
  **/
51
52
    public function &instance($config)
53
    {
54
        static $instance;
55
        if(!isset($instance)) {
56
            $instance = new TinyMCE($config);
57
        }else{
58
            $instance->setConfig($config);
59
        }
60
61
        return $instance;
62
    }
63
64
    public function setConfig($config)
65
    {
66
        foreach ($config as $key => $val) {
67
            $this->config[$key] = $val;
68
        }
69
    }
70
71
  /**
72
  * Initializes the tinyMCE
73
  * @return   true
74
  **/
75
76
    public function init()
77
    {
78
        // list of configured options
79
        $configured                = [];
80
        $this->setting['selector'] = 'textarea';
81
        $this->setting['theme']    = 'modern';
82
83
        // Load default settings
84
        if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) {
85
            $this->setting = include __DIR__ . '/settings.php';
86
        }
87
88
        // get editor language (from ...)
89
        if (is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) {
90
            $this->setting['language'] = $this->config['language'];
91
            $configured[]              = 'language';
92
        }
93
94
        $this->setting['content_css'] = implode(',', $this->loadCss());
95
        $configured[]                 = 'content_css';
96
97
        if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) {
98
            $this->setting['theme'] = $this->config['theme'];
99
            $configured[]           = 'theme';
100
        }
101
102
        if (!empty($this->config['mode'])) {
103
            $this->setting['mode'] = $this->config['mode'];
104
            $configured[]          = 'mode';
105
        }
106
107
        // load all plugins except the plugins in setting["exclude_plugins"]
108
        $this->setting['plugins'] = implode(',', $this->loadPlugins());
109
        $configured[]             = 'plugins';
110
111
        if ('simple' !== $this->setting['theme']) {
112
            if (empty($this->config['buttons'])) {
113
                $this->config['buttons'][] = [
114
                    'before' => '',
115
                    'add'    => '',
116
                ];
117
                $this->config['buttons'][] = [
118
                    'before' => '',
119
                    'add'    => '',
120
                ];
121
                $this->config['buttons'][] = [
122
                    'before' => '',
123
                    'add'    => '',
124
                ];
125
            }
126
            $i = 0;
127
            foreach ($this->config['buttons'] as $button) {
128
                ++$i;
129
                if (isset($button['before'])) {
130
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before'];
131
                }
132
                if (isset($button['add'])) {
133
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add'];
134
                }
135
                if (isset($button[''])) {
136
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button[''];
137
                }
138
            }
139
            $configured[] = 'buttons';
140
141
            if (isset($this->config['toolbar_location'])) {
142
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location'];
143
                $configured[]                                                            = 'toolbar_location';
144
            } else {
145
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top';
146
            }
147
148
            if (isset($this->config['toolbar_align'])) {
149
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align'];
150
                $configured[]                                                         = 'toolbar_align';
151
            } else {
152
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left';
153
            }
154
155
            if (isset($this->config['statusbar_location'])) {
156
                $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location'];
157
                $configured[]                                                              = 'statusbar_location';
158
            }
159
160
            if (isset($this->config['path_location'])) {
161
                $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location'];
162
                $configured[]                                                         = 'path_location';
163
            }
164
165
            if (isset($this->config['resize_horizontal'])) {
166
                $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal'];
167
                $configured[]                                                             = 'resize_horizontal';
168
            }
169
170
            if (isset($this->config['resizing'])) {
171
                $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing'];
172
                $configured[]                                                    = 'resizing';
173
            }
174
175
            if (!empty($this->config['fonts'])) {
176
                $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts'];
177
                $configured[]                                                 = 'fonts';
178
            }
179
180
            for ($i=1 ; $i <= 4 ; ++$i ) {
181
                $buttons = [];
182
                if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) {
183
                    $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]);
184
                    foreach ( $checklist as $plugin ) {
185
                        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...
186
                            if ( in_array( $plugin, $this->xoopsPlugins ) ) {
187
                                $buttons[] = $plugin;
188
                            }
189
                        } else {
190
                            $buttons[] = $plugin;
191
                        }
192
                    }
193
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons);
194
                }
195
            }
196
        }
197
198
        $configured = array_unique($configured);
199
        foreach ($this->config as $key => $val) {
200
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
201
                continue;
202
            }
203
            $this->setting[$key] = $val;
204
        }
205
206
        if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) {
207
            $this->setting['docs_language'] = 'en';
208
        }
209
210
        unset($this->config, $configured);
211
212
        return true;
213
    }
214
215
    // load all plugins execpt the plugins in setting["exclude_plugins"]
216
    public function loadPlugins()
217
    {
218
        $plugins      = [];
219
        $plugins_list = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
220
        if (empty($this->setting['plugins'])) {
221
            $plugins = $plugins_list;
222
        } else {
223
            $plugins = array_intersect(explode(',', $this->setting['plugins']), $plugins_list);
224
        }
225
        if (!empty($this->setting['exclude_plugins'])) {
226
            $plugins = array_diff($plugins, explode(',', $this->setting['exclude_plugins']));
227
        }
228
        if (!empty($this->config['plugins'])) {
229
            $plugins = array_merge($plugins, $this->config['plugins']);
230
        }
231
232
        return $plugins;
233
    }
234
235
    // return all xoops plugins
236
    public function get_xoopsPlugins()
237
    {
238
        $xoopsPlugins = [];
239
        $allplugins   = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
240
        foreach ( $allplugins as $plugin ) {
241
            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...
242
                if ($right = @include XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php") {
243
                    $xoopsPlugins[$plugin] = $plugin;
244
                }
245
            }
246
        }
247
248
        return $xoopsPlugins;
249
    }
250
251
    public function loadCss($css_file = 'style.css')
252
    {
253
        static $css_url, $css_path;
254
255
        if (!isset($css_url)) {
256
            $css_url = dirname( xoops_getcss($GLOBALS['xoopsConfig']['theme_set']) );
257
            $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
258
        }
259
260
        $css         = [];
261
        $css[] = $css_url . '/' . $css_file;
262
        $css_content = file_get_contents( $css_path . '/' . $css_file );
263
264
        // get all import css files
265
        if ( preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER) ) {
266
            foreach( $matches[1] as $key => $css_import ) {
267
                $css = array_merge( $css, $this->loadCss( $css_import) );
268
            }
269
        }
270
271
        return $css;
272
    }
273
274
  /**
275
  * Renders the tinyMCE
276
  * @return   string  $ret      The rendered HTML string
277
  **/
278
    public function render()
279
  {
280
    static $rendered;
281
        if ($rendered) {
282
            return null;
283
        }
284
285
    $rendered = true;
286
287
    $this->init();
288
289
        if (!empty($this->setting['callback'])) {
290
            $callback = $this->setting['callback'];
291
            unset($this->setting['callback']);
292
    } else {
293
            $callback = '';
294
    }
295
296
    $ret = '<script language="javascript" type="text/javascript" src="' . XOOPS_URL . $this->rootpath . '/tinymce.min.js"></script>';
297
    $ret .= '<script language="javascript" type="text/javascript">
298
                tinyMCE.init({
299
            ';
300
301
foreach ($this->setting as $key => $val) {
302
            $ret .= $key . ':';
303
            if (true === $val) {
304
                $ret .= 'true,';
305
            } elseif (false === $val) {
306
                $ret .= 'false,';
307
         } else {
308
             $ret .= "'{$val}',";
309
         }
310
         $ret .= "\n";
311
     }
312
313
//   Ajout alain01 tinymce v4
314
315
    $chemin_array=parse_url(XOOPS_URL);
316
        $chemin_scheme = $chemin_array['scheme']; // http
0 ignored issues
show
Unused Code introduced by
The assignment to $chemin_scheme is dead and can be removed.
Loading history...
317
        $chemin_host   = $chemin_array['host']; // www.example.com  or // localhost
0 ignored issues
show
Unused Code introduced by
The assignment to $chemin_host is dead and can be removed.
Loading history...
318
//  $chemin_path =  $chemin_array["path"]; // /myweb1
319
    if (!isset($chemin_array['path'])){
320
            $chemin_path = '';
321
    } else    {
322
            $chemin_path = $chemin_array['path'];
323
    }
324
325
        //   $ret .='language_url : "'.$chemin_path.'/class/xoopseditor/tinymce4/tinymce/js/tinymce/langs/fr_FR.js",';
326
327
    $ret .= 'external_plugins: {';
328
        $ret .= '"qrcode": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/qrcode/plugin.min.js",';
329
        $ret .= '"youtube": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/youtube/plugin.min.js",';
330
        $ret .= '"alignbtn": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/alignbtn/plugin.min.js",';
331
        $ret .= '"chartextbtn": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/chartextbtn/plugin.min.js",';
332
        $ret .= '"xoops_code": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/xoops_code/plugin.min.js",';
333
        $ret .= '"xoops_quote": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/xoops_quote/plugin.min.js",';
334
        $ret .= '"xoops_tagextgal": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/xoops_tagextgal/plugin.min.js",';
335
336
        $ret .= '"codemirror": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/codemirror/plugin.min.js",';
337
338
        $ret .= '"filemanager": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/filemanager/plugin.min.js",';
339
        $ret .= '"responsivefilemanager": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/responsivefilemanager/plugin.min.js",';
340
    $ret .= '},';
341
342
$ret .= 'codemirror: {
343
    indentOnInit: true,
344
    path: "CodeMirror",
345
    config: {
346
       mode: "application/x-httpd-php",
347
       lineNumbers: false
348
    },
349
    jsFiles: [
350
       "mode/clike/clike.js",
351
       "mode/php/php.js"
352
    ]
353
  },';
354
355
    $ret .= "\n";
356
357
        $ret .= '"external_filemanager_path": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/filemanager/",';
358
    $ret .= "\n";
359
360
    $ret .='templates: "'.$chemin_path.'/uploads/filemanager/templates/liste-templates.js",';
361
    $ret .= "\n";
362
// fin ajout alain01
363
364
    $ret .= 'relative_urls : false,
365
                remove_script_host : false, tinymceload : "1"});
366
                '.$callback.'
367
                function showMCE(id) {
368
                    if (tinyMCE.getInstanceById(id) == null) {
369
                        tinyMCE.execCommand("mceAddControl", false, id);
370
                    } else {
371
                        tinyMCE.execCommand("mceRemoveControl", false, id);
372
                    }
373
                }
374
                </script>
375
            ';
376
377
    return $ret ;
378
  }
379
}
380