Completed
Push — master ( f47147...0adc58 )
by
unknown
02:15
created

XoopsfaqContentsHandler::getPublished()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 1
dl 0
loc 22
rs 9.2
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 XoopsfaqCategoryHandler $xfCatHandler */
86
        /** @var Xmf\Module\Helper\GenericHelper $xfHelper */
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
        return $form->render();
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
            $sort = in_array(mb_strtolower($sort), array('id', 'cid', 'title', 'publish', 'weight')) ? 'contents_' . mb_strtolower($sort) : 'contents_id';
251
            $criteria->setSort($sort);
252
            $criteria->order = 'ASC';
253
            $criteria->setStart(0);
254
            $criteria->setLimit(0);
255
        } else {
256
            $criteria = $sort;
257
        }
258
        $obj['list']  = $this->getObjects($criteria, false);
259
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
260
        return $obj;
261
    }
262
263
    /**
264
     * XoopsfaqContentsHandler::getPublished()
265
     *
266
     * @param string $id
267
     * @return mixed array of XoopsfaqContent objects | false on failure
268
     */
269
    public function getPublished($id = '')
270
    {
271
        xoops_load('constants', basename(dirname(__DIR__)));
272
273
        $obj = false;
274
        $criteriaPublished = new CriteriaCompo();
275
        $criteriaPublished->add(new Criteria('contents_publish', XoopsfaqConstants::NOT_PUBLISHED, '>'));
276
        $criteriaPublished->add(new Criteria('contents_publish', time(), '<='));
277
278
        $criteria = new CriteriaCompo(new Criteria('contents_active', XoopsfaqConstants::ACTIVE));
279
        if (!empty($id)) {
280
            $criteria->add(new Criteria('contents_cid', $id, '='));
281
        }
282
        $criteria->add($criteriaPublished);
283
        $criteria->order = 'ASC';
284
        $criteria->setSort('contents_weight');
285
286
        $obj['list'] = $this->getObjects($criteria, false);
287
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
288
289
        return $obj;
290
    }
291
292
    /**
293
     * Returns category ids of categories that have content
294
     *
295
     * @return array contains category ids
296
     */
297
    public function getCategoriesIdsWithContent()
298
    {
299
        $ret = array();
300
        $sql  = 'SELECT contents_cid ';
301
        $sql .= 'FROM `' . $this->table . '` ';
302
        $sql .= 'WHERE (contents_active =\''. XoopsfaqConstants::ACTIVE . '\') ';
303
        $sql .= 'GROUP BY contents_cid';
304
        if (!$result = $this->db->query($sql)) {
305
            return $ret;
306
        }
307
        while ($myrow = $this->db->fetchArray($result)) {
308
            $ret[$myrow['contents_cid']] = $myrow['contents_cid'];
309
        }
310
311
        return $ret;
312
    }
313
314
    /**
315
     * XoopsfaqContentsHandler::displayAdminListing()
316
     *
317
     * @param string $sort
318
     * @return void
319
     */
320
    public function displayAdminListing($sort = 'id')
321
    {
322
        echo $this->renderAdminListing($sort);
323
    }
324
325
    /**
326
     * XoopsfaqContentsHandler::renderAdminListing()
327
     *
328
     * @see Xmf\Module\Helper
329
     *
330
     * @param string $sort
331
     * @return string html listing of Contents (FAQ) for Admin
332
     */
333
    public function renderAdminListing($sort = 'id')
334
    {
335
        if (!class_exists('XoopsfaqUtility')) {
336
            xoops_load('utility', basename(dirname(__DIR__)));
337
        }
338
339
        /** @var XoopsfaqCategoryHandler $xfCatHandler */
340
        $objects      = $this->getObj($sort);
341
        $xfHelper     = Xmf\Module\Helper::getHelper(basename(dirname(__DIR__)));
342
        $xfCatHandler = $xfHelper->getHandler('category');
343
        $catFields    = array('category_id', 'category_title');
344
        $catArray     = $xfCatHandler->getAll(null, $catFields, false);
345
346
        $buttons      = array('edit', 'delete');
347
348
        $ret = '<table class="outer width100 bnone pad3 marg5">'
349
             . '  <thead>'
350
             . '  <tr class="center">'
351
             . '    <th class="width5">' . _AM_XOOPSFAQ_CONTENTS_ID . '</th>'
352
             . '    <th class="width5">' . _AM_XOOPSFAQ_CONTENTS_ACTIVE . '</th>'
353
             . '    <th class="width5">' . _AM_XOOPSFAQ_CONTENTS_WEIGHT . '</th>'
354
             . '    <th class="left">' . _AM_XOOPSFAQ_CONTENTS_TITLE . '</th>'
355
             . '    <th class="left">' . _AM_XOOPSFAQ_CATEGORY_TITLE . '</th>'
356
             . '    <th>' . _AM_XOOPSFAQ_CONTENTS_PUBLISH . '</th>'
357
             . '    <th class="width20">' . _AM_XOOPSFAQ_ACTIONS . '</th>'
358
             . '  </tr>'
359
             . '  </thead>'
360
             . '  <tbody>';
361
        if ($objects['count'] > 0) {
362
            $tdClass = 0;
363
            /** @var \XoopsfaqContents $object */
364
            foreach ($objects['list'] as $object) {
365
                $thisCatId = $object->getVar('contents_cid');
366
                $thisCatTitle = $catArray[$thisCatId]['category_title'];
367
                $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>';
368
                ++$tdClass;
369
                $dispClass = ($tdClass % 1) ? 'even' : 'odd';
370
                $ret .= '  <tr class="center middle">'
371
                      . '    <td class="' . $dispClass . '">' . $object->getVar('contents_id') . '</td>'
372
                      . '    <td class="' . $dispClass . '">' . $object->getActiveIcon() . '</td>'
373
                      . '    <td class="' . $dispClass . '">' . $object->getVar('contents_weight') . '</td>'
374
                      . '    <td class="' . $dispClass . ' left">' . $thisContentTitle . '</td>'
375
                      . '    <td class="' . $dispClass . ' left">' . $thisCatTitle . '</td>'
376
                      . '    <td class="' . $dispClass . '">' . $object->getPublished(_SHORTDATESTRING) . '</td>'
377
                      . '    <td class="' . $dispClass . '">';
378
                $ret .= XoopsfaqUtility::renderIconLinks($buttons, 'contents_id', $object->getVar('contents_id'))
379
                      . '</td>'
380
                      . '  </tr>';
381
            }
382
        } else {
383
            $ret .= '  <tr class="center"><td colspan="7" class="even">' . _AM_XOOPSFAQ_NOLISTING . '</td></tr>';
384
        }
385
        $ret .= '  </tbody>'
386
              . '</table>';
387
        return $ret;
388
    }
389
390
    /**
391
     * XoopsfaqContentsHandler::displayError()
392
     *
393
     * @see Xmf\Module\Admin
394
     *
395
     * @param array|string $errors will display a page with the error(s)
396
     *
397
     * @return void
398
     */
399 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...
400
    {
401
        if ('' !== $errors) {
402
            xoops_cp_header();
403
            $moduleAdmin = Xmf\Module\Admin::getInstance();
404
            $moduleAdmin->displayNavigation('index.php');
405
            xoops_error($errors, _AM_XOOPSFAQ_ERROR_SUB);
406
            xoops_cp_footer();
407
        }
408
        return;
409
    }
410
}
411