Passed
Branch master (61342d)
by Michael
01:52
created

publisher_items_spot_show()   D

Complexity

Conditions 19
Paths 48

Size

Total Lines 92
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 67
nc 48
nop 1
dl 0
loc 92
rs 4.5166
c 0
b 0
f 0

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
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       The XUUPS Project http://sourceforge.net/projects/xuups/
14
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @package         Publisher
16
 * @subpackage      Blocks
17
 * @since           1.0
18
 * @author          trabis <[email protected]>
19
 * @author          The SmartFactory <www.smartfactory.ca>
20
 */
21
22
use XoopsModules\Publisher;
23
24
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
require_once dirname(__DIR__) . '/include/common.php';
27
28
/**
29
 * @param $options
30
 *
31
 * @return array|bool
32
 */
33
function publisher_items_spot_show($options)
34
{
35
    //    global $xoTheme;
36
    /** @var Publisher\Helper $helper */
37
    $helper = Publisher\Helper::getInstance();
38
    /** @var Publisher\CategoryHandler $categoryHandler */
39
    $categoryHandler   = $helper->getHandler('Category');
40
    /** @var Publisher\ItemHandler $itemHandler */
41
    $itemHandler = $helper->getHandler('Item');
42
43
    $optDisplayLast    = $options[0];
44
    $optItemsCount     = $options[1];
45
    $optCategoryId     = $options[2];
46
    $selItems          = isset($options[3]) ? explode(',', $options[3]) : '';
47
    $optDisplayPoster  = $options[4];
48
    $optDisplayComment = $options[5];
49
    $optDisplayType    = $options[6];
50
    $optTruncate       = (int)$options[7];
51
    $optCatImage       = $options[8];
52
    if (0 == $optCategoryId) {
53
        $optCategoryId = -1;
54
    }
55
    $block = [];
56
    if (1 == $optDisplayLast) {
57
        $itemsObj   = $itemHandler->getAllPublished($optItemsCount, 0, $optCategoryId, $sort = 'datesub', $order = 'DESC', 'summary');
58
        $i          = 1;
59
        $itemsCount = count($itemsObj);
60
        if ($itemsObj) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $itemsObj of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61
            if (-1 != $optCategoryId && $optCatImage) {
62
                /** @var Publisher\Category $cat */
63
                $cat                     = $categoryHandler->get($optCategoryId);
64
                $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...
65
                $category['categoryurl'] = $cat->getCategoryUrl();
66
                if ('blank.png' !== $cat->getImage()) {
67
                    $category['image_path'] = Publisher\Utility::getImageDir('category', false) . $cat->getImage();
68
                } else {
69
                    $category['image_path'] = '';
70
                }
71
                $block['category'] = $category;
72
            }
73
            foreach ($itemsObj as $key => $thisItem) {
74
                $item = $thisItem->toArraySimple('default', 0, $optTruncate);
75
                if ($i < $itemsCount) {
76
                    $item['showline'] = true;
77
                } else {
78
                    $item['showline'] = false;
79
                }
80
                if ($optTruncate > 0) {
81
                    $block['truncate'] = true;
82
                }
83
                $block['items'][] = $item;
84
                ++$i;
85
            }
86
        }
87
    } else {
88
        $i = 1;
89
        if (is_array($selItems) && count($selItems) > 0) {
90
            foreach ($selItems as $itemId) {
91
                /** @var Publisher\Item $itemObj */
92
                $itemObj = $itemHandler->get($itemId);
0 ignored issues
show
Bug introduced by
$itemId of type string is incompatible with the type integer 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

92
                $itemObj = $itemHandler->get(/** @scrutinizer ignore-type */ $itemId);
Loading history...
93
                if (!$itemObj->notLoaded()) {
94
                    $item             = $itemObj->toArraySimple();
95
                    $item['who_when'] = sprintf(_MB_PUBLISHER_WHO_WHEN, $itemObj->posterName, $itemObj->getDatesub);
96
                    if ($i < $itemsCount) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $itemsCount seems to be never defined.
Loading history...
97
                        $item['showline'] = true;
98
                    } else {
99
                        $item['showline'] = false;
100
                    }
101
                    if ($optTruncate > 0) {
102
                        $block['truncate'] = true;
103
                        $item['summary']   = Publisher\Utility::truncateHtml($item['summary'], $optTruncate);
104
                    }
105
                    $block['items'][] = $item;
106
                    ++$i;
107
                }
108
            }
109
        }
110
    }
111
    if (!isset($block['items']) || 0 == count($block['items'])) {
112
        return false;
113
    }
114
    $block['lang_reads']           = _MB_PUBLISHER_READS;
115
    $block['lang_comments']        = _MB_PUBLISHER_COMMENTS;
116
    $block['lang_readmore']        = _MB_PUBLISHER_READMORE;
117
    $block['display_whowhen_link'] = $optDisplayPoster;
118
    $block['display_comment_link'] = $optDisplayComment;
119
    $block['display_type']         = $optDisplayType;
120
121
    $block['publisher_url'] = PUBLISHER_URL;
122
    $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/' . PUBLISHER_DIRNAME . '/assets/css/publisher.css');
123
124
    return $block;
125
}
126
127
/**
128
 * @param $options
129
 *
130
 * @return string
131
 */
132
function publisher_items_spot_edit($options)
133
{
134
    // require_once PUBLISHER_ROOT_PATH . '/class/blockform.php';
135
    xoops_load('XoopsFormLoader');
136
    $form     = new Publisher\BlockForm();
137
    $autoEle  = new \XoopsFormRadioYN(_MB_PUBLISHER_AUTO_LAST_ITEMS, 'options[0]', $options[0]);
138
    $countEle = new \XoopsFormText(_MB_PUBLISHER_LAST_ITEMS_COUNT, 'options[1]', 2, 255, $options[1]);
139
    $catEle   = new \XoopsFormLabel(_MB_PUBLISHER_SELECTCAT, Publisher\Utility::createCategorySelect($options[2], 0, true, 'options[2]'));
140
    /** @var Publisher\Helper $helper */
141
    $helper = Publisher\Helper::getInstance();
142
    /** @var Publisher\ItemHandler $itemHandler */
143
    $itemHandler = $helper->getHandler('Item');
144
    $criteria    = new \CriteriaCompo();
145
    $criteria->setSort('datesub');
146
    $criteria->setOrder('DESC');
147
    $itemsObj = $itemHandler->getList($criteria);
148
    $keys     = array_keys($itemsObj);
149
    unset($criteria);
150
    if (empty($options[3]) || (0 == $options[3])) {
151
        $selItems = isset($keys[0]) ? $keys[0] : 0;
152
    } else {
153
        $selItems = explode(',', $options[3]);
154
    }
155
    $itemEle = new \XoopsFormSelect(_MB_PUBLISHER_SELECT_ITEMS, 'options[3]', $selItems, 10, true);
156
    $itemEle->addOptionArray($itemsObj);
157
    $whoEle  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_WHO_AND_WHEN, 'options[4]', $options[4]);
158
    $comEle  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_COMMENTS, 'options[5]', $options[5]);
159
    $typeEle = new \XoopsFormSelect(_MB_PUBLISHER_DISPLAY_TYPE, 'options[6]', $options[6]);
160
    $typeEle->addOptionArray([
161
                                 'block'  => _MB_PUBLISHER_DISPLAY_TYPE_BLOCK,
162
                                 'bullet' => _MB_PUBLISHER_DISPLAY_TYPE_BULLET,
163
                             ]);
164
    $truncateEle = new \XoopsFormText(_MB_PUBLISHER_TRUNCATE, 'options[7]', 4, 255, $options[7]);
165
    $imageEle    = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_CATIMAGE, 'options[8]', $options[8]);
166
    $form->addElement($autoEle);
167
    $form->addElement($countEle);
168
    $form->addElement($catEle);
169
    $form->addElement($itemEle);
170
    $form->addElement($whoEle);
171
    $form->addElement($comEle);
172
    $form->addElement($typeEle);
173
    $form->addElement($truncateEle);
174
    $form->addElement($imageEle);
175
176
    return $form->render();
177
}
178