Total Complexity | 66 |
Total Lines | 360 |
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 | } |
||
351 | |||
352 | if ($as_object) { |
||
353 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
354 | $object = $this->create(false); |
||
355 | $object->assignVars($myrow); |
||
356 | if ($id_as_key) { |
||
357 | $ret[$myrow[$this->keyName]] = $object; |
||
358 | } else { |
||
359 | $ret[] = $object; |
||
360 | } |
||
361 | unset($object); |
||
362 | } |
||
363 | } else { |
||
364 | $object = $this->create(false); |
||
365 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
366 | $object->assignVars($myrow); |
||
367 | if ($id_as_key) { |
||
368 | $ret[$myrow[$this->keyName]] = $object->getValues(array_keys($myrow)); |
||
369 | } else { |
||
370 | $ret[] = $object->getValues(array_keys($myrow)); |
||
371 | } |
||
372 | } |
||
373 | unset($object); |
||
374 | } |
||
375 | |||
376 | return $ret; |
||
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 | \trigger_error("Query Failed! SQL: $sql- Error: " . $db->error(), E_USER_ERROR); |
||
441 | } |
||
442 | $added = array(); |
||
443 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
444 | if (!in_array($myrow['bid'], $added)) { |
||
445 | if (!$asobject) { |
||
446 | $ret[] = $myrow['bid']; |
||
447 | } else { |
||
448 | $ret[] = new XoopsBlock($myrow); |
||
449 | } |
||
450 | $added[] = $myrow['bid']; |
||
451 | } |
||
452 | } |
||
453 | |||
454 | return $ret; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * @param $groupid |
||
459 | * |
||
460 | * @return array |
||
461 | */ |
||
462 | public function getBlockByPerm($groupid) |
||
463 | { |
||
464 | if (isset($groupid)) { |
||
465 | $sql = 'SELECT DISTINCT gperm_itemid FROM ' . $this->db->prefix('group_permission') . " WHERE gperm_name = 'block_read' AND gperm_modid = 1"; |
||
466 | if (is_array($groupid)) { |
||
467 | $sql .= ' AND gperm_groupid IN (' . implode(',', $groupid) . ')'; |
||
468 | } else { |
||
469 | if ((int)$groupid > 0) { |
||
470 | $sql .= ' AND gperm_groupid=' . (int)$groupid; |
||
471 | } |
||
472 | } |
||
473 | $result = $this->db->query($sql); |
||
474 | if (!$this->db->isResultSet($result)) { |
||
475 | \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
476 | } |
||
477 | $blockids = array(); |
||
478 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
479 | $blockids[] = $myrow['gperm_itemid']; |
||
480 | } |
||
481 | if (empty($blockids)) { |
||
482 | return $blockids; |
||
483 | } |
||
484 | |||
485 | return $blockids; |
||
486 | } |
||
487 | |||
488 | return null; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * @param $groupid |
||
493 | * @param int $module_id |
||
494 | * @param bool $toponlyblock |
||
495 | * @param null $visible |
||
496 | * @param string $orderby |
||
497 | * @param int $isactive |
||
498 | * |
||
499 | * @return array |
||
500 | */ |
||
501 | public function getAllByGroupModule($groupid, $module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1) |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * @param int $module_id |
||
565 | * @param bool $toponlyblock |
||
566 | * @param null $visible |
||
567 | * @param string $orderby |
||
568 | * @param int $isactive |
||
569 | * |
||
570 | * @return array |
||
571 | */ |
||
572 | public function getNonGroupedBlocks($module_id = 0, $toponlyblock = false, $visible = null, $orderby = 'b.weight, m.block_id', $isactive = 1) |
||
573 | { |
||
574 | $db = $GLOBALS['xoopsDB']; |
||
575 | $ret = array(); |
||
576 | $bids = array(); |
||
577 | $sql = 'SELECT DISTINCT(bid) from ' . $db->prefix('newblocks'); |
||
578 | $result = $db->query($sql); |
||
579 | if (!$db->isResultSet($result)) { |
||
580 | \trigger_error("Query Failed! SQL: $sql- Error: " . $db->error(), E_USER_ERROR); |
||
581 | } |
||
582 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
583 | $bids[] = $myrow['bid']; |
||
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 | \trigger_error("Query Failed! SQL: $sql- Error: " . $db->error(), E_USER_ERROR); |
||
591 | } |
||
592 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
593 | $grouped[] = $myrow['gperm_itemid']; |
||
594 | } |
||
595 | |||
596 | $non_grouped = array_diff($bids, $grouped); |
||
597 | if (!empty($non_grouped)) { |
||
598 | $sql = 'SELECT b.* FROM ' . $db->prefix('newblocks') . ' b, ' . $db->prefix('block_module_link') . ' m WHERE m.block_id=b.bid'; |
||
599 | $sql .= ' AND b.isactive=' . (int)$isactive; |
||
600 | if (isset($visible)) { |
||
601 | $sql .= ' AND b.visible=' . (int)$visible; |
||
602 | } |
||
603 | if (!isset($module_id)) { |
||
604 | } elseif (!empty($module_id)) { |
||
605 | $sql .= ' AND m.module_id IN (0,' . (int)$module_id; |
||
606 | if ($toponlyblock) { |
||
607 | $sql .= ',-1'; |
||
608 | } |
||
609 | $sql .= ')'; |
||
610 | } else { |
||
611 | if ($toponlyblock) { |
||
612 | $sql .= ' AND m.module_id IN (0,-1)'; |
||
613 | } else { |
||
614 | $sql .= ' AND m.module_id=0'; |
||
615 | } |
||
616 | } |
||
617 | $sql .= ' AND b.bid IN (' . implode(',', $non_grouped) . ')'; |
||
618 | $sql .= ' ORDER BY ' . $orderby; |
||
619 | $result = $db->query($sql); |
||
620 | if (!$db->isResultSet($result)) { |
||
621 | \trigger_error("Query Failed! SQL: $sql- Error: " . $db->error(), E_USER_ERROR); |
||
622 | } |
||
623 | while (false !== ($myrow = $db->fetchArray($result))) { |
||
624 | $block = new XoopsBlock($myrow); |
||
625 | $ret[$myrow['bid']] =& $block; |
||
626 | unset($block); |
||
627 | } |
||
628 | } |
||
629 | |||
630 | return $ret; |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * XoopsBlock::countSimilarBlocks() |
||
635 | * |
||
636 | * @param mixed $moduleId |
||
637 | * @param mixed $funcNum |
||
638 | * @param mixed $showFunc |
||
639 | * @return int |
||
640 | */ |
||
641 | public function countSimilarBlocks($moduleId, $funcNum, $showFunc = null) |
||
663 | } |
||
664 | } |
||
665 |