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

TemplatesBlocks::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: TemplatesBlocks.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TemplatesBlocks.
27
 */
28
class TemplatesBlocks 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
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
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 TemplatesBlocks
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
    /**
65
     * @param $module
66
     * @param $table
67
     */
68
    public function write($module, $table, $filename)
69
    {
70
        $this->setModule($module);
71
        $this->setTable($table);
72
		$this->setFileName($filename);
73
    }
74
75
    /*
76
    *  @private function getTemplatesBlocksTableThead
77
    *  @param string $moduleDirname
78
    *  @param string $table
79
    *  @param string $language
80
    */
81
    /**
82
     * @param $moduleDirname
83
     * @param $table
84
     * @param $language
85
     *
86
     * @return string
87
     */
88
    private function getTemplatesBlocksTableThead($tableId, $tableMid, $language)
89
    {
90
        $th = '';
91
		$htmlcode = TDMCreateHtmlSmartyCodes::getInstance();
92
        $fields = $this->getTableFields($tableMid, $tableId);
93
        foreach (array_keys($fields) as $f) {
94
            $fieldName = $fields[$f]->getVar('field_name');
95
            $stuFieldName = strtoupper($fieldName);
96
            $lang = $htmlcode->getSmartyConst($language, $stuFieldName);
97
            $th    .= $htmlcode->getHtmlTableHead($lang, 'center').PHP_EOL;
98
        }
99
        $tr = $htmlcode->getHtmlTableRow($th, 'head').PHP_EOL;
100
101
        return $htmlcode->getHtmlTableThead($tr).PHP_EOL;
102
    }
103
104
    /*
105
    *  @private function getTemplatesBlocksTableTbody
106
    *  @param string $moduleDirname
107
    *  @param string $table
108
    *  @param string $language
109
    */
110
    /**
111
     * @param $moduleDirname
112
     * @param $table
113
     * @param $language
114
     *
115
     * @return string
116
     */
117
    private function getTemplatesBlocksTableTbody($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableAutoincrement, $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...
118
    {
119
        $td = '';
120
		$htmlcode = TDMCreateHtmlSmartyCodes::getInstance();
121
        if (1 == $tableAutoincrement) {
122
            $double = $htmlcode->getSmartyDoubleVar($tableSoleName, 'id');
123
            $td    .= $htmlcode->getHtmlTag('td', array('class' => 'center'), $double).PHP_EOL;
124
        }
125
        $fields = $this->getTableFields($tableMid, $tableId);
126
        foreach (array_keys($fields) as $f) {
127
            $fieldName = $fields[$f]->getVar('field_name');
128
            $fieldElement = $fields[$f]->getVar('field_element');
129
            $rpFieldName = $this->getRightString($fieldName);
130
            if (0 == $f) {
131
                $fieldId = $fieldName;
132
            }
133
            if (1 == $fields[$f]->getVar('field_inlist')) {
134
                switch ($fieldElement) {
135
                    case 9:
136
                        // This is to be reviewed, as it was initially to style = "backgroung-color: #"
137
                        // Now with HTML5 is not supported inline style in the parameters of the HTML tag
138
                        // Old code was <span style="background-color: #<{\$list.{$rpFieldName}}>;">...
139
                        $double = $htmlcode->getSmartyDoubleVar($tableSoleName, $rpFieldName);
140
                        $span = $htmlcode->getHtmlTag('span', array(), $double);
141
                        $td .= $htmlcode->getHtmlTag('td', array('class' => 'center'), $span).PHP_EOL;
142
                        /*$ret .= <<<EOT
143
                    <td class="center"><span style="background-color: #<{\$list.{$rpFieldName}}>;">&nbsp;&nbsp;&nbsp;&nbsp;</span></td>\n
144
EOT;*/
145
                        break;
146
                    case 10:
147
                        $src = $htmlcode->getSmartyNoSimbol('xoModuleIcons32');
148
                        $src .= $htmlcode->getSmartyDoubleVar($tableSoleName, $rpFieldName);
149
                        $img = $htmlcode->getHtmlTag('img', array('src' => $src, 'alt' => $tableName), '', false);
150
                        $td  .= $htmlcode->getHtmlTag('td', array('class' => 'center'), $img).PHP_EOL;
151
                        break;
152
                    case 13:
153
                        $single = $htmlcode->getSmartySingleVar($moduleDirname.'_upload_url');
154
                        $double = $htmlcode->getSmartyDoubleVar($tableSoleName, $rpFieldName);
155
                        $img = $htmlcode->getHtmlTag('img', array('src' => $single."/images/{$tableName}/".$double, 'alt' => $tableName), '', false);
156
                        $td    .= $htmlcode->getHtmlTag('td', array('class' => 'center'), $img).PHP_EOL;
157
                        break;
158
                    default:
159
                        if (0 != $f) {
160
                            $double = $htmlcode->getSmartyDoubleVar($tableSoleName, $rpFieldName);
161
                            $td    .= $htmlcode->getHtmlTag('td', array('class' => 'center'), $double).PHP_EOL;
162
                        }
163
                        break;
164
                }
165
            }
166
        }
167
        $lang = $htmlcode->getSmartyConst('', '_EDIT');
168
        $double = $htmlcode->getSmartyDoubleVar($tableSoleName, 'id');
169
        $src = $htmlcode->getSmartyNoSimbol('xoModuleIcons32 edit.png');
170
        $img = $htmlcode->getHtmlTag('img', array('src' => $src, 'alt' => $tableName), '', false);
171
        $anchor = $htmlcode->getHtmlTag('a', array('href' => $tableName.".php?op=edit&amp;{$fieldId}=".$double, 'title' => $lang), $img).PHP_EOL;
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...
172
        $lang = $htmlcode->getSmartyConst('', '_DELETE');
173
        $double = $htmlcode->getSmartyDoubleVar($tableSoleName, 'id');
174
        $src = $htmlcode->getSmartyNoSimbol('xoModuleIcons32 delete.png');
175
        $img = $htmlcode->getHtmlTag('img', array('src' => $src.$double, 'alt' => $tableName), '', false);
176
        $anchor .= $htmlcode->getHtmlTag('a', array('href' => $tableName.".php?op=delete&amp;{$fieldId}=".$double, 'title' => $lang), $img).PHP_EOL;
177
        $td     .= $htmlcode->getHtmlTag('td', array('class' => 'center'), "\n".$anchor).PHP_EOL;
178
        $cycle = $htmlcode->getSmartyNoSimbol('cycle values="odd, even"');
179
        $tr = $htmlcode->getHtmlTag('tr', array('class' => $cycle), $td).PHP_EOL;
180
        $foreach = $htmlcode->getSmartyForeach($tableSoleName, $tableName.'_list', $tr).PHP_EOL;
181
        $tbody = $htmlcode->getHtmlTag('tbody', array(), $foreach).PHP_EOL;
182
183
        return $htmlcode->getSmartyConditions($tableName.'_count', '', '', $tbody).PHP_EOL;
184
    }
185
186
    /*
187
    *  @private function getTemplatesBlocksTfoot
188
    *  @param string $moduleDirname
189
    *  @param string $table
190
    *  @param string $language
191
    */
192
    /**
193
     * @param $moduleDirname
194
     * @param $table
195
     * @param $language
196
     *
197
     * @return string
198
     */
199
    private function getTemplatesBlocksTableTfoot()
200
    {
201
        $htmlcode = TDMCreateHtmlSmartyCodes::getInstance();
202
		$td = $htmlcode->getHtmlTag('td', array(), '&nbsp;').PHP_EOL;
203
        $tr = $htmlcode->getHtmlTag('tr', array(), $td).PHP_EOL;
204
205
        return $htmlcode->getHtmlTag('tfoot', array(), $tr).PHP_EOL;
206
    }
207
208
    /*
209
    *  @private function getTemplatesBlocksTable
210
    *  @param string $moduleDirname
211
    *  @param string $tableName
212
    *  @param string $fields
0 ignored issues
show
Bug introduced by
There is no parameter named $fields. 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...
213
    *  @param string $language
214
    *  @return string
215
    */
216
    private function getTemplatesBlocksTable($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableAutoincrement, $language)
217
    {
218
        $htmlcode = TDMCreateHtmlSmartyCodes::getInstance();
219
		$tbody = $this->getTemplatesBlocksTableThead($tableId, $tableMid, $language);
220
        $tbody .= $this->getTemplatesBlocksTableTbody($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableAutoincrement, $language);
221
        $tbody .= $this->getTemplatesBlocksTableTfoot();
222
        $single = $htmlcode->getSmartySingleVar('table_type');
223
224
        return $htmlcode->getHtmlTable($tbody, 'table table-'.$single).PHP_EOL;
225
    }
226
227
    /*
228
    *  @public function render
229
    *  @param null
230
    */
231
    /**
232
     * @param null
233
     *
234
     * @return bool|string
235
     */
236
    public function render()
237
    {
238
        $module = $this->getModule();
239
        $table = $this->getTable();
240
        $moduleDirname = $module->getVar('mod_dirname');
241
		$filename = $this->getFileName();
242
        $tableId = $table->getVar('table_id');
243
        $tableMid = $table->getVar('table_mid');
244
        $tableName = $table->getVar('table_name');
245
        $tableSoleName = $table->getVar('table_solename');
246
        $tableAutoincrement = $table->getVar('table_autoincrement');
247
        $language = $this->getLanguage($moduleDirname, 'MB');
248
        $content = $this->getTemplatesBlocksTable($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableAutoincrement, $language);
249
        //
250
        $this->create($moduleDirname, 'templates/blocks', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
251
252
        return $this->renderFile();
253
    }
254
}
255