Passed
Push — master ( 09f9f5...606015 )
by Michael
16s
created

BlocksFiles::render()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 22
nc 5
nop 0
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
    }
41
42
    /*
43
    *  @static function &getInstance
44
    *  @param null
45
    */
46
    /**
47
     * @return BlocksFiles
48
     */
49
    public static function &getInstance()
50
    {
51
        static $instance = false;
52
        if (!$instance) {
53
            $instance = new self();
54
        }
55
56
        return $instance;
57
    }
58
59
    /**
60
     * @public function write
61
    
62
     * @param $module
63
     * @param $table
64
	 * @param $filename
65
     */
66
    public function write($module, $table, $filename)
67
    {
68
        $this->setModule($module);
69
        $this->setTable($table);
70
		$this->setFileName($filename);
71
    }
72
73
    /*
74
    *  @private function getBlocksShow
75
    *  @param null
76
    */
77
    /**
78
     * @param $moduleDirname
79
     * @param $tableName
80
     * @param $tableFieldname
81
     * @param $tableCategory
82
     * @param $fields
83
     * @param $fieldId
84
     *
85
     * @return string
86
     */
87
    private function getBlocksShow($moduleDirname, $tableName, $tableFieldname, $fields, $fieldId, $fieldParent = 0)
88
    {
89
        $stuModuleDirname = strtoupper($moduleDirname);
90
        $ucfModuleDirname = ucfirst($moduleDirname);
91
        $ret = <<<EOT
92
include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/include/common.php';
93
// Function show block
94
function b_{$moduleDirname}_{$tableName}_show(\$options)
95
{
96
    include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/class/{$tableName}.php';
97
    \$myts =& MyTextSanitizer::getInstance();
98
    \$GLOBALS['xoopsTpl']->assign('{$moduleDirname}_upload_url', {$stuModuleDirname}_UPLOAD_URL);
99
    \$block       = array();
100
    \$typeBlock   = \$options[0];
101
    \$limit       = \$options[1];
102
    \$lenghtTitle = \$options[2];
103
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
104
    \${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');
105
    \$criteria = new CriteriaCompo();
106
    array_shift(\$options);
107
    array_shift(\$options);
108
    array_shift(\$options);\n
109
EOT;
110
        if (1 == $fieldParent) {
111
            $ret .= <<<EOT
112
	\${$tableName} = {$moduleDirname}_getMyItemIds('{$moduleDirname}_view', '{$moduleDirname}');
113
    \$criteria->add(new Criteria('cid', '(' . implode(',', \${$tableName}) . ')','IN'));
114
    if (1 != (count(\$options) && 0 == \$options[0])) {
115
        \$criteria->add(new Criteria('{$fieldId}', {$moduleDirname}_block_addCatSelect(\$options), 'IN'));
116
    }
117
118
    if (\$typeBlock)
119
    {
120
        \$criteria->add(new Criteria('{$fieldId}', 0, '!='));
121
        \$criteria->setSort('{$fieldId}');
122
        \$criteria->setOrder('ASC');
123
    }\n
124
EOT;
125
        } else {
126
            $ret .= <<<EOT
127
	switch(\$typeBlock)
128
	{
129
		// For the block: {$tableName} last
130
		case 'last':
131
			\$criteria->add(new Criteria('{$tableFieldname}_display', 1));
132
			\$criteria->setSort('{$tableFieldname}_created');
133
			\$criteria->setOrder('DESC');
134
		break;
135
		// For the block: {$tableName} new
136
		case 'new':
137
			\$criteria->add(new Criteria('{$tableFieldname}_display', 1));
138
			\$criteria->add(new Criteria('{$tableFieldname}_created', strtotime(date(_SHORTDATESTRING)), '>='));
139
			\$criteria->add(new Criteria('{$tableFieldname}_created', strtotime(date(_SHORTDATESTRING))+86400, '<='));
140
			\$criteria->setSort('{$tableFieldname}_created');
141
			\$criteria->setOrder('ASC');
142
		break;
143
		// For the block: {$tableName} hits
144
		case 'hits':
145
            \$criteria->setSort('{$tableFieldname}_hits');
146
            \$criteria->setOrder('DESC');
147
        break;
148
		// For the block: {$tableName} top
149
		case 'top':
150
            \$criteria->setSort('{$tableFieldname}_top');
151
            \$criteria->setOrder('ASC');
152
        break;
153
		// For the block: {$tableName} random
154
		case 'random':
155
			\$criteria->add(new Criteria('{$tableFieldname}_display', 1));
156
			\$criteria->setSort('RAND()');
157
		break;
158
	}\n
159
EOT;
160
        }
161
        $ret .= <<<EOT
162
    \$criteria->setLimit(\$limit);
163
    \${$tableName}All = \${$tableName}Handler->getAll(\$criteria);
164
	unset(\$criteria);
165
    foreach(array_keys(\${$tableName}All) as \$i)
166
    {\n
167
EOT;
168
        foreach (array_keys($fields) as $f) {
169
            $fieldName = $fields[$f]->getVar('field_name');
170
            // Verify if table_fieldname is not empty
171
            //$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...
172
            $rpFieldName = $this->getRightString($fieldName);
173
            $fieldElement = $fields[$f]->getVar('field_element');
174
            if (1 == $fields[$f]->getVar('field_block')) {
175
                switch ($fieldElement) {
176
                    case 2:
177
                        $ret .= <<<EOT
178
        \$block[\$i]['{$rpFieldName}'] = \$myts->htmlSpecialChars(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
179
EOT;
180
                        break;
181
                    case 3:
182
                    case 4:
183
                        $ret .= <<<EOT
184
		\$block[\$i]['{$rpFieldName}'] = strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
185
EOT;
186
                        break;
187
                    case 8:
188
                    $ret .= <<<EOT
189
		\$block[\$i]['{$rpFieldName}'] = XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
190
EOT;
191
                        break;
192
                    case 15:
193
                    $ret .= <<<EOT
194
		\$block[\$i]['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
195
EOT;
196
                        break;
197
                    default:
198
                        $ret .= <<<EOT
199
        \$block[\$i]['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n
200
EOT;
201
                        break;
202
                }
203
            }
204
        }
205
        $ret .= <<<EOT
206
    }
207
    return \$block;
208
}\n\n
209
EOT;
210
211
        return $ret;
212
    }
213
214
    /*
215
    *  @public function getBlocksEdit
216
    *  @param string $moduleDirname
217
    *  @param string $tableName
218
    *  @param string $fieldId
219
    *  @param string $fieldMain
220
    *  @param string $language
221
    */
222
    /**
223
     * @param $moduleDirname
224
     * @param $tableName
225
     * @param $fieldId
226
     * @param $fieldMain
227
     * @param $language
228
     *
229
     * @return string
230
     */
231
    private function getBlocksEdit($moduleDirname, $tableName, $fieldId, $fieldMain, $language)
232
    {
233
        $stuModuleDirname = strtoupper($moduleDirname);
234
        $stuTableName = strtoupper($tableName);
235
        $ucfModuleDirname = ucfirst($moduleDirname);
236
        $ret = <<<EOT
237
// Function edit block
238
function b_{$moduleDirname}_{$tableName}_edit(\$options)
239
{
240
    include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/class/{$tableName}.php';
241
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
242
    \${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');
243
    \$GLOBALS['xoopsTpl']->assign('{$moduleDirname}_upload_url', {$stuModuleDirname}_UPLOAD_URL);
244
    \$form  = {$language}DISPLAY;
245
    \$form .= "<input type='hidden' name='options[0]' value='".\$options[0]."' />";
246
    \$form .= "<input type='text' name='options[1]' size='5' maxlength='255' value='".\$options[1]."' />&nbsp;<br />";
247
    \$form .= {$language}TITLE_LENGTH." : <input type='text' name='options[2]' size='5' maxlength='255' value='".\$options[2]."' /><br /><br />";
248
    array_shift(\$options);
249
    array_shift(\$options);
250
    array_shift(\$options);
251
    \$criteria = new CriteriaCompo();
252
    \$criteria->add(new Criteria('{$fieldId}', 0, '!='));
253
    \$criteria->setSort('{$fieldId}');
254
    \$criteria->setOrder('ASC');
255
    \${$tableName}All = \${$tableName}Handler->getAll(\$criteria);
256
    unset(\$criteria);
257
    \$form .= {$language}{$stuTableName}_TO_DISPLAY."<br /><select name='options[]' multiple='multiple' size='5'>";
258
    \$form .= "<option value='0' " . (array_search(0, \$options) === false ? "" : "selected='selected'") . ">" .{$language}ALL_{$stuTableName} . "</option>";
259
    foreach (array_keys(\${$tableName}All) as \$i) {
260
        \${$fieldId} = \${$tableName}All[\$i]->getVar('{$fieldId}');
261
        \$form .= "<option value='" . \${$fieldId} . "' " . (array_search(\${$fieldId}, \$options) === false ? "" : "selected='selected'") . ">".\${$tableName}All[\$i]->getVar('{$fieldMain}')."</option>";
262
    }
263
    \$form .= "</select>";
264
    return \$form;
265
}
266
EOT;
267
268
        return $ret;
269
    }
270
271
    /*
272
    *  @public function renderFile
273
    *  @param null
274
    */
275
    /**
276
     * @param null
277
     *
278
     * @return bool|string
279
     */
280
    public function render()
281
    {
282
        $module = $this->getModule();
283
        $table = $this->getTable();
284
        $moduleDirname = $module->getVar('mod_dirname');
285
		$filename = $this->getFileName();
286
        $tableName = $table->getVar('table_name');
287
        $tableFieldname = $table->getVar('table_fieldname');
288
        $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...
289
        $language = $this->getLanguage($moduleDirname, 'MB');
290
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
291
        foreach (array_keys($fields) as $f) {
292
            $fieldName = $fields[$f]->getVar('field_name');
293
            $fieldParent = $fields[$f]->getVar('field_parent');
294
            if (0 == $f) {
295
                $fieldId = $fieldName;
296
            }
297
            if (1 == $fields[$f]->getVar('field_main')) {
298
                $fieldMain = $fieldName;
299
            }
300
        }
301
        $content = $this->getHeaderFilesComments($module, $filename);
302
        $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...
303
        $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...
304
        //
305
        $this->create($moduleDirname, 'blocks', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
306
307
        return $this->renderFile();
308
    }
309
}
310