Passed
Push — master ( e884aa...a88df0 )
by Goffy
03:31
created

UserPages   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 602
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 338
dl 0
loc 602
rs 8.8
c 1
b 0
f 0
wmc 45

14 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 5 1
A getInstance() 0 8 2
A __construct() 0 6 1
B getUserPagesList() 0 81 4
A getUserPagesHeader() 0 36 2
F getUserPagesSave() 0 113 15
A getUserPagesDelete() 0 15 2
A getUserPagesNew() 0 11 2
A getUserPagesBroken() 0 41 2
A getUserPagesEdit() 0 15 2
A getUserPagesFooter() 0 22 2
A getPermissionsSave() 0 9 1
A getUserPagesSwitch() 0 16 3
B render() 0 47 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
     * @param $language
99
     * @return string
100
     */
101
    private function getUserPagesHeader($moduleDirname, $tableName, $fieldId, $tablePermissions, $language)
102
    {
103
        $stuModuleDirname = \mb_strtoupper($moduleDirname);
104
        $ccFieldId        = $this->getCamelCase($fieldId, false, true);
105
106
        $ret       = $this->pc->getPhpCodeUseNamespace(['Xmf', 'Request'], '', '');
107
        $ret       .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', '');
108
        $ret       .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants'], '', '');
109
        $ret       .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Common']);
110
        $ret       .= $this->getInclude();
111
        $ret       .= $this->uxc->getUserTplMain($moduleDirname, $tableName);
112
        $ret       .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
113
        $ret       .= $this->pc->getPhpCodeBlankLine();
114
        $ret       .= $this->xc->getXcXoopsRequest('op   ', 'op', 'list', 'Cmd');
115
        $ret       .= $this->xc->getXcXoopsRequest('start', 'start', '0', 'Int');
116
        $userpager = $this->xc->getXcGetConfig('userpager');
117
        $ret       .= $this->xc->getXcXoopsRequest('limit', 'limit', $userpager, 'Int');
118
        $ret       .= $this->xc->getXcXoopsRequest($ccFieldId, $fieldId, '0', 'Int');
119
        $ret       .= $this->pc->getPhpCodeBlankLine();
120
        $ret       .= $this->pc->getPhpCodeCommentLine('Define Stylesheet');
121
        $ret       .= $this->xc->getXcXoThemeAddStylesheet();
122
        $ret       .= $this->pc->getPhpCodeCommentLine('Paths');
123
        $ret       .= $this->xc->getXcXoopsTplAssign('xoops_icons32_url', 'XOOPS_ICONS32_URL');
124
        $ret       .= $this->xc->getXcXoopsTplAssign("{$moduleDirname}_url", "{$stuModuleDirname}_URL");
125
        $ret       .= $this->pc->getPhpCodeCommentLine('Keywords');
126
        $ret       .= $this->pc->getPhpCodeArray('keywords', null, false, '');
127
        $ret       .= $this->uxc->getUserBreadcrumbs($language, 'index', '', 'index.php');
128
        $ret       .= $this->pc->getPhpCodeCommentLine('Permissions');
129
        if (1 == $tablePermissions) {
130
            $ret .= $this->xc->getXcEqualsOperator('$permEdit', '$permissionsHandler->getPermGlobalSubmit()');
131
            $ret .= $this->xc->getXcXoopsTplAssign("permEdit", '$permEdit');
132
        }
133
        $ret       .= $this->xc->getXcXoopsTplAssign("showItem", "\${$ccFieldId} > 0");
134
        $ret       .= $this->pc->getPhpCodeBlankLine();
135
136
        return $ret;
137
    }
138
139
    /**
140
     * @private function getUserPagesList
141
     * @param $moduleDirname
142
     * @param $tableName
143
     * @param $fieldId
144
     * @param $fieldMain
145
     * @param $tableRate
146
     * @param $fieldReads
147
     * @param $language
148
     * @param string $t
149
     * @return string
150
     */
151
    private function getUserPagesList($moduleDirname, $tableName, $fieldId, $fieldMain, $tableRate, $fieldReads, $language, $t = '')
152
    {
153
        $ucfTableName     = \ucfirst($tableName);
154
        $stuTableName     = \mb_strtoupper($tableName);
155
        $ccFieldId        = $this->getCamelCase($fieldId, false, true);
156
        $ccFieldMain      = $this->getCamelCase($fieldMain, false, true);
157
        $ccFieldReads     = $this->getCamelCase($fieldReads, false, true);
158
        $stuModuleDirname = \mb_strtoupper($moduleDirname);
159
160
        $ret = '';
161
        $ret .= $this->uxc->getUserBreadcrumbs($language, $tableName, 'list', '', "\t\t");
162
        if ($tableRate) {
163
            $varRate = '$ratingbars';
164
            $ret .= $this->xc->getXcEqualsOperator($varRate, '(int)' . $this->xc->getXcGetConfig('ratingbars'),'', $t);
165
            $contIf = $this->xc->getXcXoThemeAddStylesheet("{$stuModuleDirname}_URL . '/assets/css/rating.css'", $t . "\t", false);
166
            $contIf .= $this->xc->getXcXoopsTplAssign('rating', $varRate, true, $t . "\t");
167
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_5stars', "(Constants::RATING_5STARS === {$varRate})", true, $t . "\t");
168
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_10stars', "(Constants::RATING_10STARS === {$varRate})", true, $t . "\t");
169
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_10num', "(Constants::RATING_10NUM === {$varRate})", true, $t . "\t");
170
            $contIf .= $this->xc->getXcXoopsTplAssign('rating_likes', "(Constants::RATING_LIKES === {$varRate})", true, $t . "\t");
171
            $contIf .= $this->xc->getXcXoopsTplAssign('itemid', "'{$fieldId}'", true, $t . "\t");
172
            $contIf .= $this->xc->getXcXoopsTplAssign($moduleDirname . '_icon_url_16', "{$stuModuleDirname}_URL . '/' . \$modPathIcon16", true, $t . "\t");
173
            $ret .= $this->pc->getPhpCodeConditions($varRate, ' > ', '0', $contIf, false, $t);
174
        }
175
        $critName  = 'cr' . $ucfTableName;
176
        $ret       .= $this->xc->getXcCriteriaCompo($critName, $t);
177
        $crit      = $this->xc->getXcCriteria('', "'{$fieldId}'", "\${$ccFieldId}",'',true);
178
        $contIf    = $this->xc->getXcCriteriaAdd($critName, $crit, $t . "\t");
179
        $ret       .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $contIf, false, $t);
180
        $ret       .= $this->xc->getXcHandlerCountClear($tableName . 'Count', $tableName, '$' . $critName, $t);
181
        $ret       .= $this->xc->getXcXoopsTplAssign($tableName . 'Count', "\${$tableName}Count", '', $t);
182
        $ret       .= $this->xc->getXcCriteriaSetStart($critName, '$start', $t);
183
        $ret       .= $this->xc->getXcCriteriaSetLimit($critName, '$limit', $t);
184
        $ret       .= $this->xc->getXcHandlerAllClear($tableName . 'All', $tableName, '$' . $critName, $t);
185
        $condIf    = $this->pc->getPhpCodeArray($tableName, null, false, $t . "\t");
186
        $condIf    .= $this->xc->getXcEqualsOperator("\${$ccFieldMain}", "''",'', $t . "\t");
187
        $condIf    .= $this->pc->getPhpCodeCommentLine('Get All', $ucfTableName, $t . "\t");
188
        $foreach   = $this->xc->getXcGetValues($tableName, $tableName . '[$i]', 'i', false, $t . "\t\t");
189
        $foreach   .= $this->xc->getXcGetVar($ccFieldMain, "{$tableName}All[\$i]", $fieldMain, false, $t . "\t\t");
190
        $foreach   .= $this->xc->getXcEqualsOperator('$keywords[$i]', "\${$ccFieldMain}",'', $t . "\t\t");
191
        if ($tableRate) {
192
            $itemId   = $this->xc->getXcGetVar($ccFieldId, "{$tableName}All[\$i]", $fieldId, true);
193
            $const  = $this->xc->getXcGetConstants('TABLE_' . $stuTableName);
194
            $foreach .= $this->xc->getXcEqualsOperator("\${$tableName}[\$i]['rating']", "\$ratingsHandler->getItemRating({$itemId}, {$const})",'', $t . "\t\t");
195
        }
196
        $condIf    .= $this->pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $foreach, $t . "\t");
197
        $condIf    .= $this->xc->getXcXoopsTplAssign($tableName, "\${$tableName}", true, $t . "\t");
198
        $condIf    .= $this->pc->getPhpCodeUnset($tableName, $t . "\t");
199
        $condIf    .= $this->xc->getXcPageNav($tableName, $t . "\t");
200
        $config    = $this->xc->getXcGetConfig('table_type');
201
        $condIf    .= $this->xc->getXcXoopsTplAssign('table_type', $config, true, $t . "\t");
202
        $config    = $this->xc->getXcGetConfig('panel_type');
203
        $condIf    .= $this->xc->getXcXoopsTplAssign('panel_type', $config, true, $t . "\t");
204
        $divideby  = $this->xc->getXcGetConfig('divideby');
205
        $condIf    .= $this->xc->getXcXoopsTplAssign('divideby', $divideby, true, $t . "\t");
206
        $numbCol   = $this->xc->getXcGetConfig('numb_col');
207
        $condIf    .= $this->xc->getXcXoopsTplAssign('numb_col', $numbCol, true, $t . "\t");
208
        $stripTags      = $this->pc->getPhpCodeStripTags('', "\${$ccFieldMain} . ' - ' . " . "\$GLOBALS['xoopsModule']->getVar('name')", true);
209
        $condIf2         = $this->xc->getXcXoopsTplAssign('xoops_pagetitle', $stripTags, true, $t . "\t\t");
210
        $condIf       .= $this->pc->getPhpCodeConditions("'show' == \$op && '' != \${$ccFieldMain}", '', "", $condIf2, false, $t . "\t");
211
212
        if ('' !== $fieldReads) {
213
            $condIf3 = $this->xc->getXcHandlerGetObj($tableName, $ccFieldId, $t . "\t\t");
214
215
216
            $getVar = $this->xc->getXcGetVar('', "{$tableName}Obj", $fieldReads, true);
217
            $condIf3 .= $this->xc->getXcEqualsOperator("\${$ccFieldReads}", "(int)" . $getVar . ' + 1', false, $t . "\t\t");
218
            $condIf3 .= $this->xc->getXcSetVarObj($tableName, $fieldReads, "\${$ccFieldReads}", $t . "\t\t");
219
            $condIf3 .= $this->pc->getPhpCodeCommentLine('Insert Data', null, $t . "\t\t");
220
            $insert = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj', 'Handler');
221
            $condIf3 .= $this->getSimpleString($insert .';',$t . "\t\t");
222
            //$contentInsert = $this->xc->getXcRedirectHeader("'{$tableName}.php?op=list&{$fieldId}=' . \${$ccFieldId}", '', '5', "\${$tableName}Obj->getHtmlErrors()", false, $t . "\t\t\t");
223
            //$condIf3 .= $this->pc->getPhpCodeConditions('!' . $insert, '', '', $contentInsert, false, $t . "\t\t");
224
            $condIf .= $this->pc->getPhpCodeConditions("'show' == \$op", '', "", $condIf3, false, $t . "\t");
225
226
        }
227
228
229
        $ret       .= $this->pc->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $condIf, false, $t);
230
231
        return $ret;
232
    }
233
234
    /**
235
     * @public function getUserSubmitSave
236
     * @param string $moduleDirname
237
     * @param        $fields
238
     * @param string $tableName
239
     * @param        $tableSoleName
240
     * @param $tablePermissions
241
     * @param $tableNotifications
242
     * @param        $language
243
     * @param string $t
244
     * @return string
245
     */
246
    public function getUserPagesSave($moduleDirname, $fields, $tableName, $tableSoleName, $tablePermissions, $tableNotifications, $language, $t = '')
247
    {
248
        $ucfTableName  = \ucfirst($tableName);
249
        $countUploader = 0;
250
        $fieldId       = '';
251
        $ccFieldId     = '';
252
        $fieldMain     = '';
253
        $fieldStatus   = '';
254
        $ucfFieldId    = '';
255
        $ccFieldMain   = '';
256
        $ccFieldStatus = '';
257
        foreach (\array_keys($fields) as $f) {
258
            $fieldName = $fields[$f]->getVar('field_name');
259
            if (0 == $f) {
260
                $fieldId   = $fieldName;
261
                $ccFieldId = $this->getCamelCase($fieldId, false, true);
262
                $ucfFieldId = \ucfirst($ccFieldId);
263
            }
264
            if ($fields[$f]->getVar('field_element') >= Constants::FIELD_ELE_IMAGELIST && $fields[$f]->getVar('field_element') <= Constants::FIELD_ELE_UPLOADFILE) {
265
                $countUploader++;
266
            }
267
            if (1 == $fields[$f]->getVar('field_main')) {
268
                $fieldMain    = $fieldName; // fieldMain = fields parameters main field
269
                $ccFieldMain  = $this->getCamelCase($fieldMain, false, true);
270
            }
271
            if ($fields[$f]->getVar('field_element') == Constants::FIELD_ELE_SELECTSTATUS) {
272
                $fieldStatus   = $fieldName;
273
                $ccFieldStatus = $this->getCamelCase($fieldStatus, false, true);
274
            }
275
        }
276
277
        $ret                = $this->pc->getPhpCodeCommentLine('Security Check', '', $t);
278
        $xoopsSecurityCheck = $this->xc->getXcXoopsSecurityCheck();
279
        $securityError      = $this->xc->getXcXoopsSecurityErrors();
280
        $implode            = $this->pc->getPhpCodeImplode(',', $securityError);
281
        $redirectError      = $this->xc->getXcRedirectHeader($tableName, '', '3', $implode, true, $t . "\t");
282
        $ret                .= $this->pc->getPhpCodeConditions('!' . $xoopsSecurityCheck, '', '', $redirectError, false, $t);
283
        if (1 == $tablePermissions) {
284
            $ret                .= $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
285
            $contIf             = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
286
            $ret                .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
287
        }
288
        $getObj             = $this->xc->getXcHandlerGetObj($tableName, $ccFieldId,  $t . "\t");
289
        $createObj          = $this->xc->getXcHandlerCreateObj($tableName, $t . "\t");
290
        $ret                .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $getObj, $createObj, $t);
291
        $ret                .= $this->xc->getXcSaveElements($moduleDirname, $tableName, $tableSoleName, $fields, $t);
292
        $ret                .= $this->pc->getPhpCodeCommentLine('Insert Data', null, $t);
293
        $insert             = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj', 'Handler');
294
295
        $contentInsert = '';
296
        if (1 == $tableNotifications || $countUploader > 0) {
297
            $contentInsert .= $this->pc->getPhpCodeTernaryOperator("new{$ucfFieldId}", "\${$ccFieldId} > 0", "\${$ccFieldId}", "\${$tableName}Obj->getNewInsertedId{$ucfTableName}()", $t . "\t");
298
        }
299
300
        if (1 == $tablePermissions) {
301
            $contentInsert .= $this->xc->getXcXoopsHandler('groupperm', $t . "\t");
302
            $contentInsert .= $this->xc->getXcEqualsOperator('$mid', "\$GLOBALS['xoopsModule']->getVar('mid')", null, $t . "\t");
303
            $contentInsert .= $this->getPermissionsSave($moduleDirname, $ucfFieldId,'view_' . $tableName);
304
            $contentInsert .= $this->getPermissionsSave($moduleDirname, $ucfFieldId, 'submit_' . $tableName);
305
            $contentInsert .= $this->getPermissionsSave($moduleDirname, $ucfFieldId, 'approve_' . $tableName);
306
        }
307
308
        if (1 == $tableNotifications) {
309
            $contentInsert .= $this->pc->getPhpCodeCommentLine('Handle notification', null, $t . "\t");
310
            $contentInsert .= $this->xc->getXcGetVar($ccFieldMain, "{$tableName}Obj", $fieldMain, false, $t. "\t");
311
            if ('' !== $fieldStatus) {
312
                $contentInsert .= $this->xc->getXcGetVar($ccFieldStatus, "{$tableName}Obj", $fieldStatus, false, $t . "\t");
313
            }
314
            $contentInsert .= $this->pc->getPhpCodeArray('tags', [], false, $t . "\t");
315
            $contentInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_NAME']", "\${$ccFieldMain}", '', $t . "\t");
316
            $url = "XOOPS_URL . '/modules/{$moduleDirname}/{$tableName}.php?op=show&{$fieldId}=' . \${$ccFieldId}";
317
            $contentInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_URL'] ", $url, '', $t . "\t");
318
            $contentInsert .= $this->xc->getXcXoopsHandler('notification', $t . "\t");
319
            if ('' === $fieldStatus) {
320
                $not2If        = $this->pc->getPhpCodeCommentLine('Event modify notification', null, $t . "\t\t");
321
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_modify', \$tags);", $t . "\t\t");
322
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_modify', \$tags);", $t . "\t\t");
323
                $not2Else      = $this->pc->getPhpCodeCommentLine('Event new notification', null, $t . "\t\t");
324
                $not2Else      .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_new', \$tags);", $t . "\t\t");
325
                //$not2Else      .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_new', \$tags);", $t . "\t\t");
326
                $not1Else      = $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $not2If, $not2Else, $t . "\t");
327
                $contentInsert .= $not1Else;
328
            } else {
329
                $not1If        = $this->pc->getPhpCodeCommentLine('Event approve notification', null, $t . "\t\t");
330
                $not1If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_approve', \$tags);", $t . "\t\t");
331
                $not1If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_approve', \$tags);", $t . "\t\t");
332
                $not2If        = $this->pc->getPhpCodeCommentLine('Event modify notification', null, $t . "\t\t\t");
333
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_modify', \$tags);", $t . "\t\t\t");
334
                $not2If        .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \$new{$ucfFieldId}, '{$tableSoleName}_modify', \$tags);", $t . "\t\t\t");
335
                $not2Else      = $this->pc->getPhpCodeCommentLine('Event new notification', null, $t . "\t\t\t");
336
                $not2Else      .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_new', \$tags);", $t . "\t\t\t");
337
                $not1Else      = $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' > ', '0', $not2If, $not2Else, $t . "\t\t");
338
                $contentInsert .= $this->pc->getPhpCodeConditions("\${$ccFieldStatus}", ' == ', $this->xc->getXcGetConstants('STATUS_SUBMITTED'), $not1If, $not1Else, $t . "\t");
339
            }
340
        }
341
342
        $contentInsert .= $this->pc->getPhpCodeCommentLine('redirect after insert', null, $t . "\t");
343
        if ($countUploader > 0) {
344
            $errIf     = $this->xc->getXcRedirectHeader("'{$tableName}.php?op=edit&{$fieldId}=' . \$new{$ucfFieldId}", '', '5', '$uploaderErrors', false, $t . "\t\t");
345
            $errElse   = $this->xc->getXcRedirectHeader($tableName, '?op=list', '2', "{$language}FORM_OK", true, $t . "\t\t");
346
            $confirmOk = $this->pc->getPhpCodeConditions('$uploaderErrors', ' !== ', "''", $errIf, $errElse, $t . "\t");
347
        } else {
348
            $confirmOk = $this->xc->getXcRedirectHeader($tableName, '', '2', "{$language}FORM_OK", true, $t . "\t");
349
        }
350
        $contentInsert .= $confirmOk;
351
        $ret           .= $this->pc->getPhpCodeConditions($insert, '', '', $contentInsert, false, $t);
352
353
        $ret .= $this->pc->getPhpCodeCommentLine('Get Form Error', null, $t);
354
        $ret .= $this->xc->getXcXoopsTplAssign('error', "\${$tableName}Obj->getHtmlErrors()", true, $t);
355
        $ret .= $this->xc->getXcGetForm('form', $tableName, 'Obj', $t);
356
        $ret .= $this->xc->getXcXoopsTplAssign('form', '$form->render()', true, $t);
357
358
        return $ret;
359
    }
360
361
    /**
362
     * @private function getPermissionsSave
363
     * @param $moduleDirname
364
     * @param $ucfFieldId
365
     * @param string $perm
366
     *
367
     * @return string
368
     */
369
    private function getPermissionsSave($moduleDirname, $ucfFieldId, $perm = 'view')
370
    {
371
        $ret     = $this->pc->getPhpCodeCommentLine('Permission to', $perm, "\t\t\t");
372
        $ret     .= $this->xc->getXcDeleteRight('grouppermHandler', "{$moduleDirname}_{$perm}", '$mid', "\$new{$ucfFieldId}", false, "\t\t\t");
373
        $content = $this->xc->getXcAddRight('grouppermHandler', "{$moduleDirname}_{$perm}", "\$new{$ucfFieldId}", '$onegroupId', '$mid', false, "\t\t\t\t\t");
374
        $foreach = $this->pc->getPhpCodeForeach("_POST['groups_{$perm}']", false, false, 'onegroupId', $content, "\t\t\t\t");
375
        $ret     .= $this->pc->getPhpCodeConditions("isset(\$_POST['groups_{$perm}'])", null, null, $foreach, false, "\t\t\t");
376
377
        return $ret;
378
    }
379
380
    /**
381
     * @public function getUserPagesNew
382
     * @param        $tableName
383
     * @param        $tableSoleName
384
     * @param        $tablePermissions
385
     * @param        $language
386
     * @param string $t
387
     * @return string
388
     */
389
    public function getUserPagesNew($tableName, $tableSoleName, $tablePermissions, $language, $t = '')
390
    {
391
        $ret    = $this->uxc->getUserBreadcrumbs($language, $tableSoleName, 'add', '', "\t\t");
392
        if (1 == $tablePermissions) {
393
            $ret    .= $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
394
            $contIf = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
395
            $ret    .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
396
        }
397
        $ret    .= $this->xc->getXcCommonPagesNew($tableName, $t);
398
399
        return $ret;
400
    }
401
402
    /**
403
     * @public function getUserPagesEdit
404
     * @param        $tableName
405
     * @param        $tableSoleName
406
     * @param        $tablePermissions
407
     * @param        $fieldId
408
     * @param        $language
409
     * @param string $t
410
     * @return string
411
     */
412
    public function getUserPagesEdit($tableName, $tableSoleName, $tablePermissions, $fieldId, $language, $t = '')
413
    {
414
        $ret       = $this->uxc->getUserBreadcrumbs($language, $tableSoleName, 'edit', '', "\t\t");
415
        $ccFieldId = $this->getCamelCase($fieldId, false, true);
416
        if (1 == $tablePermissions) {
417
            $ret       .= $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
418
            $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
419
            $ret       .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
420
        }
421
        $ret       .= $this->pc->getPhpCodeCommentLine('Check params', '', $t);
422
        $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, "{$language}INVALID_PARAM", true, $t . "\t");
423
        $ret       .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' == ', '0', $contIf, false, $t);
424
        $ret       .= $this->xc->getXcCommonPagesEdit($tableName, $ccFieldId, $t);
425
426
        return $ret;
427
    }
428
429
    /**
430
     * @private function getUserPagesDelete
431
     * @param        $tableName
432
     * @param        $tableSoleName
433
     * @param        $tablePermissions
434
     * @param        $language
435
     * @param        $fieldId
436
     * @param        $fieldMain
437
     * @param        $tableNotifications
438
     * @param string $t
439
     * @return string
440
     */
441
    private function getUserPagesDelete($tableName, $tableSoleName, $tablePermissions, $language, $fieldId, $fieldMain, $tableNotifications, $t = '')
442
    {
443
        $ret       = $this->uxc->getUserBreadcrumbs($language, $tableSoleName, 'delete', '', "\t\t");
444
        $ccFieldId = $this->getCamelCase($fieldId, false, true);
445
        if (1 == $tablePermissions) {
446
            $ret       .= $this->pc->getPhpCodeCommentLine('Check permissions', '', $t);
447
            $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, '_NOPERM', true, $t . "\t");
448
            $ret       .= $this->pc->getPhpCodeConditions('!$permissionsHandler->getPermGlobalSubmit()', '', '', $contIf, false, $t);
449
        }
450
        $ret       .= $this->pc->getPhpCodeCommentLine('Check params', '', $t);
451
        $contIf    = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, "{$language}INVALID_PARAM", true, $t . "\t");
452
        $ret       .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' == ', '0', $contIf, false, $t);
453
        $ret       .= $this->xc->getXcCommonPagesDelete($language, $tableName, $tableSoleName, $fieldId, $fieldMain, $tableNotifications, $t);
454
455
        return $ret;
456
    }
457
458
    /**
459
     * @private function getUserPagesBroken
460
     * @param        $language
461
     * @param        $moduleDirname
462
     * @param        $tableName
463
     * @param        $tableSoleName
464
     * @param        $fieldId
465
     * @param $fieldStatus
466
     * @param        $fieldMain
467
     * @param        $tableNotifications
468
     * @param string $t
469
     * @return string
470
     */
471
    private function getUserPagesBroken($language, $moduleDirname, $tableName, $tableSoleName, $fieldId, $fieldStatus, $fieldMain, $tableNotifications, $t = '')
472
    {
473
        $ccFieldId   = $this->getCamelCase($fieldId, false, true);
474
        $ccFieldMain = $this->getCamelCase($fieldMain, false, true);
475
        $ret    = $this->uxc->getUserBreadcrumbs($language, '', 'broken', '', "\t\t");
476
        $ret    .= $this->pc->getPhpCodeCommentLine('Check params', '', $t);
477
        $contIf = $this->xc->getXcRedirectHeader($tableName, '?op=list', 3, "{$language}INVALID_PARAM", true, $t . "\t");
478
        $ret    .= $this->pc->getPhpCodeConditions("\${$ccFieldId}", ' == ', '0', $contIf, false, $t);
479
480
        $ret                  .= $this->xc->getXcHandlerGet($tableName, $ccFieldId, 'Obj', $tableName . 'Handler', false, $t);
481
        $ret                  .= $this->xc->getXcGetVar($ccFieldMain, "{$tableName}Obj", $fieldMain, false, $t);
482
        $reqOk                = "_REQUEST['ok']";
483
        $isset                = $this->pc->getPhpCodeIsset($reqOk);
484
        $xoopsSecurityCheck   = $this->xc->getXcXoopsSecurityCheck();
485
        $xoopsSecurityErrors  = $this->xc->getXcXoopsSecurityErrors();
486
        $implode              = $this->pc->getPhpCodeImplode(', ', $xoopsSecurityErrors);
487
        $redirectHeaderErrors = $this->xc->getXcRedirectHeader($tableName, '', '3', $implode, true, $t . "\t\t");
488
        $insert               = $this->xc->getXcHandlerInsert($tableName, $tableName, 'Obj', 'Handler');
489
        $condition            = $this->pc->getPhpCodeConditions('!' . $xoopsSecurityCheck, '', '', $redirectHeaderErrors, false, $t . "\t");
490
        $constant             = $this->xc->getXcGetConstants('STATUS_BROKEN');
491
        $condition            .= $this->xc->getXcSetVarObj($tableName, $fieldStatus, $constant, $t . "\t");
492
493
        $contInsert = '';
494
        if (1 == $tableNotifications) {
495
            $contInsert .= $this->pc->getPhpCodeCommentLine('Event broken notification', null, $t . "\t\t");
496
            $contInsert .= $this->pc->getPhpCodeArray('tags', [], false, $t . "\t\t");
497
            $contInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_NAME']", "\${$ccFieldMain}", '', $t . "\t\t");
498
            $url = "XOOPS_URL . '/modules/{$moduleDirname}/{$tableName}.php?op=show&{$fieldId}=' . \${$ccFieldId}";
499
            $contInsert .= $this->xc->getXcEqualsOperator("\$tags['ITEM_URL'] ", $url, '', $t . "\t\t");
500
            $contInsert .= $this->xc->getXcXoopsHandler('notification', $t . "\t\t");
501
            $contInsert .= $this->getSimpleString("\$notificationHandler->triggerEvent('global', 0, 'global_broken', \$tags);", $t . "\t\t");
502
            $contInsert .= $this->getSimpleString("\$notificationHandler->triggerEvent('{$tableName}', \${$ccFieldId}, '{$tableSoleName}_broken', \$tags);", $t . "\t\t");
503
        }
504
        $contInsert   .= $this->xc->getXcRedirectHeader($tableName, '', '3', "{$language}FORM_OK", true, $t . "\t\t");
505
        $htmlErrors   = $this->xc->getXcHtmlErrors($tableName, true);
506
        $internalElse = $this->xc->getXcXoopsTplAssign('error', $htmlErrors, true, $t . "\t\t");
507
        $condition    .= $this->pc->getPhpCodeConditions($insert, '', '', $contInsert, $internalElse, $t . "\t");
508
        $mainElse     = $this->xc->getXcXoopsConfirm($tableName, $language, $fieldId, $fieldMain, 'broken', $t . "\t");
509
        $ret          .= $this->pc->getPhpCodeConditions($isset, ' && ', "1 == \${$reqOk}", $condition, $mainElse, $t);
510
511
        return $ret;
512
    }
513
514
    /**
515
     * @private function getUserPagesFooter
516
     * @param $moduleDirname
517
     * @param $tableName
518
     * @param $tableComments
519
     * @param $language
520
     *
521
     * @return string
522
     */
523
    private function getUserPagesFooter($moduleDirname, $tableName, $tableComments, $language)
524
    {
525
        $stuModuleDirname = \mb_strtoupper($moduleDirname);
526
        $stuTableName     = \mb_strtoupper($tableName);
527
        $ret = $this->pc->getPhpCodeBlankLine();
528
        $ret .= $this->pc->getPhpCodeCommentLine('Keywords');
529
        $ret .= $this->uxc->getUserMetaKeywords($moduleDirname);
530
        $ret .= $this->pc->getPhpCodeUnset('keywords');
531
        $ret .= $this->pc->getPhpCodeBlankLine();
532
        $ret .= $this->pc->getPhpCodeCommentLine('Description');
533
        $ret .= $this->uxc->getUserMetaDesc($moduleDirname, $language, $stuTableName);
534
        $ret .= $this->xc->getXcXoopsTplAssign('xoops_mpageurl', "{$stuModuleDirname}_URL.'/{$tableName}.php'");
535
        $ret .= $this->xc->getXcXoopsTplAssign("{$moduleDirname}_upload_url", "{$stuModuleDirname}_UPLOAD_URL");
536
        if (1 == $tableComments) {
537
            $ret .= $this->pc->getPhpCodeBlankLine();
538
            $ret .= $this->pc->getPhpCodeCommentLine('View comments');
539
            $ret .= $this->pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'include/comment_view', true, false, 'require');
540
        }
541
        $ret .= $this->pc->getPhpCodeBlankLine();
542
        $ret .= $this->getInclude('footer');
543
544
        return $ret;
545
    }
546
547
    /**
548
     * @private function getUserPagesSwitch
549
     * @param $moduleDirname
550
     * @param $tableId
551
     * @param $tableMid
552
     * @param $tableName
553
     * @param $tableSoleName
554
     * @param $tableSubmit
555
     * @param $tablePermissions
556
     * @param $tableBroken
557
     * @param $fieldId
558
     * @param $fieldMain
559
     * @param $fieldStatus
560
     * @param $tableNotifications
561
     * @param $tableRate
562
     * @param $fieldReads
563
     * @param $language
564
     * @param $t
565
     * @return string
566
     */
567
    private function getUserPagesSwitch($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableSubmit, $tablePermissions, $tableBroken, $fieldId, $fieldMain, $fieldStatus, $tableNotifications, $tableRate, $fieldReads, $language, $t)
568
    {
569
        $fields = $this->getTableFields($tableMid, $tableId);
570
        $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...
571
        $cases['list'] = [$this->getUserPagesList($moduleDirname, $tableName, $fieldId, $fieldMain, $tableRate, $fieldReads, $language,$t . "\t")];
572
        if (1 == $tableSubmit) {
573
            $cases['save']   = [$this->getUserPagesSave($moduleDirname, $fields, $tableName, $tableSoleName, $tablePermissions, $tableNotifications, $language, $t . "\t")];
574
            $cases['new']    = [$this->getUserPagesNew($tableName, $tableSoleName, $tablePermissions, $language, $t . "\t")];
575
            $cases['edit']   = [$this->getUserPagesEdit($tableName, $tableSoleName, $tablePermissions, $fieldId, $language, $t . "\t")];
576
            $cases['delete'] = [$this->getUserPagesDelete($tableName, $tableSoleName, $tablePermissions, $language, $fieldId, $fieldMain, $tableNotifications,$t . "\t")];
577
        }
578
        if (1 == $tableBroken) {
579
            $cases['broken']  = [$this->getUserPagesBroken($language, $moduleDirname, $tableName, $tableSoleName, $fieldId, $fieldStatus, $fieldMain, $tableNotifications, $t . "\t")];
580
        }
581
582
        return $this->xc->getXcSwitch('op', $cases, true, false);
583
    }
584
585
    /**
586
     * @public function render
587
     * @param null
588
     * @return bool|string
589
     */
590
    public function render()
591
    {
592
        $module             = $this->getModule();
593
        $table              = $this->getTable();
594
        $tableId            = $table->getVar('table_id');
595
        $tableMid           = $table->getVar('table_mid');
596
        $tableName          = $table->getVar('table_name');
597
        $tableSubmit        = $table->getVar('table_submit');
598
        $tablePermissions   = $table->getVar('table_permissions');
599
        $tableSoleName      = $table->getVar('table_solename');
600
        $tableBroken        = $table->getVar('table_broken');
601
        $tableNotifications = $table->getVar('table_notifications');
602
        $tableComments      = $table->getVar('table_comments');
603
        $tableRate          = $table->getVar('table_rate');
604
        $filename           = $this->getFileName();
605
        $moduleDirname      = $module->getVar('mod_dirname');
606
        $language           = $this->getLanguage($moduleDirname, 'MA');
607
608
        // Fields
609
        $fieldId    = '';
610
        $fieldMain  = '';
611
        $fieldStatus = '';
612
        $fieldReads = '';
613
        $fields    = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
614
        foreach (\array_keys($fields) as $f) {
615
            $fieldName = $fields[$f]->getVar('field_name');
616
            if (0 == $f) {
617
                $fieldId = $fieldName;
618
            }
619
            if (1 == $fields[$f]->getVar('field_main')) {
620
                $fieldMain = $fieldName; // fields parameters main field
621
            }
622
            if (Constants::FIELD_ELE_SELECTSTATUS == $fields[$f]->getVar('field_element')) {
623
                $fieldStatus = $fieldName; // fields for status
624
            }
625
            if (Constants::FIELD_ELE_TEXTREADS == $fields[$f]->getVar('field_element')) {
626
                $fieldReads = $fieldName; // fields for count reads
627
            }
628
        }
629
        $content = $this->getHeaderFilesComments($module);
630
        $content .= $this->getUserPagesHeader($moduleDirname, $tableName, $fieldId, $tablePermissions, $language);
631
        $content .= $this->getUserPagesSwitch($moduleDirname, $tableId, $tableMid, $tableName, $tableSoleName, $tableSubmit, $tablePermissions, $tableBroken, $fieldId, $fieldMain, $fieldStatus, $tableNotifications, $tableRate, $fieldReads, $language, "\t");
632
        $content .= $this->getUserPagesFooter($moduleDirname, $tableName, $tableComments, $language);
633
634
        $this->create($moduleDirname, '/', $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
635
636
        return $this->renderFile();
637
    }
638
}
639