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

ContentsHandler::renderAdminListing()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 93
rs 7.8416
c 0
b 0
f 0

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
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
 * Contents (FAQ) and Handler Class Definitions
18
 *
19
 * @package   module\xoopsfaq\class\contents
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
use XoopsModules\Xoopsfaq;
28
29
defined('XOOPS_ROOT_PATH') || die('Restricted access');
30
31
/**
32
 * ContentsHandler
33
 *
34
 * @package  ::   xoopsfaq
35
 * @author   ::    John Neill
36
 * @copyright:: Copyright (c) 2009
37
 * @access::    public
38
 */
39
class ContentsHandler 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_contents', Contents::class, 'contents_id', 'contents_title');
49
    }
50
51
    /**
52
     * ContentsHandler::getObj()
53
     *
54
     * @param \CriteriaElement|string sort order ('id', 'cid', 'title', 'publish', or 'weight') default: 'id'
55
     *
56
     * @return mixed Contents object | false on failure
57
     */
58
    public function getObj($sort = 'id')
59
    {
60
        $obj = false;
61
        if (!$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
            $sort     = in_array(mb_strtolower($sort), ['id', 'cid', 'title', 'publish', 'weight']) ? 'contents_' . mb_strtolower($sort) : 'contents_id';
64
            $criteria->setSort($sort);
65
            $criteria->order = 'ASC';
66
            $criteria->setStart(0);
67
            $criteria->setLimit(0);
68
        } else {
69
            $criteria = $sort;
70
        }
71
        $obj['list']  = $this->getObjects($criteria, false);
72
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
73
        return $obj;
74
    }
75
76
    /**
77
     * ContentsHandler::getPublished()
78
     *
79
     * @param string $id
80
     * @return mixed array of XoopsfaqContent objects | false on failure
81
     */
82
    public function getPublished($id = '')
83
    {
84
        xoops_load('constants', basename(dirname(__DIR__)));
85
86
        $obj               = false;
87
        $criteriaPublished = new \CriteriaCompo();
88
        $criteriaPublished->add(new \Criteria('contents_publish', Xoopsfaq\Constants::NOT_PUBLISHED, '>'));
89
        $criteriaPublished->add(new \Criteria('contents_publish', time(), '<='));
90
91
        $criteria = new \CriteriaCompo(new \Criteria('contents_active', Xoopsfaq\Constants::ACTIVE));
92
        if (!empty($id)) {
93
            $criteria->add(new \Criteria('contents_cid', $id, '='));
94
        }
95
        $criteria->add($criteriaPublished);
96
        $criteria->order = 'ASC';
97
        $criteria->setSort('contents_weight');
98
99
        $obj['list']  = $this->getObjects($criteria, false);
100
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
101
102
        return $obj;
103
    }
104
105
    /**
106
     * Returns category ids of categories that have content
107
     *
108
     * @return array contains category ids
109
     */
110
    public function getCategoriesIdsWithContent()
111
    {
112
        $ret = [];
113
        $sql = 'SELECT contents_cid ';
114
        $sql .= 'FROM `' . $this->table . '` ';
115
        $sql .= 'WHERE (contents_active =\'' . Xoopsfaq\Constants::ACTIVE . '\') ';
116
        $sql .= 'GROUP BY contents_cid';
117
        if (!$result = $this->db->query($sql)) {
118
            return $ret;
119
        }
120
        while (false !== ($myrow = $this->db->fetchArray($result))) {
121
            $ret[$myrow['contents_cid']] = $myrow['contents_cid'];
122
        }
123
124
        return $ret;
125
    }
126
127
    /**
128
     * ContentsHandler::displayAdminListing()
129
     *
130
     * @param string $sort
131
     * @return void
132
     */
133
    public function displayAdminListing($sort = 'id')
134
    {
135
        echo $this->renderAdminListing($sort);
136
    }
137
138
    /**
139
     * ContentsHandler::renderAdminListing()
140
     *
141
     * @param string $sort
142
     * @return string html listing of Contents (FAQ) for Admin
143
     * @see \XoopsModules\Xoopsfaq\Helper
144
     *
145
     */
146
    public function renderAdminListing($sort = 'id')
147
    {
148
        if (!class_exists('Xoopsfaq\Utility')) {
149
            xoops_load('utility', basename(dirname(__DIR__)));
150
        }
151
152
        /** @var CategoryHandler $categoryHandler */
153
        $objects         = $this->getObj($sort);
154
        $helper          = \XoopsModules\Xoopsfaq\Helper::getHelper(basename(dirname(__DIR__)));
155
        $categoryHandler = $helper->getHandler('Category');
156
        $catFields       = ['category_id', 'category_title'];
157
        $catArray        = $categoryHandler->getAll(null, $catFields, false);
158
159
        $buttons = ['edit', 'delete'];
160
161
        $ret = '<table class="outer width100 bnone pad3 marg5">'
162
               . '  <thead>'
163
               . '  <tr class="center">'
164
               . '    <th class="width5">'
165
               . _AM_XOOPSFAQ_CONTENTS_ID
166
               . '</th>'
167
               . '    <th class="width5">'
168
               . _AM_XOOPSFAQ_CONTENTS_ACTIVE
169
               . '</th>'
170
               . '    <th class="width5">'
171
               . _AM_XOOPSFAQ_CONTENTS_WEIGHT
172
               . '</th>'
173
               . '    <th class="left">'
174
               . _AM_XOOPSFAQ_CONTENTS_TITLE
175
               . '</th>'
176
               . '    <th class="left">'
177
               . _AM_XOOPSFAQ_CATEGORY_TITLE
178
               . '</th>'
179
               . '    <th>'
180
               . _AM_XOOPSFAQ_CONTENTS_PUBLISH
181
               . '</th>'
182
               . '    <th class="width20">'
183
               . _AM_XOOPSFAQ_ACTIONS
184
               . '</th>'
185
               . '  </tr>'
186
               . '  </thead>'
187
               . '  <tbody>';
188
        if ($objects['count'] > 0) {
189
            $tdClass = 0;
190
            /** @var \Contents $object */
191
            foreach ($objects['list'] as $object) {
192
                $thisCatId        = $object->getVar('contents_cid');
193
                $thisCatTitle     = $catArray[$thisCatId]['category_title'];
194
                $thisContentTitle = '<a href="' . $helper->url('index.php?cat_id=' . $thisCatId . '#q' . $object->getVar('contents_id')) . '" title="' . _AM_XOOPSFAQ_CONTENTS_VIEW . '">' . $object->getVar('contents_title') . '</a>';
195
                ++$tdClass;
196
                $dispClass = ($tdClass % 1) ? 'even' : 'odd';
197
                $ret       .= '  <tr class="center middle">'
198
                              . '    <td class="'
199
                              . $dispClass
200
                              . '">'
201
                              . $object->getVar('contents_id')
202
                              . '</td>'
203
                              . '    <td class="'
204
                              . $dispClass
205
                              . '">'
206
                              . $object->getActiveIcon()
207
                              . '</td>'
208
                              . '    <td class="'
209
                              . $dispClass
210
                              . '">'
211
                              . $object->getVar('contents_weight')
212
                              . '</td>'
213
                              . '    <td class="'
214
                              . $dispClass
215
                              . ' left">'
216
                              . $thisContentTitle
217
                              . '</td>'
218
                              . '    <td class="'
219
                              . $dispClass
220
                              . ' left">'
221
                              . $thisCatTitle
222
                              . '</td>'
223
                              . '    <td class="'
224
                              . $dispClass
225
                              . '">'
226
                              . $object->getPublished(_SHORTDATESTRING)
227
                              . '</td>'
228
                              . '    <td class="'
229
                              . $dispClass
230
                              . '">';
231
                $ret       .= Xoopsfaq\Utility::renderIconLinks($buttons, 'contents_id', $object->getVar('contents_id')) . '</td>' . '  </tr>';
232
            }
233
        } else {
234
            $ret .= '  <tr class="center"><td colspan="7" class="even">' . _AM_XOOPSFAQ_NOLISTING . '</td></tr>';
235
        }
236
        $ret .= '  </tbody>' . '</table>';
237
        return $ret;
238
    }
239
240
    /**
241
     * ContentsHandler::displayError()
242
     *
243
     * @param array|string $errors will display a page with the error(s)
244
     *
245
     * @return void
246
     * @see \Xmf\Module\Admin
247
     *
248
     */
249 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...
250
    {
251
        if ('' !== $errors) {
252
            xoops_cp_header();
253
            $moduleAdmin = \Xmf\Module\Admin::getInstance();
254
            $moduleAdmin->displayNavigation(basename(__FILE__));
255
            xoops_error($errors, _AM_XOOPSFAQ_ERROR_SUB);
256
            xoops_cp_footer();
257
        }
258
        return;
259
    }
260
}
261