Contents::renderForm()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 114
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 80
c 1
b 0
f 0
nc 13
nop 0
dl 0
loc 114
rs 7.503

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 int    $contents_id;
0 ignored issues
show
introduced by
The private property $contents_id is not used, and could be removed.
Loading history...
37
    private int    $contents_cid;
0 ignored issues
show
introduced by
The private property $contents_cid is not used, and could be removed.
Loading history...
38
    private string $contents_title;
0 ignored issues
show
introduced by
The private property $contents_title is not used, and could be removed.
Loading history...
39
    private string $contents_contents;
0 ignored issues
show
introduced by
The private property $contents_contents is not used, and could be removed.
Loading history...
40
    private int    $contents_publish;
0 ignored issues
show
introduced by
The private property $contents_publish is not used, and could be removed.
Loading history...
41
    private int    $contents_weight;
0 ignored issues
show
introduced by
The private property $contents_weight is not used, and could be removed.
Loading history...
42
    private int    $contents_active;
0 ignored issues
show
introduced by
The private property $contents_active is not used, and could be removed.
Loading history...
43
    private int    $dohtml;
0 ignored issues
show
introduced by
The private property $dohtml is not used, and could be removed.
Loading history...
44
    private int    $doxcode;
0 ignored issues
show
introduced by
The private property $doxcode is not used, and could be removed.
Loading history...
45
    private int    $dosmiley;
0 ignored issues
show
introduced by
The private property $dosmiley is not used, and could be removed.
Loading history...
46
    private int    $doimage;
0 ignored issues
show
introduced by
The private property $doimage is not used, and could be removed.
Loading history...
47
    private int    $dobr;
0 ignored issues
show
introduced by
The private property $dobr is not used, and could be removed.
Loading history...
48
    /**
49
     * @var string contains this modules directory name
50
     */
51
    protected string $dirname;
52
53
    /**
54
     * Constructor
55
     */
56
    public function __construct()
57
    {
58
        $this->dirname = \basename(\dirname(__DIR__));
59
        \xoops_load('constants', $this->dirname);
60
61
        parent::__construct();
62
        $this->initVar('contents_id', \XOBJ_DTYPE_INT, null, false);
63
        $this->initVar('contents_cid', \XOBJ_DTYPE_INT, Constants::DEFAULT_CATEGORY, false);
64
        $this->initVar('contents_title', \XOBJ_DTYPE_TXTBOX, null, true, 255);
65
        $this->initVar('contents_contents', \XOBJ_DTYPE_TXTAREA, null, false);
66
        $this->initVar('contents_publish', \XOBJ_DTYPE_INT, \time(), false);
67
        $this->initVar('contents_weight', \XOBJ_DTYPE_INT, Constants::DEFAULT_WEIGHT, false);
68
        $this->initVar('contents_active', \XOBJ_DTYPE_INT, Constants::ACTIVE, false);
69
        $this->initVar('dohtml', \XOBJ_DTYPE_INT, Constants::SET, false);
70
        $this->initVar('doxcode', \XOBJ_DTYPE_INT, Constants::SET, false);
71
        $this->initVar('dosmiley', \XOBJ_DTYPE_INT, Constants::SET, false);
72
        $this->initVar('doimage', \XOBJ_DTYPE_INT, Constants::SET, false);
73
        $this->initVar('dobr', \XOBJ_DTYPE_INT, Constants::SET, false);
74
    }
75
76
    /**
77
     * Display Content (FAQ)
78
     *
79
     * @return string
80
     */
81
    public function __toString(): string
82
    {
83
        return (string)$this->getVar('contents_title', 's');
84
    }
85
86
    /**
87
     * Display the Content (FAQ) Editor form for Admin
88
     */
89
    public function displayForm(): void
90
    {
91
        echo $this->renderForm();
92
    }
93
94
    /**
95
     * Displays the Content (FAQ) Editor form for Admin
96
     */
97
    public function renderForm()
98
    {
99
        /** @var Helper $helper */
100
        $helper = Helper::getHelper($this->dirname);
101
        /** @var CategoryHandler $categoryHandler */
102
        $categoryHandler = $helper->getHandler('Category');
103
        $catCount        = $categoryHandler->getCount();
104
        if (empty($catCount)) {
105
            \xoops_error(\_AM_XOOPSFAQ_ERROR_NO_CATS_EXIST, '');
106
            \xoops_cp_footer();
107
            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...
108
        }
109
110
        require_once \dirname(__DIR__, 3) . '/class/xoopsformloader.php';
111
112
        if ($this->isNew()) {
113
            $caption = \_AM_XOOPSFAQ_CREATE_NEW;
114
        } else {
115
            $contentsTitle = $this->getVar('contents_title');
116
            if (\is_scalar($contentsTitle)) {
117
                $caption = \sprintf(\_AM_XOOPSFAQ_MODIFY_ITEM, $contentsTitle);
118
            } else {
119
                $caption = '';
120
            }
121
        }
122
123
        $form = new \XoopsThemeForm($caption, 'content', $_SERVER['REQUEST_URI'], 'post', true);
124
        //        $form->addElement(new \XoopsFormHiddenToken());
125
        $form->addElement(new \XoopsFormHidden('op', 'save'));
126
        $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

126
        $form->addElement(new \XoopsFormHidden('contents_id', /** @scrutinizer ignore-type */ $this->getVar('contents_id', 'e')));
Loading history...
127
128
        // Active
129
        $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

129
        $contents_active = new \XoopsFormRadioYN(\_AM_XOOPSFAQ_E_CONTENTS_ACTIVE, 'contents_active', /** @scrutinizer ignore-type */ $this->getVar('contents_active', 'e'), ' ' . \_YES, ' ' . \_NO);
Loading history...
130
        $contents_active->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_ACTIVE_DESC);
131
        $form->addElement($contents_active, false);
132
133
        // Title
134
        $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

134
        $contents_title = new \XoopsFormText(\_AM_XOOPSFAQ_E_CONTENTS_TITLE, 'contents_title', 50, 150, /** @scrutinizer ignore-type */ $this->getVar('contents_title', 'e'));
Loading history...
135
        $contents_title->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_TITLE_DESC);
136
        $form->addElement($contents_title, true);
137
138
        // Category
139
        $catCriteria        = new \CriteriaCompo();
140
        $catCriteria->order = 'ASC';
141
        $catCriteria->setSort('category_order');
142
        $objects      = $categoryHandler->getList($catCriteria);
143
        $contents_cid = new \XoopsFormSelect(\_AM_XOOPSFAQ_E_CONTENTS_CATEGORY, 'contents_cid', $this->getVar('contents_cid', 'e'), 1, false);
144
        $contents_cid->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_CATEGORY_DESC);
145
        $contents_cid->addOptionArray($objects);
146
        $form->addElement($contents_cid);
147
148
        // Weight
149
        $contents_weight = new \XoopsFormText(\_AM_XOOPSFAQ_E_CONTENTS_WEIGHT, 'contents_weight', 5, 5, $this->getVar('contents_weight', 'e'));
150
        $contents_weight->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_WEIGHT_DESC);
151
        $form->addElement($contents_weight, false);
152
153
        // Editor
154
        $editorConfigs = [];
155
        $options_tray  = new \XoopsFormElementTray(\_AM_XOOPSFAQ_E_CONTENTS_CONTENT, '<br>');
156
        if (\class_exists('XoopsFormEditor')) {
157
            // $editorConfigs = array('editor' => $GLOBALS['xoopsConfig']['general_editor'],
158
            $editorConfigs     = [
159
                'editor' => $helper->getConfig('use_wysiwyg', 'dhtmltextarea'),
160
                'rows'   => 25,
161
                'cols'   => '100%',
162
                'width'  => '100%',
163
                'height' => '600px',
164
                'name'   => 'contents_contents',
165
                'value'  => $this->getVar('contents_contents', 'e'),
166
            ];
167
            $contents_contents = new \XoopsFormEditor('', 'contents_contents', $editorConfigs);
168
        } else {
169
            $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

169
            $contents_contents = new \XoopsFormDhtmlTextArea('', 'contents_contents', $this->getVar('contents_contents', 'e'), /** @scrutinizer ignore-type */ '100%', '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

169
            $contents_contents = new \XoopsFormDhtmlTextArea('', 'contents_contents', /** @scrutinizer ignore-type */ $this->getVar('contents_contents', 'e'), '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

169
            $contents_contents = new \XoopsFormDhtmlTextArea('', 'contents_contents', $this->getVar('contents_contents', 'e'), '100%', /** @scrutinizer ignore-type */ '100%');
Loading history...
170
        }
171
        $options_tray->addElement($contents_contents);
172
        $options_tray->setDescription(\_AM_XOOPSFAQ_E_CONTENTS_CONTENT_DESC);
173
174
        \xoops_load('XoopsEditorHandler');
175
        $editorHandler = \XoopsEditorHandler::getInstance();
176
        $editorList    = $editorHandler->getList(true);
177
        if (isset($editorConfigs['editor']) && \in_array($editorConfigs['editor'], \array_flip($editorList), true)) {
178
            $form->addElement(new \XoopsFormHidden('dohtml', (string)Constants::NOTSET));
179
            $form->addElement(new \XoopsFormHidden('dobr', (string)Constants::SET));
180
        } else {
181
            $html_checkbox = new \XoopsFormCheckBox('', 'dohtml', $this->getVar('dohtml', 'e'));
182
            $html_checkbox->addOption('1', \_AM_XOOPSFAQ_E_DOHTML);
183
            $options_tray->addElement($html_checkbox);
184
185
            $breaks_checkbox = new \XoopsFormCheckBox('', 'dobr', $this->getVar('dobr', 'e'));
186
            $breaks_checkbox->addOption('1', \_AM_XOOPSFAQ_E_BREAKS);
187
            $options_tray->addElement($breaks_checkbox);
188
        }
189
190
        $doimage_checkbox = new \XoopsFormCheckBox('', 'doimage', $this->getVar('doimage', 'e'));
191
        $doimage_checkbox->addOption('1', \_AM_XOOPSFAQ_E_DOIMAGE);
192
        $options_tray->addElement($doimage_checkbox);
193
194
        $xcodes_checkbox = new \XoopsFormCheckBox('', 'doxcode', $this->getVar('doxcode', 'e'));
195
        $xcodes_checkbox->addOption('1', \_AM_XOOPSFAQ_E_DOXCODE);
196
        $options_tray->addElement($xcodes_checkbox);
197
198
        $smiley_checkbox = new \XoopsFormCheckBox('', 'dosmiley', $this->getVar('dosmiley', 'e'));
199
        $smiley_checkbox->addOption('1', \_AM_XOOPSFAQ_E_DOSMILEY);
200
        $options_tray->addElement($smiley_checkbox);
201
202
        $form->addElement($options_tray);
203
204
        $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

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