ClassHandlerFiles   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 336
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 152
dl 0
loc 336
rs 10
c 1
b 0
f 1
wmc 21

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassByCategory() 0 34 2
A getClassGetInsertId() 0 9 1
A write() 0 6 1
A getClassCriteria() 0 18 1
A getInstance() 0 8 2
A getClassCreate() 0 9 1
A render() 0 39 5
A getClassAll() 0 16 1
A getClassGet() 0 9 1
A __construct() 0 3 1
A getClassObjectHandler() 0 27 3
A getClassGetTableSolenameById() 0 22 1
A getClassCounter() 0 16 1
1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files\Classes;
4
5
use XoopsModules\Tdmcreate;
6
use XoopsModules\Tdmcreate\Files;
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
 * tc 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
/**
30
 * Class ClassHandlerFiles.
31
 */
32
class ClassHandlerFiles extends Files\CreateFile
33
{
34
    /**
35
     * @public function constructor
36
     *
37
     * @param null
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * @static function getInstance
46
     *
47
     * @return bool|ClassHandlerFiles
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
     *
62
     * @param string $module
63
     * @param string $table
64
     * @param mixed  $tables
65
     * @param        $filename
66
     */
67
    public function write($module, $table, $tables, $filename)
68
    {
69
        $this->setModule($module);
70
        $this->setTable($table);
71
        $this->setTables($tables);
72
        $this->setFileName($filename);
73
    }
74
75
    /**
76
     * @public function getClassHandler
77
     *
78
     * @param string $moduleDirname
79
     * @param string $table
80
     * @param string $fieldId
81
     * @param        $fieldName
82
     * @param string $fieldMain
83
     * @param        $fieldParentId
84
     * @param        $fieldElement
85
     * @return string
86
     */
87
    private function getClassObjectHandler($moduleDirname, $table, $fieldId, $fieldName, $fieldMain, $fieldParentId, $fieldElement)
88
    {
89
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
90
        $tableName        = $table->getVar('table_name');
91
        $tableFieldName   = $table->getVar('table_fieldname');
92
        $ucfTableName     = ucfirst($tableName);
93
        $multiLineCom     = ['Class Object Handler' => $ucfTableName];
94
        $ret              = $pc->getPhpCodeCommentMultiLine($multiLineCom);
95
96
        $cClh   = $pc->getPhpCodeCommentMultiLine(['Constructor' => '', '' => '', '@param' => '\XoopsDatabase $db'], "\t");
97
        $constr = "\t\tparent::__construct(\$db, '{$moduleDirname}_{$tableName}', {$ucfTableName}::class, '{$fieldId}', '{$fieldMain}');\n";
98
99
        $cClh .= $pc->getPhpCodeFunction('__construct', '\XoopsDatabase $db', $constr, 'public ', false, "\t");
100
        $cClh .= $this->getClassCreate();
101
        $cClh .= $this->getClassGet();
102
        $cClh .= $this->getClassGetInsertId();
103
        $cClh .= $this->getClassCounter($tableName, $fieldId, $fieldMain);
104
        $cClh .= $this->getClassAll($tableName, $fieldId, $fieldMain);
105
        $cClh .= $this->getClassCriteria($tableName);
106
        if ($fieldElement > 16 && in_array(1, $fieldParentId)) {
107
            $cClh .= $this->getClassByCategory($moduleDirname, $tableName, $tableFieldName, $fieldId, $fieldName, $fieldMain, $fieldElement);
108
            $cClh .= $this->getClassGetTableSolenameById($table, $fieldMain);
109
        }
110
111
        $ret .= $pc->getPhpCodeClass("{$ucfTableName}Handler", $cClh, '\XoopsPersistableObjectHandler');
112
113
        return $ret;
114
    }
115
116
    /**
117
     * @public function getClassCreate
118
     *
119
     * @return string
120
     */
121
    private function getClassCreate()
122
    {
123
        $pc    = Tdmcreate\Files\CreatePhpCode::getInstance();
124
        $ret   = $pc->getPhpCodeCommentMultiLine(['@param bool' => '$isNew', '' => '', '@return' => 'object'], "\t");
125
        $cClhc = $this->getSimpleString('return parent::create($isNew);', "\t\t");
126
127
        $ret .= $pc->getPhpCodeFunction('create', '$isNew = true', $cClhc, 'public ', false, "\t");
128
129
        return $ret;
130
    }
131
132
    /**
133
     * @public function getClassGet
134
     *
135
     * @return string
136
     */
137
    private function getClassGet()
138
    {
139
        $pc    = Tdmcreate\Files\CreatePhpCode::getInstance();
140
        $ret   = $pc->getPhpCodeCommentMultiLine(['retrieve a' => 'field', '' => '', '@param int' => '$i field id', '@param null' => 'fields', '@return mixed reference to the' => '{@link Get} object'], "\t");
141
        $cClhg = $this->getSimpleString('return parent::get($i, $fields);', "\t\t");
142
143
        $ret .= $pc->getPhpCodeFunction('get', '$i = null, $fields = null', $cClhg, 'public ', false, "\t");
144
145
        return $ret;
146
    }
147
148
    /**
149
     * @public function getClassGetInsertId
150
     *
151
     * @return string
152
     */
153
    private function getClassGetInsertId()
154
    {
155
        $pc      = Tdmcreate\Files\CreatePhpCode::getInstance();
156
        $ret     = $pc->getPhpCodeCommentMultiLine(['get inserted' => 'id', '' => '', '@param' => 'null', '@return integer reference to the' => '{@link Get} object'], "\t");
157
        $cClhgid = $this->getSimpleString('return $this->db->getInsertId();', "\t\t");
158
159
        $ret .= $pc->getPhpCodeFunction('getInsertId', '', $cClhgid, 'public ', false, "\t");
160
161
        return $ret;
162
    }
163
164
    /**
165
     * @public function getClassCounter
166
     *
167
     * @param $tableName
168
     * @param $fieldId
169
     * @param $fieldMain
170
     *
171
     * @return string
172
     */
173
    private function getClassCounter($tableName, $fieldId, $fieldMain)
174
    {
175
        $pc           = Tdmcreate\Files\CreatePhpCode::getInstance();
176
        $xc           = Tdmcreate\Files\CreateXoopsCode::getInstance();
177
        $ucfTableName = ucfirst($tableName);
178
        $ret          = $pc->getPhpCodeCommentMultiLine(['Get Count ' . $ucfTableName => 'in the database', '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'int'], "\t");
179
180
        $critCount  = $xc->getXcCriteriaCompo('crCount' . $ucfTableName, "\t\t");
181
        $paramsCrit = "\$this->get{$ucfTableName}Criteria(\$crCount{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
182
        $critCount  .= $xc->getXcEqualsOperator('$crCount' . $ucfTableName, $paramsCrit, null, "\t\t");
183
        $critCount  .= $this->getSimpleString("return parent::getCount(\$crCount{$ucfTableName});", "\t\t");
184
        $params     = "\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
185
186
        $ret .= $pc->getPhpCodeFunction('getCount' . $ucfTableName, $params, $critCount, 'public ', false, "\t");
187
188
        return $ret;
189
    }
190
191
    /**
192
     * @public function getClassAll
193
     *
194
     * @param $tableName
195
     * @param $fieldId
196
     * @param $fieldMain
197
     *
198
     * @return string
199
     */
200
    private function getClassAll($tableName, $fieldId, $fieldMain)
201
    {
202
        $pc           = Tdmcreate\Files\CreatePhpCode::getInstance();
203
        $xc           = Tdmcreate\Files\CreateXoopsCode::getInstance();
204
        $ucfTableName = ucfirst($tableName);
205
        $ret          = $pc->getPhpCodeCommentMultiLine(['Get All ' . $ucfTableName => 'in the database', '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'array'], "\t");
206
207
        $critAll    = $xc->getXcCriteriaCompo('crAll' . $ucfTableName, "\t\t");
208
        $paramsCrit = "\$this->get{$ucfTableName}Criteria(\$crAll{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
209
        $critAll    .= $xc->getXcEqualsOperator('$crAll' . $ucfTableName, $paramsCrit, null, "\t\t");
210
        $critAll    .= $this->getSimpleString("return parent::getAll(\$crAll{$ucfTableName});", "\t\t");
211
        $params     = "\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
212
213
        $ret .= $pc->getPhpCodeFunction('getAll' . $ucfTableName, $params, $critAll, 'public ', false, "\t");
214
215
        return $ret;
216
    }
217
218
    /**
219
     * @public function getClassByCategory
220
     *
221
     * @param $moduleDirname
222
     * @param $tableName
223
     * @param $tableFieldName
224
     * @param $fieldId
225
     * @param $fieldName
226
     * @param $fieldMain
227
     * @param $fieldElement
228
     * @return string
229
     */
230
    private function getClassByCategory($moduleDirname, $tableName, $tableFieldName, $fieldId, $fieldName, $fieldMain, $fieldElement)
231
    {
232
        $tc                = Tdmcreate\Helper::getInstance();
233
        $pc                = Tdmcreate\Files\CreatePhpCode::getInstance();
234
        $xc                = Tdmcreate\Files\CreateXoopsCode::getInstance();
235
        $ucfTableName      = ucfirst($tableName);
236
        $fieldElements     = $tc->getHandler('fieldelements')->get($fieldElement);
237
        $fieldElementName  = $fieldElements->getVar('fieldelement_name');
238
        $fieldNameDesc     = ucfirst(mb_substr($fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen($fieldElementName)));
0 ignored issues
show
Bug introduced by
It seems like $fieldElementName can also be of type array and array; however, parameter $haystack of mb_strrpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

238
        $fieldNameDesc     = ucfirst(mb_substr($fieldElementName, mb_strrpos(/** @scrutinizer ignore-type */ $fieldElementName, ':'), mb_strlen($fieldElementName)));
Loading history...
Bug introduced by
It seems like $fieldElementName can also be of type array and array; however, parameter $str of mb_strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

238
        $fieldNameDesc     = ucfirst(mb_substr($fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen(/** @scrutinizer ignore-type */ $fieldElementName)));
Loading history...
Bug introduced by
It seems like $fieldElementName can also be of type array and array; however, parameter $str of mb_substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

238
        $fieldNameDesc     = ucfirst(mb_substr(/** @scrutinizer ignore-type */ $fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen($fieldElementName)));
Loading history...
239
        $topicTableName    = str_replace(': ', '', $fieldNameDesc);
240
        $lcfTopicTableName = lcfirst($topicTableName);
241
242
        $ret = $pc->getPhpCodeCommentMultiLine(["Get All {$ucfTableName} By" => "{$fieldNameDesc} Id", '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'array'], "\t");
243
244
        $critAll = $xc->getXcXoopsHandler('groupperm', "\t\t");
245
        $param1  = "'{$moduleDirname}_view'";
246
        $param2  = "\$GLOBALS['xoopsUser']->getGroups()";
247
        $param3  = "\$GLOBALS['xoopsModule']->getVar('mid')";
248
        $critAll .= $xc->getXcGetItemIds($lcfTopicTableName, 'grouppermHandler', $param1, $param2, $param3, "\t\t");
249
        $critAll .= $xc->getXcCriteriaCompo('crAll' . $ucfTableName, "\t\t");
250
251
        if (false !== mb_strpos($fieldName, 'status')) {
252
            $crit    = $xc->getXcCriteria('', "'{$fieldName}'", '0', "'!='", true);
253
            $critAll .= $xc->getXcCriteriaAdd('crAll' . $ucfTableName, $crit, "\t\t");
254
        }
255
        $paramsCritAll = "\$this->get{$ucfTableName}Criteria(\$crAll{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
256
        $critAll       .= $xc->getXcEqualsOperator('$crAll' . $ucfTableName, $paramsCritAll, null, "\t\t");
257
258
        $critAll .= $this->getSimpleString("return parent::getAll(\$crAll{$ucfTableName});", "\t\t");
259
        $params  = "\${$tableFieldName}Id, \$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
260
261
        $ret .= $pc->getPhpCodeFunction("getAll{$ucfTableName}By{$fieldNameDesc}Id" . $ucfTableName, $params, $critAll, 'public ', false, "\t");
262
263
        return $ret;
264
    }
265
266
    /**
267
     * @public function getClassCriteria
268
     *
269
     * @param $tableName
270
     * @return string
271
     */
272
    private function getClassCriteria($tableName)
273
    {
274
        $pc           = Tdmcreate\Files\CreatePhpCode::getInstance();
275
		$xc           = Tdmcreate\Files\CreateXoopsCode::getInstance();
276
        $ucfTableName = ucfirst($tableName);
277
        $ret          = $pc->getPhpCodeCommentMultiLine(['Get' => 'Criteria ' . $ucfTableName, '@param       ' => "\$cr{$ucfTableName}", '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'int'], "\t");
278
279
        $paramsAllCriteria = "\$cr{$ucfTableName}, \$start, \$limit, \$sort, \$order";
280
281
        $critSets = $xc->getXcCriteriaSetStart('cr' . $ucfTableName, '$start', "\t\t");
282
        $critSets .= $xc->getXcCriteriaSetLimit('cr' . $ucfTableName, '$limit', "\t\t");
283
        $critSets .= $xc->getXcCriteriaSetSort('cr' . $ucfTableName, '$sort', "\t\t");
284
        $critSets .= $xc->getXcCriteriaSetOrder('cr' . $ucfTableName, '$order', "\t\t");
285
        $critSets .= $this->getSimpleString("return \$cr{$ucfTableName};", "\t\t");
286
287
        $ret .= $pc->getPhpCodeFunction("get{$ucfTableName}Criteria", $paramsAllCriteria, $critSets, 'private ', false, "\t");
288
289
        return $ret;
290
    }
291
292
    /**
293
     * @public function getClassGetTableSolenameById
294
     *
295
     * @param $table
296
     * @param $fieldMain
297
     * @return string
298
     */
299
    private function getClassGetTableSolenameById($table, $fieldMain)
300
    {
301
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
302
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
303
        $tableName        = $table->getVar('table_name');
304
        $tableSoleName    = $table->getVar('table_solename');
305
        $ucfTableSoleName = ucfirst($tableSoleName);
306
        $ccTableSoleName  = $this->getCamelCase($tableSoleName, true);
307
308
        $ret              = $pc->getPhpCodeCommentMultiLine(['Returns the' => $ucfTableSoleName . ' from id', '' => '', '@return' => 'string'], "\t");
309
        $soleName         = $xc->getXcEqualsOperator("\${$tableSoleName}Id", "(int)( \${$tableSoleName}Id )", null, "\t\t");
310
        $soleName         .= $xc->getXcEqualsOperator("\${$tableSoleName}", "''", null, "\t\t");
311
        $contentIf        = $xc->getXcHandlerLine($tableName, "\t\t\t");
312
        $contentIf        .= $xc->getXcHandlerGet($tableName, "\${$tableSoleName}Id", 'Obj', true, false, "\t\t\t");
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $handler of XoopsModules\Tdmcreate\F...Code::getXcHandlerGet(). ( Ignorable by Annotation )

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

312
        $contentIf        .= $xc->getXcHandlerGet($tableName, "\${$tableSoleName}Id", 'Obj', /** @scrutinizer ignore-type */ true, false, "\t\t\t");
Loading history...
313
        $getVar           = $xc->getXcGetVar($ccTableSoleName, "{$tableSoleName}Obj", $fieldMain, false, "\t\t\t\t");
314
        $contentIf        .= $pc->getPhpCodeConditions("is_object( \${$tableSoleName}Obj )", '', '', $getVar, false, "\t\t\t");
315
        $soleName         .= $pc->getPhpCodeConditions("\${$tableSoleName}Id", ' > ', '0', $contentIf, false, "\t\t");
316
        $soleName         .= $this->getSimpleString("return \${$tableSoleName};", "\t\t");
317
318
        $ret .= $pc->getPhpCodeFunction("get{$ucfTableSoleName}FromId", "\${$tableSoleName}Id", $soleName, 'public ', false, "\t");
319
320
        return $ret;
321
    }
322
323
    /**
324
     * @public function render
325
     * @param null
326
     *
327
     * @return bool|string
328
     */
329
    public function render()
330
    {
331
        $tc             = Tdmcreate\Helper::getInstance();
332
        $pc             = Tdmcreate\Files\CreatePhpCode::getInstance();
333
        $module         = $this->getModule();
334
        $table          = $this->getTable();
335
        $filename       = $this->getFileName();
336
        $moduleDirname  = $module->getVar('mod_dirname');
337
        $fields         = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
338
        $fieldInForm    = [];
339
        $fieldParentId  = [];
340
        $fieldElementId = [];
341
        $fieldId        = null;
342
        $fieldName      = null;
343
        $fieldMain      = null;
344
        $fieldElement   = null;
345
        foreach (array_keys($fields) as $f) {
346
            $fieldName       = $fields[$f]->getVar('field_name');
347
            $fieldInForm[]   = $fields[$f]->getVar('field_inform');
348
            $fieldParentId[] = $fields[$f]->getVar('field_parent');
349
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
350
                $fieldId = $fieldName; // $fieldId = fields parameter index field
351
            }
352
            if (1 == $fields[$f]->getVar('field_main')) {
353
                $fieldMain = $fieldName; // $fieldMain = fields parameter main field
354
            }
355
            $fieldElement = $fields[$f]->getVar('field_element');
356
357
            $fieldElements    = $tc->getHandler('fieldelements')->get($fieldElement);
358
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
359
        }
360
        $namespace = $pc->getPhpCodeNamespace(['XoopsModules', $moduleDirname]);
361
        $content   = $this->getHeaderFilesComments($module, null, $namespace);
362
        $content   .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname]);
363
        $content   .= $this->getClassObjectHandler($moduleDirname, $table, $fieldId, $fieldName, $fieldMain, $fieldParentId, $fieldElement);
364
365
        $this->create($moduleDirname, 'class', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
366
367
        return $this->renderFile();
368
    }
369
}
370