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\Tdmcreate\Files\Admin;
4
5
use XoopsModules\Tdmcreate;
6
use XoopsModules\Tdmcreate\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
 * tdmcreate 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 AdminPermissions.
31
 */
32
class AdminPermissions extends Files\CreateFile
33
{
34
    /**
35
     * @public function constructor
36
     *
37
     * @param null
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * @static function getInstance
46
     *
47
     * @param null
48
     *
49
     * @return AdminPermissions
50
     */
51
    public static function getInstance()
52
    {
53
        static $instance = false;
54
        if (!$instance) {
55
            $instance = new self();
56
        }
57
58
        return $instance;
59
    }
60
61
    /**
62
     * @public function write
63
     *
64
     * @param string $module
65
     * @param mixed  $tables
66
     * @param string $filename
67
     *
68
     * @return null
69
     */
70
    public function write($module, $tables, $filename)
71
    {
72
        $this->setModule($module);
73
        $this->setTables($tables);
74
        $this->setFileName($filename);
75
        return null;
76
    }
77
78
    /**
79
     * @private function getPermissionsHeader
80
     *
81
     * @param $module
82
     * @param $language
83
     *
84
     * @return string
85
     */
86
    private function getPermissionsHeader($module, $language)
87
    {
88
        $pc            = Tdmcreate\Files\CreatePhpCode::getInstance();
89
        $xc            = Tdmcreate\Files\CreateXoopsCode::getInstance();
90
        $cc            = Tdmcreate\Files\Classes\ClassXoopsCode::getInstance();
91
        $axc           = Tdmcreate\Files\Admin\AdminXoopsCode::getInstance();
92
        $moduleDirname = $module->getVar('mod_dirname');
93
        $tables        = $this->getTableTables($module->getVar('mod_id'));
94
        $tableNames    = [];
95
        foreach (array_keys($tables) as $t) {
96
            if (1 == $tables[$t]->getVar('table_permissions')) {
97
                $tableNames[] = $tables[$t]->getVar('table_name');
98
            }
99
        }
100
        $ret           = $pc->getPhpCodeUseNamespace(['Xmf', 'Request'], '', '');
101
        $ret           .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', '');
102
        $ret           .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants']);
103
        $ret           .= $this->getInclude('header');
104
        $ret           .= $pc->getPhpCodeBlankLine();
105
        $ret           .= $pc->getPhpCodeCommentLine('Template Index');
106
        $ret           .= $axc->getAdminTemplateMain($moduleDirname, 'permissions');
107
        $ret           .= $xc->getXcXoopsTplAssign('navigation', "\$adminObject->displayNavigation('permissions.php')");
108
        $ret           .= $pc->getPhpCodeBlankLine();
109
        $ret           .= $xc->getXcXoopsRequest('op', 'op', 'global');
110
        $ret           .= $pc->getPhpCodeBlankLine();
111
        $ret           .= $pc->getPhpCodeCommentLine('Get Form');
112
        $ret           .= $pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
113
        $ret           .= $xc->getXcXoopsLoad('XoopsFormLoader');
114
        $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...
115
        foreach ($tableNames as $tableName) {
116
            $ucfTablename = ucfirst($tableName);
117
            $optionsSelect["approve_{$tableName}"] = "{$language}PERMISSIONS_APPROVE . ' {$ucfTablename}'";
118
            $optionsSelect["submit_{$tableName}"] = "{$language}PERMISSIONS_SUBMIT . ' {$ucfTablename}'";
119
            $optionsSelect["view_{$tableName}"] = "{$language}PERMISSIONS_VIEW . ' {$ucfTablename}'";
120
        }
121
        $formSelect    = $xc->getXoopsFormSelectExtraOptions('formSelect', '\'\'', 'op', $optionsSelect, 'onchange="document.fselperm.submit()"');
0 ignored issues
show
Bug introduced by
'onchange="document.fselperm.submit()"' of type string is incompatible with the type boolean expected by parameter $setExtra of XoopsModules\Tdmcreate\F...ormSelectExtraOptions(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        $formSelect    = $xc->getXoopsFormSelectExtraOptions('formSelect', '\'\'', 'op', $optionsSelect, /** @scrutinizer ignore-type */ 'onchange="document.fselperm.submit()"');
Loading history...
122
        $ret           .= $cc->getXoopsSimpleForm('permTableForm', 'formSelect', $formSelect, '\'\'', 'fselperm', 'permissions');
123
124
        return $ret;
125
    }
126
127
    /**
128
     * @private function getPermissionsSwitch
129
     * @param $module
130
     * @param $language
131
     *
132
     * @return string
133
     */
134
    private function getPermissionsSwitch($module, $language)
135
    {
136
        $pc    = Tdmcreate\Files\CreatePhpCode::getInstance();
137
138
        $moduleDirname = $module->getVar('mod_dirname');
139
        $tables        = $this->getTableTables($module->getVar('mod_id'));
140
        $t = "\t\t";
141
        $n = "\n";
142
        $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...
143
                "{$t}\$formTitle = {$language}PERMISSIONS_GLOBAL;{$n}",
144
                "{$t}\$permName = '{$moduleDirname}_ac';{$n}",
145
                "{$t}\$permDesc = {$language}PERMISSIONS_GLOBAL_DESC;{$n}",
146
                "{$t}\$globalPerms = array( '4' => {$language}PERMISSIONS_GLOBAL_4, '8' => {$language}PERMISSIONS_GLOBAL_8, '16' => {$language}PERMISSIONS_GLOBAL_16 );{$n}",
147
                ];
148
        foreach (array_keys($tables) as $i) {
149
            if (1 == $tables[$i]->getVar('table_permissions')) {
150
                $tableName = $tables[$i]->getVar('table_name');
151
                $ucfTablename = ucfirst($tableName);
152
                $cases["approve_{$tableName}"] = [
153
                    "{$t}\$formTitle = {$language}PERMISSIONS_APPROVE;{$n}",
154
                    "{$t}\$permName = '{$moduleDirname}_approve_{$tableName}';{$n}",
155
                    "{$t}\$permDesc = {$language}PERMISSIONS_APPROVE_DESC . ' {$ucfTablename}';{$n}",
156
                    "{$t}\$handler = \$helper->getHandler('{$tableName}');{$n}",
157
                ];
158
                $cases["submit_{$tableName}"] = [
159
                    "{$t}\$formTitle = {$language}PERMISSIONS_SUBMIT;{$n}",
160
                    "{$t}\$permName = '{$moduleDirname}_submit_{$tableName}';{$n}",
161
                    "{$t}\$permDesc = {$language}PERMISSIONS_SUBMIT_DESC . ' {$ucfTablename}';{$n}",
162
                    "{$t}\$handler = \$helper->getHandler('{$tableName}');{$n}",
163
                ];
164
                $cases["view_{$tableName}"] = [
165
                    "{$t}\$formTitle = {$language}PERMISSIONS_VIEW;{$n}",
166
                    "{$t}\$permName = '{$moduleDirname}_view_{$tableName}';{$n}",
167
                    "{$t}\$permDesc = {$language}PERMISSIONS_VIEW_DESC . ' {$ucfTablename}';{$n}",
168
                    "{$t}\$handler = \$helper->getHandler('{$tableName}');{$n}",
169
                ];
170
            }
171
        }
172
        $contentSwitch = $pc->getPhpCodeCaseSwitch($cases, true, false, "\t");
173
174
        return $pc->getPhpCodeSwitch('op', $contentSwitch);
175
    }
176
177
    /**
178
     * @private function getPermissionsBody
179
     *
180
     * @param string $module
181
     * @param string $language
182
     *
183
     * @return string
184
     */
185
    private function getPermissionsBody($module, $language)
186
    {
187
        $pc       = Tdmcreate\Files\CreatePhpCode::getInstance();
188
        $xc       = Tdmcreate\Files\CreateXoopsCode::getInstance();
189
        $tables   = $this->getTableTables($module->getVar('mod_id'));
190
191
        $ret      = $xc->getXcGetVar('moduleId', 'xoopsModule', 'mid');
192
        $ret      .= $xc->getXcXoopsFormGroupPerm('permform', '$formTitle', '$moduleId', '$permName', '$permDesc', "'admin/permissions.php'");
193
        $ret      .= $xc->getXcEqualsOperator('$permFound', 'false');
194
        $foreach1 = $xc->getXcAddItem('permform', '$gPermId', '$gPermName', "\t\t");
195
        $if1      = $pc->getPhpCodeForeach('globalPerms', false, 'gPermId', 'gPermName', $foreach1, "\t");
196
        $if1      .= $xc->getXcXoopsTplAssign('form', '$permform->render()', true, "\t");
197
        $if1      .= $xc->getXcEqualsOperator('$permFound', 'true', null, "\t");
198
        $ret      .= $pc->getPhpCodeConditions('$op', ' === ', "'global'", $if1, false);
199
200
        foreach (array_keys($tables) as $t) {
201
            if (1 == $tables[$t]->getVar('table_permissions')) {
202
                $tableId   = $tables[$t]->getVar('table_id');
203
                $tableMid  = $tables[$t]->getVar('table_mid');
204
                $tableName = $tables[$t]->getVar('table_name');
205
                $fields    = $this->getTableFields($tableMid, $tableId);
206
                $fieldId   = 'id';
207
                $fieldMain = 'title';
208
                foreach (array_keys($fields) as $f) {
209
                    $fieldName = $fields[$f]->getVar('field_name');
210
                    if (0 == $f) {
211
                        $fieldId = $fieldName;
212
                    }
213
                    if (1 == $fields[$f]->getVar('field_main')) {
214
                        $fieldMain = $fieldName;
215
                    }
216
                }
217
                $if_count   = $xc->getXcHandlerAllObj($tableName, $fieldMain, 0, 0, "\t\t");
218
                $getVar1    = $xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldId, true);
219
                $getVar2    = $xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldMain, true);
220
                $fe_content = $xc->getXcAddItem('permform', $getVar1, $getVar2, "\t\t\t");
221
                $if_table   = $xc->getXcHandlerCountObj($tableName, "\t");
222
                $if_count   .= $pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $fe_content, "\t\t");
223
                $if_count   .= $xc->getXcXoopsTplAssign('form', '$permform->render()', true, "\t\t");
224
                $if_table   .= $pc->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $if_count, false, "\t");
225
                $if_table   .= $xc->getXcEqualsOperator('$permFound', 'true', null, "\t");
226
                $cond       = "\$op === 'approve_{$tableName}' || \$op === 'submit_{$tableName}' || \$op === 'view_{$tableName}'";
227
                $ret        .= $pc->getPhpCodeConditions($cond, '', '', $if_table, false);
228
            }
229
        }
230
231
        $ret       .= $pc->getPhpCodeUnset('permform');
232
        $elseInter = $xc->getXcRedirectHeader("'permissions.php'", '', '3', "{$language}NO_PERMISSIONS_SET", false, "\t");
233
        $elseInter .= $this->getSimpleString("exit();", "\t");
234
        $ret       .= $pc->getPhpCodeConditions('$permFound', ' !== ', 'true', $elseInter, false);
235
236
        return $ret;
237
    }
238
239
    /**
240
     * @public function render
241
     *
242
     * @param null
243
     *
244
     * @return bool|string
245
     */
246
    public function render()
247
    {
248
        $module        = $this->getModule();
249
        $filename      = $this->getFileName();
250
        $moduleDirname = $module->getVar('mod_dirname');
251
        $language      = $this->getLanguage($moduleDirname, 'AM');
252
        $content       = $this->getHeaderFilesComments($module);
253
        $content       .= $this->getPermissionsHeader($module, $language);
254
        $content       .= $this->getPermissionsSwitch($module, $language);
255
        $content       .= $this->getPermissionsBody($module, $language);
256
        $content       .= $this->getInclude('footer');
257
258
        $this->create($moduleDirname, 'admin', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
259
260
        return $this->renderFile();
261
    }
262
}
263