CategoryHandler   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 64
c 2
b 0
f 0
dl 0
loc 122
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getObj() 0 18 5
A displayAdminListing() 0 4 1
A displayError() 0 9 2
A renderAdminListing() 0 53 3
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
class CategoryHandler extends \XoopsPersistableObjectHandler
35
{
36
    /**
37
     * Constructor
38
     */
39
    public function __construct(\XoopsDatabase $db = null)
40
    {
41
        parent::__construct($db, 'xoopsfaq_categories', Category::class, 'category_id', 'category_title');
42
    }
43
44
    /**
45
     * CategoryHandler::getObj()
46
     *
47
     * @param \CriteriaElement|string|null $sort order ('id', order', or 'title') - default: id
48
     *
49
     * @return array Category
50
     */
51
    public function getObj($sort = null): array
52
    {
53
        $obj = [];
54
        if ((null !== $sort) && (!$sort instanceof \CriteriaElement)) {
55
            $criteria        = new \CriteriaCompo();
56
            $obj['count']    = $this->getCount($criteria);
57
            $criteria->order = 'ASC';
58
            $sort            = \in_array(mb_strtolower($sort), ['id', 'order', 'title'], true) ? 'category_' . \mb_strtolower($sort) : 'category_id';
59
            $criteria->setSort($sort);
60
            $criteria->setStart(0);
61
            $criteria->setLimit(0);
62
        } else {
63
            $criteria = $sort;
64
        }
65
        $obj['list']  = $this->getObjects($criteria, false);
66
        $obj['count'] = 0 != ($obj['list']) ? \count($obj['list']) : 0;
67
68
        return $obj;
69
    }
70
71
    /**
72
     * CategoryHandler::displayAdminListing()
73
     */
74
    public function displayAdminListing(?string $sort = null): void
75
    {
76
        $sort ??= 'id';
77
        echo $this->renderAdminListing($sort);
78
    }
79
80
    /**
81
     * Display a Category listing for administrators
82
     *
83
     * @param string|null $sort listing order
84
     *
85
     * @return string HTML listing for Admin
86
     */
87
    public function renderAdminListing(?string $sort = null): string
88
    {
89
        $sort ??= 'id';
90
        //        if (!\class_exists('Xoopsfaq\Utility')) {
91
        //            \xoops_load('utility', \basename(\dirname(__DIR__)));
92
        //        }
93
94
        /** @var array $objects */
95
        $objects = $this->getObj($sort);
96
97
        $buttons = ['edit', 'delete'];
98
99
        $ret = '<table class="outer width100 bnone pad3 marg5">'
100
               . '  <thead>'
101
               . '  <tr class="xoopsCenter">'
102
               . '    <th class="width5">'
103
               . \_AM_XOOPSFAQ_CATEGORY_ORDER
104
               . '</th>'
105
               . '    <th class="width5">'
106
               . \_AM_XOOPSFAQ_CATEGORY_ID
107
               . '</th>'
108
               . '    <th class="txtleft">'
109
               . \_AM_XOOPSFAQ_CATEGORY_TITLE
110
               . '</th>'
111
               . '    <th class="width20">'
112
               . \_AM_XOOPSFAQ_ACTIONS
113
               . '</th>'
114
               . '  </tr>'
115
               . '  </thead>'
116
               . '  <tbody>';
117
        if ($objects['count'] > 0) {
118
            /** @var \XoopsObject $object */
119
            foreach ($objects['list'] as $object) {
120
                $ret .= '  <tr class="xoopsCenter">'
121
                        . '    <td class="even txtcenter">'
122
                        . $object->getVar('category_order')
123
                        . '</td>'
124
                        . '    <td class="even txtcenter">'
125
                        . $object->getVar('category_id')
126
                        . '</td>'
127
                        . '    <td class="even txtleft">'
128
                        . $object->getVar('category_title')
129
                        . '</td>'
130
                        . '    <td class="even txtcenter">';
131
                $ret .= Utility::renderIconLinks($buttons, 'category_id', $object->getVar('category_id'));
132
                $ret .= '    </td>' . '  </tr>';
133
            }
134
        } else {
135
            $ret .= '  <tr class="txtcenter"><td colspan="4" class="even">' . \_AM_XOOPSFAQ_NOLISTING . '</td></tr>';
136
        }
137
        $ret .= '  </tbody>' . '</table>';
138
139
        return $ret;
140
    }
141
142
    /**
143
     * Display the class error(s) encountered
144
     *
145
     * @param array|string $errors the error(s) to be displayed
146
     */
147
    public function displayError($errors = ''): void
148
    {
149
        if ('' !== $errors) {
150
            \xoops_cp_header();
151
            /** @var Admin $moduleAdmin */
152
            $moduleAdmin = Admin::getInstance();
153
            $moduleAdmin->displayNavigation(\basename(__FILE__));
154
            \xoops_error($errors, \_AM_XOOPSFAQ_ERROR_SUB);
155
            \xoops_cp_footer();
156
        }
157
    }
158
}
159