UserPages::getUserPagesSave()   F
last analyzed

Complexity

Conditions 15
Paths 1360

Size

Total Lines 113
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 91
nc 1360
nop 8
dl 0
loc 113
rs 1.5963
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

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

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

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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