publisher_items_new_show()   F
last analyzed

Complexity

Conditions 25
Paths 24

Size

Total Lines 149
Code Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 106
c 0
b 0
f 0
nc 24
nop 1
dl 0
loc 149
rs 3.3333

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\Constants;
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
 * @param $options
30
 *
31
 * @return array
32
 */
33
function publisher_items_new_show($options)
34
{
35
    $helper = Helper::getInstance();
36
    /** @var ItemHandler $itemHandler */
37
    $itemHandler = $helper->getHandler('Item');
38
39
    $selectedcatids = explode(',', $options[0]);
40
41
    $block   = [];
42
    $allcats = false;
43
    if (in_array(0, $selectedcatids, true)) {
44
        $allcats = true;
45
    }
46
47
    $sort  = $options[1];
48
    $order = Utility::getOrderBy($sort);
49
    $limit = $options[3];
50
    $start = 0;
51
    $image = $options[5];
52
53
    // creating the ITEM objects that belong to the selected category
54
    if ($allcats) {
55
        $criteria = null;
56
    } else {
57
        $criteria = new \CriteriaCompo();
58
        $criteria->add(new \Criteria('categoryid', '(' . $options[0] . ')', 'IN'));
59
    }
60
61
    $publisherIsAdmin = $helper->isUserAdmin();
62
    if (!$publisherIsAdmin) {
63
        if (null === $criteria) {
64
            $criteria = new \CriteriaCompo();
65
        }
66
        $criteriaDateSub = new \Criteria('datesub', time(), '<=');
67
        $criteria->add($criteriaDateSub);
68
    }
69
70
    //    $optCatItems    = (int)$options[2];
71
    //    $categoryid = -1;
72
    //    $categoryItemsObj = $itemHandler->getAllPublished($optCatItems, 0, $categoryid);
73
74
    $itemsObj = $itemHandler->getItems($limit, $start, [Constants::PUBLISHER_STATUS_PUBLISHED], -1, $sort, $order, '', true, $criteria, 'none');
75
76
    $totalitems = count($itemsObj);
77
    if ($totalitems > 0) {
78
        foreach ($itemsObj as $iValue) {
79
            $item                  = [];
80
            $item['itemurl']       = $iValue->getItemUrl();
81
            $item['link']          = $iValue->getItemLink(false, $options[4] ?? 65);
82
            $item['id']            = $iValue->itemid();
83
            $item['poster']        = $iValue->posterName(); // for make poster name linked, use getLinkedPosterName() instead of posterName()
84
            $item['categorylink']  = $iValue->getCategoryLink();
85
            $item['date']          = $iValue->getDatesub();
86
            $item['hits']          = $iValue->counter();
87
            $item['summary']       = $iValue->getBlockSummary(300, true); //show complete summary  but truncate to 300 if only body available
88
            $item['rating']        = $iValue->rating();
89
            $item['votes']         = $iValue->votes();
90
            $item['lang_fullitem'] = _MB_PUBLISHER_FULLITEM;
91
            $item['lang_poster']   = _MB_PUBLISHER_POSTEDBY;
92
            $item['lang_date']     = _MB_PUBLISHER_ON;
93
            $item['lang_category'] = _MB_PUBLISHER_CATEGORY;
94
            $item['lang_hits']     = _MB_PUBLISHER_TOTALHITS;
95
            $item['cancomment']    = $iValue->cancomment();
96
            $comments              = $iValue->comments();
97
            if ($comments > 0) {
98
                //shows 1 comment instead of 1 comm. if comments ==1
99
                //langugage file modified accordingly
100
                if (1 == $comments) {
101
                    $item['comment'] = '&nbsp;' . _MB_PUBLISHER_ONECOMMENT . '&nbsp;';
102
                } else {
103
                    $item['comment'] = '&nbsp;' . $comments . '&nbsp;' . _MB_PUBLISHER_COMMENTS . '&nbsp;';
104
                }
105
            } else {
106
                $item['comment'] = '&nbsp;' . _MB_PUBLISHER_NO_COMMENTS . '&nbsp;';
107
            }
108
109
            if ('article' === $image) {
110
                $item['image']      = PUBLISHER_URL . '/assets/images/default_image.jpg';
111
                $item['image_name'] = '';
112
                $images             = $iValue->getImages();
113
114
                if (empty($images['image_path'])) {
115
                    $images['image_path'] = PUBLISHER_URL . '/assets/images/default_image.jpg';
116
                }
117
                if (is_object($images['main'])) {
118
                    // check to see if GD function exist
119
                    if (function_exists('imagecreatetruecolor')) {
120
                        $item['image']      = PUBLISHER_URL . '/thumb.php?src=' . XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name') . '';
121
                        $item['image_path'] = XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name') . '';
122
                    } else {
123
                        $item['image'] = XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name');
124
                    }
125
                    $item['image_name'] = $images['main']->getVar('image_nicename');
126
                }
127
            } elseif ('category' === $image) {
128
                $item['image']      = $iValue->getCategoryImagePath();
129
                $item['image_name'] = $iValue->getCategoryName();
130
            } elseif ('avatar' === $image) {
131
                if ('0' == $iValue->uid()) {
132
                    $item['image'] = XOOPS_URL . '/uploads/avatars/blank.gif';
133
                    $images        = $iValue->getImages();
134
                    if (is_object($images['main'])) {
135
                        // check to see if GD function exist
136
                        if (function_exists('imagecreatetruecolor')) {
137
                            $item['image'] = PUBLISHER_URL . '/thumb.php?src=' . XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name') . '&amp;w=50';
138
                        } else {
139
                            $item['image'] = XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name');
140
                        }
141
                    }
142
                    // check to see if GD function exist
143
                } elseif (function_exists('imagecreatetruecolor')) {
144
                    $item['image'] = PUBLISHER_URL . '/thumb.php?src=' . XOOPS_URL . '/uploads/' . $iValue->posterAvatar() . '&amp;w=50';
145
                } else {
146
                    $item['image'] = XOOPS_URL . '/uploads/' . $iValue->posterAvatar();
147
                }
148
149
                $item['image_name'] = $iValue->posterName();
150
            }
151
152
            $item['title'] = $iValue->getTitle();
153
            $item['alt']   = strip_tags($iValue->getItemLink());
154
            if ('datesub' === $sort) {
155
                $item['new'] = $iValue->getDatesub();
156
            } elseif ('counter' === $sort) {
157
                $item['new'] = $iValue->counter();
158
            } elseif ('weight' === $sort) {
159
                $item['new'] = $iValue->weight();
160
            } elseif ('rating' === $sort) {
161
                $item['new'] = $iValue->rating();
162
            } elseif ('votes' === $sort) {
163
                $item['new'] = $iValue->votes();
164
            } elseif ('comments' === $sort) {
165
                $item['new'] = $iValue->comments();
166
            }
167
168
            $block['newitems'][] = $item;
169
        }
170
    }
171
172
    $block['show_order']    = $options[2];
173
    $block['show_summary']  = $options[6];
174
    $block['show_poster']   = $options[7];
175
    $block['show_date']     = $options[8];
176
    $block['show_category'] = $options[9];
177
    $block['show_hits']     = $options[10];
178
    $block['show_comment']  = $options[11];
179
    $block['show_rating']   = $options[12];
180
181
    return $block;
182
}
183
184
/**
185
 * @param $options
186
 *
187
 * @return string
188
 */
189
function publisher_items_new_edit($options)
190
{
191
    // require_once PUBLISHER_ROOT_PATH . '/class/blockform.php';
192
    xoops_load('XoopsFormLoader');
193
194
    $form = new BlockForm();
195
196
    $catEle   = new \XoopsFormLabel(_MB_PUBLISHER_SELECTCAT, Utility::createCategorySelect($options[0], 0, true, 'options[0]'));
197
    $orderEle = new \XoopsFormSelect(_MB_PUBLISHER_ORDER, 'options[1]', $options[1]);
198
    $orderEle->addOptionArray(
199
        [
200
            'datesub'  => _MB_PUBLISHER_DATE,
201
            'counter'  => _MB_PUBLISHER_HITS,
202
            'weight'   => _MB_PUBLISHER_WEIGHT,
203
            'rating'   => _MI_PUBLISHER_ORDERBY_RATING,
204
            'votes'    => _MI_PUBLISHER_ORDERBY_VOTES,
205
            'comments' => _MI_PUBLISHER_ORDERBY_COMMENTS,
206
        ]
207
    );
208
209
    $showEle  = new \XoopsFormRadioYN(_MB_PUBLISHER_ORDER_SHOW, 'options[2]', $options[2]);
210
    $dispEle  = new \XoopsFormText(_MB_PUBLISHER_DISP, 'options[3]', 10, 255, $options[3]);
211
    $charsEle = new \XoopsFormText(_MB_PUBLISHER_CHARS, 'options[4]', 10, 255, $options[4]);
212
213
    $imageEle = new \XoopsFormSelect(_MB_PUBLISHER_IMAGE_TO_DISPLAY, 'options[5]', $options[5]);
214
    $imageEle->addOptionArray(
215
        [
216
            'none'     => _NONE,
217
            'article'  => _MB_PUBLISHER_IMAGE_ARTICLE,
218
            'category' => _MB_PUBLISHER_IMAGE_CATEGORY,
219
            'avatar'   => _MB_PUBLISHER_IMAGE_AVATAR,
220
        ]
221
    );
222
    $showSummary  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_SUMMARY, 'options[6]', $options[6]);
223
    $showPoster   = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_POSTEDBY, 'options[7]', $options[7]);
224
    $showDate     = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_POSTTIME, 'options[8]', $options[8]);
225
    $showCategory = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_TOPICLINK, 'options[9]', $options[9]);
226
    $showHits     = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_READ, 'options[10]', $options[10]);
227
    $showComment  = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_COMMENT, 'options[11]', $options[11]);
228
    $showRating   = new \XoopsFormRadioYN(_MB_PUBLISHER_DISPLAY_RATING, 'options[12]', $options[12]);
229
230
    $form->addElement($catEle);
231
    $form->addElement($orderEle);
232
    $form->addElement($showEle);
233
    $form->addElement($dispEle);
234
    $form->addElement($charsEle);
235
    $form->addElement($imageEle);
236
    $form->addElement($showSummary);
237
    $form->addElement($showPoster);
238
    $form->addElement($showDate);
239
    $form->addElement($showCategory);
240
    $form->addElement($showHits);
241
    $form->addElement($showComment);
242
    $form->addElement($showRating);
243
244
    return $form->render();
245
}
246