| Total Complexity | 56 |
| Total Lines | 356 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TinyMCE 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 TinyMCE, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class TinyMCE |
||
| 25 | { |
||
| 26 | public $rootpath; |
||
| 27 | public $config = array(); |
||
| 28 | public $setting = array(); |
||
| 29 | public static $LastOfElementsTinymce = ''; |
||
| 30 | public static $ListOfElementsTinymce = array(); |
||
| 31 | |||
| 32 | // PHP 5 Constructor |
||
| 33 | /** |
||
| 34 | * @param $config |
||
| 35 | */ |
||
| 36 | public function __construct($config) |
||
| 37 | { |
||
| 38 | $this->setConfig($config); |
||
| 39 | $this->rootpath = $this->config['rootpath'] . '/tinymce/jscripts/tiny_mce'; |
||
| 40 | $this->xoopsPlugins = $this->get_xoopsPlugins(); |
||
|
|
|||
| 41 | self::$LastOfElementsTinymce = $this->config['elements']; |
||
| 42 | self::$ListOfElementsTinymce[] = self::$LastOfElementsTinymce; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param $config |
||
| 47 | * |
||
| 48 | * @return TinyMCE |
||
| 49 | */ |
||
| 50 | public function &instance($config) |
||
| 51 | { |
||
| 52 | static $instance; |
||
| 53 | if (!isset($instance)) { |
||
| 54 | $instance = new TinyMCE($config); |
||
| 55 | } else { |
||
| 56 | $instance->setConfig($config); |
||
| 57 | } |
||
| 58 | |||
| 59 | return $instance; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $config |
||
| 64 | */ |
||
| 65 | public function setConfig($config) |
||
| 66 | { |
||
| 67 | foreach ($config as $key => $val) { |
||
| 68 | $this->config[$key] = $val; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function init() |
||
| 76 | { |
||
| 77 | // list of configured options |
||
| 78 | $configured = array(); |
||
| 79 | |||
| 80 | // Load default settings |
||
| 81 | if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) { |
||
| 82 | $this->setting = include __DIR__ . '/settings.php'; |
||
| 83 | } |
||
| 84 | |||
| 85 | // get editor language (from ...) |
||
| 86 | if (isset($this->config['language']) && is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) { |
||
| 87 | $this->setting['language'] = $this->config['language']; |
||
| 88 | $configured[] = 'language'; |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->setting['content_css'] = implode(',', $this->loadCss()); |
||
| 92 | $configured[] = 'content_css'; |
||
| 93 | |||
| 94 | if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) { |
||
| 95 | $this->setting['theme'] = $this->config['theme']; |
||
| 96 | $configured[] = 'theme'; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!empty($this->config['mode'])) { |
||
| 100 | $this->setting['mode'] = $this->config['mode']; |
||
| 101 | $configured[] = 'mode'; |
||
| 102 | } |
||
| 103 | |||
| 104 | // load all plugins except the plugins in setting["exclude_plugins"] |
||
| 105 | $this->setting['plugins'] = implode(',', $this->loadPlugins()); |
||
| 106 | $configured[] = 'plugins'; |
||
| 107 | |||
| 108 | if ($this->setting['theme'] !== 'simple') { |
||
| 109 | if (empty($this->config['buttons'])) { |
||
| 110 | $this->config['buttons'][] = array( |
||
| 111 | 'before' => '', |
||
| 112 | 'add' => ''); |
||
| 113 | $this->config['buttons'][] = array( |
||
| 114 | 'before' => '', |
||
| 115 | 'add' => ''); |
||
| 116 | $this->config['buttons'][] = array( |
||
| 117 | 'before' => '', |
||
| 118 | 'add' => ''); |
||
| 119 | } |
||
| 120 | $i = 0; |
||
| 121 | foreach ($this->config['buttons'] as $button) { |
||
| 122 | ++$i; |
||
| 123 | if (isset($button['before'])) { |
||
| 124 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before']; |
||
| 125 | } |
||
| 126 | if (isset($button['add'])) { |
||
| 127 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add']; |
||
| 128 | } |
||
| 129 | if (isset($button[''])) { |
||
| 130 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button['']; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $configured[] = 'buttons'; |
||
| 134 | |||
| 135 | if (isset($this->config['toolbar_location'])) { |
||
| 136 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location']; |
||
| 137 | $configured[] = 'toolbar_location'; |
||
| 138 | } else { |
||
| 139 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top'; |
||
| 140 | } |
||
| 141 | |||
| 142 | if (isset($this->config['toolbar_align'])) { |
||
| 143 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align']; |
||
| 144 | $configured[] = 'toolbar_align'; |
||
| 145 | } else { |
||
| 146 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left'; |
||
| 147 | } |
||
| 148 | |||
| 149 | if (isset($this->config['statusbar_location'])) { |
||
| 150 | $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location']; |
||
| 151 | $configured[] = 'statusbar_location'; |
||
| 152 | } |
||
| 153 | |||
| 154 | if (isset($this->config['path_location'])) { |
||
| 155 | $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location']; |
||
| 156 | $configured[] = 'path_location'; |
||
| 157 | } |
||
| 158 | |||
| 159 | if (isset($this->config['resize_horizontal'])) { |
||
| 160 | $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal']; |
||
| 161 | $configured[] = 'resize_horizontal'; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (isset($this->config['resizing'])) { |
||
| 165 | $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing']; |
||
| 166 | $configured[] = 'resizing'; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (!empty($this->config['fonts'])) { |
||
| 170 | $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts']; |
||
| 171 | $configured[] = 'fonts'; |
||
| 172 | } |
||
| 173 | |||
| 174 | for ($i = 1; $i <= 4; ++$i) { |
||
| 175 | $buttons = array(); |
||
| 176 | if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) { |
||
| 177 | $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]); |
||
| 178 | foreach ($checklist as $plugin) { |
||
| 179 | if (strpos(strtolower($plugin), 'xoops') != false) { |
||
| 180 | if (in_array($plugin, $this->xoopsPlugins)) { |
||
| 181 | $buttons[] = $plugin; |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | $buttons[] = $plugin; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | $configured = array_unique($configured); |
||
| 193 | foreach ($this->config as $key => $val) { |
||
| 194 | if (isset($this->setting[$key]) || in_array($key, $configured)) { |
||
| 195 | continue; |
||
| 196 | } |
||
| 197 | $this->setting[$key] = $val; |
||
| 198 | } |
||
| 199 | |||
| 200 | if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) { |
||
| 201 | $this->setting['docs_language'] = 'en'; |
||
| 202 | } |
||
| 203 | |||
| 204 | unset($this->config, $configured); |
||
| 205 | |||
| 206 | return true; |
||
| 207 | } |
||
| 208 | |||
| 209 | // load all plugins execpt the plugins in setting["exclude_plugins"] |
||
| 210 | /** |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function loadPlugins() |
||
| 230 | } |
||
| 231 | |||
| 232 | // return all xoops plugins |
||
| 233 | /** |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public function get_xoopsPlugins() |
||
| 237 | { |
||
| 238 | $xoopsPlugins = array(); |
||
| 239 | $allplugins = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins'); |
||
| 240 | foreach ($allplugins as $plugin) { |
||
| 241 | if (strpos(strtolower($plugin), 'xoops') != false && file_exists(XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php")) { |
||
| 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 | /** |
||
| 252 | * @param string $css_file |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function loadCss($css_file = 'style.css') |
||
| 257 | { |
||
| 258 | static $css_url, $css_path; |
||
| 259 | |||
| 260 | if (!isset($css_url)) { |
||
| 261 | $css_url = dirname(xoops_getcss($GLOBALS['xoopsConfig']['theme_set'])); |
||
| 262 | $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url); |
||
| 263 | } |
||
| 264 | |||
| 265 | $css = array(); |
||
| 266 | $css[] = $css_url . '/' . $css_file; |
||
| 267 | $css_content = file_get_contents($css_path . '/' . $css_file); |
||
| 268 | |||
| 269 | // get all import css files |
||
| 270 | if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) { |
||
| 271 | foreach ($matches[1] as $key => $css_import) { |
||
| 272 | $css = array_merge($css, $this->loadCss($css_import)); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | return $css; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function render() |
||
| 380 | } |
||
| 381 | } |
||
| 382 |