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

ContentsHandler::getObj()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 16
rs 9.8333
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'
0 ignored issues
show
Bug introduced by
The type XoopsModules\Xoopsfaq\sort was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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) {
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
    public function displayError($errors = '')
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