ClassFiles::getFunctionForm()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 55
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 46
nc 6
nop 4
dl 0
loc 55
rs 9.1781
c 0
b 0
f 0

How to fix   Long Method   

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\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 ClassFiles.
31
 */
32
class ClassFiles 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
     * @param null
48
     *
49
     * @return ClassFiles
50
     */
51
    public static function getInstance()
52
    {
53
        static $instance = false;
54
        if (!$instance) {
55
            $instance = new self();
56
        }
57
58
        return $instance;
59
    }
60
61
    /**
62
     * @public function write
63
     *
64
     * @param string $module
65
     * @param string $table
66
     * @param mixed  $tables
67
     * @param        $filename
68
     */
69
    public function write($module, $table, $tables, $filename)
70
    {
71
        $this->setModule($module);
72
        $this->setTable($table);
73
        $this->setTables($tables);
74
        $this->setFileName($filename);
75
    }
76
77
    /**
78
     * @private function getInitVar
79
     *
80
     * @param string $fieldName
81
     * @param string $type
82
     *
83
     * @return string
84
     */
85
    private function getInitVar($fieldName, $type = 'INT')
86
    {
87
        $cxc = Tdmcreate\Files\Classes\ClassXoopsCode::getInstance();
88
89
        return $cxc->getClassInitVar($fieldName, $type);
90
    }
91
92
    /**
93
     * @private function getInitVars
94
     *
95
     * @param array $fields
96
     *
97
     * @return string
98
     */
99
    private function getInitVars($fields)
100
    {
101
        $ret = '';
102
        // Creation of the initVar functions list
103
        foreach (array_keys($fields) as $f) {
104
            $fieldName = $fields[$f]->getVar('field_name');
105
            $fieldType = $fields[$f]->getVar('field_type');
106
            switch ($fieldType) {
107
                case 2:
108
                case 3:
109
                case 4:
110
                case 5:
111
                    $ret .= $this->getInitVar($fieldName, 'INT');
112
                    break;
113
                case 6:
114
                    $ret .= $this->getInitVar($fieldName, 'FLOAT');
115
                    break;
116
                case 7:
117
                case 8:
118
                    $ret .= $this->getInitVar($fieldName, 'DECIMAL');
119
                    break;
120
                case 10:
121
                    $ret .= $this->getInitVar($fieldName, 'ENUM');
122
                    break;
123
                case 11:
124
                    $ret .= $this->getInitVar($fieldName, 'EMAIL');
125
                    break;
126
                case 12:
127
                    $ret .= $this->getInitVar($fieldName, 'URL');
128
                    break;
129
                case 13:
130
                case 14:
131
                    $ret .= $this->getInitVar($fieldName, 'TXTBOX');
132
                    break;
133
                case 15:
134
                case 16:
135
                case 17:
136
                case 18:
137
                    $ret .= $this->getInitVar($fieldName, 'TXTAREA');
138
                    break;
139
                case 19:
140
                case 20:
141
                case 21:
142
                case 22:
143
                case 23:
144
                    $ret .= $this->getInitVar($fieldName, 'LTIME');
145
                    break;
146
            }
147
        }
148
149
        return $ret;
150
    }
151
152
    /**
153
     * @private  function getClassObject
154
     * @param $module
155
     * @param $table
156
     * @param $fields
157
     * @return string
158
     */
159
    private function getClassObject($module, $table, $fields)
160
    {
161
        $tc               = Tdmcreate\Helper::getInstance();
162
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
163
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
164
        $moduleDirname    = $module->getVar('mod_dirname');
165
        $tableName        = $table->getVar('table_name');
166
        $ucfTableName     = ucfirst($tableName);
167
        $ret              = $pc->getPhpCodeDefined();
168
        $ret              .= $pc->getPhpCodeCommentMultiLine(['Class Object' => $ucfTableName]);
169
        $cCl              = '';
170
171
        $fieldInForm      = [];
172
        $fieldElementId   = [];
173
        $optionsFieldName = [];
174
        $fieldId          = null;
175
        foreach (array_keys($fields) as $f) {
176
            $fieldName        = $fields[$f]->getVar('field_name');
177
            $fieldElement     = $fields[$f]->getVar('field_element');
178
            $fieldInForm[]    = $fields[$f]->getVar('field_inform');
179
            $fieldElements    = $tc->getHandler('fieldelements')->get($fieldElement);
180
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
181
            $rpFieldName      = $this->getRightString($fieldName);
182
            if (in_array(5, $fieldElementId)) {
183
                //if (count($rpFieldName) % 5) {
184
                    //$optionsFieldName[] = "'" . $rpFieldName . "'";
185
                //} else {
186
                    $optionsFieldName[] = "'" . $rpFieldName . "'\n";
187
                //}
188
            }
189
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
190
                $fieldId = $fieldName;
191
            }
192
        }
193
        if (in_array(5, $fieldElementId) > 1) {
194
            $cCl             .= $pc->getPhpCodeCommentMultiLine(['Options' => '']);
195
            $options         = $pc->getPhpCodeArray('', $optionsFieldName, true);
196
            $cCl             .= $pc->getPhpCodeVariableClass('private', 'options', $options);
197
        }
198
        unset($optionsFieldName);
199
200
        $cCl              .= $pc->getPhpCodeCommentMultiLine(['Constructor' => '', '' => '', '@param' => 'null'], "\t");
201
        $constr           = $this->getInitVars($fields);
202
        $cCl              .= $pc->getPhpCodeFunction('__construct', '', $constr, 'public ', false, "\t");
203
        $arrayGetInstance = ['@static function' => '&getInstance', '' => '', '@param' => 'null'];
204
        $cCl              .= $pc->getPhpCodeCommentMultiLine($arrayGetInstance, "\t");
205
        $getInstance      = $pc->getPhpCodeVariableClass('static', 'instance', 'false', "\t\t");
206
        $instance         = $xc->getXcEqualsOperator('$instance', 'new self()', null, "\t\t\t");
207
        $getInstance      .= $pc->getPhpCodeConditions('!$instance', '', '', $instance, false, "\t\t");
208
        $cCl              .= $pc->getPhpCodeFunction('getInstance', '', $getInstance, 'public static ', false, "\t");
209
210
        $cCl .= $this->getNewInsertId($table);
211
        $cCl .= $this->getFunctionForm($module, $table, $fieldId, $fieldInForm);
212
        $cCl .= $this->getValuesInObject($moduleDirname, $table, $fields);
213
        $cCl .= $this->getToArrayInObject($table);
214
215
        if (in_array(5, $fieldElementId) > 1) {
216
            $cCl .= $this->getOptionsCheck($table);
217
        }
218
        unset($fieldElementId);
219
220
        $ret .= $pc->getPhpCodeClass($ucfTableName, $cCl, '\XoopsObject');
221
222
        return $ret;
223
    }
224
225
    /**
226
     * @private function getNewInsertId
227
     *
228
     * @param $table
229
     *
230
     * @return string
231
     */
232
    private function getNewInsertId($table)
233
    {
234
        $pc            = Tdmcreate\Files\CreatePhpCode::getInstance();
235
        $xc            = Tdmcreate\Files\CreateXoopsCode::getInstance();
236
        $tableName     = $table->getVar('table_name');
237
        $ucfTableName  = ucfirst($tableName);
238
        $ret           = $pc->getPhpCodeCommentMultiLine(['The new inserted' => '$Id', '@return' => 'inserted id'], "\t");
239
        $getInsertedId = $xc->getXcEqualsOperator('$newInsertedId', "\$GLOBALS['xoopsDB']->getInsertId()", null, "\t\t");
240
        $getInsertedId .= $this->getSimpleString('return $newInsertedId;', "\t\t");
241
242
        $ret .= $pc->getPhpCodeFunction('getNewInsertedId' . $ucfTableName, '', $getInsertedId, 'public ', false, "\t");
243
244
        return $ret;
245
    }
246
247
    /**
248
     * @private function getFunctionForm
249
     *
250
     * @param string $module
251
     * @param string $table
252
     *
253
     * @param        $fieldId
254
     * @param        $fieldInForm
255
     * @return string
256
     */
257
    private function getFunctionForm($module, $table, $fieldId, $fieldInForm)
258
    {
259
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
260
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
261
        $cxc              = Tdmcreate\Files\Classes\ClassXoopsCode::getInstance();
262
        $fe               = ClassFormElements::getInstance();
263
        $moduleDirname    = $module->getVar('mod_dirname');
264
        $tableName        = $table->getVar('table_name');
265
        $tableSoleName    = $table->getVar('table_solename');
266
        $tableCategory    = $table->getVar('table_category');
267
        $ucfTableName     = ucfirst($tableName);
268
        $stuTableSoleName = mb_strtoupper($tableSoleName);
269
        $language         = $this->getLanguage($moduleDirname, 'AM');
270
        $fe->initForm($module, $table);
271
        $ret              = $pc->getPhpCodeCommentMultiLine(['@public function' => 'getForm', '@param bool' => '$action', '@return' => '\XoopsThemeForm'], "\t");
272
        $action           = $xc->getXcEqualsOperator('$action', "\$_SERVER['REQUEST_URI']", null, "\t\t\t");
273
        $ucfModuleDirname = ucfirst($moduleDirname);
274
        $getForm          = $xc->getXcGetInstance('helper', "\XoopsModules\\{$ucfModuleDirname}\Helper", "\t\t");
275
        $getForm          .= $pc->getPhpCodeConditions('false', ' === ', '$action', $action, false, "\t\t");
276
        $xUser            = $pc->getPhpCodeGlobals('xoopsUser');
277
        $xModule          = $pc->getPhpCodeGlobals('xoopsModule');
278
        $permString = 'upload_groups';
279
        if (1 != $tableCategory/* && (1 == $tablePermissions)*/) {
280
            $getForm          .= $pc->getPhpCodeCommentLine('Permissions for', 'uploader', "\t\t");
281
            $getForm          .= $xc->getXcXoopsHandler('groupperm', "\t\t");
282
            $getForm          .= $pc->getPhpCodeTernaryOperator('groups', 'is_object(' . $xUser . ')', $xUser . '->getGroups()', 'XOOPS_GROUP_ANONYMOUS', "\t\t");
283
            $checkRight       = $xc->getXcCheckRight('$grouppermHandler', $permString, 32, '$groups', $xModule . '->getVar(\'mid\')', true);
284
            $ternaryOperator  = $pc->getPhpCodeTernaryOperator('permissionUpload', $checkRight, 'true', 'false', "\t\t\t");
285
            $permissionUpload = $xc->getXcEqualsOperator('$permissionUpload', 'true', null, "\t\t\t\t");
286
            $ternOperator     = $pc->getPhpCodeRemoveCarriageReturn($ternaryOperator, '', "\r");
287
            $if               = $pc->getPhpCodeConditions('!' . $xUser . '->isAdmin(' . $xModule . '->mid())', '', '', "\t" . $ternaryOperator, $permissionUpload, "\t\t\t");
288
            $getForm          .= $pc->getPhpCodeConditions($xUser, '', '', $if, $ternOperator, "\t\t");
289
        }
290
        $getForm .= $pc->getPhpCodeCommentLine('Title', '', "\t\t");
291
        $getForm .= $pc->getPhpCodeTernaryOperator('title', '$this->isNew()', "sprintf({$language}{$stuTableSoleName}_ADD)", "sprintf({$language}{$stuTableSoleName}_EDIT)", "\t\t");
292
        $getForm .= $pc->getPhpCodeCommentLine('Get Theme', 'Form', "\t\t");
293
        $getForm .= $xc->getXcXoopsLoad('XoopsFormLoader', "\t\t");
294
        $getForm .= $cxc->getClassXoopsThemeForm('form', 'title', 'form', 'action', 'post');
295
        $getForm .= $cxc->getClassSetExtra('form', "'enctype=\"multipart/form-data\"'");
296
        $getForm .= $fe->renderElements();
297
298
        if (in_array(1, $fieldInForm)) {
299
            if (1 == $table->getVar('table_permissions')) {
300
                $getForm .= $this->getPermissionsInForm($moduleDirname, $fieldId, $tableName);
301
            }
302
        }
303
        $getForm .= $pc->getPhpCodeCommentLine('To Save', '', "\t\t");
304
        //$hiddenSave = $cc->getClassXoopsFormHidden('', "'op'", "'save'", true, false);
305
        $getForm .= $cxc->getClassAddElement('form', "new \XoopsFormHidden('op', 'save')");
306
        $getForm .= $cxc->getClassAddElement('form', "new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)");
307
        $getForm .= $this->getSimpleString('return $form;', "\t\t");
308
309
        $ret .= $pc->getPhpCodeFunction('getForm' . $ucfTableName, '$action = false', $getForm, 'public ', false, "\t");
310
311
        return $ret;
312
    }
313
314
    /**
315
     * @private function getPermissionsInForm
316
     *
317
     * @param string $moduleDirname
318
     * @param string $fieldId
319
     *
320
     * @param $tableName
321
     * @return string
322
     */
323
    private function getPermissionsInForm($moduleDirname, $fieldId, $tableName)
324
    {
325
        $pc                = Tdmcreate\Files\CreatePhpCode::getInstance();
326
        $xc                = Tdmcreate\Files\CreateXoopsCode::getInstance();
327
        $cxc               = Tdmcreate\Files\Classes\ClassXoopsCode::getInstance();
328
        $permissionApprove = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_APPROVE');
329
        $permissionSubmit  = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_SUBMIT');
330
        $permissionView    = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_VIEW');
331
        $ret               = $pc->getPhpCodeCommentLine('Permissions', '', "\t\t");
332
        $ret               .= $xc->getXcXoopsHandler('member', "\t\t");
333
        $ret               .= $xc->getXcEqualsOperator('$groupList', '$memberHandler->getGroupList()', null, "\t\t");
334
        $ret               .= $xc->getXcXoopsHandler('groupperm',  "\t\t");
335
        $ret               .= $pc->getPhpCodeArrayType('fullList', 'keys', 'groupList', null, false, "\t\t");
336
        $fId               = $xc->getXcGetVar('', 'this', $fieldId, true);
337
        $mId               = $xc->getXcGetVar('', "GLOBALS['xoopsModule']", 'mid', true);
338
        $ifGroups          = $xc->getXcGetGroupIds('groupsIdsApprove', 'grouppermHandler', "'{$moduleDirname}_approve_{$tableName}'", $fId, $mId, "\t\t\t");
339
        $ifGroups          .= $pc->getPhpCodeArrayType('groupsIdsApprove', 'values', 'groupsIdsApprove', null, false, "\t\t\t");
340
        $ifGroups          .= $cxc->getClassXoopsFormCheckBox('groupsCanApproveCheckbox', $permissionApprove, "groups_approve_{$tableName}[]", '$groupsIdsApprove', false, "\t\t\t");
341
        $ifGroups          .= $xc->getXcGetGroupIds('groupsIdsSubmit', 'grouppermHandler', "'{$moduleDirname}_submit_{$tableName}'", $fId, $mId, "\t\t\t");
342
        $ifGroups          .= $pc->getPhpCodeArrayType('groupsIdsSubmit', 'values', 'groupsIdsSubmit', null, false, "\t\t\t");
343
        $ifGroups          .= $cxc->getClassXoopsFormCheckBox('groupsCanSubmitCheckbox', $permissionSubmit, "groups_submit_{$tableName}[]", '$groupsIdsSubmit', false, "\t\t\t");
344
        $ifGroups          .= $xc->getXcGetGroupIds('groupsIdsView', 'grouppermHandler', "'{$moduleDirname}_view_{$tableName}'", $fId, $mId, "\t\t\t");
345
        $ifGroups          .= $pc->getPhpCodeArrayType('groupsIdsView', 'values', 'groupsIdsView', null, false, "\t\t\t");
346
        $ifGroups          .= $cxc->getClassXoopsFormCheckBox('groupsCanViewCheckbox', $permissionView, "groups_view_{$tableName}[]", '$groupsIdsView', false, "\t\t\t");
347
348
        $else = $cxc->getClassXoopsFormCheckBox('groupsCanApproveCheckbox', $permissionApprove, "groups_approve_{$tableName}[]", '$fullList', false, "\t\t\t");
349
        $else .= $cxc->getClassXoopsFormCheckBox('groupsCanSubmitCheckbox', $permissionSubmit, "groups_submit_{$tableName}[]", '$fullList', false, "\t\t\t");
350
        $else .= $cxc->getClassXoopsFormCheckBox('groupsCanViewCheckbox', $permissionView, "groups_view_{$tableName}[]", '$fullList', false, "\t\t\t");
351
352
        $ret .= $pc->getPhpCodeConditions('!$this->isNew()', null, null, $ifGroups, $else, "\t\t");
353
        $ret .= $pc->getPhpCodeCommentLine('To Approve', '', "\t\t");
354
        $ret .= $cxc->getClassAddOptionArray('groupsCanApproveCheckbox', '$groupList');
355
        $ret .= $cxc->getClassAddElement('form', '$groupsCanApproveCheckbox');
356
        $ret .= $pc->getPhpCodeCommentLine('To Submit', '', "\t\t");
357
        $ret .= $cxc->getClassAddOptionArray('groupsCanSubmitCheckbox', '$groupList');
358
        $ret .= $cxc->getClassAddElement('form', '$groupsCanSubmitCheckbox');
359
        $ret .= $pc->getPhpCodeCommentLine('To View', '', "\t\t");
360
        $ret .= $cxc->getClassAddOptionArray('groupsCanViewCheckbox', '$groupList');
361
        $ret .= $cxc->getClassAddElement('form', '$groupsCanViewCheckbox');
362
363
        return $ret;
364
    }
365
366
    /**
367
     * @private  function getValuesInObject
368
     *
369
     * @param $moduleDirname
370
     * @param $table
371
     * @param $fields
372
     * @return string
373
     * @internal param $null
374
     */
375
    private function getValuesInObject($moduleDirname, $table, $fields)
376
    {
377
        $tc               = Tdmcreate\Helper::getInstance();
378
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
379
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
380
        $ucfTableName     = ucfirst($table->getVar('table_name'));
381
        $ret              = $pc->getPhpCodeCommentMultiLine(['Get' => 'Values', '@param null $keys' => '', '@param null $format' => '', '@param null$maxDepth' => '', '@return' => 'array'], "\t");
382
        $ucfModuleDirname = ucfirst($moduleDirname);
383
        $getValues        = $xc->getXcGetInstance('helper', "\XoopsModules\\{$ucfModuleDirname}\Helper", "\t\t");
384
        $getValues        .= $xc->getXcEqualsOperator('$ret', '$this->getValues($keys, $format, $maxDepth)', null, "\t\t");
385
        $fieldMainTopic   = null;
386
        foreach (array_keys($fields) as $f) {
387
            $fieldName    = $fields[$f]->getVar('field_name');
388
            $fieldElement = $fields[$f]->getVar('field_element');
389
            $rpFieldName  = $this->getRightString($fieldName);
390
            switch ($fieldElement) {
391
                case 3:
392
                case 4:
393
                    $getValues .= $pc->getPhpCodeStripTags("ret['{$rpFieldName}']", "\$this->getVar('{$fieldName}')", false, "\t\t");
394
                    break;
395
                case 6:
396
                    $getValues .= $xc->getXcEqualsOperator("\$ret['{$rpFieldName}']", "(int)\$this->getVar('{$fieldName}') > 0 ? _YES : _NO", false, "\t\t");
397
                    break;
398
                case 8:
399
                    $getValues .= $xc->getXcXoopsUserUnameFromId("ret['{$rpFieldName}']", "\$this->getVar('{$fieldName}')", "\t\t");
400
                    break;
401
                case 15:
402
                    $getValues .= $xc->getXcFormatTimeStamp("ret['{$rpFieldName}']", "\$this->getVar('{$fieldName}')", 's', "\t\t");
403
                    break;
404
                case 21:
405
                    $getValues .= $xc->getXcFormatTimeStamp("ret['{$rpFieldName}']", "\$this->getVar('{$fieldName}')", 'm', "\t\t");
406
                    break;
407
                default:
408
                    $fieldElements    = $tc->getHandler('fieldelements')->get($fieldElement);
409
                    $fieldElementTid  = $fieldElements->getVar('fieldelement_tid');
410
                    if ((int)$fieldElementTid > 0 ) {
411
                        $fieldElementMid = $fieldElements->getVar('fieldelement_mid');
412
                        $fieldElementName = $fieldElements->getVar('fieldelement_name');
413
                        $fieldNameDesc = 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

413
                        $fieldNameDesc = 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

413
                        $fieldNameDesc = 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

413
                        $fieldNameDesc = mb_substr(/** @scrutinizer ignore-type */ $fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen($fieldElementName));
Loading history...
414
                        $topicTableName = str_replace(': ', '', mb_strtolower($fieldNameDesc));
415
                        $fieldsTopics = $this->getTableFields($fieldElementMid, $fieldElementTid);
416
                        foreach (array_keys($fieldsTopics) as $g) {
417
                            $fieldNameTopic = $fieldsTopics[$g]->getVar('field_name');
418
                            if (1 == $fieldsTopics[$g]->getVar('field_main')) {
419
                                $fieldMainTopic = $fieldNameTopic;
420
                            }
421
                        }
422
                        $getValues .= $xc->getXcHandlerLine($topicTableName, "\t\t");
423
                        $getTopicTable = "\${$topicTableName}Handler->get(\$this->getVar('{$fieldName}'))";
424
                        $getValues .= $xc->getXcEqualsOperator("\${$topicTableName}Obj", $getTopicTable, null, "\t\t");
425
                        $fMainTopic = "\${$topicTableName}Obj->getVar('{$fieldMainTopic}')";
426
                        $getValues .= $xc->getXcEqualsOperator("\$ret['{$rpFieldName}']", $fMainTopic, null, "\t\t");
427
428
                    } else {
429
                        $getValues .= $xc->getXcGetVar("ret['{$rpFieldName}']", 'this', $fieldName, false, "\t\t");
430
                    }
431
                    break;
432
            }
433
        }
434
        $getValues .= $this->getSimpleString('return $ret;', "\t\t");
435
436
        $ret .= $pc->getPhpCodeFunction('getValues' . $ucfTableName, '$keys = null, $format = null, $maxDepth = null', $getValues, 'public ', false, "\t");
437
438
        return $ret;
439
    }
440
441
    /**
442
     * @private function getToArrayInObject
443
     *
444
     * @param $table
445
     *
446
     * @return string
447
     */
448
    private function getToArrayInObject($table)
449
    {
450
        $pc           = Tdmcreate\Files\CreatePhpCode::getInstance();
451
        $xc           = Tdmcreate\Files\CreateXoopsCode::getInstance();
452
        $tableName    = $table->getVar('table_name');
453
        $ucfTableName = ucfirst($tableName);
454
        $multiLineCom = ['Returns an array representation' => 'of the object', '' => '', '@return' => 'array'];
455
        $ret          = $pc->getPhpCodeCommentMultiLine($multiLineCom, "\t");
456
457
        $getToArray = $pc->getPhpCodeArray('ret', [], false, "\t\t");
458
        $getToArray .= $xc->getXcEqualsOperator('$vars', '$this->getVars()', null, "\t\t");
459
        $foreach    = $xc->getXcGetVar('ret[$var]', 'this', '"{$var}"', false, "\t\t\t");
460
        $getToArray .= $pc->getPhpCodeForeach('vars', true, false, 'var', $foreach, "\t\t");
461
        $getToArray .= $this->getSimpleString('return $ret;', "\t\t");
462
463
        $ret .= $pc->getPhpCodeFunction('toArray' . $ucfTableName, '', $getToArray, 'public ', false, "\t");
464
465
        return $ret;
466
    }
467
468
    /**
469
     * @private function getOptionsCheck
470
     *
471
     * @param $table
472
     *
473
     * @return string
474
     */
475
    private function getOptionsCheck($table)
476
    {
477
        $tc           = Tdmcreate\Helper::getInstance();
478
        $pc           = Tdmcreate\Files\CreatePhpCode::getInstance();
479
        $tableName    = $table->getVar('table_name');
480
        $ucfTableName = ucfirst($tableName);
481
        $ret          = $pc->getPhpCodeCommentMultiLine(['Get' => 'Options'], "\t");
482
        $getOptions   = $pc->getPhpCodeArray('ret', [], false, "\t");
483
484
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
485
        foreach (array_keys($fields) as $f) {
486
            $fieldName    = $fields[$f]->getVar('field_name');
487
            $fieldElement = $fields[$f]->getVar('field_element');
488
489
            $fieldElements  = $tc->getHandler('fieldelements')->get($fieldElement);
490
            $fieldElementId = $fieldElements->getVar('fieldelement_id');
491
            $rpFieldName    = $this->getRightString($fieldName);
492
            if (5 == $fieldElementId) {
493
                $arrayPush  = $pc->getPhpCodeArrayType('ret', 'push', "'{$rpFieldName}'", null, false, "\t\t\t");
494
                $getOptions .= $pc->getPhpCodeConditions(1, ' == ', "\$this->getVar('{$fieldName}')", $arrayPush, false, "\t\t");
495
            }
496
        }
497
498
        $getOptions .= $this->getSimpleString('return $ret;', "\t\t");
499
500
        $ret .= $pc->getPhpCodeFunction('getOptions' . $ucfTableName, '', $getOptions, 'public ', false, "\t");
501
502
        return $ret;
503
    }
504
505
    /**
506
     * @public function render
507
     * @param null
508
     *
509
     * @return bool|string
510
     */
511
    public function render()
512
    {
513
        $pc             = Tdmcreate\Files\CreatePhpCode::getInstance();
514
        $module         = $this->getModule();
515
        $table          = $this->getTable();
516
        $filename       = $this->getFileName();
517
        $moduleDirname  = $module->getVar('mod_dirname');
518
        $fields         = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
519
520
        $namespace = $pc->getPhpCodeNamespace(['XoopsModules', $moduleDirname]);
521
        $content   = $this->getHeaderFilesComments($module, null, $namespace);
522
        $content   .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname]);
523
        $content   .= $this->getClassObject($module, $table, $fields);
524
525
        $this->create($moduleDirname, 'class', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
526
527
        return $this->renderFile();
528
    }
529
}
530