CategoryHandler::renderAdminListing()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 53
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 40
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 53
rs 9.28

How to fix   Long Method   

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
namespace XoopsModules\Xoopsfaq;
4
5
/*
6
 You may not change or alter any portion of this comment or credits of
7
 supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit
9
 authors.
10
11
 This program is distributed in the hope that it will be useful, but
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 */
15
16
/**
17
 * XOOPS FAQ Category & Category Handler Class Definitions
18
 *
19
 * @author    John Neill
20
 * @author    XOOPS Module Development Team
21
 * @copyright Copyright (c) 2001-2017 {@link https://xoops.org XOOPS Project}
22
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
23
 * @since     ::   1.23
24
 */
25
26
use Xmf\Module\Admin;
27
28
/**
29
 * CategoryHandler
30
 *
31
 * @author   ::    John Neill
32
 * @copyright:: Copyright (c) 2009
33
 */
34
final class CategoryHandler extends \XoopsPersistableObjectHandler
35
{
36
    /**
37
     * Constructor
38
     *
39
     * @param mixed $db
40
     */
41
    public function __construct(\XoopsDatabase $db = null)
42
    {
43
        parent::__construct($db, 'xoopsfaq_categories', Category::class, 'category_id', 'category_title');
44
    }
45
46
    /**
47
     * CategoryHandler::getObj()
48
     *
49
     * @param \CriteriaElement|string|null $sort order ('id', order', or 'title') - default: id
50
     *
51
     * @return array Category
52
     */
53
    public function getObj($sort = null): array
54
    {
55
        $obj = [];
56
        if ((null !== $sort) && (!$sort instanceof \CriteriaElement)) {
57
            $criteria        = new \CriteriaCompo();
58
            $obj['count']    = $this->getCount($criteria);
59
            $criteria->order = 'ASC';
60
            $sort            = \in_array(mb_strtolower($sort), ['id', 'order', 'title'], true) ? 'category_' . \mb_strtolower($sort) : 'category_id';
61
            $criteria->setSort($sort);
62
            $criteria->setStart(0);
63
            $criteria->setLimit(0);
64
        } else {
65
            $criteria = $sort;
66
        }
67
        $obj['list']  = $this->getObjects($criteria, false);
68
        $obj['count'] = (false != $obj['list']) ? \count($obj['list']) : 0;
69
70
        return $obj;
71
    }
72
73
    /**
74
     * CategoryHandler::displayAdminListing()
75
     *
76
     * @param string|null $sort
77
     */
78
    public function displayAdminListing(?string $sort = null): void
79
    {
80
        $sort ??= 'id';
81
        echo $this->renderAdminListing($sort);
82
    }
83
84
    /**
85
     * Display a Category listing for administrators
86
     *
87
     * @param string|null $sort listing order
88
     *
89
     * @return string HTML listing for Admin
90
     */
91
    public function renderAdminListing(?string $sort = null): string
92
    {
93
        $sort ??= 'id';
94
        //        if (!\class_exists('Xoopsfaq\Utility')) {
95
        //            \xoops_load('utility', \basename(\dirname(__DIR__)));
96
        //        }
97
98
        /** @var array $objects */
99
        $objects = $this->getObj($sort);
100
101
        $buttons = ['edit', 'delete'];
102
103
        $ret = '<table class="outer width100 bnone pad3 marg5">'
104
               . '  <thead>'
105
               . '  <tr class="xoopsCenter">'
106
               . '    <th class="width5">'
107
               . \_AM_XOOPSFAQ_CATEGORY_ORDER
108
               . '</th>'
109
               . '    <th class="width5">'
110
               . \_AM_XOOPSFAQ_CATEGORY_ID
111
               . '</th>'
112
               . '    <th class="txtleft">'
113
               . \_AM_XOOPSFAQ_CATEGORY_TITLE
114
               . '</th>'
115
               . '    <th class="width20">'
116
               . \_AM_XOOPSFAQ_ACTIONS
117
               . '</th>'
118
               . '  </tr>'
119
               . '  </thead>'
120
               . '  <tbody>';
121
        if ($objects['count'] > 0) {
122
            /** @var XoopsObject $object */
123
            foreach ($objects['list'] as $object) {
124
                $ret .= '  <tr class="xoopsCenter">'
125
                        . '    <td class="even txtcenter">'
126
                        . $object->getVar('category_order')
127
                        . '</td>'
128
                        . '    <td class="even txtcenter">'
129
                        . $object->getVar('category_id')
130
                        . '</td>'
131
                        . '    <td class="even txtleft">'
132
                        . $object->getVar('category_title')
133
                        . '</td>'
134
                        . '    <td class="even txtcenter">';
135
                $ret .= Utility::renderIconLinks($buttons, 'category_id', $object->getVar('category_id'));
136
                $ret .= '    </td>' . '  </tr>';
137
            }
138
        } else {
139
            $ret .= '  <tr class="txtcenter"><td colspan="4" class="even">' . \_AM_XOOPSFAQ_NOLISTING . '</td></tr>';
140
        }
141
        $ret .= '  </tbody>' . '</table>';
142
143
        return $ret;
144
    }
145
146
    /**
147
     * Display the class error(s) encountered
148
     *
149
     * @param array|string $errors the error(s) to be displayed
150
     */
151
    public function displayError($errors = ''): void
152
    {
153
        if ('' !== $errors) {
154
            \xoops_cp_header();
155
            $moduleAdmin = Admin::getInstance();
156
            $moduleAdmin->displayNavigation(\basename(__FILE__));
157
            \xoops_error($errors, \_AM_XOOPSFAQ_ERROR_SUB);
158
            \xoops_cp_footer();
159
        }
160
    }
161
}
162