AdminPermissions::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files\Admin;
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 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.0
24
 *
25
 * @author          Txmod Xoops https://xoops.org 
26
 *                  Goffy https://myxoops.org
27
 *
28
 */
29
30
/**
31
 * Class AdminPermissions.
32
 */
33
class AdminPermissions extends Files\CreateFile
34
{
35
    /**
36
     * @var mixed
37
     */
38
    private $axc = null;
39
40
    /**
41
     * @var mixed
42
     */
43
    private $xc = null;
44
45
    /**
46
     * @var mixed
47
     */
48
    private $pc = null;
49
50
    /**
51
     * @var mixed
52
     */
53
    private $cxc = null;
54
55
    /**
56
     * @public function constructor
57
     * @param null
58
     */
59
    public function __construct()
60
    {
61
        parent::__construct();
62
        $this->xc  = Modulebuilder\Files\CreateXoopsCode::getInstance();
63
        $this->pc  = Modulebuilder\Files\CreatePhpCode::getInstance();
64
        $this->axc = Modulebuilder\Files\Admin\AdminXoopsCode::getInstance();
65
        $this->cxc = Modulebuilder\Files\Classes\ClassXoopsCode::getInstance();
66
    }
67
68
    /**
69
     * @static function getInstance
70
     *
71
     * @param null
72
     *
73
     * @return AdminPermissions
74
     */
75
    public static function getInstance()
76
    {
77
        static $instance = false;
78
        if (!$instance) {
79
            $instance = new self();
80
        }
81
82
        return $instance;
83
    }
84
85
    /**
86
     * @public function write
87
     *
88
     * @param string $module
89
     * @param mixed  $tables
90
     * @param string $filename
91
     *
92
     * @return null
93
     */
94
    public function write($module, $tables, $filename)
95
    {
96
        $this->setModule($module);
97
        $this->setTables($tables);
98
        $this->setFileName($filename);
99
        return null;
100
    }
101
102
    /**
103
     * @private function getPermissionsHeader
104
     *
105
     * @param $module
106
     * @param $language
107
     *
108
     * @return string
109
     */
110
    private function getPermissionsHeader($module, $language)
111
    {
112
        $moduleDirname = $module->getVar('mod_dirname');
113
        $tables        = $this->getTableTables($module->getVar('mod_id'));
114
        $tableNames    = [];
115
        foreach (\array_keys($tables) as $t) {
116
            if (1 == $tables[$t]->getVar('table_permissions')) {
117
                $tableNames[] = $tables[$t]->getVar('table_name');
118
            }
119
        }
120
        $ret           = $this->pc->getPhpCodeUseNamespace(['Xmf', 'Request'], '', '');
121
        $ret           .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', '');
122
        $ret           .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants']);
123
        $ret           .= $this->getRequire('header');
124
        $ret           .= $this->pc->getPhpCodeBlankLine();
125
        $ret           .= $this->pc->getPhpCodeCommentLine('Template Index');
126
        $ret           .= $this->axc->getAdminTemplateMain($moduleDirname, 'permissions');
127
        $ret           .= $this->xc->getXcXoopsTplAssign('navigation', "\$adminObject->displayNavigation('permissions.php')");
128
        $ret           .= $this->pc->getPhpCodeBlankLine();
129
        $ret           .= $this->xc->getXcXoopsRequest('op', 'op', 'global', 'Cmd');
130
        $ret           .= $this->pc->getPhpCodeBlankLine();
131
        $ret           .= $this->pc->getPhpCodeCommentLine('Get Form');
132
        $ret           .= $this->pc->getPhpCodeIncludeDir('\XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
133
        $ret           .= $this->xc->getXcXoopsLoad('XoopsFormLoader');
134
        $optionsSelect['global'] = "{$language}PERMISSIONS_GLOBAL";
0 ignored issues
show
Comprehensibility Best Practice introduced by
$optionsSelect was never initialized. Although not strictly required by PHP, it is generally a good practice to add $optionsSelect = array(); before regardless.
Loading history...
135
        foreach ($tableNames as $tableName) {
136
            $ucfTablename = \ucfirst($tableName);
137
            $optionsSelect["approve_{$tableName}"] = "{$language}PERMISSIONS_APPROVE . ' {$ucfTablename}'";
138
            $optionsSelect["submit_{$tableName}"] = "{$language}PERMISSIONS_SUBMIT . ' {$ucfTablename}'";
139
            $optionsSelect["view_{$tableName}"] = "{$language}PERMISSIONS_VIEW . ' {$ucfTablename}'";
140
        }
141
        $formSelect    = $this->xc->getXoopsFormSelectExtraOptions('formSelect', '\'\'', 'op', $optionsSelect, 'onchange="document.fselperm.submit()"');
142
        $ret           .= $this->cxc->getXoopsSimpleForm('permTableForm', 'formSelect', $formSelect, '\'\'', 'fselperm', 'permissions');
143
144
        return $ret;
145
    }
146
147
    /**
148
     * @private function getPermissionsSwitch
149
     * @param $module
150
     * @param $language
151
     *
152
     * @return string
153
     */
154
    private function getPermissionsSwitch($module, $language)
155
    {
156
        $moduleDirname = $module->getVar('mod_dirname');
157
        $tables        = $this->getTableTables($module->getVar('mod_id'));
158
        $t = "\t\t";
159
        $n = "\n";
160
        $cases['global']= [
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...
161
                "{$t}\$formTitle = {$language}PERMISSIONS_GLOBAL;{$n}",
162
                "{$t}\$permName = '{$moduleDirname}_ac';{$n}",
163
                "{$t}\$permDesc = {$language}PERMISSIONS_GLOBAL_DESC;{$n}",
164
                "{$t}\$globalPerms = ['4' => {$language}PERMISSIONS_GLOBAL_4, '8' => {$language}PERMISSIONS_GLOBAL_8, '16' => {$language}PERMISSIONS_GLOBAL_16 ];{$n}",
165
                ];
166
        foreach (\array_keys($tables) as $i) {
167
            if (1 == $tables[$i]->getVar('table_permissions')) {
168
                $tableName = $tables[$i]->getVar('table_name');
169
                $ucfTablename = \ucfirst($tableName);
170
                $cases["approve_{$tableName}"] = [
171
                    "{$t}\$formTitle = {$language}PERMISSIONS_APPROVE;{$n}",
172
                    "{$t}\$permName = '{$moduleDirname}_approve_{$tableName}';{$n}",
173
                    "{$t}\$permDesc = {$language}PERMISSIONS_APPROVE_DESC . ' {$ucfTablename}';{$n}",
174
                    "{$t}\$handler = \$helper->getHandler('{$tableName}');{$n}",
175
                ];
176
                $cases["submit_{$tableName}"] = [
177
                    "{$t}\$formTitle = {$language}PERMISSIONS_SUBMIT;{$n}",
178
                    "{$t}\$permName = '{$moduleDirname}_submit_{$tableName}';{$n}",
179
                    "{$t}\$permDesc = {$language}PERMISSIONS_SUBMIT_DESC . ' {$ucfTablename}';{$n}",
180
                    "{$t}\$handler = \$helper->getHandler('{$tableName}');{$n}",
181
                ];
182
                $cases["view_{$tableName}"] = [
183
                    "{$t}\$formTitle = {$language}PERMISSIONS_VIEW;{$n}",
184
                    "{$t}\$permName = '{$moduleDirname}_view_{$tableName}';{$n}",
185
                    "{$t}\$permDesc = {$language}PERMISSIONS_VIEW_DESC . ' {$ucfTablename}';{$n}",
186
                    "{$t}\$handler = \$helper->getHandler('{$tableName}');{$n}",
187
                ];
188
            }
189
        }
190
        $contentSwitch = $this->pc->getPhpCodeCaseSwitch($cases, true, false, "\t");
191
192
        return $this->pc->getPhpCodeSwitch('op', $contentSwitch);
193
    }
194
195
    /**
196
     * @private function getPermissionsBody
197
     *
198
     * @param string $module
199
     * @param string $language
200
     *
201
     * @return string
202
     */
203
    private function getPermissionsBody($module, $language)
204
    {
205
        $tables   = $this->getTableTables($module->getVar('mod_id'));
206
207
        $ret      = $this->xc->getXcGetVar('moduleId', 'xoopsModule', 'mid');
208
        $ret      .= $this->xc->getXcXoopsFormGroupPerm('permform', '$formTitle', '$moduleId', '$permName', '$permDesc', "'admin/permissions.php'");
209
        $ret      .= $this->xc->getXcEqualsOperator('$permFound', 'false');
210
        $foreach1 = $this->xc->getXcAddItem('permform', '$gPermId', '$gPermName', "\t\t");
211
        $if1      = $this->pc->getPhpCodeForeach('globalPerms', false, 'gPermId', 'gPermName', $foreach1, "\t");
212
        $if1      .= $this->xc->getXcXoopsTplAssign('form', '$permform->render()', true, "\t");
213
        $if1      .= $this->xc->getXcEqualsOperator('$permFound', 'true', null, "\t");
214
        $ret      .= $this->pc->getPhpCodeConditions("'global'", ' === ', '$op', $if1, false);
215
216
        foreach (\array_keys($tables) as $t) {
217
            if (1 == $tables[$t]->getVar('table_permissions')) {
218
                $tableId   = $tables[$t]->getVar('table_id');
219
                $tableMid  = $tables[$t]->getVar('table_mid');
220
                $tableName = $tables[$t]->getVar('table_name');
221
                $fields    = $this->getTableFields($tableMid, $tableId);
222
                $fieldId   = 'id';
223
                $fieldMain = 'title';
224
                foreach (\array_keys($fields) as $f) {
225
                    $fieldName = $fields[$f]->getVar('field_name');
226
                    if (0 == $f) {
227
                        $fieldId = $fieldName;
228
                    }
229
                    if (1 == $fields[$f]->getVar('field_main')) {
230
                        $fieldMain = $fieldName;
231
                    }
232
                }
233
                $if_count   = $this->xc->getXcHandlerAllObj($tableName, $fieldMain, 0, 0, "\t\t");
234
                $getVar1    = $this->xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldId, true);
235
                $getVar2    = $this->xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldMain, true);
236
                $fe_content = $this->xc->getXcAddItem('permform', $getVar1, $getVar2, "\t\t\t");
237
                $if_table   = $this->xc->getXcHandlerCountObj($tableName, "\t");
238
                $if_count   .= $this->pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $fe_content, "\t\t");
239
                $if_count   .= $this->xc->getXcXoopsTplAssign('form', '$permform->render()', true, "\t\t");
240
                $if_table   .= $this->pc->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $if_count, false, "\t");
241
                $if_table   .= $this->xc->getXcEqualsOperator('$permFound', 'true', null, "\t");
242
                $cond       = "'approve_{$tableName}' === \$op || 'submit_{$tableName}' === \$op || 'view_{$tableName}' === \$op";
243
                $ret        .= $this->pc->getPhpCodeConditions($cond, '', '', $if_table, false);
244
            }
245
        }
246
247
        $ret       .= $this->pc->getPhpCodeUnset('permform');
248
        $elseInter = $this->xc->getXcRedirectHeader("'permissions.php'", '', '3', "{$language}NO_PERMISSIONS_SET", false, "\t");
249
        $elseInter .= $this->getSimpleString("exit();", "\t");
250
        $ret       .= $this->pc->getPhpCodeConditions('$permFound', ' !== ', 'true', $elseInter, false);
251
252
        return $ret;
253
    }
254
255
    /**
256
     * @public function render
257
     *
258
     * @param null
259
     *
260
     * @return bool|string
261
     */
262
    public function render()
263
    {
264
        $module        = $this->getModule();
265
        $filename      = $this->getFileName();
266
        $moduleDirname = $module->getVar('mod_dirname');
267
        $language      = $this->getLanguage($moduleDirname, 'AM');
268
        $content       = $this->getHeaderFilesComments($module);
269
        $content       .= $this->getPermissionsHeader($module, $language);
270
        $content       .= $this->getPermissionsSwitch($module, $language);
271
        $content       .= $this->getPermissionsBody($module, $language);
272
        $content       .= $this->getRequire('footer');
273
274
        $this->create($moduleDirname, 'admin', $filename, $content, \_AM_MODULEBUILDER_FILE_CREATED, \_AM_MODULEBUILDER_FILE_NOTCREATED);
275
276
        return $this->renderFile();
277
    }
278
}
279