Passed
Push — master ( 9ec10c...b9c0c3 )
by Goffy
03:36
created

ClassFiles::getFunctionForm()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 49
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 40
c 0
b 0
f 0
nc 6
nop 4
dl 0
loc 49
rs 8.9688
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
        $getValues        = $this->xc->getXcEqualsOperator('$ret', '$this->getValues($keys, $format, $maxDepth)', null, "\t\t");
395
        $fieldMainTopic   = null;
396
        $helper           = 0;
397
        $utility          = 0;
398
        $header           = '';
399
        $configMaxchar    = 0;
400
        $lenMaxName       = 0;
401
        foreach (array_keys($fields) as $f) {
402
            $fieldName = $fields[$f]->getVar('field_name');
403
            $rpFieldName  = $this->getRightString($fieldName);
404
            $len = strlen($rpFieldName);
405
            if (3 == $fields[$f]->getVar('field_element') || 4 == $fields[$f]->getVar('field_element')) {
406
                $len = $len + strlen('_short');
407
            }
408
            $lenMaxName = max($len, $lenMaxName);
409
        }
410
        foreach (array_keys($fields) as $f) {
411
            $fieldName    = $fields[$f]->getVar('field_name');
412
            $fieldElement = $fields[$f]->getVar('field_element');
413
            $rpFieldName  = $this->getRightString($fieldName);
414
            $spacer = str_repeat(' ', $lenMaxName - strlen($rpFieldName));
415
            switch ($fieldElement) {
416
                case 3:
417
                    $getValues .= $this->pc->getPhpCodeStripTags("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}', 'e')", false, "\t\t");
418
                    if ($configMaxchar == 0) {
419
                        $getValues .= $this->xc->getXcEqualsOperator('$editorMaxchar', $this->xc->getXcGetConfig('editor_maxchar'), false, "\t\t");
420
                        $configMaxchar = 1;
421
                    }
422
                    $truncate  =  "\$utility::truncateHtml(\$ret['{$rpFieldName}'], \$editorMaxchar)";
423
                    $spacer = str_repeat(' ', $lenMaxName - strlen($rpFieldName) - strlen('_short'));
424
                    $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}_short']{$spacer}", $truncate, false, "\t\t");
425
                    $helper = 1;
426
                    $utility = 1;
427
                    break;
428
                case 4:
429
                    $getValues .= $this->xc->getXcGetVar("ret['{$rpFieldName}']{$spacer}", 'this', $fieldName, false, "\t\t", ", 'e'");
430
                    if ($configMaxchar == 0) {
431
                        $getValues .= $this->xc->getXcEqualsOperator('$editorMaxchar', $this->xc->getXcGetConfig('editor_maxchar'), false, "\t\t");
432
                        $configMaxchar = 1;
433
                    }
434
                    $truncate  =  "\$utility::truncateHtml(\$ret['{$rpFieldName}'], \$editorMaxchar)";
435
                    $spacer = str_repeat(' ', $lenMaxName - strlen($rpFieldName) - strlen('_short'));
436
                    $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}_short']{$spacer}", $truncate, false, "\t\t");
437
                    $helper = 1;
438
                    $utility = 1;
439
                    break;
440
                case 6:
441
                    $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}']{$spacer}", "(int)\$this->getVar('{$fieldName}') > 0 ? _YES : _NO", false, "\t\t");
442
                    break;
443
                case 8:
444
                    $getValues .= $this->xc->getXcXoopsUserUnameFromId("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}')", "\t\t");
445
                    break;
446
                case 15:
447
                    $getValues .= $this->xc->getXcFormatTimeStamp("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}')", 's', "\t\t");
448
                    break;
449
                case 21:
450
                    $getValues .= $this->xc->getXcFormatTimeStamp("ret['{$rpFieldName}']{$spacer}", "\$this->getVar('{$fieldName}')", 'm', "\t\t");
451
                    break;
452
                default:
453
                    $fieldElements    = $this->helper->getHandler('Fieldelements')->get($fieldElement);
454
                    $fieldElementTid  = $fieldElements->getVar('fieldelement_tid');
455
                    if ((int)$fieldElementTid > 0 ) {
456
                        $fieldElementMid = $fieldElements->getVar('fieldelement_mid');
457
                        $fieldElementName = (string)$fieldElements->getVar('fieldelement_name');
458
                        $fieldNameDesc = mb_substr($fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen($fieldElementName));
459
                        $topicTableName = str_replace(': ', '', mb_strtolower($fieldNameDesc));
460
                        $fieldsTopics = $this->getTableFields($fieldElementMid, $fieldElementTid);
461
                        foreach (array_keys($fieldsTopics) as $g) {
462
                            $fieldNameTopic = $fieldsTopics[$g]->getVar('field_name');
463
                            if (1 == $fieldsTopics[$g]->getVar('field_main')) {
464
                                $fieldMainTopic = $fieldNameTopic;
465
                            }
466
                        }
467
                        $getValues .= $this->xc->getXcHandlerLine($topicTableName, "\t\t");
468
                        $getTopicTable = "\${$topicTableName}Handler->get(\$this->getVar('{$fieldName}'))";
469
                        $getValues .= $this->xc->getXcEqualsOperator("\${$topicTableName}Obj", $getTopicTable, null, "\t\t");
470
                        $fMainTopic = "\${$topicTableName}Obj->getVar('{$fieldMainTopic}')";
471
                        $getValues .= $this->xc->getXcEqualsOperator("\$ret['{$rpFieldName}']{$spacer}", $fMainTopic, null, "\t\t");
472
                        $helper = 1;
473
                    } else {
474
                        $getValues .= $this->xc->getXcGetVar("ret['{$rpFieldName}']{$spacer}", 'this', $fieldName, false, "\t\t");
475
                    }
476
                    break;
477
            }
478
        }
479
        if ($helper > 0) {
480
            $header .= $this->xc->getXcGetInstance('helper ', "\XoopsModules\\{$ucfModuleDirname}\Helper", "\t\t");
481
        }
482
        if ($utility > 0) {
483
            $header .= $this->xc->getXcEqualsOperator('$utility', "new \XoopsModules\\{$ucfModuleDirname}\Utility()", '',"\t\t");
484
        }
485
        $getValues .= $this->getSimpleString('return $ret;', "\t\t");
486
487
        $ret .= $this->pc->getPhpCodeFunction('getValues' . $ucfTableName, '$keys = null, $format = null, $maxDepth = null', $header . $getValues, 'public ', false, "\t");
488
489
        return $ret;
490
    }
491
492
    /**
493
     * @private function getToArrayInObject
494
     *
495
     * @param $table
496
     *
497
     * @return string
498
     */
499
    private function getToArrayInObject($table)
500
    {
501
        $tableName    = $table->getVar('table_name');
502
        $ucfTableName = ucfirst($tableName);
503
        $multiLineCom = ['Returns an array representation' => 'of the object', '' => '', '@return' => 'array'];
504
        $ret          = $this->pc->getPhpCodeCommentMultiLine($multiLineCom, "\t");
505
506
        $getToArray = $this->pc->getPhpCodeArray('ret', [], false, "\t\t");
507
        $getToArray .= $this->xc->getXcEqualsOperator('$vars', '$this->getVars()', null, "\t\t");
508
        $foreach    = $this->xc->getXcGetVar('ret[$var]', 'this', '"{$var}"', false, "\t\t\t");
509
        $getToArray .= $this->pc->getPhpCodeForeach('vars', true, false, 'var', $foreach, "\t\t");
510
        $getToArray .= $this->getSimpleString('return $ret;', "\t\t");
511
512
        $ret .= $this->pc->getPhpCodeFunction('toArray' . $ucfTableName, '', $getToArray, 'public ', false, "\t");
513
514
        return $ret;
515
    }
516
517
    /**
518
     * @private function getOptionsCheck
519
     *
520
     * @param $table
521
     *
522
     * @return string
523
     */
524
    private function getOptionsCheck($table)
525
    {
526
        $tableName    = $table->getVar('table_name');
527
        $ucfTableName = ucfirst($tableName);
528
        $ret          = $this->pc->getPhpCodeCommentMultiLine(['Get' => 'Options'], "\t");
529
        $getOptions   = $this->pc->getPhpCodeArray('ret', [], false, "\t");
530
531
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
532
        foreach (array_keys($fields) as $f) {
533
            $fieldName    = $fields[$f]->getVar('field_name');
534
            $fieldElement = $fields[$f]->getVar('field_element');
535
536
            $fieldElements  = $this->helper->getHandler('Fieldelements')->get($fieldElement);
537
            $fieldElementId = $fieldElements->getVar('fieldelement_id');
538
            $rpFieldName    = $this->getRightString($fieldName);
539
            if (5 == $fieldElementId) {
540
                $arrayPush  = $this->pc->getPhpCodeArrayType('ret', 'push', "'{$rpFieldName}'", null, false, "\t\t\t");
541
                $getOptions .= $this->pc->getPhpCodeConditions("\$this->getVar('{$fieldName}')", ' == ', '1', $arrayPush, false, "\t\t");
542
            }
543
        }
544
545
        $getOptions .= $this->getSimpleString('return $ret;', "\t\t");
546
547
        $ret .= $this->pc->getPhpCodeFunction('getOptions' . $ucfTableName, '', $getOptions, 'public ', false, "\t");
548
549
        return $ret;
550
    }
551
552
    /**
553
     * @public function render
554
     * @param null
555
     *
556
     * @return bool|string
557
     */
558
    public function render()
559
    {
560
        $module         = $this->getModule();
561
        $table          = $this->getTable();
562
        $filename       = $this->getFileName();
563
        $moduleDirname  = $module->getVar('mod_dirname');
564
        $fields         = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
565
566
        $namespace = $this->pc->getPhpCodeNamespace(['XoopsModules', $moduleDirname]);
567
        $content   = $this->getHeaderFilesComments($module, null, $namespace);
568
        $content   .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname]);
569
        $content   .= $this->getClassObject($module, $table, $fields);
570
571
        $this->create($moduleDirname, 'class', $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
572
573
        return $this->renderFile();
574
    }
575
}
576