Passed
Pull Request — master (#88)
by Michael
02:56
created

Modified::getForm()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 325
Code Lines 149

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 149
nc 72480
nop 4
dl 0
loc 325
rs 0
c 1
b 0
f 0

How to fix   Long Method    Complexity   

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\Tdmdownloads;
4
5
/**
6
 * TDMDownload
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
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright   Gregory Mage (Aka Mage)
16
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @author      Gregory Mage (Aka Mage)
18
 */
19
20
use XoopsModules\Tdmdownloads;
21
22
/**
23
 * Class Modified
24
 * @package XoopsModules\Tdmdownloads
25
 */
26
class Modified extends \XoopsObject
27
{
28
    // constructor
29
30
    public function __construct()
31
    {
32
        $this->initVar('requestid', \XOBJ_DTYPE_INT, null, false, 11);
33
34
        $this->initVar('lid', \XOBJ_DTYPE_INT, null, false, 11);
35
36
        $this->initVar('cid', \XOBJ_DTYPE_INT, null, false, 5);
37
38
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false);
39
40
        $this->initVar('url', \XOBJ_DTYPE_TXTBOX, null, false);
41
42
        $this->initVar('homepage', \XOBJ_DTYPE_TXTBOX, null, false);
43
44
        $this->initVar('version', \XOBJ_DTYPE_TXTBOX, null, false);
45
46
        $this->initVar('size', \XOBJ_DTYPE_TXTBOX, null, false);
47
48
        $this->initVar('platform', \XOBJ_DTYPE_TXTBOX, null, false);
49
50
        $this->initVar('logourl', \XOBJ_DTYPE_TXTBOX, null, false);
51
52
        $this->initVar('description', \XOBJ_DTYPE_TXTAREA, null, false);
53
54
        // Pour autoriser le html
55
56
        $this->initVar('dohtml', \XOBJ_DTYPE_INT, 1, false);
57
58
        $this->initVar('modifysubmitter', \XOBJ_DTYPE_INT, null, false, 11);
59
    }
60
61
    /**
62
     * @param null $db
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $db is correct as it would always require null to be passed?
Loading history...
63
     * @return int
64
     */
65
    public function getNewEnreg($db = null)
66
    {
67
        $newEnreg = 0;
68
69
        /** @var \XoopsMySQLDatabase $db */
70
71
        if (null !== $db) {
72
            $newEnreg = $db->getInsertId();
73
        }
74
75
        return $newEnreg;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $newEnreg also could return the type string which is incompatible with the documented return type integer.
Loading history...
76
    }
77
78
    /**
79
     * @param       $lid
80
     * @param       $erreur
81
     * @param array $donnee
82
     * @param bool  $action
83
     * @return \XoopsThemeForm
84
     */
85
    public function getForm($lid, $erreur, $donnee = [], $action = false)
86
    {
87
        global $xoopsDB, $xoopsModule, $xoopsUser;
88
89
        /** @var \XoopsModules\Tdmdownloads\Helper $helper */
90
91
        $helper = \XoopsModules\Tdmdownloads\Helper::getInstance();
92
93
        $moduleDirName = \basename(\dirname(__DIR__));
94
95
        if (!$action) {
96
            $action = $_SERVER['REQUEST_URI'];
97
        }
98
99
        $groups = \is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
100
101
        /** @var \XoopsGroupPermHandler $grouppermHandler */
102
        $grouppermHandler = \xoops_getHandler('groupperm');
103
104
        $perm_upload = $grouppermHandler->checkRight('tdmdownloads_ac', 32, $groups, $xoopsModule->getVar('mid'));
105
106
        //appel des class
107
108
        /** @var \XoopsModules\Tdmdownloads\DownloadsHandler $downloadsHandler */
109
110
        $downloadsHandler = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Downloads');
111
112
        //        $categoryHandler  = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Category');
113
114
        $viewDownloads = $downloadsHandler->get($lid);
115
116
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
117
118
        // affectation des variables
119
120
        if (true === $erreur) {
121
            $d_title = $donnee['title'];
122
123
            $d_cid = $donnee['cid'];
124
125
            $d_homepage = $donnee['homepage'];
126
127
            $d_version = $donnee['version'];
128
129
            $d_platform = $donnee['platform'];
130
131
            $d_description = $donnee['description'];
132
        } else {
133
            $d_title = $viewDownloads->getVar('title');
134
135
            $d_cid = $viewDownloads->getVar('cid');
136
137
            $d_homepage = $viewDownloads->getVar('homepage');
138
139
            $d_version = $viewDownloads->getVar('version');
140
141
            $d_platform = $viewDownloads->getVar('platform');
142
143
            $d_description = $viewDownloads->getVar('description', 'e');
144
        }
145
146
        //nom du formulaire
147
148
        $title = \sprintf(_AM_TDMDOWNLOADS_FORMEDIT);
149
150
        //création du formulaire
151
152
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
153
154
        $form->setExtra('enctype="multipart/form-data"');
155
156
        //titre
157
158
        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'title', 50, 255, $d_title), true);
159
160
        // fichier
161
162
        $fichier = new \XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMFILE, '<br><br>');
163
164
        $url = $viewDownloads->getVar('url');
165
166
        $formurl = new \XoopsFormText(_AM_TDMDOWNLOADS_FORMURL, 'url', 75, 255, $url);
0 ignored issues
show
Bug introduced by
It seems like $url 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

166
        $formurl = new \XoopsFormText(_AM_TDMDOWNLOADS_FORMURL, 'url', 75, 255, /** @scrutinizer ignore-type */ $url);
Loading history...
167
168
        $fichier->addElement($formurl, false);
169
170
        if ($perm_upload) {
171
            $fichier->addElement(new \XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD, 'attachedfile', $helper->getConfig('maxuploadsize')), false);
172
        }
173
174
        $form->addElement($fichier);
175
176
        //catégorie
177
178
        /** @var \XoopsModules\Tdmdownloads\CategoryHandler $categoryHandler */
179
180
        $categoryHandler = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Category');
181
182
        /** @var \XoopsModules\Tdmdownloads\Utility $utility */
183
184
        $utility = new \XoopsModules\Tdmdownloads\Utility();
185
186
        $categories = $utility->getItemIds('tdmdownloads_submit', $moduleDirName);
187
188
        $criteria = new \CriteriaCompo();
189
190
        $criteria->setSort('cat_weight ASC, cat_title');
191
192
        $criteria->setOrder('ASC');
193
194
        if ($xoopsUser) {
195
            if (!$xoopsUser->isAdmin($xoopsModule->mid())) {
196
                $criteria->add(new \Criteria('cat_cid', '(' . \implode(',', $categories) . ')', 'IN'));
197
            }
198
        } else {
199
            $criteria->add(new \Criteria('cat_cid', '(' . \implode(',', $categories) . ')', 'IN'));
200
        }
201
202
        $downloadscatArray = $categoryHandler->getAll($criteria);
203
204
        if (empty($downloadscatArray)) {
205
            \redirect_header('index.php', 2, \_NOPERM);
206
        }
207
208
        $mytree = new \XoopsModules\Tdmdownloads\Tree($downloadscatArray, 'cat_cid', 'cat_pid');
209
210
        $form->addElement($mytree->makeSelectElement('cid', 'cat_title', '--', $d_cid, true, 0, '', _AM_TDMDOWNLOADS_FORMINCAT), true);
211
212
        //affichage des champs
213
214
        $fieldHandler = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Field');
215
216
        $criteria = new \CriteriaCompo();
217
218
        $criteria->setSort('weight ASC, title');
219
220
        $criteria->setOrder('ASC');
221
222
        $downloads_field = $fieldHandler->getAll($criteria);
223
224
        foreach (\array_keys($downloads_field) as $i) {
225
            /** @var \XoopsModules\Tdmdownloads\Field[] $downloads_field */
226
227
            if (1 == $downloads_field[$i]->getVar('status_def')) {
228
                if (1 == $downloads_field[$i]->getVar('fid')) {
229
                    //page d'accueil
230
231
                    if (1 == $downloads_field[$i]->getVar('status')) {
232
                        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMHOMEPAGE, 'homepage', 50, 255, $d_homepage));
233
                    } else {
234
                        $form->addElement(new \XoopsFormHidden('homepage', ''));
235
                    }
236
                }
237
238
                if (2 == $downloads_field[$i]->getVar('fid')) {
239
                    //version
240
241
                    if (1 == $downloads_field[$i]->getVar('status')) {
242
                        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMVERSION, 'version', 10, 255, $d_version));
243
                    } else {
244
                        $form->addElement(new \XoopsFormHidden('version', ''));
245
                    }
246
                }
247
248
                if (3 == $downloads_field[$i]->getVar('fid')) {
249
                    //taille du fichier
250
251
                    if (1 == $downloads_field[$i]->getVar('status')) {
252
                        $size_value_arr = \explode(' ', $viewDownloads->getVar('size'));
253
254
                        $aff_size = new \XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMSIZE, '');
255
256
                        $aff_size->addElement(new \XoopsFormText('', 'sizeValue', 13, 13, $size_value_arr[0]));
257
258
                        if (!\array_key_exists(1, $size_value_arr)) {
259
                            $size_value_arr[1] = 'K';
260
                        }
261
262
                        $type = new \XoopsFormSelect('', 'sizeType', $size_value_arr[1]);
263
264
                        $typeArray = [
265
                            'B' => _AM_TDMDOWNLOADS_BYTES,
266
                            'K' => _AM_TDMDOWNLOADS_KBYTES,
267
                            'M' => _AM_TDMDOWNLOADS_MBYTES,
268
                            'G' => _AM_TDMDOWNLOADS_GBYTES,
269
                            'T' => _AM_TDMDOWNLOADS_TBYTES,
270
                        ];
271
272
                        $type->addOptionArray($typeArray);
273
274
                        $aff_size->addElement($type);
275
276
                        $form->addElement($aff_size);
277
                    } else {
278
                        $form->addElement(new \XoopsFormHidden('size', ''));
279
280
                        $form->addElement(new \XoopsFormHidden('type_size', ''));
281
                    }
282
                }
283
284
                if (4 == $downloads_field[$i]->getVar('fid')) {
285
                    //plateforme
286
287
                    if (1 == $downloads_field[$i]->getVar('status')) {
288
                        $platformselect = new \XoopsFormSelect(_AM_TDMDOWNLOADS_FORMPLATFORM, 'platform', \explode('|', $d_platform), 5, true);
289
290
                        $platformArray = \explode('|', $helper->getConfig('platform'));
0 ignored issues
show
Bug introduced by
It seems like $helper->getConfig('platform') can also be of type null; however, parameter $string of explode() 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

290
                        $platformArray = \explode('|', /** @scrutinizer ignore-type */ $helper->getConfig('platform'));
Loading history...
291
292
                        foreach ($platformArray as $platform) {
293
                            $platformselect->addOption((string)$platform, $platform);
294
                        }
295
296
                        $form->addElement($platformselect, false);
297
                        //$form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMPLATFORM, 'platform', 50, 255, $d_platform));
298
                    } else {
299
                        $form->addElement(new \XoopsFormHidden('platform', ''));
300
                    }
301
                }
302
            } else {
303
                $contenu = '';
304
305
                $fieldName = 'champ' . $downloads_field[$i]->getVar('fid');
306
307
                $fielddataHandler = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Fielddata');
308
309
                $criteria = new \CriteriaCompo();
310
311
                $criteria->add(new \Criteria('lid', $viewDownloads->getVar('lid')));
0 ignored issues
show
Bug introduced by
It seems like $viewDownloads->getVar('lid') can also be of type array and array; however, parameter $value of Criteria::__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

311
                $criteria->add(new \Criteria('lid', /** @scrutinizer ignore-type */ $viewDownloads->getVar('lid')));
Loading history...
312
313
                $criteria->add(new \Criteria('fid', $downloads_field[$i]->getVar('fid')));
314
315
                $downloadsfielddata = $fielddataHandler->getAll($criteria);
316
317
                foreach (\array_keys($downloadsfielddata) as $j) {
318
                    /** @var \XoopsModules\Tdmdownloads\Fielddata[] $downloadsfielddata */
319
320
                    if (true === $erreur) {
321
                        $contenu = $donnee[$fieldName];
322
                    } else {
323
                        $contenu = $downloadsfielddata[$j]->getVar('data');
324
                    }
325
                }
326
327
                if (1 == $downloads_field[$i]->getVar('status')) {
328
                    $form->addElement(new \XoopsFormText($downloads_field[$i]->getVar('title'), $fieldName, 50, 255, $contenu));
0 ignored issues
show
Bug introduced by
It seems like $downloads_field[$i]->getVar('title') can also be of type array and array; however, parameter $caption 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

328
                    $form->addElement(new \XoopsFormText(/** @scrutinizer ignore-type */ $downloads_field[$i]->getVar('title'), $fieldName, 50, 255, $contenu));
Loading history...
329
                } else {
330
                    $form->addElement(new \XoopsFormHidden($fieldName, ''));
331
                }
332
            }
333
        }
334
335
        //description
336
337
        $editorConfigs = [];
338
339
        $editorConfigs['name'] = 'description';
340
341
        $editorConfigs['value'] = $d_description;
342
343
        $editorConfigs['rows'] = 20;
344
345
        $editorConfigs['cols'] = 60;
346
347
        $editorConfigs['width'] = '100%';
348
349
        $editorConfigs['height'] = '400px';
350
351
        $editorConfigs['editor'] = $helper->getConfig('editor');
352
353
        $form->addElement(new \XoopsFormEditor(_AM_TDMDOWNLOADS_FORMTEXTDOWNLOADS, 'description', $editorConfigs), true);
354
355
        //image
356
357
        if ($helper->getConfig('useshots')) {
358
            $uploaddir = XOOPS_ROOT_PATH . '/uploads/' . $moduleDirName . '/images/shots/' . $viewDownloads->getVar('logourl');
359
360
            $categoryImage = $viewDownloads->getVar('logourl') ?: 'blank.gif';
361
362
            $uploadirectory = '/uploads/' . $moduleDirName . '/images/shots';
363
364
            if (!\is_file($uploaddir)) {
365
                $categoryImage = 'blank.gif';
366
            }
367
368
            $imgtray = new \XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMIMG, '<br>');
369
370
            $imgpath = \sprintf(_AM_TDMDOWNLOADS_FORMPATH, $uploadirectory);
371
372
            $imageselect = new \XoopsFormSelect($imgpath, 'logo_img', $categoryImage);
373
374
            $topics_array = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadirectory);
375
376
            foreach ($topics_array as $image) {
377
                $imageselect->addOption($image, $image);
378
            }
379
380
            $imageselect->setExtra("onchange='showImgSelected(\"image3\", \"logo_img\", \"" . $uploadirectory . '", "", "' . XOOPS_URL . "\")'");
381
382
            $imgtray->addElement($imageselect, false);
383
384
            $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadirectory . '/' . $categoryImage . "' name='image3' id='image3' alt=''>"));
385
386
            $fileseltray = new \XoopsFormElementTray('', '<br>');
387
388
            if ($perm_upload) {
389
                $fileseltray->addElement(new \XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD, 'attachedimage', $helper->getConfig('maxuploadsize')), false);
390
            }
391
392
            $imgtray->addElement($fileseltray);
393
394
            $form->addElement($imgtray);
395
        }
396
397
        $form->addElement(new \XoopsFormCaptcha(), true);
398
399
        $form->addElement(new \XoopsFormHidden('lid', $lid));
400
401
        //pour enregistrer le formulaire
402
403
        $form->addElement(new \XoopsFormHidden('op', 'save'));
404
405
        //bouton d'envoi du formulaire
406
407
        $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', 'submit', false));
408
409
        return $form;
410
    }
411
}
412