Passed
Push — master ( 919ce4...d1101e )
by Michael
03:15 queued 11s
created

ListingForm::__construct()   C

Complexity

Conditions 9
Paths 30

Size

Total Lines 278
Code Lines 171

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 171
dl 0
loc 278
rs 6.4444
c 1
b 0
f 0
cc 9
nc 30
nop 1

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
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight\Form;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * Module: Adslight
19
 *
20
 * @category        Module
21
 * @package         adslight
22
 * @author          XOOPS Development Team <https://xoops.org>
23
 * @copyright       {@link https://xoops.org/ XOOPS Project}
24
 * @license         GPL 2.0 or later
25
 * @link            https://xoops.org/
26
 * @since           1.0.0
27
 */
28
29
use Xmf\Module\Helper\Permission;
30
use XoopsDatabaseFactory;
31
use XoopsModules\Adslight\{
32
    Helper,
33
    ItemvotedataHandler,
0 ignored issues
show
Bug introduced by
The type XoopsModules\Adslight\ItemvotedataHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
    TypeHandler,
35
    UservotedataHandler,
0 ignored issues
show
Bug introduced by
The type XoopsModules\Adslight\UservotedataHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
    Utility
37
};
38
use XoopsThemeForm;
39
40
require_once \dirname(__DIR__, 2) . '/include/common.php';
41
$moduleDirName = \basename(\dirname(__DIR__, 2));
42
//$helper = Helper::getInstance();
43
$permHelper = new Permission();
44
\xoops_load('XoopsFormLoader');
45
46
/**
47
 * Class ListingForm
48
 */
49
class ListingForm extends XoopsThemeForm
50
{
51
    public $targetObject;
52
    public $helper;
53
54
    /**
55
     * Constructor
56
     *
57
     * @param $target
58
     */
59
    public function __construct($target)
60
    {
61
        //  global $helper;
62
        $this->helper       = $target->helper;
63
        $this->targetObject = $target;
64
        $title              = $this->targetObject->isNew() ? \sprintf(\AM_ADSLIGHT_LISTING_ADD) : \sprintf(\AM_ADSLIGHT_LISTING_EDIT);
65
        parent::__construct($title, 'form', \xoops_getenv('SCRIPT_NAME'), 'post', true);
66
        $this->setExtra('enctype="multipart/form-data"');
67
        //include ID field, it's needed so the module knows if it is a new form or an edited form
68
        $hidden = new \XoopsFormHidden(
69
            'lid', $this->targetObject->getVar(
70
            'lid'
71
        )
72
        );
73
        $this->addElement($hidden);
74
        unset($hidden);
75
        // Lid
76
        $this->addElement(
77
            new \XoopsFormLabel(\AM_ADSLIGHT_LISTING_LID, $this->targetObject->getVar('lid'), 'lid')
78
        );
79
        // Cid
80
        //        $this->addElement(
81
        //            new \XoopsFormText(AM_ADSLIGHT_LISTING_CID, 'cid', 50, 255, $this->targetObject->getVar('cid')),
82
        //            false
83
        //        );
84
85
        //$categoriesHandler = $this->helper->getHandler('Categories');
86
        //$db     = \XoopsDatabaseFactory::getDatabaseConnection();
87
        /** @var \XoopsPersistableObjectHandler $categoriesHandler */
88
        $categoriesHandler = $this->helper->getHandler('Categories');
89
90
        $categories_id_select = new \XoopsFormSelect(\AM_ADSLIGHT_LISTING_CID, 'cid', $this->targetObject->getVar('cid'));
91
        $categories_id_select->addOptionArray($categoriesHandler->getList());
92
        $this->addElement($categories_id_select, false);
93
94
        // Title
95
        $this->addElement(
96
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_TITLE, 'title', 50, 255, $this->targetObject->getVar('title')),
97
            false
98
        );
99
        // Status
100
        $this->addElement(
101
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_STATUS, 'status', 50, 255, $this->targetObject->getVar('status')),
102
            false
103
        );
104
        // Expire
105
        $this->addElement(
106
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_EXPIRE, 'expire', 50, 255, $this->targetObject->getVar('expire')),
107
            false
108
        );
109
        // Type
110
        //$typeHandler = $this->helper->getHandler('Type');
111
        $db             = XoopsDatabaseFactory::getDatabaseConnection();
112
        $typeHandler    = new TypeHandler($db);
113
        $type_id_select = new \XoopsFormSelect(\AM_ADSLIGHT_LISTING_TYPE, 'type', $this->targetObject->getVar('type'));
114
        $type_id_select->addOptionArray($typeHandler->getList());
115
        $this->addElement($type_id_select, false);
116
        // Desctext
117
        if (\class_exists('XoopsFormEditor')) {
118
            $editorOptions           = [];
119
            $editorOptions['name']   = 'desctext';
120
            $editorOptions['value']  = $this->targetObject->getVar('desctext', 'e');
121
            $editorOptions['rows']   = 5;
122
            $editorOptions['cols']   = 40;
123
            $editorOptions['width']  = '100%';
124
            $editorOptions['height'] = '400px';
125
            //$editorOptions['editor'] = xoops_getModuleOption('adslight_editor', 'adslight');
126
            //$this->addElement( new \XoopsFormEditor(AM_ADSLIGHT_LISTING_DESCTEXT, 'desctext', $editorOptions), false  );
127
            if ($this->helper->isUserAdmin()) {
128
                $descEditor = new \XoopsFormEditor(
129
                    \AM_ADSLIGHT_LISTING_DESCTEXT, $this->helper->getConfig(
130
                    'adslightEditorAdmin'
131
                ),  $editorOptions, $nohtml = false, $onfailure = 'textarea'
132
                );
133
            } else {
134
                $descEditor = new \XoopsFormEditor(
135
                    \AM_ADSLIGHT_LISTING_DESCTEXT, $this->helper->getConfig(
136
                    'adslightEditorUser'
137
                ),  $editorOptions, $nohtml = false, $onfailure = 'textarea'
138
                );
139
            }
140
        } else {
141
            $descEditor = new \XoopsFormDhtmlTextArea(
142
                \AM_ADSLIGHT_LISTING_DESCTEXT, 'description', $this->targetObject->getVar(
143
                'description',
144
                'e'
145
            ),  '100%', '100%'
0 ignored issues
show
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

145
            ),  '100%', /** @scrutinizer ignore-type */ '100%'
Loading history...
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

145
            ),  /** @scrutinizer ignore-type */ '100%', '100%'
Loading history...
146
            );
147
        }
148
        $this->addElement($descEditor);
149
        // Tel
150
        $this->addElement(
151
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_TEL, 'tel', 50, 255, $this->targetObject->getVar('tel')),
152
            false
153
        );
154
        // Price
155
        $this->addElement(
156
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_PRICE, 'price', 50, 255, $this->targetObject->getVar('price')),
157
            false
158
        );
159
        // Typeprice
160
        $this->addElement(
161
            new \XoopsFormText(
162
                \AM_ADSLIGHT_LISTING_TYPEPRICE, 'typeprice', 50, 255, $this->targetObject->getVar(
163
                'typeprice'
164
            )
165
            ),
166
            false
167
        );
168
        // Typecondition
169
        $this->addElement(
170
            new \XoopsFormText(
171
                \AM_ADSLIGHT_LISTING_TYPEUSER, 'typecondition', 50, 255, $this->targetObject->getVar(
0 ignored issues
show
Bug introduced by
The constant AM_ADSLIGHT_LISTING_TYPEUSER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
172
                'typecondition'
173
            )
174
            ),
175
            false
176
        );
177
        // Date
178
        $this->addElement(
179
            new \XoopsFormTextDateSelect(
180
                \AM_ADSLIGHT_LISTING_DATE, 'date_created', 0, \formatTimestamp($this->targetObject->getVar('date_created'), 's')
0 ignored issues
show
Bug introduced by
formatTimestamp($this->t...r('date_created'), 's') of type string is incompatible with the type integer expected by parameter $value of XoopsFormTextDateSelect::__construct(). ( Ignorable by Annotation )

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

180
                \AM_ADSLIGHT_LISTING_DATE, 'date_created', 0, /** @scrutinizer ignore-type */ \formatTimestamp($this->targetObject->getVar('date_created'), 's')
Loading history...
181
            )
182
        );
183
        // Email
184
        $this->addElement(
185
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_EMAIL, 'email', 50, 255, $this->targetObject->getVar('email')),
186
            false
187
        );
188
        // Submitter
189
        $this->addElement(
190
            new \XoopsFormText(
191
                \AM_ADSLIGHT_LISTING_SUBMITTER, 'submitter', 50, 255, $this->targetObject->getVar(
192
                'submitter'
193
            )
194
            ),
195
            false
196
        );
197
        // Usid
198
        $this->addElement(
199
            new \XoopsFormSelectUser(
200
                \AM_ADSLIGHT_LISTING_USID, 'usid', false, $this->targetObject->getVar(
201
                'usid'
202
            ),  1, false
203
            ),
204
            false
205
        );
206
        // Town
207
        $this->addElement(
208
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_TOWN, 'town', 50, 255, $this->targetObject->getVar('town')),
209
            false
210
        );
211
        // Country
212
        $country      = new \XoopsFormSelect(
213
            \AM_ADSLIGHT_LISTING_COUNTRY, 'country', $this->targetObject->getVar(
214
            'country'
215
        )
216
        );
217
        $optionsArray = Utility::enumerate('adslight_listing', 'country');
218
        if (!\is_array($optionsArray)) {
219
            throw new \RuntimeException($optionsArray . ' must be an array.');
0 ignored issues
show
Bug introduced by
Are you sure $optionsArray of type false can be used in concatenation? ( Ignorable by Annotation )

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

219
            throw new \RuntimeException(/** @scrutinizer ignore-type */ $optionsArray . ' must be an array.');
Loading history...
220
        }
221
        foreach ($optionsArray as $enum) {
222
            $country->addOption($enum, (\defined($enum) ? \constant($enum) : $enum));
223
        }
224
        $this->addElement($country, false);
225
        // Contactby
226
        $this->addElement(
227
            new \XoopsFormText(
228
                \AM_ADSLIGHT_LISTING_CONTACTBY, 'contactby', 50, 255, $this->targetObject->getVar(
229
                'contactby'
230
            )
231
            ),
232
            false
233
        );
234
        // Premium
235
        $this->addElement(
236
            new \XoopsFormText(
237
                \AM_ADSLIGHT_LISTING_PREMIUM, 'premium', 50, 255, $this->targetObject->getVar(
238
                'premium'
239
            )
240
            ),
241
            false
242
        );
243
        // Valid
244
        $this->addElement(
245
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_VALID, 'valid', 50, 255, $this->targetObject->getVar('valid')),
246
            false
247
        );
248
        // Photo
249
        $photo       = $this->targetObject->getVar('photo') ?: 'blank.png';
250
        $uploadDir   = '/uploads/adslight/';
251
        $imgtray     = new \XoopsFormElementTray(\AM_ADSLIGHT_LISTING_PHOTO, '<br>');
252
        $imgpath     = \sprintf(\AM_ADSLIGHT_FORMIMAGE_PATH, $uploadDir);
253
        $imageselect = new \XoopsFormSelect($imgpath, 'photo', $photo);
254
        $imageArray  = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir);
255
        foreach ($imageArray as $image) {
256
            $imageselect->addOption($image, $image);
257
        }
258
        $imageselect->setExtra(
259
            "onchange='showImgSelected(\"image_photo\", \"photo\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'"
260
        );
261
        $imgtray->addElement($imageselect);
262
        $imgtray->addElement(
263
            new \XoopsFormLabel(
264
                '', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $photo . "' name='image_photo' id='image_photo' alt=''>"
265
            )
266
        );
267
        $fileseltray = new \XoopsFormElementTray('', '<br>');
268
        $fileseltray->addElement(
269
            new \XoopsFormFile(\AM_ADSLIGHT_FORMUPLOAD, 'photo', \xoops_getModuleOption('maxsize'))
0 ignored issues
show
Bug introduced by
xoops_getModuleOption('maxsize') of type boolean is incompatible with the type integer expected by parameter $maxfilesize of XoopsFormFile::__construct(). ( Ignorable by Annotation )

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

269
            new \XoopsFormFile(\AM_ADSLIGHT_FORMUPLOAD, 'photo', /** @scrutinizer ignore-type */ \xoops_getModuleOption('maxsize'))
Loading history...
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

269
            new \XoopsFormFile(\AM_ADSLIGHT_FORMUPLOAD, 'photo', /** @scrutinizer ignore-deprecated */ \xoops_getModuleOption('maxsize'))
Loading history...
270
        );
271
        $fileseltray->addElement(new \XoopsFormLabel(''));
272
        $imgtray->addElement($fileseltray);
273
        $this->addElement($imgtray);
274
        // Hits
275
        $this->addElement(
276
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_HITS, 'hits', 50, 255, $this->targetObject->getVar('hits')),
277
            false
278
        );
279
        // Item_rating
280
        $this->addElement(
281
            new \XoopsFormText(
282
                \AM_ADSLIGHT_LISTING_ITEM_RATING, 'item_rating', 50, 255, $this->targetObject->getVar(
283
                'item_rating'
284
            )
285
            ),
286
            false
287
        );
288
        // Item_votes
289
        //$itemvotedataHandler = $this->helper->getHandler('Itemvotedata');
290
        $db                     = \XoopsDatabaseFactory::getDatabaseConnection();
291
        $itemvotedataHandler    = new ItemvotedataHandler($db);
292
        $itemvotedata_id_select = new \XoopsFormSelect(
293
            \AM_ADSLIGHT_LISTING_ITEM_VOTES, 'item_votes', $this->targetObject->getVar(
294
            'item_votes'
295
        )
296
        );
297
        $itemvotedata_id_select->addOptionArray($itemvotedataHandler->getList());
298
        $this->addElement($itemvotedata_id_select, false);
299
        // User_rating
300
        $this->addElement(
301
            new \XoopsFormText(
302
                \AM_ADSLIGHT_LISTING_USER_RATING, 'user_rating', 50, 255, $this->targetObject->getVar(
303
                'user_rating'
304
            )
305
            ),
306
            false
307
        );
308
        // User_votes
309
        //$uservotedataHandler = $this->helper->getHandler('Uservotedata');
310
        $db                     = \XoopsDatabaseFactory::getDatabaseConnection();
311
        $uservotedataHandler    = new UservotedataHandler(
312
            $db
313
        );
314
        $uservotedata_id_select = new \XoopsFormSelect(
315
            \AM_ADSLIGHT_LISTING_USER_VOTES, 'user_votes', $this->targetObject->getVar(
316
            'user_votes'
317
        )
318
        );
319
        $uservotedata_id_select->addOptionArray($uservotedataHandler->getList());
320
        $this->addElement($uservotedata_id_select, false);
321
        // Comments
322
        $this->addElement(
323
            new \XoopsFormText(
324
                \AM_ADSLIGHT_LISTING_COMMENTS, 'comments', 50, 255, $this->targetObject->getVar(
325
                'comments'
326
            )
327
            ),
328
            false
329
        );
330
        // Remind
331
        $this->addElement(
332
            new \XoopsFormText(\AM_ADSLIGHT_LISTING_REMIND, 'remind', 50, 255, $this->targetObject->getVar('remind')),
333
            false
334
        );
335
        $this->addElement(new \XoopsFormHidden('op', 'save'));
336
        $this->addElement(new \XoopsFormButton('', 'submit', \_SUBMIT, 'submit'));
337
    }
338
}
339