Completed
Pull Request — master (#134)
by Michael
03:43
created

TemplatesUserIndex   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 336
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 336
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 9 2
A write() 0 7 1
A getTemplateUserIndexHeader() 0 6 1
A getTemplatesUserIndexTable() 0 9 1
A getTemplatesUserIndexTableThead() 0 11 1
A getTemplatesUserIndexTableTfoot() 0 8 1
B getTemplatesUserIndexBodyDefault() 0 45 2
B getTemplateUserIndexCategories() 0 42 1
B getTemplateUserIndexTable() 0 26 1
A getTemplateUserIndexFooter() 0 6 1
B render() 0 29 6
A getTemplatesUserIndexTableTBody() 0 14 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: templates_index.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TemplatesUserIndex.
27
 */
28
class TemplatesUserIndex 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
    public function __construct()
36
    {
37
        parent::__construct();
38
    }
39
40
    /*
41
    *  @static function getInstance
42
    *  @param null
43
    */
44
    /**
45
     * @return TemplatesUserIndex
46
     */
47
    public static function getInstance()
48
    {
49
        static $instance = false;
50
        if (!$instance) {
51
            $instance = new self();
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     * @public function write
59
     *
60
     * @param $module
61
     * @param $table
62
     * @param $filename
63
     */
64
    public function write($module, $table, $tables, $filename)
65
    {
66
        $this->setModule($module);
67
        $this->setTable($table);
68
        $this->setTables($tables);
69
        $this->setFileName($filename);
70
    }
71
72
    /*
73
    *  @public function getTemplateUserIndexHeader
74
    *  @param $moduleDirname
75
    */
76
    /**
77
     * @return bool|string
78
     */
79
    public function getTemplateUserIndexHeader($moduleDirname)
80
    {
81
        $hc = TDMCreateSmartyCode::getInstance();
82
83
        return $hc->getSmartyIncludeFile($moduleDirname, 'header').PHP_EOL;
84
    }
85
86
    /*
87
    *  @private function getTemplatesUserIndexTable
88
    *  @param string $language
89
    */
90
    /**
91
     * @param $language
92
     *
93
     * @return string
94
     */
95
    private function getTemplatesUserIndexTable($moduleDirname, $tableName, $tableSolename, $language)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
96
    {
97
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
98
        $single = $hc->getSmartySingleVar('table_type');
99
        $table = $this->getTemplatesUserIndexTableThead($tableName, $language);
100
        $table .= $this->getTemplatesUserIndexTableTBody($moduleDirname, $tableName, $tableSolename, $language);
101
102
        return $hc->getHtmlTable($table, 'table table-'.$single).PHP_EOL;
103
    }
104
105
    /*
106
    *  @private function getTemplatesUserIndexThead
107
    *  @param string $language
108
    */
109
    /**
110
     * @param $language
111
     *
112
     * @return string
113
     */
114
    private function getTemplatesUserIndexTableThead($tableName, $language)
115
    {
116
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
117
        $stuTableName = strtoupper($tableName);
118
        $lang = $hc->getSmartyConst($language, $stuTableName);
119
        $col = $hc->getSmartySingleVar('numb_col');
120
        $th = $hc->getHtmlTableHead($lang, '', $col).PHP_EOL;
121
        $tr = $hc->getHtmlTableRow($th, 'head').PHP_EOL;
122
123
        return $hc->getHtmlTableThead($tr).PHP_EOL;
124
    }
125
126
    /*
127
    *  @private function getTemplatesUserIndexTbody
128
    *  @param string $moduleDirname
129
    *  @param string $table
130
    *  @param string $language
131
    */
132
    /**
133
     * @param $moduleDirname
134
     * @param $table
135
     * @param $language
136
     *
137
     * @return string
138
     */
139
    private function getTemplatesUserIndexTableTBody($moduleDirname, $tableName, $tableSolename, $language)
0 ignored issues
show
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...
140
    {
141
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
142
        $type = $hc->getSmartySingleVar('panel_type');
143
        $include = $hc->getSmartyIncludeFileListForeach($moduleDirname, $tableName, $tableSolename);
144
        $div = $hc->getHtmlDiv($include, 'panel panel-'.$type);
145
        $cont = $hc->getHtmlTableData($div).PHP_EOL;
146
        $html = $hc->getHtmlEmpty('</tr><tr>').PHP_EOL;
147
        $cont .= $hc->getSmartyConditions($tableSolename.'.count', ' is div by ', '$divideby', $html).PHP_EOL;
148
        $foreach = $hc->getSmartyForeach($tableSolename, $tableName, $cont).PHP_EOL;
149
        $tr = $hc->getHtmlTableRow($foreach).PHP_EOL;
150
151
        return $hc->getHtmlTableTbody($tr).PHP_EOL;
152
    }
153
154
    /*
155
    *  @private function getTemplatesUserIndexTfoot
156
    *  @param string $moduleDirname
157
    *  @param string $table
158
    *  @param string $language
159
    */
160
    /**
161
     * @param $moduleDirname
162
     * @param $table
163
     * @param $language
164
     *
165
     * @return string
166
     */
167
    private function getTemplatesUserIndexTableTfoot()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
168
    {
169
        $hc = TDMCreateHtmlCode::getInstance();
170
        $td = $hc->getHtmlTableData('&nbsp;').PHP_EOL;
171
        $tr = $hc->getHtmlTableRow($td).PHP_EOL;
172
173
        return $hc->getHtmlTableTfoot($tr).PHP_EOL;
174
    }
175
176
    /*
177
    *  @public function getTemplatesUserIndexBodyDefault
178
    *  @param null
179
    */
180
    /**
181
     * @return bool|string
182
     */
183
    public function getTemplatesUserIndexBodyDefault($module, $table, $language)
184
    {
185
        $moduleDirname = $module->getVar('mod_dirname');
186
        $tableName = $table->getVar('table_name');
187
        $ret = <<<EOT
188
<{if count(\${$tableName}) == 0}>
189
<table class="table table-<{\$table_type}>">
190
    <thead>
191
        <tr class="center">
192
            <th><{\$smarty.const.{$language}TITLE}>  -  <{\$smarty.const.{$language}DESC}></th>
193
        </tr>
194
    </thead>
195
    <tbody>
196
        <tr class="center">
197
            <td class="bold pad5">
198
                <ul class="menu text-center">
199
                    <li><a href="<{\${$moduleDirname}_url}>"><{\$smarty.const.{$language}INDEX}></a></li>\n
200
EOT;
201
        $tables = $this->getTableTables($module->getVar('mod_id'), 'table_order');
202
        foreach (array_keys($tables) as $i) {
203
            $tableName = $tables[$i]->getVar('table_name');
204
            $stuTableName = strtoupper($tableName);
205
            $ret .= <<<EOT
206
                    <li><a href="<{\${$moduleDirname}_url}>/{$tableName}.php"><{\$smarty.const.{$language}{$stuTableName}}></a></li>\n
207
EOT;
208
        }
209
        $ret .= <<<EOT
210
                </ul>
211
				<div class="justify pad5"><{\$smarty.const.{$language}INDEX_DESC}></div>
212
            </td>
213
        </tr>
214
    </tbody>
215
    <tfoot>
216
    <{if \$adv != ''}>
217
        <tr class="center"><td class="center bold pad5"><{\$adv}></td></tr>
218
    <{else}>
219
        <tr class="center"><td class="center bold pad5">&nbsp;</td></tr>
220
    <{/if}>
221
    </tfoot>
222
</table>
223
<{/if}>\n
224
EOT;
225
226
        return $ret;
227
    }
228
229
    /*
230
    *  @public function getTemplateUserIndexCategories
231
    *  @param null
232
    */
233
    /**
234
     * @return bool|string
235
     */
236
    public function getTemplateUserIndexCategories($moduleDirname, $tableName, $tableSoleName, $language)
237
    {
238
        $stuTableName = strtoupper($tableName);
239
        $ret = <<<EOT
240
<{if count(\${$tableName}) > 0}>
241
<div class="table-responsive">
242
    <table class="table table-<{\$table_type}>">
243
		<thead>
244
			<tr>
245
				<th colspan="<{\$numb_col}>"><{\$smarty.const.{$language}{$stuTableName}}></th>
246
			</tr>
247
		</thead>
248
		<tbody>
249
			<tr>
250
				<{foreach item={$tableSoleName} from=\${$tableName}}>
251
				<td>
252
					<{include file="db:{$moduleDirname}_{$tableName}_list.tpl" {$tableSoleName}=\${$tableSoleName}}>
253
				</td>
254
			<{if \${$tableSoleName}.count is div by \$numb_col}>
255
			</tr><tr>
256
			<{/if}>
257
				<{/foreach}>
258
			</tr>
259
		</tbody>
260
		<tfoot>
261
			<tr>
262
				<td colspan="<{\$numb_col}>" class="{$tableSoleName}-thereare"><{\$lang_thereare}></td>
263
			</tr>
264
		</tfoot>
265
	</table>
266
</div>
267
<{/if}>\n
268
EOT;
269
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
270
        $single = $hc->getSmartySingleVar('table_type');
271
        $table = $this->getTemplatesUserIndexTableThead($tableName, $language);
272
        $table .= $this->getTemplatesUserIndexTableTBody($moduleDirname, $tableName, $tableSoleName, $language);
273
        $table .= $hc->getHtmlTable($table, 'table table-'.$single).PHP_EOL;
274
        $div = $hc->getHtmlDiv($table, 'table-responsive').PHP_EOL;
0 ignored issues
show
Unused Code introduced by
$div 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...
275
276
        return $ret/*$hc->getSmartyConditions($tableName, ' > ', '0', $div, false, true)*/.PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
277
    }
278
279
    /*
280
    *  @public function getTemplateUserIndexTable
281
    *  @param null
282
    */
283
    /**
284
     * @return bool|string
285
     */
286
    public function getTemplateUserIndexTable($moduleDirname, $tableName, $tableSoleName, $language)
287
    {
288
        $ret = <<<EOT
289
<{if count(\${$tableName}) > 0}>
290
	<!-- Start Show new {$tableName} in index -->
291
	<div class="{$moduleDirname}-linetitle"><{\$smarty.const.{$language}INDEX_LATEST_LIST}></div>
292
	<table class="table table-<{\$table_type}>">
293
		<tr>
294
			<!-- Start new link loop -->
295
			<{section name=i loop=\${$tableName}}>
296
				<td class="col_width<{\$numb_col}> top center">
297
					<{include file="db:{$moduleDirname}_{$tableName}_list.tpl" {$tableSoleName}=\${$tableName}[i]}>
298
				</td>
299
	<{if \${$tableName}[i].count is div by \$divideby}>
300
		</tr><tr>
301
	<{/if}>
302
			<{/section}>
303
	<!-- End new link loop -->
304
		</tr>
305
	</table>
306
<!-- End Show new files in index -->
307
<{/if}>\n
308
EOT;
309
310
        return $ret;
311
    }
312
313
    /*
314
    *  @public function getTemplateUserIndexFooter
315
    *  @param $moduleDirname
316
    */
317
    /**
318
     * @return bool|string
319
     */
320
    public function getTemplateUserIndexFooter($moduleDirname)
321
    {
322
        $hc = TDMCreateSmartyCode::getInstance();
323
324
        return $hc->getSmartyIncludeFile($moduleDirname, 'footer');
325
    }
326
327
    /*
328
    *  @public function render
329
    *  @param null
330
    */
331
    /**
332
     * @return bool|string
333
     */
334
    public function render()
335
    {
336
        $module = $this->getModule();
337
        $table = $this->getTable();
338
        $tables = $this->getTableTables($module->getVar('mod_id'), 'table_order');
339
        $filename = $this->getFileName();
340
        $moduleDirname = $module->getVar('mod_dirname');
341
        $language = $this->getLanguage($moduleDirname, 'MA');
342
        $content = $this->getTemplateUserIndexHeader($moduleDirname);
343
        $content .= $this->getTemplatesUserIndexBodyDefault($module, $table, $language);
344
        foreach (array_keys($tables) as $t) {
345
            $tableName = $tables[$t]->getVar('table_name');
346
            $tableSoleName = $tables[$t]->getVar('table_solename');
347
            $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...
348
            $tableFieldname = $tables[$t]->getVar('table_fieldname');
0 ignored issues
show
Unused Code introduced by
$tableFieldname 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...
349
            $tableIndex[] = $tables[$t]->getVar('table_index');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tableIndex was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tableIndex = 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...
350
            if (in_array(1, $tableCategory) && in_array(1, $tableIndex)) {
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...
Bug introduced by
The variable $tableIndex 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...
351
                $content .= $this->getTemplateUserIndexCategories($moduleDirname, $tableName, $tableSoleName, $language);
352
            }
353
            if (in_array(0, $tableCategory) && in_array(1, $tableIndex)) {
354
                $content .= $this->getTemplateUserIndexTable($moduleDirname, $tableName, $tableSoleName, $language);
355
            }
356
        }
357
        $content .= $this->getTemplateUserIndexFooter($moduleDirname);
358
        $tdmcfile = TDMCreateFile::getInstance();
359
        $tdmcfile->create($moduleDirname, 'templates', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
360
361
        return $tdmcfile->renderFile();
362
    }
363
}
364