| Total Complexity | 53 |
| Total Lines | 365 |
| 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 |
||
| 26 | class TinyMCE |
||
| 27 | { |
||
| 28 | public $rootpath; |
||
| 29 | public $config = array(); |
||
| 30 | public $setting = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * constructor |
||
| 34 | * |
||
| 35 | * @param string $config The configuration |
||
| 36 | */ |
||
| 37 | public function __construct($config) |
||
| 38 | { |
||
| 39 | $this->setConfig($config); |
||
| 40 | $this->rootpath = $this->config["rootpath"] . "/tinymce/js/tinymce"; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Creates one instance of the tinyMCE object |
||
| 45 | * |
||
| 46 | * @param array $config The configuration |
||
| 47 | * |
||
| 48 | * @return TinyMCE $instance The instance of tinyMCE object |
||
| 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 | public function setConfig($config) |
||
| 63 | { |
||
| 64 | foreach ($config as $key => $val) { |
||
| 65 | $this->config[$key] = $val; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initializes the tinyMCE |
||
| 71 | * |
||
| 72 | * @return boolean |
||
| 73 | */ |
||
| 74 | public function init() |
||
| 75 | { |
||
| 76 | // list of configured options |
||
| 77 | $configured = array(); |
||
| 78 | $this->setting["selector"] = "textarea"; |
||
| 79 | $this->setting["theme"] = "modern"; |
||
| 80 | |||
| 81 | // Load default settings |
||
| 82 | if (!($this->setting = @include($GLOBALS['xoops']->path("var/configs/tinymce.php")))) { |
||
| 83 | $this->setting = include __DIR__ . "/settings.php"; |
||
| 84 | } |
||
| 85 | |||
| 86 | // get editor language (from ...) |
||
| 87 | if (is_readable(\XoopsBaseConfig::get('root-path') . $this->rootpath . '/langs/' . $this->config["language"] . '.js')) { |
||
| 88 | $this->setting["language"] = $this->config["language"]; |
||
| 89 | $configured[] = "language"; |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->setting["content_css"] = implode(",", $this->loadCss()); |
||
| 93 | $configured[] = "content_css"; |
||
| 94 | |||
| 95 | if (!empty($this->config["theme"]) |
||
| 96 | && is_dir(\XoopsBaseConfig::get('root-path') . $this->rootpath . "/themes/" . $this->config["theme"]) |
||
| 97 | ) { |
||
| 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 ($this->setting["theme"] !== "simple") { |
||
| 112 | if (empty($this->config["buttons"])) { |
||
| 113 | $this->config["buttons"][] = array( |
||
| 114 | "before" => "", |
||
| 115 | "add" => "", |
||
| 116 | ); |
||
| 117 | $this->config["buttons"][] = array( |
||
| 118 | "before" => "", |
||
| 119 | "add" => "", |
||
| 120 | ); |
||
| 121 | $this->config["buttons"][] = array( |
||
| 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"] |
||
| 143 | = $this->config["toolbar_location"]; |
||
| 144 | $configured[] = "toolbar_location"; |
||
| 145 | } else { |
||
| 146 | $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = "top"; |
||
| 147 | } |
||
| 148 | |||
| 149 | if (isset($this->config["toolbar_align"])) { |
||
| 150 | $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = $this->config["toolbar_align"]; |
||
| 151 | $configured[] = "toolbar_align"; |
||
| 152 | } else { |
||
| 153 | $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = "left"; |
||
| 154 | } |
||
| 155 | |||
| 156 | if (isset($this->config["statusbar_location"])) { |
||
| 157 | $this->setting["theme_" . $this->setting["theme"] . "_statusbar_location"] |
||
| 158 | = $this->config["statusbar_location"]; |
||
| 159 | $configured[] = "statusbar_location"; |
||
| 160 | } |
||
| 161 | |||
| 162 | if (isset($this->config["path_location"])) { |
||
| 163 | $this->setting["theme_" . $this->setting["theme"] . "_path_location"] = $this->config["path_location"]; |
||
| 164 | $configured[] = "path_location"; |
||
| 165 | } |
||
| 166 | |||
| 167 | if (isset($this->config["resize_horizontal"])) { |
||
| 168 | $this->setting["theme_" . $this->setting["theme"] . "_resize_horizontal"] |
||
| 169 | = $this->config["resize_horizontal"]; |
||
| 170 | $configured[] = "resize_horizontal"; |
||
| 171 | } |
||
| 172 | |||
| 173 | if (isset($this->config["resizing"])) { |
||
| 174 | $this->setting["theme_" . $this->setting["theme"] . "_resizing"] = $this->config["resizing"]; |
||
| 175 | $configured[] = "resizing"; |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!empty($this->config["fonts"])) { |
||
| 179 | $this->setting["theme_" . $this->setting["theme"] . "_fonts"] = $this->config["fonts"]; |
||
| 180 | $configured[] = "fonts"; |
||
| 181 | } |
||
| 182 | |||
| 183 | for ($i=1; $i <= 4; $i++) { |
||
| 184 | $buttons = array(); |
||
| 185 | if (isset($this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"])) { |
||
| 186 | $checklist = explode(",", $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"]); |
||
| 187 | foreach ($checklist as $plugin) { |
||
| 188 | if (strpos(strtolower($plugin), "xoops") !== false) { |
||
| 189 | if (in_array($plugin, $this->xoopsPlugins)) { |
||
| 190 | $buttons[] = $plugin; |
||
| 191 | } |
||
| 192 | } else { |
||
| 193 | $buttons[] = $plugin; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = implode(",", $buttons); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | $configured = array_unique($configured); |
||
| 202 | foreach ($this->config as $key => $val) { |
||
| 203 | if (isset($this->setting[$key]) || in_array($key, $configured)) { |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | $this->setting[$key] = $val; |
||
| 207 | } |
||
| 208 | |||
| 209 | if (!is_dir(\XoopsBaseConfig::get('root-path') . $this->rootpath . "/themes/" . $this->setting["theme"] . '/docs/' . $this->setting["language"] . '/')) { |
||
| 210 | $this->setting["docs_language"] = "en"; |
||
| 211 | } |
||
| 212 | |||
| 213 | unset($this->config, $configured); |
||
| 214 | |||
| 215 | return true; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * loadPlugins - load all plugins execpt the plugins in setting["exclude_plugins"] |
||
| 220 | * |
||
| 221 | * @return array plugins |
||
| 222 | */ |
||
| 223 | public function loadPlugins() |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * get_xoopsPlugins - return all xoops plugins |
||
| 244 | * |
||
| 245 | * @return array plugins |
||
| 246 | */ |
||
| 247 | public function get_xoopsPlugins() |
||
| 248 | { |
||
| 249 | $xoopsPlugins = array(); |
||
| 250 | $xoops_root_path = \XoopsBaseConfig::get('root-path'); |
||
| 251 | $allplugins = XoopsLists::getDirListAsArray ($xoops_root_path . $this->rootpath . "/plugins"); |
||
| 252 | foreach ($allplugins as $plugin) { |
||
| 253 | if (strpos(strtolower($plugin), "xoops") !== false |
||
| 254 | && file_exists($xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php")) { |
||
| 255 | if ($right = @include $xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php") { |
||
| 256 | $xoopsPlugins[$plugin] = $plugin; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | return $xoopsPlugins; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function loadCss($css_file = 'style.css') |
||
| 265 | { |
||
| 266 | static $css_url, $css_path; |
||
| 267 | |||
| 268 | if (!isset($css_url)) { |
||
| 269 | $css_url = dirname(\Xoops::getInstance()->getCss($GLOBALS['xoopsConfig']['theme_set'])); |
||
| 270 | $css_path = str_replace(\XoopsBaseConfig::get('themes-url'), \XoopsBaseConfig::get('themes-path'), $css_url); |
||
| 271 | } |
||
| 272 | |||
| 273 | $css = array(); |
||
| 274 | $css[] = $css_url . '/' . $css_file; |
||
| 275 | $css_content = file_get_contents($css_path . '/' . $css_file); |
||
| 276 | |||
| 277 | // get all import css files |
||
| 278 | if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) { |
||
| 279 | foreach ($matches[1] as $key => $css_import) { |
||
| 280 | $css = array_merge($css, $this->loadCss($css_import)); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | return $css; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Renders the tinyMCE |
||
| 289 | * |
||
| 290 | * @return string $ret The rendered HTML string |
||
| 291 | */ |
||
| 292 | public function render() |
||
| 391 | } |
||
| 392 | } |
||
| 393 |