Completed
Push — master ( 3f0046...b1c777 )
by Gino
06:41 queued 03:19
created

ClassFormElements::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
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
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: ClassFormElements.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class ClassFormElements.
27
 */
28
class ClassFormElements extends TDMCreateFile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
        $this->tdmcreate = TDMCreateHelper::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateHelper::getInstance() of type object<TDMCreateHelper> is incompatible with the declared type string of property $tdmcreate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /*
44
    *  @static function &getInstance
45
    *  @param null
46
    */
47
    /**
48
     * @return ClassFormElements
49
     */
50
    public static function &getInstance()
51
    {
52
        static $instance = false;
53
        if (!$instance) {
54
            $instance = new self();
55
        }
56
57
        return $instance;
58
    }
59
60
    /*
61
    *  @public function initForm
62
    *  @param string $module
63
    *  @param string $table
64
    */
65
    /**
66
     * @param $module
67
     * @param $table
68
     */
69
    public function initForm($module, $table)
70
    {
71
        $this->setModule($module);
72
        $this->setTable($table);
73
    }
74
75
    /*
76
    *  @private function getXoopsFormText
77
    *  @param string $language
78
    *  @param string $fieldName
79
    *  @param string $required
80
    */
81
    /**
82
     * @param $language
83
     * @param $fieldName
84
     * @param $required
85
     *
86
     * @return string
87
     */
88
    private function getXoopsFormText($language, $fieldName, $fieldDefault, $required = 'false')
89
    {
90
        $ucfFieldName = $this->getCamelCase($fieldName, true);
91
        $ccFieldName = $this->getCamelCase($fieldName, false, true);
92
        if ($fieldDefault != '') {
93
            $ret = <<<EOT
94
        // Form Text {$ucfFieldName}
95
		\${$ccFieldName} = \$this->isNew() ? '{$fieldDefault}' : \$this->getVar('{$fieldName}');
96
        \$form->addElement( new XoopsFormText({$language}, '{$fieldName}', 20, 150, \${$ccFieldName}){$required} );\n
97
EOT;
98
        } else {
99
            $ret = <<<EOT
100
        // Form Text {$ucfFieldName}
101
        \$form->addElement( new XoopsFormText({$language}, '{$fieldName}', 50, 255, \$this->getVar('{$fieldName}')){$required} );\n
102
EOT;
103
        }
104
105
        return $ret;
106
    }
107
108
    /*
109
    *  @private function getXoopsFormText
110
    *  @param string $language
111
    *  @param string $fieldName
112
    *  @param string $required
113
    */
114
    /**
115
     * @param        $language
116
     * @param        $fieldName
117
     * @param string $required
118
     *
119
     * @return string
120
     */
121
    private function getXoopsFormTextArea($language, $fieldName, $required = 'false')
122
    {
123
        $ret = <<<EOT
124
        // Form Text Area
125
        \$form->addElement( new XoopsFormTextArea({$language}, '{$fieldName}', \$this->getVar('{$fieldName}'), 4, 47){$required} );\n
126
EOT;
127
128
        return $ret;
129
    }
130
131
    /*
132
    *  @private function getXoopsFormDhtmlTextArea
133
    *  @param string $language
134
    *  @param string $moduleDirname
135
    *  @param string $fieldName
136
    *  @param string $required
137
    */
138
    /**
139
     * @param        $language
140
     * @param        $moduleDirname
141
     * @param        $fieldName
142
     * @param string $required
143
     *
144
     * @return string
145
     */
146
    private function getXoopsFormDhtmlTextArea($language, $moduleDirname, $fieldName, $required = 'false')
147
    {
148
        $rpFieldName = $this->getRightString($fieldName);
149
        $ret = <<<EOT
150
        // Form Dhtml Text Area
151
        \$editor_configs = array();
152
        \$editor_configs['name'] = '{$fieldName}';
153
        \$editor_configs['value'] = \$this->getVar('{$fieldName}', 'e');
154
        \$editor_configs['rows'] = 5;
155
        \$editor_configs['cols'] = 40;
156
        \$editor_configs['width'] = '100%';
157
        \$editor_configs['height'] = '400px';
158
        \$editor_configs['editor'] = \$this->{$moduleDirname}->getConfig('{$moduleDirname}_editor_{$rpFieldName}');
159
        \$form->addElement( new XoopsFormEditor({$language}, '{$fieldName}', \$editor_configs){$required} );\n
160
EOT;
161
162
        return $ret;
163
    }
164
165
    /*
166
    *  @private function getXoopsFormCheckBox
167
    *  @param string $language
168
    *  @param string $fieldName
169
    *  @param string $required
170
    */
171
    /**
172
     * @param        $language
173
     * @param        $fieldName
174
     * @param string $required
175
     *
176
     * @return string
177
     */
178
    private function getXoopsFormCheckBox($language, $tableSoleName, $fieldName, $fieldElementId, $required = 'false')
179
    {
180
        $stuTableSoleName = strtoupper($tableSoleName);
181
        $ucfFieldName = $this->getCamelCase($fieldName, true);
182
        $ccFieldName = $this->getCamelCase($fieldName, false, true);
183
        if (in_array(5, $fieldElementId) > 1) {
184
            $ret = <<<EOT
185
        // Form Check Box List Array
186
		\$checkOption          = \$this->getOptions();
187
        \$check{$ucfFieldName} = new XoopsFormCheckbox('<hr />', '{$tableSoleName}_option', \$checkOption, false);
188
        \$check{$ucfFieldName}->setDescription({$language}{$stuTableSoleName}_OPTIONS_DESC);
189
        foreach(\$this->options as \$option) {
190
            \$check{$ucfFieldName}->addOption(\$option, {$language}{$stuTableSoleName}_ . strtoupper(\$option));
191
        }
192
		\$form->addElement(\$check{$ucfFieldName}{$required} );\n
193
EOT;
194
        } else {
195
            $ret = <<<EOT
196
        // Form Check Box
197
        \${$ccFieldName} = \$this->isNew() ? 0 : \$this->getVar('{$fieldName}');
198
        \$check{$ucfFieldName} = new XoopsFormCheckBox({$language}, '{$fieldName}', \${$ccFieldName});
199
        \$check{$ucfFieldName}->addOption(1, " ");
200
        \$form->addElement( \$check{$ucfFieldName}{$required} );\n
201
EOT;
202
        }
203
204
        return $ret;
205
    }
206
207
    /*
208
    *  @private function getXoopsFormHidden
209
    *  @param string $fieldName
210
    */
211
    /**
212
     * @param $fieldName
213
     *
214
     * @return string
215
     */
216
    private function getXoopsFormHidden($fieldName)
217
    {
218
        $ret = <<<EOT
219
        // Form Hidden
220
        \$form->addElement( new XoopsFormHidden('{$fieldName}', \$this->getVar('{$fieldName}')) );\n
221
EOT;
222
223
        return $ret;
224
    }
225
226
    /*
227
    *  @private function getXoopsFormImageList
228
    *  @param string $language
229
    *  @param string $moduleDirname
230
    *  @param string $tableName
231
    *  @param string $fieldName
232
    *  @param string $required
233
    */
234
    /**
235
     * @param        $language
236
     * @param        $moduleDirname
237
     * @param        $tableName
238
     * @param        $tableSoleName
239
     * @param        $fieldName
240
     * @param string $required
241
     *
242
     * @return string
243
     */
244
    private function getXoopsFormImageList($language, $moduleDirname, $tableName, $tableSoleName, $fieldName, $required = 'false')
245
    {
246
        $stuTableName = strtoupper($tableName);
247
        $rpFieldName = $this->getRightString($fieldName);
248
        $stuSoleName = strtoupper($tableSoleName.'_'.$rpFieldName);
249
        $ucfFieldName = $this->getCamelCase($fieldName, true);
250
        $ccFieldName = $this->getCamelCase($fieldName, false, true);
251
        $ret = <<<EOT
252
        // Form Frameworks Image Files
253
        \$get{$ucfFieldName} = \$this->getVar('{$fieldName}');
254
        \${$ccFieldName} = \$get{$ucfFieldName} ? \$get{$ucfFieldName} : 'blank.gif';
255
        \$imageDirectory = '/Frameworks/moduleclasses/icons/32';
256
        \$imageTray = new XoopsFormElementTray({$language}{$stuSoleName},'<br />');
257
        \$imageSelect = new XoopsFormSelect(sprintf({$language}FORM_IMAGE_PATH, ".{\$imageDirectory}/"), '{$fieldName}', \${$ccFieldName}, 5);
258
        \$imageArray = XoopsLists::getImgListAsArray( XOOPS_ROOT_PATH . \$imageDirectory );
259
        foreach( \$imageArray as \$image1 ) {
260
            \$imageSelect->addOption("{\$image1}", \$image1);
261
        }
262
        \$imageSelect->setExtra( "onchange='showImgSelected(\"image1\", \"{$fieldName}\", \"".\$imageDirectory."\", \"\", \"".XOOPS_URL."\")'" );
263
        \$imageTray->addElement(\$imageSelect, false);
264
        \$imageTray->addElement( new XoopsFormLabel( '', "<br /><img src='".XOOPS_URL."/".\$imageDirectory."/".\${$ccFieldName}."' name='image1' id='image1' alt='' />" ) );
265
        // Form File
266
        \$fileSelectTray = new XoopsFormElementTray('','<br />');
267
        \$fileSelectTray->addElement(new XoopsFormFile({$language}FORM_IMAGE_LIST_{$stuTableName} , 'attachedfile', \$this->{$moduleDirname}->getConfig('maxsize')));
268
        \$fileSelectTray->addElement(new XoopsFormLabel(''));
269
        \$imageTray->addElement(\$fileSelectTray);
270
        \$form->addElement( \$imageTray{$required} );\n
271
EOT;
272
273
        return $ret;
274
    }
275
276
    /*
277
    *  @private function getXoopsFormSelectFile
278
    *  @param string $language
279
    *  @param string $moduleDirname
280
    *  @param string $fieldName
281
    *  @param string $fieldDefault
282
    *  @param string $fieldElement
283
    *  @param string $required
284
    */
285
    /**
286
     * @param        $language
287
     * @param        $moduleDirname
288
     * @param        $fieldName
289
     * @param string $required
290
     *
291
     * @return string
292
     */
293
    private function getXoopsFormSelectFile($language, $moduleDirname, $fieldName, $required = 'false')
294
    {
295
        $ucfFieldName = $this->getCamelCase($fieldName, true);
0 ignored issues
show
Unused Code introduced by
$ucfFieldName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
296
        $ccFieldName = $this->getCamelCase($fieldName, false, true);
297
        $ret = <<<EOT
298
        // Image Select or Upload
299
        if ( \$this->{$moduleDirname}->getConfig('useshots') ) {
300
            \${$ccFieldName} = \$this->getVar('{$fieldName}');
301
            \$uploadDirectory = '/uploads/{$moduleDirname}/images/shots';
302
            \${$moduleDirname}ShotImage = \${$fieldName} ? \${$fieldName} : 'blank.gif';
303
            //
304
            \$imageTray = new XoopsFormElementTray({$language}FORM_IMAGE,'<br />');
305
            \$imageSelect = new XoopsFormSelect(sprintf({$language}_FORM_PATH, \$uploadDirectory ), 'selected_image',\${$moduleDirname}ShotImage);
306
            \$imageArray = XoopsLists::getImgListAsArray( XOOPS_ROOT_PATH . \$uploadDirectory );
307
            foreach( \$imageArray as \$image ) {
308
                \$imageSelect->addOption("{\$image}", \$image);
309
            }
310
            \$imageSelect->setExtra( "onchange='showImgSelected(\"image3\", \"selected_image\", \"" . \$uploadDirectory . "\", \"\", \"" . XOOPS_URL . "\")'" );
311
            \$imageTray->addElement(\$imageSelect,false);
312
            \$imageTray -> addElement( new XoopsFormLabel( '', "<br /><img src='" . XOOPS_URL . "/" . \$uploadDirectory . "/" . \${$moduleDirname}ShotImage . "' name='image3' id='image3' alt='' />" ) );
313
            \$fileSelectTray= new XoopsFormElementTray('','<br />');
314
            //if (\$permissionUpload == true) {
315
                \$fileSelectTray->addElement(new XoopsFormFile({$language}_FORM_UPLOAD , 'attachedimage', \$this->{$moduleDirname}->getConfig('maxuploadsize')){$required});
316
            //}
317
            \$imageTray->addElement(\$fileSelectTray);
318
            \$form->addElement(\$imageTray);
319
        }\n
320
EOT;
321
322
        return $ret;
323
    }
324
325
    /*
326
    *  @private function getXoopsFormUrlFile
327
    *  @param string $language
328
    *  @param string $moduleDirname
329
    *  @param string $fieldName
330
    *  @param string $fieldDefault
331
    *  @param string $fieldElement
332
    *  @param string $required
333
    */
334
    /**
335
     * @param        $language
336
     * @param        $moduleDirname
337
     * @param        $fieldName
338
     * @param        $fieldDefault
339
     * @param        $fieldElement
340
     * @param string $required
341
     *
342
     * @return string
343
     */
344
    private function getXoopsFormUrlFile($language, $moduleDirname, $fieldName, $fieldDefault, $fieldElement, $required = 'false')
0 ignored issues
show
Unused Code introduced by
The parameter $fieldElement is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
345
    {
346
        $ret = <<<EOT
347
        // Form Url Text File
348
        \$formUrlFile = new XoopsFormElementTray({$language}FORM_FILE,'<br /><br />');
349
        \$formUrl     = \$this->isNew() ? '{$fieldDefault}' : \$this->getVar('{$fieldName}');
350
        \$formText    = new XoopsFormText({$language}FORM_TEXT, '{$fieldName}', 75, 255, \$formUrl);
351
        \$formUrlFile->addElement(\$formText{$required} );
352
        \$formUrlFile->addElement(new XoopsFormFile({$language}FORM_UPLOAD , 'attachedfile', \$this->{$moduleDirname}->getConfig('maxsize')){$required});
353
        \$form->addElement(\$formUrlFile);\n
354
EOT;
355
356
        return $ret;
357
    }
358
359
    /*
360
    *  @private function getXoopsFormUploadImage
361
    *  @param string $language
362
    *  @param string $moduleDirname
363
    *  @param string $tableName
364
    *  @param string $required
365
    */
366
    /**
367
     * @param        $language
368
     * @param        $moduleDirname
369
     * @param        $tableName
370
     * @param string $required
371
     *
372
     * @return string
373
     */
374
    private function getXoopsFormUploadImage($language, $moduleDirname, $tableName, $tableSoleName, $fieldName, $required = 'false')
375
    {
376
        $stuTableName = strtoupper($tableName);
377
        $stuSoleName = strtoupper($tableSoleName);
378
        $ucfFieldName = $this->getCamelCase($fieldName, true);
379
        $ccFieldName = $this->getCamelCase($fieldName, false, true);
380
        $ret = <<<EOT
381
        // Form Upload Image
382
        \$get{$ucfFieldName} = \$this->getVar('{$fieldName}');
383
        \${$ccFieldName} = \$get{$ucfFieldName} ? \$get{$ucfFieldName} : 'blank.gif';
384
        \$imageDirectory = '/uploads/{$moduleDirname}/images/{$tableName}';
385
        //
386
        \$imageTray   = new XoopsFormElementTray({$language}{$stuSoleName}_IMAGE,'<br />');
387
        \$imageSelect = new XoopsFormSelect(sprintf({$language}FORM_IMAGE_PATH, ".{\$imageDirectory}/"), '{$fieldName}', \${$ccFieldName}, 5);
388
        \$imageArray  = XoopsLists::getImgListAsArray( XOOPS_ROOT_PATH . \$imageDirectory );
389
        foreach( \$imageArray as \$image ) {
390
            \$imageSelect->addOption("{\$image}", \$image);
391
        }
392
        \$imageSelect->setExtra( "onchange='showImgSelected(\"image2\", \"{$fieldName}\", \"".\$imageDirectory."\", \"\", \"".XOOPS_URL."\")'" );
393
        \$imageTray->addElement(\$imageSelect, false);
394
        \$imageTray->addElement( new XoopsFormLabel( '', "<br /><img src='".XOOPS_URL."/".\$imageDirectory."/".\${$ccFieldName}."' name='image2' id='image2' alt='' />" ) );
395
        // Form File
396
        \$fileSelectTray = new XoopsFormElementTray('','<br />');
397
        \$fileSelectTray->addElement(new XoopsFormFile({$language}FORM_UPLOAD_IMAGE_{$stuTableName} , 'attachedfile', \$this->{$moduleDirname}->getConfig('maxsize')));
398
        \$fileSelectTray->addElement(new XoopsFormLabel(''));
399
        \$imageTray->addElement(\$fileSelectTray);
400
        \$form->addElement( \$imageTray{$required} );\n
401
EOT;
402
403
        return $ret;
404
    }
405
406
    /*
407
    *  @private function getXoopsFormUploadFile
408
    *  @param string $language
409
    *  @param string $moduleDirname
410
    *  @param string $tableName
411
    *  @param string $fieldName
412
    *  @param string $required
413
    */
414
    /**
415
     * @param        $language
416
     * @param        $moduleDirname
417
     * @param        $tableName
418
     * @param        $fieldName
419
     * @param string $required
420
     *
421
     * @return string
422
     */
423
    private function getXoopsFormUploadFile($language, $moduleDirname, $tableName, $fieldName, $required = 'false')
424
    {
425
        $stuTableName = strtoupper($tableName);
426
        $ret = <<<EOT
427
        // Form file
428
        \$form->addElement( new XoopsFormFile({$language}FORM_UPLOAD_FILE_{$stuTableName}, '{$fieldName}', \$this->{$moduleDirname}->getConfig('maxsize')){$required} );\n
429
EOT;
430
431
        return $ret;
432
    }
433
434
    /*
435
    *  @private function getXoopsFormColorPicker
436
    *  @param string $language
437
    *  @param string $fieldName
438
    *  @param string $required
439
    */
440
    /**
441
     * @param        $language
442
     * @param        $moduleDirname
443
     * @param        $fieldName
444
     * @param string $required
445
     *
446
     * @return string
447
     */
448
    private function getXoopsFormColorPicker($language, $moduleDirname, $fieldName, $required = 'false')
449
    {
450
        $ret = <<<EOT
451
        // Form Color Picker
452
        \$form->addElement( new XoopsFormColorPicker({$language}, '{$fieldName}', \$this->{$moduleDirname}->getConfig('maxsize')){$required} );\n
453
EOT;
454
455
        return $ret;
456
    }
457
458
    /*
459
    *  @private function getXoopsFormSelectBox
460
    *  @param string $language
461
    *  @param string $tableName
462
    *  @param string $fieldName
463
    *  @param string $required
464
    */
465
    /**
466
     * @param $language
467
     * @param $moduleDirname
468
     * @param $tableName
469
     * @param $fieldName
470
     * @param $required
471
     *
472
     * @return string
473
     */
474
    private function getXoopsFormSelectBox($language, $moduleDirname, $tableName, $fieldName, $required = 'false')
475
    {
476
        $ucfTableName = ucfirst($tableName);
477
        $ccFieldName = $this->getCamelCase($fieldName, false, true);
478
        $ret = <<<EOT
479
        // {$ucfTableName} handler
480
		\${$tableName}Handler =& \$this->{$moduleDirname}->getHandler('{$tableName}');
481
		// Form Select
482
        \${$ccFieldName}Select = new XoopsFormSelect({$language}, '{$fieldName}', \$this->getVar('{$fieldName}'));
483
        \${$ccFieldName}Select->addOption('Empty');
484
        \${$ccFieldName}Select->addOptionArray(\${$tableName}Handler->getList());
485
        \$form->addElement( \${$ccFieldName}Select{$required} );\n
486
EOT;
487
488
        return $ret;
489
    }
490
491
    /*
492
    *  @private function getXoopsFormSelectUser
493
    *  @param string $language
494
    *  @param string $fieldName
495
    *  @param string $required
496
    */
497
    /**
498
     * @param        $language
499
     * @param        $fieldName
500
     * @param string $required
501
     *
502
     * @return string
503
     */
504
    private function getXoopsFormSelectUser($language, $fieldName, $required = 'false')
505
    {
506
        $ret = <<<EOT
507
        // Form Select User
508
        \$form->addElement( new XoopsFormSelectUser({$language}, '{$fieldName}', false, \$this->getVar('{$fieldName}'), 1, false){$required} );\n
509
EOT;
510
511
        return $ret;
512
    }
513
514
    /*
515
    *  @private function getXoopsFormRadioYN
516
    *  @param string $language
517
    *  @param string $fieldName
518
    *  @param string $required
519
    */
520
    /**
521
     * @param        $language
522
     * @param        $fieldName
523
     * @param string $required
524
     *
525
     * @return string
526
     */
527
    private function getXoopsFormRadioYN($language, $fieldName, $required = 'false')
528
    {
529
        $ccFieldName = $this->getCamelCase($fieldName, false, true);
530
        $ret = <<<EOT
531
        // Form Radio Yes/No
532
        \${$ccFieldName} = \$this->isNew() ? 0 : \$this->getVar('{$fieldName}');
533
        \$form->addElement( new XoopsFormRadioYN({$language}, '{$fieldName}', \${$ccFieldName}){$required} );\n
534
EOT;
535
536
        return $ret;
537
    }
538
539
    /*
540
    *  @private function getXoopsFormTextDateSelect
541
    *  @param string $language
542
    *  @param string $fieldName
543
    *  @param string $required
544
    */
545
    /**
546
     * @param        $language
547
     * @param        $fieldName
548
     * @param string $required
549
     *
550
     * @return string
551
     */
552
    private function getXoopsFormTextDateSelect($language, $fieldName, $required = 'false')
553
    {
554
        $ret = <<<EOT
555
        // Form Text Date Select
556
        \$form->addElement( new XoopsFormTextDateSelect({$language}, '{$fieldName}', '', \$this->getVar('{$fieldName}')){$required} );\n
557
EOT;
558
559
        return $ret;
560
    }
561
562
    /*
563
    *  @private function getXoopsFormTable
564
    *  @param string $language
565
    *  @param string $moduleDirname
566
    *  @param string $table
567
    *  @param string $fields
568
    *  @param string $required
569
    */
570
    /**
571
     * @param        $language
572
     * @param        $moduleDirname
573
     * @param        $tableName
574
     * @param        $fieldName
575
     * @param        $fieldElement
576
     * @param string $required
577
     *
578
     * @return string
579
     */
580
    private function getXoopsFormTable($language, $moduleDirname, $tableName, $fieldName, $fieldElement, $required = 'false')
581
    {
582
        $ucfTableName = ucfirst($tableName);
583
        if ($fieldElement > 15) {
584
            $fElement = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
585
            $rpFieldelementName = strtolower(str_replace('Table : ', '', $fElement->getVar('fieldelement_name')));
586
            $ccFieldName = $this->getCamelCase($fieldName, false, true);
587
            $ret = <<<EOT
588
			// Form Table {$ucfTableName}
589
			\${$rpFieldelementName}Handler =& \$this->{$moduleDirname}->getHandler('{$rpFieldelementName}');
590
			\${$ccFieldName}Select = new XoopsFormSelect({$language}, '{$fieldName}', \$this->getVar('{$fieldName}'));
591
			\${$ccFieldName}Select->addOptionArray(\${$rpFieldelementName}Handler->getList());
592
			\$form->addElement( \${$ccFieldName}Select{$required} );\n		
593
EOT;
594
        }
595
596
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
597
    }
598
599
    /*
600
    *  @private function getXoopsFormTopic
601
    *  @param string $language
602
    *  @param string $moduleDirname
603
    *  @param string $table
604
    *  @param string $fields
605
    *  @param string $required
606
    */
607
    /**
608
     * @param        $language
609
     * @param        $moduleDirname
610
     * @param        $table
611
     * @param        $fields
612
     * @param string $required
613
     *
614
     * @return string
615
     */
616
    private function getXoopsFormTopic($language, $moduleDirname, $topicTableName, $fieldId, $fieldPid, $fieldMain, $required = 'false')
617
    {
618
        $ucfTopicTableName = ucfirst($topicTableName);
619
        $stlTopicTableName = strtolower($topicTableName);
620
        $ccFieldPid = $this->getCamelCase($fieldPid, false, true);
621
        $ret = <<<EOT
622
        // Form Topic {$ucfTopicTableName}
623
        \${$stlTopicTableName}Handler = \$this->{$moduleDirname}->getHandler('{$stlTopicTableName}');
624
        \$criteria = new CriteriaCompo();
625
        \${$stlTopicTableName}Count = \${$stlTopicTableName}Handler->getCount( \$criteria );
626
        if(\${$stlTopicTableName}Count) {
627
            include_once(XOOPS_ROOT_PATH . '/class/tree.php');
628
			\${$stlTopicTableName}All = \${$stlTopicTableName}Handler->getAll(\$criteria);
629
            \${$stlTopicTableName}Tree = new XoopsObjectTree( \${$stlTopicTableName}All, '{$fieldId}', '{$fieldPid}' );
630
            \${$ccFieldPid} = \${$stlTopicTableName}Tree->makeSelBox( '{$fieldPid}', '{$fieldMain}', '--', \$this->getVar('{$fieldPid}', 'e' ), true );
631
            \$form->addElement( new XoopsFormLabel ( {$language}, \${$ccFieldPid} ){$required} );
632
        }
633
		unset(\$criteria);\n
634
EOT;
635
636
        return $ret;
637
    }
638
639
    /*
640
    *  @private function getXoopsFormTag
641
    *  @param string $moduleDirname
642
    *  @param string $fieldId
643
    *  @param string $required
644
    */
645
    /**
646
     * @param        $moduleDirname
647
     * @param        $fieldId
648
     * @param string $required
649
     *
650
     * @return string
651
     */
652
    private function getXoopsFormTag($moduleDirname, $fieldId, $required = 'false')
653
    {
654
        $ret = <<<EOT
655
		// Use tag module
656
		\$dirTag = is_dir(XOOPS_ROOT_PATH . '/modules/tag') ? true : false;
657
        if ((\$this->{$moduleDirname}->getConfig('usetag') == 1) && \$dirTag){
658
            \$tagId = \$this->isNew() ? 0 : \$this->getVar('{$fieldId}');
659
            include_once XOOPS_ROOT_PATH.'/modules/tag/include/formtag.php';
660
            \$form->addElement(new XoopsFormTag('tag', 60, 255, \$tagId, 0){$required});
661
        }\n
662
EOT;
663
664
        return $ret;
665
    }
666
667
    /*
668
    *  @public function renderElements
669
    *  @param null
670
    */
671
    /**
672
     * @return string
673
     */
674
    public function renderElements()
675
    {
676
        $module = $this->getModule();
677
        $table = $this->getTable();
678
        $moduleDirname = $module->getVar('mod_dirname');
679
        $tableName = $table->getVar('table_name');
680
        $tableSoleName = $table->getVar('table_solename');
681
        $languageFunct = $this->getLanguage($moduleDirname, 'AM');
682
        //$language_table = $languageFunct . strtoupper($tableName);
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
683
        $ret = '';
684
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'), 'field_order ASC, field_id');
685
        $fieldId = '';
686
        $fieldIdTopic = '';
687
        $fieldPidTopic = '';
688
        $fieldMainTopic = '';
689
        $fieldElementId = array();
690
        foreach (array_keys($fields) as $f) {
691
            $fieldName = $fields[$f]->getVar('field_name');
692
            $fieldDefault = $fields[$f]->getVar('field_default');
693
            $fieldElement = $fields[$f]->getVar('field_element');
694
            $fieldParent = $fields[$f]->getVar('field_parent');
695
            $fieldInForm = $fields[$f]->getVar('field_inform');
696
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
697
                $fieldId = $fieldName;
698
            }
699
            $rpFieldName = $this->getRightString($fieldName);
700
            $language = $languageFunct.strtoupper($tableSoleName).'_'.strtoupper($rpFieldName);
701
            $required = (1 == $fields[$f]->getVar('field_required')) ? ', true' : '';
702
            //
703
            $fieldElements = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
704
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
705
            //
706
            if (1 == $fieldInForm) {
707
                // Switch elements
708
                switch ($fieldElement) {
709
                    case 1:
710
                        break;
711
                    case 2:
712
                        $ret .= $this->getXoopsFormText($language, $fieldName, $fieldDefault, $required);
713
                        break;
714
                    case 3:
715
                        $ret .= $this->getXoopsFormTextArea($language, $fieldName, $required);
716
                        break;
717
                    case 4:
718
                        $ret .= $this->getXoopsFormDhtmlTextArea($language, $moduleDirname, $fieldName, $required);
719
                        break;
720
                    case 5:
721
                        $ret .= $this->getXoopsFormCheckBox($language, $tableSoleName, $fieldName, $fieldElementId, $required);
722
                        break;
723
                    case 6:
724
                        $ret .= $this->getXoopsFormRadioYN($language, $fieldName, $required);
725
                        break;
726
                    case 7:
727
                        $ret .= $this->getXoopsFormSelectBox($language, $moduleDirname, $tableName, $fieldName, $required);
728
                        break;
729
                    case 8:
730
                        $ret .= $this->getXoopsFormSelectUser($language, $fieldName, $required);
731
                        break;
732
                    case 9:
733
                        $ret .= $this->getXoopsFormColorPicker($language, $moduleDirname, $fieldName, $required);
734
                        break;
735
                    case 10:
736
                        $ret .= $this->getXoopsFormImageList($languageFunct, $moduleDirname, $tableName, $tableSoleName, $fieldName, $required);
737
                        break;
738
                    case 11:
739
                        $ret .= $this->getXoopsFormSelectFile($language, $moduleDirname, $fieldName, $required);
740
                        break;
741
                    case 12:
742
                        $ret .= $this->getXoopsFormUrlFile($language, $moduleDirname, $fieldName, $fieldDefault, $fieldElement, $required);
743
                        break;
744
                    case 13:
745
                        $ret .= $this->getXoopsFormUploadImage($languageFunct, $moduleDirname, $tableName, $tableSoleName, $fieldName, $required);
746
                        break;
747
                    case 14:
748
                        $ret .= $this->getXoopsFormUploadFile($language, $moduleDirname, $tableName, $fieldName, $required);
749
                        break;
750
                    case 15:
751
                        $ret .= $this->getXoopsFormTextDateSelect($language, $moduleDirname, $fieldName, $required);
0 ignored issues
show
Unused Code introduced by
The call to ClassFormElements::getXoopsFormTextDateSelect() has too many arguments starting with $required.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
752
                        break;
753
                    default:
754
                        // If we use tag module
755
                        if (1 == $table->getVar('table_tag')) {
756
                            $ret .= $this->getXoopsFormTag($moduleDirname, $fieldId, $required);
757
                        }
758
                        // If we want to hide XoopsFormHidden() or field id
759
                        if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
760
                            $ret .= $this->getXoopsFormHidden($fieldName);
761
                        }
762
                        break;
763
                }
764
                if ($fieldElement > 15) {
765
                    if (1 == $table->getVar('table_category') || (1 == $fieldParent)) {
766
                        $fieldElements = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
767
                        $fieldElementMid = $fieldElements->getVar('fieldelement_mid');
768
                        $fieldElementTid = $fieldElements->getVar('fieldelement_tid');
769
                        $fieldElementName = $fieldElements->getVar('fieldelement_name');
770
                        $fieldNameDesc = substr($fieldElementName, strrpos($fieldElementName, ':'), strlen($fieldElementName));
771
                        $topicTableName = str_replace(': ', '', $fieldNameDesc);
772
                        $fieldsTopics = $this->getTableFields($fieldElementMid, $fieldElementTid);
773
                        foreach (array_keys($fieldsTopics) as $f) {
774
                            $fieldNameTopic = $fieldsTopics[$f]->getVar('field_name');
775
                            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
776
                                $fieldIdTopic = $fieldNameTopic;
777
                            }
778
                            if (1 == $fieldsTopics[$f]->getVar('field_parent')) {
779
                                $fieldPidTopic = $fieldNameTopic;
780
                            }
781
                            if (1 == $fieldsTopics[$f]->getVar('field_main')) {
782
                                $fieldMainTopic = $fieldNameTopic;
783
                            }
784
                        }
785
                        $ret .= $this->getXoopsFormTopic($language, $moduleDirname, $topicTableName, $fieldIdTopic, $fieldPidTopic, $fieldMainTopic, $required);
786
                    } else {
787
                        $ret .= $this->getXoopsFormTable($language, $moduleDirname, $tableName, $fieldName, $fieldElement, $required);
788
                    }
789
                }
790
            }
791
        }
792
        unset($fieldElementId);
793
794
        return $ret;
795
    }
796
}
797