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
2
3
namespace XoopsModules\Tdmcreate\Files\Blocks;
4
5
use XoopsModules\Tdmcreate;
6
use XoopsModules\Tdmcreate\Files;
7
8
9
/*
10
 You may not change or alter any portion of this comment or credits
11
 of supporting developers from this source code or any supporting source code
12
 which is considered copyrighted (c) material of the original comment or credit authors.
13
14
 This program is distributed in the hope that it will be useful,
15
 but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
 */
18
/**
19
 * tdmcreate module.
20
 *
21
 * @copyright       XOOPS Project (https://xoops.org)
22
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
23
 *
24
 * @since           2.5.0
25
 *
26
 * @author          Txmod Xoops http://www.txmodxoops.org
27
 *
28
 */
29
30
/**
31
 * Class BlocksFiles.
32
 */
33
class BlocksFiles extends Files\CreateFile
34
{
35
    /**
36
     * @public function constructor
37
     * @param null
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * @static function getInstance
46
     * @param null
47
     * @return BlocksFiles
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 mixed  $table
63
     * @param        $filename
64
     */
65
    public function write($module, $table, $filename)
66
    {
67
        $this->setModule($module);
68
        $this->setTable($table);
69
        $this->setFileName($filename);
70
    }
71
72
    /**
73
     * @private  function getBlocksShow
74
     * @param     $moduleDirname
75
     * @param     $tableName
76
     * @param     $tableFieldname
77
     * @param     $fields
78
     * @param     $fieldId
79
     * @param int $fieldParent
80
     * @return string
81
     */
82
    private function getBlocksShow($moduleDirname, $tableName, $tableFieldname, $fields, $fieldId, $fieldParent = 0)
83
    {
84
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
85
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
86
        $stuModuleDirname = mb_strtoupper($moduleDirname);
87
        $ucfTableName     = ucfirst($tableName);
88
        $critName         = 'cr' . $ucfTableName;
89
90
        $ret  = $pc->getPhpCodeCommentMultiLine(['Function' => 'show block', '@param  $options' => '', '@return' => 'array']);
91
92
        $func = $pc->getPhpCodeIncludeDir("XOOPS_ROOT_PATH . '/modules/{$moduleDirname}/class/{$tableName}.php'",'',true, true, '', "\t");
93
        $func .= $xc->getXcEqualsOperator('$myts', 'MyTextSanitizer::getInstance()', '',"\t");
94
        $func .= $xc->getXcXoopsTplAssign("{$moduleDirname}_upload_url","{$stuModuleDirname}_UPLOAD_URL",'',"\t");
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type boolean expected by parameter $leftIsString of XoopsModules\Tdmcreate\F...::getXcXoopsTplAssign(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        $func .= $xc->getXcXoopsTplAssign("{$moduleDirname}_upload_url","{$stuModuleDirname}_UPLOAD_URL",/** @scrutinizer ignore-type */ '',"\t");
Loading history...
95
        $func .= $xc->getXcEqualsOperator('$block      ', '[]', '',"\t");
96
        $func .= $xc->getXcEqualsOperator('$typeBlock  ', '$options[0]','',"\t");
97
        $func .= $xc->getXcEqualsOperator('$limit      ', '$options[1]','',"\t");
98
        $func .= $xc->getXcEqualsOperator('$lenghtTitle', '$options[2]','',"\t");
99
        $func .= $xc->getXcEqualsOperator('$helper     ', 'Helper::getInstance()','',"\t");
100
        $func .= $xc->getXcHandlerLine($tableName, "\t");
101
        $func .= $xc->getXcCriteriaCompo($critName, "\t");
102
        $func .= $pc->getPhpCodeArrayShift('$options', "\t");
103
        $func .= $pc->getPhpCodeArrayShift('$options', "\t");
104
        $func .= $pc->getPhpCodeArrayShift('$options', "\t");
105
        $func .= $pc->getPhpCodeBlankLine();
106
107
        //content if: parent
108
        $contIf  = $xc->getXcEqualsOperator("\${$tableName}", "{$moduleDirname}_getMyItemIds('{$moduleDirname}_view', '{$moduleDirname}')", null, "\t");
109
        $crit    = $xc->getXcCriteria('', "'cid'", "'(' . implode(',', \${$tableName}) . ')'", "'IN'", true);
110
        $contIf  .= $xc->getXcCriteriaAdd($critName, $crit, "\t");
111
        $crit    = $xc->getXcCriteria('', "'{$fieldId}'", "{$moduleDirname}_block_addCatSelect(\$options)", "'IN'", true);
112
        $contIf2 = $xc->getXcCriteriaAdd($critName, $crit, "\t\t");
113
        $contIf  .= $pc->getPhpCodeConditions('1 != (count(\$options) && 0 == \$options[0])', null, null, $contIf2, false, "\t");
114
        $crit    = $xc->getXcCriteria('', "'{$fieldId}'", '0', "'!='", true);
115
        $contIf2 = $xc->getXcCriteriaAdd($critName, $crit, "\t\t");
116
        $contIf2 .= $xc->getXcCriteriaSetSort($critName, "'{$fieldId}'", "\t\t");
117
        $contIf2 .= $xc->getXcCriteriaSetOrder($critName, "'ASC'", "\t\t");
118
        $contIf  .= $pc->getPhpCodeConditions('$typeBlock', null, null, $contIf2, false, "\t");
119
120
        //content else: parent
121
        //search for SelectStatus field
122
        $fieldStatus = '';
123
        $critStatus  = '';
124
        foreach ($fields as $field) {
125
            if ($field->getVar('field_element') == 16) {
126
                $fieldStatus = $field->getVar('field_name');
127
            }
128
        }
129
        if ('' !== $fieldStatus) {
130
            $crit = $xc->getXcCriteria('', "'{$fieldStatus}'", 'Constants::PERM_GLOBAL_VIEW', '', true);
131
            $critStatus .= $xc->getXcCriteriaAdd($critName, $crit, "\t\t\t");
132
        }
133
134
        $case1[] = $pc->getPhpCodeCommentLine("For the block: {$tableName} last",'',"\t\t\t");
0 ignored issues
show
Comprehensibility Best Practice introduced by
$case1 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $case1 = array(); before regardless.
Loading history...
135
        if ('' !== $fieldStatus) {
136
            $case1[] = $critStatus;
137
        }
138
        $case1[] = $xc->getXcCriteriaSetSort($critName, "'{$tableFieldname}_date'","\t\t\t");
139
        $case1[] = $xc->getXcCriteriaSetOrder($critName, "'DESC'","\t\t\t");
140
        $case2[] = $pc->getPhpCodeCommentLine("For the block: {$tableName} new",'',"\t\t\t");
0 ignored issues
show
Comprehensibility Best Practice introduced by
$case2 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $case2 = array(); before regardless.
Loading history...
141
        if ('' !== $fieldStatus) {
142
            $case2[] = $critStatus;
143
        }
144
        $crit    = $xc->getXcCriteria('', "'{$tableFieldname}_date'", 'strtotime(date(_SHORTDATESTRING))', "'>='", true);
145
        $case2[] = $xc->getXcCriteriaAdd($critName, $crit,"\t\t\t");
146
        $crit    = $xc->getXcCriteria('', "'{$tableFieldname}_date'", 'strtotime(date(_SHORTDATESTRING))+86400', "'<='", true);
147
        $case2[] = $xc->getXcCriteriaAdd($critName, $crit,"\t\t\t");
148
        $case2[] = $xc->getXcCriteriaSetSort($critName, "'{$tableFieldname}_date'","\t\t\t");
149
        $case2[] = $xc->getXcCriteriaSetOrder($critName, "'ASC'","\t\t\t");
150
        $case3[] = $pc->getPhpCodeCommentLine("For the block: {$tableName} hits",'',"\t\t\t");
0 ignored issues
show
Comprehensibility Best Practice introduced by
$case3 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $case3 = array(); before regardless.
Loading history...
151
        if ('' !== $fieldStatus) {
152
            $case3[] = $critStatus;
153
        }
154
        $case3[] = $xc->getXcCriteriaSetSort($critName, "'{$tableFieldname}_hits'","\t\t\t");
155
        $case3[] = $xc->getXcCriteriaSetOrder($critName, "'DESC'","\t\t\t");
156
        $case4[] = $pc->getPhpCodeCommentLine("For the block: {$tableName} top",'',"\t\t\t");
0 ignored issues
show
Comprehensibility Best Practice introduced by
$case4 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $case4 = array(); before regardless.
Loading history...
157
        if ('' !== $fieldStatus) {
158
            $case4[] = $critStatus;
159
        }
160
        $case4[] = $xc->getXcCriteriaAdd($critName, $crit,"\t\t\t");
161
        $case4[] = $xc->getXcCriteriaSetSort($critName, "'{$tableFieldname}_top'","\t\t\t");
162
        $case4[] = $xc->getXcCriteriaSetOrder($critName, "'ASC'","\t\t\t");
163
        $case5[] = $pc->getPhpCodeCommentLine("For the block: {$tableName} random",'',"\t\t\t");
0 ignored issues
show
Comprehensibility Best Practice introduced by
$case5 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $case5 = array(); before regardless.
Loading history...
164
        if ('' !== $fieldStatus) {
165
            $case5[] = $critStatus;
166
        }
167
        $case5[] = $xc->getXcCriteriaAdd($critName, $crit,"\t\t\t");
168
        $case5[] = $xc->getXcCriteriaSetSort($critName, "'RAND()'","\t\t\t");
169
        $cases  = [
170
            'last'   => $case1,
171
            'new'    => $case2,
172
            'hits'   => $case3,
173
            'top'    => $case4,
174
            'random' => $case5,
175
        ];
176
        $contSwitch = $pc->getPhpCodeCaseSwitch($cases, true, false, "\t\t");
177
        $contElse   = $pc->getPhpCodeSwitch('typeBlock', $contSwitch, "\t");
178
        //end: content else: parent
179
        if (1 == $fieldParent) {
180
            $func .= $contIf;
181
        } else {
182
            $func .= $contElse;
183
        }
184
        $func .= $pc->getPhpCodeBlankLine();
185
186
        $func .= $xc->getXcCriteriaSetLimit($critName, '$limit', "\t");
187
        $func .= $xc->getXcHandlerAllClear("{$tableName}All", $tableName, "\${$critName}", "\t");
188
        $func .= $pc->getPhpCodeUnset($critName, "\t");
189
        $contentForeach = '';
190
        foreach (array_keys($fields) as $f) {
191
            $fieldName = $fields[$f]->getVar('field_name');
192
            // Verify if table_fieldname is not empty
193
            //$lpFieldName = !empty($tableFieldname) ? substr($fieldName, 0, strpos($fieldName, '_')) : $tableName;
194
            $rpFieldName  = $this->getRightString($fieldName);
195
            $fieldElement = $fields[$f]->getVar('field_element');
196
            if (1 == $fields[$f]->getVar('field_block')) {
197
                switch ($fieldElement) {
198
                    case 2:
199
                        $contentForeach .= $xc->getXcEqualsOperator("\$block[\$i]['{$rpFieldName}']", "\$myts->htmlSpecialChars(\${$tableName}All[\$i]->getVar('{$fieldName}'))", null, "\t\t\t");
200
                        break;
201
                    case 3:
202
                    case 4:
203
                        $contentForeach .= $xc->getXcEqualsOperator("\$block[\$i]['{$rpFieldName}']", "strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'))", null, "\t\t\t");
204
                        break;
205
                    case 8:
206
                        $contentForeach .= $xc->getXcEqualsOperator("\$block[\$i]['{$rpFieldName}']", "\XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'))", null, "\t\t\t");
207
                        break;
208
                    case 15:
209
                        $contentForeach .= $xc->getXcEqualsOperator("\$block[\$i]['{$rpFieldName}']","formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'))", null, "\t\t\t");
210
                        break;
211
                    default:
212
                        $contentForeach .= $xc->getXcEqualsOperator("\$block[\$i]['{$rpFieldName}']","\${$tableName}All[\$i]->getVar('{$fieldName}')", null, "\t\t\t");
213
                        break;
214
                }
215
            }
216
        }
217
        $foreach = $pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $contentForeach, "\t\t");
218
219
        $func .= $pc->getPhpCodeConditions("count(\${$tableName}All)", ' > ', '0', $foreach, false, "\t");
220
        $func .= $pc->getPhpCodeBlankLine();
221
        $func .= $this->getSimpleString('return $block;',"\t");
222
        $func .= $pc->getPhpCodeBlankLine();
223
224
        $ret  .= $pc->getPhpCodeFunction("b_{$moduleDirname}_{$tableName}_show", '$options', $func, '', false, "");
225
226
        return $ret;
227
    }
228
229
    /**
230
     * @public function getBlocksEdit
231
     * @param string $moduleDirname
232
     * @param string $tableName
233
     * @param string $fieldId
234
     * @param string $fieldMain
235
     * @param string $language
236
     *
237
     * @return string
238
     */
239
    private function getBlocksEdit($moduleDirname, $tableName, $fieldId, $fieldMain, $language)
240
    {
241
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
242
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
243
        $stuModuleDirname = mb_strtoupper($moduleDirname);
244
        $stuTableName     = mb_strtoupper($tableName);
245
        $ucfTableName     = ucfirst($tableName);
246
        $critName         = 'cr' . $ucfTableName;
247
248
        $ret  = $pc->getPhpCodeCommentMultiLine(['Function' => 'edit block', '@param  $options' => '', '@return' => 'string']);
249
        $func = $pc->getPhpCodeIncludeDir("XOOPS_ROOT_PATH . '/modules/{$moduleDirname}/class/{$tableName}.php'",'',true, true, '', "\t");
250
        $func .= $xc->getXcEqualsOperator('$helper', 'Helper::getInstance()', '',"\t");
251
		$func .= $xc->getXcHandlerLine($tableName, "\t");
252
        $func .= $xc->getXcXoopsTplAssign("{$moduleDirname}_upload_url","{$stuModuleDirname}_UPLOAD_URL",'',"\t");
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type boolean expected by parameter $leftIsString of XoopsModules\Tdmcreate\F...::getXcXoopsTplAssign(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
        $func .= $xc->getXcXoopsTplAssign("{$moduleDirname}_upload_url","{$stuModuleDirname}_UPLOAD_URL",/** @scrutinizer ignore-type */ '',"\t");
Loading history...
253
        $func .= $xc->getXcEqualsOperator('$form', "{$language}DISPLAY", '',"\t");
254
        $func .= $xc->getXcEqualsOperator('$form', "\"<input type='hidden' name='options[0]' value='\".\$options[0].\"' />\"", '.',"\t");
255
        $func .= $xc->getXcEqualsOperator('$form', "\"<input type='text' name='options[1]' size='5' maxlength='255' value='\" . \$options[1] . \"' />&nbsp;<br>\"", '.',"\t");
256
        $func .= $xc->getXcEqualsOperator('$form', "{$language}TITLE_LENGTH . \" : <input type='text' name='options[2]' size='5' maxlength='255' value='\" . \$options[2] . \"' /><br><br>\"", '.',"\t");
257
        $func .= $pc->getPhpCodeArrayShift('$options', "\t");
258
        $func .= $pc->getPhpCodeArrayShift('$options', "\t");
259
        $func .= $pc->getPhpCodeArrayShift('$options', "\t");
260
        $func .= $pc->getPhpCodeBlankLine();
261
        $func .= $xc->getXcCriteriaCompo($critName, "\t");
262
        $crit = $xc->getXcCriteria('', "'{$fieldId}'", '0', "'!='", true);
263
        $func .= $xc->getXcCriteriaAdd($critName, $crit, "\t", "\n");
264
        $func .= $xc->getXcCriteriaSetSort($critName, "'{$fieldId}'","\t","\n");
265
        $func .= $xc->getXcCriteriaSetOrder($critName, "'ASC'","\t","\n");
266
        $func .= $xc->getXcHandlerAllClear("{$tableName}All", $tableName, "\${$critName}", "\t");
267
        $func .= $pc->getPhpCodeUnset($critName, "\t");
268
        $func .= $xc->getXcEqualsOperator('$form', "{$language}{$stuTableName}_TO_DISPLAY . \"<br><select name='options[]' multiple='multiple' size='5'>\"", '.',"\t");
269
        $func .= $xc->getXcEqualsOperator('$form', "\"<option value='0' \" . (in_array(0, \$options) == false ? '' : \"selected='selected'\") . '>' . {$language}ALL_{$stuTableName} . '</option>'", '.',"\t");
270
        $contentForeach = $xc->getXcEqualsOperator("\${$fieldId}", "\${$tableName}All[\$i]->getVar('{$fieldId}')", '',"\t\t");
271
        $contentForeach .= $xc->getXcEqualsOperator('$form', "\"<option value='\" . \${$fieldId} . \"' \" . (in_array(\${$fieldId}, \$options) == false ? '' : \"selected='selected'\") . '>' . \${$tableName}All[\$i]->getVar('{$fieldMain}') . '</option>'", '.',"\t\t");
272
        $func .= $pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $contentForeach, "\t");
273
        $func .= $xc->getXcEqualsOperator('$form', "'</select>'", '.',"\t");
274
        $func .= $pc->getPhpCodeBlankLine();
275
        $func .= $this->getSimpleString('return $form;', "\t");
276
        $func .= $pc->getPhpCodeBlankLine();
277
278
        $ret .= $pc->getPhpCodeFunction("b_{$moduleDirname}_{$tableName}_edit", '$options', $func, '', false, "");
279
280
        return $ret;
281
282
    }
283
284
    /**
285
     * @public function render
286
     * @param null
287
     *
288
     * @return bool|string
289
     */
290
    public function render()
291
    {
292
        $pc             = Tdmcreate\Files\CreatePhpCode::getInstance();
293
        $module         = $this->getModule();
294
        $filename       = $this->getFileName();
295
        $table          = $this->getTable();
296
        $moduleDirname  = $module->getVar('mod_dirname');
297
        $tableName      = $table->getVar('table_name');
298
        $tableFieldname = $table->getVar('table_fieldname');
299
        $language       = $this->getLanguage($moduleDirname, 'MB');
300
        $fields         = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
301
        $fieldId        = null;
302
        $fieldParent    = null;
303
        $fieldMain      = null;
304
        foreach (array_keys($fields) as $f) {
305
            $fieldName   = $fields[$f]->getVar('field_name');
306
            $fieldParent = $fields[$f]->getVar('field_parent');
307
            if (0 == $f) {
308
                $fieldId = $fieldName;
309
            }
310
            if (1 == $fields[$f]->getVar('field_main')) {
311
                $fieldMain = $fieldName;
312
            }
313
        }
314
        $content = $this->getHeaderFilesComments($module);
315
        $content .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', '');
316
        $content .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Helper'], '', '');
317
        $content .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants']);
318
        $content .= $pc->getPhpCodeIncludeDir("XOOPS_ROOT_PATH . '/modules/{$moduleDirname}/include/common.php'",'',true, true);
319
        $content .= $this->getBlocksShow($moduleDirname, $tableName, $tableFieldname, $fields, $fieldId, $fieldParent);
320
        $content .= $this->getBlocksEdit($moduleDirname, $tableName, $fieldId, $fieldMain, $language);
321
322
        $this->create($moduleDirname, 'blocks', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
323
324
        return $this->renderFile();
325
    }
326
}
327