Passed
Push — master ( 8c6869...45559c )
by Goffy
03:18
created

CheckData::getCheckReads()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 8
nop 0
dl 0
loc 22
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files;
4
5
use XoopsModules\Modulebuilder;
6
use XoopsModules\Modulebuilder\Constants;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
/**
18
 * modulebuilder module.
19
 *
20
 * @copyright       XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.0
24
 *
25
 * @author          Txmod Xoops http://www.txmodxoops.org
26
 *
27
 */
28
29
//include \dirname(__DIR__) . '/autoload.php';
30
31
/**
32
 * Class CheckData.
33
 */
34
class CheckData
35
{
36
    /**
37
     * @var mixed
38
     */
39
    private $cf = null;
40
41
    /**
42
     * @var mixed
43
     */
44
    private $modId = null;
45
46
    /**
47
     * @var mixed
48
     */
49
    private $tables = null;
50
51
    /**
52
     * @var mixed
53
     */
54
    private $infos = [];
55
56
57
58
    /**
59
     * @public function constructor
60
     * @param null
61
     */
62
    public function __construct()
63
    {
64
        $this->cf = Modulebuilder\Files\CreateFile::getInstance();
65
    }
66
67
    /**
68
     * @static function getInstance
69
     *
70
     * @param null
71
     *
72
     * @return Modulebuilder\Files\CheckData
73
     */
74
    public static function getInstance()
75
    {
76
        static $instance = false;
77
        if (!$instance) {
78
            $instance = new self();
79
        }
80
81
        return $instance;
82
    }
83
84
    /**
85
     * @public function getCheckResult
86
     *
87
     * @param $module
88
     * @return array
89
     */
90
    public function getCheckPreBuilding($module)
91
    {
92
        $this->modId  = $module->getVar('mod_id');
93
        $this->tables = $this->cf->getTableTables($this->modId);
94
        $this->infos = [];
95
96
        $this->getCheckBlock();
97
        $this->getCheckBroken();
98
        $this->getCheckComments();
99
        $this->getCheckUserpage();
100
        $this->getCheckRating();
101
        $this->getCheckReads();
102
        $this->getCheckSQL();
103
104
        return $this->infos;
105
    }
106
107
    /**
108
     * @public function getCheckBroken
109
     *
110
     * @return array|bool
111
     */
112
    private function getCheckBroken()
113
    {
114
        foreach (\array_keys($this->tables) as $t) {
115
            if (1 == $this->tables[$t]->getVar('table_broken')) {
116
                $tableId = $this->tables[$t]->getVar('table_id');
117
                $tableName = $this->tables[$t]->getVar('table_name');
118
                $fields = $this->cf->getTableFields($this->modId, $tableId);
119
                $fieldSatus = '';
120
121
                foreach (\array_keys($fields) as $f) {
122
                    $fieldName = $fields[$f]->getVar('field_name');
123
                    if (Constants::FIELD_ELE_SELECTSTATUS == $fields[$f]->getVar('field_element')) {
124
                        $fieldSatus = $fieldName;
125
                    }
126
                }
127
                // check whether each table with handling "broken" has also a field "status"
128
                if ('' == $fieldSatus) {
129
                    $info = \str_replace('%t', $tableName, _AM_MODULEBUILDER_BUILDING_CHECK_BROKEN1);
130
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
131
                }
132
            }
133
        }
134
135
        return true;
136
    }
137
138
    /**
139
     * @private function getCheckUserpage
140
     *
141
     * @return array|bool
142
     */
143
    private function getCheckUserpage()
144
    {
145
        //check field params: minimum one param is selected
146
        foreach (\array_keys($this->tables) as $t) {
147
            $tableId = $this->tables[$t]->getVar('table_id');
148
            $tableName = $this->tables[$t]->getVar('table_name');
149
            $fields = $this->cf->getTableFields($this->modId, $tableId);
150
151
            foreach (\array_keys($fields) as $f) {
152
                $fieldName = $fields[$f]->getVar('field_name');
153
                // check fields for parameters
154
                if ($f > 0) {
155
                    $fieldParams = (int)$fields[$f]->getVar('field_parent') + (int)$fields[$f]->getVar('field_admin') + (int)$fields[$f]->getVar('field_inlist') + (int)$fields[$f]->getVar('field_inform')
156
                        + (int)$fields[$f]->getVar('field_user') + (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot')
157
                        + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot') + (int)$fields[$f]->getVar('field_block')
158
                        + (int)$fields[$f]->getVar('field_main') + (int)$fields[$f]->getVar('field_search') + (int)$fields[$f]->getVar('field_required');
159
                    if (0 == $fieldParams) {
160
                        $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS1);
161
                        $this->infos[] = ['icon' => 'error', 'info' => $info];
162
                    }
163
                }
164
            }
165
        }
166
167
        //check field params: user file no usage in index or item
168
        foreach (\array_keys($this->tables) as $t) {
169
            $tableId = $this->tables[$t]->getVar('table_id');
170
            $tableName = $this->tables[$t]->getVar('table_name');
171
            $fields = $this->cf->getTableFields($this->modId, $tableId);
172
173
            foreach (\array_keys($fields) as $f) {
174
                $fieldName = $fields[$f]->getVar('field_name');
175
                if (1 == $fields[$f]->getVar('field_user')) {
176
                    // check fields for parameters
177
                    if ($f > 0) {
178
                        $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot')
179
                            + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot');
180
                        if (0 == $fieldParams) {
181
                            $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS2);
182
                            $this->infos[] = ['icon' => 'warning', 'info' => $info];
183
                        }
184
                    }
185
                }
186
            }
187
        }
188
        //check field params:  user file index multiple usage
189
        //check field params:  user file item multiple usage
190
        foreach (\array_keys($this->tables) as $t) {
191
            $tableId = $this->tables[$t]->getVar('table_id');
192
            $tableName = $this->tables[$t]->getVar('table_name');
193
            $fields = $this->cf->getTableFields($this->modId, $tableId);
194
195
            foreach (\array_keys($fields) as $f) {
196
                $fieldName = $fields[$f]->getVar('field_name');
197
                if (1 == $fields[$f]->getVar('field_user')) {
198
                    // check fields for parameters
199
                    if ($f > 0) {
200
                        $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot');
201
                        if ($fieldParams > 1) {
202
                            $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS3);
203
                            $this->infos[] = ['icon' => 'warning', 'info' => $info];
204
                        }
205
                        $fieldParams = (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot');
206
                        if ($fieldParams > 1) {
207
                            $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS3);
208
                            $this->infos[] = ['icon' => 'warning', 'info' => $info];
209
                        }
210
                    }
211
                }
212
            }
213
        }
214
215
        //check table params: user submit or rate or broken, but table not for user side
216
        foreach (\array_keys($this->tables) as $t) {
217
            $tableName = $this->tables[$t]->getVar('table_name');
218
            if ((0 == $this->tables[$t]->getVar('table_user')) && (1 == $this->tables[$t]->getVar('table_submit') || 1 == $this->tables[$t]->getVar('table_broken') || 1 == $this->tables[$t]->getVar('table_rate'))) {
219
                $info = \str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_USERPAGE1);
220
                $this->infos[] = ['icon' => 'error', 'info' => $info];
221
            }
222
        }
223
224
        //check field/table params:  params for index file, but table param table_index = 0
225
        //check field/table params:  params for user file, but table param table_user = 0
226
        foreach (\array_keys($this->tables) as $t) {
227
            $tableId = $this->tables[$t]->getVar('table_id');
228
            $tableName = $this->tables[$t]->getVar('table_name');
229
            $tableIndex = (int)$this->tables[$t]->getVar('table_index');
230
            $tableUser = (int)$this->tables[$t]->getVar('table_user');
231
            $fields = $this->cf->getTableFields($this->modId, $tableId);
232
233
            $fieldParamsIndex = 0;
234
            $fieldParamsUser = 0;
235
            foreach (\array_keys($fields) as $f) {
236
                $fieldName = $fields[$f]->getVar('field_name');
237
                if (1 == $fields[$f]->getVar('field_user')) {
238
                    // check fields for parameters
239
                    if ($f > 0) {
240
                        $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot');
241
                        $fieldParamsIndex += $fieldParams;
242
                        if ($fieldParams >= 1 && 0 == $tableIndex) {
243
                            $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS5);
244
                            $this->infos[] = ['icon' => 'warning', 'info' => $info];
245
                        }
246
                        $fieldParams = (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot');
247
                        $fieldParamsUser += $fieldParams;
248
                        if ($fieldParams >= 1 && 0 == $tableUser) {
249
                            $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS7);
250
                            $this->infos[] = ['icon' => 'warning', 'info' => $info];
251
                        }
252
                    }
253
                }
254
            }
255
            if (0 == $fieldParamsIndex && 1 == $tableIndex) {
256
                $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS4);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldName does not seem to be defined for all execution paths leading up to this point.
Loading history...
257
                $this->infos[] = ['icon' => 'warning', 'info' => $info];
258
            }
259
            if (0 == $fieldParamsUser && 1 == $tableUser) {
260
                $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS6);
261
                $this->infos[] = ['icon' => 'warning', 'info' => $info];
262
            }
263
        }
264
        
265
        return true;
266
    }
267
268
    /**
269
     * @private function getCheckBlock
270
     *
271
     * @return array|bool
272
     */
273
    private function getCheckBlock()
274
    {
275
        //use in block but no field selected
276
        foreach (\array_keys($this->tables) as $t) {
277
            $tableId = $this->tables[$t]->getVar('table_id');
278
            $tableName = $this->tables[$t]->getVar('table_name');
279
            $fields = $this->cf->getTableFields($this->modId, $tableId);
280
            $count = 0;
281
            if (1 == $this->tables[$t]->getVar('table_blocks')) {
282
                foreach (\array_keys($fields) as $f) {
283
                    if (1 == $fields[$f]->getVar('field_block')) {
284
                        $count++;
285
                    }
286
                }
287
                if (0 == $count) {
288
                    $info = \str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK1);
289
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
290
                }
291
            }
292
        }
293
        //use in block but no field date
294
        foreach (\array_keys($this->tables) as $t) {
295
            $tableId = $this->tables[$t]->getVar('table_id');
296
            $tableName = $this->tables[$t]->getVar('table_name');
297
298
            $count = 0;
299
            if (1 == $this->tables[$t]->getVar('table_blocks')) {
300
                $fields = $this->cf->getTableFields($this->modId, $tableId);
301
                foreach (\array_keys($fields) as $f) {
302
                    if (Constants::FIELD_ELE_TEXTDATESELECT == $fields[$f]->getVar('field_element') || Constants::FIELD_ELE_DATETIME == $fields[$f]->getVar('field_element')) {
303
                        $count++;
304
                    }
305
                }
306
                if (0 == $count) {
307
                    $info = \str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK2);
308
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
309
                }
310
            }
311
        }
312
313
        return true;
314
    }
315
316
    /**
317
     * @private function getCheckComments
318
     *
319
     * @return array|bool
320
     */
321
    private function getCheckComments()
322
    {
323
        //use comments in multiple tables
324
        $count         = 0;
325
        $tableComments = [];
326
        foreach (\array_keys($this->tables) as $t) {
327
            if (1 == $this->tables[$t]->getVar('table_comments')) {
328
                $count++;
329
                $tableComments[] = $this->tables[$t]->getVar('table_name');
330
            }
331
        }
332
        if ($count > 1) {
333
            $tablesComments = \implode(', ', $tableComments);
334
            $info = \str_replace('%t', $tablesComments, _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS1);
335
            $this->infos[] = ['icon' => 'error', 'info' => $info];
336
        }
337
338
        foreach (\array_keys($this->tables) as $t) {
339
            if (1 == $this->tables[$t]->getVar('table_comments')) {
340
                $tableId = $this->tables[$t]->getVar('table_id');
341
                $tableName = $this->tables[$t]->getVar('table_name');
342
                $fields = $this->cf->getTableFields($this->modId, $tableId);
343
                $fieldComments = 0;
344
                foreach (\array_keys($fields) as $f) {
345
                    if (Constants::FIELD_ELE_TEXTCOMMENTS == (int)$fields[$f]->getVar('field_element')) {
346
                        $fieldComments++;
347
                    }
348
                }
349
                // check whether each table with handling "comments" has also a field "comments"
350
                if (0 == $fieldComments) {
351
                    $info = \str_replace('%t', $tableName, _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS2);
352
                    $this->infos[] = ['icon' => 'warning', 'info' => $info];
353
                }
354
            }
355
        }
356
357
        return true;
358
    }
359
360
    /**
361
     * @private function getCheckRatings
362
     *
363
     * @return array|bool
364
     */
365
    private function getCheckRating()
366
    {
367
        foreach (\array_keys($this->tables) as $t) {
368
            if (1 == $this->tables[$t]->getVar('table_rate')) {
369
                $tableId = $this->tables[$t]->getVar('table_id');
370
                $tableName = $this->tables[$t]->getVar('table_name');
371
                $fields = $this->cf->getTableFields($this->modId, $tableId);
372
                $fieldRatings = 0;
373
                $fieldVotes = 0;
374
                foreach (\array_keys($fields) as $f) {
375
                    if (Constants::FIELD_ELE_TEXTRATINGS == (int)$fields[$f]->getVar('field_element')) {
376
                        $fieldRatings++;
377
                    }
378
                    if (Constants::FIELD_ELE_TEXTVOTES == (int)$fields[$f]->getVar('field_element')) {
379
                        $fieldVotes++;
380
                    }
381
                }
382
                // check whether each table with handling "rating" has also a field "rating"
383
                if (0 == (int)$fieldRatings) {
384
                    $info = \str_replace('%t', $tableName, _AM_MODULEBUILDER_BUILDING_CHECK_RATINGS1);
385
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
386
                }
387
                // check whether each table with handling "rating" has also a field "votes"
388
                if (0 == (int)$fieldVotes) {
389
                    $info = \str_replace('%t', $tableName, _AM_MODULEBUILDER_BUILDING_CHECK_RATINGS2);
390
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
391
                }
392
            }
393
        }
394
395
        return true;
396
    }
397
398
    /**
399
     * @private function getCheckReads
400
     *
401
     * @return array|bool
402
     */
403
    private function getCheckReads()
404
    {
405
        foreach (\array_keys($this->tables) as $t) {
406
            if (1 == $this->tables[$t]->getVar('table_reads')) {
407
                $tableId = $this->tables[$t]->getVar('table_id');
408
                $tableName = $this->tables[$t]->getVar('table_name');
409
                $fields = $this->cf->getTableFields($this->modId, $tableId);
410
                $fieldReads = 0;
411
                foreach (\array_keys($fields) as $f) {
412
                    if (Constants::FIELD_ELE_TEXTREADS == (int)$fields[$f]->getVar('field_element')) {
413
                        $fieldReads++;
414
                    }
415
                }
416
                // check whether each table with handling "reads" has also a field "reads"
417
                if (0 == (int)$fieldReads) {
418
                    $info = \str_replace('%t', $tableName, _AM_MODULEBUILDER_BUILDING_CHECK_READS1);
419
                    $this->infos[] = ['icon' => 'error', 'info' => $info];
420
                }
421
            }
422
        }
423
424
        return true;
425
    }
426
427
    /**
428
     * @private function getCheckSql
429
     *
430
     * @return array|bool
431
     */
432
    private function getCheckSql()
433
    {
434
        foreach (\array_keys($this->tables) as $t) {
435
            $tableId   = $this->tables[$t]->getVar('table_id');
436
            $tableName = $this->tables[$t]->getVar('table_name');
437
            $fields    = $this->cf->getTableFields($this->modId, $tableId);
438
            foreach (\array_keys($fields) as $f) {
439
                $fieldName = $fields[$f]->getVar('field_name');
440
                $fieldType = $fields[$f]->getVar('field_type');
441
                if (6 == $fieldType || 7 == $fieldType || 8 == $fieldType) {
442
                    $fieldValue = $fields[$f]->getVar('field_value');
443
                    if (0 == \strpos($fieldValue,',')) {
444
                        $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_SQL1);
445
                        $this->infos[] = ['icon' => 'error', 'info' => $info];
446
                    }
447
                    $fieldDefault = $fields[$f]->getVar('field_default');
448
                    if (0 == \strpos($fieldDefault,'.')) {
449
                        $info = \str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_SQL2);
450
                        $this->infos[] = ['icon' => 'warning', 'info' => $info];
451
                    }
452
                }
453
            }
454
        }
455
456
        return true;
457
    }
458
}
459