| Conditions | 19 |
| Paths | 1104 |
| Total Lines | 116 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 35 | function publisher_items_columns_show($options) |
||
| 36 | { |
||
| 37 | // global $xoTheme; |
||
| 38 | $helper = Helper::getInstance(); |
||
| 39 | /** @var CategoryHandler $categoryHandler */ |
||
| 40 | $categoryHandler = $helper->getHandler('Category'); |
||
| 41 | /** @var ItemHandler $itemHandler */ |
||
| 42 | $itemHandler = $helper->getHandler('Item'); |
||
| 43 | //Column Settings |
||
| 44 | $optNumColumns = isset($options[0]) ? (int)$options[0] : '2'; |
||
| 45 | $selCategories = isset($options[1]) ? array_map('\intval', explode(',', $options[1])) : []; |
||
| 46 | $optCatItems = (int)$options[2]; |
||
| 47 | $optCatTruncate = isset($options[3]) ? (int)$options[3] : '0'; |
||
| 48 | |||
| 49 | $block = []; |
||
| 50 | $block['lang_reads'] = _MB_PUBLISHER_READS; |
||
| 51 | $block['lang_comments'] = _MB_PUBLISHER_COMMENTS; |
||
| 52 | $block['lang_readmore'] = _MB_PUBLISHER_READMORE; |
||
| 53 | |||
| 54 | $selCategoriesObj = []; |
||
| 55 | |||
| 56 | //get permited categories only once |
||
| 57 | $categoriesObj = $categoryHandler->getCategories(0, 0, -1); |
||
| 58 | |||
| 59 | //if not selected 'all', let's get the selected ones |
||
| 60 | if (in_array(0, $selCategories, true)) { |
||
| 61 | $selCategoriesObj = $categoriesObj; |
||
| 62 | } else { |
||
| 63 | foreach ($categoriesObj as $key => $value) { |
||
| 64 | if (in_array($key, $selCategories, true)) { |
||
| 65 | $selCategoriesObj[$key] = $value; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | unset($key, $value); |
||
| 70 | |||
| 71 | $ccount = count($selCategoriesObj); |
||
| 72 | |||
| 73 | if (0 === $ccount) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($ccount < $optNumColumns) { |
||
| 78 | $optNumColumns = $ccount; |
||
| 79 | } |
||
| 80 | |||
| 81 | $k = 0; |
||
| 82 | $columns = $mainItem = $subItem = []; |
||
| 83 | |||
| 84 | foreach ($selCategoriesObj as $categoryid => $mainItemCatObj) { |
||
| 85 | $categoryItemsObj = $itemHandler->getAllPublished($optCatItems, 0, $categoryid); |
||
| 86 | $scount = count($categoryItemsObj); |
||
| 87 | if ($scount > 0 && \is_array($categoryItemsObj)) { |
||
| 88 | reset($categoryItemsObj); |
||
| 89 | //First Item |
||
| 90 | $thisItem = array_values($categoryItemsObj)[0]; |
||
| 91 | |||
| 92 | $mainItem['item_title'] = $thisItem->getTitle(); |
||
| 93 | $mainItem['item_cleantitle'] = strip_tags($thisItem->getTitle()); |
||
| 94 | $mainItem['item_link'] = $thisItem->itemid(); |
||
| 95 | $mainItem['itemurl'] = $thisItem->getItemUrl(); |
||
| 96 | $mainItem['date'] = $thisItem->getDatesub(); |
||
| 97 | |||
| 98 | $mainImage = $thisItem->getMainImage(); |
||
| 99 | if (empty($mainImage['image_path'])) { |
||
| 100 | $mainImage['image_path'] = PUBLISHER_URL . '/assets/images/default_image.jpg'; |
||
| 101 | } |
||
| 102 | // check to see if GD function exist |
||
| 103 | $mainItem['item_image'] = $mainImage['image_path']; |
||
| 104 | if (!empty($mainImage['image_path']) && function_exists('imagecreatetruecolor')) { |
||
| 105 | $mainItem['item_image'] = PUBLISHER_URL . '/thumb.php?src=' . $mainImage['image_path'] . '&w=100'; |
||
| 106 | $mainItem['image_path'] = $mainImage['image_path']; |
||
| 107 | } |
||
| 108 | |||
| 109 | $mainItem['item_summary'] = $thisItem->getBlockSummary($optCatTruncate); |
||
| 110 | |||
| 111 | $mainItem['item_cat_name'] = $mainItemCatObj->name(); |
||
| 112 | $mainItem['item_cat_description'] = '' !== $mainItemCatObj->description() ? $mainItemCatObj->description() : $mainItemCatObj->name(); |
||
| 113 | $mainItem['item_cat_link'] = $mainItemCatObj->getCategoryLink(); |
||
| 114 | $mainItem['categoryurl'] = $mainItemCatObj->getCategoryUrl(); |
||
| 115 | |||
| 116 | //The Rest |
||
| 117 | if ($scount > 1) { |
||
| 118 | // while ((list($itemId, $thisItem) = each($categoryItemsObj)) !== false) { |
||
| 119 | foreach ($categoryItemsObj as $itemId => $thisItem) { |
||
| 120 | //TODO do I need to start with 2nd element? |
||
| 121 | $subItem['title'] = $thisItem->getTitle(); |
||
| 122 | $subItem['cleantitle'] = strip_tags($thisItem->getTitle()); |
||
| 123 | $subItem['link'] = $thisItem->getItemLink(); |
||
| 124 | $subItem['itemurl'] = $thisItem->getItemUrl(); |
||
| 125 | $subItem['summary'] = $thisItem->getBlockSummary($optCatTruncate); |
||
| 126 | $subItem['date'] = $thisItem->getDatesub(); |
||
| 127 | $mainItem['subitem'][] = $subItem; |
||
| 128 | unset($subItem); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $columns[$k][] = $mainItem; |
||
| 132 | unset($thisItem, $mainItem); |
||
| 133 | ++$k; |
||
| 134 | |||
| 135 | if ($k == $optNumColumns) { |
||
| 136 | $k = 0; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | unset($categoryid); |
||
| 141 | |||
| 142 | $block['template'] = $options[4]; |
||
| 143 | $block['columns'] = $columns; |
||
| 144 | $block['columnwidth'] = (int)(100 / $optNumColumns); |
||
| 145 | $block['display_datemainitem'] = $options[5] ?? ''; |
||
| 146 | $block['display_datesubitem'] = $options[6] ?? ''; |
||
| 147 | |||
| 148 | $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/' . PUBLISHER_DIRNAME . '/assets/css/publisher.css'); |
||
| 149 | |||
| 150 | return $block; |
||
| 151 | } |
||
| 202 |