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

TemplatesAdminPages::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 2
b 2
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: TemplatesAdminPages.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TemplatesAdminPages.
27
 */
28
class TemplatesAdminPages 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 TemplatesAdminPages
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
    */
64
    public function write($module, $table, $filename)
65
    {
66
        $this->setModule($module);
67
        $this->setTable($table);
68
        $this->setFileName($filename);
69
    }
70
71
    /*
72
    *  @private function getTemplatesAdminPagesHeader
73
    *  @param string $moduleDirname
74
    *  @return string
75
    */
76
    private function getTemplatesAdminPagesHeader($moduleDirname)
77
    {
78
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
79
        $ret = $hc->getHtmlComment('Header').PHP_EOL;
80
        $ret .= $hc->getSmartyIncludeFile($moduleDirname, 'header', true);
81
82
        return $ret;
83
    }
84
85
    /*
86
    *  @private function getTemplatesAdminPagesTableThead
87
    *  @param string $moduleDirname
0 ignored issues
show
Bug introduced by
There is no parameter named $moduleDirname. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
88
    *  @param string $tableName
0 ignored issues
show
Bug introduced by
There is no parameter named $tableName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
89
    *  @param string $fields
90
    *  @param string $language
91
    *  @return string
92
    */
93
    private function getTemplatesAdminPagesTableThead($tableSoleName, $tableAutoincrement, $fields, $language)
94
    {
95
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
96
        $th = '';
97
        $langHeadId = strtoupper($tableSoleName).'_ID';
98
        if (1 == $tableAutoincrement) {
99
            $lang = $hc->getSmartyConst($language, $langHeadId);
100
            $th  .= $hc->getHtmlTag('th', array('class' => 'center'), $lang, false, false, "\t\t\t");
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to TDMCreateHtmlSmartyCodes::getHtmlTag() has too many arguments starting with ' '.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
        }
102
        foreach (array_keys($fields) as $f) {
103
            $fieldName = $fields[$f]->getVar('field_name');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $fields[$f] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
104
            $rpFieldName = $this->getRightString($fieldName);
105
            $langFieldName = strtoupper($tableSoleName).'_'.strtoupper($rpFieldName);
106
            if (1 == $fields[$f]->getVar('field_inlist')) {
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $fields[$f] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
107
                $lang = $hc->getSmartyConst($language, $langFieldName);
108
                $th  .= $hc->getHtmlTag('th', array('class' => 'center'), $lang, false, false, "\t\t\t");
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to TDMCreateHtmlSmartyCodes::getHtmlTag() has too many arguments starting with ' '.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
109
            }
110
        }
111
112
        $lang = $hc->getSmartyConst($language, 'FORM_ACTION');
113
        $th  .= $hc->getHtmlTag('th', array('class' => 'center width5'), $lang, false, false, "\t\t\t");
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to TDMCreateHtmlSmartyCodes::getHtmlTag() has too many arguments starting with ' '.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
        $tr = $hc->getHtmlTag('tr', array('class' => 'head'), $th).PHP_EOL;
115
        $ret = $hc->getHtmlTag('thead', array(), $tr);
116
117
        return $ret;
118
    }
119
120
    /*
121
    *  @private function getTemplatesAdminPagesTableTBody
122
    *  @param string $moduleDirname
123
    *  @param string $tableName
124
    *  @param string $fields
125
    *  @param string $language
0 ignored issues
show
Bug introduced by
There is no parameter named $language. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
126
    *  @return string
127
    */
128
    private function getTemplatesAdminPagesTableTBody($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $fields)
129
    {
130
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
131
        $td = '';
132
        if (1 == $tableAutoincrement) {
133
            $double = $hc->getSmartyDoubleVar($tableSoleName, 'id');
134
            $td    .= $hc->getHtmlTableData($double, 'center');
135
        }
136
        foreach (array_keys($fields) as $f) {
137
            $fieldName = $fields[$f]->getVar('field_name');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $fields[$f] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
138
            $fieldElement = $fields[$f]->getVar('field_element');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $fields[$f] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
139
            $rpFieldName = $this->getRightString($fieldName);
140
            if (0 == $f) {
141
                $fieldId = $fieldName;
142
            }
143
            if (1 == $fields[$f]->getVar('field_inlist')) {
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $fields[$f] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
144
                switch ($fieldElement) {
145
                    case 9:
146
                        // This is to be reviewed, as it was initially to style = "backgroung-color: #"
147
                        // Now with HTML5 is not supported inline style in the parameters of the HTML tag
148
                        // Old code was <span style="background-color: #<{\$list.{$rpFieldName}}>;">...
149
                        $double = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
150
                        $span = $hc->getHtmlTag('span', array(), $double);
151
                        $td .= $hc->getHtmlTag('td', array('class' => 'center'), $span);
152
                        /*$ret .= <<<EOT
153
                    <td class="center"><span style="background-color: #<{\$list.{$rpFieldName}}>;">&nbsp;&nbsp;&nbsp;&nbsp;</span></td>\n
154
EOT;*/
155
                        break;
156
                    case 10:
157
                        $src = $hc->getSmartyNoSimbol('xoModuleIcons32');
158
                        $src .= $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
159
                        $img = $hc->getHtmlTag('img', array('src' => $src, 'alt' => $tableName), '', true, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
                        $td  .= $hc->getHtmlTag('td', array('class' => 'center'), "\n\t".$img);
161
                        break;
162
                    case 13:
163
                        $single = $hc->getSmartySingleVar($moduleDirname.'_upload_url');
164
                        $double = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
165
                        $img = $hc->getHtmlTag('img', array('src' => $single."/images/{$tableName}/".$double, 'alt' => $tableName, 'style' => 'max-width:100px'), '', true, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166
                        $td    .= $hc->getHtmlTag('td', array('class' => 'center'), $img, false, false, "\t\t");
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to TDMCreateHtmlSmartyCodes::getHtmlTag() has too many arguments starting with ' '.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
167
                        break;
168
                    default:
169
                        if (0 != $f) {
170
                            $double = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
171
                            $td    .= $hc->getHtmlTag('td', array('class' => 'center'), $double);
172
                        }
173
                        break;
174
                }
175
            }
176
        }
177
        $lang = $hc->getSmartyConst('', '_EDIT');
178
        $double = $hc->getSmartyDoubleVar($tableSoleName, 'id');
179
        $src = $hc->getSmartyNoSimbol('xoModuleIcons16 edit.png');
180
        $img = $hc->getHtmlTag('img', array('src' => $src, 'alt' => $tableName), '', true, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
        $anchor = $hc->getHtmlTag('a', array('href' => $tableName.".php?op=edit&amp;{$fieldId}=".$double, 'title' => $lang), "\n\t".$img);
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...
182
        $lang = $hc->getSmartyConst('', '_DELETE');
183
        $double = $hc->getSmartyDoubleVar($tableSoleName, 'id');
184
        $src = $hc->getSmartyNoSimbol('xoModuleIcons16 delete.png');
185
        $img = $hc->getHtmlTag('img', array('src' => $src, 'alt' => $tableName), '', true, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
186
        $anchor .= $hc->getHtmlTag('a', array('href' => $tableName.".php?op=delete&amp;{$fieldId}=".$double, 'title' => $lang), "\n\t".$img);
187
        $td     .= $hc->getHtmlTag('td', array('class' => 'center  width5'), "\n".$anchor);
188
        $cycle = $hc->getSmartyNoSimbol('cycle values=\'odd, even\'');
189
        $tr = $hc->getHtmlTag('tr', array('class' => $cycle), $td);
190
        $foreach = $hc->getSmartyForeach($tableSoleName, $tableName.'_list', $tr);
191
        $tbody = $hc->getHtmlTag('tbody', array(), $foreach);
192
193
        return $hc->getSmartyConditions($tableName.'_count', '', '', $tbody);
194
    }
195
196
    /*
197
    *  @private function getTemplatesAdminPagesTable
198
    *  @param string $moduleDirname
199
    *  @param string $tableName
200
    *  @param string $fields
201
    *  @param string $language
202
    *  @return string
203
    */
204
    private function getTemplatesAdminPagesTable($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $fields, $language)
205
    {
206
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
207
        $tbody = $this->getTemplatesAdminPagesTableThead($tableSoleName, $tableAutoincrement, $fields, $language);
208
        $tbody .= $this->getTemplatesAdminPagesTableTBody($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $fields);
209
210
        return $hc->getHtmlTable($tbody, 'table table-bordered');
211
    }
212
213
    /*
214
    *  @private function getTemplatesAdminPages
215
    *  @param string $moduleDirname
216
    *  @param string $tableName
217
    *  @param string $fields
218
    *  @param string $language
219
    *  @return string
220
    */
221
    private function getTemplatesAdminPages($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $fields, $language)
222
    {
223
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
224
        $htmlTable = $this->getTemplatesAdminPagesTable($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $fields, $language);
225
        $htmlTable .= $hc->getHtmlTag('div', array('class' => 'clear'), '&nbsp;');
226
        $single = $hc->getSmartySingleVar('pagenav');
227
        $div = $hc->getHtmlTag('div', array('class' => 'xo-pagenav floatright'), $single);
228
        $div       .= $hc->getHtmlTag('div', array('class' => 'clear spacer'), '');
229
        $htmlTable .= $hc->getSmartyConditions('pagenav', '', '', $div);
230
        $ifList = $hc->getSmartyConditions($tableName.'_list', '', '', $htmlTable);
231
        $single = $hc->getSmartySingleVar('form');
232
        $divComm = $hc->getHtmlComment('Display navigation');
0 ignored issues
show
Unused Code introduced by
$divComm 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...
233
        //$divComm .= $hc->getHtmlTag('div', array('class' => 'errorMsg'), $single);
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...
234
        $ifList .= $hc->getSmartyConditions('form', '', '', $single);
235
        $single = $hc->getSmartySingleVar('error');
236
        $strong = $hc->getHtmlTag('strong', array(), $single);
237
        $div = $hc->getHtmlTag('div', array('class' => 'errorMsg'), $strong);
238
        $ifList .= $hc->getSmartyConditions('error', '', '', $div);
239
240
        return $ifList;
241
    }
242
243
    /*
244
    *  @private function getTemplatesAdminPagesFooter
245
    *  @param string $moduleDirname
246
    *  @return string
247
    */
248
    private function getTemplatesAdminPagesFooter($moduleDirname)
249
    {
250
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
251
        $ret = $hc->getHtmlTag('br /', array(), '', false);
252
        $ret .= $hc->getHtmlComment('Footer');
253
        $ret .= $hc->getSmartyIncludeFile($moduleDirname, 'footer', true);
254
255
        return $ret;
256
    }
257
258
    /*
259
    *  @public function render
260
    *  @param null
261
    *  @return bool|string
262
    */
263
    public function render()
264
    {
265
        $module = $this->getModule();
266
        $table = $this->getTable();
267
        $filename = $this->getFileName();
268
        $moduleDirname = $module->getVar('mod_dirname');
269
        $language = $this->getLanguage($moduleDirname, 'AM');
270
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'), 'field_order');
271
        $content = $this->getTemplatesAdminPagesHeader($moduleDirname);
272
        $content .= $this->getTemplatesAdminPages($moduleDirname, $table->getVar('table_name'), $table->getVar('table_solename'), $table->getVar('table_autoincrement'), $fields, $language);
273
        $content .= $this->getTemplatesAdminPagesFooter($moduleDirname);
274
275
        $this->create($moduleDirname, 'templates/admin', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
276
277
        return $this->renderFile();
278
    }
279
}
280