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

UserPages   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 586
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 333
dl 0
loc 586
rs 9.1199
c 0
b 0
f 0
wmc 41

14 Methods

Rating   Name   Duplication   Size   Complexity  
F getUserPagesSave() 0 111 14
B getUserPagesList() 0 81 4
A getUserPagesDelete() 0 12 1
A getUserPagesNew() 0 8 1
A getUserPagesBroken() 0 41 2
A getUserPagesEdit() 0 12 1
A write() 0 5 1
A getUserPagesFooter() 0 26 2
A getPermissionsSave() 0 9 1
A getInstance() 0 8 2
A getUserPagesSwitch() 0 16 3
A __construct() 0 6 1
A getUserPagesHeader() 0 35 2
B render() 0 48 6

How to fix   Complexity   

Complex Class

Complex classes like UserPages often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UserPages, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files\User;
4
5
use XoopsModules\Modulebuilder;
6
use XoopsModules\Modulebuilder\{
7
    Files,
8
    Constants
9
};
10
11
/*
12
 You may not change or alter any portion of this comment or credits
13
 of supporting developers from this source code or any supporting source code
14
 which is considered copyrighted (c) material of the original comment or credit authors.
15
16
 This program is distributed in the hope that it will be useful,
17
 but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
 */
20
/**
21
 * modulebuilder module.
22
 *
23
 * @copyright       XOOPS Project (https://xoops.org)
24
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
25
 *
26
 * @since           2.5.0
27
 *
28
 * @author          Txmod Xoops http://www.txmodxoops.org
29
 *
30
 */
31
32
/**
33
 * Class UserPages.
34
 */
35
class UserPages extends Files\CreateFile
36
{
37
    /**
38
     * @var mixed
39
     */
40
    private $uxc = null;
41
42
    /**
43
     * @var mixed
44
     */
45
    private $xc = null;
46
47
    /**
48
     * @var mixed
49
     */
50
    private $pc = null;
51
52
    /**
53
     * @public function constructor
54
     * @param null
55
     */
56
    public function __construct()
57
    {
58
        parent::__construct();
59
        $this->xc  = Modulebuilder\Files\CreateXoopsCode::getInstance();
60
        $this->pc  = Modulebuilder\Files\CreatePhpCode::getInstance();
61
        $this->uxc = Modulebuilder\Files\User\UserXoopsCode::getInstance();
62
    }
63
64
    /**
65
     * @static function getInstance
66
     * @param null
67
     * @return UserPages
68
     */
69
    public static function getInstance()
70
    {
71
        static $instance = false;
72
        if (!$instance) {
73
            $instance = new self();
74
        }
75
76
        return $instance;
77
    }
78
79
    /**
80
     * @public function write
81
     * @param $module
82
     * @param $table
83
     * @param $filename
84
     */
85
    public function write($module, $table, $filename)
86
    {
87
        $this->setModule($module);
88
        $this->setTable($table);
89
        $this->setFileName($filename);
90
    }
91
92
    /**
93
     * @private function getUserPagesHeader
94
     * @param $moduleDirname
95
     * @param $tableName
96
     * @param $fieldId
97
     * @param $tablePermissions
98
     * @return string
99
     */
100
    private function getUserPagesHeader($moduleDirname, $tableName, $fieldId, $tablePermissions)
101
    {
102
        $stuModuleDirname = \mb_strtoupper($moduleDirname);
103
        $ccFieldId        = $this->getCamelCase($fieldId, false, true);
104
105
        $ret       = $this->pc->getPhpCodeUseNamespace(['Xmf', 'Request'], '', '');
106
        $ret       .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', '');
107
        $ret       .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants'], '', '');
108
        $ret       .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Common']);
109
        $ret       .= $this->getInclude();
110
        $ret       .= $this->uxc->getUserTplMain($moduleDirname, $tableName);
111
        $ret       .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
112
        $ret       .= $this->pc->getPhpCodeBlankLine();
113
        $ret       .= $this->xc->getXcXoopsRequest('op   ', 'op', 'list', 'Cmd');
114
        $ret       .= $this->xc->getXcXoopsRequest('start', 'start', '0', 'Int');
115
        $userpager = $this->xc->getXcGetConfig('userpager');
116
        $ret       .= $this->xc->getXcXoopsRequest('limit', 'limit', $userpager, 'Int');
117
        $ret       .= $this->xc->getXcXoopsRequest($ccFieldId, $fieldId, '0', 'Int');
118
        $ret       .= $this->pc->getPhpCodeBlankLine();
119
        $ret       .= $this->pc->getPhpCodeCommentLine('Define Stylesheet');
120
        $ret       .= $this->xc->getXcXoThemeAddStylesheet();
121
        $ret       .= $this->pc->getPhpCodeBlankLine();
122
        $ret       .= $this->xc->getXcXoopsTplAssign('xoops_icons32_url', 'XOOPS_ICONS32_URL');
123
        $ret       .= $this->xc->getXcXoopsTplAssign("{$moduleDirname}_url", "{$stuModuleDirname}_URL");
124
        $ret       .= $this->pc->getPhpCodeBlankLine();
125
        $ret       .= $this->pc->getPhpCodeArray('keywords', null, false, '');
126
        $ret       .= $this->pc->getPhpCodeBlankLine();
127
        if (1 == $tablePermissions) {
128
            $ret .= $this->xc->getXcEqualsOperator('$permEdit', '$permissionsHandler->getPermGlobalSubmit()');
129
            $ret .= $this->xc->getXcXoopsTplAssign("permEdit", '$permEdit');
130
        }
131
        $ret       .= $this->xc->getXcXoopsTplAssign("showItem", "\${$ccFieldId} > 0");
132
        $ret       .= $this->pc->getPhpCodeBlankLine();
133
134
        return $ret;
135
    }
136
137
    /**
138
     * @private function getUserPagesList
139
     * @param $moduleDirname
140
     * @param $tableName
141
     * @param $fieldId
142
     * @param $fieldMain
143
     * @param $tableRate
144
     * @param string $t
145
     * @return string
146
     */
147
    private function getUserPagesList($moduleDirname, $tableName, $fieldId, $fieldMain, $tableRate, $fieldReads, $t = '')
148
    {
149
        $ucfTableName     = \ucfirst($tableName);
150
        $stuTableName     = \mb_strtoupper($tableName);
151
        $ccFieldId        = $this->getCamelCase($fieldId, false, true);
152
        $ucfFieldId       = \ucfirst($ccFieldId);
0 ignored issues
show
Unused Code introduced by
The assignment to $ucfFieldId is dead and can be removed.
Loading history...
153
        $ccFieldMain      = $this->getCamelCase($fieldMain, false, true);
154
        $ccFieldReads     = $this->getCamelCase($fieldReads, false, true);
155
        $stuModuleDirname = \mb_strtoupper($moduleDirname);
156
157
        $ret = '';
158
        if ($tableRate) {
159
            $varRate = '$ratingbars';
160
            $ret .= $this->xc->getXcEqualsOperator($varRate, '(int)' . $this->xc->getXcGetConfig('ratingbars'),'', $t);
161
            $contIf = $this->xc->getXcXoThemeAddStylesheet("{$stuModuleDirname}_URL . '/assets/css/rating.css'", $t . "\t", false);
162
            $contIf .= $this->xc->getXcXoopsTplAssign('rating', $varRate, true, $t . "\t");
163
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_5stars', "(Constants::RATING_5STARS === {$varRate})", true, $t . "\t");
164
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_10stars', "(Constants::RATING_10STARS === {$varRate})", true, $t . "\t");
165
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_10num', "(Constants::RATING_10NUM === {$varRate})", true, $t . "\t");
166
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_likes', "(Constants::RATING_LIKES === {$varRate})", true, $t . "\t");
167
            $contIf .= $this->xc->getXcXoopsTplAssign('itemid', "'{$fieldId}'", true, $t . "\t");
168
            $contIf .= $this->xc->getXcXoopsTplAssign($moduleDirname . '_icon_url_16', "{$stuModuleDirname}_URL . '/' . \$modPathIcon16", true, $t . "\t");
169
            $ret .= $this->pc->getPhpCodeConditions($varRate, ' > ', '0', $contIf, false, $t);
170
        }
171
        $critName  = 'cr' . $ucfTableName;
172
        $ret       .= $this->xc->getXcCriteriaCompo($critName, $t);
173
        $crit      = $this->xc->getXcCriteria('', "'{$fieldId}'", "\${$ccFieldId}",'',true);
174
        $contIf    = $this->xc->getXcCriteriaAdd($critName, $crit, $t . "\t");
175
        $ret       .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $contIf, false, $t);
176
        $ret       .= $this->xc->getXcHandlerCountClear($tableName . 'Count', $tableName, '$' . $critName, $t);
177
        $ret       .= $this->xc->getXcXoopsTplAssign($tableName . 'Count', "\${$tableName}Count", '', $t);
178
        $ret       .= $this->xc->getXcCriteriaSetStart($critName, '$start', $t);
179
        $ret       .= $this->xc->getXcCriteriaSetLimit($critName, '$limit', $t);
180
        $ret       .= $this->xc->getXcHandlerAllClear($tableName . 'All', $tableName, '$' . $critName, $t);
181
        $condIf    = $this->pc->getPhpCodeArray($tableName, null, false, $t . "\t");
182
        $condIf    .= $this->xc->getXcEqualsOperator("\${$ccFieldMain}", "''",'', $t . "\t");
183
        $condIf    .= $this->pc->getPhpCodeCommentLine('Get All', $ucfTableName, $t . "\t");
184
        $foreach   = $this->xc->getXcGetValues($tableName, $tableName . '[$i]', 'i', false, $t . "\t\t");
185
        $foreach   .= $this->xc->getXcGetVar($ccFieldMain, "{$tableName}All[\$i]", $fieldMain, false, $t . "\t\t");
186
        $foreach   .= $this->xc->getXcEqualsOperator('$keywords[$i]', "\${$ccFieldMain}",'', $t . "\t\t");
187
        if ($tableRate) {
188
            $itemId   = $this->xc->getXcGetVar($ccFieldId, "{$tableName}All[\$i]", $fieldId, true);
189
            $const  = $this->xc->getXcGetConstants('TABLE_' . $stuTableName);
190
            $foreach .= $this->xc->getXcEqualsOperator("\${$tableName}[\$i]['rating']", "\$ratingsHandler->getItemRating({$itemId}, {$const})",'', $t . "\t\t");
191
        }
192
        $condIf    .= $this->pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $foreach, $t . "\t");
193
        $condIf    .= $this->xc->getXcXoopsTplAssign($tableName, "\${$tableName}", true, $t . "\t");
194
        $condIf    .= $this->pc->getPhpCodeUnset($tableName, $t . "\t");
195
        $condIf    .= $this->xc->getXcPageNav($tableName, $t . "\t");
196
        $config    = $this->xc->getXcGetConfig('table_type');
197
        $condIf    .= $this->xc->getXcXoopsTplAssign('table_type', $config, true, $t . "\t");
198
        $config    = $this->xc->getXcGetConfig('panel_type');
199
        $condIf    .= $this->xc->getXcXoopsTplAssign('panel_type', $config, true, $t . "\t");
200
        $divideby  = $this->xc->getXcGetConfig('divideby');
201
        $condIf    .= $this->xc->getXcXoopsTplAssign('divideby', $divideby, true, $t . "\t");
202
        $numbCol   = $this->xc->getXcGetConfig('numb_col');
203
        $condIf    .= $this->xc->getXcXoopsTplAssign('numb_col', $numbCol, true, $t . "\t");
204
        $stripTags      = $this->pc->getPhpCodeStripTags('', "\${$ccFieldMain} . ' - ' . " . "\$GLOBALS['xoopsModule']->getVar('name')", true);
205
        $condIf2         = $this->xc->getXcXoopsTplAssign('xoops_pagetitle', $stripTags, true, $t . "\t\t");
206
        $condIf       .= $this->pc->getPhpCodeConditions("'show' == \$op && '' != \${$ccFieldMain}", '', "", $condIf2, false, $t . "\t");
207
208
        if ('' !== $fieldReads) {
209
            $condIf3 = $this->xc->getXcHandlerGetObj($tableName, $ccFieldId, $t . "\t\t");
210
211
212
            $getVar = $this->xc->getXcGetVar('', "{$tableName}Obj", $fieldReads, true);
213
            $condIf3 .= $this->xc->getXcEqualsOperator("\${$ccFieldReads}", "(int)" . $getVar . ' + 1', false, $t . "\t\t");
214
            $condIf3 .= $this->xc->getXcSetVarObj($tableName, $fieldReads, "\${$ccFieldReads}", $t . "\t\t");
215
            $condIf3 .= $this->pc->getPhpCodeCommentLine('Insert Data', null, $t . "\t\t");
216
            $insert = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj', 'Handler');
217
            $condIf3 .= $this->getSimpleString($insert .';',$t . "\t\t");
218
            //$contentInsert = $this->xc->getXcRedirectHeader("'{$tableName}.php?op=list&{$fieldId}=' . \${$ccFieldId}", '', '5', "\${$tableName}Obj->getHtmlErrors()", false, $t . "\t\t\t");
219
            //$condIf3 .= $this->pc->getPhpCodeConditions('!' . $insert, '', '', $contentInsert, false, $t . "\t\t");
220
            $condIf .= $this->pc->getPhpCodeConditions("'show' == \$op", '', "", $condIf3, false, $t . "\t");
221
222
        }
223
224
225
        $ret       .= $this->pc->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $condIf, false, $t);
226
227
        return $ret;
228
    }
229
230
    /**
231
     * @public function getUserSubmitSave
232
     * @param string $moduleDirname
233
     * @param        $fields
234
     * @param string $tableName
235
     * @param        $tableSoleName
236
     * @param $tablePermissions
237
     * @param $tableNotifications
238
     * @param        $language
239
     * @param string $t
240
     * @return string
241
     */
242
    public function getUserPagesSave($moduleDirname, $fields, $tableName, $tableSoleName, $tablePermissions, $tableNotifications, $language, $t = '')
243
    {
244
        $ucfTableName  = \ucfirst($tableName);
245
        $countUploader = 0;
246
        $fieldId       = '';
247
        $ccFieldId     = '';
248
        $fieldMain     = '';
249
        $fieldStatus   = '';
250
        $ucfFieldId    = '';
251
        $ccFieldMain   = '';
252
        $ccFieldStatus = '';
253
        foreach (\array_keys($fields) as $f) {
254
            $fieldName = $fields[$f]->getVar('field_name');
255
            if (0 == $f) {
256
                $fieldId   = $fieldName;
257
                $ccFieldId = $this->getCamelCase($fieldId, false, true);
258
                $ucfFieldId = \ucfirst($ccFieldId);
259
            }
260
            if ($fields[$f]->getVar('field_element') >= Constants::FIELD_ELE_IMAGELIST && $fields[$f]->getVar('field_element') <= Constants::FIELD_ELE_UPLOADFILE) {
261
                $countUploader++;
262
            }
263
            if (1 == $fields[$f]->getVar('field_main')) {
264
                $fieldMain    = $fieldName; // fieldMain = fields parameters main field
265
                $ccFieldMain  = $this->getCamelCase($fieldMain, false, true);
266
            }
267
            if ($fields[$f]->getVar('field_element') == Constants::FIELD_ELE_SELECTSTATUS) {
268
                $fieldStatus   = $fieldName;
269
                $ccFieldStatus = $this->getCamelCase($fieldStatus, false, true);
270
            }
271
        }
272
273
        $ret                = $this->pc->getPhpCodeCommentLine('Security Check', '', $t);
274
        $xoopsSecurityCheck = $this->xc->getXcXoopsSecurityCheck();
275
        $securityError      = $this->xc->getXcXoopsSecurityErrors();
276
        $implode            = $this->pc->getPhpCodeImplode(',', $securityError);
277
        $redirectError      = $this->xc->getXcRedirectHeader($tableName, '', '3', $implode, true, $t . "\t");
278
        $ret                .= $this->pc->getPhpCodeConditions('!' . $xoopsSecurityCheck, '', '', $redirectError, false, $t);
279
        $ret                .= $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
280
        $contIf             = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
281
        $ret                .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
282
        $getObj             = $this->xc->getXcHandlerGetObj($tableName, $ccFieldId,  $t . "\t");
283
        $createObj          = $this->xc->getXcHandlerCreateObj($tableName, $t . "\t");
284
        $ret                .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $getObj, $createObj, $t);
285
        $ret                .= $this->xc->getXcSaveElements($moduleDirname, $tableName, $tableSoleName, $fields, $t);
286
        $ret                .= $this->pc->getPhpCodeCommentLine('Insert Data', null, $t);
287
        $insert             = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj', 'Handler');
288
289
        $contentInsert = '';
290
        if (1 == $tableNotifications || $countUploader > 0) {
291
            $contentInsert .= $this->pc->getPhpCodeTernaryOperator("new{$ucfFieldId}", "\${$ccFieldId} > 0", "\${$ccFieldId}", "\${$tableName}Obj->getNewInsertedId{$ucfTableName}()", $t . "\t");
292
        }
293
294
        if (1 == $tablePermissions) {
295
            $contentInsert .= $this->xc->getXcXoopsHandler('groupperm', $t . "\t");
296
            $contentInsert .= $this->xc->getXcEqualsOperator('$mid', "\$GLOBALS['xoopsModule']->getVar('mid')", null, $t . "\t");
297
            $contentInsert .= $this->getPermissionsSave($moduleDirname, $ucfFieldId,'view_' . $tableName);
298
            $contentInsert .= $this->getPermissionsSave($moduleDirname, $ucfFieldId, 'submit_' . $tableName);
299
            $contentInsert .= $this->getPermissionsSave($moduleDirname, $ucfFieldId, 'approve_' . $tableName);
300
        }
301
302
        if (1 == $tableNotifications) {
303
            $contentInsert .= $this->pc->getPhpCodeCommentLine('Handle notification', null, $t . "\t");
304
            $contentInsert .= $this->xc->getXcGetVar($ccFieldMain, "{$tableName}Obj", $fieldMain, false, $t. "\t");
305
            if ('' !== $fieldStatus) {
306
                $contentInsert .= $this->xc->getXcGetVar($ccFieldStatus, "{$tableName}Obj", $fieldStatus, false, $t . "\t");
307
            }
308
            $contentInsert .= $this->pc->getPhpCodeArray('tags', [], false, $t . "\t");
309
            $contentInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_NAME']", "\${$ccFieldMain}", '', $t . "\t");
310
            $url = "XOOPS_URL . '/modules/{$moduleDirname}/{$tableName}.php?op=show&{$fieldId}=' . \${$ccFieldId}";
311
            $contentInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_URL'] ", $url, '', $t . "\t");
312
            $contentInsert .= $this->xc->getXcXoopsHandler('notification', $t . "\t");
313
            if ('' === $fieldStatus) {
314
                $not2If        = $this->pc->getPhpCodeCommentLine('Event modify notification', null, $t . "\t\t");
315
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_modify', \$tags);", $t . "\t\t");
316
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_modify', \$tags);", $t . "\t\t");
317
                $not2Else      = $this->pc->getPhpCodeCommentLine('Event new notification', null, $t . "\t\t");
318
                $not2Else      .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_new', \$tags);", $t . "\t\t");
319
                //$not2Else      .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_new', \$tags);", $t . "\t\t");
320
                $not1Else      = $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $not2If, $not2Else, $t . "\t");
321
                $contentInsert .= $not1Else;
322
            } else {
323
                $not1If        = $this->pc->getPhpCodeCommentLine('Event approve notification', null, $t . "\t\t");
324
                $not1If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_approve', \$tags);", $t . "\t\t");
325
                $not1If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_approve', \$tags);", $t . "\t\t");
326
                $not2If        = $this->pc->getPhpCodeCommentLine('Event modify notification', null, $t . "\t\t\t");
327
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_modify', \$tags);", $t . "\t\t\t");
328
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_modify', \$tags);", $t . "\t\t\t");
329
                $not2Else      = $this->pc->getPhpCodeCommentLine('Event new notification', null, $t . "\t\t\t");
330
                $not2Else      .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_new', \$tags);", $t . "\t\t\t");
331
                $not1Else      = $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $not2If, $not2Else, $t . "\t\t");
332
                $contentInsert .= $this->pc->getPhpCodeConditions("\${$ccFieldStatus}", ' == ', $this->xc->getXcGetConstants('STATUS_SUBMITTED'), $not1If, $not1Else, $t . "\t");
333
            }
334
        }
335
336
        $contentInsert .= $this->pc->getPhpCodeCommentLine('redirect after insert', null, $t . "\t");
337
        if ($countUploader > 0) {
338
            $errIf     = $this->xc->getXcRedirectHeader("'{$tableName}.php?op=edit&{$fieldId}=' . \$new{$ucfFieldId}", '', '5', '$uploaderErrors', false, $t . "\t\t");
339
            $errElse   = $this->xc->getXcRedirectHeader($tableName, '?op=list', '2', "{$language}FORM_OK", true, $t . "\t\t");
340
            $confirmOk = $this->pc->getPhpCodeConditions('$uploaderErrors', ' !== ', "''", $errIf, $errElse, $t . "\t");
341
        } else {
342
            $confirmOk = $this->xc->getXcRedirectHeader('index', '', '2', "{$language}FORM_OK", true, $t . "\t");
343
        }
344
        $contentInsert .= $confirmOk;
345
        $ret           .= $this->pc->getPhpCodeConditions($insert, '', '', $contentInsert, false, $t);
346
347
        $ret .= $this->pc->getPhpCodeCommentLine('Get Form Error', null, $t);
348
        $ret .= $this->xc->getXcXoopsTplAssign('error', "\${$tableName}Obj->getHtmlErrors()", true, $t);
349
        $ret .= $this->xc->getXcGetForm('form', $tableName, 'Obj', $t);
350
        $ret .= $this->xc->getXcXoopsTplAssign('form', '$form->render()', true, $t);
351
352
        return $ret;
353
    }
354
355
    /**
356
     * @private function getPermissionsSave
357
     * @param $moduleDirname
358
     * @param $ucfFieldId
359
     * @param string $perm
360
     *
361
     * @return string
362
     */
363
    private function getPermissionsSave($moduleDirname, $ucfFieldId, $perm = 'view')
364
    {
365
        $ret     = $this->pc->getPhpCodeCommentLine('Permission to', $perm, "\t\t\t");
366
        $ret     .= $this->xc->getXcDeleteRight('grouppermHandler', "{$moduleDirname}_{$perm}", '$mid', "\$new{$ucfFieldId}", false, "\t\t\t");
367
        $content = $this->xc->getXcAddRight('grouppermHandler', "{$moduleDirname}_{$perm}", "\$new{$ucfFieldId}", '$onegroupId', '$mid', false, "\t\t\t\t\t");
368
        $foreach = $this->pc->getPhpCodeForeach("_POST['groups_{$perm}']", false, false, 'onegroupId', $content, "\t\t\t\t");
369
        $ret     .= $this->pc->getPhpCodeConditions("isset(\$_POST['groups_{$perm}'])", null, null, $foreach, false, "\t\t\t");
370
371
        return $ret;
372
    }
373
374
    /**
375
     * @public function getUserPagesNew
376
     * @param        $tableName
377
     * @param string $t
378
     * @return string
379
     */
380
    public function getUserPagesNew($tableName, $t = '')
381
    {
382
        $ret    = $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
383
        $contIf = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
384
        $ret    .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
385
        $ret    .= $this->xc->getXcCommonPagesNew($tableName, $t);
386
387
        return $ret;
388
    }
389
390
    /**
391
     * @public function getUserPagesEdit
392
     * @param        $tableName
393
     * @param $fieldId
394
     * @param        $language
395
     * @param string $t
396
     * @return string
397
     */
398
    public function getUserPagesEdit($tableName, $fieldId, $language, $t = '')
399
    {
400
        $ccFieldId = $this->getCamelCase($fieldId, false, true);
401
        $ret       = $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
402
        $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
403
        $ret       .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
404
        $ret       .= $this->pc->getPhpCodeCommentLine('Check params', '', $t);
405
        $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, "{$language}INVALID_PARAM", true, $t . "\t");
406
        $ret       .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' == ', '0', $contIf, false, $t);
407
        $ret       .= $this->xc->getXcCommonPagesEdit($tableName, $ccFieldId, $t);
408
409
        return $ret;
410
    }
411
412
    /**
413
     * @private function getUserPagesDelete
414
     * @param        $tableName
415
     * @param $tableSoleName
416
     * @param        $language
417
     * @param        $fieldId
418
     * @param        $fieldMain
419
     * @param $tableNotifications
420
     * @param string $t
421
     * @return string
422
     */
423
    private function getUserPagesDelete($tableName, $tableSoleName, $language, $fieldId, $fieldMain, $tableNotifications, $t = '')
424
    {
425
        $ccFieldId = $this->getCamelCase($fieldId, false, true);
426
        $ret       = $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
427
        $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
428
        $ret       .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
429
        $ret       .= $this->pc->getPhpCodeCommentLine('Check params', '', $t);
430
        $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, "{$language}INVALID_PARAM", true, $t . "\t");
431
        $ret       .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' == ', '0', $contIf, false, $t);
432
        $ret       .= $this->xc->getXcCommonPagesDelete($language, $tableName, $tableSoleName, $fieldId, $fieldMain, $tableNotifications, $t);
433
434
        return $ret;
435
    }
436
437
    /**
438
     * @private function getUserPagesBroken
439
     * @param        $language
440
     * @param        $moduleDirname
441
     * @param        $tableName
442
     * @param        $tableSoleName
443
     * @param        $fieldId
444
     * @param $fieldStatus
445
     * @param        $fieldMain
446
     * @param        $tableNotifications
447
     * @param string $t
448
     * @return string
449
     */
450
    private function getUserPagesBroken($language, $moduleDirname, $tableName, $tableSoleName, $fieldId, $fieldStatus, $fieldMain, $tableNotifications, $t = '')
451
    {
452
        $ccFieldId   = $this->getCamelCase($fieldId, false, true);
453
        $ccFieldMain = $this->getCamelCase($fieldMain, false, true);
454
455
        $ret    = $this->pc->getPhpCodeCommentLine('Check params', '', $t);
456
        $contIf = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, "{$language}INVALID_PARAM", true, $t . "\t");
457
        $ret    .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' == ', '0', $contIf, false, $t);
458
459
        $ret                  .= $this->xc->getXcHandlerGet($tableName, $ccFieldId, 'Obj', $tableName . 'Handler', false, $t);
460
        $ret                  .= $this->xc->getXcGetVar($ccFieldMain, "{$tableName}Obj", $fieldMain, false, $t);
461
        $reqOk                = "_REQUEST['ok']";
462
        $isset                = $this->pc->getPhpCodeIsset($reqOk);
463
        $xoopsSecurityCheck   = $this->xc->getXcXoopsSecurityCheck();
464
        $xoopsSecurityErrors  = $this->xc->getXcXoopsSecurityErrors();
465
        $implode              = $this->pc->getPhpCodeImplode(', ', $xoopsSecurityErrors);
466
        $redirectHeaderErrors = $this->xc->getXcRedirectHeader($tableName, '', '3', $implode, true, $t . "\t\t");
467
        $insert               = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj', 'Handler');
468
        $condition            = $this->pc->getPhpCodeConditions('!' . $xoopsSecurityCheck, '', '', $redirectHeaderErrors, false, $t . "\t");
469
        $constant             = $this->xc->getXcGetConstants('STATUS_BROKEN');
470
        $condition            .= $this->xc->getXcSetVarObj($tableName, $fieldStatus, $constant, $t . "\t");
471
472
        $contInsert = '';
473
        if (1 == $tableNotifications) {
474
            $contInsert .= $this->pc->getPhpCodeCommentLine('Event broken notification', null, $t . "\t\t");
475
            $contInsert .= $this->pc->getPhpCodeArray('tags', [], false, $t . "\t\t");
476
            $contInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_NAME']", "\${$ccFieldMain}", '', $t . "\t\t");
477
            $url = "XOOPS_URL . '/modules/{$moduleDirname}/{$tableName}.php?op=show&{$fieldId}=' . \${$ccFieldId}";
478
            $contInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_URL'] ", $url, '', $t . "\t\t");
479
            $contInsert .= $this->xc->getXcXoopsHandler('notification', $t . "\t\t");
480
            $contInsert .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_broken', \$tags);", $t . "\t\t");
481
            $contInsert .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \${$ccFieldId}, '{$tableSoleName}_broken', \$tags);", $t . "\t\t");
482
        }
483
        $contInsert   .= $this->xc->getXcRedirectHeader($tableName, '', '3', "{$language}FORM_OK", true, $t . "\t\t");
484
        $htmlErrors   = $this->xc->getXcHtmlErrors($tableName, true);
485
        $internalElse = $this->xc->getXcXoopsTplAssign('error', $htmlErrors, true, $t . "\t\t");
486
        $condition    .= $this->pc->getPhpCodeConditions($insert, '', '', $contInsert, $internalElse, $t . "\t");
487
        $mainElse     = $this->xc->getXcXoopsConfirm($tableName, $language, $fieldId, $fieldMain, 'broken', $t . "\t");
488
        $ret          .= $this->pc->getPhpCodeConditions($isset, ' && ', "1 == \${$reqOk}", $condition, $mainElse, $t);
489
490
        return $ret;
491
    }
492
493
    /**
494
     * @private function getUserPagesFooter
495
     * @param $moduleDirname
496
     * @param $tableName
497
     * @param $tableComments
498
     * @param $language
499
     *
500
     * @return string
501
     */
502
    private function getUserPagesFooter($moduleDirname, $tableName, $tableComments, $language)
503
    {
504
        $stuModuleDirname = \mb_strtoupper($moduleDirname);
505
        $stuTableName     = \mb_strtoupper($tableName);
506
507
        $ret = $this->pc->getPhpCodeBlankLine();
508
        $ret .= $this->pc->getPhpCodeCommentLine('Breadcrumbs');
509
        $ret .= $this->uxc->getUserBreadcrumbs($language, $stuTableName);
510
        $ret .= $this->pc->getPhpCodeBlankLine();
511
        $ret .= $this->pc->getPhpCodeCommentLine('Keywords');
512
        $ret .= $this->uxc->getUserMetaKeywords($moduleDirname);
513
        $ret .= $this->pc->getPhpCodeUnset('keywords');
514
        $ret .= $this->pc->getPhpCodeBlankLine();
515
        $ret .= $this->pc->getPhpCodeCommentLine('Description');
516
        $ret .= $this->uxc->getUserMetaDesc($moduleDirname, $language, $stuTableName);
517
        $ret .= $this->xc->getXcXoopsTplAssign('xoops_mpageurl', "{$stuModuleDirname}_URL.'/{$tableName}.php'");
518
        $ret .= $this->xc->getXcXoopsTplAssign("{$moduleDirname}_upload_url", "{$stuModuleDirname}_UPLOAD_URL");
519
        if (1 == $tableComments) {
520
            $ret .= $this->pc->getPhpCodeBlankLine();
521
            $ret .= $this->pc->getPhpCodeCommentLine('View comments');
522
            $ret .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'include/comment_view', true, false, 'require');
523
        }
524
        $ret .= $this->pc->getPhpCodeBlankLine();
525
        $ret .= $this->getInclude('footer');
526
527
        return $ret;
528
    }
529
530
    /**
531
     * @private function getUserPagesSwitch
532
     * @param $moduleDirname
533
     * @param $tableId
534
     * @param $tableMid
535
     * @param $tableName
536
     * @param $tableSoleName
537
     * @param $tableSubmit
538
     * @param $tablePermissions
539
     * @param $tableBroken
540
     * @param $fieldId
541
     * @param $fieldMain
542
     * @param $fieldStatus
543
     * @param $tableNotifications
544
     * @param $tableRate
545
     * @param $fieldReads
546
     * @param $language
547
     * @param $t
548
     * @return string
549
     */
550
    private function getUserPagesSwitch($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableSubmit, $tablePermissions, $tableBroken, $fieldId, $fieldMain, $fieldStatus, $tableNotifications, $tableRate, $fieldReads, $language, $t)
551
    {
552
        $fields = $this->getTableFields($tableMid, $tableId);
553
        $cases['show'] = [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$cases was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cases = array(); before regardless.
Loading history...
554
        $cases['list'] = [$this->getUserPagesList($moduleDirname, $tableName, $fieldId, $fieldMain, $tableRate, $fieldReads,$t . "\t")];
555
        if (1 == $tableSubmit) {
556
            $cases['save']   = [$this->getUserPagesSave($moduleDirname, $fields, $tableName, $tableSoleName, $tablePermissions, $tableNotifications, $language, $t . "\t")];
557
            $cases['new']    = [$this->getUserPagesNew($tableName, $t . "\t")];
558
            $cases['edit']   = [$this->getUserPagesEdit($tableName, $fieldId, $language, $t . "\t")];
559
            $cases['delete'] = [$this->getUserPagesDelete($tableName, $tableSoleName, $language, $fieldId, $fieldMain, $tableNotifications,$t . "\t")];
560
        }
561
        if (1 == $tableBroken) {
562
            $cases['broken']  = [$this->getUserPagesBroken($language, $moduleDirname, $tableName, $tableSoleName, $fieldId, $fieldStatus, $fieldMain, $tableNotifications, $t . "\t")];
563
        }
564
565
        return $this->xc->getXcSwitch('op', $cases, true, false);
566
    }
567
568
    /**
569
     * @public function render
570
     * @param null
571
     * @return bool|string
572
     */
573
    public function render()
574
    {
575
        $module             = $this->getModule();
576
        $table              = $this->getTable();
577
        $tableId            = $table->getVar('table_id');
578
        $tableMid           = $table->getVar('table_mid');
579
        $tableName          = $table->getVar('table_name');
580
        $tableSubmit        = $table->getVar('table_submit');
581
        $tablePermissions   = $table->getVar('table_permissions');
582
        $tableSoleName      = $table->getVar('table_solename');
583
        $tableBroken        = $table->getVar('table_broken');
584
        $tableNotifications = $table->getVar('table_notifications');
585
        $tableComments      = $table->getVar('table_comments');
586
        $tableRate          = $table->getVar('table_rate');
587
        $tableReads         = $table->getVar('table_reads');
0 ignored issues
show
Unused Code introduced by
The assignment to $tableReads is dead and can be removed.
Loading history...
588
        $filename           = $this->getFileName();
589
        $moduleDirname      = $module->getVar('mod_dirname');
590
        $language           = $this->getLanguage($moduleDirname, 'MA');
591
592
        // Fields
593
        $fieldId    = '';
594
        $fieldMain  = '';
595
        $fieldStatus = '';
596
        $fieldReads = '';
597
        $fields    = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
598
        foreach (\array_keys($fields) as $f) {
599
            $fieldName = $fields[$f]->getVar('field_name');
600
            if (0 == $f) {
601
                $fieldId = $fieldName;
602
            }
603
            if (1 == $fields[$f]->getVar('field_main')) {
604
                $fieldMain = $fieldName; // fields parameters main field
605
            }
606
            if (Constants::FIELD_ELE_SELECTSTATUS == $fields[$f]->getVar('field_element')) {
607
                $fieldStatus = $fieldName; // fields for status
608
            }
609
            if (Constants::FIELD_ELE_TEXTREADS == $fields[$f]->getVar('field_element')) {
610
                $fieldReads = $fieldName; // fields for count reads
611
            }
612
        }
613
        $content = $this->getHeaderFilesComments($module);
614
        $content .= $this->getUserPagesHeader($moduleDirname, $tableName, $fieldId, $tablePermissions);
615
        $content .= $this->getUserPagesSwitch($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableSubmit, $tablePermissions, $tableBroken, $fieldId, $fieldMain, $fieldStatus, $tableNotifications, $tableRate, $fieldReads, $language, "\t");
616
        $content .= $this->getUserPagesFooter($moduleDirname, $tableName, $tableComments, $language);
617
618
        $this->create($moduleDirname, '/', $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
619
620
        return $this->renderFile();
621
    }
622
}
623