Passed
Push — master ( 8394af...8c6869 )
by Goffy
03:32
created

UserPages::getUserPagesList()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 61
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 55
nc 4
nop 6
dl 0
loc 61
rs 8.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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