Completed
Pull Request — master (#2)
by
unknown
02:13
created

getCategoriesIdsWithContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * Contents (FAQ) and Handler Class Definitions
14
 *
15
 * @package   module\xoopsfaq\class\contents
16
 * @author    John Neill
17
 * @author    XOOPS Module Development Team
18
 * @copyright Copyright (c) 2001-2017 {@link http://xoops.org XOOPS Project}
19
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
20
 * @since::   1.23
21
 */
22
23
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
24
25
/**
26
 * XoopsfaqContents handles CRUD operations for FAQs
27
 *
28
 * @author::    John Neill
29
 * @copyright:: Copyright (c) 2009
30
 */
31
class XoopsfaqContents extends XoopsObject
32
{
33
  /**
34
   * @var string contains this modules directory name
35
   */
36
    protected $dirname;
37
38
    /**
39
     * Constructor
40
     */
41
    public function __construct()
42
    {
43
        $this->dirname = basename(dirname(__DIR__));
44
        xoops_load('constants', $this->dirname);
45
46
        parent::__construct();
47
        $this->initVar('contents_id', XOBJ_DTYPE_INT, null, false);
48
        $this->initVar('contents_cid', XOBJ_DTYPE_INT, XoopsfaqConstants::DEFAULT_CATEGORY, false);
49
        $this->initVar('contents_title', XOBJ_DTYPE_TXTBOX, null, true, 255);
50
        $this->initVar('contents_contents', XOBJ_DTYPE_TXTAREA, null, false);
51
        $this->initVar('contents_publish', XOBJ_DTYPE_INT, time(), false);
52
        $this->initVar('contents_weight', XOBJ_DTYPE_INT, XoopsfaqConstants::DEFAULT_WEIGHT, false);
53
        $this->initVar('contents_active', XOBJ_DTYPE_INT, XoopsfaqConstants::ACTIVE, false);
54
        $this->initVar('dohtml', XOBJ_DTYPE_INT, XoopsfaqConstants::SET, false);
55
        $this->initVar('doxcode', XOBJ_DTYPE_INT, XoopsfaqConstants::SET, false);
56
        $this->initVar('dosmiley', XOBJ_DTYPE_INT, XoopsfaqConstants::SET, false);
57
        $this->initVar('doimage', XOBJ_DTYPE_INT, XoopsfaqConstants::SET, false);
58
        $this->initVar('dobr', XOBJ_DTYPE_INT, XoopsfaqConstants::SET, false);
59
    }
60
61
    /**
62
     * Display Content (FAQ)
63
     *
64
     * @return string
65
     */
66
    public function __toString()
67
    {
68
        return $this->getVar('contents_title', 's');
69
    }
70
71
    /**
72
     * Display the Content (FAQ) Editor form for Admin
73
     *
74
     * @return void
75
     */
76
    public function displayForm()
77
    {
78
        echo $this->renderForm();
79
    }
80
    /**
81
     * Displays the Content (FAQ) Editor form for Admin
82
     */
83
    public function renderForm()
84
    {
85
        /* @var $xfCatHandler XoopsfaqCategoryHandler */
86
        /* @var $xfHelper Xmf\Module\Helper\GenericHelper */
87
        $xfHelper      = Xmf\Module\Helper::getHelper($this->dirname);
88
        $xfCatHandler  = $xfHelper->getHandler('category');
89
        $catCount      = $xfCatHandler->getCount();
90
        if (empty($catCount)) {
91
            xoops_error(_AM_XOOPSFAQ_ERROR_NO_CATS_EXIST, '');
92
            xoops_cp_footer();
93
            exit();
94
        }
95
96
        include_once $GLOBALS['xoops']->path('/class/xoopsformloader.php');
97
98
        $caption = ($this->isNew()) ? _AM_XOOPSFAQ_CREATE_NEW: sprintf(_AM_XOOPSFAQ_MODIFY_ITEM, $this->getVar('contents_title'));
99
        $form = new XoopsThemeForm($caption, 'content', $_SERVER['REQUEST_URI'], 'post', true);
100
//        $form->addElement(new XoopsFormHiddenToken());
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
        $form->addElement(new xoopsFormHidden('op', 'save'));
102
        $form->addElement(new xoopsFormHidden('contents_id', $this->getVar('contents_id', 'e')));
103
104
        // Active
105
        $contents_active = new XoopsFormRadioYN(_AM_XOOPSFAQ_E_CONTENTS_ACTIVE, 'contents_active', $this->getVar('contents_active', 'e'),   ' ' . _YES . '', ' ' . _NO . '' );
106
        $contents_active->setDescription(_AM_XOOPSFAQ_E_CONTENTS_ACTIVE_DESC);
107
        $form->addElement($contents_active, false);
108
109
        // Title
110
        $contents_title = new XoopsFormText(_AM_XOOPSFAQ_E_CONTENTS_TITLE, 'contents_title', 50, 150, $this->getVar('contents_title', 'e'));
111
        $contents_title->setDescription(_AM_XOOPSFAQ_E_CONTENTS_TITLE_DESC);
112
        $form->addElement($contents_title, true);
113
114
        // Category
115
        $catCriteria = new CriteriaCompo();
116
        $catCriteria->order = 'ASC';
117
        $catCriteria->setSort('category_order');
118
        $objects = $xfCatHandler->getList($catCriteria);
119
        $contents_cid = new XoopsFormSelect(_AM_XOOPSFAQ_E_CONTENTS_CATEGORY, 'contents_cid', $this->getVar('contents_cid', 'e'), 1, false);
120
        $contents_cid->setDescription(_AM_XOOPSFAQ_E_CONTENTS_CATEGORY_DESC);
121
        $contents_cid->addOptionArray($objects);
122
        $form->addElement($contents_cid);
123
124
        // Weight
125
        $contents_weight = new XoopsFormText(_AM_XOOPSFAQ_E_CONTENTS_WEIGHT, 'contents_weight', 5, 5, $this->getVar('contents_weight', 'e'));
126
        $contents_weight->setDescription(_AM_XOOPSFAQ_E_CONTENTS_WEIGHT_DESC);
127
        $form->addElement($contents_weight, false);
128
129
        // Editor
130
        $options_tray  = new XoopsFormElementTray(_AM_XOOPSFAQ_E_CONTENTS_CONTENT, '<br>');
131
        if (class_exists('XoopsFormEditor')) {
132
            // $editorConfigs = array('editor' => $GLOBALS['xoopsConfig']['general_editor'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133
            $editorConfigs = array('editor' => $xfHelper->getConfig('use_wysiwyg', 'dhtmltextarea'),
134
                                     'rows' => 25,
135
                                     'cols' => '100%',
136
                                    'width' => '100%',
137
                                   'height' => '600px',
138
                                     'name' => 'contents_contents',
139
                                    'value' => $this->getVar('contents_contents', 'e')
140
            );
141
            $contents_contents = new XoopsFormEditor('', 'contents_contents', $editorConfigs);
142
        } else {
143
            $contents_contents = new XoopsFormDhtmlTextArea('', 'contents_contents', $this->getVar('contents_contents', 'e'), '100%', '100%');
144
        }
145
        $options_tray->addElement($contents_contents);
146
        $options_tray->setDescription(_AM_XOOPSFAQ_E_CONTENTS_CONTENT_DESC);
147
148
        xoops_load('XoopsEditorHandler');
149
        $editor_handler = XoopsEditorHandler::getInstance();
150
        $editorList     = $editor_handler->getList(true);
151
        if (isset($editorConfigs['editor']) && in_array($editorConfigs['editor'], array_flip($editorList))) {
152
            $form->addElement(new xoopsFormHidden('dohtml', XoopsfaqConstants::NOTSET));
153
            $form->addElement(new xoopsFormHidden('dobr', XoopsfaqConstants::SET));
154
        } else {
155
            $html_checkbox = new XoopsFormCheckBox('', 'dohtml', $this->getVar('dohtml', 'e'));
156
            $html_checkbox->addOption(1, _AM_XOOPSFAQ_E_DOHTML);
157
            $options_tray->addElement($html_checkbox);
158
159
            $breaks_checkbox = new XoopsFormCheckBox('', 'dobr', $this->getVar('dobr', 'e'));
160
            $breaks_checkbox->addOption(1, _AM_XOOPSFAQ_E_BREAKS);
161
            $options_tray->addElement($breaks_checkbox);
162
        }
163
164
        $doimage_checkbox = new XoopsFormCheckBox('', 'doimage', $this->getVar('doimage', 'e'));
165
        $doimage_checkbox->addOption(1, _AM_XOOPSFAQ_E_DOIMAGE);
166
        $options_tray->addElement($doimage_checkbox);
167
168
        $xcodes_checkbox = new XoopsFormCheckBox('', 'doxcode', $this->getVar('doxcode', 'e'));
169
        $xcodes_checkbox->addOption(1, _AM_XOOPSFAQ_E_DOXCODE);
170
        $options_tray->addElement($xcodes_checkbox);
171
172
        $smiley_checkbox = new XoopsFormCheckBox('', 'dosmiley', $this->getVar('dosmiley', 'e'));
173
        $smiley_checkbox->addOption(1, _AM_XOOPSFAQ_E_DOSMILEY);
174
        $options_tray->addElement($smiley_checkbox);
175
176
        $form->addElement($options_tray);
177
178
        $contents_publish = new XoopsFormTextDateSelect(_AM_XOOPSFAQ_E_CONTENTS_PUBLISH, 'contents_publish', 20, (int)$this->getVar('contents_publish'), $this->isNew());
179
        $contents_publish->setDescription(_AM_XOOPSFAQ_E_CONTENTS_PUBLISH_DESC);
180
        $form->addElement($contents_publish);
181
182
        $form->addElement(new XoopsFormButtonTray('contents_form', _SUBMIT, 'submit'));
183
184
        $form->display();
185
    }
186
187
    /**
188
     * Get the FAQ Active/Inactive icon to display
189
     *
190
     * @return string HTML <img> tag representing current active status
191
     */
192
    public function getActiveIcon()
193
    {
194
        if ($this->getVar('contents_active') > XoopsfaqConstants::INACTIVE) {
195
            $icon = '<img src="' . Xmf\Module\Admin::iconUrl('green.gif', '16') . '" alt="' . _YES . '">';
196
        } else {
197
            $icon = '<img src="' . Xmf\Module\Admin::iconUrl('red.gif', '16') . '" alt="' . _NO . '">';
198
        }
199
        return $icon;
200
    }
201
202
    /**
203
     * Get the timestamp for when Content (FAQ) was published
204
     *
205
     * @param int|string Unix timestamp
206
     *
207
     * @return string|bool formatted timestamp on success, false on failure
208
     */
209
    public function getPublished($timestamp = '')
210
    {
211
        if (!$this->getVar('contents_publish')) {
212
            return '';
213
        }
214
        return formatTimestamp($this->getVar('contents_publish'), $timestamp);
215
    }
216
}
217
218
/**
219
 * XoopsfaqContentsHandler
220
 *
221
 * @package::   xoopsfaq
222
 * @author::    John Neill
223
 * @copyright:: Copyright (c) 2009
224
 * @access::    public
225
 */
226
class XoopsfaqContentsHandler extends XoopsPersistableObjectHandler
227
{
228
    /**
229
     * Constructor
230
     *
231
     * @param mixed $db
232
     */
233
    public function __construct($db)
234
    {
235
        parent::__construct($db, 'xoopsfaq_contents', 'XoopsfaqContents', 'contents_id', 'contents_title');
236
    }
237
238
    /**
239
     * XoopsfaqContentsHandler::getObj()
240
     *
241
     * @param CriteriaElement|string sort order ('id', 'cid', 'title', 'publish', or 'weight') default: 'id'
242
     *
243
     * @return mixed XoopsfaqContents object | false on failure
244
     */
245
    public function getObj($sort = 'id')
246
    {
247
        $obj = false;
248
        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...
249
            $criteria = new CriteriaCompo();
250
            // $obj['count'] = $this->getCount($criteria);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
251
            $sort = in_array(mb_strtolower($sort), array('id', 'cid', 'title', 'publish', 'weight')) ? 'contents_' . mb_strtolower($sort) : 'contents_id';
252
            $criteria->setSort($sort);
253
            $criteria->order = 'ASC';
254
            $criteria->setStart(0);
255
            $criteria->setLimit(0);
256
            $obj['list'] = $this->getObjects($criteria, false);
257
        } else {
258
            $obj['list']  = $this->getObjects($sort, false);
259
        }
260
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
261
        return $obj;
262
    }
263
264
    /**
265
     * XoopsfaqContentsHandler::getPublished()
266
     *
267
     * @param string $id
268
     * @return mixed array of XoopsfaqContent objects | false on failure
269
     */
270
    public function getPublished($id = '')
271
    {
272
        xoops_load('constants', basename(dirname(__DIR__)));
273
274
        $obj = false;
275
        $criteriaPublished = new CriteriaCompo();
276
        $criteriaPublished->add(new Criteria('contents_publish', XoopsfaqConstants::NOT_PUBLISHED, '>'));
277
        $criteriaPublished->add(new Criteria('contents_publish', time(), '<='));
278
279
        $criteria = new CriteriaCompo(new Criteria('contents_active', XoopsfaqConstants::ACTIVE));
280
        if (!empty($id)) {
281
            $criteria->add(new Criteria('contents_cid', $id, '='));
282
        }
283
        $criteria->add($criteriaPublished);
284
        $criteria->order = 'ASC';
285
        $criteria->setSort('contents_weight');
286
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
287
        $obj['count'] = $this->getCount($criteria);
288
        if (!empty($args[0])) {
289
            $criteria->order = 'ASC';
290
            $criteria->setSort('contents_title');
291
            $criteria->setStart(0);
292
            $criteria->setLimit(0);
293
        }
294
*/
295
        $obj['list'] = $this->getObjects($criteria, false);
296
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
297
        return $obj;
298
    }
299
300
    /**
301
     * Returns categories ids of categories that have content
302
     */
303
    public function getCategoriesIdsWithContent()
304
    {
305
        $ret = array();
306
        $sql  = "SELECT contents_cid ";
307
        $sql .= "FROM `{$this->table}` ";
308
        $sql .= "WHERE (contents_active = '1') ";
309
        $sql .= "GROUP BY contents_cid";
310
        if (!$result = $this->db->query($sql)) {
311
            return $ret;
312
        }
313
        while ($myrow = $this->db->fetchArray($result)) {
314
            $ret[$myrow['contents_cid']] = $myrow['contents_cid'];
315
        }
316
317
        return $ret;
318
    }
319
320
    /**
321
     * XoopsfaqContentsHandler::displayAdminListing()
322
     *
323
     * @param string $sort
324
     * @return void
325
     */
326
    public function displayAdminListing($sort = 'id')
327
    {
328
        echo $this->renderAdminListing($sort);
329
    }
330
331
    /**
332
     * XoopsfaqContentsHandler::renderAdminListing()
333
     *
334
     * @see Xmf\Module\Helper
335
     *
336
     * @param string $sort
337
     * @return string html listing of Contents (FAQ) for Admin
338
     */
339
    public function renderAdminListing($sort = 'id')
340
    {
341
        if (!class_exists('XoopsfaqUtility')) {
342
            xoops_load('utility', basename(dirname(__DIR__)));
343
        }
344
345
        /* @var $xfCatHandler XoopsfaqCategoryHandler */
346
        $objects      = $this->getObj($sort);
347
        $xfHelper     = Xmf\Module\Helper::getHelper(basename(dirname(__DIR__)));
348
        $xfCatHandler = $xfHelper->getHandler('category');
349
        $catFields    = array('category_id', 'category_title');
350
        $catArray     = $xfCatHandler->getAll(null, $catFields, false);
351
352
        $buttons      = array('edit', 'delete');
353
354
        $ret = '<table class="outer width100 bnone pad3 marg5">'
355
             . '  <thead>'
356
             . '  <tr class="center">'
357
             . '    <th class="width5">' . _AM_XOOPSFAQ_CONTENTS_ID . '</th>'
358
             . '    <th class="width5">' . _AM_XOOPSFAQ_CONTENTS_ACTIVE . '</th>'
359
             . '    <th class="width5">' . _AM_XOOPSFAQ_CONTENTS_WEIGHT . '</th>'
360
             . '    <th class="left">' . _AM_XOOPSFAQ_CONTENTS_TITLE . '</th>'
361
             . '    <th class="left">' . _AM_XOOPSFAQ_CATEGORY_TITLE . '</th>'
362
             . '    <th>' . _AM_XOOPSFAQ_CONTENTS_PUBLISH . '</th>'
363
             . '    <th class="width20">' . _AM_XOOPSFAQ_ACTIONS . '</th>'
364
             . '  </tr>'
365
             . '  </thead>'
366
             . '  <tbody>';
367
        if ($objects['count'] > 0) {
368
            $tdClass = 0;
369
            /* @var $object \XoopsfaqContents */
370
            foreach ($objects['list'] as $object) {
371
                $thisCatId = $object->getVar('contents_cid');
372
                $thisCatTitle = $catArray[$thisCatId]['category_title'];
373
                $thisContentTitle = '<a href="' . $xfHelper->url('index.php?cat_id=' . $thisCatId . '#q' . $object->getVar('contents_id')) . '" title="' . _AM_XOOPSFAQ_CONTENTS_VIEW . '">' . $object->getVar('contents_title') . '</a>';
374
                ++$tdClass;
375
                $dispClass = ($tdClass % 1) ? 'even' : 'odd';
376
                $ret .= '  <tr class="center middle">'
377
                      . '    <td class="' . $dispClass . '">' . $object->getVar('contents_id') . '</td>'
378
                      . '    <td class="' . $dispClass . '">' . $object->getActiveIcon() . '</td>'
379
                      . '    <td class="' . $dispClass . '">' . $object->getVar('contents_weight') . '</td>'
380
                      . '    <td class="' . $dispClass . ' left">' . $thisContentTitle . '</td>'
381
                      . '    <td class="' . $dispClass . ' left">' . $thisCatTitle . '</td>'
382
                      . '    <td class="' . $dispClass . '">' . $object->getPublished(_SHORTDATESTRING) . '</td>'
383
                      . '    <td class="' . $dispClass . '">';
384
                $ret .= XoopsfaqUtility::renderIconLinks($buttons, 'contents_id', $object->getVar('contents_id'))
385
                      . '</td>'
386
                      . '  </tr>';
387
            }
388
        } else {
389
            $ret .= '  <tr class="center"><td colspan="7" class="even">' . _AM_XOOPSFAQ_NOLISTING . '</td></tr>';
390
        }
391
        $ret .= '  </tbody>'
392
//             . '  <tr class="center"><td colspan="7" class="foot">&nbsp;</td></tr>'
393
              . '</table>';
394
        return $ret;
395
    }
396
397
    /**
398
     * XoopsfaqContentsHandler::displayError()
399
     *
400
     * @see Xmf\Module\Admin
401
     *
402
     * @param array|string $errors will display a page with the error(s)
403
     *
404
     * @return void
405
     */
406 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...
407
    {
408
        if ('' !== $errors) {
409
            xoops_cp_header();
410
            $moduleAdmin = Xmf\Module\Admin::getInstance();
411
            $moduleAdmin->displayNavigation('index.php');
412
            xoops_error($errors, _AM_XOOPSFAQ_ERROR_SUB);
413
            xoops_cp_footer();
414
        }
415
        return;
416
    }
417
}
418