| Total Complexity | 66 |
| Total Lines | 361 |
| 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) |
||
| 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) |
||
| 338 | { |
||
| 339 | $ret = array(); |
||
| 340 | $limit = $start = 0; |
||
| 341 | $sql = 'SELECT DISTINCT(b.bid), b.* FROM ' . $this->db->prefix('newblocks') . ' b LEFT JOIN ' . $this->db->prefix('block_module_link') . ' l ON b.bid=l.block_id'; |
||
| 342 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 343 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 344 | $limit = $criteria->getLimit(); |
||
| 345 | $start = $criteria->getStart(); |
||
| 346 | } |
||
| 347 | $result = $this->db->query($sql, $limit, $start); |
||
| 348 | if (!$this->db->isResultSet($result)) { |
||
| 349 | // \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 350 | return $ret; |
||
| 351 | } |
||
| 352 | |||
| 353 | if ($as_object) { |
||
| 354 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 355 | $object = $this->create(false); |
||
| 356 | $object->assignVars($myrow); |
||
| 357 | if ($id_as_key) { |
||
| 358 | $ret[$myrow[$this->keyName]] = $object; |
||
| 359 | } else { |
||
| 360 | $ret[] = $object; |
||
| 361 | } |
||
| 362 | unset($object); |
||
| 363 | } |
||
| 364 | } else { |
||
| 365 | $object = $this->create(false); |
||
| 366 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 367 | $object->assignVars($myrow); |
||
| 368 | if ($id_as_key) { |
||
| 369 | $ret[$myrow[$this->keyName]] = $object->getValues(array_keys($myrow)); |
||
| 370 | } else { |
||
| 371 | $ret[] = $object->getValues(array_keys($myrow)); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | unset($object); |
||
| 375 | } |
||
| 376 | |||
| 377 | return $ret; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * get all the blocks that match the supplied parameters |
||
| 382 | * |
||
| 383 | * @param int|int[] $groupid groupid (can be an array) |
||
| 384 | * @param bool $asobject |
||
| 385 | * @param int|string $side |
||
| 386 | * 0: sideblock - left |
||
| 387 | * 1: sideblock - right |
||
| 388 | * 2: sideblock - left and right |
||
| 389 | * 3: centerblock - left |
||
| 390 | * 4: centerblock - right |
||
| 391 | * 5: centerblock - center |
||
| 392 | * 6: centerblock - left, right, center |
||
| 393 | * @param $visible 0: not visible 1: visible |
||
| 394 | * @param string $orderby order of the blocks |
||
| 395 | * @param int $isactive |
||
| 396 | * |
||
| 397 | * @return array of block objects |
||
| 398 | */ |
||
| 399 | public function getAllBlocksByGroup($groupid, $asobject = true, $side = null, $visible = null, $orderby = 'b.weight,b.bid', $isactive = 1) |
||
| 400 | { |
||
| 401 | /* @var XoopsMySQLDatabase $db */ |
||
| 402 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 403 | $ret = array(); |
||
| 404 | $sql = 'SELECT b.* '; |
||
| 405 | if (!$asobject) { |
||
| 406 | $sql = 'SELECT b.bid '; |
||
| 407 | } |
||
| 408 | $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"; |
||
| 409 | if (is_array($groupid)) { |
||
| 410 | $sql .= ' AND (l.gperm_groupid=' . $groupid[0] . ''; |
||
| 411 | $size = count($groupid); |
||
| 412 | if ($size > 1) { |
||
| 413 | for ($i = 1; $i < $size; ++$i) { |
||
| 414 | $sql .= ' OR l.gperm_groupid=' . $groupid[$i] . ''; |
||
| 415 | } |
||
| 416 | } |
||
| 417 | $sql .= ')'; |
||
| 418 | } else { |
||
| 419 | $sql .= ' AND l.gperm_groupid=' . $groupid . ''; |
||
| 420 | } |
||
| 421 | $sql .= ' AND b.isactive=' . $isactive; |
||
| 422 | if (isset($side)) { |
||
| 423 | // get both sides in sidebox? (some themes need this) |
||
| 424 | if ($side === XOOPS_SIDEBLOCK_BOTH) { |
||
| 425 | $side = '(b.side=0 OR b.side=1)'; |
||
| 426 | } elseif ($side === XOOPS_CENTERBLOCK_ALL) { |
||
| 427 | $side = '(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )'; |
||
| 428 | } elseif ($side === XOOPS_FOOTERBLOCK_ALL) { |
||
| 429 | $side = '(b.side=10 OR b.side=11 OR b.side=12 )'; |
||
| 430 | } else { |
||
| 431 | $side = 'b.side=' . $side; |
||
| 432 | } |
||
| 433 | $sql .= ' AND ' . $side; |
||
| 434 | } |
||
| 435 | if (isset($visible)) { |
||
| 436 | $sql .= " AND b.visible=$visible"; |
||
| 437 | } |
||
| 438 | $sql .= " ORDER BY $orderby"; |
||
| 439 | $result = $db->query($sql); |
||
| 440 | if (!$db->isResultSet($result)) { |
||
| 441 | \trigger_error("Query Failed! SQL: $sql- Error: " . $db->error(), E_USER_ERROR); |
||
| 442 | } |
||
| 443 | $added = array(); |
||
| 444 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
| 445 | if (!in_array($myrow['bid'], $added)) { |
||
| 446 | if (!$asobject) { |
||
| 447 | $ret[] = $myrow['bid']; |
||
| 448 | } else { |
||
| 449 | $ret[] = new XoopsBlock($myrow); |
||
| 450 | } |
||
| 451 | $added[] = $myrow['bid']; |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | return $ret; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param $groupid |
||
| 460 | * |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function getBlockByPerm($groupid) |
||
| 464 | { |
||
| 465 | if (isset($groupid)) { |
||
| 466 | $sql = 'SELECT DISTINCT gperm_itemid FROM ' . $this->db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1"; |
||
| 467 | if (is_array($groupid)) { |
||
| 468 | $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')'; |
||
| 469 | } else { |
||
| 470 | if ((int)$groupid > 0) { |
||
| 471 | $sql .= ' AND gperm_groupid=' . (int)$groupid; |
||
| 472 | } |
||
| 473 | } |
||
| 474 | $result = $this->db->query($sql); |
||
| 475 | if (!$this->db->isResultSet($result)) { |
||
| 476 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 477 | } |
||
| 478 | $blockids = array(); |
||
| 479 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 480 | $blockids[] = $myrow['gperm_itemid']; |
||
| 481 | } |
||
| 482 | if (empty($blockids)) { |
||
| 483 | return $blockids; |
||
| 484 | } |
||
| 485 | |||
| 486 | return $blockids; |
||
| 487 | } |
||
| 488 | |||
| 489 | return null; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param $groupid |
||
| 494 | * @param int $module_id |
||
| 495 | * @param bool $toponlyblock |
||
| 496 | * @param null $visible |
||
| 497 | * @param string $orderby |
||
| 498 | * @param int $isactive |
||
| 499 | * |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | public function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1) |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @param int $module_id |
||
| 566 | * @param bool $toponlyblock |
||
| 567 | * @param null $visible |
||
| 568 | * @param string $orderby |
||
| 569 | * @param int $isactive |
||
| 570 | * |
||
| 571 | * @return array |
||
| 572 | */ |
||
| 573 | public function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1) |
||
| 574 | { |
||
| 575 | $db = $GLOBALS['xoopsDB']; |
||
| 576 | $ret = array(); |
||
| 577 | $bids = array(); |
||
| 578 | $sql = 'SELECT DISTINCT(bid) from ' . $db->prefix('newblocks'); |
||
| 579 | $result = $db->query($sql); |
||
| 580 | if ($db->isResultSet($result)) { |
||
| 581 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
| 582 | $bids[] = $myrow['bid']; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | |||
| 586 | $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'"; |
||
| 587 | $grouped = array(); |
||
| 588 | $result = $db->query($sql); |
||
| 589 | if ($db->isResultSet($result)) { |
||
| 590 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
| 591 | $grouped[] = $myrow['gperm_itemid']; |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | $non_grouped = array_diff($bids, $grouped); |
||
| 596 | if (!empty($non_grouped)) { |
||
| 597 | $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid'; |
||
| 598 | $sql .= ' AND b.isactive=' . (int)$isactive; |
||
| 599 | if (isset($visible)) { |
||
| 600 | $sql .= ' AND b.visible=' . (int)$visible; |
||
| 601 | } |
||
| 602 | if (!isset($module_id)) { |
||
| 603 | } elseif (!empty($module_id)) { |
||
| 604 | $sql .= ' AND m.module_id IN (0,' . (int)$module_id; |
||
| 605 | if ($toponlyblock) { |
||
| 606 | $sql .= ',-1'; |
||
| 607 | } |
||
| 608 | $sql .= ')'; |
||
| 609 | } else { |
||
| 610 | if ($toponlyblock) { |
||
| 611 | $sql .= ' AND m.module_id IN (0,-1)'; |
||
| 612 | } else { |
||
| 613 | $sql .= ' AND m.module_id=0'; |
||
| 614 | } |
||
| 615 | } |
||
| 616 | $sql .= ' AND b.bid IN (' . implode(',', $non_grouped) . ')'; |
||
| 617 | $sql .= ' ORDER BY ' . $orderby; |
||
| 618 | $result = $db->query($sql); |
||
| 619 | if (!$db->isResultSet($result)) { |
||
| 620 | \trigger_error("Query Failed! SQL: $sql- Error: " . $db->error(), E_USER_ERROR); |
||
| 621 | } |
||
| 622 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
| 623 | $block = new XoopsBlock($myrow); |
||
| 624 | $ret[$myrow['bid']] =& $block; |
||
| 625 | unset($block); |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | return $ret; |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * XoopsBlock::countSimilarBlocks() |
||
| 634 | * |
||
| 635 | * @param mixed $moduleId |
||
| 636 | * @param mixed $funcNum |
||
| 637 | * @param mixed $showFunc |
||
| 638 | * @return int |
||
| 639 | */ |
||
| 640 | public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null) |
||
| 664 | } |
||
| 665 | } |
||
| 666 |