b_xoopsfaq_recent_show()   C
last analyzed

Complexity

Conditions 13
Paths 132

Size

Total Lines 72
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 49
c 3
b 0
f 0
nc 132
nop 1
dl 0
loc 72
rs 6.35

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 of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * Recent Term Block file
15
 *
16
 * @author    ZySpec
17
 * @author    XOOPS Module Development Team
18
 * @copyright Copyright (c) 2001-2020 {@link https://xoops.org XOOPS Project}
19
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
20
 * @since     ::   1.25
21
 *
22
 * @see       Xmf\Module\Helper
23
 * @see       MyTextSanitizer
24
 */
25
26
use Xmf\Module\Helper\Permission;
27
use XoopsModules\Xoopsfaq\{
28
    Constants,
29
    CategoryHandler,
30
    ContentsHandler,
31
    Helper
32
};
33
34
/**
35
 * Display the most recent FAQs added
36
 *
37
 * @param array $options for display parameters
38
 *                       [0] = num of FAQs to show
39
 *                       [1] = num chars in answer(s) to show
40
 *                       [2] = display date added
41
 *                       [3] = FAQ categories to use
42
 *
43
 * @return array contains recent FAQ(s) parameters
44
 */
45
function b_xoopsfaq_recent_show(array $options): array
46
{
47
    $moduleDirName = \basename(\dirname(__DIR__));
48
49
    $myts = \MyTextSanitizer::getInstance();
50
51
    /** @var Helper $helper */
52
    $helper = Helper::getInstance();
53
    /** @var ContentsHandler $contentsHandler */
54
    $contentsHandler = $helper->getHandler('Contents');
55
    $permHelper      = new Permission($moduleDirName);
56
    $block           = [];
57
58
    $criteria = new \CriteriaCompo();
59
    $criteria->add(new \Criteria('contents_active', Constants::ACTIVE, '='));
60
    $criteria->setSort('contents_publish DESC, contents_weight');
61
    $criteria->order = 'ASC';
62
    $criteria->setLimit($options[0]);
63
64
    $options[3] ??= [0];
65
    $cTu        = $catsToUse = (false === mb_strpos((string)$options[3], ',')) ? (array)$options[3] : explode(',', (string)$options[3]);
66
    if (in_array(0, $catsToUse, true) || empty($catsToUse)) {
67
        // Get a list of all cats
68
        /** @var CategoryHandler $categoryHandler */
69
        $categoryHandler = $helper->getHandler('Category');
70
        $catListArray    = $categoryHandler->getList();
71
        $cTu             = $catsToUse = array_keys($catListArray);
72
    }
73
    // Remove any cats this user doesn't have rights to view
74
    foreach ($cTu as $key => $thisCat) {
75
        if (false === $permHelper->checkPermission('viewcat', $thisCat)) {
76
            unset($catsToUse[$key]);
77
        }
78
    }
79
    if (!empty($catsToUse)) {
80
        $criteria->add(new \Criteria('contents_cid', '(' . implode(',', $catsToUse) . ')', 'IN'));
81
    } else {
82
        return $block;
83
    }
84
85
    $fieldsArray = ['contents_cid', 'contents_title', 'contents_contents'];
86
    $faqObjArray = $contentsHandler->getAll($criteria, $fieldsArray);
87
    $faqCount    = is_array($faqObjArray) ? count($faqObjArray) : 0;
0 ignored issues
show
introduced by
The condition is_array($faqObjArray) is always true.
Loading history...
88
    if ($faqCount > 0) {
89
        $block['title']     = _MB_XOOPSFAQ_RECENT_TITLE;
90
        $block['show_date'] = (isset($options[2]) && $options[2] > 0) ? 1 : 0;
91
        foreach ($faqObjArray as $fId => $faqObj) {
92
            $faqTitle = $myts->displayTarea($faqObj->getVar('contents_title'));
93
            if (!empty($options[1])) {
94
                $txtAns = strip_tags((string)$faqObj->getVar('contents_contents')); // get rid of html for block
95
                $faqAns = $myts->displayTarea(xoops_substr($txtAns, 0, $options[1]), 0, 0, 0, 0, 0);
96
            } else {
97
                $faqAns = $myts->displayTarea(
98
                    $faqObj->getVar('contents_contents'),
99
                    (int)$faqObj->getVar('dohtml'),
100
                    (int)$faqObj->getVar('dosmiley'),
101
                    (int)$faqObj->getVar('doxcode'),
102
                    1,
103
                    (int)$faqObj->getVar('dobr')
104
                );
105
            }
106
            $block['faq'][] = [
107
                'title'     => $faqTitle,
108
                'ans'       => $faqAns,
109
                'published' => $faqObj->getPublished(_SHORTDATESTRING),
110
                'id'        => $faqObj->getVar('contents_id'),
111
                'cid'       => $faqObj->getVar('contents_cid'),
112
            ];
113
        }
114
    }
115
116
    return $block;
117
}
118
119
/**
120
 * Edit the most recent FAQs block parameters
121
 *
122
 * @param array $options for display parameters
123
 *                       [0] = num of FAQs to show
124
 *                       [1] = num chars in answer(s) to show
125
 *                       [2] = display date added
126
 *                       [3] = FAQ categories to use
127
 *
128
 * @return string HTML to display to get input from user
129
 */
130
function b_xoopsfaq_recent_edit(array $options): string
131
{
132
    $moduleDirName = \basename(\dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
133
    xoops_load('XoopsFormSelect');
134
135
    /** @var Helper $helper */
136
    $helper = Helper::getInstance();
137
    /** @var CategoryHandler $categoryHandler */
138
    $categoryHandler = $helper->getHandler('Category');
139
140
    $catList     = $categoryHandler->getList();
141
    $optionArray = array_merge([0 => _MB_XOOPSFAQ_ALL_CATS], $catList);
142
    $formSelect  = new \XoopsFormSelect('category', 'options[3]', null, 3, true);
143
    $formSelect->addOptionArray($optionArray);
144
    $selOptions = (false === mb_strpos((string)$options[3], ',')) ? $options[3] : explode(',', (string)$options[3]);
145
    $formSelect->setValue($selOptions);
146
    $selectCat = $formSelect->render();
147
148
    $ychck = (isset($options[2]) && ($options[2] > 0)) ? ' checked' : '';
149
    $nchck = !empty($ychck) ? '' : ' checked';
150
151
    $form = '<div class="line140">'
152
            . _MB_XOOPSFAQ_NUM_FAQS
153
            . '&nbsp;'
154
            . '<input type="number" name="options[0]" value="'
155
            . $options[0]
156
            . '" style="width: 5em;" min="0" class="right"><br>'
157
            . _MB_XOOPSFAQ_CHARS
158
            . '&nbsp;<input type="number" name="options[1]" value="'
159
            . $options[1]
160
            . '" style="width: 5em;" min="0" class="right">&nbsp;'
161
            . _MB_XOOPSFAQ_LENGTH
162
            . '<br>'
163
            . _MB_XOOPSFAQ_SHOW_DATE
164
            . '&nbsp;'
165
            . '<label for="r0">'
166
            . _NO
167
            . '</label>'
168
            . '<input type="radio" name="options[2]" id="r0" value="0"'
169
            . $nchck
170
            . '>&nbsp;'
171
            . '<label for="r1">'
172
            . _YES
173
            . '</label>'
174
            . '<input type="radio" name="options[2]" id="r1" value="1"'
175
            . $ychck
176
            . '>'
177
            . '<br><br>'
178
            . _MB_XOOPSFAQ_ALL_CATS_INTRO
179
            . '&nbsp;&nbsp;'
180
            . $selectCat
181
            . '</div>';
182
183
    return $form;
184
}
185