Passed
Push — master ( 0f0f99...fb91d6 )
by Michael
17s
created

TemplatesUserPrint::renderFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4285
cc 2
eloc 14
nc 2
nop 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: TemplatesUserPrint.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class TemplatesUserPrint.
27
 */
28
class TemplatesUserPrint 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
    * @var string
32
    */
33
    private $tdmcfile = null;
34
35
    /*
36
    *  @public function constructor
37
    *  @param null
38
    */
39
    /**
40
     *
41
     */
42
    public function __construct()
43
    {
44
        parent::__construct();
45
        $this->tdmcfile = TDMCreateFile::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateFile::getInstance() of type object<TDMCreateFile> is incompatible with the declared type string of property $tdmcfile.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
    }
47
48
    /*
49
    *  @static function &getInstance
50
    *  @param null
51
    */
52
    /**
53
     * @return TemplatesUserPrint
54
     */
55
    public static function &getInstance()
56
    {
57
        static $instance = false;
58
        if (!$instance) {
59
            $instance = new self();
60
        }
61
62
        return $instance;
63
    }
64
65
    /*
66
    *  @public function write
67
    *  @param string $module
68
    *  @param string $table
69
    *  @param string $filename
70
    */
71
    /**
72
     * @param $module
73
     * @param $table
74
     */
75
    public function write($module, $table)
76
    {
77
        $this->setModule($module);
78
        $this->setTable($table);
79
    }
80
81
    /*
82
    *  @private function getTemplatesUserPrintHeader
83
    *  @param string $moduleDirname
84
    *  @param string $table
85
    *  @param string $language
86
    */
87
    /**
88
     * @param $moduleDirname
89
     * @param $table
90
     * @param $language
91
     *
92
     * @return string
93
     */
94
    private function getTemplatesUserPrintHeader($moduleDirname, $table, $language)
95
    {
96
        $ret = <<<EOT
97
<{include file="db:{$moduleDirname}_header.tpl"}>
98
<table class="{$moduleDirname}">
99
    <thead class="outer">
100
        <tr class="head">\n
101
EOT;
102
        $fields = $this->tdmcfile->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
0 ignored issues
show
Bug introduced by
The method getTableFields cannot be called on $this->tdmcfile (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...
103
        foreach (array_keys($fields) as $f) {
104
            $fieldName = $fields[$f]->getVar('field_name');
105
            $langStuFieldName = $language.strtoupper($fieldName);
106
            if ((1 == $table->getVar('table_autoincrement')) || (1 == $fields[$f]->getVar('field_user'))) {
107
                $ret .= <<<EOT
108
            <th class="center"><{\$smarty.const.{$langStuFieldName}}></th>\n
109
EOT;
110
            }
111
        }
112
        $ret .= <<<EOT
113
        </tr>
114
    </thead>\n
115
EOT;
116
117
        return $ret;
118
    }
119
120
    /*
121
    *  @private function getTemplatesUserPrintBody
122
    *  @param string $moduleDirname
123
    *  @param string $table
124
    *  @param string $language
125
    */
126
    /**
127
     * @param $moduleDirname
128
     * @param $table
129
     * @param $language
130
     *
131
     * @return string
132
     */
133
    private function getTemplatesUserPrintBody($moduleDirname, $table, $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...
134
    {
135
        $tableName = $table->getVar('table_name');
136
        $ret = <<<EOT
137
    <tbody>
138
        <{foreach item=list from=\${$tableName}}>
139
            <tr class="<{cycle values='odd, even'}>">\n
140
EOT;
141
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
142
        foreach (array_keys($fields) as $f) {
143
            $fieldName = $fields[$f]->getVar('field_name');
144
            $fieldElement = $fields[$f]->getVar('field_element');
145
            $rpFieldName = $this->tdmcfile->getRightString($fieldName);
0 ignored issues
show
Bug introduced by
The method getRightString cannot be called on $this->tdmcfile (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...
146
            if ((1 == $table->getVar('table_autoincrement')) || (1 == $fields[$f]->getVar('field_user'))) {
147
                switch ($fieldElement) {
148
                    case 9:
149
                        $ret .= <<<EOT
150
                <td class="center"><span style="background-color: #<{\$list.{$rpFieldName}}>;">\t\t</span></td>\n
151
EOT;
152
                        break;
153
                    case 10:
154
                        $ret .= <<<EOT
155
                <td class="center"><img src="<{xoModuleIcons32}><{\$list.{$rpFieldName}}>" alt="{$tableName}"></td>\n
156
EOT;
157
                        break;
158
                    case 13:
159
                        $ret .= <<<EOT
160
                <td class="center"><img src="<{\${$moduleDirname}_upload_url}>/images/{$tableName}/<{\$list.{$rpFieldName}}>" alt="{$tableName}"></td>\n
161
EOT;
162
                        break;
163
                    default:
164
                        $ret .= <<<EOT
165
                <td class="center"><{\$list.{$rpFieldName}}></td>\n
166
EOT;
167
                        break;
168
                }
169
            }
170
        }
171
        $ret .= <<<EOT
172
            </tr>
173
        <{/foreach}>
174
    </tbody>
175
</table>\n
176
EOT;
177
178
        return $ret;
179
    }
180
181
    /*
182
    *  @private function getTemplatesUserPrintBodyFieldnameEmpty
183
    *  @param string $moduleDirname
184
    *  @param string $table
185
    *  @param string $language
186
    */
187
    /**
188
     * @param $moduleDirname
189
     * @param $table
190
     * @param $language
191
     *
192
     * @return string
193
     */
194
    private function getTemplatesUserPrintBodyFieldnameEmpty($moduleDirname, $table, $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...
195
    {
196
        $tableName = $table->getVar('table_name');
197
        $ret = <<<EOT
198
    <tbody>
199
        <{foreach item=list from=\${$tableName}}>
200
            <tr class="<{cycle values='odd, even'}>">\n
201
EOT;
202
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
203
        foreach (array_keys($fields) as $f) {
204
            $fieldName = $fields[$f]->getVar('field_name');
205
            $fieldElement = $fields[$f]->getVar('field_element');
206
            if ((1 == $table->getVar('table_autoincrement')) || (1 == $fields[$f]->getVar('field_user'))) {
207
                switch ($fieldElement) {
208
                    case 9:
209
                        $ret .= <<<EOT
210
            <td class="center"><span style="background-color: #<{\$list.{$fieldName}}>;"></span></td>\n
211
EOT;
212
                        break;
213
                    case 13:
214
                        $ret .= <<<EOT
215
            <td class="center"><img src="<{\${$moduleDirname}_upload_url}>/images/{$tableName}/<{\$list.{$fieldName}}>" alt="{$tableName}"></td>\n
216
EOT;
217
                        break;
218
                    default:
219
                        $ret .= <<<EOT
220
            <td class="center"><{\$list.{$fieldName}}></td>\n
221
EOT;
222
                        break;
223
                }
224
            }
225
        }
226
        $ret .= <<<EOT
227
            </tr>
228
        <{/foreach}>
229
    </tbody>
230
</table>\n
231
EOT;
232
233
        return $ret;
234
    }
235
236
    /*
237
    *  @private function getTemplatesUserPrintFooter
238
    *  @param string $moduleDirname
239
    */
240
    /**
241
     * @param $moduleDirname
242
     *
243
     * @return string
244
     */
245
    private function getTemplatesUserPrintFooter($moduleDirname)
246
    {
247
        $ret = <<<EOT
248
<{include file="db:{$moduleDirname}_footer.tpl"}>
249
EOT;
250
251
        return $ret;
252
    }
253
254
    /*
255
    *  @public function renderFile
256
    *  @param $filename
257
    */
258
    /**
259
     * @param $filename
260
     *
261
     * @return bool|string
262
     */
263
    public function renderFile($filename)
264
    {
265
        $module = $this->getModule();
266
        $table = $this->getTable();
267
        $moduleDirname = $module->getVar('mod_dirname');
268
        $tableFieldname = $table->getVar('table_fieldname');
269
        $language = $this->getLanguage($moduleDirname, 'MA');
270
        $content = $this->getTemplatesUserPrintHeader($moduleDirname, $table, $language);
271
        // Verify if table_fieldname is not empty
272
        if (!empty($tableFieldname)) {
273
            $content .= $this->getTemplatesUserPrintBody($moduleDirname, $table, $language);
274
        } else {
275
            $content .= $this->getTemplatesUserPrintBodyFieldnameEmpty($moduleDirname, $table, $language);
276
        }
277
        $content .= $this->getTemplatesUserPrintFooter($moduleDirname);
278
        //
279
        $this->tdmcfile->create($moduleDirname, 'templates', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
0 ignored issues
show
Bug introduced by
The method create cannot be called on $this->tdmcfile (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...
280
281
        return $this->tdmcfile->renderFile();
0 ignored issues
show
Bug introduced by
The method renderFile cannot be called on $this->tdmcfile (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...
282
    }
283
}
284