publisher_items_spot_show()   F
last analyzed

Complexity

Conditions 26
Paths 360

Size

Total Lines 150
Code Lines 121

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 26
eloc 121
c 2
b 1
f 0
nc 360
nop 1
dl 0
loc 150
rs 1.0666

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          The SmartFactory <www.smartfactory.ca>
18
 */
19
20
use XoopsModules\Publisher\BlockForm;
21
use XoopsModules\Publisher\Category;
22
use XoopsModules\Publisher\CategoryHandler;
23
use XoopsModules\Publisher\Helper;
24
use XoopsModules\Publisher\Item;
25
use XoopsModules\Publisher\ItemHandler;
26
use XoopsModules\Publisher\Utility;
27
28
require_once \dirname(__DIR__) . '/include/common.php';
29
30
/**
31
 * @param $options
32
 *
33
 * @return array|bool
34
 */
35
function publisher_items_spot_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
    xoops_loadLanguage('main', 'publisher');
44
45
    $optDisplayLast         = $options[0];
46
    $optItemsCount          = $options[1];
47
    $optCategoryId          = $options[2];
48
    $selItems               = isset($options[3]) ? explode(',', $options[3]) : '';
49
    $optDisplayPoster       = $options[4];
50
    $optDisplayComment      = $options[5];
51
    $optDisplayType         = $options[6];
52
    $optTruncate            = (int)$options[7];
53
    $optCatImage            = $options[8];
54
    $optSortOrder           = $options[9] ?? '';
55
    $optBtnDisplayMore      = $options[10] ?? '';
56
    $optDisplayReads        = $options[11] ?? '';
57
    $optdisplayitemimage    = $options[12] ?? '';
58
    $optdisplaywhenlink     = $options[13] ?? '';
59
    $optdisplaycategorylink = $options[14] ?? '';
60
    $optdisplayadminlink    = $options[15] ?? '';
61
    $optdisplayreadmore     = $options[16] ?? '';
62
63
    if (0 == $optCategoryId) {
64
        $optCategoryId = -1;
65
    }
66
    $block = [];
67
    if (1 == $optDisplayLast) {
68
        switch ($optSortOrder) {
69
            case 'title':
70
                $sort  = 'title';
71
                $order = 'ASC';
72
                break;
73
            case 'date':
74
                $sort  = 'datesub';
75
                $order = 'DESC';
76
                break;
77
            case 'counter':
78
                $sort  = 'counter';
79
                $order = 'DESC';
80
                break;
81
            case 'rating':
82
                $sort  = 'rating';
83
                $order = 'DESC';
84
                break;
85
            case 'votes':
86
                $sort  = 'votes';
87
                $order = 'DESC';
88
                break;
89
            case 'comments':
90
                $sort  = 'comments';
91
                $order = 'DESC';
92
                break;
93
            default:
94
                $sort  = 'weight';
95
                $order = 'ASC';
96
                break;
97
        }
98
        $itemsObj   = $itemHandler->getAllPublished($optItemsCount, 0, $optCategoryId, $sort, $order, 'summary');
99
        $i          = 1;
100
        $itemsCount = count($itemsObj);
101
        if ($itemsObj) {
102
            if (-1 != $optCategoryId) {
103
                /** @var Category $cat */
104
                $cat                     = $categoryHandler->get($optCategoryId);
105
                $category['name']        = $cat->name;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$category was never initialized. Although not strictly required by PHP, it is generally a good practice to add $category = array(); before regardless.
Loading history...
106
                $category['categoryurl'] = $cat->getCategoryUrl();
107
                if ('blank.png' !== $cat->getImage()) {
108
                    $category['image_path'] = Utility::getImageDir('category', false) . $cat->getImage();
109
                } else {
110
                    $category['image_path'] = '';
111
                }
112
                $block['category'] = $category;
113
            } else {
114
                $block['category']['categoryurl'] = XOOPS_URL . '/modules/' . PUBLISHER_DIRNAME;
115
            }
116
            foreach ($itemsObj as $key => $thisItem) {
117
                $item = $thisItem->toArraySimple('default', 0, $optTruncate);
118
                if ($i < $itemsCount) {
119
                    $item['showline'] = true;
120
                } else {
121
                    $item['showline'] = false;
122
                }
123
                if ($optTruncate > 0) {
124
                    $block['truncate'] = true;
125
                }
126
                $block['items'][] = $item;
127
                ++$i;
128
            }
129
        }
130
    } else {
131
        $i = 1;
132
        if ($selItems && is_array($selItems)) {
133
            $itemsCount = count($selItems);
134
            foreach ($selItems as $itemId) {
135
                /** @var Item $itemObj */
136
                $itemObj = $itemHandler->get($itemId);
0 ignored issues
show
Bug introduced by
$itemId of type string is incompatible with the type integer|null expected by parameter $id of XoopsModules\Publisher\ItemHandler::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
                $itemObj = $itemHandler->get(/** @scrutinizer ignore-type */ $itemId);
Loading history...
137
                if (null !== $itemObj && !$itemObj->notLoaded()) {
138
                    $item             = $itemObj->toArraySimple();
139
                    $item['who_when'] = sprintf(_MB_PUBLISHER_WHO_WHEN, $item['who'], $item['when']);
140
                    if ($i < $itemsCount) {
141
                        $item['showline'] = true;
142
                    } else {
143
                        $item['showline'] = false;
144
                    }
145
                    if ($optTruncate > 0) {
146
                        $block['truncate'] = true;
147
                        $item['summary']   = Utility::truncateHtml($item['summary'], $optTruncate);
148
                    }
149
                    $block['items'][] = $item;
150
                    ++$i;
151
                }
152
            }
153
        }
154
    }
155
    if (!isset($block['items']) || 0 == count($block['items'])) {
156
        return false;
157
    }
158
    $block['lang_reads']    = _MB_PUBLISHER_READS;
159
    $block['lang_comments'] = _MB_PUBLISHER_COMMENTS;
160
    $block['lang_readmore'] = _MB_PUBLISHER_READMORE;
161
    $block['lang_poster']   = _MB_PUBLISHER_POSTEDBY;
162
    $block['lang_date']     = _MB_PUBLISHER_ON;
163
    $block['lang_category'] = _MB_PUBLISHER_CATEGORY;
164
165
    $block['display_whowhen_link'] = $optDisplayPoster;
166
    $block['display_who_link']     = $optDisplayPoster;
167
    $block['display_comment_link'] = $optDisplayComment;
168
    $block['display_type']         = $optDisplayType;
169
    $block['display_reads']        = $optDisplayReads;
170
    $block['display_cat_image']    = $optCatImage;
171
    $block['display_item_image']   = $optdisplayitemimage;
172
    $block['display_when_link']    = $optdisplaywhenlink;
173
    $block['display_categorylink'] = $optdisplaycategorylink;
174
    $block['display_adminlink']    = $optdisplayadminlink;
175
    $block['display_readmore']     = $optdisplayreadmore;
176
177
    if ($optBtnDisplayMore) {
178
        $block['lang_displaymore'] = _MB_PUBLISHER_MORE_ITEMS;
179
    }
180
181
    $block['publisher_url'] = PUBLISHER_URL;
182
    $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/' . PUBLISHER_DIRNAME . '/assets/css/' . PUBLISHER_DIRNAME . '.css');
183
184
    return $block;
185
}
186
187
/**
188
 * @param $options
189
 *
190
 * @return string
191
 */
192
function publisher_items_spot_edit($options)
193
{
194
    // require_once PUBLISHER_ROOT_PATH . '/class/blockform.php';
195
    xoops_load('XoopsFormLoader');
196
    $form     = new BlockForm();
197
    $autoEle  = new \XoopsFormRadioYN(_MB_PUBLISHER_AUTO_LAST_ITEMS, 'options[0]', $options[0]);
198
    $countEle = new \XoopsFormText(_MB_PUBLISHER_LAST_ITEMS_COUNT, 'options[1]', 2, 255, $options[1]);
199
    $catEle   = new \XoopsFormLabel(_MB_PUBLISHER_SELECTCAT, Utility::createCategorySelect($options[2], 0, true, 'options[2]', false));
200
    $helper   = Helper::getInstance();
201
    /** @var ItemHandler $itemHandler */
202
    $itemHandler = $helper->getHandler('Item');
203
    $criteria    = new \CriteriaCompo();
204
    $criteria->setSort('datesub');
205
    $criteria->setOrder('DESC');
206
    $itemsObj = $itemHandler->getList($criteria);
207
    $keys     = array_keys($itemsObj);
208
    unset($criteria);
209
    if (empty($options[3]) || (0 == $options[3])) {
210
        $selItems = $keys[0] ?? 0;
211
    } else {
212
        $selItems = explode(',', $options[3]);
213
    }
214
    $itemEle = new \XoopsFormSelect(_MB_PUBLISHER_SELECT_ITEMS, 'options[3]', $selItems, 10, true);
215
    $itemEle->addOptionArray($itemsObj);
216
    $whoEle  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_POSTEDBY, 'options[4]', $options[4]);
217
    $comEle  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_COMMENTS, 'options[5]', $options[5]);
218
    $typeEle = new \XoopsFormSelect(_MB_PUBLISHER_DISPLAY_TYPE, 'options[6]', $options[6]);
219
    $typeEle->addOptionArray(
220
        [
221
            'block'  => _MB_PUBLISHER_DISPLAY_TYPE_BLOCK,
222
            'bullet' => _MB_PUBLISHER_DISPLAY_TYPE_BULLET,
223
        ]
224
    );
225
    $truncateEle = new \XoopsFormText(_MB_PUBLISHER_TRUNCATE, 'options[7]', 4, 255, $options[7]);
226
    $imageEle    = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_CATIMAGE, 'options[8]', $options[8]);
227
    $sortEle     = new \XoopsFormSelect(_MI_PUBLISHER_ORDERBY, 'options[9]', $options[9]);
228
    $sortEle->addOptionArray(
229
        [
230
            'title'    => _MI_PUBLISHER_ORDERBY_TITLE,
231
            'date'     => _MI_PUBLISHER_ORDERBY_DATE,
232
            'counter'  => _MI_PUBLISHER_ORDERBY_HITS,
233
            'rating'   => _MI_PUBLISHER_ORDERBY_RATING,
234
            'votes'    => _MI_PUBLISHER_ORDERBY_VOTES,
235
            'comments' => _MI_PUBLISHER_ORDERBY_COMMENTS,
236
            'weight'   => _MI_PUBLISHER_ORDERBY_WEIGHT,
237
        ]
238
    );
239
    $dispMoreEle   = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_MORELINK, 'options[10]', $options[10]);
240
    $readsEle      = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_READ, 'options[11]', $options[11]);
241
    $dispImage     = new \XoopsFormRadioYN(_MB_PUBLISHER_IMGDISPLAY, 'options[12]', $options[12]);
242
    $dispDate      = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_POSTTIME, 'options[13]', $options[13]);
243
    $dispCategory  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_TOPICLINK, 'options[14]', $options[14]);
244
    $dispAdminlink = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_ADMINLINK, 'options[15]', $options[15]);
245
    $dispReadmore  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_READ_FULLITEM, 'options[16]', $options[16]);
246
247
    $form->addElement($autoEle);
248
    $form->addElement($countEle);
249
    $form->addElement($catEle);
250
    $form->addElement($itemEle);
251
    $form->addElement($whoEle);
252
    $form->addElement($comEle);
253
    $form->addElement($typeEle);
254
    $form->addElement($truncateEle);
255
    $form->addElement($imageEle);
256
    $form->addElement($sortEle);
257
    $form->addElement($dispMoreEle);
258
    $form->addElement($readsEle);
259
    $form->addElement($dispImage);
260
    $form->addElement($dispDate);
261
    $form->addElement($dispCategory);
262
    $form->addElement($dispAdminlink);
263
    $form->addElement($dispReadmore);
264
265
    return $form->render();
266
}
267