Passed
Push — master ( b8cb12...0cff9c )
by Goffy
04:11
created

ClassSpecialFiles::renderConstants()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 46
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 35
nc 4
nop 0
dl 0
loc 46
rs 9.36
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files\Classes;
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
 * tc 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 ClassSpecialFiles.
31
 */
32
class ClassSpecialFiles extends Files\CreateFile
33
{
34
        
35
    /**
36
     * "className" attribute of the files.
37
     *
38
     * @var mixed
39
     */
40
    public $className = null;
41
    
42
    
43
    /**
44
     * @public function constructor
45
     *
46
     * @param null
47
     */
48
    public function __construct()
49
    {
50
        parent::__construct();
51
    }
52
53
    /**
54
     * @static function getInstance
55
     *
56
     * @return bool|ClassSpecialFiles
57
     */
58
    public static function getInstance()
59
    {
60
        static $instance = false;
61
        if (!$instance) {
62
            $instance = new self();
63
        }
64
65
        return $instance;
66
    }
67
68
    /**
69
     * @public function write
70
     *
71
     * @param string $module
72
     * @param string $table
73
     * @param mixed  $tables
74
     * @param        $filename
75
     */
76
    public function write($module, $table, $tables, $filename)
77
    {
78
        $this->setModule($module);
79
        $this->setTable($table);
80
        $this->setTables($tables);
81
        $this->setFileName($filename);
82
    }
83
84
    /**
85
     * @public function renderPermissionsHandler
86
     * @param null
87
     *
88
     * @return bool|string
89
     */
90
    public function getGlobalPerms($permId)
91
    {
92
        $pc             = Modulebuilder\Files\CreatePhpCode::getInstance();
93
        $xc             = Modulebuilder\Files\CreateXoopsCode::getInstance();
94
        $module         = $this->getModule();
95
        $moduleDirname  = $module->getVar('mod_dirname');
96
97
        $returnTrue     = $this->getSimpleString("return true;", "\t\t\t");
98
        $right     = '';
99
        $cond      = '';
100
        $funcname  = '';
101
        $comment   = '';
102
        switch ($permId) {
103
            case 4:
104
                $comment  .= $pc->getPhpCodeCommentMultiLine(['@public' => 'function permGlobalApprove', 'returns' => 'right for global approve', '' => '', '@param' => 'null', '@return' => 'bool'], "\t");
105
                $right    .= $xc->getXcCheckRight('$grouppermHandler', $moduleDirname . '_ac', 4, '$my_group_ids', '$mid', true, "\t\t\t");
106
                $cond     .= $pc->getPhpCodeConditions($right, '', '', $returnTrue, false, "\t\t");
107
                $funcname .= 'getPermGlobalApprove';
108
                break;
109
            case 8:
110
                $comment  .= $pc->getPhpCodeCommentMultiLine(['@public' => 'function permGlobalSubmit', 'returns' => 'right for global submit', '' => '', '@param' => 'null', '@return' => 'bool'], "\t");
111
                $cond     .= $pc->getPhpCodeConditions('$this->getGlobalApprove()', '', '', $returnTrue, false, "\t\t");
112
                $right    .= $xc->getXcCheckRight('$grouppermHandler', $moduleDirname . '_ac', 8, '$my_group_ids', '$mid', true, "\t\t\t");
113
                $cond     .= $pc->getPhpCodeConditions($right, '', '', $returnTrue, false, "\t\t");
114
                $funcname .= 'getPermGlobalSubmit';
115
                break;
116
            case 16:
117
                $comment  .= $pc->getPhpCodeCommentMultiLine(['@public' => 'function permGlobalView', 'returns' => 'right for global view', '' => '', '@param' => 'null', '@return' => 'bool'], "\t");
118
                $cond     .= $pc->getPhpCodeConditions('$this->getGlobalApprove()', '', '', $returnTrue, false, "\t\t");
119
                $cond     .= $pc->getPhpCodeConditions('$this->getGlobalSubmit()', '', '', $returnTrue, false, "\t\t");
120
                $right    .= $xc->getXcCheckRight('$grouppermHandler', $moduleDirname . '_ac', 16, '$my_group_ids', '$mid', true, "\t\t\t");
121
                $cond     .= $pc->getPhpCodeConditions($right, '', '', $returnTrue, false, "\t\t");
122
                $funcname .= 'getPermGlobalView';
123
                break;
124
            case 0:
125
            default:
126
                break;
127
        }
128
        $functions      = $comment;
129
        $globalContent  = $xc->getXcGetGlobal(['xoopsUser', 'xoopsModule'], "\t\t");
130
        $globalContent  .= $xc->getXcEqualsOperator('$currentuid', '0', null, "\t\t");
131
132
        $contIf         = $pc->getPhpCodeConditions("\$xoopsUser->isAdmin(\$xoopsModule->mid())", '', '', "\t" . $returnTrue, false, "\t\t\t");
133
        $contIf         .= $xc->getXcEqualsOperator('$currentuid', '$xoopsUser->uid()', null, "\t\t\t");
134
        $globalContent  .= $pc->getPhpCodeConditions('isset($xoopsUser)', ' && ', 'is_object($xoopsUser)', $contIf, false, "\t\t");
135
        $globalContent  .= $xc->getXcXoopsHandler('groupperm', "\t\t");
136
        $globalContent  .= $xc->getXcEqualsOperator('$mid', '$xoopsModule->mid()', null, "\t\t");
137
        $globalContent  .= $xc->getXcXoopsHandler('member', "\t\t");
138
139
        $contIfInt      = $xc->getXcEqualsOperator('$my_group_ids', '[XOOPS_GROUP_ANONYMOUS]', null, "\t\t\t");
140
        $contElseInt    = $xc->getXcEqualsOperator('$my_group_ids', '$memberHandler->getGroupsByUser($currentuid);', null, "\t\t\t");
141
        $globalContent  .= $pc->getPhpCodeConditions('0', ' == ', '$currentuid', $contIfInt, $contElseInt, "\t\t");
142
        $globalContent  .= $cond;
143
        $globalContent  .= $this->getSimpleString("return false;", "\t\t");
144
        $functions      .= $pc->getPhpCodeFunction($funcname, '', $globalContent, 'public ', false, "\t");
145
146
        return $functions;
147
    }
148
149
    /**
150
     * @public function render
151
     * @param null
152
     *
153
     * @return bool|string
154
     */
155
    public function renderClass()
156
    {
157
        $pc             = Modulebuilder\Files\CreatePhpCode::getInstance();
158
        $xc             = Modulebuilder\Files\CreateXoopsCode::getInstance();
159
        $module         = $this->getModule();
160
        $filename       = $this->getFileName();
161
        $moduleDirname  = $module->getVar('mod_dirname');
162
        $namespace      = $pc->getPhpCodeNamespace(['XoopsModules', $moduleDirname]);
163
        $content        = $this->getHeaderFilesComments($module, null, $namespace);
164
        $content        .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname]);
165
        $content        .= $pc->getPhpCodeDefined();
166
        $content        .= $pc->getPhpCodeCommentMultiLine(['Class Object' => $this->className]);
167
        $cCl            = $pc->getPhpCodeCommentMultiLine(['Constructor' => '', '' => '', '@param' => 'null'], "\t");
168
        $constr         = '';
169
        $cCl            .= $pc->getPhpCodeFunction('__construct', '', $constr, 'public ', false, "\t");
170
        $arrGetInstance = ['@static function' => '&getInstance', '' => '', '@param' => 'null'];
171
        $cCl            .= $pc->getPhpCodeCommentMultiLine($arrGetInstance, "\t");
172
        $getInstance    = $pc->getPhpCodeVariableClass('static', 'instance', 'false', "\t\t");
173
        $instance       = $xc->getXcEqualsOperator('$instance', 'new self()', null, "\t\t\t");
174
        $getInstance    .= $pc->getPhpCodeConditions('!$instance', '', '', $instance, false, "\t\t");
175
        $cCl            .= $pc->getPhpCodeFunction('getInstance', '', $getInstance, 'public static ', false, "\t");
176
        $content        .= $pc->getPhpCodeClass($this->className, $cCl, '\XoopsObject');
177
178
        $this->create($moduleDirname, 'class', $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
179
180
181
182
        return $this->renderFile();
183
    }
184
185
    /**
186
     * @public function renderPermissionsHandler
187
     * @param null
188
     *
189
     * @return bool|string
190
     */
191
    public function renderPermissionsHandler()
192
    {
193
        $pc             = Modulebuilder\Files\CreatePhpCode::getInstance();
194
        $module         = $this->getModule();
195
        $filename       = $this->getFileName();
196
        $moduleDirname  = $module->getVar('mod_dirname');
197
        $namespace      = $pc->getPhpCodeNamespace(['XoopsModules', $moduleDirname]);
198
        $content        = $this->getHeaderFilesComments($module, null, $namespace);
199
        $content        .= $pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname]);
200
        $content        .= $pc->getPhpCodeDefined();
201
        $content        .= $pc->getPhpCodeCommentMultiLine(['Class Object' => $this->className]);
202
203
        $constr         = $pc->getPhpCodeCommentMultiLine(['Constructor' => '', '' => '', '@param' => 'null'], "\t");
204
        $constr         .= $pc->getPhpCodeFunction('__construct', '', '', 'public ', false, "\t");
205
        $functions      = $constr;
206
        $functions      .= $this->getGlobalPerms(4);
207
        $functions      .= $this->getGlobalPerms(8);
208
        $functions      .= $this->getGlobalPerms(16);
209
210
        $content        .= $pc->getPhpCodeClass($this->className, $functions, '\XoopsPersistableObjectHandler');
211
        $this->create($moduleDirname, 'class', $filename, $content, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
212
213
        return $this->renderFile();
214
    }
215
216
    public function renderConstantsInterface()
217
    {
218
        $pc               = Modulebuilder\Files\CreatePhpCode::getInstance();
219
220
        $module           = $this->getModule();
221
        $filename         = $this->getFileName();
222
        $tables           = $this->getTables();
223
        $tablePermissions = [];
224
        foreach (array_keys($tables) as $t) {
225
            $tablePermissions[]   = $tables[$t]->getVar('table_permissions');
226
        }
227
        $moduleDirname  = $module->getVar('mod_dirname');
228
        $namespace      = $pc->getPhpCodeNamespace(['XoopsModules', $moduleDirname]);
229
        $contentFile    = $this->getHeaderFilesComments($module, null, $namespace);
230
        $contentFile    .= $pc->getPhpCodeCommentMultiLine(['Interface ' => $this->className]);
231
232
        $contentClass   = $pc->getPhpCodeBlankLine();
233
        $contentClass .= $pc->getPhpCodeCommentLine('Constants for tables', '', "\t");
234
        foreach (array_keys($tables) as $t) {
235
            $tablePermissions[]   = $tables[$t]->getVar('table_permissions');
236
            $stuTableName = mb_strtoupper($tables[$t]->getVar('table_name'));
237
            $contentClass .= $pc->getPhpCodeConstant("TABLE_" . $stuTableName, $t, "\t",'const');
238
        }
239
240
        $contentClass .= $pc->getPhpCodeBlankLine();
241
        $contentClass .= $pc->getPhpCodeCommentLine('Constants for status', '', "\t");
242
        $contentClass .= $pc->getPhpCodeConstant("STATUS_NONE     ", 0, "\t",'const');
243
        $contentClass .= $pc->getPhpCodeConstant("STATUS_OFFLINE  ", 1, "\t",'const');
244
        $contentClass .= $pc->getPhpCodeConstant("STATUS_SUBMITTED", 2, "\t",'const');
245
        $contentClass .= $pc->getPhpCodeConstant("STATUS_APPROVED ", 3, "\t",'const');
246
        $contentClass .= $pc->getPhpCodeConstant("STATUS_BROKEN   ", 4, "\t",'const');
247
        if (in_array(1, $tablePermissions)) {
248
            $constPerm = $pc->getPhpCodeBlankLine();
249
            $constPerm .= $pc->getPhpCodeCommentLine('Constants for permissions', '', "\t");
250
            $constPerm .= $pc->getPhpCodeConstant("PERM_GLOBAL_NONE   ", 0, "\t",'const');
251
            $constPerm .= $pc->getPhpCodeConstant("PERM_GLOBAL_VIEW   ", 1,"\t", 'const');
252
            $constPerm .= $pc->getPhpCodeConstant("PERM_GLOBAL_SUBMIT ", 2,"\t", 'const');
253
            $constPerm .= $pc->getPhpCodeConstant("PERM_GLOBAL_APPROVE", 3,"\t", 'const');
254
            $contentClass .= $constPerm;
255
        }
256
        $contentClass        .= $pc->getPhpCodeBlankLine();
257
258
        $contentFile   .= $pc->getPhpCodeInterface($this->className, $contentClass);
259
260
        $this->create($moduleDirname, 'class', $filename, $contentFile, _AM_MODULEBUILDER_FILE_CREATED, _AM_MODULEBUILDER_FILE_NOTCREATED);
261
262
        return $this->renderFile();
263
    }
264
}
265