Contents::renderForm()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 104
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 73
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 104
rs 7.9668

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 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 Xmf\Module\Admin;
27
28
/**
29
 * Contents handles CRUD operations for FAQs
30
 *
31
 * @author   ::    John Neill
32
 * @copyright:: Copyright (c) 2009
33
 */
34
final class Contents extends \XoopsObject
35
{
36
    private $contents_id;
0 ignored issues
show
introduced by
The private property $contents_id is not used, and could be removed.
Loading history...
37
    private $contents_cid;
0 ignored issues
show
introduced by
The private property $contents_cid is not used, and could be removed.
Loading history...
38
    private $contents_title;
0 ignored issues
show
introduced by
The private property $contents_title is not used, and could be removed.
Loading history...
39
    private $contents_contents;
0 ignored issues
show
introduced by
The private property $contents_contents is not used, and could be removed.
Loading history...
40
    private $contents_publish;
0 ignored issues
show
introduced by
The private property $contents_publish is not used, and could be removed.
Loading history...
41
    private $contents_weight;
0 ignored issues
show
introduced by
The private property $contents_weight is not used, and could be removed.
Loading history...
42
    private $contents_active;
0 ignored issues
show
introduced by
The private property $contents_active is not used, and could be removed.
Loading history...
43
    private $dohtml;
0 ignored issues
show
introduced by
The private property $dohtml is not used, and could be removed.
Loading history...
44
    private $doxcode;
0 ignored issues
show
introduced by
The private property $doxcode is not used, and could be removed.
Loading history...
45
    private $dosmiley;
0 ignored issues
show
introduced by
The private property $dosmiley is not used, and could be removed.
Loading history...
46
    private $doimage;
0 ignored issues
show
introduced by
The private property $doimage is not used, and could be removed.
Loading history...
47
    private $dobr;
0 ignored issues
show
introduced by
The private property $dobr is not used, and could be removed.
Loading history...
48
49
    /**
50
     * @var string contains this modules directory name
51
     */
52
    protected $dirname;
53
54
    /**
55
     * Constructor
56
     */
57
    public function __construct()
58
    {
59
        $this->dirname = \basename(\dirname(__DIR__));
60
        \xoops_load('constants', $this->dirname);
61
62
        parent::__construct();
63
        $this->initVar('contents_id', \XOBJ_DTYPE_INT, null, false);
64
        $this->initVar('contents_cid', \XOBJ_DTYPE_INT, Constants::DEFAULT_CATEGORY, false);
65
        $this->initVar('contents_title', \XOBJ_DTYPE_TXTBOX, null, true, 255);
66
        $this->initVar('contents_contents', \XOBJ_DTYPE_TXTAREA, null, false);
67
        $this->initVar('contents_publish', \XOBJ_DTYPE_INT, \time(), false);
68
        $this->initVar('contents_weight', \XOBJ_DTYPE_INT, Constants::DEFAULT_WEIGHT, false);
69
        $this->initVar('contents_active', \XOBJ_DTYPE_INT, Constants::ACTIVE, false);
70
        $this->initVar('dohtml', \XOBJ_DTYPE_INT, Constants::SET, false);
71
        $this->initVar('doxcode', \XOBJ_DTYPE_INT, Constants::SET, false);
72
        $this->initVar('dosmiley', \XOBJ_DTYPE_INT, Constants::SET, false);
73
        $this->initVar('doimage', \XOBJ_DTYPE_INT, Constants::SET, false);
74
        $this->initVar('dobr', \XOBJ_DTYPE_INT, Constants::SET, false);
75
    }
76
77
    /**
78
     * Display Content (FAQ)
79
     *
80
     * @return string
81
     */
82
    public function __toString(): string
83
    {
84
        return (string)$this->getVar('contents_title', 's');
85
    }
86
87
    /**
88
     * Display the Content (FAQ) Editor form for Admin
89
     */
90
    public function displayForm(): void
91
    {
92
        echo $this->renderForm();
93
    }
94
95
    /**
96
     * Displays the Content (FAQ) Editor form for Admin
97
     */
98
    public function renderForm()
99
    {
100
        /** @var CategoryHandler $categoryHandler */
101
        /** @var Helper $helper */
102
        $helper          = Helper::getHelper($this->dirname);
103
        $categoryHandler = $helper->getHandler('Category');
104
        $catCount        = $categoryHandler->getCount();
105
        if (empty($catCount)) {
106
            \xoops_error(\_AM_XOOPSFAQ_ERROR_NO_CATS_EXIST, '');
107
            \xoops_cp_footer();
108
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
109
        }
110
111
        require_once $GLOBALS['xoops']->path('/class/xoopsformloader.php');
112
113
        $caption = ($this->isNew()) ? \_AM_XOOPSFAQ_CREATE_NEW : \sprintf(\_AM_XOOPSFAQ_MODIFY_ITEM, $this->getVar('contents_title'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('contents_title') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        $caption = ($this->isNew()) ? \_AM_XOOPSFAQ_CREATE_NEW : \sprintf(\_AM_XOOPSFAQ_MODIFY_ITEM, /** @scrutinizer ignore-type */ $this->getVar('contents_title'));
Loading history...
114
        $form    = new \XoopsThemeForm($caption, 'content', $_SERVER['REQUEST_URI'], 'post', true);
115
        //        $form->addElement(new \XoopsFormHiddenToken());
116
        $form->addElement(new \XoopsFormHidden('op', 'save'));
117
        $form->addElement(new \XoopsFormHidden('contents_id', $this->getVar('contents_id', 'e')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('contents_id', 'e') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
        $form->addElement(new \XoopsFormHidden('contents_id', /** @scrutinizer ignore-type */ $this->getVar('contents_id', 'e')));
Loading history...
118
119
        // Active
120
        $contents_active = new \XoopsFormRadioYN(\_AM_XOOPSFAQ_E_CONTENTS_ACTIVE, 'contents_active', $this->getVar('contents_active', 'e'), ' ' . \_YES, ' ' . \_NO);
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('contents_active', 'e') can also be of type array and array; however, parameter $value of XoopsFormRadioYN::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
        $contents_active = new \XoopsFormRadioYN(\_AM_XOOPSFAQ_E_CONTENTS_ACTIVE, 'contents_active', /** @scrutinizer ignore-type */ $this->getVar('contents_active', 'e'), ' ' . \_YES, ' ' . \_NO);
Loading history...
121
        $contents_active->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_ACTIVE_DESC);
122
        $form->addElement($contents_active, false);
123
124
        // Title
125
        $contents_title = new \XoopsFormText(\_AM_XOOPSFAQ_E_CONTENTS_TITLE, 'contents_title', 50, 150, $this->getVar('contents_title', 'e'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('contents_title', 'e') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        $contents_title = new \XoopsFormText(\_AM_XOOPSFAQ_E_CONTENTS_TITLE, 'contents_title', 50, 150, /** @scrutinizer ignore-type */ $this->getVar('contents_title', 'e'));
Loading history...
126
        $contents_title->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_TITLE_DESC);
127
        $form->addElement($contents_title, true);
128
129
        // Category
130
        $catCriteria        = new \CriteriaCompo();
131
        $catCriteria->order = 'ASC';
132
        $catCriteria->setSort('category_order');
133
        $objects      = $categoryHandler->getList($catCriteria);
134
        $contents_cid = new \XoopsFormSelect(\_AM_XOOPSFAQ_E_CONTENTS_CATEGORY, 'contents_cid', $this->getVar('contents_cid', 'e'), 1, false);
135
        $contents_cid->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_CATEGORY_DESC);
136
        $contents_cid->addOptionArray($objects);
137
        $form->addElement($contents_cid);
138
139
        // Weight
140
        $contents_weight = new \XoopsFormText(\_AM_XOOPSFAQ_E_CONTENTS_WEIGHT, 'contents_weight', 5, 5, $this->getVar('contents_weight', 'e'));
141
        $contents_weight->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_WEIGHT_DESC);
142
        $form->addElement($contents_weight, false);
143
144
        // Editor
145
        $editorConfigs = [];
146
        $options_tray  = new \XoopsFormElementTray(\_AM_XOOPSFAQ_E_CONTENTS_CONTENT, '<br>');
147
        if (\class_exists('XoopsFormEditor')) {
148
            // $editorConfigs = array('editor' => $GLOBALS['xoopsConfig']['general_editor'],
149
            $editorConfigs     = [
150
                'editor' => $helper->getConfig('use_wysiwyg', 'dhtmltextarea'),
151
                'rows'   => 25,
152
                'cols'   => '100%',
153
                'width'  => '100%',
154
                'height' => '600px',
155
                'name'   => 'contents_contents',
156
                'value'  => $this->getVar('contents_contents', 'e'),
157
            ];
158
            $contents_contents = new \XoopsFormEditor('', 'contents_contents', $editorConfigs);
159
        } else {
160
            $contents_contents = new \XoopsFormDhtmlTextArea('', 'contents_contents', $this->getVar('contents_contents', 'e'), '100%', '100%');
0 ignored issues
show
Bug introduced by
'100%' of type string is incompatible with the type integer expected by parameter $rows of XoopsFormDhtmlTextArea::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
            $contents_contents = new \XoopsFormDhtmlTextArea('', 'contents_contents', $this->getVar('contents_contents', 'e'), /** @scrutinizer ignore-type */ '100%', '100%');
Loading history...
Bug introduced by
'100%' of type string is incompatible with the type integer expected by parameter $cols of XoopsFormDhtmlTextArea::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
            $contents_contents = new \XoopsFormDhtmlTextArea('', 'contents_contents', $this->getVar('contents_contents', 'e'), '100%', /** @scrutinizer ignore-type */ '100%');
Loading history...
Bug introduced by
It seems like $this->getVar('contents_contents', 'e') can also be of type array and array; however, parameter $value of XoopsFormDhtmlTextArea::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
            $contents_contents = new \XoopsFormDhtmlTextArea('', 'contents_contents', /** @scrutinizer ignore-type */ $this->getVar('contents_contents', 'e'), '100%', '100%');
Loading history...
161
        }
162
        $options_tray->addElement($contents_contents);
163
        $options_tray->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_CONTENT_DESC);
164
165
        \xoops_load('XoopsEditorHandler');
166
        $editorHandler = \XoopsEditorHandler::getInstance();
167
        $editorList    = $editorHandler->getList(true);
168
        if (isset($editorConfigs['editor']) && \in_array($editorConfigs['editor'], \array_flip($editorList), true)) {
169
            $form->addElement(new \XoopsFormHidden('dohtml', Constants::NOTSET));
170
            $form->addElement(new \XoopsFormHidden('dobr', Constants::SET));
171
        } else {
172
            $html_checkbox = new \XoopsFormCheckBox('', 'dohtml', $this->getVar('dohtml', 'e'));
173
            $html_checkbox->addOption(1, \_AM_XOOPSFAQ_E_DOHTML);
174
            $options_tray->addElement($html_checkbox);
175
176
            $breaks_checkbox = new \XoopsFormCheckBox('', 'dobr', $this->getVar('dobr', 'e'));
177
            $breaks_checkbox->addOption(1, \_AM_XOOPSFAQ_E_BREAKS);
178
            $options_tray->addElement($breaks_checkbox);
179
        }
180
181
        $doimage_checkbox = new \XoopsFormCheckBox('', 'doimage', $this->getVar('doimage', 'e'));
182
        $doimage_checkbox->addOption(1, \_AM_XOOPSFAQ_E_DOIMAGE);
183
        $options_tray->addElement($doimage_checkbox);
184
185
        $xcodes_checkbox = new \XoopsFormCheckBox('', 'doxcode', $this->getVar('doxcode', 'e'));
186
        $xcodes_checkbox->addOption(1, \_AM_XOOPSFAQ_E_DOXCODE);
187
        $options_tray->addElement($xcodes_checkbox);
188
189
        $smiley_checkbox = new \XoopsFormCheckBox('', 'dosmiley', $this->getVar('dosmiley', 'e'));
190
        $smiley_checkbox->addOption(1, \_AM_XOOPSFAQ_E_DOSMILEY);
191
        $options_tray->addElement($smiley_checkbox);
192
193
        $form->addElement($options_tray);
194
195
        $contents_publish = new \XoopsFormTextDateSelect(\_AM_XOOPSFAQ_E_CONTENTS_PUBLISH, 'contents_publish', 20, (int)$this->getVar('contents_publish'), $this->isNew());
0 ignored issues
show
Unused Code introduced by
The call to XoopsFormTextDateSelect::__construct() has too many arguments starting with $this->isNew(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
        $contents_publish = /** @scrutinizer ignore-call */ new \XoopsFormTextDateSelect(\_AM_XOOPSFAQ_E_CONTENTS_PUBLISH, 'contents_publish', 20, (int)$this->getVar('contents_publish'), $this->isNew());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
196
        $contents_publish->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_PUBLISH_DESC);
197
        $form->addElement($contents_publish);
198
199
        $form->addElement(new \XoopsFormButtonTray('contents_form', \_SUBMIT, 'submit'));
200
201
        return $form->render();
202
    }
203
204
    /**
205
     * Get the FAQ Active/Inactive icon to display
206
     *
207
     * @return string HTML <img> tag representing current active status
208
     */
209
    public function getActiveIcon(): string
210
    {
211
        if ($this->getVar('contents_active') > Constants::INACTIVE) {
212
            $icon = '<img src="' . Admin::iconUrl('green.gif', '16') . '" alt="' . \_YES . '">';
213
        } else {
214
            $icon = '<img src="' . Admin::iconUrl('red.gif', '16') . '" alt="' . \_NO . '">';
215
        }
216
217
        return $icon;
218
    }
219
220
    /**
221
     * Get the timestamp for when Content (FAQ) was published
222
     *
223
     * @param int|string $timestamp Unix timestamp
224
     *
225
     * @return string formatted timestamp on success, false on failure
226
     */
227
    public function getPublished($timestamp = ''): string
228
    {
229
        if (!$this->getVar('contents_publish')) {
230
            return '';
231
        }
232
233
        return \formatTimestamp($this->getVar('contents_publish'), $timestamp);
234
    }
235
}
236