BlocksFiles::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Tdmcreate\Files\Blocks;
2
3
use XoopsModules\Tdmcreate\Files;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
/**
15
 * tdmcreate module.
16
 *
17
 * @copyright       XOOPS Project (https://xoops.org)
18
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
19
 *
20
 * @since           2.5.0
21
 *
22
 * @author          Txmod Xoops http://www.txmodxoops.org
23
 *
24
 * @version         $Id: blocks_pages.php 12258 2014-01-02 09:33:29Z timgno $
25
 */
26
27
/**
28
 * Class BlocksFiles.
29
 */
30
class BlocksFiles extends Files\CreateFile
31
{
32
    /**
33
     *  @public function constructor
34
     *  @param null
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     *  @static function getInstance
43
     *  @param null
44
     * @return BlocksFiles
45
     */
46
    public static function getInstance()
47
    {
48
        static $instance = false;
49
        if (!$instance) {
50
            $instance = new self();
51
        }
52
53
        return $instance;
54
    }
55
56
    /**
57
     * @public function write
58
     * @param string $module
59
     * @param mixed  $table
60
     * @param        $filename
61
     */
62
    public function write($module, $table, $filename)
63
    {
64
        $this->setModule($module);
65
        $this->setTable($table);
66
        $this->setFileName($filename);
67
    }
68
69
    /**
70
     * @private  function getBlocksShow
71
     * @param     $moduleDirname
72
     * @param     $tableName
73
     * @param     $tableFieldname
74
     * @param     $fields
75
     * @param     $fieldId
76
     * @param int $fieldParent
77
     * @return string
78
     */
79
    private function getBlocksShow($moduleDirname, $tableName, $tableFieldname, $fields, $fieldId, $fieldParent = 0)
80
    {
81
        $stuModuleDirname = mb_strtoupper($moduleDirname);
82
        $ucfModuleDirname = ucfirst($moduleDirname);
83
        $ret = <<<EOT
84
include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/include/common.php';
85
// Function show block
86
function b_{$moduleDirname}_{$tableName}_show(\$options)
87
{
88
    include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/class/{$tableName}.php';
89
    \$myts = MyTextSanitizer::getInstance();
90
    \$GLOBALS['xoopsTpl']->assign('{$moduleDirname}_upload_url', {$stuModuleDirname}_UPLOAD_URL);
91
    \$block       = array();
92
    \$typeBlock   = \$options[0];
93
    \$limit       = \$options[1];
94
    \$lenghtTitle = \$options[2];
95
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
96
    \${$tableName}Handler = \${$moduleDirname}->getHandler('{$tableName}');
97
    \$criteria = new \CriteriaCompo();
98
    array_shift(\$options);
99
    array_shift(\$options);
100
    array_shift(\$options);\n
101
EOT;
102
        if (1 == $fieldParent) {
103
            $ret .= <<<EOT
104
	\${$tableName} = {$moduleDirname}_getMyItemIds('{$moduleDirname}_view', '{$moduleDirname}');
105
    \$criteria->add(new \Criteria('cid', '(' . implode(',', \${$tableName}) . ')','IN'));
106
    if (1 != (count(\$options) && 0 == \$options[0])) {
107
        \$criteria->add(new \Criteria('{$fieldId}', {$moduleDirname}_block_addCatSelect(\$options), 'IN'));
108
    }
109
110
    if (\$typeBlock)
111
    {
112
        \$criteria->add(new \Criteria('{$fieldId}', 0, '!='));
113
        \$criteria->setSort('{$fieldId}');
114
        \$criteria->setOrder('ASC');
115
    }\n
116
EOT;
117
        } else {
118
            $ret .= <<<EOT
119
	switch(\$typeBlock)
120
	{
121
		// For the block: {$tableName} last
122
		case 'last':
123
			\$criteria->add(new \Criteria('{$tableFieldname}_display', 1));
124
			\$criteria->setSort('{$tableFieldname}_created');
125
			\$criteria->setOrder('DESC');
126
		break;
127
		// For the block: {$tableName} new
128
		case 'new':
129
			\$criteria->add(new \Criteria('{$tableFieldname}_display', 1));
130
			\$criteria->add(new \Criteria('{$tableFieldname}_created', strtotime(date(_SHORTDATESTRING)), '>='));
131
			\$criteria->add(new \Criteria('{$tableFieldname}_created', strtotime(date(_SHORTDATESTRING))+86400, '<='));
132
			\$criteria->setSort('{$tableFieldname}_created');
133
			\$criteria->setOrder('ASC');
134
		break;
135
		// For the block: {$tableName} hits
136
		case 'hits':
137
            \$criteria->setSort('{$tableFieldname}_hits');
138
            \$criteria->setOrder('DESC');
139
        break;
140
		// For the block: {$tableName} top
141
		case 'top':
142
            \$criteria->setSort('{$tableFieldname}_top');
143
            \$criteria->setOrder('ASC');
144
        break;
145
		// For the block: {$tableName} random
146
		case 'random':
147
			\$criteria->add(new \Criteria('{$tableFieldname}_display', 1));
148
			\$criteria->setSort('RAND()');
149
		break;
150
	}\n
151
EOT;
152
        }
153
        $ret .= <<<EOT
154
    \$criteria->setLimit(\$limit);
155
    \${$tableName}All = \${$tableName}Handler->getAll(\$criteria);
156
	unset(\$criteria);
157
    foreach(array_keys(\${$tableName}All) as \$i)
158
    {\n
159
EOT;
160
        foreach (array_keys($fields) as $f) {
161
            $fieldName = $fields[$f]->getVar('field_name');
162
            // Verify if table_fieldname is not empty
163
            //$lpFieldName = !empty($tableFieldname) ? substr($fieldName, 0, strpos($fieldName, '_')) : $tableName;
164
            $rpFieldName = $this->getRightString($fieldName);
165
            $fieldElement = $fields[$f]->getVar('field_element');
166
            if (1 == $fields[$f]->getVar('field_block')) {
167
                switch ($fieldElement) {
168
                    case 2:
169
                        $ret .= <<<EOT
170
        \$block[\$i]['{$rpFieldName}'] = \$myts->htmlSpecialChars(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
171
EOT;
172
                        break;
173
                    case 3:
174
                    case 4:
175
                        $ret .= <<<EOT
176
		\$block[\$i]['{$rpFieldName}'] = strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
177
EOT;
178
                        break;
179
                    case 8:
180
                    $ret .= <<<EOT
181
		\$block[\$i]['{$rpFieldName}'] = \XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
182
EOT;
183
                        break;
184
                    case 15:
185
                    $ret .= <<<EOT
186
		\$block[\$i]['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n
187
EOT;
188
                        break;
189
                    default:
190
                        $ret .= <<<EOT
191
        \$block[\$i]['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n
192
EOT;
193
                        break;
194
                }
195
            }
196
        }
197
        $ret .= <<<EOT
198
    }
199
    return \$block;
200
}\n\n
201
EOT;
202
203
        return $ret;
204
    }
205
206
    /**
207
     *  @public function getBlocksEdit
208
     *  @param string $moduleDirname
209
     *  @param string $tableName
210
     *  @param string $fieldId
211
     *  @param string $fieldMain
212
     *  @param string $language
213
     *
214
     * @return string
215
     */
216
    private function getBlocksEdit($moduleDirname, $tableName, $fieldId, $fieldMain, $language)
217
    {
218
        $stuModuleDirname = mb_strtoupper($moduleDirname);
219
        $stuTableName = mb_strtoupper($tableName);
220
        $ucfModuleDirname = ucfirst($moduleDirname);
221
        $ret = <<<EOT
222
// Function edit block
223
function b_{$moduleDirname}_{$tableName}_edit(\$options)
224
{
225
    include_once XOOPS_ROOT_PATH.'/modules/{$moduleDirname}/class/{$tableName}.php';
226
    \${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
227
    \${$tableName}Handler = \${$moduleDirname}->getHandler('{$tableName}');
228
    \$GLOBALS['xoopsTpl']->assign('{$moduleDirname}_upload_url', {$stuModuleDirname}_UPLOAD_URL);
229
    \$form  = {$language}DISPLAY;
230
    \$form .= "<input type='hidden' name='options[0]' value='".\$options[0]."' />";
231
    \$form .= "<input type='text' name='options[1]' size='5' maxlength='255' value='" . \$options[1] . "' />&nbsp;<br>";
232
    \$form .= {$language}TITLE_LENGTH." : <input type='text' name='options[2]' size='5' maxlength='255' value='" . \$options[2] . "' /><br><br>";
233
    array_shift(\$options);
234
    array_shift(\$options);
235
    array_shift(\$options);
236
    \$criteria = new \CriteriaCompo();
237
    \$criteria->add(new \Criteria('{$fieldId}', 0, '!='));
238
    \$criteria->setSort('{$fieldId}');
239
    \$criteria->setOrder('ASC');
240
    \${$tableName}All = \${$tableName}Handler->getAll(\$criteria);
241
    unset(\$criteria);
242
    \$form .= {$language}{$stuTableName}_TO_DISPLAY."<br><select name='options[]' multiple='multiple' size='5'>";
243
    \$form .= "<option value='0' " . (in_array(0, \$options) === false ? '' : "selected='selected'") . '>' . {$language}ALL_{$stuTableName} . '</option>';
244
    foreach (array_keys(\${$tableName}All) as \$i) {
245
        \${$fieldId} = \${$tableName}All[\$i]->getVar('{$fieldId}');
246
        \$form .= "<option value='" . \${$fieldId} . "' " . (in_array(\${$fieldId}, \$options) === false ? '' : "selected='selected'") . '>' . \${$tableName}All[\$i]->getVar('{$fieldMain}') . '</option>';
247
    }
248
    \$form .= '</select>';
249
    return \$form;
250
}
251
EOT;
252
253
        return $ret;
254
    }
255
256
    /**
257
     *  @public function render
258
     *  @param null
259
     *
260
     * @return bool|string
261
     */
262
    public function render()
263
    {
264
        $module = $this->getModule();
265
        $filename = $this->getFileName();
266
        $table = $this->getTable();
267
        $moduleDirname = $module->getVar('mod_dirname');
268
        $tableName = $table->getVar('table_name');
269
        $tableFieldname = $table->getVar('table_fieldname');
270
        $tableCategory = $table->getVar('table_category');
0 ignored issues
show
Unused Code introduced by
The assignment to $tableCategory is dead and can be removed.
Loading history...
271
        $language = $this->getLanguage($moduleDirname, 'MB');
272
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
273
        foreach (array_keys($fields) as $f) {
274
            $fieldName = $fields[$f]->getVar('field_name');
275
            $fieldParent = $fields[$f]->getVar('field_parent');
276
            if (0 == $f) {
277
                $fieldId = $fieldName;
278
            }
279
            if (1 == $fields[$f]->getVar('field_main')) {
280
                $fieldMain = $fieldName;
281
            }
282
        }
283
        $content = $this->getHeaderFilesComments($module, $filename);
284
        $content .= $this->getBlocksShow($moduleDirname, $tableName, $tableFieldname, $fields, $fieldId, $fieldParent);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldParent seems to be defined by a foreach iteration on line 273. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
Comprehensibility Best Practice introduced by
The variable $fieldId does not seem to be defined for all execution paths leading up to this point.
Loading history...
285
        $content .= $this->getBlocksEdit($moduleDirname, $tableName, $fieldId, $fieldMain, $language);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldMain does not seem to be defined for all execution paths leading up to this point.
Loading history...
286
287
        $this->create($moduleDirname, 'blocks', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
288
289
        return $this->renderFile();
290
    }
291
}
292