Completed
Push — master ( 1a43b9...76063f )
by Gino
07:55 queued 03:52
created

AdminXoopsCode::getAdminXoopsCodeImageListSetVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4285
cc 1
eloc 14
nc 1
nop 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
defined('XOOPS_ROOT_PATH') || die('Restricted access');
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 string
32
    */
33
    private $xoopscode;
34
35
    /*
36
    *  @public function constructor
37
    *  @param null
38
    */
39
    /**
40
     *
41
     */
42
    public function __construct()
43
    {
44
        $this->phpcode = TDMCreatePhpCode::getInstance();
0 ignored issues
show
Bug introduced by
The property phpcode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
        $this->xoopscode = TDMCreateXoopsCode::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateXoopsCode::getInstance() of type object<TDMCreateXoopsCode> is incompatible with the declared type string of property $xoopscode.

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

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

Loading history...
46
    }
47
48
    /*
49
    *  @static function &getInstance
50
    *  @param null
51
    */
52
    /**
53
     * @return AdminXoopsCode
54
     */
55
    public static function &getInstance()
56
    {
57
        static $instance = false;
58
        if (!$instance) {
59
            $instance = new self();
60
        }
61
62
        return $instance;
63
    }
64
65
    /*
66
     *  @public function getAdminTemplateMain
67
     *  @param $moduleDirname
68
     *  @param $tableName
69
     *
70
     *  @return string
71
     */
72
    public function getAdminTemplateMain($moduleDirname, $tableName)
73
    {
74
        return "\$templateMain = '{$moduleDirname}_admin_{$tableName}.tpl';\n";
75
    }
76
77
    /*
78
     *  @public function getAdminTemplateMain
79
     *  @param $language
80
     *  @param $tableName
81
     *  @param $stuTableSoleName
82
     *  @param $type
83
     *
84
     *  @return string
85
     */
86
    public function getAdminItemButton($language, $tableName, $stuTableSoleName, $op = '?op=new', $type = 'add')
87
    {
88
        $stuType = strtoupper($type);
89
        $adminMenu = '$adminMenu->addItemButton(';
90
        if ($type === 'add') {
91
            $ret = $adminMenu."{$language}ADD_{$stuTableSoleName}, '{$tableName}.php{$op}', '{$type}');\n";
92
        } else {
93
            $ret = $adminMenu."{$language}{$stuTableSoleName}_{$stuType}, '{$tableName}.php{$op}', '{$type}');\n";
94
        }
95
96
        return $ret;
97
    }
98
99
    /**
100
     *  @public function getAdminAddNavigation
101
     *
102
     *  @param $tableName
103
     *
104
     *  @return string
105
     */
106
    public function getAdminAddNavigation($tableName)
107
    {
108
        return "\$adminMenu->addNavigation('{$tableName}.php')";
109
    }
110
    
111
    /*
112
    *  @public function getAdminXoopsCodeAddInfoBox
113
    *  @param $language
114
    *  
115
    *  @return string
116
    */
117
    public function getAdminXoopsCodeAddInfoBox($language)
118
    {
119
        return "\$adminMenu->addInfoBox({$language});\n";
120
    }
121
122
    /*
123
    *  @public function getAdminXoopsCodeAddInfoBoxLine
124
    *  @param $language
125
    *  @param $label
126
    *  @param $var
127
    *  
128
    *  @return string
129
    */
130 View Code Duplication
    public function getAdminXoopsCodeAddInfoBoxLine($language, $label = '', $var = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $aMenu = '$adminMenu->addInfoBoxLine(';
133
        if ($var != '') {
134
            $ret = $aMenu."{$language}, '<label>'.{$label}.'</label>', {$var});\n";
135
        } else {
136
            $ret = $aMenu."{$language}, '<label>'.{$label}.'</label>');\n";
137
        }
138
139
        return $ret;
140
    }
141
142
    /*
143
    *  @public function getAdminXoopsCodeAddConfigBoxLine
144
    *  @param $language
145
    *  @param $label
146
    *  @param $var
147
    *  
148
    *  @return string
149
    */
150 View Code Duplication
    public function getAdminXoopsCodeAddConfigBoxLine($language, $label = '', $var = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $aMenu = '$adminMenu->addConfigBoxLine(';
153
        if ($var != '') {
154
            $ret = $aMenu."{$language}, '{$label}', {$var});\n";
155
        } else {
156
            $ret = $aMenu."{$language}, '{$label}');\n";
157
        }
158
159
        return $ret;
160
    }
161
162
    /*
163
    *  @public function getAdminXoopsCodeImageListSetVar
164
    *  @param string $moduleDirname
165
    *  @param string $tableName
166
    *  @param string $fieldName
167
    *  @return string
168
    */
169
    public function getAdminXoopsCodeImageListSetVar($moduleDirname, $tableName, $fieldName)
170
    {
171
        $ret = $this->phpcode->getPhpCodeCommentLine('Set Var', $fieldName);
172
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true);
173
        $ret .= $this->xoopscode->getAdminXoopsCodeMediaUploader('uploader', "XOOPS_ROOT_PATH . '/Frameworks/moduleclasses/icons/32'", $tableName, $moduleDirname);
0 ignored issues
show
Bug introduced by
The method getAdminXoopsCodeMediaUploader cannot be called on $this->xoopscode (of type string).

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

Loading history...
174
        $fetchMedia = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])";
175
        $ifelse = "//\$uploader->setPrefix('{$fieldName}_');\n";
176
        $ifelse .= "//\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])\n";
177
        $contentElseInt = "\${$tableName}Obj->setVar('{$fieldName}', \$uploader->getSavedFileName());";
178
        $contentIf = "\$errors = \$uploader->getErrors();\n";
179
        $contentIf .= "redirect_header('javascript:history.go(-1)', 3, \$errors);\n";
180
        $ifelse .= $this->phpcode->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElseInt);
181
        $contentElseExt = "\${$tableName}Obj->setVar('{$fieldName}', \$_POST['{$fieldName}']);\n";
182
183
        $ret .= $this->phpcode->getPhpCodeConditions($fetchMedia, '', '', $ifelse, $contentElseExt);
184
185
        return $ret;
186
    }
187
188
    /*
189
    *  @public function getAdminXoopsCodeUploadImageSetVar
190
    *  @param string $moduleDirname
191
    *  @param string $tableName
192
    *  @param string $fieldName
193
    *  @return string
194
    */
195
    public function getAdminXoopsCodeUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain)
196
    {
197
        $stuModuleDirname = strtoupper($moduleDirname);
198
        $ret = $this->phpcode->getPhpCodeCommentLine('Set Var', $fieldName);
199
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true);
200
        $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...
201
202
        $fetchMedia = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])";
203
        $ifelse = "\$extension = preg_replace( '/^.+\.([^.]+)$/sU' , '' , \$_FILES['attachedfile']['name']);\n";
204
        $ifelse .= "\$imgName = str_replace(' ', '', \$_POST['{$fieldMain}']).'.'.\$extension;\n";
205
        $ifelse .= "\$uploader->setPrefix(\$imgName);\n";
206
        $ifelse .= "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0]);\n";
207
        $contentElseInt = "\${$tableName}Obj->setVar('{$fieldName}', \$uploader->getSavedFileName());";
208
        $contentIf = "\$errors = \$uploader->getErrors();\n";
209
        $contentIf .= "redirect_header('javascript:history.go(-1)', 3, \$errors);\n";
210
        $ifelse .= $this->phpcode->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElseInt);
211
        $contentElseExt = "\${$tableName}Obj->setVar('{$fieldName}', \$_POST['{$fieldName}']);\n";
212
213
        $ret .= $this->phpcode->getPhpCodeConditions($fetchMedia, '', '', $ifelse, $contentElseExt);
214
215
        return $ret;
216
    }
217
218
    /*
219
    *  @public function getAdminXoopsCodeFileSetVar
220
    *  @param $moduleDirname
221
    *  @param $tableName
222
    *  @param $fieldName
223
    *  @param $formatUrl
224
    *  @return string
225
    */
226
    public function getAdminXoopsCodeFileSetVar($moduleDirname, $tableName, $fieldName, $formatUrl = false)
227
    {
228
        $stuModuleDirname = strtoupper($moduleDirname);
229
        if ($formatUrl) {
230
            $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...
231
            $ret .= $this->phpcode->getPhpCodeCommentLine('Set Var', $fieldName);
232
        } else {
233
            $ret = $this->phpcode->getPhpCodeCommentLine('Set Var', $fieldName);
234
        }
235
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/uploader', true);
236
        $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...
237
        $fetchMedia = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])";
238
        if ($formatUrl) {
239
            $ifelse = "\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])\n";
240
        } else {
241
            $ifelse = "//\$uploader->setPrefix('{$fieldName}_');\n";
242
            $ifelse .= "//\$uploader->fetchMedia(\$_POST['xoops_upload_file'][0])\n";
243
        }
244
        $contentElse = "\${$tableName}Obj->setVar('{$fieldName}', \$uploader->getSavedFileName());";
245
        $contentIf = "\$errors = \$uploader->getErrors();\n";
246
        $contentIf .= "redirect_header('javascript:history.go(-1)', 3, \$errors);\n";
247
        $ifelse .= $this->phpcode->getPhpCodeConditions('!$uploader->upload()', '', '', $contentIf, $contentElse);
248
249
        $ret .= $this->phpcode->getPhpCodeConditions($fetchMedia, '', '', $ifelse);
250
251
        return $ret;
252
    }
253
254
    /**
255
     *  @public function getAdminXoopsCodeSetVarsObjects
256
     *
257
     *  @param $moduleDirname
258
     *  @param $tableName
259
     *  @param $fields
260
     *
261
     *  @return string
262
     */
263
    public function getAdminXoopsCodeSetVarsObjects($moduleDirname, $tableName, $fields)
264
    {
265
        $ret = $this->phpcode->getPhpCodeCommentLine($comment = 'Set Vars', $var = '');
266
        foreach (array_keys($fields) as $f) {
267
            $fieldName = $fields[$f]->getVar('field_name');
268
            $fieldElement = $fields[$f]->getVar('field_element');
269
            if ($f > 0) { // If we want to hide field id
270
                switch ($fieldElement) {
271
                    case 5:
272
                    case 6:
273
                        $ret .= $this->xoopscode->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeCheckBoxOrRadioYNSetVar cannot be called on $this->xoopscode (of type string).

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

Loading history...
274
                        break;
275
                    case 11:
276
                        $ret .= $this->xoopscode->getXoopsCodeImageListSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeImageListSetVar cannot be called on $this->xoopscode (of type string).

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

Loading history...
277
                        break;
278
                    case 12:
279
                        $ret .= $this->xoopscode->getXoopsCodeUrlFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeUrlFileSetVar cannot be called on $this->xoopscode (of type string).

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

Loading history...
280
                        break;
281
                    case 13:
282
                        if (1 == $fields[$f]->getVar('field_main')) {
283
                            $fieldMain = $fieldName;
284
                        }
285
                        $ret .= $this->xoopscode->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...
Bug introduced by
The method getXoopsCodeUploadImageSetVar cannot be called on $this->xoopscode (of type string).

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

Loading history...
286
                        break;
287
                    case 14:
288
                        $ret .= $this->xoopscode->getXoopsCodeUploadFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeUploadFileSetVar cannot be called on $this->xoopscode (of type string).

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

Loading history...
289
                        break;
290
                    case 15:
291
                        $ret .= $this->xoopscode->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeTextDateSelectSetVar cannot be called on $this->xoopscode (of type string).

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

Loading history...
292
                        break;
293
                    default:
294
                        $ret .= $this->xoopscode->getXoopsCodeSimpleSetVar($tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeSimpleSetVar cannot be called on $this->xoopscode (of type string).

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

Loading history...
295
                        break;
296
                }
297
            }
298
        }
299
300
        return $ret;
301
    }    
302
    
303
    /**
304
     *  @public function getAdminXoopsCodeGetObjHandlerId
305
     *
306
     *  @param string $tableName
307
     *  @param string $fieldId
308
     *
309
     *  @return string
310
     */
311
    public function getAdminXoopsCodeGetObjHandlerId($tableName, $fieldId)
312
    {
313
        $ret = <<<EOT
314
        \${$tableName}Obj =& \${$tableName}Handler->get(\${$fieldId});\n
315
EOT;
316
317
        return $ret;
318
    }    
319
}
320