| Total Complexity | 66 | 
| Total Lines | 372 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like SystemBlockHandler 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 SystemBlockHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 303 | class SystemBlockHandler extends XoopsPersistableObjectHandler  | 
            ||
| 304 | { | 
            ||
| 305 | /**  | 
            ||
| 306 | * @param null|XoopsDatabase $db  | 
            ||
| 307 | */  | 
            ||
| 308 | public function __construct(XoopsDatabase $db)  | 
            ||
| 311 | }  | 
            ||
| 312 | |||
| 313 | /**  | 
            ||
| 314 | * @param XoopsObject|SystemBlock $obj  | 
            ||
| 315 | *  | 
            ||
| 316 | * @return int|bool object id on success, otherwise false  | 
            ||
| 317 | */  | 
            ||
| 318 | public function insert(XoopsObject $obj, $force = true)  | 
            ||
| 319 |     { | 
            ||
| 320 |         if (!($obj instanceof $this->className)) { | 
            ||
| 321 | return false;  | 
            ||
| 322 | }  | 
            ||
| 323 |         $obj->setVar('last_modified', time()); | 
            ||
| 324 | |||
| 325 | return parent::insert($obj, $force);  | 
            ||
| 326 | }  | 
            ||
| 327 | |||
| 328 | /**  | 
            ||
| 329 |      * retrieve array of {@link XoopsBlock}s meeting certain conditions | 
            ||
| 330 | *  | 
            ||
| 331 |      * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} with conditions for the blocks | 
            ||
| 332 | * @param bool $id_as_key should the blocks' bid be the key for the returned array?  | 
            ||
| 333 | * @param bool $as_object return an array of objects  | 
            ||
| 334 | *  | 
            ||
| 335 |      * @return array {@link XoopsBlock}s matching the conditions | 
            ||
| 336 | **/  | 
            ||
| 337 | public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)  | 
            ||
| 377 | }  | 
            ||
| 378 | |||
| 379 | /**  | 
            ||
| 380 | * get all the blocks that match the supplied parameters  | 
            ||
| 381 | *  | 
            ||
| 382 | * @param int|int[] $groupid groupid (can be an array)  | 
            ||
| 383 | * @param bool $asobject  | 
            ||
| 384 | * @param int|string $side  | 
            ||
| 385 | * 0: sideblock - left  | 
            ||
| 386 | * 1: sideblock - right  | 
            ||
| 387 | * 2: sideblock - left and right  | 
            ||
| 388 | * 3: centerblock - left  | 
            ||
| 389 | * 4: centerblock - right  | 
            ||
| 390 | * 5: centerblock - center  | 
            ||
| 391 | * 6: centerblock - left, right, center  | 
            ||
| 392 | * @param $visible 0: not visible 1: visible  | 
            ||
| 393 | * @param string $orderby order of the blocks  | 
            ||
| 394 | * @param int $isactive  | 
            ||
| 395 | *  | 
            ||
| 396 | * @return array of block objects  | 
            ||
| 397 | */  | 
            ||
| 398 | public function getAllBlocksByGroup($groupid, $asobject = true, $side = null, $visible = null, $orderby = 'b.weight,b.bid', $isactive = 1)  | 
            ||
| 399 |     { | 
            ||
| 400 | /* @var XoopsMySQLDatabase $db */  | 
            ||
| 401 | $db = XoopsDatabaseFactory::getDatabaseConnection();  | 
            ||
| 402 | $ret = array();  | 
            ||
| 403 | $sql = 'SELECT b.* ';  | 
            ||
| 404 |         if (!$asobject) { | 
            ||
| 405 | $sql = 'SELECT b.bid ';  | 
            ||
| 406 | }  | 
            ||
| 407 |         $sql .= 'FROM ' . $db->prefix('newblocks') . ' b LEFT JOIN ' . $db->prefix('group_permission') . " l ON l.gperm_itemid=b.bid WHERE gperm_name = 'block_read' AND gperm_modid = 1"; | 
            ||
| 408 |         if (is_array($groupid)) { | 
            ||
| 409 | $sql .= ' AND (l.gperm_groupid=' . $groupid[0] . '';  | 
            ||
| 410 | $size = count($groupid);  | 
            ||
| 411 |             if ($size > 1) { | 
            ||
| 412 |                 for ($i = 1; $i < $size; ++$i) { | 
            ||
| 413 | $sql .= ' OR l.gperm_groupid=' . $groupid[$i] . '';  | 
            ||
| 414 | }  | 
            ||
| 415 | }  | 
            ||
| 416 | $sql .= ')';  | 
            ||
| 417 |         } else { | 
            ||
| 418 | $sql .= ' AND l.gperm_groupid=' . $groupid . '';  | 
            ||
| 419 | }  | 
            ||
| 420 | $sql .= ' AND b.isactive=' . $isactive;  | 
            ||
| 421 |         if (isset($side)) { | 
            ||
| 422 | // get both sides in sidebox? (some themes need this)  | 
            ||
| 423 |             if ($side === XOOPS_SIDEBLOCK_BOTH) { | 
            ||
| 424 | $side = '(b.side=0 OR b.side=1)';  | 
            ||
| 425 |             } elseif ($side === XOOPS_CENTERBLOCK_ALL) { | 
            ||
| 426 | $side = '(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )';  | 
            ||
| 427 |             } elseif ($side === XOOPS_FOOTERBLOCK_ALL) { | 
            ||
| 428 | $side = '(b.side=10 OR b.side=11 OR b.side=12 )';  | 
            ||
| 429 |             } else { | 
            ||
| 430 | $side = 'b.side=' . $side;  | 
            ||
| 431 | }  | 
            ||
| 432 | $sql .= ' AND ' . $side;  | 
            ||
| 433 | }  | 
            ||
| 434 |         if (isset($visible)) { | 
            ||
| 435 | $sql .= " AND b.visible=$visible";  | 
            ||
| 436 | }  | 
            ||
| 437 | $sql .= " ORDER BY $orderby";  | 
            ||
| 438 | $result = $db->query($sql);  | 
            ||
| 439 |         if (!$db->isResultSet($result)) { | 
            ||
| 440 | throw new \RuntimeException(  | 
            ||
| 441 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR  | 
            ||
| 442 | );  | 
            ||
| 443 | }  | 
            ||
| 444 | $added = array();  | 
            ||
| 445 |         while (false !== ($myrow = $db->fetchArray($result))) { | 
            ||
| 446 |             if (!in_array($myrow['bid'], $added)) { | 
            ||
| 447 |                 if (!$asobject) { | 
            ||
| 448 | $ret[] = $myrow['bid'];  | 
            ||
| 449 |                 } else { | 
            ||
| 450 | $ret[] = new XoopsBlock($myrow);  | 
            ||
| 451 | }  | 
            ||
| 452 | $added[] = $myrow['bid'];  | 
            ||
| 453 | }  | 
            ||
| 454 | }  | 
            ||
| 455 | |||
| 456 | return $ret;  | 
            ||
| 457 | }  | 
            ||
| 458 | |||
| 459 | /**  | 
            ||
| 460 | * @param $groupid  | 
            ||
| 461 | *  | 
            ||
| 462 | * @return array  | 
            ||
| 463 | */  | 
            ||
| 464 | public function getBlockByPerm($groupid)  | 
            ||
| 465 |     { | 
            ||
| 466 |         if (isset($groupid)) { | 
            ||
| 467 |             $sql = 'SELECT DISTINCT gperm_itemid FROM ' . $this->db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1"; | 
            ||
| 468 |             if (is_array($groupid)) { | 
            ||
| 469 |                 $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')'; | 
            ||
| 470 |             } else { | 
            ||
| 471 |                 if ((int)$groupid > 0) { | 
            ||
| 472 | $sql .= ' AND gperm_groupid=' . (int)$groupid;  | 
            ||
| 473 | }  | 
            ||
| 474 | }  | 
            ||
| 475 | $result = $this->db->query($sql);  | 
            ||
| 476 |             if (!$this->db->isResultSet($result)) { | 
            ||
| 477 | throw new \RuntimeException(  | 
            ||
| 478 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR  | 
            ||
| 479 | );  | 
            ||
| 480 | }  | 
            ||
| 481 | $blockids = array();  | 
            ||
| 482 |             while (false !== ($myrow = $this->db->fetchArray($result))) { | 
            ||
| 483 | $blockids[] = $myrow['gperm_itemid'];  | 
            ||
| 484 | }  | 
            ||
| 485 |             if (empty($blockids)) { | 
            ||
| 486 | return $blockids;  | 
            ||
| 487 | }  | 
            ||
| 488 | |||
| 489 | return $blockids;  | 
            ||
| 490 | }  | 
            ||
| 491 | |||
| 492 | return null;  | 
            ||
| 493 | }  | 
            ||
| 494 | |||
| 495 | /**  | 
            ||
| 496 | * @param $groupid  | 
            ||
| 497 | * @param int $module_id  | 
            ||
| 498 | * @param bool $toponlyblock  | 
            ||
| 499 | * @param null $visible  | 
            ||
| 500 | * @param string $orderby  | 
            ||
| 501 | * @param int $isactive  | 
            ||
| 502 | *  | 
            ||
| 503 | * @return array  | 
            ||
| 504 | */  | 
            ||
| 505 | public function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)  | 
            ||
| 569 | }  | 
            ||
| 570 | |||
| 571 | /**  | 
            ||
| 572 | * @param int $module_id  | 
            ||
| 573 | * @param bool $toponlyblock  | 
            ||
| 574 | * @param null $visible  | 
            ||
| 575 | * @param string $orderby  | 
            ||
| 576 | * @param int $isactive  | 
            ||
| 577 | *  | 
            ||
| 578 | * @return array  | 
            ||
| 579 | */  | 
            ||
| 580 | public function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1)  | 
            ||
| 581 |     { | 
            ||
| 582 | $db = $GLOBALS['xoopsDB'];  | 
            ||
| 583 | $ret = array();  | 
            ||
| 584 | $bids = array();  | 
            ||
| 585 |         $sql  = 'SELECT DISTINCT(bid) from ' . $db->prefix('newblocks'); | 
            ||
| 586 | $result = $db->query($sql);  | 
            ||
| 587 |         if ($db->isResultSet($result)) { | 
            ||
| 588 |             while (false !== ($myrow = $db->fetchArray($result))) { | 
            ||
| 589 | $bids[] = $myrow['bid'];  | 
            ||
| 590 | }  | 
            ||
| 591 | }  | 
            ||
| 592 | |||
| 593 |         $sql     = 'SELECT DISTINCT(p.gperm_itemid) from ' . $db->prefix('group_permission') . ' p, ' . $db->prefix('groups') . " g WHERE g.groupid=p.gperm_groupid AND p.gperm_name='block_read'"; | 
            ||
| 594 | $grouped = array();  | 
            ||
| 595 | $result = $db->query($sql);  | 
            ||
| 596 |         if ($db->isResultSet($result)) { | 
            ||
| 597 |             while (false !== ($myrow = $db->fetchArray($result))) { | 
            ||
| 598 | $grouped[] = $myrow['gperm_itemid'];  | 
            ||
| 599 | }  | 
            ||
| 600 | }  | 
            ||
| 601 | |||
| 602 | $non_grouped = array_diff($bids, $grouped);  | 
            ||
| 603 |         if (!empty($non_grouped)) { | 
            ||
| 604 |             $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid'; | 
            ||
| 605 | $sql .= ' AND b.isactive=' . (int)$isactive;  | 
            ||
| 606 |             if (isset($visible)) { | 
            ||
| 607 | $sql .= ' AND b.visible=' . (int)$visible;  | 
            ||
| 608 | }  | 
            ||
| 609 |             if (!isset($module_id)) { | 
            ||
| 610 |             } elseif (!empty($module_id)) { | 
            ||
| 611 | $sql .= ' AND m.module_id IN (0,' . (int)$module_id;  | 
            ||
| 612 |                 if ($toponlyblock) { | 
            ||
| 613 | $sql .= ',-1';  | 
            ||
| 614 | }  | 
            ||
| 615 | $sql .= ')';  | 
            ||
| 616 |             } else { | 
            ||
| 617 |                 if ($toponlyblock) { | 
            ||
| 618 | $sql .= ' AND m.module_id IN (0,-1)';  | 
            ||
| 619 |                 } else { | 
            ||
| 620 | $sql .= ' AND m.module_id=0';  | 
            ||
| 621 | }  | 
            ||
| 622 | }  | 
            ||
| 623 |             $sql .= ' AND b.bid IN (' . implode(',', $non_grouped) . ')'; | 
            ||
| 624 | $sql .= ' ORDER BY ' . $orderby;  | 
            ||
| 625 | $result = $db->query($sql);  | 
            ||
| 626 |             if (!$db->isResultSet($result)) { | 
            ||
| 627 | throw new \RuntimeException(  | 
            ||
| 628 | \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR  | 
            ||
| 629 | );  | 
            ||
| 630 | }  | 
            ||
| 631 |             while (false !== ($myrow = $db->fetchArray($result))) { | 
            ||
| 632 | $block = new XoopsBlock($myrow);  | 
            ||
| 633 | $ret[$myrow['bid']] =& $block;  | 
            ||
| 634 | unset($block);  | 
            ||
| 635 | }  | 
            ||
| 636 | }  | 
            ||
| 637 | |||
| 638 | return $ret;  | 
            ||
| 639 | }  | 
            ||
| 640 | |||
| 641 | /**  | 
            ||
| 642 | * XoopsBlock::countSimilarBlocks()  | 
            ||
| 643 | *  | 
            ||
| 644 | * @param mixed $moduleId  | 
            ||
| 645 | * @param mixed $funcNum  | 
            ||
| 646 | * @param mixed $showFunc  | 
            ||
| 647 | * @return int  | 
            ||
| 648 | */  | 
            ||
| 649 | public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)  | 
            ||
| 675 | }  | 
            ||
| 676 | }  | 
            ||
| 677 |