Completed
Pull Request — master (#91)
by Gino
03:29
created

AdminXoopsCode::getAdminCodeCaseUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 12
nc 1
nop 5
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: AdminXoopsCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class AdminXoopsCode.
27
 */
28
class AdminXoopsCode
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
    * @var mixed
32
    */
33
    private $tf = null;
34
35
    /*
36
    * @var mixed
37
    */
38
    private $pc = null;
39
40
    /*
41
    * @var mixed
42
    */
43
    private $xc = null;
44
45
    /*
46
    *  @public function constructor
47
    *  @param null
48
    */
49
    /**
50
     *
51
     */
52
    public function __construct()
53
    {
54
        $this->tf = TDMCreateFile::getInstance();
55
        $this->pc = TDMCreatePhpCode::getInstance();
56
        $this->xc = TDMCreateXoopsCode::getInstance();
57
    }
58
59
    /*
60
    *  @static function &getInstance
61
    *  @param null
62
    */
63
    /**
64
     * @return AdminXoopsCode
65
     */
66
    public static function &getInstance()
67
    {
68
        static $instance = false;
69
        if (!$instance) {
70
            $instance = new self();
71
        }
72
73
        return $instance;
74
    }
75
76
    /*
77
     *  @public function getAdminTemplateMain
78
     *  @param $moduleDirname
79
     *  @param $tableName
80
     *
81
     *  @return string
82
     */
83
    public function getAdminTemplateMain($moduleDirname, $tableName, $t = '')
84
    {
85
        return "{$t}\$templateMain = '{$moduleDirname}_admin_{$tableName}.tpl';\n";
86
    }
87
88
    /*
89
     *  @public function getAdminTemplateMain
90
     *  @param $language
91
     *  @param $tableName
92
     *  @param $stuTableSoleName
93
     *  @param $type
94
     *
95
     *  @return string
96
     */
97
    public function getAdminItemButton($language, $tableName, $stuTableSoleName, $op = '?op=new', $type = 'add', $t = '')
98
    {
99
        $stuType = strtoupper($type);
100
        $aM = $t.'$adminMenu->addItemButton(';
101
        if ($type === 'add') {
102
            $ret = $aM."{$language}ADD_{$stuTableSoleName}, '{$tableName}.php{$op}', '{$type}');\n";
103
        } else {
104
            $ret = $aM."{$language}{$stuTableSoleName}_{$stuType}, '{$tableName}.php{$op}', '{$type}');\n";
105
        }
106
107
        return $ret;
108
    }
109
110
    /**
111
     *  @public function getAdminAddNavigation
112
     *
113
     *  @param $tableName
114
     *
115
     *  @return string
116
     */
117
    public function getAdminAddNavigation($tableName, $t = '')
118
    {
119
        return "{$t}\$adminMenu->addNavigation('{$tableName}.php')";
120
    }
121
122
    /*
123
    *  @public function getAdminXoopsCodeAddInfoBox
124
    *  @param $language
125
    *  
126
    *  @return string
127
    */
128
    public function getAdminXoopsCodeAddInfoBox($language, $t = '')
129
    {
130
        return "{$t}\$adminMenu->addInfoBox({$language});\n";
131
    }
132
133
    /*
134
    *  @public function getAdminXoopsCodeAddInfoBoxLine
135
    *  @param $language
136
    *  @param $label
137
    *  @param $var
138
    *  
139
    *  @return string
140
    */
141
    public function getAdminXoopsCodeAddInfoBoxLine($language, $label = '', $var = '', $t = '')
142
    {
143
        $aMenu = $t.'$adminMenu->addInfoBoxLine(';
144
        if ($var != '') {
145
            $ret = $aMenu."{$language}, '<label>'.{$label}.'</label>', {$var});\n";
146
        } else {
147
            $ret = $aMenu."{$language}, '<label>'.{$label}.'</label>');\n";
148
        }
149
150
        return $ret;
151
    }
152
153
    /*
154
    *  @public function getAdminXoopsCodeAddConfigBoxLine
155
    *  @param $language
156
    *  @param $label
157
    *  @param $var
158
    *  
159
    *  @return string
160
    */
161
    public function getAdminXoopsCodeAddConfigBoxLine($language, $label = '', $var = '', $t = '')
162
    {
163
        $aMenu = $t.'$adminMenu->addConfigBoxLine(';
164
        if ($var != '') {
165
            $ret = $aMenu."{$language}, '{$label}', {$var});\n";
166
        } else {
167
            $ret = $aMenu."{$language}, '{$label}');\n";
168
        }
169
170
        return $ret;
171
    }
172
173
    /*
174
    *  @public function getAdminXoopsCodeImageListSetVar
175
    *  @param string $moduleDirname
176
    *  @param string $tableName
177
    *  @param string $fieldName
178
    *  @return string
179
    */
180
    public function getAdminXoopsCodeImageListSetVar($moduleDirname, $tableName, $fieldName)
181
    {
182
        $ret = $this->pc->getPhpCodeCommentLine('Set Var', $fieldName);
183
        $ret .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true);
184
        $ret .= $this->xc->getAdminXoopsCodeMediaUploader('uploader', "XOOPS_ROOT_PATH . '/Frameworks/moduleclasses/icons/32'", $tableName, $moduleDirname);
185
        $fetchMedia = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])";
186
        $ifelse = "//\$uploader->setPrefix('{$fieldName}_');\n";
187
        $ifelse .= "//\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])\n";
188
        $contentElseInt = "\${$tableName}Obj->setVar('{$fieldName}', \$uploader->getSavedFileName());";
189
        $contentIf = "\$errors = \$uploader->getErrors();\n";
190
        $contentIf .= "redirect_header('javascript:history.go(-1)', 3, \$errors);\n";
191
        $ifelse .= $this->pc->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElseInt);
192
        $contentElseExt = "\${$tableName}Obj->setVar('{$fieldName}', \$_POST['{$fieldName}']);\n";
193
194
        $ret .= $this->pc->getPhpCodeConditions($fetchMedia, '', '', $ifelse, $contentElseExt);
195
196
        return $ret;
197
    }
198
199
    /*
200
    *  @public function getAdminXoopsCodeUploadImageSetVar
201
    *  @param string $moduleDirname
202
    *  @param string $tableName
203
    *  @param string $fieldName
204
    *  @return string
205
    */
206
    public function getAdminXoopsCodeUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain)
207
    {
208
        $stuModuleDirname = strtoupper($moduleDirname);
209
        $ret = $this->pc->getPhpCodeCommentLine('Set Var', $fieldName);
210
        $ret .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true);
211
        $ret .= $this->getAdminXoopsCodeMediaUploader('uploader', "{$stuModuleDirname}_UPLOAD_IMAGE_PATH", $tableName, $moduleDirname);
0 ignored issues
show
Bug introduced by
The method getAdminXoopsCodeMediaUploader() 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...
212
213
        $fetchMedia = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])";
214
        $ifelse = "\$extension = preg_replace( '/^.+\.([^.]+)$/sU' , '' , \$_FILES['attachedfile']['name']);\n";
215
        $ifelse .= "\$imgName = str_replace(' ', '', \$_POST['{$fieldMain}']).'.'.\$extension;\n";
216
        $ifelse .= "\$uploader->setPrefix(\$imgName);\n";
217
        $ifelse .= "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0]);\n";
218
        $contentElseInt = "\${$tableName}Obj->setVar('{$fieldName}', \$uploader->getSavedFileName());";
219
        $contentIf = "\$errors = \$uploader->getErrors();\n";
220
        $contentIf .= "redirect_header('javascript:history.go(-1)', 3, \$errors);\n";
221
        $ifelse .= $this->pc->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElseInt);
222
        $contentElseExt = "\${$tableName}Obj->setVar('{$fieldName}', \$_POST['{$fieldName}']);\n";
223
224
        $ret .= $this->pc->getPhpCodeConditions($fetchMedia, '', '', $ifelse, $contentElseExt);
225
226
        return $ret;
227
    }
228
229
    /*
230
    *  @public function getAdminXoopsCodeFileSetVar
231
    *  @param $moduleDirname
232
    *  @param $tableName
233
    *  @param $fieldName
234
    *  @param $formatUrl
235
    *  @return string
236
    */
237
    public function getAdminXoopsCodeFileSetVar($moduleDirname, $tableName, $fieldName, $formatUrl = false)
238
    {
239
        $stuModuleDirname = strtoupper($moduleDirname);
240
        if ($formatUrl) {
241
            $ret = $this->getAdminXoopsCodeSetVar($tableName, $fieldName, "formatUrl(\$_REQUEST['{$fieldName}'])");
0 ignored issues
show
Bug introduced by
The method getAdminXoopsCodeSetVar() does not exist on AdminXoopsCode. Did you maybe mean getAdminXoopsCodeSetVarsObjects()?

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...
242
            $ret .= $this->pc->getPhpCodeCommentLine('Set Var', $fieldName);
243
        } else {
244
            $ret = $this->pc->getPhpCodeCommentLine('Set Var', $fieldName);
245
        }
246
        $ret .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true);
247
        $ret .= $this->getAdminXoopsCodeMediaUploader('uploader', "{$stuModuleDirname}_UPLOAD_FILES_PATH", $tableName, $moduleDirname);
0 ignored issues
show
Bug introduced by
The method getAdminXoopsCodeMediaUploader() 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...
248
        $fetchMedia = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])";
249
        if ($formatUrl) {
250
            $ifelse = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])\n";
251
        } else {
252
            $ifelse = "//\$uploader->setPrefix('{$fieldName}_');\n";
253
            $ifelse .= "//\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])\n";
254
        }
255
        $contentElse = "\${$tableName}Obj->setVar('{$fieldName}', \$uploader->getSavedFileName());";
256
        $contentIf = "\$errors = \$uploader->getErrors();\n";
257
        $contentIf .= "redirect_header('javascript:history.go(-1)', 3, \$errors);\n";
258
        $ifelse .= $this->pc->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElse);
259
260
        $ret .= $this->pc->getPhpCodeConditions($fetchMedia, '', '', $ifelse);
261
262
        return $ret;
263
    }
264
265
    /**
266
     *  @public function getAdminXoopsCodeSetVarsObjects
267
     *
268
     *  @param $moduleDirname
269
     *  @param $tableName
270
     *  @param $fields
271
     *
272
     *  @return string
273
     */
274
    public function getAdminXoopsCodeSetVarsObjects($moduleDirname, $tableName, $fields)
275
    {
276
        $ret = $this->pc->getPhpCodeCommentLine($comment = 'Set Vars', $var = '');
277
        foreach (array_keys($fields) as $f) {
278
            $fieldName = $fields[$f]->getVar('field_name');
279
            $fieldElement = $fields[$f]->getVar('field_element');
280
            if ($f > 0) { // If we want to hide field id
281
                switch ($fieldElement) {
282
                    case 5:
283
                    case 6:
284
                        $ret .= $this->xc->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
285
                        break;
286
                    case 11:
287
                        $ret .= $this->xc->getXoopsCodeImageListSetVar($moduleDirname, $tableName, $fieldName);
288
                        break;
289
                    case 12:
290
                        $ret .= $this->xc->getXoopsCodeUrlFileSetVar($moduleDirname, $tableName, $fieldName);
291
                        break;
292
                    case 13:
293
                        if (1 == $fields[$f]->getVar('field_main')) {
294
                            $fieldMain = $fieldName;
295
                        }
296
                        $ret .= $this->xc->getXoopsCodeUploadImageSetVar($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...
297
                        break;
298
                    case 14:
299
                        $ret .= $this->xc->getXoopsCodeUploadFileSetVar($moduleDirname, $tableName, $fieldName);
300
                        break;
301
                    case 15:
302
                        $ret .= $this->xc->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
303
                        break;
304
                    default:
305
                        $ret .= $this->xc->getXoopsCodeSimpleSetVar($tableName, $fieldName);
306
                        break;
307
                }
308
            }
309
        }
310
311
        return $ret;
312
    }
313
314
    /**
315
     *  @public function getAdminXoopsCodeGetObjHandlerId
316
     *
317
     *  @param string $tableName
318
     *  @param string $fieldId
319
     *
320
     *  @return string
321
     */
322
    public function getAdminXoopsCodeGetObjHandlerId($tableName, $fieldId, $t = '')
323
    {
324
        return "{$t}\${$tableName}Obj =& \${$tableName}Handler->get(\${$fieldId});\n";
325
    }
326
327
    /*
328
    *  @public function getAdminCodeCaseDelete
329
    *  @param $tableName
330
    *  @param $language
331
    *  @param $fieldId
332
    *  @param $fieldMain
333
    *  @return string
334
    */
335
    public function getAdminCodeCaseDelete($language, $tableName, $fieldId, $fieldMain, $t = '')
336
    {
337
        $ccFieldId = $this->tf->getCamelCase($fieldId, false, true);
338
        $ret = $this->xc->getXoopsCodeGet($tableName, $ccFieldId, 'Obj', $tableName.'Handler');
339
340
        $reqOk = "_REQUEST['ok']";
341
        $isset = $this->pc->getPhpCodeIsset($reqOk);
342
        $xoopsSecurityCheck = $this->xc->getXoopsCodeSecurityCheck();
343
        $xoopsSecurityErrors = $this->xc->getXoopsCodeSecurityErrors();
344
        $implode = $this->pc->getPhpCodeImplode(', ', $xoopsSecurityErrors);
345
        $redirectHeaderErrors = $this->xc->getXoopsCodeRedirectHeader($tableName, '.php', '3', $implode, true, $t."\t\t");
346
347
        $delete = $this->xc->getXoopsCodeDelete($tableName, $tableName, 'Obj', true);
348
        $condition = $this->pc->getPhpCodeConditions('!'.$xoopsSecurityCheck, '', '', $redirectHeaderErrors, false, $t."\t");
349
350
        $redirectHeaderLanguage = $this->xc->getXoopsCodeRedirectHeader($tableName, '.php', '3', "{$language}FORM_DELETE_OK", true, $t."\t\t");
351
        $htmlErrors = $this->xc->getXoopsCodeHtmlErrors($tableName, true);
352
        $internalElse = $this->xc->getXoopsCodeTplAssign('error', $htmlErrors, false, $t."\t\t");
353
        $condition .= $this->pc->getPhpCodeConditions($delete, '', '', $redirectHeaderLanguage, $internalElse, $t."\t");
354
355
        $mainElse = $this->xc->getXoopsCodeXoopsConfirm($tableName, $language, $fieldId, $fieldMain, 'delete', $t."\t");
356
        $ret .= $this->pc->getPhpCodeConditions($isset, ' && ', "1 == \${$reqOk}", $condition, $mainElse, $t);
357
358
        return $ret;
359
    }
360
}
361