Passed
Push — master ( fd2af9...93b609 )
by Michael
16:23 queued 02:10
created

TDMDownloads_downloads::getForm()   F

Complexity

Conditions 53
Paths > 20000

Size

Total Lines 279

Duplication

Lines 59
Ratio 21.15 %

Importance

Changes 0
Metric Value
cc 53
nc 3059942400
nop 3
dl 59
loc 279
rs 0
c 0
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
2
/**
3
 * TDMDownload
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   Gregory Mage (Aka Mage)
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Gregory Mage (Aka Mage)
15
 */
16
17
if (!defined("XOOPS_ROOT_PATH")) {
18
    die("XOOPS root path not defined");
19
}
20
21
class TDMDownloads_downloads extends XoopsObject
22
{
23
// constructor
24
    public function __construct()
25
    {
26
        $this->initVar("lid",XOBJ_DTYPE_INT,null,false,11);
27
        $this->initVar("cid",XOBJ_DTYPE_INT,null,false,5);
28
        $this->initVar("title",XOBJ_DTYPE_TXTBOX, null, false);
29
        $this->initVar("url",XOBJ_DTYPE_TXTBOX, null, false);
30
        $this->initVar("homepage",XOBJ_DTYPE_TXTBOX, null, false);
31
        $this->initVar("version",XOBJ_DTYPE_TXTBOX, null, false);
32
        $this->initVar("size",XOBJ_DTYPE_TXTBOX, null, false);
33
        $this->initVar("platform",XOBJ_DTYPE_TXTBOX, null, false);
34
        $this->initVar("description",XOBJ_DTYPE_TXTAREA, null, false);
35
        // Pour autoriser le html
36
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
37
        $this->initVar("logourl",XOBJ_DTYPE_TXTBOX, null, false);
38
        $this->initVar("submitter",XOBJ_DTYPE_INT,null,false,11);
39
        $this->initVar("status",XOBJ_DTYPE_INT,null,false,2);
40
        $this->initVar("date",XOBJ_DTYPE_INT,null,false,10);
41
        $this->initVar("hits",XOBJ_DTYPE_INT,null,false,10);
42
        $this->initVar("rating",XOBJ_DTYPE_OTHER,null,false,10);
43
        $this->initVar("votes",XOBJ_DTYPE_INT,null,false,11);
44
        $this->initVar("comments",XOBJ_DTYPE_INT,null,false,11);
45
        $this->initVar("top",XOBJ_DTYPE_INT,null,false,2);
46
        $this->initVar("paypal",XOBJ_DTYPE_TXTBOX, null, false);
47
48
        //pour les jointures:
49
        $this->initVar("cat_title",XOBJ_DTYPE_TXTBOX, null, false);
50
        $this->initVar("cat_imgurl",XOBJ_DTYPE_TXTBOX, null, false);
51
    }
52
    public function get_new_enreg()
53
    {
54
        global $xoopsDB;
55
        $new_enreg = $xoopsDB->getInsertId();
56
57
        return $new_enreg;
58
    }
59
    public function TDMDownloads_downloads()
60
    {
61
        $this->__construct();
62
    }
63
    public function getForm($donnee = array(), $erreur = false, $action = false)
64
    {
65
        global $xoopsDB, $xoopsModule, $xoopsModuleConfig, $xoopsUser;
66
        if ($action === false) {
67
            $action = $_SERVER['REQUEST_URI'];
68
        }
69
        //permission pour uploader
70
        $gperm_handler = xoops_gethandler('groupperm');
71
        $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
72
        if ($xoopsUser) {
73
            if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
74
                $perm_upload = ($gperm_handler->checkRight('tdmdownloads_ac', 32, $groups, $xoopsModule->getVar('mid'))) ? true : false ;
75
            } else {
76
                $perm_upload = true;
77
            }
78
        } else {
79
            $perm_upload = ($gperm_handler->checkRight('tdmdownloads_ac', 32, $groups, $xoopsModule->getVar('mid'))) ? true : false ;
80
        }
81
        //nom du formulaire selon l'action (editer ou ajouter):
82
        $title = $this->isNew() ? sprintf(_AM_TDMDOWNLOADS_FORMADD) : sprintf(_AM_TDMDOWNLOADS_FORMEDIT);
83
84
        //cr�ation du formulaire
85
        $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
86
        $form->setExtra('enctype="multipart/form-data"');
87
        //titre
88
        $form->addElement(new XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'title', 50, 255, $this->getVar('title')), true);
89
        // fichier
90
        $fichier = new XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMFILE,'<br /><br />');
91
        $url = $this->isNew() ? 'http://' : $this->getVar('url');
92
        $formurl = new XoopsFormText(_AM_TDMDOWNLOADS_FORMURL, 'url', 75, 255, $url);
93
        $fichier->addElement($formurl,false);
94
        if ($perm_upload == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
95
            $fichier->addElement(new XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD , 'attachedfile', $xoopsModuleConfig['maxuploadsize']), false);
96
        }
97
        $form->addElement($fichier);
98
99
        //cat�gorie
100
        $downloadscat_Handler = xoops_getModuleHandler('tdmdownloads_cat', 'TDMDownloads');
101
        $categories = TDMDownloads_MygetItemIds('tdmdownloads_submit', 'TDMDownloads');
102
        $criteria = new CriteriaCompo();
103
        $criteria->setSort('cat_weight ASC, cat_title');
104
        $criteria->setOrder('ASC');
105
        if ($xoopsUser) {
106
            if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
107
                $criteria->add(new Criteria('cat_cid', '(' . implode(',', $categories) . ')','IN'));
108
            }
109
        } else {
110
            $criteria->add(new Criteria('cat_cid', '(' . implode(',', $categories) . ')','IN'));
111
        }
112
        $downloadscat_arr = $downloadscat_Handler->getall($criteria);
113
        if (count($downloadscat_arr) == 0) {
114
            redirect_header('index.php', 2,  _NOPERM);
115
        }
116
        $mytree = new XoopsObjectTree($downloadscat_arr, 'cat_cid', 'cat_pid');
117
        $form->addElement($mytree->makeSelectElement('cid', 'cat_title', '--', $this->getVar('cid'), true, 0, '', _AM_TDMDOWNLOADS_FORMINCAT), true);
118
119
        //affichage des champs
120
        $downloadsfield_Handler = xoops_getModuleHandler('tdmdownloads_field', 'TDMDownloads');
121
        $criteria = new CriteriaCompo();
122
        $criteria->setSort('weight ASC, title');
123
        $criteria->setOrder('ASC');
124
        $downloads_field = $downloadsfield_Handler->getall($criteria);
125
        foreach (array_keys($downloads_field) as $i) {
126
            if ($downloads_field[$i]->getVar('status_def') == 1) {
127
                if ($downloads_field[$i]->getVar('fid') == 1) {
128
                    //page d'accueil
129
                    if ($downloads_field[$i]->getVar('status') == 1) {
130
                        $form->addElement(new XoopsFormText(_AM_TDMDOWNLOADS_FORMHOMEPAGE, 'homepage', 50, 255, $this->getVar('homepage')));
131
                    } else {
132
                        $form->addElement(new XoopsFormHidden('homepage', ''));
133
                    }
134
                }
135
                if ($downloads_field[$i]->getVar('fid') == 2) {
136
                    //version
137
                    if ($downloads_field[$i]->getVar('status') == 1) {
138
                        $form->addElement(new XoopsFormText(_AM_TDMDOWNLOADS_FORMVERSION, 'version', 10, 255, $this->getVar('version')));
139
                    } else {
140
                        $form->addElement(new XoopsFormHidden('version', ''));
141
                    }
142
                }
143
                if ($downloads_field[$i]->getVar('fid') == 3) {
144
                    //taille du fichier
145
                    if ($downloads_field[$i]->getVar('status') == 1) {
146
                        if ($this->isNew()) {
147
                            $size_value = $this->getVar('size');
148
                            if ($erreur == false) {
149
                                $type_value = '[Ko]';
150
                            } else {
151
                                $type_value = $donnee['type_size'];
152
                            }
153
                        } else {
154
                            $size_value_arr = explode(' ', $this->getVar('size'));
155
                            $size_value = $size_value_arr[0];
156
                            if ($erreur == false) {
157
                                $type_value = $size_value_arr[1];
158
                            } else {
159
                                $type_value = $donnee['type_size'];
160
                            }
161
                        }
162
                        $aff_size = new XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMSIZE,'');
163
                        $aff_size->addElement(new XoopsFormText('', 'size', 10, 255, $size_value));
164
                        $type = new XoopsFormSelect('', 'type_size', $type_value);
165
                        $type_arr = array(_AM_TDMDOWNLOADS_BYTES => '['._AM_TDMDOWNLOADS_BYTES.']', _AM_TDMDOWNLOADS_KBYTES => '['._AM_TDMDOWNLOADS_KBYTES.']', _AM_TDMDOWNLOADS_MBYTES => '['._AM_TDMDOWNLOADS_MBYTES.']', _AM_TDMDOWNLOADS_GBYTES => '['._AM_TDMDOWNLOADS_GBYTES.']', _AM_TDMDOWNLOADS_TBYTES => '['._AM_TDMDOWNLOADS_TBYTES.']');
166
                        $type->addOptionArray($type_arr);
167
                        $aff_size->addElement($type);
168
                        $form->addElement($aff_size);
169
170
                    } else {
171
                        $form->addElement(new XoopsFormHidden('size', ''));
172
                        $form->addElement(new XoopsFormHidden('type_size', ''));
173
                    }
174
                }
175
                if ($downloads_field[$i]->getVar('fid') == 4) {
176
                    //plateforme
177
                    if ($downloads_field[$i]->getVar('status') == 1) {
178
                        $platformselect = new XoopsFormSelect(_AM_TDMDOWNLOADS_FORMPLATFORM, 'platform', explode('|',$this->getVar('platform')), 5, true);
179
                        $platform_array = explode('|',$xoopsModuleConfig['platform']);
180
                        foreach ($platform_array as $platform) {
181
                            $platformselect->addOption("$platform", $platform);
182
                        }
183
                        $form->addElement($platformselect, false);
184
                    } else {
185
                        $form->addElement(new XoopsFormHidden('platform', ''));
186
                    }
187
                }
188
            } else {
189
                $contenu = '';
190
                $contenu_iddata = '';
191
                $nom_champ = 'champ' . $downloads_field[$i]->getVar('fid');
192
                $downloadsfielddata_Handler = xoops_getModuleHandler('tdmdownloads_fielddata', 'TDMDownloads');
193
                $criteria = new CriteriaCompo();
194
                $criteria->add(new Criteria('lid', $this->getVar('lid')));
195
                $criteria->add(new Criteria('fid', $downloads_field[$i]->getVar('fid')));
196
                $downloadsfielddata = $downloadsfielddata_Handler->getall($criteria);
197
                foreach (array_keys($downloadsfielddata) as $j) {
198
                    if ($erreur == true) {
199
                        $contenu = $donnee[$nom_champ];
200
                    } else {
201
                        if (!$this->isNew()) {
202
                            $contenu = $downloadsfielddata[$j]->getVar('data');
203
                        }
204
                    }
205
                    $contenu_iddata = $downloadsfielddata[$j]->getVar('iddata');
206
                }
207
                $iddata = 'iddata' . $downloads_field[$i]->getVar('fid');
208
                if (!$this->isNew()) {
209
                    $form->addElement(new XoopsFormHidden($iddata, $contenu_iddata));
210
                }
211
                if ($downloads_field[$i]->getVar('status') == 1) {
212
                    $form->addElement(new XoopsFormText($downloads_field[$i]->getVar('title'), $nom_champ, 50, 255, $contenu));
213
                } else {
214
                    $form->addElement(new XoopsFormHidden($nom_champ, ''));
215
                }
216
            }
217
        }
218
        //description
219
        $editor_configs=array();
220
        $editor_configs["name"] ="description";
221
        $editor_configs["value"] = $this->getVar('description', 'e');
222
        $editor_configs["rows"] = 20;
223
        $editor_configs["cols"] = 100;
224
        $editor_configs["width"] = "100%";
225
        $editor_configs["height"] = "400px";
226
        $editor_configs["editor"] = $xoopsModuleConfig['editor'];
227
        $form->addElement( new XoopsFormEditor(_AM_TDMDOWNLOADS_FORMTEXTDOWNLOADS, "description", $editor_configs), true);
228
        //tag
229
        if (is_dir('../../tag') || is_dir('../tag')) {
230
            $dir_tag_ok = True;
231
        } else {
232
           $dir_tag_ok = False;
233
        }
234
        if (($xoopsModuleConfig['usetag'] == 1) and $dir_tag_ok) {
235
            $tagId = $this->isNew() ? 0 : $this->getVar('lid');
236
            if ($erreur == true) {
237
                $tagId = $donnee['TAG'];
238
            }
239
            require_once XOOPS_ROOT_PATH.'/modules/tag/class/formtag.php';
240
            $form->addElement(new TagFormTag('tag', 60, 255, $tagId, 0));
241
        }
242
243
        //image
244
        if ($xoopsModuleConfig['useshots']) {
245
            $uploaddir = XOOPS_ROOT_PATH . '/uploads/TDMDownloads/images/shots/' . $this->getVar('logourl');
246
            $downloadscat_img = $this->getVar('logourl') ? $this->getVar('logourl') : 'blank.gif';
247
            if (!is_file($uploaddir)) {
248
                $downloadscat_img = 'blank.gif';
249
            }
250
            $uploadirectory='/uploads/TDMDownloads/images/shots';
251
            $imgtray = new XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMIMG,'<br />');
252
            $imgpath=sprintf(_AM_TDMDOWNLOADS_FORMPATH, $uploadirectory );
253
            $imageselect= new XoopsFormSelect($imgpath, 'logo_img',$downloadscat_img);
254
            $topics_array = XoopsLists :: getImgListAsArray( XOOPS_ROOT_PATH . $uploadirectory );
255
            foreach ($topics_array as $image) {
256
                $imageselect->addOption("$image", $image);
257
            }
258
            $imageselect->setExtra( "onchange='showImgSelected(\"image3\", \"logo_img\", \"" . $uploadirectory . "\", \"\", \"" . XOOPS_URL . "\")'" );
259
            $imgtray->addElement($imageselect,false);
260
            $imgtray -> addElement( new XoopsFormLabel( '', "<br /><img src='" . XOOPS_URL . "/" . $uploadirectory . "/" . $downloadscat_img . "' name='image3' id='image3' alt='' />" ) );
261
            $fileseltray= new XoopsFormElementTray('','<br />');
262
            if ($perm_upload == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
263
                $fileseltray->addElement(new XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD , 'attachedimage', $xoopsModuleConfig['maxuploadsize']), false);
264
            }
265
            $imgtray->addElement($fileseltray);
266
            $form->addElement($imgtray);
267
        }
268
        // pour changer de poster et pour ne pas mettre � jour la date:
269
270
        if ($xoopsUser) {
271
            if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
272
                // auteur
273
                if ($this->isNew()) {
274
                    $submitter = !empty($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
275
                    $donnee['date_update'] = 0;
276
                } else {
277
                    $submitter = $this->getVar('submitter');
278
                    $v_date = $this->getVar('date');
279
                }
280
                if ($erreur == true) {
281
                    $date_update = $donnee['date_update'];
282
                    $v_status = $donnee['status'];
283
                    $submitter = $donnee['submitter'];
284
                } else {
285
                    $date_update = 'N';
286
                    $v_status = 1;
287
                }
288
                $form->addElement(new XoopsFormSelectUser(_AM_TDMDOWNLOADS_FORMSUBMITTER, 'submitter', true, $submitter, 1, false), true);
289
290
                // date
291
                if (!$this->isNew()) {
292
                    $selection_date = new XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMDATEUPDATE);
293
                    $date = new XoopsFormRadio('', 'date_update', $date_update);
294
                    $options = array('N' =>_AM_TDMDOWNLOADS_FORMDATEUPDATE_NO . ' (' . formatTimestamp($v_date,'s') . ')', 'Y' => _AM_TDMDOWNLOADS_FORMDATEUPDATE_YES);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $v_date does not seem to be defined for all execution paths leading up to this point.
Loading history...
295
                    $date->addOptionArray($options);
296
                    $selection_date->addElement($date);
297
                    $selection_date->addElement(new XoopsFormTextDateSelect('', 'date', '', time()));
298
                    $form->addElement($selection_date);
299
                }
300
                $status = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_FORMSTATUS, 'status', $v_status);
301
                $status->addOption(1, _AM_TDMDOWNLOADS_FORMSTATUS_OK);
302
                $form->addElement($status);
303
                //permissions pour t�l�charger
304
                if ($xoopsModuleConfig['permission_download'] == 2) {
305
                    $member_handler =  xoops_gethandler('member');
306
                    $group_list = $member_handler->getGroupList();
307
                    $gperm_handler = xoops_gethandler('groupperm');
308
                    $full_list = array_keys($group_list);
309
                    global $xoopsModule;
310
                    if (!$this->isNew()) {
311
                        $item_ids_download = $gperm_handler->getGroupIds('tdmdownloads_download_item', $this->getVar('lid'), $xoopsModule->getVar('mid'));
312
                        $item_ids_downloa = array_values($item_ids_download);
0 ignored issues
show
Unused Code introduced by
The assignment to $item_ids_downloa is dead and can be removed.
Loading history...
313
                        $item_news_can_download_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_FORMPERMDOWNLOAD, 'item_download[]', $item_ids_download);
314
                    } else {
315
                        $item_news_can_download_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_FORMPERMDOWNLOAD, 'item_download[]', $full_list);
316
                    }
317
                    $item_news_can_download_checkbox->addOptionArray($group_list);
318
                    $form->addElement($item_news_can_download_checkbox);
319
                }
320
            }
321
        }
322
        //paypal
323
        if ($xoopsModuleConfig['use_paypal'] == true) {
324
            $form->addElement(new XoopsFormText(_AM_TDMDOWNLOADS_FORMPAYPAL, 'paypal', 50, 255, $this->getVar('paypal')), false);
325
        } else {
326
            $form->addElement(new XoopsFormHidden('paypal', ''));
327
        }
328
        // captcha
329
        $form->addElement(new XoopsFormCaptcha(), true);
330
        // pour passer "lid" si on modifie la cat�gorie
331
        if (!$this->isNew()) {
332
            $form->addElement(new XoopsFormHidden('lid', $this->getVar('lid')));
333
            $form->addElement(new XoopsFormHidden('downloads_modified', true));
334
        }
335
        //pour enregistrer le formulaire
336
        $form->addElement(new XoopsFormHidden('op', 'save_downloads'));
337
        //bouton d'envoi du formulaire
338
        $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
339
340
        return $form;
341
    }
342
}
343
344
class TDMDownloadstdmdownloads_downloadsHandler extends XoopsPersistableObjectHandler
345
{
346
    public function __construct(&$db)
347
    {
348
        parent::__construct($db, "tdmdownloads_downloads", 'tdmdownloads_downloads', 'lid', 'title');
349
    }
350
}
351