Completed
Push — master ( d1a68a...39bb73 )
by Michael
01:32
created

CategoryHandler::displayAdminListing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
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
 * @package   module\xoopsfaq\class
20
 * @author    John Neill
21
 * @author    XOOPS Module Development Team
22
 * @copyright Copyright (c) 2001-2017 {@link http://xoops.org XOOPS Project}
23
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
24
 * @since     ::   1.23
25
 *
26
 */
27
28
use XoopsModules\Xoopsfaq;
29
30
defined('XOOPS_ROOT_PATH') || die('Restricted access');
31
32
/**
33
 * CategoryHandler
34
 *
35
 * @package  ::   xoopsfaq
36
 * @author   ::    John Neill
37
 * @copyright:: Copyright (c) 2009
38
 */
39
class CategoryHandler extends \XoopsPersistableObjectHandler
40
{
41
    /**
42
     * Constructor
43
     *
44
     * @param mixed $db
45
     */
46
    public function __construct(\XoopsDatabase $db = null)
47
    {
48
        parent::__construct($db, 'xoopsfaq_categories', Category::class, 'category_id', 'category_title');
49
    }
50
51
    /**
52
     * CategoryHandler::getObj()
53
     *
54
     * @param string $sort order ('id', order', or 'title') - default: id
55
     *
56
     * @return mixed Category | false on failure
57
     */
58
    public function getObj($sort = 'id')
59
    {
60
        $obj = false;
61
        if ((null !== $sort) && (!$sort instanceof \CriteriaElement)) {
0 ignored issues
show
Bug introduced by
The class CriteriaElement does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
62
            $criteria        = new \CriteriaCompo();
63
            $obj['count']    = $this->getCount($criteria);
64
            $criteria->order = 'ASC';
65
            $sort            = in_array(mb_strtolower($sort), ['id', 'order', 'title']) ? 'category_' . mb_strtolower($sort) : 'category_id';
66
            $criteria->setSort($sort);
67
            $criteria->setStart(0);
68
            $criteria->setLimit(0);
69
        } else {
70
            $criteria = $sort;
71
        }
72
        $obj['list']  = $this->getObjects($criteria, false);
73
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
74
        return $obj;
75
    }
76
77
    /**
78
     * CategoryHandler::displayAdminListing()
79
     *
80
     * @param string $sort
81
     * @return void
82
     */
83
    public function displayAdminListing($sort = 'id')
84
    {
85
        echo $this->renderAdminListing($sort);
86
    }
87
88
    /**
89
     * Display a Category listing for administrators
90
     *
91
     * @param string $sort listing order
92
     *
93
     * @return string HTML listing for Admin
94
     */
95
    public function renderAdminListing($sort = 'id')
96
    {
97
        if (!class_exists('Xoopsfaq\Utility')) {
98
            xoops_load('utility', basename(dirname(__DIR__)));
99
        }
100
101
        $objects = $this->getObj($sort);
102
103
        $buttons = ['edit', 'delete'];
104
105
        $ret = '<table class="outer width100 bnone pad3 marg5">'
106
               . '  <thead>'
107
               . '  <tr class="xoopsCenter">'
108
               . '    <th class="width5">'
109
               . _AM_XOOPSFAQ_CATEGORY_ORDER
110
               . '</th>'
111
               . '    <th class="width5">'
112
               . _AM_XOOPSFAQ_CATEGORY_ID
113
               . '</th>'
114
               . '    <th class="txtleft">'
115
               . _AM_XOOPSFAQ_CATEGORY_TITLE
116
               . '</th>'
117
               . '    <th class="width20">'
118
               . _AM_XOOPSFAQ_ACTIONS
119
               . '</th>'
120
               . '  </tr>'
121
               . '  </thead>'
122
               . '  <tbody>';
123
        if ($objects['count'] > 0) {
124
            /** @var XoopsObject $object */
125
            foreach ($objects['list'] as $object) {
126
                $ret .= '  <tr class="xoopsCenter">'
127
                        . '    <td class="even txtcenter">'
128
                        . $object->getVar('category_order')
129
                        . '</td>'
130
                        . '    <td class="even txtcenter">'
131
                        . $object->getVar('category_id')
132
                        . '</td>'
133
                        . '    <td class="even txtleft">'
134
                        . $object->getVar('category_title')
135
                        . '</td>'
136
                        . '    <td class="even txtcenter">';
137
                $ret .= Xoopsfaq\Utility::renderIconLinks($buttons, 'category_id', $object->getVar('category_id'));
138
                $ret .= '    </td>' . '  </tr>';
139
            }
140
        } else {
141
            $ret .= '  <tr class="txtcenter"><td colspan="4" class="even">' . _AM_XOOPSFAQ_NOLISTING . '</td></tr>';
142
        }
143
        $ret .= '  </tbody>' . '</table>';
144
        return $ret;
145
    }
146
147
    /**
148
     * Display the class error(s) encountered
149
     *
150
     * @param array|string $errors the error(s) to be displayed
151
     *
152
     * @return void
153
     */
154 View Code Duplication
    public function displayError($errors = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        if ('' !== $errors) {
157
            xoops_cp_header();
158
            $moduleAdmin = \Xmf\Module\Admin::getInstance();
159
            $moduleAdmin->displayNavigation(basename(__FILE__));
160
            xoops_error($errors, _AM_XOOPSFAQ_ERROR_SUB);
161
            xoops_cp_footer();
162
        }
163
        return;
164
    }
165
}
166