Completed
Push — master ( bb5c69...2e7c79 )
by Gino
06:03 queued 02:48
created

TDMCreateXoopsCode::getXoopsCodeHandler()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 6
eloc 8
nc 4
nop 7
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: TDMCreateXoopsCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TDMCreateXoopsCode.
27
 */
28
class TDMCreateXoopsCode
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
    *  @static function &getInstance
32
    *  @param null
33
    */
34
    /**
35
     * @return TDMCreateXoopsCode
36
     */
37
    public static function &getInstance()
38
    {
39
        static $instance = false;
40
        if (!$instance) {
41
            $instance = new self();
42
        }
43
44
        return $instance;
45
    }
46
47
    /*
48
     *  @public function getXcSwitch
49
     *  @param $op
50
     *  @param $cases
51
     *  @param $defaultAfterCase
52
     *  @param $default
53
     *  @param $t - Indentation 
54
     *
55
     * @return string
56
     */
57
    public function getXcSwitch($op = '', $cases = array(), $defaultAfterCase = false, $default = false, $t = '')
58
    {
59
        $phpCodeSwitch = TDMCreatePhpCode::getInstance();
60
        $contentSwitch = $phpCodeSwitch->getPhpCodeCaseSwitch($cases, $defaultAfterCase, $default, $t);
61
62
        return $phpCodeSwitch->getPhpCodeSwitch($op, $contentSwitch, $t);
63
    }
64
65
    /*
66
    *  @public function getXcEqualsOperator
67
    *  @param $var
68
    *  @param $value
69
    *  @param $interlock
70
    *  @param $ref
71
    *  @param $t - Indentation 
72
    *
73
    *  @return string
74
    */
75
    public function getXcEqualsOperator($var, $value, $interlock = null, $ref = false, $t = '')
76
    {
77
        if (false === $ref) {
78
            $ret = "{$t}{$var} {$interlock}= {$value};\n";
79
        } else {
80
            $ret = "{$t}{$var} =& {$value};\n";
81
        }
82
83
        return $ret;
84
    }
85
86
    /*
87
    *  @public function getXcCPHeader
88
    *  @param null
89
    *  @return string
90
    */
91
    public function getXcCPHeader()
92
    {
93
        return "xoops_cp_header();\n";
94
    }
95
96
    /*
97
    *  @public function getXcCPFooter
98
    *  @param null
99
    *  @return string
100
    */
101
    public function getXcCPFooter()
102
    {
103
        return "xoops_cp_footer();\n";
104
    }
105
106
    /**
107
     *  @public function getXcLoad
108
     *
109
     *  @param $var
110
     *
111
     *  @return string
112
     */
113
    public function getXcLoad($var = '', $t = '')
114
    {
115
        return "{$t}xoops_load('{$var}');\n";
116
    }
117
118
    /**
119
     *  @public function getXcLoadLanguage
120
     *
121
     *  @param $lang
122
     *
123
     *  @return string
124
     */
125
    public function getXcLoadLanguage($lang, $t = '')
126
    {
127
        return "{$t}xoops_loadLanguage('{$lang}');\n";
128
    }
129
130
    /*
131
    *  @public function getXcAnchorFunction
132
    *  @param $anchor
133
    *  @param $name
134
    *  @param $vars
135
    *  @param $close
136
    *
137
    *  @return string
138
    */
139
    public function getXcAnchorFunction($anchor, $name, $vars, $close = false)
140
    {
141
        $semicolon = $close !== false ? ';' : '';
142
143
        return "\${$anchor}->{$name}({$vars}){$semicolon}";
144
    }
145
146
    /*
147
    *  @public function getXcSetVar
148
    *  @param $tableName
149
    *  @param $fieldName
150
    *  @param $var
151
    *  @return string
152
    */
153
    public function getXcSetVar($tableName, $fieldName, $var, $t = '')
154
    {
155
        return "{$t}\${$tableName}Obj->setVar('{$fieldName}', {$var});\n";
156
    }
157
158
    /*
159
    *  @public function getXcGetVar
160
    *  @param $varLeft
161
    *  @param $handle
162
    *  @param $var
163
    *  @param $isParam
164
    *
165
    *  @return string
166
    */
167
    public function getXcGetVar($varLeft = '', $handle = '', $var = '', $isParam = false, $t = '')
168
    {
169
        if (!$isParam) {
170
            $ret = "{$t}\${$varLeft} = \${$handle}->getVar('{$var}');\n";
171
        } else {
172
            $ret = "\${$handle}->getVar('{$var}')";
173
        }
174
175
        return $ret;
176
    }
177
178
    /*
179
    *  @public function getXcGroupPermForm
180
    *  @param $varLeft
181
    *  @param $formTitle
182
    *  @param $moduleId
183
    *  @param $permName
184
    *  @param $permDesc
185
    *  @param $filename
186
    *
187
    *  @return string
188
    */
189
    public function getXcGroupPermForm($varLeft = '', $formTitle = '', $moduleId = '', $permName = '', $permDesc = '', $filename = '', $t = '')
190
    {
191
        return "{$t}\${$varLeft} = new XoopsGroupPermForm({$formTitle}, {$moduleId}, {$permName}, {$permDesc}, {$filename});\n";
192
    }
193
194
    /*
195
    *  @public function getXcAddItem
196
    *  @param $varLeft
197
    *  @param $paramLeft
198
    *  @param $paramRight
199
    *
200
    *  @return string
201
    */
202
    public function getXcAddItem($varLeft = '', $paramLeft = '', $paramRight = '', $t = '')
203
    {
204
        return "{$t}\${$varLeft}->addItem({$paramLeft}, {$paramRight});\n";
205
    }
206
207
    /*
208
    *  @public function getXcGetGroupIds
209
    *  @param $var
210
    *  @param $param1
211
    *  @param $param2
212
    *  @param $param3
213
    *
214
    *  @return string
215
    */
216
    public function getXcGetGroupIds($var = '', $anchor = '', $param1 = null, $param2 = null, $param3 = null, $t = '')
217
    {
218
        return "{$t}\${$var} = \${$anchor}->getGroupIds({$param1}, {$param2}, {$param3});\n";
219
    }
220
221
    /*
222
    *  @public function getXcGetItemIds
223
    *  @param $var
224
    *  @param $param1
225
    *  @param $param2
226
    *  @param $param3
227
    *
228
    *  @return string
229
    */
230
    public function getXcGetItemIds($var = '', $anchor = '', $param1 = null, $param2 = null, $param3 = null, $t = '')
231
    {
232
        return "{$t}\${$var} = \${$anchor}->getItemIds({$param1}, {$param2}, {$param3});\n";
233
    }
234
235
    /*
236
    *  @public function getXcTextDateSelectSetVar
237
    *  @param $tableName
238
    *  @param $fieldName
239
    *  @return string
240
    */
241
    public function getXcTextDateSelectSetVar($tableName, $fieldName, $t = '')
242
    {
243
        return self::getXcSetVar($tableName, $fieldName, "strtotime(\$_POST['{$fieldName}'])", $t);
244
    }
245
246
    /*
247
    *  @public function getXcCheckBoxOrRadioYNSetVar
248
    *  @param $tableName
249
    *  @param $fieldName
250
    *  @return string
251
    */
252
    public function getXcCheckBoxOrRadioYNSetVar($tableName, $fieldName, $t = '')
253
    {
254
        return self::getXcSetVar($tableName, $fieldName, "((1 == \$_REQUEST['{$fieldName}']) ? '1' : '0')", $t);
255
    }
256
257
    /*
258
    *  @public function getXcMediaUploader
259
    *  @param $var
260
    *  @param $dirPath
261
    *  @param $tableName
262
    *  @param $moduleDirname
263
    *  @return string
264
    */
265
    public function getXcMediaUploader($var = '', $dirPath, $tableName, $moduleDirname, $t = '')
0 ignored issues
show
Unused Code introduced by
The parameter $tableName 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...
266
    {
267
        $mimetypes = self::getXcGetConfig($moduleDirname, 'mimetypes');
268
        $maxsize = self::getXcGetConfig($moduleDirname, 'maxsize');
269
270
        return "{$t}\${$var} = new XoopsMediaUploader({$dirPath}, {$mimetypes}, {$maxsize}, null, null);\n";
271
    }
272
273
    /*
274
    *  @public function getXcXoopsCaptcha
275
    *  @param $var
276
    *  @param $instance
277
    *
278
    *  @return string
279
    */
280
    public function getXcGetInstance($var = '', $instance = '', $t = '')
281
    {
282
        return "{$t}\${$var} = {$instance}::getInstance();\n";
283
    }
284
285
    /*
286
    *  @public function getXcXoopsCaptcha
287
    *  @param null
288
    *  @return string
289
    */
290
    public function getXcXoopsCaptcha($t = '')
291
    {
292
        return "{$t}\$xoopsCaptcha = XoopsCaptcha::getInstance();\n";
293
    }
294
295
    /*
296
    *  @public function getXcXoopsImgListArray
297
    *  @param $return
298
    *  @param $var
299
    *  @param $t
300
    *
301
    *  @return string
302
    */
303
    public function getXcXoopsImgListArray($return, $var, $t = '')
304
    {
305
        return "{$t}\${$return} = XoopsLists::getImgListAsArray( {$var} );\n";
306
    }
307
308
    /*
309
    *  @public function getXcGetConfig
310
    *  @param $moduleDirname
311
    *  @param $name
312
    *  @return string
313
    */
314
    public function getXcGetConfig($moduleDirname, $name)
315
    {
316
        return "\${$moduleDirname}->getConfig('{$name}')";
317
    }
318
319
    /*
320
    *  @public function getXcIdGetVar
321
    *  @param $lpFieldName
322
    *  @return string
323
    */
324
    public function getXcIdGetVar($lpFieldName, $t = '')
325
    {
326
        return "{$t}\${$lpFieldName}['id'] = \$i;\n";
327
    }
328
329
    /*
330
    *  @public function getXcGetVarAll
331
    *  @param $lpFieldName
332
    *  @param $rpFieldName
333
    *  @param $tableName
334
    *  @param $fieldName
335
    *  @return string
336
    */
337
    public function getXcGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName, $t = '')
338
    {
339
        return "{$t}\${$lpFieldName}['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n";
340
    }
341
342
    /*
343
    *  @public function getXoopsHandlerInstance
344
    *  @param $moduleDirname
345
    *  
346
    *  @return string
347
    */
348
    public function getXoopsHandlerInstance($moduleDirname, $t = '')
349
    {
350
        $ucfModuleDirname = ucfirst($moduleDirname);
351
        $ret = "{$t}// Get instance of module\n";
352
        $ret .= "{$t}\${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();\n";
353
354
        return $ret;
355
    }
356
357
    /*
358
    *  @public function getXoopsHandlerLine
359
    *  @param $moduleDirname
360
    *  @param $tableName
361
    *  @return string
362
    */
363
    public function getXoopsHandlerLine($moduleDirname, $tableName, $t = '')
364
    {
365
        return "{$t}\${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');\n";
366
    }
367
368
    /*
369
    *  @public function getXoopsClearHandler
370
    *  @param $left
371
    *  @param $ref
372
    *  @param $anchor
373
    *  @param $var
374
    *
375
    *  @return string
376
    */
377
    public function getXoopsClearHandler($left, $ref = '&', $anchor, $var, $t = '')
378
    {
379
        return "{$t}\${$left}Handler ={$ref} \${$anchor}->getHandler('{$var}');\n";
380
    }
381
382
    /*
383
    *  @public function getXoopsFormSelectExtraOptions
384
    *  @param $varSelect
385
    *  @param $caption
386
    *  @param $var
387
    *  @param $options
388
    *  @param $setExtra
389
    *  
390
    *  @return string
391
    */
392
    public function getXoopsFormSelectExtraOptions($varSelect = '', $caption = '', $var = '', $options = array(), $setExtra = true, $t = '')
393
    {
394
        $ret = "{$t}\${$varSelect} = new XoopsFormSelect({$caption}, '{$var}', \${$var});\n";
395
        if (false !== $setExtra) {
396
            $ret .= "{$t}\${$varSelect}->setExtra('{$setExtra}');\n";
397
        }
398
        foreach ($options as $key => $value) {
399
            $ret .= "{$t}\${$varSelect}->addOption('{$key}', {$value});\n";
400
        }
401
402
        return $ret;
403
    }
404
405
    /*
406
     *  @public function getXcUnameFromId
407
     *  @param $left
408
     *  @param $tableName
409
     *
410
     * @return string
411
     */
412
    public function getXcUnameFromId($left, $value, $t = '')
413
    {
414
        return "{$t}\${$left} = XoopsUser::getUnameFromId({$value});\n";
415
    }
416
417
    /*
418
    *  @public function getXcFormatTimeStamp
419
    *  @param $lpFieldName
420
    *  @param $rpFieldName
421
    *  @param $tableName
422
    *  @param $fieldName
423
    *  @return string
424
    */
425
    public function getXcFormatTimeStamp($left, $value, $format = 's', $t = '')
426
    {
427
        return "{$t}\${$left} = formatTimeStamp({$value}, '{$format}');\n";
428
    }
429
430
    /*
431
    *  @public function getXcTopicGetVar
432
    *  @param $lpFieldName
433
    *  @param $rpFieldName
434
    *  @param $tableName
435
    *  @param $tableNameTopic
436
    *  @param $fieldNameParent
437
    *  @param $fieldNameTopic
438
    *  @return string
439
    */
440
    public function getXcTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic, $t = '')
441
    {
442
        $pTopic = TDMCreatePhpCode::getInstance();
443
        $ret = $pTopic->getPhpCodeCommentLine('Get Var', $fieldNameParent, $t);
444
        $fieldParent = self::getXcGetVar('', "\${$tableName}All[\$i]", $fieldNameParent, true, '');
445
        $ret .= self::getXcGet($rpFieldName, $fieldParent, '', $tableNameTopic.'Handler', false, $t);
446
        $ret .= self::getXcGetVar("\${$lpFieldName}['{$rpFieldName}']", "\${$rpFieldName}", $fieldNameTopic, false, $t);
447
448
        return $ret;
449
    }
450
451
    /*
452
    *  @public function getXcParentTopicGetVar
453
    *  @param $moduleDirname
454
    *  @param $lpFieldName
455
    *  @param $rpFieldName
456
    *  @param $tableName
457
    *  @param $tableSoleNameTopic
458
    *  @param $tableNameTopic
459
    *  @param $fieldNameParent
460
    *  @return string
461
    */
462
    public function getXcParentTopicGetVar($moduleDirname, $lpFieldName, $rpFieldName, $tableName, $tableSoleNameTopic, $tableNameTopic, $fieldNameParent, $t = '')
463
    {
464
        $pParentTopic = TDMCreatePhpCode::getInstance();
465
        $parentTopic = $pParentTopic->getPhpCodeCommentLine('Get', $tableNameTopic.' Handler', $t."\t");
466
        $parentTopic .= self::getXoopsHandlerLine($moduleDirname, $tableNameTopic, $t."\t");
467
        $ret = $phpCodePHeader->getPhpCodeConditions("!isset(\${$tableNameTopic}Handler", '', '', $parentTopic, $elseGroups);
0 ignored issues
show
Bug introduced by
The variable $phpCodePHeader does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $elseGroups does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
468
        $ret .= self::getXcGetVarFromID("\${$lpFieldName}['{$rpFieldName}']", $tableNameTopic, $tableSoleNameTopic, $tableName, $fieldNameParent, $t);
469
470
        return $ret;
471
    }
472
473
    /*
474
    *  @public function getXcGetVarFromID
475
    *  @param $left
476
    *  @param $anchor
477
    *  @param $var
478
    *  @param $tableName
479
    *  @param $fieldName
480
    *  @return string
481
    */
482
    public function getXcGetVarFromID($left, $anchor, $var, $tableName, $fieldName, $t = '')
483
    {
484
        $pVarFromID = TDMCreatePhpCode::getInstance();
485
        $ret = $pVarFromID->getPhpCodeCommentLine('Get Var', $fieldName, $t);
486
        $getVarFromID = self::getXcGetVar('', "\${$tableName}All[\$i]", $fieldName, true, '');
487
        $rightGet = self::getXcAnchorFunction($anchor.'Handler', 'get'.$var.'FromId', $getVarFromID);
488
        $ret .= self::getXcEqualsOperator($left, $rightGet, null, false, $t);
489
490
        return $ret;
491
    }
492
493
    /*
494
    *  @public function getXcUploadImageGetVar
495
    *  @param $lpFieldName
496
    *  @param $rpFieldName
497
    *  @param $tableName
498
    *  @param $fieldName
499
    *  @return string
500
    */
501
    public function getXcUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName, $t = '')
502
    {
503
        $pUploadImage = TDMCreatePhpCode::getInstance();
504
        $ret = $pUploadImage->getPhpCodeCommentLine('Get Var', $fieldName, $t);
505
        $ret .= self::getXcGetVar($fieldName, "\${$tableName}All[\$i]", $fieldName, false, '');
506
        $ret .= $pUploadImage->getPhpCodeTernaryOperator("{$lpFieldName}['{$rpFieldName}']", "\${$fieldName}", "\${$fieldName}", "'blank.gif'", $t);
507
508
        return $ret;
509
    }
510
    /*
511
    *  @public function getXcUrlFileGetVar
512
    *  @param $lpFieldName
513
    *  @param $rpFieldName
514
    *  @param $tableName
515
    *  @param $fieldName
516
    *  @return string
517
    */
518
    public function getXcUrlFileGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
519
    {
520
        return self::getXcGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName);
521
    }
522
    /*
523
    *  @public function getXcTextAreaGetVar
524
    *  @param $lpFieldName
525
    *  @param $rpFieldName
526
    *  @param $tableName
527
    *  @param $fieldName
528
    *  @return string
529
    */
530
    public function getXcTextAreaGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName, $t = '')
531
    {
532
        $phpCodeTextArea = TDMCreatePhpCode::getInstance();
533
        $getVar = self::getXcGetVar('', "\${$tableName}All[\$i]", $fieldName, true, '');
534
535
        return "{$t}".$phpCodeTextArea->getPhpCodeStripTags("{$lpFieldName}['{$rpFieldName}']", $getVar, false, $t);
536
    }
537
538
    /*
539
    *  @public function getXcSelectUserGetVar
540
    *  @param $lpFieldName
541
    *  @param $rpFieldName
542
    *  @param $tableName
543
    *  @param $fieldName
544
    * @return string
545
    */
546
    public function getXcSelectUserGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName, $t = '')
547
    {
548
        return "{$t}\${$lpFieldName}['{$rpFieldName}'] = XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
549
    }
550
551
    /*
552
    *  @public function getXcTextDateSelectGetVar
553
    *  @param $lpFieldName
554
    *  @param $rpFieldName
555
    *  @param $tableName
556
    *  @param $fieldName
557
    *  @return string
558
    */
559
    public function getXcTextDateSelectGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName, $t = '')
560
    {
561
        return "{$t}\${$lpFieldName}['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
562
    }
563
564
    /*
565
    *  @public function getXcUserHeader
566
    *  @param $moduleDirname
567
    *  @param $tableName
568
    *  @return string
569
    */
570
    public function getXcXoopsOptionTemplateMain($moduleDirname, $tableName, $t = '')
571
    {
572
        return "{$t}\$GLOBALS['xoopsOption']['template_main'] = '{$moduleDirname}_{$tableName}.tpl';\n";
573
    }
574
575
    /*
576
    *  @public function getXcUserHeader
577
    *  @param $moduleDirname
578
    *  @param $tableName
579
    *  @return string
580
    */
581
    public function getXcUserHeader($moduleDirname, $tableName)
582
    {
583
        $phpCodeUserHeader = TDMCreatePhpCode::getInstance();
584
        $ret = $phpCodeUserHeader->getPhpCodeIncludeDir('__DIR__', 'header');
585
        $ret .= self::getXcXoopsOptionTemplateMain($moduleDirname, $tableName);
586
        $ret .= $phpCodeUserHeader->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
587
588
        return $ret;
589
    }
590
591
    /*
592
    *  @public function getXcPermissionsHeader
593
    *  @param null
594
    */
595
    /**
596
     * @return string
597
     */
598
    public function getXcPermissionsHeader()
599
    {
600
        $phpCodePHeader = TDMCreatePhpCode::getInstance();
601
        $ret = $phpCodePHeader->getPhpCodeCommentLine('Permission');
602
        $ret .= $phpCodePHeader->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
603
        $ret .= self::getXcEqualsOperator('$gpermHandler', "xoops_gethandler('groupperm')", true);
604
        $groups = self::getXcEqualsOperator('$groups', '$xoopsUser->getGroups()');
605
        $elseGroups = self::getXcEqualsOperator('$groups', 'XOOPS_GROUP_ANONYMOUS');
606
        $ret .= $phpCodePHeader->getPhpCodeConditions('is_object($xoopsUser)', '', $type = '', $groups, $elseGroups);
607
608
        return $ret;
609
    }
610
611
    /**
612
     *  @public function getXcGetFieldId
613
     *
614
     *  @param $fields
615
     *
616
     *  @return string
617
     */
618
    public function getXcGetFieldId($fields)
619
    {
620
        $fieldId = 'id';
621
        foreach (array_keys($fields) as $f) {
622
            $fieldName = $fields[$f]->getVar('field_name');
623
            if (0 == $f) {
624
                $fieldId = $fieldName;
625
            }
626
        }
627
628
        return $fieldId;
629
    }
630
631
    /**
632
     *  @public function getXcGetFieldName
633
     *
634
     *  @param $fields
635
     *
636
     *  @return string
637
     */
638
    public function getXcGetFieldName($fields)
639
    {
640
        foreach (array_keys($fields) as $f) {
641
            $fieldName = $fields[$f]->getVar('field_name');
642
        }
643
644
        return $fieldName;
0 ignored issues
show
Bug introduced by
The variable $fieldName 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...
645
    }
646
647
    /**
648
     *  @public function getXcGetFieldParentId
649
     *
650
     *  @param $fields
651
     *
652
     *  @return string
653
     */
654
    public function getXcGetFieldParentId($fields)
655
    {
656
        $fieldPid = 'pid';
657
        foreach (array_keys($fields) as $f) {
658
            $fieldName = $fields[$f]->getVar('field_name');
659
            if (1 == $fields[$f]->getVar('field_parent')) {
660
                $fieldPid = $fieldName;
661
            }
662
        }
663
664
        return $fieldPid;
665
    }
666
667
    /**
668
     *  @public function getXcUserSaveElements
669
     *
670
     *  @param $moduleDirname
671
     *  @param $tableName
672
     *  @param $fields
673
     *
674
     *  @return string
675
     */
676
    public function getXcUserSaveElements($moduleDirname, $tableName, $fields)
677
    {
678
        $axCodeUserSave = AdminXoopsCode::getInstance();
679
        $ret = '';
680
        foreach (array_keys($fields) as $f) {
681
            $fieldName = $fields[$f]->getVar('field_name');
682
            $fieldElement = $fields[$f]->getVar('field_element');
683
            if (1 == $fields[$f]->getVar('field_main')) {
684
                $fieldMain = $fieldName;
685
            }
686
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
687
                $ret .= self::getXcCheckBoxOrRadioYNSetVar($tableName, $fieldName);
688
            } elseif (13 == $fieldElement) {
689
                $ret .= $axCodeUserSave->getXcUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain);
0 ignored issues
show
Bug introduced by
The variable $fieldMain 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...
Bug introduced by
The method getXcUploadImageSetVar() does not exist on AdminXoopsCode. Did you maybe mean getAxcUploadImageSetVar()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
690
            } elseif (14 == $fieldElement) {
691
                $ret .= $axCodeUserSave->getXcUploadFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXcUploadFileSetVar() does not exist on AdminXoopsCode. Did you maybe mean getAxcUploadFileSetVar()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
692
            } elseif (15 == $fieldElement) {
693
                $ret .= self::getXcTextDateSelectSetVar($tableName, $fieldName);
694
            } else {
695
                $ret .= self::getXcSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
696
            }
697
        }
698
699
        return $ret;
700
    }
701
702
    /*
703
    *  @public function getXcXoopsRequest
704
    *  @param $left
705
    *  @param $var1
706
    *  @param $var2
707
    *  @param $type
708
    *  @param $metod
709
    *  @return string
710
    */
711
    public function getXcXoopsRequest($left = '', $var1 = '', $var2 = '', $type = 'String', $metod = false, $t = '')
712
    {
713
        $ret = '';
714
        $intVars = ($var2 != '') ? "'{$var1}', {$var2}" : "'{$var1}'";
715
        if ($type == 'String') {
716
            $ret .= "{$t}\${$left} = XoopsRequest::getString('{$var1}', '{$var2}');\n";
717
        } elseif ($type == 'Int') {
718
            $ret .= "{$t}\${$left} = XoopsRequest::getInt({$intVars});\n";
719
        } elseif ($type == 'Int' && $metod !== false) {
720
            $ret .= "{$t}\${$left} = XoopsRequest::getInt({$intVars}, '{$metod}');\n";
721
        }
722
723
        return $ret;
724
    }
725
726
    /**
727
     *  @public function getXcTplAssign     
728
     *
729
     *  @param $tplString
730
     *  @param $phpRender
731
     *  @param $leftIsString 
732
     *
733
     *  @return string
734
     */
735
    public function getXcTplAssign($tplString, $phpRender, $leftIsString = true, $t = '')
736
    {
737
        $assign = "{$t}\$GLOBALS['xoopsTpl']->assign(";
738
        if ($leftIsString === false) {
739
            $ret = $assign."{$tplString}, {$phpRender});\n";
740
        } else {
741
            $ret = $assign."'{$tplString}', {$phpRender});\n";
742
        }
743
744
        return $ret;
745
    }
746
747
    /**
748
     *  @public function getXcXoopsTplAppend
749
     *
750
     *  @param $tplString
751
     *  @param $phpRender
752
     *
753
     *  @return string
754
     */
755
    public function getXcXoopsTplAppend($tplString, $phpRender, $t = '')
756
    {
757
        return "{$t}\$GLOBALS['xoopsTpl']->append('{$tplString}', {$phpRender});\n";
758
    }
759
760
    /**
761
     *  @public function getXcXoopsTplAppendByRef
762
     *
763
     *  @param $tplString
764
     *  @param $phpRender
765
     *
766
     *  @return string
767
     */
768
    public function getXcXoopsTplAppendByRef($tplString, $phpRender, $t = '')
769
    {
770
        return "{$t}\$GLOBALS['xoopsTpl']->appendByRef('{$tplString}', {$phpRender});\n";
771
    }
772
773
    /**
774
     *  @public function getXcPath
775
     *
776
     *  @param $directory
777
     *  @param $filename
778
     *  @param $isParam
779
     *
780
     *  @return string
781
     */
782
    public function getXcPath($directory, $filename, $isParam = false, $t = '')
783
    {
784
        if (!$isParam) {
785
            $ret = "{$t}\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php');\n";
786
        } else {
787
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php')";
788
        }
789
790
        return $ret;
791
    }
792
793
    /**
794
     *  @public function getXcTplDisplay
795
     *
796
     *  @param $displayTpl
797
     *
798
     *  @return string
799
     */
800
    public function getXcTplDisplay($displayTpl = '{$templateMain}', $t = '')
801
    {
802
        return "{$t}\$GLOBALS['xoopsTpl']->display(\"db:{$displayTpl}\");\n";
803
    }
804
805
    /**
806
     *  @public function getXcGetInfo
807
     *
808
     *  @param $left
809
     *  @param $string
810
     *  @param $isParam
811
     *
812
     *  @return string
813
     */
814
    public function getXcGetInfo($left = '', $string, $isParam = false, $t = '')
815
    {
816
        if (!$isParam) {
817
            $ret = "{$t}\${$left} = \$GLOBALS['xoopsModule']->getInfo('{$string}');\n";
818
        } else {
819
            $ret = "\$GLOBALS['xoopsModule']->getInfo('{$string}')";
820
        }
821
822
        return $ret;
823
    }
824
825
    /**
826
     *  @public function getXcAddRight
827
     *
828
     *  @param $anchor
829
     *  @param $permString
830
     *  @param $var
831
     *  @param $groups
832
     *  @param $mid
833
     *  @param $isParam
834
     *
835
     *  @return string
836
     */
837
    public function getXcAddRight($anchor, $permString = '', $var = '', $groups = '', $mid = '', $isParam = false, $t = '')
838
    {
839
        if (!$isParam) {
840
            $ret = "{$t}\${$anchor}->addRight('{$permString}', {$var}, {$groups}, {$mid});\n";
841
        } else {
842
            $ret = "\${$anchor}->addRight('{$permString}', {$var}, {$groups}, {$mid})";
843
        }
844
845
        return $ret;
846
    }
847
848
    /**
849
     *  @public function getXcCheckRight
850
     *
851
     *  @param $anchor
852
     *  @param $permString
853
     *  @param $var
854
     *  @param $groups
855
     *  @param $mid
856
     *  @param $isParam
857
     *
858
     *  @return string
859
     */
860
    public function getXcCheckRight($anchor, $permString = '', $var = '', $groups = '', $mid = '', $isParam = false, $t = '')
861
    {
862
        if (!$isParam) {
863
            $ret = "{$t}{$anchor}->checkRight('{$permString}', {$var}, {$groups}, {$mid});\n";
864
        } else {
865
            $ret = "{$anchor}->checkRight('{$permString}', {$var}, {$groups}, {$mid})";
866
        }
867
868
        return $ret;
869
    }
870
871
    /**
872
     *  @public function getXcObjHandlerCreate
873
     *
874
     *  @param $tableName
875
     *
876
     *  @return string
877
     */
878
    public function getXcObjHandlerCreate($tableName, $t = '')
879
    {
880
        return "{$t}\${$tableName}Obj =& \${$tableName}Handler->create();\n";
881
    }
882
883
    /**
884
     *  @public function getXcObjHandlerCount
885
     *
886
     *  @param $tableName
887
     *
888
     *  @return string
889
     */
890
    public function getXcObjHandlerCount($tableName, $t = '')
891
    {
892
        $ucfTableName = ucfirst($tableName);
893
        $ret = "{$t}\${$tableName}Count = \${$tableName}Handler->getCount{$ucfTableName}();\n";
894
895
        return $ret;
896
    }
897
898
    /**
899
     *  @public function getXcClearCount
900
     *
901
     *  @param $tableName
902
     *
903
     *  @return string
904
     */
905
    public function getXcClearHandlerCount($left, $anchor = '', $params = '', $t = '')
906
    {
907
        $ret = "{$t}\${$left} = \${$anchor}Handler->getCount({$params});\n";
908
909
        return $ret;
910
    }
911
912
    /**
913
     *  @public function getXcObjHandlerAll
914
     *
915
     *  @param $tableName
916
     *  @param $fieldMain
917
     *  @param $start
918
     *  @param $limit
919
     *
920
     *  @return string
921
     */
922
    public function getXcObjHandlerAll($tableName, $fieldMain = '', $start = '0', $limit = '0', $t = '')
923
    {
924
        $ucfTableName = ucfirst($tableName);
925
        $startLimit = ($limit != '0') ? "{$start}, {$limit}" : '0';
926
        $params = ($fieldMain != '') ? "{$startLimit}, '{$fieldMain}'" : $startLimit;
927
        $ret = "{$t}\${$tableName}All = \${$tableName}Handler->getAll{$ucfTableName}({$params});\n";
928
929
        return $ret;
930
    }
931
932
    /**
933
     *  @public function getXcClearHandlerAll
934
     *
935
     *  @param $tableName
936
     *  @param $fieldMain
937
     *  @param $start
938
     *  @param $limit
939
     *
940
     *  @return string
941
     */
942
    public function getXcClearHandlerAll($left, $anchor = '', $params = '', $t = '')
943
    {
944
        $ret = "{$t}\${$left} = \${$anchor}Handler->getAll({$params});\n";
945
946
        return $ret;
947
    }
948
949
    /**
950
     *  @public function getXcGetValues
951
     *
952
     *  @param $tableName
953
     *  @param $tableSoleName
954
     *
955
     *  @return string
956
     */
957
    public function getXcGetValues($tableName, $tableSoleName, $index = 'i', $noArray = false, $t = '')
958
    {
959
        $ucfTableName = ucfirst($tableName);
960
        if (!$noArray) {
961
            $ret = "{$t}\${$tableSoleName} = \${$tableName}All[\${$index}]->getValues{$ucfTableName}();\n";
962
        } else {
963
            $ret = "{$t}\${$tableSoleName} = \${$tableName}->getValues{$ucfTableName}();\n";
964
        }
965
966
        return $ret;
967
    }
968
969
    /**
970
     *  @public function getXcSetVarsObjects
971
     *
972
     *  @param $moduleDirname
973
     *  @param $tableName
974
     *  @param $fields
975
     *
976
     *  @return string
977
     */
978
    public function getXcSetVarsObjects($moduleDirname, $tableName, $fields, $t = '')
0 ignored issues
show
Unused Code introduced by
The parameter $t 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...
979
    {
980
        $axCode = AdminXoopsCode::getInstance();
981
        $ret = '';
982
        foreach (array_keys($fields) as $f) {
983
            $fieldName = $fields[$f]->getVar('field_name');
984
            $fieldElement = $fields[$f]->getVar('field_element');
985
            $fieldMain = '';
986
            if (1 == $fields[$f]->getVar('field_main')) {
987
                $fieldMain = $fieldName;
988
            }
989
            if ($f > 0) { // If we want to hide field id
990
                switch ($fieldElement) {
991
                    case 5:
992
                    case 6:
993
                        $ret .= self::getXcCheckBoxOrRadioYNSetVar($tableName, $fieldName);
994
                        break;
995
                    case 11:
996
                        $ret .= $axCode->getAdminXoopsCodeImageListSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getAdminXoopsCodeImageListSetVar() does not seem to exist on object<AdminXoopsCode>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
997
                        break;
998
                    case 12:
999
                        $ret .= $axCode->getAdminXoopsCodeFileSetVar($moduleDirname, $tableName, $fieldName, true);
0 ignored issues
show
Bug introduced by
The method getAdminXoopsCodeFileSetVar() does not seem to exist on object<AdminXoopsCode>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1000
                        break;
1001
                    case 13:
1002
                        $ret .= $axCode->getAdminXoopsCodeUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain);
0 ignored issues
show
Bug introduced by
The method getAdminXoopsCodeUploadImageSetVar() does not seem to exist on object<AdminXoopsCode>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1003
                        break;
1004
                    case 14:
1005
                        $ret .= $axCode->getXcFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXcFileSetVar() does not seem to exist on object<AdminXoopsCode>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1006
                        break;
1007
                    case 15:
1008
                        $ret .= self::getXcTextDateSelectSetVar($tableName, $fieldName);
1009
                        break;
1010
                    default:
1011
                        $ret .= self::getXcSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1012
                        break;
1013
                }
1014
            }
1015
        }
1016
1017
        return $ret;
1018
    }
1019
1020
    /**
1021
     *  @public function getXcSecurity
1022
     *
1023
     *  @param $tableName
1024
     *
1025
     *  @return string
1026
     */
1027
    public function getXcSecurity($tableName, $t = '')
1028
    {
1029
        $phpCodeSecurity = TDMCreatePhpCode::getInstance();
1030
        $securityError = self::getXcSecurityErrors();
1031
        $implode = $phpCodeSecurity->getPhpCodeImplode(',', $securityError);
1032
        $content = "{$t}\t".self::getXcRedirectHeader($tableName.'.php', '', 3, $implode, $t);
0 ignored issues
show
Documentation introduced by
$t is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1033
        $securityCheck = self::getXcSecurityCheck();
1034
1035
        return $phpCodeSecurity->getPhpCodeConditions('!'.$securityCheck, '', '', $content, $t);
1036
    }
1037
1038
    /*
1039
    *  @public function getXcInsertData
1040
    *  @param $tableName
1041
    *  @param $language
1042
    *  @return string
1043
    */
1044
    public function getXcInsertData($tableName, $language, $t = '')
1045
    {
1046
        $phpCodeInsertData = TDMCreatePhpCode::getInstance();
1047
        $content = "{$t}\t".self::getXcRedirectHeader($tableName.'.php', '?op=list', 2, "{$language}FORM_OK");
1048
        $handlerInsert = self::getXcHandler($tableName, $tableName, false, true, false, 'Obj');
1049
1050
        return $phpCodeInsertData->getPhpCodeConditions($handlerInsert, '', '', $content, $t);
1051
    }
1052
1053
    /*
1054
    *  @public function getXcRedirectHeader
1055
    *  @param $directory
1056
    *  @param $options
1057
    *  @param $numb
1058
    *  @param $var
1059
    *  @param $isString
1060
    *
1061
    *  @return string
1062
    */
1063
    public function getXcRedirectHeader($directory, $options = '', $numb = '2', $var, $isString = true, $t = '')
1064
    {
1065
        if (!$isString) {
1066
            $ret = "{$t}redirect_header({$directory}, {$numb}, {$var});\n";
1067
        } else {
1068
            $ret = "{$t}redirect_header('{$directory}{$options}', {$numb}, {$var});\n";
1069
        }
1070
1071
        return $ret;
1072
    }
1073
1074
    /*
1075
    *  @public function getXcXoopsConfirm
1076
    *  @param $tableName
1077
    *  @param $language
1078
    *  @param $fieldId
1079
    *  @param $fieldMain    
1080
    *  @param $options
1081
    *
1082
    *  @return string
1083
    */
1084
    public function getXcXoopsConfirm($tableName, $language, $fieldId, $fieldMain, $options = 'delete', $t = '')
1085
    {
1086
        $stuOptions = strtoupper($options);
1087
        $ccFieldId = TDMCreateFile::getInstance()->getCamelCase($fieldId, false, true);
1088
        $phpXoopsConfirm = TDMCreatePhpCode::getInstance();
1089
        $array = "array('ok' => 1, '{$fieldId}' => \${$ccFieldId}, 'op' => '{$options}')";
1090
        $server = $phpXoopsConfirm->getPhpCodeGlobalsVariables('REQUEST_URI', 'SERVER');
1091
        $getVar = self::getXcGetVar('', $tableName.'Obj', $fieldMain, true, '');
1092
        $sprintf = $phpXoopsConfirm->getPhpCodeSprintf($language.'FORM_SURE_'.$stuOptions, $getVar);
1093
        $ret = "{$t}xoops_confirm({$array}, {$server}, {$sprintf});\n";
1094
1095
        return $ret;
1096
    }
1097
1098
    /*
1099
    *  @public function getXcAddStylesheet
1100
    *  @param $style
1101
    *  
1102
    *  @return string
1103
    */
1104
    public function getXcAddStylesheet($style = 'style', $t = '')
1105
    {
1106
        return "{$t}\$GLOBALS['xoTheme']->addStylesheet( \${$style}, null );\n";
1107
    }
1108
1109
    /*
1110
    *  @public function getXcSecurityCheck
1111
    *  @param $denial
1112
    *  @return boolean
1113
    */
1114
    public function getXcSecurityCheck($denial = '')
1115
    {
1116
        return "{$denial}\$GLOBALS['xoopsSecurity']->check()";
1117
    }
1118
1119
    /*
1120
    *  @public function getXcSecurityErrors
1121
    *  @param null
1122
    *  @return string
1123
    */
1124
    public function getXcSecurityErrors()
1125
    {
1126
        return "\$GLOBALS['xoopsSecurity']->getErrors()";
1127
    }
1128
1129
    /**
1130
     *  @public function getXcHtmlErrors
1131
     *
1132
     *  @param $tableName
1133
     *  @param $isParam
1134
     *  @param $obj
1135
     *
1136
     *  @return string
1137
     */
1138
    public function getXcHtmlErrors($tableName, $isParam = false, $obj = 'Obj', $t = '')
1139
    {
1140
        $ret = '';
0 ignored issues
show
Unused Code introduced by
$ret 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...
1141
        if ($isParam) {
1142
            $ret = "\${$tableName}{$obj}->getHtmlErrors()";
1143
        } else {
1144
            $ret = "{$t}\${$tableName}{$obj} =& \${$tableName}->getHtmlErrors();";
1145
        }
1146
1147
        return $ret;
1148
    }
1149
1150
    /**
1151
     *  @public function getXcObjHandlerCount
1152
     *
1153
     *  @param $left
1154
     *  @param $tableName
1155
     *  @param $obj
1156
     *
1157
     *  @return string
1158
     */
1159
    public function getXcGetForm($left, $tableName, $obj = '', $t = '')
1160
    {
1161
        $ucfTableName = ucfirst($tableName);
1162
1163
        return "{$t}\${$left} =& \${$tableName}{$obj}->getForm{$ucfTableName}();\n";
1164
    }
1165
1166
    /**
1167
     *  @public function getXcGet
1168
     *
1169
     *  @param $left
1170
     *  @param $var
1171
     *  @param $obj
1172
     *  @param $handler
1173
     *  @param $isParam
1174
     *
1175
     *  @return string
1176
     */
1177
    public function getXcGet($left, $var, $obj = '', $handler = 'Handler', $isParam = false, $t = '')
1178
    {
1179
        if ($isParam) {
1180
            $ret = "\${$left}{$handler}->get(\${$var})";
1181
        } else {
1182
            $ret = "{$t}\${$left}{$obj} =& \${$handler}->get(\${$var});\n";
1183
        }
1184
1185
        return $ret;
1186
    }
1187
1188
    /**
1189
     *  @public function getXcHandler
1190
     *
1191
     *  @param $left
1192
     *  @param $var
1193
     *  @param $obj
1194
     *  @param $isHandler
1195
     *
1196
     *  @return string
1197
     */
1198
    public function getXcInsert($left, $var, $obj = '', $isHandler = false)
1199
    {
1200
        $handler = ($isHandler === false) ? '' : 'Handler';
1201
        if ($obj != '') {
1202
            $ret = "\${$left}{$handler}->insert(\${$var}{$obj})";
1203
        } else {
1204
            $ret = "\${$left}{$handler}->insert(\${$var})";
1205
        }
1206
1207
        return $ret;
1208
    }
1209
1210
    /**
1211
     *  @public function getXcDelete
1212
     *
1213
     *  @param $tableName
1214
     *  @param $var
1215
     *  @param $obj
1216
     *  @param $isHandler
1217
     *
1218
     *  @return string
1219
     */
1220
    public function getXcDelete($left, $var, $obj = '', $isHandler = false)
1221
    {
1222
        $handler = $isHandler === false ? '' : 'Handler';
1223
        if ($obj != '') {
1224
            $ret = "\${$left}{$handler}->delete(\${$var}{$obj})";
1225
        } else {
1226
            $ret = "\${$left}{$handler}->delete(\${$var})";
1227
        }
1228
1229
        return $ret;
1230
    }
1231
1232
    /**
1233
     *  @public function getXcHandler
1234
     *
1235
     *  @param $left
1236
     *  @param $var
1237
     *
1238
     *  @return string
1239
     */
1240
    public function getXcHandler($left, $var, $get = false, $insert = false, $delete = false, $obj = '', $t = '')
1241
    {
1242
        if ($get) {
1243
            $ret = "{$t}\${$left}Handler->get(\${$var});";
1244
        } elseif ($insert && ($obj != '')) {
1245
            $ret = "{$t}\${$left}Handler->insert(\${$var}{$obj});";
1246
        } elseif ($delete && ($obj != '')) {
1247
            $ret = "{$t}\${$left}Handler->delete(\${$var}{$obj});";
1248
        }
1249
1250
        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...
1251
    }
1252
1253
    /*
1254
    *  @public function getTopicGetVar
1255
    *  @param $lpFieldName
1256
    *  @param $rpFieldName
1257
    *  @param $tableName
1258
    *  @param $tableNameTopic
1259
    *  @param $fieldNameParent
1260
    *  @param $fieldNameTopic
1261
    *  @return string
1262
    */
1263
    public function getTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic, $t = '')
1264
    {
1265
        $ret = TDMCreatePhpCode::getInstance()->getPhpCodeCommentLine('Get Var', $fieldNameParent, $t);
1266
        $paramGet = self::getXcGetVar('', "\${$tableName}All[\$i]", $fieldNameParent, true, '');
1267
        $ret .= self::getXcGet($rpFieldName, $paramGet, '', $tableNameTopic.'Handler', false, $t);
1268
        $ret .= self::getXcGetVar("\${$lpFieldName}['{$rpFieldName}']", "\${$rpFieldName}", $fieldNameTopic, false, $t);
1269
1270
        return $ret;
1271
    }
1272
1273
    /*
1274
    *  @public function getUploadImageGetVar
1275
    *  @param $lpFieldName
1276
    *  @param $rpFieldName
1277
    *  @param $tableName
1278
    *  @param $fieldName
1279
    *  @return string
1280
    */
1281
    public function getUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName, $t = '')
1282
    {
1283
        $pImageGetVar = TDMCreatePhpCode::getInstance();
1284
        $ret = $pImageGetVar->getPhpCodeCommentLine('Get Var', $fieldName, $t);
1285
        $ret .= self::getXcGetVar($fieldName, "\${$tableName}All[\$i]", $fieldName, false, '');
1286
        $ret .= $pImageGetVar->getPhpCodeTernaryOperator('uploadImage', "\${$fieldName}", "\${$fieldName}", "'blank.gif'", $t);
1287
        $ret .= self::getXcEqualsOperator("${$lpFieldName}['{$rpFieldName}']", '$uploadImage', null, false, $t);
1288
1289
        return $ret;
1290
    }
1291
1292
    /**
1293
     *  @public function getXcSaveFieldId
1294
     *
1295
     *  @param $fields
1296
     *
1297
     *  @return string
1298
     */
1299
    public function getXcSaveFieldId($fields)
1300
    {
1301
        $fieldId = '';
1302
        foreach (array_keys($fields) as $f) {
1303
            if (0 == $f) {
1304
                $fieldId = $fields[$f]->getVar('field_name');
1305
            }
1306
        }
1307
1308
        return $fieldId;
1309
    }
1310
1311
    /**
1312
     *  @public function getXcSaveFieldMain
1313
     *
1314
     *  @param $fields
1315
     *
1316
     *  @return string
1317
     */
1318
    public function getXcSaveFieldMain($fields)
1319
    {
1320
        $fieldMain = '';
1321
        foreach (array_keys($fields) as $f) {
1322
            if (1 == $fields[$f]->getVar('field_main')) {
1323
                $fieldMain = $fields[$f]->getVar('field_name');
1324
            }
1325
        }
1326
1327
        return $fieldMain;
1328
    }
1329
1330
    /**
1331
     *  @public function getXcSaveElements
1332
     *
1333
     *  @param $moduleDirname
1334
     *  @param $tableName
1335
     *  @param $fields
1336
     *
1337
     *  @return string
1338
     */
1339
    public function getXcSaveElements($moduleDirname, $tableName, $fields)
1340
    {
1341
        $axCodeSaveElements = AdminXoopsCode::getInstance();
1342
        $ret = '';
1343
        foreach (array_keys($fields) as $f) {
1344
            $fieldName = $fields[$f]->getVar('field_name');
1345
            $fieldElement = $fields[$f]->getVar('field_element');
1346
            $fieldMain = '';
1347
            if (1 == $fields[$f]->getVar('field_main')) {
1348
                $fieldMain = $fieldName;
1349
            }
1350
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
1351
                $ret .= self::getXcCheckBoxOrRadioYNSetVar($tableName, $fieldName);
1352
            } elseif (13 == $fieldElement) {
1353
                $ret .= $axCodeSaveElements->getAxcUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain);
1354
            } elseif (14 == $fieldElement) {
1355
                $ret .= $axCodeSaveElements->getAxcUploadFileSetVar($moduleDirname, $tableName, $fieldName);
1356
            } elseif (15 == $fieldElement) {
1357
                $ret .= self::getXcTextDateSelectSetVar($tableName, $fieldName);
1358
            } else {
1359
                $ret .= self::getXcSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1360
            }
1361
        }
1362
1363
        return $ret;
1364
    }
1365
1366
    /*
1367
    *  @public function getXcPageNav
1368
    *  @param $tableName
1369
    *
1370
    *  @return string
1371
    */
1372
    public function getXcPageNav($tableName, $t = '')
1373
    {
1374
        $phpCodePageNav = TDMCreatePhpCode::getInstance();
1375
        $classXCode = ClassXoopsCode::getInstance();
1376
        $ret = $phpCodePageNav->getPhpCodeCommentLine('Display Navigation', null, $t);
1377
        $condition = $phpCodePageNav->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/pagenav', true, false, 'include', $t."\t");
1378
        $condition .= $classXCode->getClassXoopsPageNav('pagenav', $tableName.'Count', 'limit', 'start', 'start', "'op=list&limit=' . \$limit", false, $t."\t");
1379
        $condition .= self::getXcTplAssign('pagenav', '$pagenav->renderNav(4)', true, $t."\t");
1380
        $ret .= $phpCodePageNav->getPhpCodeConditions("\${$tableName}Count", ' > ', '$limit', $condition, false, $t);
1381
1382
        return $ret;
1383
    }
1384
}
1385