Total Complexity | 86 |
Total Lines | 626 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like XoopsModule 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 XoopsModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class XoopsModule extends XoopsObject |
||
26 | { |
||
27 | /** |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | public $modinfo; |
||
32 | /** |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | public $adminmenu; |
||
37 | /** |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | public $_msg; |
||
42 | |||
43 | /** |
||
44 | * Constructor |
||
45 | */ |
||
46 | public function __construct() |
||
47 | { |
||
48 | parent::__construct(); |
||
49 | $this->initVar('mid', XOBJ_DTYPE_INT, null, false); |
||
50 | $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150); |
||
51 | $this->initVar('version', XOBJ_DTYPE_TXTBOX, null, false); |
||
52 | $this->initVar('last_update', XOBJ_DTYPE_INT, null, false); |
||
53 | $this->initVar('weight', XOBJ_DTYPE_INT, 0, false); |
||
54 | $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false); |
||
55 | $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true); |
||
56 | $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false); |
||
57 | $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false); |
||
58 | $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false); |
||
59 | $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false); |
||
60 | $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false); |
||
61 | // RMV-NOTIFY |
||
62 | $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Load module info |
||
67 | * |
||
68 | * @param string $dirname Directory Name |
||
69 | * @param boolean $verbose |
||
70 | */ |
||
71 | public function loadInfoAsVar($dirname, $verbose = true) |
||
72 | { |
||
73 | $dirname = basename($dirname); |
||
74 | if (!isset($this->modinfo)) { |
||
75 | $this->loadInfo($dirname, $verbose); |
||
76 | } |
||
77 | $this->setVar('name', $this->modinfo['name'], true); |
||
78 | $this->setVar('version', $this->modinfo['version'], true); |
||
79 | $this->setVar('dirname', $this->modinfo['dirname'], true); |
||
80 | $hasmain = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0; |
||
81 | $hasadmin = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0; |
||
82 | $hassearch = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 1 : 0; |
||
83 | $hasconfig = ((isset($this->modinfo['config']) && is_array($this->modinfo['config'])) || !empty($this->modinfo['hasComments'])) ? 1 : 0; |
||
84 | $hascomments = (isset($this->modinfo['hasComments']) && $this->modinfo['hasComments'] == 1) ? 1 : 0; |
||
85 | // RMV-NOTIFY |
||
86 | $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 1 : 0; |
||
87 | $this->setVar('hasmain', $hasmain); |
||
88 | $this->setVar('hasadmin', $hasadmin); |
||
89 | $this->setVar('hassearch', $hassearch); |
||
90 | $this->setVar('hasconfig', $hasconfig); |
||
91 | $this->setVar('hascomments', $hascomments); |
||
92 | // RMV-NOTIFY |
||
93 | $this->setVar('hasnotification', $hasnotification); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * add a message |
||
98 | * |
||
99 | * @param string $str message to add |
||
100 | * @access public |
||
101 | */ |
||
102 | public function setMessage($str) |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * return the messages for this object as an array |
||
109 | * |
||
110 | * @return array an array of messages |
||
111 | * @access public |
||
112 | */ |
||
113 | public function getMessages() |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Set module info |
||
120 | * |
||
121 | * @param string $name |
||
122 | * @param mixed $value |
||
123 | * @return bool |
||
124 | **/ |
||
125 | public function setInfo($name, $value) |
||
126 | { |
||
127 | if (empty($name)) { |
||
128 | $this->modinfo = $value; |
||
129 | } else { |
||
130 | $this->modinfo[$name] = $value; |
||
131 | } |
||
132 | |||
133 | return true; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Get module info |
||
138 | * |
||
139 | * @param string $name |
||
140 | * @return array|string Array of module information. |
||
141 | * If {@link $name} is set, returns a single module information item as string. |
||
142 | */ |
||
143 | public function &getInfo($name = null) |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get statut |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function getStatus() |
||
166 | { |
||
167 | return substr(strrchr($this->getVar('version'), '-'), 1); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Compares two "XOOPS-standardized" version number strings. |
||
172 | * |
||
173 | * @param string $version1 |
||
174 | * @param string $version2 |
||
175 | * @param string $operator |
||
176 | * @return boolean The function will return true if the relationship is the one specified by the operator, false otherwise. |
||
177 | */ |
||
178 | public function versionCompare($version1 = '',$version2 = '', $operator = '<') |
||
179 | { |
||
180 | $version1 = strtolower($version1); |
||
181 | $version2 = strtolower($version2); |
||
182 | if (true == strpos($version2, '-stable')){ |
||
183 | $version2 = substr($version2, 0, strpos($version2, '-stable')); |
||
184 | } |
||
185 | if (true == strpos($version1, '-stable')){ |
||
186 | $version1 = substr($version1, 0, strpos($version1, '-stable')); |
||
187 | } |
||
188 | return version_compare($version1, $version2, $operator); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Get a link to the modules main page |
||
193 | * |
||
194 | * @return string FALSE on fail |
||
195 | */ |
||
196 | public function mainLink() |
||
197 | { |
||
198 | if ($this->getVar('hasmain') == 1) { |
||
199 | $ret = '<a href="' . XOOPS_URL . '/modules/' . $this->getVar('dirname') . '/">' . $this->getVar('name') . '</a>'; |
||
200 | |||
201 | return $ret; |
||
202 | } |
||
203 | |||
204 | return false; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Get links to the subpages |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function subLink() |
||
213 | { |
||
214 | $ret = array(); |
||
215 | if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) { |
||
216 | foreach ($this->getInfo('sub') as $submenu) { |
||
217 | $ret[] = array( |
||
218 | 'name' => $submenu['name'], |
||
219 | 'url' => $submenu['url']); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | return $ret; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Load the admin menu for the module |
||
228 | */ |
||
229 | public function loadAdminMenu() |
||
230 | { |
||
231 | $adminmenu = array(); |
||
232 | if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'))) { |
||
233 | include XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'); |
||
234 | } |
||
235 | $this->adminmenu =& $adminmenu; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get the admin menu for the module |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function &getAdminMenu() |
||
244 | { |
||
245 | if (!isset($this->adminmenu)) { |
||
246 | $this->loadAdminMenu(); |
||
247 | } |
||
248 | |||
249 | return $this->adminmenu; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Load the module info for this module |
||
254 | * |
||
255 | * @param string $dirname Module directory |
||
256 | * @param bool $verbose Give an error on fail? |
||
257 | * |
||
258 | * @return bool true if loaded |
||
259 | */ |
||
260 | public function loadInfo($dirname, $verbose = true) |
||
261 | { |
||
262 | static $modVersions; |
||
263 | $dirname = basename($dirname); |
||
264 | if (isset($modVersions[$dirname])) { |
||
265 | $this->modinfo = $modVersions[$dirname]; |
||
266 | |||
267 | return true; |
||
268 | } |
||
269 | global $xoopsConfig; |
||
270 | if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/' . $xoopsConfig['language'] . '/modinfo.php'))) { |
||
271 | include_once $file; |
||
272 | } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/english/modinfo.php'))) { |
||
273 | include_once $file; |
||
274 | } |
||
275 | |||
276 | if (!file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/xoops_version.php'))) { |
||
277 | if (false !== (bool)$verbose) { |
||
278 | echo "Module File for $dirname Not Found!"; |
||
279 | } |
||
280 | |||
281 | return false; |
||
282 | } |
||
283 | include $file; |
||
284 | $modVersions[$dirname] = $modversion; |
||
285 | $this->modinfo = $modVersions[$dirname]; |
||
286 | |||
287 | return true; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Search contents within a module |
||
292 | * |
||
293 | * @param string $term |
||
294 | * @param string $andor 'AND' or 'OR' |
||
295 | * @param integer $limit |
||
296 | * @param integer $offset |
||
297 | * @param integer $userid |
||
298 | * @return mixed Search result. |
||
299 | */ |
||
300 | public function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0) |
||
301 | { |
||
302 | if ($this->getVar('hassearch') != 1) { |
||
303 | return false; |
||
304 | } |
||
305 | $search =& $this->getInfo('search'); |
||
306 | if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') { |
||
307 | return false; |
||
308 | } |
||
309 | if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/' . $search['file']))) { |
||
310 | include_once $file; |
||
311 | } else { |
||
312 | return false; |
||
313 | } |
||
314 | if (function_exists($search['func'])) { |
||
315 | $func = $search['func']; |
||
316 | |||
317 | return $func($term, $andor, $limit, $offset, $userid); |
||
318 | } |
||
319 | |||
320 | return false; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Returns Class Base Variable mid |
||
325 | * @param string $format |
||
326 | * @return mixed |
||
327 | */ |
||
328 | public function id($format = 'N') |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Returns Class Base Variable mid |
||
335 | * @param string $format |
||
336 | * @return mixed |
||
337 | */ |
||
338 | public function mid($format = '') |
||
339 | { |
||
340 | return $this->getVar('mid', $format); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Returns Class Base Variable name |
||
345 | * @param string $format |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function name($format = '') |
||
349 | { |
||
350 | return $this->getVar('name', $format); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Returns Class Base Variable version |
||
355 | * @param string $format |
||
356 | * @return mixed |
||
357 | */ |
||
358 | public function version($format = '') |
||
359 | { |
||
360 | return $this->getVar('version', $format); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Returns Class Base Variable last_update |
||
365 | * @param string $format |
||
366 | * @return mixed |
||
367 | */ |
||
368 | public function last_update($format = '') |
||
369 | { |
||
370 | return $this->getVar('last_update', $format); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Returns Class Base Variable weight |
||
375 | * @param string $format |
||
376 | * @return mixed |
||
377 | */ |
||
378 | public function weight($format = '') |
||
379 | { |
||
380 | return $this->getVar('weight', $format); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Returns Class Base Variable isactive |
||
385 | * @param string $format |
||
386 | * @return mixed |
||
387 | */ |
||
388 | public function isactive($format = '') |
||
389 | { |
||
390 | return $this->getVar('isactive', $format); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Returns Class Base Variable dirname |
||
395 | * @param string $format |
||
396 | * @return mixed |
||
397 | */ |
||
398 | public function dirname($format = '') |
||
399 | { |
||
400 | return $this->getVar('dirname', $format); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Returns Class Base Variable hasmain |
||
405 | * @param string $format |
||
406 | * @return mixed |
||
407 | */ |
||
408 | public function hasmain($format = '') |
||
409 | { |
||
410 | return $this->getVar('hasmain', $format); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Returns Class Base Variable hasadmin |
||
415 | * @param string $format |
||
416 | * @return mixed |
||
417 | */ |
||
418 | public function hasadmin($format = '') |
||
419 | { |
||
420 | return $this->getVar('hasadmin', $format); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Returns Class Base Variable hassearch |
||
425 | * @param string $format |
||
426 | * @return mixed |
||
427 | */ |
||
428 | public function hassearch($format = '') |
||
429 | { |
||
430 | return $this->getVar('hassearch', $format); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Returns Class Base Variable hasconfig |
||
435 | * @param string $format |
||
436 | * @return mixed |
||
437 | */ |
||
438 | public function hasconfig($format = '') |
||
439 | { |
||
440 | return $this->getVar('hasconfig', $format); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Returns Class Base Variable hascomments |
||
445 | * @param string $format |
||
446 | * @return mixed |
||
447 | */ |
||
448 | public function hascomments($format = '') |
||
449 | { |
||
450 | return $this->getVar('hascomments', $format); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Returns Class Base Variable hasnotification |
||
455 | * @param string $format |
||
456 | * @return mixed |
||
457 | */ |
||
458 | public function hasnotification($format = '') |
||
459 | { |
||
460 | return $this->getVar('hasnotification', $format); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param $dirname |
||
465 | * |
||
466 | * @return mixed |
||
467 | */ |
||
468 | public static function getByDirname($dirname) |
||
469 | { |
||
470 | /* @var XoopsModuleHandler $modhandler */ |
||
471 | $modhandler = xoops_getHandler('module'); |
||
472 | $inst = $modhandler->getByDirname($dirname); |
||
473 | |||
474 | return $inst; |
||
475 | } |
||
476 | |||
477 | ##################### Deprecated Methods ###################### |
||
478 | |||
479 | /**#@+ |
||
480 | * @deprecated |
||
481 | */ |
||
482 | public function checkAccess() |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * @param string $type |
||
491 | * |
||
492 | * @return bool |
||
493 | */ |
||
494 | public function loadLanguage($type = 'main') |
||
495 | { |
||
496 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
497 | |||
498 | return false; |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function loadErrorMessages() |
||
505 | { |
||
506 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
507 | |||
508 | return false; |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * @return bool |
||
513 | */ |
||
514 | public function getCurrentPage() |
||
515 | { |
||
516 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
517 | |||
518 | return false; |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * @param array $admingroups |
||
523 | * @param array $accessgroups |
||
524 | * |
||
525 | * @return bool |
||
526 | */ |
||
527 | public function install($admingroups = array(), $accessgroups = array()) |
||
528 | { |
||
529 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
530 | |||
531 | return false; |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * @return bool |
||
536 | */ |
||
537 | public function update() |
||
538 | { |
||
539 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
540 | |||
541 | return false; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * @return bool |
||
546 | */ |
||
547 | public function insert() |
||
548 | { |
||
549 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
550 | |||
551 | return false; |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * @return bool |
||
556 | */ |
||
557 | public function executeSQL() |
||
558 | { |
||
559 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
560 | |||
561 | return false; |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * @return bool |
||
566 | */ |
||
567 | public function insertTemplates() |
||
568 | { |
||
569 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
570 | |||
571 | return false; |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * @param $template |
||
576 | * @param bool $block |
||
577 | * |
||
578 | * @return bool |
||
579 | */ |
||
580 | public function gettemplate($template, $block = false) |
||
581 | { |
||
582 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
583 | |||
584 | return false; |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * @return bool |
||
589 | */ |
||
590 | public function insertBlocks() |
||
591 | { |
||
592 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
593 | |||
594 | return false; |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * @return bool |
||
599 | */ |
||
600 | public function insertConfigCategories() |
||
601 | { |
||
602 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
603 | |||
604 | return false; |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * @return bool |
||
609 | */ |
||
610 | public function insertConfig() |
||
611 | { |
||
612 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
613 | |||
614 | return false; |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * @return bool |
||
619 | */ |
||
620 | public function insertProfileFields() |
||
621 | { |
||
622 | trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING); |
||
623 | |||
624 | return false; |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * @param $type |
||
629 | * @param int $state |
||
630 | * |
||
631 | * @return bool |
||
632 | */ |
||
633 | public function executeScript($type, $state = 2) |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * @param $groups |
||
642 | * @param $type |
||
643 | * |
||
644 | * @return bool |
||
645 | */ |
||
646 | public function insertGroupPermissions($groups, $type) |
||
651 | } |
||
652 | /**#@-*/ |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * XOOPS module handler class. |
||
657 | * |
||
658 | * This class is responsible for providing data access mechanisms to the data source |
||
659 | * of XOOPS module class objects. |
||
660 | * |
||
661 | * @package kernel |
||
662 | * @author Kazumi Ono <[email protected]> |
||
663 | * @copyright (c) 2000-2016 XOOPS Project - www.xoops.org |
||
664 | * |
||
665 | * @todo Why is this not a XoopsPersistableObjectHandler? |
||
666 | */ |
||
667 | class XoopsModuleHandler extends XoopsObjectHandler |
||
668 | { |
||
669 | /** |
||
670 | * holds an array of cached module references, indexed by module id |
||
671 | * |
||
672 | * @var array |
||
673 | * @access private |
||
674 | */ |
||
675 | public $_cachedModule_mid = array(); |
||
676 | |||
677 | /** |
||
678 | * holds an array of cached module references, indexed by module dirname |
||
679 | * |
||
958 |