Passed
Pull Request — master (#7)
by Michael
03:59
created

b_xoopsfaq_category_show()   B

Complexity

Conditions 10
Paths 27

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 36
c 1
b 0
f 0
nc 27
nop 1
dl 0
loc 55
rs 7.6666

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 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
 * Display/Edit Category Block
15
 *
16
 * @package   module\xoopsfaq\blocks
17
 * @author    ZySpec
18
 * @author    XOOPS Module Development Team
19
 * @copyright Copyright (c) 2001-2020 {@link https://xoops.org XOOPS Project}
20
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
21
 * @since     ::   1.25
22
 *
23
 * @see       \XoopsModules\Xoopsfaq\Helper
24
 * @see       \MyTextSanitizer
25
 */
26
27
use XoopsModules\Xoopsfaq;
28
29
/**
30
 * Show FAQ Categories Block
31
 *
32
 * Display the most recent FAQs added
33
 *
34
 * @param array $options for display parameters
35
 *                       [0] = only show cats with active FAQs
36
 *
37
 * @return array $block containing category titles and links
38
 */
39
function b_xoopsfaq_category_show($options)
40
{
41
    $moduleDirName = basename(dirname(__DIR__));
42
43
    $myts = \MyTextSanitizer::getInstance();
44
45
    /** @var Xoopsfaq\CategoryHandler $categoryHandler */ /** @var Xoopsfaq\ContentsHandler $contentsHandler */
46
    /** @var Xoopsfaq\Helper $helper */
47
    $helper          = \XoopsModules\Xoopsfaq\Helper::getInstance();
48
    $permHelper      = new \Xmf\Module\Helper\Permission($moduleDirName);
49
    $categoryHandler = $helper->getHandler('Category');
50
    $block           = [];
51
52
    // Get a list of all cats this user can access
53
    $catListArray = $categoryHandler->getList();
54
    $cTu          = $catsToUse = array_keys($catListArray);
55
    // Remove any cats this user doesn't have rights to view
56
    foreach ($cTu as $key => $thisCat) {
57
        if (false === $permHelper->checkPermission('viewcat', $thisCat)) {
58
            unset($catsToUse[$key]);
59
        }
60
    }
61
    // catsToUse contains all categories user has rights to view
62
63
    $criteria = new \CriteriaCompo();
64
    $criteria->setSort('category_order ASC, category_title');
65
    $criteria->order = 'ASC';
66
    if (!isset($options[0]) || 0 === (int)$options[0]) { // only show cats with FAQs
67
        $contentsHandler = $helper->getHandler('Contents');
68
        $faqCountArray   = $contentsHandler->getCategoriesIdsWithContent();
69
        if (is_array($faqCountArray) && !empty($faqCountArray)) {
70
            $catsToShow = array_intersect($catsToUse, array_keys($faqCountArray));
71
            $criteria->add(new \Criteria('category_id', '(' . implode(',', $catsToShow) . ')', 'IN'));
72
        } else {
73
            // there are no categories to show
74
            return $block;
75
        }
76
        unset($contentsHandler, $faqCountArray, $catsToShow);
77
    } else {
78
        $criteria->add(new \Criteria('category_id', '(' . implode(',', $catsToUse) . ')', 'IN'));
79
    }
80
    $fieldsArray = ['category_id', 'category_title'];
81
    $catObjArray = $categoryHandler->getAll($criteria, $fieldsArray);
82
    $catCount    = is_array($catObjArray) ? count($catObjArray) : 0;
0 ignored issues
show
introduced by
The condition is_array($catObjArray) is always true.
Loading history...
83
    if ($catCount > 0) {
84
        $block['title'] = _MB_XOOPSFAQ_CATTITLE;
85
        foreach ($catObjArray as $cId => $catObj) {
86
            $block['cat'][] = [
87
                'title' => $myts->displayTarea($catObj->getVar('category_title')),
88
                'link'  => $helper->url('index.php?cat_id=' . $cId),
89
            ];
90
        }
91
    }
92
    unset($catObjArray, $categoryHandler);
93
    return $block;
94
}
95
96
/**
97
 * Edit Recent FAQ Block preferences
98
 *
99
 * @param array $options for display parameters
100
 *                       [0] = only show cats with active FAQs
101
 *
102
 * @return string HTML to display to get input from user
103
 */
104
function b_xoopsfaq_category_edit($options)
105
{
106
    $ychck = (isset($options[0]) && ($options[0] > 0)) ? ' checked' : '';
107
    $nchck = !empty($ychck) ? '' : ' checked';
108
    $form  = '<div class="line140">'
109
             . _MB_XOOPSFAQ_SHOW_EMPTY
110
             . '&nbsp;<label for="r0">'
111
             . _NO
112
             . '</label><input type="radio" name="options[0]" id="r0" value="0"'
113
             . $nchck
114
             . '>&nbsp;<label for="r1">'
115
             . _YES
116
             . '</label><input type="radio" name="options[0]" id="r1" value="1"'
117
             . $ychck
118
             . '></div>'
119
             . "\n";
120
    return $form;
121
}
122