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

TemplatesUserPrint::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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