Total Complexity | 53 |
Total Lines | 313 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TinyMCEJQ often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TinyMCEJQ, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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(); |
||
|
|||
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')) { |
||
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")) { |
||
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() |
||
338 | } |
||
339 | } |
||
340 | |||
341 | |||
342 |