ContentsHandler   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 130
c 2
b 0
f 0
dl 0
loc 220
rs 10
wmc 19

7 Methods

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