publisher_items_columns_show()   F
last analyzed

Complexity

Conditions 19
Paths 1104

Size

Total Lines 116
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 73
c 0
b 0
f 0
nc 1104
nop 1
dl 0
loc 116
rs 0.3499

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php declare(strict_types=1);
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright       XOOPS Project (https://xoops.org)
14
 * @license         https://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @since           1.0
16
 * @author          trabis <[email protected]>
17
 * @author          Bandit-x
18
 */
19
20
use XoopsModules\Publisher\BlockForm;
21
use XoopsModules\Publisher\CategoryHandler;
22
use XoopsModules\Publisher\Helper;
23
use XoopsModules\Publisher\ItemHandler;
24
use XoopsModules\Publisher\Utility;
25
26
require_once \dirname(__DIR__) . '/include/common.php';
27
28
/***
29
 * Function To Show Publisher Items From Categories In Their Own Columns
30
 *
31
 * @param array $options Block Options
32
 *
33
 * @return bool|array
34
 */
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'] . '&amp;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
}
152
153
/***
154
 * Edit Function For Multi-Column Category Items Display Block
155
 *
156
 * @param array $options Block Options
157
 *
158
 * @return string
159
 */
160
function publisher_items_columns_edit($options)
161
{
162
    // require_once PUBLISHER_ROOT_PATH . '/class/blockform.php';
163
    xoops_load('XoopsFormLoader');
164
165
    $form   = new BlockForm();
166
    $colEle = new \XoopsFormSelect(_MB_PUBLISHER_NUMBER_COLUMN_VIEW, 'options[0]', $options[0]);
167
    $colEle->addOptionArray(
168
        [
169
            '1' => 1,
170
            '2' => 2,
171
            '3' => 3,
172
            '4' => 4,
173
            '5' => 5,
174
        ]
175
    );
176
    $catEle = new \XoopsFormLabel(_MB_PUBLISHER_SELECTCAT, Utility::createCategorySelect($options[1], 0, true, 'options[1]'));
177
178
    $cItemsEle = new \XoopsFormText(_MB_PUBLISHER_NUMBER_ITEMS_CAT, 'options[2]', 4, 255, $options[2]);
179
180
    $truncateEle = new \XoopsFormText(_MB_PUBLISHER_TRUNCATE, 'options[3]', 4, 255, $options[3]);
181
182
    $tempEle = new \XoopsFormSelect(_MB_PUBLISHER_TEMPLATE, 'options[4]', $options[4]);
183
    $tempEle->addOptionArray(
184
        [
185
            'normal'   => _MB_PUBLISHER_TEMPLATE_NORMAL,
186
            'extended' => _MB_PUBLISHER_TEMPLATE_EXTENDED,
187
        ]
188
    );
189
    $dateMain = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_DATE_MAINITEM, 'options[5]', $options[5]);
190
    $dateSub  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_DATE_SUBITEM, 'options[6]', $options[6]);
191
192
    $form->addElement($colEle);
193
    $form->addElement($catEle);
194
    $form->addElement($cItemsEle);
195
    $form->addElement($truncateEle);
196
    $form->addElement($tempEle);
197
    $form->addElement($dateMain);
198
    $form->addElement($dateSub);
199
200
    return $form->render();
201
}
202