AdminPermissions   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 101
dl 0
loc 202
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
B getPermissionsBody() 0 47 6
A render() 0 15 1
A write() 0 5 1
A getPermissionsHeader() 0 29 3
A getInstance() 0 8 2
A getPermissionsSwitch() 0 30 1
A __construct() 0 3 1
1
<?php namespace XoopsModules\Tdmcreate\Files\Admin;
2
3
use XoopsModules\Tdmcreate;
4
use XoopsModules\Tdmcreate\Files;
5
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
/**
17
 * tdmcreate module.
18
 *
19
 * @copyright       XOOPS Project (https://xoops.org)
20
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
21
 *
22
 * @since           2.5.0
23
 *
24
 * @author          Txmod Xoops http://www.txmodxoops.org
25
 *
26
 * @version         $Id: AdminPermissions.php 12258 2014-01-02 09:33:29Z timgno $
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 string
69
     */
70
    public function write($module, $tables, $filename)
71
    {
72
        $this->setModule($module);
73
        $this->setTables($tables);
74
        $this->setFileName($filename);
75
    }
76
77
    /**
78
     * @private function getPermissionsHeader
79
     *
80
     * @param $module
81
     * @param $language
82
     *
83
     * @return string
84
     */
85
    private function getPermissionsHeader($module, $language)
86
    {
87
        $pc = Tdmcreate\Files\CreatePhpCode::getInstance();
88
        $xc = Tdmcreate\Files\CreateXoopsCode::getInstance();
89
        $cc = Tdmcreate\Files\Classes\ClassXoopsCode::getInstance();
90
        $axc = Tdmcreate\Files\Admin\AdminXoopsCode::getInstance();
91
        $moduleDirname = $module->getVar('mod_dirname');
92
        $tables = $this->getTableTables($module->getVar('mod_id'));
93
        foreach (array_keys($tables) as $t) {
94
            if (1 == $tables[$t]->getVar('table_permissions')) {
95
                $tableName = $tables[$t]->getVar('table_name');
96
            }
97
        }
98
        $ret = $this->getInclude('header');
99
        $ret .= $pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
100
        $ret .= $xc->getXoopsHandlerLine($moduleDirname, $tableName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tableName does not seem to be defined for all execution paths leading up to this point.
Loading history...
101
        $ret .= $pc->getPhpCodeCommentLine('Check admin have access to this page');
102
        $ret .= $axc->getAdminTemplateMain($moduleDirname, 'permissions');
103
        $ret .= $xc->getXcTplAssign('navigation', "\$adminObject->displayNavigation('permissions.php')");
104
        $ret .= $xc->getXcXoopsRequest('op', 'op', 'global');
105
        $ret .= $xc->getXcLoad('XoopsFormLoader');
106
        $optionsSelect = [
107
            'global' => "{$language}PERMISSIONS_GLOBAL", 'approve' => "{$language}PERMISSIONS_APPROVE",
108
            'submit' => "{$language}PERMISSIONS_SUBMIT", 'view' => "{$language}PERMISSIONS_VIEW",
109
        ];
110
        $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

110
        $formSelect = $xc->getXoopsFormSelectExtraOptions('formSelect', '\'\'', 'op', $optionsSelect, /** @scrutinizer ignore-type */ 'onchange="document.fselperm.submit()"');
Loading history...
111
        $ret .= $cc->getXoopsSimpleForm('permTableForm', 'formSelect', $formSelect, '\'\'', 'fselperm', 'permissions');
112
113
        return $ret;
114
    }
115
116
    /**
117
     *  @private function getPermissionsSwitch
118
     *  @param $moduleDirname
119
     *  @param $language
120
     *
121
     *  @return string
122
     */
123
    private function getPermissionsSwitch($moduleDirname, $language)
124
    {
125
        $pc = Tdmcreate\Files\CreatePhpCode::getInstance();
126
        $cases = [
127
            'global'  => [
128
                "\$formTitle = {$language}PERMISSIONS_GLOBAL;",
129
                "\$permName = '{$moduleDirname}_ac';",
130
                "\$permDesc = {$language}PERMISSIONS_GLOBAL_DESC;",
131
                "\$globalPerms = array( '4' => {$language}PERMISSIONS_GLOBAL_4, '8' => {$language}PERMISSIONS_GLOBAL_8, '16' => {$language}PERMISSIONS_GLOBAL_16 );",
132
            ],
133
            'approve' => [
134
                "\$formTitle = {$language}PERMISSIONS_APPROVE;",
135
                "\$permName = '{$moduleDirname}_approve';",
136
                "\$permDesc = {$language}PERMISSIONS_APPROVE_DESC;",
137
            ],
138
            'submit'  => [
139
                "\$formTitle = {$language}PERMISSIONS_SUBMIT;",
140
                "\$permName = '{$moduleDirname}_submit';",
141
                "\$permDesc = {$language}PERMISSIONS_SUBMIT_DESC;",
142
            ],
143
            'view'    => [
144
                "\$formTitle = {$language}PERMISSIONS_VIEW;",
145
                "\$permName = '{$moduleDirname}_view';",
146
                "\$permDesc = {$language}PERMISSIONS_VIEW_DESC;",
147
            ],
148
        ];
149
150
        $contentSwitch = $pc->getPhpCodeCaseSwitch($cases, true, false, "\t");
151
152
        return $pc->getPhpCodeSwitch('op', $contentSwitch);
153
    }
154
155
    /**
156
     *  @private function getPermissionsBody
157
     *
158
     *  @param string $module
159
     *  @param string $language
160
     *
161
     *  @return string
162
     */
163
    private function getPermissionsBody($module, $language)
164
    {
165
        $pc = Tdmcreate\Files\CreatePhpCode::getInstance();
166
        $xc = Tdmcreate\Files\CreateXoopsCode::getInstance();
167
        $tables = $this->getTableTables($module->getVar('mod_id'));
168
        $tableName = '';
169
        foreach (array_keys($tables) as $t) {
170
            if (1 == $tables[$t]->getVar('table_permissions')) {
171
                $tableId = $tables[$t]->getVar('table_id');
172
                $tableMid = $tables[$t]->getVar('table_mid');
173
                $tableName = $tables[$t]->getVar('table_name');
174
            }
175
        }
176
        $ucfTableName = ucfirst($tableName);
0 ignored issues
show
Unused Code introduced by
The assignment to $ucfTableName is dead and can be removed.
Loading history...
177
        $fields = $this->getTableFields($tableMid, $tableId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tableMid does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $tableId does not seem to be defined for all execution paths leading up to this point.
Loading history...
178
        $fieldId = 'id';
179
        $fieldMain = 'title';
180
        foreach (array_keys($fields) as $f) {
181
            $fieldName = $fields[$f]->getVar('field_name');
182
            if (0 == $f) {
183
                $fieldId = $fieldName;
184
            }
185
            if (1 == $fields[$f]->getVar('field_main')) {
186
                $fieldMain = $fieldName;
187
            }
188
        }
189
190
        $ret = $xc->getXcGetVar('moduleId', 'xoopsModule', 'mid');
191
        $ret .= $xc->getXcGroupPermForm('permform', '$formTitle', '$moduleId', '$permName', '$permDesc', "'admin/permissions.php'");
192
        $foreach1 = $xc->getXcAddItem('permform', '$gPermId', '$gPermName', "\t");
193
        $if1 = $pc->getPhpCodeForeach('globalPerms', false, 'gPermId', 'gPermName', $foreach1, "\t");
194
        $if1 .= $xc->getXcTplAssign('form', '$permform->render()', true, "\t");
195
        $else = $xc->getXcObjHandlerCount($tableName, "\t");
196
        $if2 = $xc->getXcObjHandlerAll($tableName, $fieldMain, 0, 0, "\t\t");
197
        $getVar1 = $xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldId, true);
198
        $getVar2 = $xc->getXcGetVar('', "{$tableName}All[\$i]", $fieldMain, true);
199
        $foreach2 = $xc->getXcAddItem('permform', $getVar1, $getVar2, "\t")."\r";
200
        $if2 .= $pc->getPhpCodeForeach("{$tableName}All", true, false, 'i', $foreach2, "\t\t");
201
        $if2 .= $xc->getXcTplAssign('form', '$permform->render()', true, "\t\t");
202
        $elseInter = $xc->getXcRedirectHeader($tableName, '?op=new', '3', "{$language}NO_PERMISSIONS_SET", true, "\t\t");
203
        $elseInter .= $this->getSimpleString("\t\texit();");
204
        $else .= $pc->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $if2, $elseInter, "\t");
205
206
        $ret .= $pc->getPhpCodeConditions('$op', ' === ', "'global'", $if1, $else);
207
        $ret .= $pc->getPhpCodeUnset('permform');
208
209
        return $ret;
210
    }
211
212
    /**
213
     *  @public function render
214
     *
215
     *  @param null
216
     *
217
     *  @return bool|string
218
     */
219
    public function render()
220
    {
221
        $module = $this->getModule();
222
        $filename = $this->getFileName();
223
        $moduleDirname = $module->getVar('mod_dirname');
224
        $language = $this->getLanguage($moduleDirname, 'AM');
225
        $content = $this->getHeaderFilesComments($module, $filename);
226
        $content .= $this->getPermissionsHeader($module, $language);
227
        $content .= $this->getPermissionsSwitch($moduleDirname, $language);
228
        $content .= $this->getPermissionsBody($module, $language);
229
        $content .= $this->getInclude('footer');
230
231
        $this->create($moduleDirname, 'admin', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
232
233
        return $this->renderFile();
234
    }
235
}
236