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

AdminXoopsCode::getAxcImageFileSetVar()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 29
rs 8.8571
cc 3
eloc 23
nc 4
nop 6
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: Axc.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class Axc.
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
    *  @static function &getInstance
32
    *  @param null
33
    */
34
    /**
35
     * @return Axc
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 getAdminTemplateMain
49
     *  @param $moduleDirname
50
     *  @param $tableName
51
     *
52
     *  @return string
53
     */
54
    public function getAdminTemplateMain($moduleDirname, $tableName, $t = '')
55
    {
56
        return "{$t}\$templateMain = '{$moduleDirname}_admin_{$tableName}.tpl';\n";
57
    }
58
59
    /*
60
     *  @public function getAdminTemplateMain
61
     *  @param $language
62
     *  @param $tableName
63
     *  @param $stuTableSoleName
64
     *  @param $type
65
     *
66
     *  @return string
67
     */
68
    public function getAdminItemButton($language, $tableName, $stuTableSoleName, $op = '?op=new', $type = 'add', $t = '')
69
    {
70
        $stuType = strtoupper($type);
71
        $aM = $t.'$adminMenu->addItemButton(';
72
        if ($type === 'add') {
73
            $ret = $aM."{$language}ADD_{$stuTableSoleName}, '{$tableName}.php{$op}', '{$type}');\n";
74
        } else {
75
            $ret = $aM."{$language}{$stuTableSoleName}_{$stuType}, '{$tableName}.php{$op}', '{$type}');\n";
76
        }
77
78
        return $ret;
79
    }
80
81
    /**
82
     *  @public function getAdminAddNavigation
83
     *
84
     *  @param $tableName
85
     *
86
     *  @return string
87
     */
88
    public function getAdminAddNavigation($tableName, $t = '')
89
    {
90
        return "{$t}\$adminMenu->addNavigation('{$tableName}.php')";
91
    }
92
93
    /*
94
    *  @public function getAxcAddInfoBox
95
    *  @param $language
96
    *  
97
    *  @return string
98
    */
99
    public function getAxcAddInfoBox($language, $t = '')
100
    {
101
        return "{$t}\$adminMenu->addInfoBox({$language});\n";
102
    }
103
104
    /*
105
    *  @public function getAxcAddInfoBoxLine
106
    *  @param $language
107
    *  @param $label
108
    *  @param $var
109
    *  
110
    *  @return string
111
    */
112
    public function getAxcAddInfoBoxLine($language, $label = '', $var = '', $t = '')
113
    {
114
        $aMenu = $t.'$adminMenu->addInfoBoxLine(';
115
        if ($var != '') {
116
            $ret = $aMenu."{$language}, '<label>'.{$label}.'</label>', {$var});\n";
117
        } else {
118
            $ret = $aMenu."{$language}, '<label>'.{$label}.'</label>');\n";
119
        }
120
121
        return $ret;
122
    }
123
124
    /*
125
    *  @public function getAxcAddConfigBoxLine
126
    *  @param $language
127
    *  @param $label
128
    *  @param $var
129
    *  
130
    *  @return string
131
    */
132
    public function getAxcAddConfigBoxLine($language, $label = '', $var = '', $t = '')
133
    {
134
        $aMenu = $t.'$adminMenu->addConfigBoxLine(';
135
        if ($var != '') {
136
            $ret = $aMenu."{$language}, '{$label}', {$var});\n";
137
        } else {
138
            $ret = $aMenu."{$language}, '{$label}');\n";
139
        }
140
141
        return $ret;
142
    }
143
144
    /*
145
    *  @public function getAxcImageListSetVar
146
    *  @param string $moduleDirname
147
    *  @param string $tableName
148
    *  @param string $fieldName
149
    *  @return string
150
    */
151
    public function getAxcImageListSetVar($moduleDirname, $tableName, $fieldName, $t = '')
152
    {
153
        $pCodeImageList = TDMCreatePhpCode::getInstance();
154
        $xCodeImageList = TDMCreateXoopsCode::getInstance();
155
        $ret = $pCodeImageList->getPhpCodeCommentLine('Set Var', $fieldName, $t);
156
        $ret .= $pCodeImageList->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true, false, '', $t);
157
        $xRootPath = "XOOPS_ROOT_PATH . '/Frameworks/moduleclasses/icons/32'";
158
        $ret .= $xCodeImageList->getXcMediaUploader('uploader', $xRootPath, $tableName, $moduleDirname, $t);
159
        $post = $pCodeImageList->getPhpCodeGlobalsVariables('xoops_upload_file', 'POST').'[0]';
160
        $fetchMedia = self::getAxcFetchMedia('uploader', $post);
161
        $ifelse = $t."\t//".self::getAxcSetPrefix('uploader', "{$fieldName}_").";\n";
162
        $ifelse .= $t."\t//{$fetchMedia};\n";
163
        $contentElseInt = $xCodeImageList->getXcSetVar($tableName, $fieldName, '$uploader->getSavedFileName()', $t."\t\t");
164
        $contentIf = $xCodeImageList->getXcEqualsOperator('$errors', '$uploader->getErrors()', null, false, $t."\t\t");
165
        $contentIf .= $xCodeImageList->getXcRedirectHeader('javascript:history.go(-1)', '', '3', '$errors', true, $t."\t\t");
166
        $ifelse .= $pCodeImageList->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElseInt, $t."\t");
167
        $contentElseExt = $xCodeImageList->getXcSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']", $t);
168
169
        $ret .= $pCodeImageList->getPhpCodeConditions($fetchMedia, '', '', $ifelse, $contentElseExt, $t);
170
171
        return $ret;
172
    }
173
174
    /*
175
    *  @public function getAxcUploadImageSetVar
176
    *  @param string $moduleDirname
177
    *  @param string $tableName
178
    *  @param string $fieldName
179
    *  @return string
180
    */
181
    public function getAxcUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain, $t = '')
182
    {
183
        $pCodeUploadImage = TDMCreatePhpCode::getInstance();
184
        $xCodeUploadImage = TDMCreateXoopsCode::getInstance();
185
        $stuModuleDirname = strtoupper($moduleDirname);
186
        $ret = $pCodeUploadImage->getPhpCodeCommentLine('Set Var', $fieldName, $t);
187
        $ret .= $pCodeUploadImage->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true, false, '', $t);
188
        $xUploadImage = "{$stuModuleDirname}_UPLOAD_IMAGE_PATH";
189
        $ret .= $xCodeUploadImage->getXcMediaUploader('uploader', $xUploadImage, $tableName, $moduleDirname, $t);
190
191
        $post = $pCodeUploadImage->getPhpCodeGlobalsVariables('xoops_upload_file', 'POST').'[0]';
192
        $fetchMedia = self::getAxcFetchMedia('uploader', $post);
193
        $file = $pCodeUploadImage->getPhpCodeGlobalsVariables('attachedfile', 'FILES')."['name']";
194
        $expr = '/^.+\.([^.]+)$/sU';
195
        $ifelse = $pCodeUploadImage->getPhpCodePregFunzions('extension', $expr, '', $file, 'replace', false, $t."\t");
196
197
        $ifelse .= $t."\t\$imgName = str_replace(' ', '', \$_POST['{$fieldMain}']).'.'.\$extension;\n";
198
        $ifelse .= self::getAxcSetPrefix('uploader', '$imgName', $t."\t").";\n";
199
        $ifelse .= $t."\t{$fetchMedia};\n";
200
        $contentElseInt = $xCodeUploadImage->getXcSetVar($tableName, $fieldName, '$uploader->getSavedFileName()', $t."\t\t");
201
        $contentIf = $xCodeUploadImage->getXcEqualsOperator('$errors', '$uploader->getErrors()', null, false, $t."\t\t");
202
        $contentIf .= $xCodeUploadImage->getXcRedirectHeader('javascript:history.go(-1)', '', '3', '$errors', true, $t."\t\t");
203
        $ifelse .= $pCodeUploadImage->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElseInt, $t."\t");
204
        $contentElseExt = $xCodeUploadImage->getXcSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']", $t."\t\t");
205
206
        $ret .= $pCodeUploadImage->getPhpCodeConditions($fetchMedia, '', '', $ifelse, $contentElseExt, $t);
207
208
        return $ret;
209
    }
210
211
    /*
212
    *  @public function getAxcFileSetVar
213
    *  @param $moduleDirname
214
    *  @param $tableName
215
    *  @param $fieldName
216
    *  @param $formatUrl
217
    *  @return string
218
    */
219
    public function getAxcUploadFileSetVar($moduleDirname, $tableName, $fieldName, $formatUrl = false, $t = '')
220
    {
221
        $stuModuleDirname = strtoupper($moduleDirname);
222
        $ret = self::getAxcImageFileSetVar($moduleDirname, $stuModuleDirname.'_UPLOAD_FILES_PATH', $tableName, $fieldName, $formatUrl, $t);
223
224
        return $ret;
225
    }
226
227
    /*
228
    *  @private function getAxcImageFileSetVar
229
    *  @param $moduleDirname
230
    *  @param $tableName
231
    *  @param $fieldName
232
    *  @param $formatUrl
233
    *  @return string
234
    */
235
    private function getAxcImageFileSetVar($moduleDirname, $dirname = '', $tableName, $fieldName, $formatUrl = false, $t = '')
236
    {
237
        $pCodeFileSetVar = TDMCreatePhpCode::getInstance();
238
        $xCodeFileSetVar = TDMCreateXoopsCode::getInstance();
239
        if ($formatUrl) {
240
            $ret = $xCodeFileSetVar->getXcSetVar($tableName, $fieldName, "formatUrl(\$_REQUEST['{$fieldName}'])", $t);
241
            $ret .= $pCodeFileSetVar->getPhpCodeCommentLine('Set Var', $fieldName, $t);
242
        } else {
243
            $ret = $pCodeFileSetVar->getPhpCodeCommentLine('Set Var', $fieldName, $t);
244
        }
245
        $ret .= $pCodeFileSetVar->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true, false, '', $t);
246
        $ret .= $xCodeFileSetVar->getXcMediaUploader('uploader', $dirname.'/'.$tableName, $tableName, $moduleDirname, $t);
247
        $post = $pCodeFileSetVar->getPhpCodeGlobalsVariables('xoops_upload_file', 'POST').'[0]';
248
        $fetchMedia = self::getAxcFetchMedia('uploader', $post);
249
        if ($formatUrl) {
250
            $ifelse = $t."\t\t{$fetchMedia};\n";
251
        } else {
252
            $ifelse = $t."\t//".self::getAxcFetchMedia('uploader', "'{$fieldName}_'").";\n";
253
            $ifelse .= $t."\t//{$fetchMedia};\n";
254
        }
255
        $contentElse = $xCodeFileSetVar->getXcSetVar($tableName, $fieldName, '$uploader->getSavedFileName()', $t."\t\t");
256
        $contentIf = $xCodeFileSetVar->getXcEqualsOperator('$errors', '$uploader->getErrors()', null, false, $t."\t\t");
257
        $contentIf .= $xCodeFileSetVar->getXcRedirectHeader('javascript:history.go(-1)', '', '3', '$errors', true, $t."\t\t");
258
        $ifelse .= $pCodeFileSetVar->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElse, $t."\t");
259
260
        $ret .= $pCodeFileSetVar->getPhpCodeConditions($fetchMedia, '', '', $t."\t".$fetchMedia.";\n", $ifelse, $t);
261
262
        return $ret;
263
    }
264
265
    /**
266
     *  @public function getAxcSetVarsObjects
267
     *
268
     *  @param $moduleDirname
269
     *  @param $tableName
270
     *  @param $fields
271
     *
272
     *  @return string
273
     */
274
    public function getAxcSetVarsObjects($moduleDirname, $tableName, $fields)
275
    {
276
        $xCodeSetVars = TDMCreateXoopsCode::getInstance();
277
        $ret = TDMCreatePhpCode::getInstance()->getPhpCodeCommentLine($comment = 'Set Vars', $var = '');
278
        foreach (array_keys($fields) as $f) {
279
            $fieldName = $fields[$f]->getVar('field_name');
280
            $fieldElement = $fields[$f]->getVar('field_element');
281
            if ($f > 0) { // If we want to hide field id
282
                switch ($fieldElement) {
283
                    case 5:
284
                    case 6:
285
                        $ret .= $xCodeSetVars->getXcCheckBoxOrRadioYNSetVar($tableName, $fieldName);
286
                        break;
287
                    case 11:
288
                        $ret .= self::getAxcImageListSetVar($moduleDirname, $tableName, $fieldName);
289
                        break;
290
                    case 12:
291
                        $ret .= $xCodeSetVars->getXcUrlFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXcUrlFileSetVar() does not exist on TDMCreateXoopsCode. Did you maybe mean getXcUrlFileGetVar()?

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...
292
                        break;
293
                    case 13:
294
                        if (1 == $fields[$f]->getVar('field_main')) {
295
                            $fieldMain = $fieldName;
296
                        }
297
                        $ret .= self::getAxcUploadImageSetVar($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...
298
                        break;
299
                    case 14:
300
                        $ret .= $xCodeSetVars->getXcUploadFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXcUploadFileSetVar() does not seem to exist on object<TDMCreateXoopsCode>.

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...
301
                        break;
302
                    case 15:
303
                        $ret .= $xCodeSetVars->getXcTextDateSelectSetVar($tableName, $fieldName);
304
                        break;
305
                    default:
306
                        $ret .= $xCodeSetVars->getXcSimpleSetVar($tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXcSimpleSetVar() does not seem to exist on object<TDMCreateXoopsCode>.

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...
307
                        break;
308
                }
309
            }
310
        }
311
312
        return $ret;
313
    }
314
315
    /**
316
     *  @public function getAxcFetchMedia
317
     *
318
     *  @param $anchor
319
     *  @param $var
320
     *
321
     *  @return string
322
     */
323
    public function getAxcFetchMedia($anchor, $var, $t = '')
324
    {
325
        return "{$t}\${$anchor}->fetchMedia({$var})";
326
    }
327
328
    /**
329
     *  @public function getAxcSetPrefix
330
     *
331
     *  @param $anchor
332
     *  @param $var
333
     *
334
     *  @return string
335
     */
336
    public function getAxcSetPrefix($anchor, $var, $t = '')
337
    {
338
        return "{$t}\${$anchor}->setPrefix({$var})";
339
    }
340
341
    /**
342
     *  @public function getAxcGetObjHandlerId
343
     *
344
     *  @param string $tableName
345
     *  @param string $fieldId
346
     *
347
     *  @return string
348
     */
349
    public function getAxcGetObjHandlerId($tableName, $fieldId, $t = '')
350
    {
351
        return "{$t}\${$tableName}Obj =& \${$tableName}Handler->get(\${$fieldId});\n";
352
    }
353
354
    /*
355
    *  @public function getAdminCodeCaseDelete
356
    *  @param $tableName
357
    *  @param $language
358
    *  @param $fieldId
359
    *  @param $fieldMain
360
    *  @return string
361
    */
362
    public function getAdminCodeCaseDelete($language, $tableName, $fieldId, $fieldMain, $t = '')
363
    {
364
        $phpCodeCaseDelete = TDMCreatePhpCode::getInstance();
365
        $xCodeCaseDelete = TDMCreateXoopsCode::getInstance();
366
        $ccFieldId = TDMCreateFile::getInstance()->getCamelCase($fieldId, false, true);
367
        $ret = $xCodeCaseDelete->getXcGet($tableName, $ccFieldId, 'Obj', $tableName.'Handler');
368
369
        $reqOk = "_REQUEST['ok']";
370
        $isset = $phpCodeCaseDelete->getPhpCodeIsset($reqOk);
371
        $xoopsSecurityCheck = $xCodeCaseDelete->getXcSecurityCheck();
372
        $xoopsSecurityErrors = $xCodeCaseDelete->getXcSecurityErrors();
373
        $implode = $phpCodeCaseDelete->getPhpCodeImplode(', ', $xoopsSecurityErrors);
374
        $redirectHeaderErrors = $xCodeCaseDelete->getXcRedirectHeader($tableName, '.php', '3', $implode, true, $t."\t\t");
375
376
        $delete = $xCodeCaseDelete->getXcDelete($tableName, $tableName, 'Obj', true);
377
        $condition = $phpCodeCaseDelete->getPhpCodeConditions('!'.$xoopsSecurityCheck, '', '', $redirectHeaderErrors, false, $t."\t");
378
379
        $redirectHeaderLanguage = $xCodeCaseDelete->getXcRedirectHeader($tableName, '.php', '3', "{$language}FORM_DELETE_OK", true, $t."\t\t");
380
        $htmlErrors = $xCodeCaseDelete->getXcHtmlErrors($tableName, true);
381
        $internalElse = $xCodeCaseDelete->getXcTplAssign('error', $htmlErrors, false, $t."\t\t");
382
        $condition .= $phpCodeCaseDelete->getPhpCodeConditions($delete, '', '', $redirectHeaderLanguage, $internalElse, $t."\t");
383
384
        $mainElse = $xCodeCaseDelete->getXcXoopsConfirm($tableName, $language, $fieldId, $fieldMain, 'delete', $t."\t");
385
        $ret .= $phpCodeCaseDelete->getPhpCodeConditions($isset, ' && ', "1 == \${$reqOk}", $condition, $mainElse, $t);
386
387
        return $ret;
388
    }
389
}
390