Completed
Push — master ( 6b7a6d...74143b )
by Gino
20s
created

getTemplatesUserIndexTableTBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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