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

CategoryHandler::getObj()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 17
rs 9.4888
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
introduced by
$sort is never a sub-type of CriteriaElement.
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
    public function displayError($errors = '')
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