Completed
Pull Request — master (#126)
by Gino
03:40
created

TemplatesUserPagesList::renderFile()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 3
eloc 19
c 4
b 1
f 0
nc 3
nop 0
dl 0
loc 32
rs 8.8571
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: TemplatesUserPagesList.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TemplatesUserPagesList.
27
 */
28
class TemplatesUserPagesList 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 TemplatesUserPagesList
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
    *  @param string $module
62
    *  @param string $table
63
    *  @param string $filename
64
    */
65
    /**
66
     * @param $module
67
     * @param $table
68
     */
69
    public function write($module, $table, $tables, $filename)
70
    {
71
        $this->setModule($module);
72
        $this->setTable($table);
73
        $this->setTables($tables);
74
        $this->setFileName($filename);
75
    }
76
77
    /*
78
    *  @private function getTemplatesUserPagesListHeader
79
    *  @param string $language
80
    */
81
    /**
82
     * @param $language
83
     *
84
     * @return string
85
     */
86
    private function getTemplatesUserPagesListStartTable()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
87
    {
88
        $ret = <<<EOT
89
<div class="table-responsive">
90
    <table class="table table-<{\$type}>">\n
91
EOT;
92
93
        return $ret;
94
    }
95
96
    /*
97
    *  @private function getTemplatesUserPagesListThead
98
    *  @param string $language
99
    */
100
    /**
101
     * @param $language
102
     *
103
     * @return string
104
     */
105
    private function getTemplatesUserPagesListThead($table, $language)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $language is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        $ret = <<<EOT
108
		<thead>
109
			<tr>\n
110
EOT;
111
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
112
        foreach (array_keys($fields) as $f) {
113
            if (1 == $fields[$f]->getVar('field_user')) {
114
                if (1 == $fields[$f]->getVar('field_thead')) {
115
                    $fieldName = $fields[$f]->getVar('field_name');
116
                    $rpFieldName = $this->getRightString($fieldName);
117
                    $ret .= <<<EOT
118
				<th><{\$list.{$rpFieldName}}></th>\n
119
EOT;
120
                }
121
            }
122
        }
123
        $ret .= <<<EOT
124
			</tr>
125
		</thead>\n
126
EOT;
127
128
        return $ret;
129
    }
130
131
    /*
132
    *  @private function getTemplatesUserPagesListTbody
133
    *  @param string $moduleDirname
134
    *  @param string $table
135
    *  @param string $language
136
    */
137
    /**
138
     * @param $moduleDirname
139
     * @param $table
140
     * @param $language
141
     *
142
     * @return string
143
     */
144
    private function getTemplatesUserPagesListTbody($moduleDirname, $table, $language)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $language is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        $tableName = $table->getVar('table_name');
147
        $ret = <<<EOT
148
		<tbody>
149
			<tr>\n
150
EOT;
151
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
152
        foreach (array_keys($fields) as $f) {
153
            $fieldElement = $fields[$f]->getVar('field_element');
154
            if (1 == $fields[$f]->getVar('field_user')) {
155
                if (1 == $fields[$f]->getVar('field_tbody')) {
156
                    switch ($fieldElement) {
157
                        default:
158
                        case 10:
159
                            $fieldName = $fields[$f]->getVar('field_name');
160
                            $rpFieldName = $this->getRightString($fieldName);
161
                            $ret .= <<<EOT
162
				<td class="center pad5"><img src="<{\$xoops_icons32_url}>/<{\$list.{$rpFieldName}}>" alt="{$tableName}" /></td>\n
163
EOT;
164
                            break;
165
                        case 13:
166
                            $fieldName = $fields[$f]->getVar('field_name');
167
                            $rpFieldName = $this->getRightString($fieldName);
168
                            $ret .= <<<EOT
169
				<td class="center pad5"><img src="<{\${$moduleDirname}_upload_url}>/images/{$tableName}/<{\$list.{$rpFieldName}}>" alt="{$tableName}" /></td>\n
170
EOT;
171
                            break;
172
                        case 2:
173
                        case 3:
174
                        case 4:
175
                            $fieldName = $fields[$f]->getVar('field_name');
176
                            $rpFieldName = $this->getRightString($fieldName);
177
                            $ret .= <<<EOT
178
				<td class="justify pad5"><{\$list.{$rpFieldName}}></td>\n
179
EOT;
180
                            break;
181
                    }
182
                }
183
            }
184
        }
185
        $ret .= <<<EOT
186
			</tr>
187
		</tbody>\n
188
EOT;
189
190
        return $ret;
191
    }
192
193
    /*
194
    *  @private function getTemplatesUserPagesListTfoot
195
    *  @param string $moduleDirname
196
    *  @param string $table
197
    *  @param string $language
198
    */
199
    /**
200
     * @param $moduleDirname
201
     * @param $table
202
     * @param $language
203
     *
204
     * @return string
205
     */
206
    private function getTemplatesUserPagesListTfoot($table, $language)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $language is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
    {
208
        $tableName = $table->getVar('table_name');
0 ignored issues
show
Unused Code introduced by
$tableName 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...
209
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
210
        $ret = <<<EOT
211
		<tfoot>
212
			<tr>\n
213
EOT;
214
215
        foreach (array_keys($fields) as $f) {
216
            if (1 == $fields[$f]->getVar('field_user')) {
217
                if (1 == $fields[$f]->getVar('field_tfoot')) {
218
                    $fieldName = $fields[$f]->getVar('field_name');
219
                    $rpFieldName = $this->getRightString($fieldName);
220
                    $ret .= <<<EOT
221
				<td class="center"><{\$list.{$rpFieldName}}></td>\n
222
EOT;
223
                }
224
            }
225
        }
226
        $ret .= <<<EOT
227
			</tr>
228
		</tfoot>\n
229
EOT;
230
231
        return $ret;
232
    }
233
234
    /*
235
    *  @private function getTemplatesUserPagesListEndTable
236
    *  @param null
237
    */
238
    /**
239
     * @param null
240
     *
241
     * @return string
242
     */
243
    private function getTemplatesUserPagesListEndTable()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
244
    {
245
        $ret = <<<EOT
246
	</table>
247
</div>\n
248
EOT;
249
250
        return $ret;
251
    }
252
253
    /*
254
    *  @private function getTemplatesUserPagesListPanel
255
    *  @param string $moduleDirname
256
    *  @param string $table
257
    *  @param string $language
258
    */
259
    /**
260
     * @param $moduleDirname
261
     * @param $table
262
     * @param $language
263
     *
264
     * @return string
265
     */
266
    private function getTemplatesUserPagesListPanel($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $language)
267
    {
268
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
269
        $fields = $this->getTableFields($tableMid, $tableId);
270
        $ret = '';
271
        $retNumb = '';
272
        foreach (array_keys($fields) as $f) {
273
            $fieldElement = $fields[$f]->getVar('field_element');
274
            if (1 == $fields[$f]->getVar('field_user')) {
275
                if (1 == $fields[$f]->getVar('field_thead')) {
276
                    switch ($fieldElement) {
277
                        default:
278
                        case 2:
279
                            $fieldName = $fields[$f]->getVar('field_name');
280
                            $rpFieldName = $this->getRightString($fieldName);
281
                            $doubleVar = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
282
                            $retNumb = $hc->getHtmlHNumb($doubleVar, '3', 'panel-title');
283
                            break;
284
                    }
285
                }
286
            }
287
        }
288
        $ret .= $hc->getHtmlDiv($retNumb, 'panel-heading').PHP_EOL;
289
        $retElem = '';
290
        foreach (array_keys($fields) as $f) {
291
            $fieldElement = $fields[$f]->getVar('field_element');
292
            if (1 == $fields[$f]->getVar('field_user')) {
293
                if (1 == $fields[$f]->getVar('field_tbody')) {
294
                    switch ($fieldElement) {
295
                        default:
296
                        case 3:
297
                        case 4:
298
                            $fieldName = $fields[$f]->getVar('field_name');
299
                            $rpFieldName = $this->getRightString($fieldName);
300
                            $doubleVar = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
301
                            $retElem .= $hc->getHtmlSpan($doubleVar, 'col-sm-9 justify').PHP_EOL;
302
                            break;
303
                        case 10:
304
                            $fieldName = $fields[$f]->getVar('field_name');
305
                            $rpFieldName = $this->getRightString($fieldName);
306
                            $singleVar = $hc->getSmartySingleVar('xoops_icons32_url');
307
                            $doubleVar = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
308
                            $img = $hc->getHtmlImage($singleVar.'/'.$doubleVar, "{$tableName}");
309
                            $retElem .= $hc->getHtmlSpan($img, 'col-sm-3').PHP_EOL;
310
                            unset($img);
311
                            break;
312
                        case 13:
313
                            $fieldName = $fields[$f]->getVar('field_name');
314
                            $rpFieldName = $this->getRightString($fieldName);
315
                            $singleVar = $hc->getSmartySingleVar($moduleDirname.'_upload_url');
316
                            $doubleVar = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
317
                            $img = $hc->getHtmlImage($singleVar."/images/{$tableName}/".$doubleVar, "{$tableName}");
318
                            $retElem .= $hc->getHtmlSpan($img, 'col-sm-3').PHP_EOL;
319
                            unset($img);
320
                            break;
321
                    }
322
                }
323
            }
324
        }
325
        $ret .= $hc->getHtmlDiv($retElem, 'panel-body').PHP_EOL;
326
        $retFoot = '';
327
        foreach (array_keys($fields) as $f) {
328
            if (1 == $fields[$f]->getVar('field_user')) {
329
                if (1 == $fields[$f]->getVar('field_tfoot')) {
330
                    $fieldName = $fields[$f]->getVar('field_name');
331
                    $rpFieldName = $this->getRightString($fieldName);
332
                    $langConst = strtoupper($tableSoleName).'_'.strtoupper($rpFieldName);
333
                    $lang = $hc->getSmartyConst($language, $langConst);
334
                    $doubleVar = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
335
                    $retFoot .= $hc->getHtmlSpan($lang.': '.$doubleVar, 'block-pie justify').PHP_EOL;
336
                }
337
            }
338
        }
339
        $ret .= $hc->getHtmlDiv($retFoot, 'panel-foot').PHP_EOL;
340
341
        return $ret;
342
    }
343
344
    /*
345
    *  @public function render
346
    *  @param string $filename
347
    */
348
    /**
349
     * @param $filename
350
     *
351
     * @return bool|string
352
     */
353
    public function render()
354
    {
355
        $module = $this->getModule();
356
        $table = $this->getTable();
0 ignored issues
show
Unused Code introduced by
$table 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...
357
        $tables = $this->getTables();
358
        //$tables = $this->getTableTables($module->getVar('mod_id'), 'table_order');
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
359
        $moduleDirname = $module->getVar('mod_dirname');
360
        $filename = $this->getFileName();
361
        $language = $this->getLanguage($moduleDirname, 'MA');
362
        $content = '';
363
        foreach (array_keys($tables) as $t) {
364
            $tableId = $tables[$t]->getVar('table_id');
365
            $tableMid = $tables[$t]->getVar('table_mid');
366
            $tableName = $tables[$t]->getVar('table_name');
367
            $tableSoleName = $tables[$t]->getVar('table_solename');
368
            $tableCategory[] = $tables[$t]->getVar('table_category');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tableCategory was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tableCategory = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
369
            $tableIndex = $tables[$t]->getVar('table_index');
0 ignored issues
show
Unused Code introduced by
$tableIndex 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...
370
            if (in_array(0, $tableCategory)) {
0 ignored issues
show
Bug introduced by
The variable $tableCategory 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...
371
                $content .= $this->getTemplatesUserPagesListPanel($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $language);
372
            }
373
        }
374
        /*$content = $this->getTemplatesUserPagesListStartTable();
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
375
        $content .= $this->getTemplatesUserPagesListThead($table, $language);
376
        $content .= $this->getTemplatesUserPagesListTbody($moduleDirname, $table, $language);
377
        $content .= $this->getTemplatesUserPagesListTfoot($table, $language);
378
        $content .= $this->getTemplatesUserPagesListEndTable();*/
379
        //$content = $this->getTemplatesUserPagesListPanel($moduleDirname, $table);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
380
        //
381
        $this->create($moduleDirname, 'templates', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
382
383
        return $this->renderFile();
384
    }
385
}
386