Passed
Push — master ( 0f0f99...fb91d6 )
by Michael
17s
created

BlocksFiles::renderFile()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 28
rs 8.5806
cc 4
eloc 21
nc 5
nop 1
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: blocks_pages.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class BlocksFiles.
27
 */
28
class BlocksFiles extends TDMCreateFile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

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

namespace YourVendor;

class YourClass { }

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

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
        $this->tdmcfile = TDMCreateFile::getInstance();
0 ignored issues
show
Bug introduced by
The property tdmcfile 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...
41
    }
42
43
    /*
44
    *  @static function &getInstance
45
    *  @param null
46
    */
47
    /**
48
     * @return BlocksFiles
49
     */
50
    public static function &getInstance()
51
    {
52
        static $instance = false;
53
        if (!$instance) {
54
            $instance = new self();
55
        }
56
57
        return $instance;
58
    }
59
60
    /*
61
    *  @public function write
62
    *  @param string $module
63
    *  @param mixed $table
64
    */
65
    /**
66
     * @param $module
67
     * @param $table
68
     */
69
    public function write($module, $table)
70
    {
71
        $this->setModule($module);
72
        $this->setTable($table);
73
    }
74
75
    /*
76
    *  @private function getBlocksShow
77
    *  @param null
78
    */
79
    /**
80
     * @param $moduleDirname
81
     * @param $tableName
82
     * @param $tableFieldname
83
     * @param $tableCategory
84
     * @param $fields
85
     * @param $fieldId
86
     *
87
     * @return string
88
     */
89
    private function getBlocksShow($moduleDirname, $tableName, $tableFieldname, $fields, $fieldId, $fieldParent = 0)
90
    {
91
        $stuModuleDirname = strtoupper($moduleDirname);
92
        $ucfModuleDirname = ucfirst($moduleDirname);
93
        $ret = <<<EOT
94
include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/include/common.php';
95
// Function show block
96
function b_{$moduleDirname}_{$tableName}_show(\$options)
97
{
98
    include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/class/{$tableName}.php';
99
    \$myts =& MyTextSanitizer::getInstance();
100
    \$GLOBALS['xoopsTpl']->assign('{$moduleDirname}_upload_url', {$stuModuleDirname}_UPLOAD_URL);
101
    \$block       = array();
102
    \$typeBlock   = \$options[0];
103
    \$limit       = \$options[1];
104
    \$lenghtTitle = \$options[2];
105
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
106
    \${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');
107
    \$criteria = new CriteriaCompo();
108
    array_shift(\$options);
109
    array_shift(\$options);
110
    array_shift(\$options);\n
111
EOT;
112
        if (1 == $fieldParent) {
113
            $ret .= <<<EOT
114
	\${$tableName} = {$moduleDirname}_getMyItemIds('{$moduleDirname}_view', '{$moduleDirname}');
115
    \$criteria->add(new Criteria('cid', '(' . implode(',', \${$tableName}) . ')','IN'));
116
    if (1 != (count(\$options) && 0 == \$options[0])) {
117
        \$criteria->add(new Criteria('{$fieldId}', {$moduleDirname}_block_addCatSelect(\$options), 'IN'));
118
    }
119
120
    if (\$typeBlock)
121
    {
122
        \$criteria->add(new Criteria('{$fieldId}', 0, '!='));
123
        \$criteria->setSort('{$fieldId}');
124
        \$criteria->setOrder('ASC');
125
    }\n
126
EOT;
127
        } else {
128
            $ret .= <<<EOT
129
	switch(\$typeBlock)
130
	{
131
		// For the block: {$tableName} last
132
		case 'last':
133
			\$criteria->add(new Criteria('{$tableFieldname}_display', 1));
134
			\$criteria->setSort('{$tableFieldname}_created');
135
			\$criteria->setOrder('DESC');
136
		break;
137
		// For the block: {$tableName} new
138
		case 'new':
139
			\$criteria->add(new Criteria('{$tableFieldname}_display', 1));
140
			\$criteria->add(new Criteria('{$tableFieldname}_created', strtotime(date(_SHORTDATESTRING)), '>='));
141
			\$criteria->add(new Criteria('{$tableFieldname}_created', strtotime(date(_SHORTDATESTRING))+86400, '<='));
142
			\$criteria->setSort('{$tableFieldname}_created');
143
			\$criteria->setOrder('ASC');
144
		break;
145
		// For the block: {$tableName} hits
146
		case 'hits':
147
            \$criteria->setSort('{$tableFieldname}_hits');
148
            \$criteria->setOrder('DESC');
149
        break;
150
		// For the block: {$tableName} top
151
		case 'top':
152
            \$criteria->setSort('{$tableFieldname}_top');
153
            \$criteria->setOrder('ASC');
154
        break;
155
		// For the block: {$tableName} random
156
		case 'random':
157
			\$criteria->add(new Criteria('{$tableFieldname}_display', 1));
158
			\$criteria->setSort('RAND()');
159
		break;
160
	}\n
161
EOT;
162
        }
163
        $ret .= <<<EOT
164
    \$criteria->setLimit(\$limit);
165
    \${$tableName}All = \${$tableName}Handler->getAll(\$criteria);
166
	unset(\$criteria);
167
    foreach(array_keys(\${$tableName}All) as \$i)
168
    {\n
169
EOT;
170
        foreach (array_keys($fields) as $f) {
171
            $fieldName = $fields[$f]->getVar('field_name');
172
            // Verify if table_fieldname is not empty
173
            //$lpFieldName = !empty($tableFieldname) ? substr($fieldName, 0, strpos($fieldName, '_')) : $tableName;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
174
            $rpFieldName = $this->tdmcfile->getRightString($fieldName);
175
            $fieldElement = $fields[$f]->getVar('field_element');
176
            if (1 == $fields[$f]->getVar('field_block')) {
177
                switch ($fieldElement) {
178
                    case 2:
179
                        $ret .= <<<EOT
180
        \$block[\$i]['{$rpFieldName}'] = \$myts->htmlSpecialChars(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
181
EOT;
182
                        break;
183
                    case 3:
184
                    case 4:
185
                        $ret .= <<<EOT
186
		\$block[\$i]['{$rpFieldName}'] = strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
187
EOT;
188
                        break;
189
                    case 8:
190
                    $ret .= <<<EOT
191
		\$block[\$i]['{$rpFieldName}'] = XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
192
EOT;
193
                        break;
194
                    case 15:
195
                    $ret .= <<<EOT
196
		\$block[\$i]['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
197
EOT;
198
                        break;
199
                    default:
200
                        $ret .= <<<EOT
201
        \$block[\$i]['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n
202
EOT;
203
                        break;
204
                }
205
            }
206
        }
207
        $ret .= <<<EOT
208
    }
209
    return \$block;
210
}\n\n
211
EOT;
212
213
        return $ret;
214
    }
215
216
    /*
217
    *  @public function getBlocksEdit
218
    *  @param string $moduleDirname
219
    *  @param string $tableName
220
    *  @param string $fieldId
221
    *  @param string $fieldMain
222
    *  @param string $language
223
    */
224
    /**
225
     * @param $moduleDirname
226
     * @param $tableName
227
     * @param $fieldId
228
     * @param $fieldMain
229
     * @param $language
230
     *
231
     * @return string
232
     */
233
    private function getBlocksEdit($moduleDirname, $tableName, $fieldId, $fieldMain, $language)
234
    {
235
        $stuModuleDirname = strtoupper($moduleDirname);
236
        $stuTableName = strtoupper($tableName);
237
        $ucfModuleDirname = ucfirst($moduleDirname);
238
        $ret = <<<EOT
239
// Function edit block
240
function b_{$moduleDirname}_{$tableName}_edit(\$options)
241
{
242
    include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/class/{$tableName}.php';
243
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
244
    \${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');
245
    \$GLOBALS['xoopsTpl']->assign('{$moduleDirname}_upload_url', {$stuModuleDirname}_UPLOAD_URL);
246
    \$form  = {$language}DISPLAY;
247
    \$form .= "<input type='hidden' name='options[0]' value='".\$options[0]."' />";
248
    \$form .= "<input type='text' name='options[1]' size='5' maxlength='255' value='".\$options[1]."' />&nbsp;<br />";
249
    \$form .= {$language}TITLE_LENGTH." : <input type='text' name='options[2]' size='5' maxlength='255' value='".\$options[2]."' /><br /><br />";
250
    array_shift(\$options);
251
    array_shift(\$options);
252
    array_shift(\$options);
253
    \$criteria = new CriteriaCompo();
254
    \$criteria->add(new Criteria('{$fieldId}', 0, '!='));
255
    \$criteria->setSort('{$fieldId}');
256
    \$criteria->setOrder('ASC');
257
    \${$tableName}All = \${$tableName}Handler->getAll(\$criteria);
258
    unset(\$criteria);
259
    \$form .= {$language}{$stuTableName}_TO_DISPLAY."<br /><select name='options[]' multiple='multiple' size='5'>";
260
    \$form .= "<option value='0' " . (array_search(0, \$options) === false ? "" : "selected='selected'") . ">" .{$language}ALL_{$stuTableName} . "</option>";
261
    foreach (array_keys(\${$tableName}All) as \$i) {
262
        \${$fieldId} = \${$tableName}All[\$i]->getVar('{$fieldId}');
263
        \$form .= "<option value='" . \${$fieldId} . "' " . (array_search(\${$fieldId}, \$options) === false ? "" : "selected='selected'") . ">".\${$tableName}All[\$i]->getVar('{$fieldMain}')."</option>";
264
    }
265
    \$form .= "</select>";
266
    return \$form;
267
}
268
EOT;
269
270
        return $ret;
271
    }
272
273
    /*
274
    *  @public function renderFile
275
    *  @param null
276
    */
277
    /**
278
     * @param $filename
279
     *
280
     * @return bool|string
281
     */
282
    public function renderFile($filename)
283
    {
284
        $module = $this->getModule();
285
        $table = $this->getTable();
286
        $moduleDirname = $module->getVar('mod_dirname');
287
        $tableName = $table->getVar('table_name');
288
        $tableFieldname = $table->getVar('table_fieldname');
289
        $tableCategory = $table->getVar('table_category');
0 ignored issues
show
Unused Code introduced by
$tableCategory 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...
290
        $language = $this->getLanguage($moduleDirname, 'MB');
291
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
292
        foreach (array_keys($fields) as $f) {
293
            $fieldName = $fields[$f]->getVar('field_name');
294
            $fieldParent = $fields[$f]->getVar('field_parent');
295
            if (0 == $f) {
296
                $fieldId = $fieldName;
297
            }
298
            if (1 == $fields[$f]->getVar('field_main')) {
299
                $fieldMain = $fieldName;
300
            }
301
        }
302
        $content = $this->getHeaderFilesComments($module, $filename);
303
        $content .= $this->getBlocksShow($moduleDirname, $tableName, $tableFieldname, $fields, $fieldId, $fieldParent);
0 ignored issues
show
Bug introduced by
The variable $fieldId 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 variable $fieldParent 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...
304
        $content .= $this->getBlocksEdit($moduleDirname, $tableName, $fieldId, $fieldMain, $language);
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...
305
        //
306
        $this->tdmcfile->create($moduleDirname, 'blocks', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
307
308
        return $this->tdmcfile->renderFile();
309
    }
310
}
311