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

TemplatesBlocks::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 2
b 1
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: 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
{
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
     * @param $filename
68
     */
69
    public function write($module, $table, $filename)
70
    {
71
        $this->setModule($module);
72
        $this->setTable($table);
73
        $this->setFileName($filename);
74
    }
75
76
    /*
77
    *  @private function getTemplatesBlocksTableThead
78
    *  @param string $moduleDirname
79
    *  @param string $table
80
    *  @param string $language
81
    */
82
    /**
83
     * @param $moduleDirname
84
     * @param $table
85
     * @param $language
86
     *
87
     * @return string
88
     */
89
    private function getTemplatesBlocksTableThead($tableId, $tableMid, $language)
90
    {
91
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
92
        $th = '';
93
        $fields = $this->getTableFields($tableMid, $tableId);
94
        foreach (array_keys($fields) as $f) {
95
            $fieldName = $fields[$f]->getVar('field_name');
96
            $stuFieldName = strtoupper($fieldName);
97
            $lang = $hc->getSmartyConst($language, $stuFieldName);
98
            $th    .= $hc->getHtmlTableHead($lang, 'center').PHP_EOL;
99
        }
100
        $tr = $hc->getHtmlTableRow($th, 'head').PHP_EOL;
101
102
        return $hc->getHtmlTableThead($tr).PHP_EOL;
103
    }
104
105
    /*
106
    *  @private function getTemplatesBlocksTableTbody
107
    *  @param string $moduleDirname
108
    *  @param string $table
109
    *  @param string $language
110
    */
111
    /**
112
     * @param $moduleDirname
113
     * @param $table
114
     * @param $language
115
     *
116
     * @return string
117
     */
118
    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...
119
    {
120
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
121
        $td = '';
122
        if (1 == $tableAutoincrement) {
123
            $double = $hc->getSmartyDoubleVar($tableSoleName, 'id');
124
            $td    .= $hc->getHtmlTag('td', array('class' => 'center'), $double).PHP_EOL;
125
        }
126
        $fields = $this->getTableFields($tableMid, $tableId);
127
        foreach (array_keys($fields) as $f) {
128
            $fieldName = $fields[$f]->getVar('field_name');
129
            $fieldElement = $fields[$f]->getVar('field_element');
130
            $rpFieldName = $this->getRightString($fieldName);
131
            if (0 == $f) {
132
                $fieldId = $fieldName;
133
            }
134
            if (1 == $fields[$f]->getVar('field_inlist')) {
135
                switch ($fieldElement) {
136
                    case 9:
137
                        // This is to be reviewed, as it was initially to style = "backgroung-color: #"
138
                        // Now with HTML5 is not supported inline style in the parameters of the HTML tag
139
                        // Old code was <span style="background-color: #<{\$list.{$rpFieldName}}>;">...
140
                        $double = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
141
                        $span = $hc->getHtmlTag('span', array(), $double);
142
                        $td .= $hc->getHtmlTag('td', array('class' => 'center'), $span).PHP_EOL;
143
                        /*$ret .= <<<EOT
144
                    <td class="center"><span style="background-color: #<{\$list.{$rpFieldName}}>;">&nbsp;&nbsp;&nbsp;&nbsp;</span></td>\n
145
EOT;*/
146
                        break;
147
                    case 10:
148
                        $src = $hc->getSmartyNoSimbol('xoModuleIcons32');
149
                        $src .= $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
150
                        $img = $hc->getHtmlTag('img', array('src' => $src, 'alt' => $tableName), '', false);
151
                        $td  .= $hc->getHtmlTag('td', array('class' => 'center'), $img).PHP_EOL;
152
                        break;
153
                    case 13:
154
                        $single = $hc->getSmartySingleVar($moduleDirname.'_upload_url');
155
                        $double = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
156
                        $img = $hc->getHtmlTag('img', array('src' => $single."/images/{$tableName}/".$double, 'alt' => $tableName), '', false);
157
                        $td    .= $hc->getHtmlTag('td', array('class' => 'center'), $img).PHP_EOL;
158
                        break;
159
                    default:
160
                        if (0 != $f) {
161
                            $double = $hc->getSmartyDoubleVar($tableSoleName, $rpFieldName);
162
                            $td    .= $hc->getHtmlTag('td', array('class' => 'center'), $double).PHP_EOL;
163
                        }
164
                        break;
165
                }
166
            }
167
        }
168
        $lang = $hc->getSmartyConst('', '_EDIT');
169
        $double = $hc->getSmartyDoubleVar($tableSoleName, 'id');
170
        $src = $hc->getSmartyNoSimbol('xoModuleIcons32 edit.png');
171
        $img = $hc->getHtmlTag('img', array('src' => $src, 'alt' => $tableName), '', false);
172
        $anchor = $hc->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...
173
        $lang = $hc->getSmartyConst('', '_DELETE');
174
        $double = $hc->getSmartyDoubleVar($tableSoleName, 'id');
175
        $src = $hc->getSmartyNoSimbol('xoModuleIcons32 delete.png');
176
        $img = $hc->getHtmlTag('img', array('src' => $src.$double, 'alt' => $tableName), '', false);
177
        $anchor .= $hc->getHtmlTag('a', array('href' => $tableName.".php?op=delete&amp;{$fieldId}=".$double, 'title' => $lang), $img).PHP_EOL;
178
        $td     .= $hc->getHtmlTag('td', array('class' => 'center'), "\n".$anchor).PHP_EOL;
179
        $cycle = $hc->getSmartyNoSimbol('cycle values="odd, even"');
180
        $tr = $hc->getHtmlTag('tr', array('class' => $cycle), $td).PHP_EOL;
181
        $foreach = $hc->getSmartyForeach($tableSoleName, $tableName.'_list', $tr).PHP_EOL;
182
        $tbody = $hc->getHtmlTag('tbody', array(), $foreach).PHP_EOL;
183
184
        return $hc->getSmartyConditions($tableName.'_count', '', '', $tbody).PHP_EOL;
185
    }
186
187
    /*
188
    *  @private function getTemplatesBlocksTfoot
189
    *  @param string $moduleDirname
190
    *  @param string $table
191
    *  @param string $language
192
    */
193
    /**
194
     * @param $moduleDirname
195
     * @param $table
196
     * @param $language
197
     *
198
     * @return string
199
     */
200
    private function getTemplatesBlocksTableTfoot()
201
    {
202
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
203
        $td = $hc->getHtmlTag('td', array(), '&nbsp;').PHP_EOL;
204
        $tr = $hc->getHtmlTag('tr', array(), $td).PHP_EOL;
205
206
        return $hc->getHtmlTag('tfoot', array(), $tr).PHP_EOL;
207
    }
208
209
    /*
210
    *  @private function getTemplatesBlocksTable
211
    *  @param string $moduleDirname
212
    *  @param string $tableName
213
    *  @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...
214
    *  @param string $language
215
    *  @return string
216
    */
217
    private function getTemplatesBlocksTable($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableAutoincrement, $language)
218
    {
219
        $hc = TDMCreateHtmlSmartyCodes::getInstance();
220
        $tbody = $this->getTemplatesBlocksTableThead($tableId, $tableMid, $language);
221
        $tbody .= $this->getTemplatesBlocksTableTbody($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableAutoincrement, $language);
222
        $tbody .= $this->getTemplatesBlocksTableTfoot();
223
        $single = $hc->getSmartySingleVar('table_type');
224
225
        return $hc->getHtmlTable($tbody, 'table table-'.$single).PHP_EOL;
226
    }
227
228
    /*
229
    *  @public function render
230
    *  @param null
231
    */
232
    /**
233
     * @param null
234
     *
235
     * @return bool|string
236
     */
237
    public function render()
238
    {
239
        $module = $this->getModule();
240
        $table = $this->getTable();
241
        $filename = $this->getFileName();
242
        $moduleDirname = $module->getVar('mod_dirname');
243
        $tableId = $table->getVar('table_id');
244
        $tableMid = $table->getVar('table_mid');
245
        $tableName = $table->getVar('table_name');
246
        $tableSoleName = $table->getVar('table_solename');
247
        $tableAutoincrement = $table->getVar('table_autoincrement');
248
        $language = $this->getLanguage($moduleDirname, 'MB');
249
        $content = $this->getTemplatesBlocksTable($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableAutoincrement, $language);
250
        //
251
        $this->create($moduleDirname, 'templates/blocks', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
252
253
        return $this->renderFile();
254
    }
255
}
256