Passed
Push — master ( 030234...7e86df )
by Goffy
03:16
created

ClassFiles::getValuesInObject()   F

Complexity

Conditions 21
Paths 180

Size

Total Lines 128
Code Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 111
c 0
b 0
f 0
nc 180
nop 3
dl 0
loc 128
rs 2.8

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files\Classes;
4
5
use XoopsModules\Modulebuilder;
6
use XoopsModules\Modulebuilder\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 ClassFiles.
31
 */
32
class ClassFiles extends Files\CreateFile
33
{
34
35
    /**
36
     * @var mixed
37
     */
38
    private $cxc = null;
39
40
    /**
41
     * @var mixed
42
     */
43
    private $xc = null;
44
45
    /**
46
     * @var mixed
47
     */
48
    private $pc = null;
49
50
    /**
51
     * @var mixed
52
     */
53
    private $helper = null;
54
55
    /**
56
     * @public function constructor
57
     * @param null
58
     */
59
    public function __construct()
60
    {
61
        parent::__construct();
62
        $this->xc     = Modulebuilder\Files\CreateXoopsCode::getInstance();
63
        $this->pc     = Modulebuilder\Files\CreatePhpCode::getInstance();
64
        $this->cxc    = Modulebuilder\Files\Classes\ClassXoopsCode::getInstance();
65
        $this->helper = Modulebuilder\Helper::getInstance();
66
    }
67
68
    /**
69
     * @static function getInstance
70
     *
71
     * @param null
72
     *
73
     * @return ClassFiles
74
     */
75
    public static function getInstance()
76
    {
77
        static $instance = false;
78
        if (!$instance) {
79
            $instance = new self();
80
        }
81
82
        return $instance;
83
    }
84
85
    /**
86
     * @public function write
87
     *
88
     * @param string $module
89
     * @param string $table
90
     * @param mixed  $tables
91
     * @param        $filename
92
     */
93
    public function write($module, $table, $tables, $filename)
94
    {
95
        $this->setModule($module);
96
        $this->setTable($table);
97
        $this->setTables($tables);
98
        $this->setFileName($filename);
99
    }
100
101
    /**
102
     * @private function getInitVar
103
     *
104
     * @param string $fieldName
105
     * @param string $type
106
     *
107
     * @return string
108
     */
109
    private function getInitVar($fieldName, $type = 'INT')
110
    {
111
112
        return $this->cxc->getClassInitVar($fieldName, $type);
113
114
    }
115
116
    /**
117
     * @private function getInitVars
118
     *
119
     * @param array $fields
120
     *
121
     * @return string
122
     */
123
    private function getInitVars($fields)
124
    {
125
        $ret = '';
126
        // Creation of the initVar functions list
127
        foreach (array_keys($fields) as $f) {
128
            $fieldName = $fields[$f]->getVar('field_name');
129
            $fieldType = $fields[$f]->getVar('field_type');
130
            switch ($fieldType) {
131
                case 2:
132
                case 3:
133
                case 4:
134
                case 5:
135
                    $ret .= $this->getInitVar($fieldName, 'INT');
136
                    break;
137
                case 6:
138
                    $ret .= $this->getInitVar($fieldName, 'FLOAT');
139
                    break;
140
                case 7:
141
                case 8:
142
                    $ret .= $this->getInitVar($fieldName, 'DECIMAL');
143
                    break;
144
                case 10:
145
                    $ret .= $this->getInitVar($fieldName, 'ENUM');
146
                    break;
147
                case 11:
148
                    $ret .= $this->getInitVar($fieldName, 'EMAIL');
149
                    break;
150
                case 12:
151
                    $ret .= $this->getInitVar($fieldName, 'URL');
152
                    break;
153
                case 13:
154
                case 14:
155
                    $ret .= $this->getInitVar($fieldName, 'TXTBOX');
156
                    break;
157
                case 15:
158
                case 16:
159
                case 17:
160
                case 18:
161
                    if ((int)$fields[$f]->getVar('field_element') == 4) {
162
                        $ret .= $this->getInitVar($fieldName, 'OTHER');
163
                    } else {
164
                        $ret .= $this->getInitVar($fieldName, 'TXTAREA');
165
                    }
166
                    break;
167
                case 19:
168
                case 20:
169
                case 21:
170
                case 22:
171
                case 23:
172
                    $ret .= $this->getInitVar($fieldName, 'LTIME');
173
                    break;
174
            }
175
        }
176
177
        return $ret;
178
    }
179
180
    /**
181
     * @private  function getClassObject
182
     * @param $module
183
     * @param $table
184
     * @param $fields
185
     * @return string
186
     */
187
    private function getClassObject($module, $table, $fields)
188
    {
189
        $moduleDirname    = $module->getVar('mod_dirname');
190
        $tableName        = $table->getVar('table_name');
191
        $ucfTableName     = ucfirst($tableName);
192
        $ret              = $this->pc->getPhpCodeDefined();
193
        $ret              .= $this->pc->getPhpCodeCommentMultiLine(['Class Object' => $ucfTableName]);
194
        $cCl              = '';
195
196
        $fieldInForm      = [];
197
        $fieldElementId   = [];
198
        $optionsFieldName = [];
199
        $fieldId          = null;
200
        foreach (array_keys($fields) as $f) {
201
            $fieldName        = $fields[$f]->getVar('field_name');
202
            $fieldElement     = $fields[$f]->getVar('field_element');
203
            $fieldInForm[]    = $fields[$f]->getVar('field_inform');
204
            $fieldElements    = $this->helper->getHandler('Fieldelements')->get($fieldElement);
205
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
206
            $rpFieldName      = $this->getRightString($fieldName);
207
            if (in_array(5, $fieldElementId)) {
208
                //if (count($rpFieldName) % 5) {
209
                    //$optionsFieldName[] = "'" . $rpFieldName . "'";
210
                //} else {
211
                    $optionsFieldName[] = "'" . $rpFieldName . "'\n";
212
                //}
213
            }
214
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
215
                $fieldId = $fieldName;
216
            }
217
        }
218
        if (in_array(5, $fieldElementId) > 1) {
219
            $cCl             .= $this->pc->getPhpCodeCommentMultiLine(['Options' => '']);
220
            $options         = $this->pc->getPhpCodeArray('', $optionsFieldName, true);
221
            $cCl             .= $this->pc->getPhpCodeVariableClass('private', 'options', $options);
222
        }
223
        unset($optionsFieldName);
224
225
        $cCl              .= $this->pc->getPhpCodeCommentMultiLine(['Constructor' => '', '' => '', '@param' => 'null'], "\t");
226
        $constr           = $this->getInitVars($fields);
227
        $cCl              .= $this->pc->getPhpCodeFunction('__construct', '', $constr, 'public ', false, "\t");
228
        $arrayGetInstance = ['@static function' => '&getInstance', '' => '', '@param' => 'null'];
229
        $cCl              .= $this->pc->getPhpCodeCommentMultiLine($arrayGetInstance, "\t");
230
        $getInstance      = $this->pc->getPhpCodeVariableClass('static', 'instance', 'false', "\t\t");
231
        $instance         = $this->xc->getXcEqualsOperator('$instance', 'new self()', null, "\t\t\t");
232
        $getInstance      .= $this->pc->getPhpCodeConditions('!$instance', '', '', $instance, false, "\t\t");
233
        $cCl              .= $this->pc->getPhpCodeFunction('getInstance', '', $getInstance, 'public static ', false, "\t");
234
235
        $cCl .= $this->getNewInsertId($table);
236
        $cCl .= $this->getFunctionForm($module, $table, $fieldId, $fieldInForm);
237
        $cCl .= $this->getValuesInObject($moduleDirname, $table, $fields);
238
        $cCl .= $this->getToArrayInObject($table);
239
240
        if (in_array(5, $fieldElementId) > 1) {
241
            $cCl .= $this->getOptionsCheck($table);
242
        }
243
        unset($fieldElementId);
244
245
        $ret .= $this->pc->getPhpCodeClass($ucfTableName, $cCl, '\XoopsObject');
246
247
        return $ret;
248
    }
249
250
    /**
251
     * @private function getNewInsertId
252
     *
253
     * @param $table
254
     *
255
     * @return string
256
     */
257
    private function getNewInsertId($table)
258
    {
259
        $tableName     = $table->getVar('table_name');
260
        $ucfTableName  = ucfirst($tableName);
261
        $ret           = $this->pc->getPhpCodeCommentMultiLine(['The new inserted' => '$Id', '@return' => 'inserted id'], "\t");
262
        $getInsertedId = $this->xc->getXcEqualsOperator('$newInsertedId', "\$GLOBALS['xoopsDB']->getInsertId()", null, "\t\t");
263
        $getInsertedId .= $this->getSimpleString('return $newInsertedId;', "\t\t");
264
265
        $ret .= $this->pc->getPhpCodeFunction('getNewInsertedId' . $ucfTableName, '', $getInsertedId, 'public ', false, "\t");
266
267
        return $ret;
268
    }
269
270
    /**
271
     * @private function getFunctionForm
272
     *
273
     * @param string $module
274
     * @param string $table
275
     *
276
     * @param        $fieldId
277
     * @param        $fieldInForm
278
     * @return string
279
     */
280
    private function getFunctionForm($module, $table, $fieldId, $fieldInForm)
281
    {
282
        $fe               = ClassFormElements::getInstance();
283
        $moduleDirname    = $module->getVar('mod_dirname');
284
        $tableName        = $table->getVar('table_name');
285
        $tableSoleName    = $table->getVar('table_solename');
286
        $tableCategory    = $table->getVar('table_category');
287
        $ucfTableName     = ucfirst($tableName);
288
        $stuTableSoleName = mb_strtoupper($tableSoleName);
289
        $language         = $this->getLanguage($moduleDirname, 'AM');
290
        $fe->initForm($module, $table);
291
        $ret              = $this->pc->getPhpCodeCommentMultiLine(['@public function' => 'getForm', '@param bool' => '$action', '@return' => '\XoopsThemeForm'], "\t");
292
        $action           = $this->xc->getXcEqualsOperator('$action', "\$_SERVER['REQUEST_URI']", null, "\t\t\t");
293
        $ucfModuleDirname = ucfirst($moduleDirname);
294
        $getForm          = $this->xc->getXcGetInstance('helper', "\XoopsModules\\{$ucfModuleDirname}\Helper", "\t\t");
295
        $getForm          .= $this->pc->getPhpCodeConditions('$action', ' === ', 'false', $action, false, "\t\t");
296
        $xUser            = $this->pc->getPhpCodeGlobals('xoopsUser');
297
        $xModule          = $this->pc->getPhpCodeGlobals('xoopsModule');
298
        $getForm          .= $this->xc->getXcEqualsOperator('$isAdmin', $xUser . '->isAdmin(' . $xModule . '->mid())', null, "\t\t");
299
        if ((1 != $tableCategory) && (1 == $table->getVar('table_permissions'))) {
300
            $permString = 'upload_groups';
301
            $getForm          .= $this->pc->getPhpCodeCommentLine('Permissions for', 'uploader', "\t\t");
302
            $getForm          .= $this->xc->getXcXoopsHandler('groupperm', "\t\t");
303
            $getForm          .= $this->pc->getPhpCodeTernaryOperator('groups', 'is_object(' . $xUser . ')', $xUser . '->getGroups()', 'XOOPS_GROUP_ANONYMOUS', "\t\t");
304
            $checkRight       = $this->xc->getXcCheckRight('$grouppermHandler', $permString, 32, '$groups', $xModule . '->getVar(\'mid\')', true);
305
            $getForm  .= $this->pc->getPhpCodeTernaryOperator('permissionUpload', $checkRight, 'true', 'false', "\t\t");
306
        }
307
        $getForm .= $this->pc->getPhpCodeCommentLine('Title', '', "\t\t");
308
        $getForm .= $this->pc->getPhpCodeTernaryOperator('title', '$this->isNew()', "sprintf({$language}{$stuTableSoleName}_ADD)", "sprintf({$language}{$stuTableSoleName}_EDIT)", "\t\t");
309
        $getForm .= $this->pc->getPhpCodeCommentLine('Get Theme', 'Form', "\t\t");
310
        $getForm .= $this->xc->getXcXoopsLoad('XoopsFormLoader', "\t\t");
311
        $getForm .= $this->cxc->getClassXoopsThemeForm('form', 'title', 'form', 'action', 'post');
312
        $getForm .= $this->cxc->getClassSetExtra('form', "'enctype=\"multipart/form-data\"'");
313
        $getForm .= $fe->renderElements();
314
315
        if (in_array(1, $fieldInForm)) {
316
            if (1 == $table->getVar('table_permissions')) {
317
                $getForm .= $this->getPermissionsInForm($moduleDirname, $fieldId, $tableName);
318
            }
319
        }
320
        $getForm .= $this->pc->getPhpCodeCommentLine('To Save', '', "\t\t");
321
        //$hiddenSave = $cc->getClassXoopsFormHidden('', "'op'", "'save'", true, false);
322
        $getForm .= $this->cxc->getClassAddElement('form', "new \XoopsFormHidden('op', 'save')");
323
        $getForm .= $this->cxc->getClassAddElement('form', "new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)");
324
        $getForm .= $this->getSimpleString('return $form;', "\t\t");
325
326
        $ret .= $this->pc->getPhpCodeFunction('getForm' . $ucfTableName, '$action = false', $getForm, 'public ', false, "\t");
327
328
        return $ret;
329
    }
330
331
    /**
332
     * @private function getPermissionsInForm
333
     *
334
     * @param string $moduleDirname
335
     * @param string $fieldId
336
     *
337
     * @param $tableName
338
     * @return string
339
     */
340
    private function getPermissionsInForm($moduleDirname, $fieldId, $tableName)
341
    {
342
        $permissionApprove = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_APPROVE');
343
        $permissionSubmit  = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_SUBMIT');
344
        $permissionView    = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_VIEW');
345
        $ret               = $this->pc->getPhpCodeCommentLine('Permissions', '', "\t\t");
346
        $ret               .= $this->xc->getXcXoopsHandler('member', "\t\t");
347
        $ret               .= $this->xc->getXcEqualsOperator('$groupList', '$memberHandler->getGroupList()', null, "\t\t");
348
        $ret               .= $this->xc->getXcXoopsHandler('groupperm',  "\t\t");
349
        $ret               .= $this->pc->getPhpCodeArrayType('fullList', 'keys', 'groupList', null, false, "\t\t");
350
        $fId               = $this->xc->getXcGetVar('', 'this', $fieldId, true);
351
        $mId               = $this->xc->getXcGetVar('', "GLOBALS['xoopsModule']", 'mid', true);
352
        $ifGroups          = $this->xc->getXcGetGroupIds('groupsIdsApprove', 'grouppermHandler', "'{$moduleDirname}_approve_{$tableName}'", $fId, $mId, "\t\t\t");
353
        $ifGroups          .= $this->pc->getPhpCodeArrayType('groupsIdsApprove', 'values', 'groupsIdsApprove', null, false, "\t\t\t");
354
        $ifGroups          .= $this->cxc->getClassXoopsFormCheckBox('groupsCanApproveCheckbox', $permissionApprove, "groups_approve_{$tableName}[]", '$groupsIdsApprove', false, "\t\t\t");
355
        $ifGroups          .= $this->xc->getXcGetGroupIds('groupsIdsSubmit', 'grouppermHandler', "'{$moduleDirname}_submit_{$tableName}'", $fId, $mId, "\t\t\t");
356
        $ifGroups          .= $this->pc->getPhpCodeArrayType('groupsIdsSubmit', 'values', 'groupsIdsSubmit', null, false, "\t\t\t");
357
        $ifGroups          .= $this->cxc->getClassXoopsFormCheckBox('groupsCanSubmitCheckbox', $permissionSubmit, "groups_submit_{$tableName}[]", '$groupsIdsSubmit', false, "\t\t\t");
358
        $ifGroups          .= $this->xc->getXcGetGroupIds('groupsIdsView', 'grouppermHandler', "'{$moduleDirname}_view_{$tableName}'", $fId, $mId, "\t\t\t");
359
        $ifGroups          .= $this->pc->getPhpCodeArrayType('groupsIdsView', 'values', 'groupsIdsView', null, false, "\t\t\t");
360
        $ifGroups          .= $this->cxc->getClassXoopsFormCheckBox('groupsCanViewCheckbox', $permissionView, "groups_view_{$tableName}[]", '$groupsIdsView', false, "\t\t\t");
361
362
        $else = $this->cxc->getClassXoopsFormCheckBox('groupsCanApproveCheckbox', $permissionApprove, "groups_approve_{$tableName}[]", '$fullList', false, "\t\t\t");
363
        $else .= $this->cxc->getClassXoopsFormCheckBox('groupsCanSubmitCheckbox', $permissionSubmit, "groups_submit_{$tableName}[]", '$fullList', false, "\t\t\t");
364
        $else .= $this->cxc->getClassXoopsFormCheckBox('groupsCanViewCheckbox', $permissionView, "groups_view_{$tableName}[]", '$fullList', false, "\t\t\t");
365
366
        $ret .= $this->pc->getPhpCodeConditions('!$this->isNew()', null, null, $ifGroups, $else, "\t\t");
367
        $ret .= $this->pc->getPhpCodeCommentLine('To Approve', '', "\t\t");
368
        $ret .= $this->cxc->getClassAddOptionArray('groupsCanApproveCheckbox', '$groupList');
369
        $ret .= $this->cxc->getClassAddElement('form', '$groupsCanApproveCheckbox');
370
        $ret .= $this->pc->getPhpCodeCommentLine('To Submit', '', "\t\t");
371
        $ret .= $this->cxc->getClassAddOptionArray('groupsCanSubmitCheckbox', '$groupList');
372
        $ret .= $this->cxc->getClassAddElement('form', '$groupsCanSubmitCheckbox');
373
        $ret .= $this->pc->getPhpCodeCommentLine('To View', '', "\t\t");
374
        $ret .= $this->cxc->getClassAddOptionArray('groupsCanViewCheckbox', '$groupList');
375
        $ret .= $this->cxc->getClassAddElement('form', '$groupsCanViewCheckbox');
376
377
        return $ret;
378
    }
379
380
    /**
381
     * @private  function getValuesInObject
382
     *
383
     * @param $moduleDirname
384
     * @param $table
385
     * @param $fields
386
     * @return string
387
     * @internal param $null
388
     */
389
    private function getValuesInObject($moduleDirname, $table, $fields)
390
    {
391
        $ucfTableName     = ucfirst($table->getVar('table_name'));
392
        $ret              = $this->pc->getPhpCodeCommentMultiLine(['Get' => 'Values', '@param null $keys' => '', '@param null $format' => '', '@param null$maxDepth' => '', '@return' => 'array'], "\t");
393
        $ucfModuleDirname = ucfirst($moduleDirname);
394
        $language         = $this->getLanguage($moduleDirname, 'AM');
395
        $getValues        = $this->xc->getXcEqualsOperator('$ret', '$this->getValues($keys, $format, $maxDepth)', null, "\t\t");
396
        $tablePermissions = $table->getVar('table_permissions');
397
        $tableBroken      = $table->getVar('table_broken');
398
        $fieldMainTopic   = null;
399
        $helper           = 0;
400
        $utility          = 0;
401
        $header           = '';
402
        $configMaxchar    = 0;
403
        $lenMaxName       = 0;
404
        foreach (array_keys($fields) as $f) {
405
            $fieldName = $fields[$f]->getVar('field_name');
406
            $rpFieldName  = $this->getRightString($fieldName);
407
            $len = strlen($rpFieldName);
408
            if (3 == $fields[$f]->getVar('field_element') || 4 == $fields[$f]->getVar('field_element')) {
409
                $len = $len + strlen('_short');
410
            }
411
            $lenMaxName = max($len, $lenMaxName);
412
        }
413
        foreach (array_keys($fields) as $f) {
414
            $fieldName    = $fields[$f]->getVar('field_name');
415
            $fieldElement = $fields[$f]->getVar('field_element');
416
            $rpFieldName  = $this->getRightString($fieldName);
417
            $spacer = str_repeat(' ', $lenMaxName - strlen($rpFieldName));
418
            switch ($fieldElement) {
419
                case 3:
420
                    $getValues .= $this->pc->getPhpCodeStripTags("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}', 'e')", false, "\t\t");
421
                    if ($configMaxchar == 0) {
422
                        $getValues .= $this->xc->getXcEqualsOperator('$editorMaxchar', $this->xc->getXcGetConfig('editor_maxchar'), false, "\t\t");
423
                        $configMaxchar = 1;
424
                    }
425
                    $truncate  =  "\$utility::truncateHtml(\$ret['{$rpFieldName}'], \$editorMaxchar)";
426
                    $spacer = str_repeat(' ', $lenMaxName - strlen($rpFieldName) - strlen('_short'));
427
                    $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}_short']{$spacer}", $truncate, false, "\t\t");
428
                    $helper = 1;
429
                    $utility = 1;
430
                    break;
431
                case 4:
432
                    $getValues .= $this->xc->getXcGetVar("ret['{$rpFieldName}']{$spacer}", 'this', $fieldName, false, "\t\t", ", 'e'");
433
                    if ($configMaxchar == 0) {
434
                        $getValues .= $this->xc->getXcEqualsOperator('$editorMaxchar', $this->xc->getXcGetConfig('editor_maxchar'), false, "\t\t");
435
                        $configMaxchar = 1;
436
                    }
437
                    $truncate  =  "\$utility::truncateHtml(\$ret['{$rpFieldName}'], \$editorMaxchar)";
438
                    $spacer = str_repeat(' ', $lenMaxName - strlen($rpFieldName) - strlen('_short'));
439
                    $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}_short']{$spacer}", $truncate, false, "\t\t");
440
                    $helper = 1;
441
                    $utility = 1;
442
                    break;
443
                case 6:
444
                    $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}']{$spacer}", "(int)\$this->getVar('{$fieldName}') > 0 ? _YES : _NO", false, "\t\t");
445
                    break;
446
                case 8:
447
                    $getValues .= $this->xc->getXcXoopsUserUnameFromId("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}')", "\t\t");
448
                    break;
449
                case 15:
450
                    $getValues .= $this->xc->getXcFormatTimeStamp("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}')", 's', "\t\t");
451
                    break;
452
                case 16:
453
                    $spacer = str_repeat(' ', $lenMaxName - strlen('status') + 7);
454
                    $getValues .= $this->xc->getXcGetVar("status{$spacer}", 'this', $fieldName, false, "\t\t");
455
                    $spacer = str_repeat(' ', $lenMaxName - strlen('status'));
456
                    $getValues .= $this->xc->getXcEqualsOperator("\$ret['status']{$spacer}", '$status', false, "\t\t");
457
                    $contCase1  = $this->xc->getXcEqualsOperator('$status_text', $language . 'STATUS_NONE', false, "\t\t\t\t");
458
                    $cases[$this->xc->getXcGetConstants('STATUS_NONE')] = [$contCase1];
459
                    $contCase2  = $this->xc->getXcEqualsOperator('$status_text', $language . 'STATUS_OFFLINE', false, "\t\t\t\t");
460
                    $cases[$this->xc->getXcGetConstants('STATUS_OFFLINE')] = [$contCase2];
461
                    $contCase3  = $this->xc->getXcEqualsOperator('$status_text', $language . 'STATUS_SUBMITTED', false, "\t\t\t\t");
462
                    $cases[$this->xc->getXcGetConstants('STATUS_SUBMITTED')] = [$contCase3];
463
                    if (1 == $tablePermissions) {
464
                        $contCase4 = $this->xc->getXcEqualsOperator('$status_text', $language . 'STATUS_APPROVED', false, "\t\t\t\t");
465
                        $cases[$this->xc->getXcGetConstants('STATUS_APPROVED')] = [$contCase4];
466
                    }
467
                    if (1 == $tableBroken) {
468
                        $contCase5 = $this->xc->getXcEqualsOperator('$status_test', $language . 'STATUS_BROKEN', false, "\t\t\t\t");
469
                        $cases[$this->xc->getXcGetConstants('STATUS_BROKEN')] = [$contCase5];
470
                    }
471
                    $contentSwitch = $this->pc->getPhpCodeCaseSwitch($cases, true, false, "\t\t\t", true);
472
                    $getValues     .= $this->pc->getPhpCodeSwitch('status', $contentSwitch, "\t\t");
473
                    $spacer        = str_repeat(' ', $lenMaxName - strlen('status_text'));
474
                    $getValues     .= $this->xc->getXcEqualsOperator("\$ret['status_text']{$spacer}", '$status_text',  false, "\t\t");
475
                    break;
476
                case 21:
477
                    $getValues .= $this->xc->getXcFormatTimeStamp("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}')", 'm', "\t\t");
478
                    break;
479
                default:
480
                    $fieldElements    = $this->helper->getHandler('Fieldelements')->get($fieldElement);
481
                    $fieldElementTid  = $fieldElements->getVar('fieldelement_tid');
482
                    if ((int)$fieldElementTid > 0 ) {
483
                        $fieldElementMid = $fieldElements->getVar('fieldelement_mid');
484
                        $fieldElementName = (string)$fieldElements->getVar('fieldelement_name');
485
                        $fieldNameDesc = mb_substr($fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen($fieldElementName));
486
                        $topicTableName = str_replace(': ', '', mb_strtolower($fieldNameDesc));
487
                        $fieldsTopics = $this->getTableFields($fieldElementMid, $fieldElementTid);
488
                        foreach (array_keys($fieldsTopics) as $g) {
489
                            $fieldNameTopic = $fieldsTopics[$g]->getVar('field_name');
490
                            if (1 == $fieldsTopics[$g]->getVar('field_main')) {
491
                                $fieldMainTopic = $fieldNameTopic;
492
                            }
493
                        }
494
                        $getValues .= $this->xc->getXcHandlerLine($topicTableName, "\t\t");
495
                        $getTopicTable = "\${$topicTableName}Handler->get(\$this->getVar('{$fieldName}'))";
496
                        $getValues .= $this->xc->getXcEqualsOperator("\${$topicTableName}Obj", $getTopicTable, null, "\t\t");
497
                        $fMainTopic = "\${$topicTableName}Obj->getVar('{$fieldMainTopic}')";
498
                        $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}']{$spacer}", $fMainTopic, null, "\t\t");
499
                        $helper = 1;
500
                    } else {
501
                        $getValues .= $this->xc->getXcGetVar("ret['{$rpFieldName}']{$spacer}", 'this', $fieldName, false, "\t\t");
502
                    }
503
                    break;
504
            }
505
        }
506
        if ($helper > 0) {
507
            $header .= $this->xc->getXcGetInstance('helper ', "\XoopsModules\\{$ucfModuleDirname}\Helper", "\t\t");
508
        }
509
        if ($utility > 0) {
510
            $header .= $this->xc->getXcEqualsOperator('$utility', "new \XoopsModules\\{$ucfModuleDirname}\Utility()", '',"\t\t");
511
        }
512
        $getValues .= $this->getSimpleString('return $ret;', "\t\t");
513
514
        $ret .= $this->pc->getPhpCodeFunction('getValues' . $ucfTableName, '$keys = null, $format = null, $maxDepth = null', $header . $getValues, 'public ', false, "\t");
515
516
        return $ret;
517
    }
518
519
    /**
520
     * @private function getToArrayInObject
521
     *
522
     * @param $table
523
     *
524
     * @return string
525
     */
526
    private function getToArrayInObject($table)
527
    {
528
        $tableName    = $table->getVar('table_name');
529
        $ucfTableName = ucfirst($tableName);
530
        $multiLineCom = ['Returns an array representation' => 'of the object', '' => '', '@return' => 'array'];
531
        $ret          = $this->pc->getPhpCodeCommentMultiLine($multiLineCom, "\t");
532
533
        $getToArray = $this->pc->getPhpCodeArray('ret', [], false, "\t\t");
534
        $getToArray .= $this->xc->getXcEqualsOperator('$vars', '$this->getVars()', null, "\t\t");
535
        $foreach    = $this->xc->getXcGetVar('ret[$var]', 'this', '"{$var}"', false, "\t\t\t");
536
        $getToArray .= $this->pc->getPhpCodeForeach('vars', true, false, 'var', $foreach, "\t\t");
537
        $getToArray .= $this->getSimpleString('return $ret;', "\t\t");
538
539
        $ret .= $this->pc->getPhpCodeFunction('toArray' . $ucfTableName, '', $getToArray, 'public ', false, "\t");
540
541
        return $ret;
542
    }
543
544
    /**
545
     * @private function getOptionsCheck
546
     *
547
     * @param $table
548
     *
549
     * @return string
550
     */
551
    private function getOptionsCheck($table)
552
    {
553
        $tableName    = $table->getVar('table_name');
554
        $ucfTableName = ucfirst($tableName);
555
        $ret          = $this->pc->getPhpCodeCommentMultiLine(['Get' => 'Options'], "\t");
556
        $getOptions   = $this->pc->getPhpCodeArray('ret', [], false, "\t");
557
558
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
559
        foreach (array_keys($fields) as $f) {
560
            $fieldName    = $fields[$f]->getVar('field_name');
561
            $fieldElement = $fields[$f]->getVar('field_element');
562
563
            $fieldElements  = $this->helper->getHandler('Fieldelements')->get($fieldElement);
564
            $fieldElementId = $fieldElements->getVar('fieldelement_id');
565
            $rpFieldName    = $this->getRightString($fieldName);
566
            if (5 == $fieldElementId) {
567
                $arrayPush  = $this->pc->getPhpCodeArrayType('ret', 'push', "'{$rpFieldName}'", null, false, "\t\t\t");
568
                $getOptions .= $this->pc->getPhpCodeConditions("\$this->getVar('{$fieldName}')", ' == ', '1', $arrayPush, false, "\t\t");
569
            }
570
        }
571
572
        $getOptions .= $this->getSimpleString('return $ret;', "\t\t");
573
574
        $ret .= $this->pc->getPhpCodeFunction('getOptions' . $ucfTableName, '', $getOptions, 'public ', false, "\t");
575
576
        return $ret;
577
    }
578
579
    /**
580
     * @public function render
581
     * @param null
582
     *
583
     * @return bool|string
584
     */
585
    public function render()
586
    {
587
        $module         = $this->getModule();
588
        $table          = $this->getTable();
589
        $filename       = $this->getFileName();
590
        $moduleDirname  = $module->getVar('mod_dirname');
591
        $fields         = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
592
593
        $namespace = $this->pc->getPhpCodeNamespace(['XoopsModules', $moduleDirname]);
594
        $content   = $this->getHeaderFilesComments($module, null, $namespace);
595
        $content   .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname]);
596
        $content   .= $this->getClassObject($module, $table, $fields);
597
598
        $this->create($moduleDirname, 'class', $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
599
600
        return $this->renderFile();
601
    }
602
}
603