UserBroken::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Tdmcreate\Files\User;
2
3
use XoopsModules\Tdmcreate;
4
use XoopsModules\Tdmcreate\Files;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 */
15
/**
16
 * tdmcreate module.
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
20
 *
21
 * @since           2.5.0
22
 *
23
 * @author          Txmod Xoops http://www.txmodxoops.org
24
 *
25
 * @version         $Id: UserBroken.php 12258 2014-01-02 09:33:29Z timgno $
26
 */
27
28
/**
29
 * Class UserBroken.
30
 */
31
class UserBroken extends Files\CreateFile
32
{
33
    /**
34
     *  @public function constructor
35
     *  @param null
36
     */
37
38
    public function __construct()
39
    {
40
        parent::__construct();
41
    }
42
43
    /**
44
     *  @static function getInstance
45
     *  @param null
46
     * @return UserBroken
47
     */
48
    public static function getInstance()
49
    {
50
        static $instance = false;
51
        if (!$instance) {
52
            $instance = new self();
53
        }
54
55
        return $instance;
56
    }
57
58
    /**
59
     *  @public function write
60
     *  @param $module
61
     *  @param mixed $table
62
     *  @param $filename
63
     */
64
    public function write($module, $table, $filename)
65
    {
66
        $this->setModule($module);
67
        $this->setTable($table);
68
        $this->setFileName($filename);
69
    }
70
71
    /**
72
     *  @public function getUserBrokenHeader
73
     * @param $moduleDirname
74
     * @param $fields
75
     * @return string
76
     */
77
    public function getUserBrokenHeader($moduleDirname, $fields)
78
    {
79
        $xc = Tdmcreate\Files\CreateXoopsCode::getInstance();
80
        $pc = Tdmcreate\Files\CreatePhpCode::getInstance();
81
        $uc = UserXoopsCode::getInstance();
82
        $fieldId = $xc->getXcSaveFieldId($fields);
83
        $ccFieldId = $this->getCamelCase($fieldId, false, true);
84
        $ret = $this->getInclude();
85
        $ret .= $xc->getXcXoopsRequest('op', 'op', 'list');
86
        $ret .= $xc->getXcXoopsRequest($ccFieldId, $fieldId, '', 'Int');
87
        $ret .= $pc->getPhpCodeCommentLine('Template');
88
        $ret .= $uc->getUserTplMain($moduleDirname, 'broken');
89
        $ret .= $pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
90
        $ret .= $xc->getXcAddStylesheet();
91
        $ret .= $pc->getPhpCodeCommentLine('Redirection if not permissions');
92
        $condIf = $xc->getXcRedirectHeader('index', '', '2', '_NOPERM', true, "\t");
93
        $condIf .= $this->getSimpleString('exit();', "\t");
94
        $ret .= $pc->getPhpCodeConditions('$permSubmit', ' === ', 'false', $condIf);
95
96
        return $ret;
97
    }
98
99
    /**
100
     * @public function getAdminPagesList
101
     * @param $tableName
102
     * @param $language
103
     * @return string
104
     */
105
    public function getUserBrokenForm($tableName, $language)
106
    {
107
        $xc = Tdmcreate\Files\CreateXoopsCode::getInstance();
108
        $pc = Tdmcreate\Files\CreatePhpCode::getInstance();
109
        $uc = UserXoopsCode::getInstance();
110
        $ret = $pc->getPhpCodeCommentLine('Navigation');
111
        $ret .= $xc->getXcEqualsOperator('$navigation', "{$language}SUBMIT_PROPOSER", null, false, "\t\t");
112
        $ret .= $xc->getXcTplAssign('navigation', '$navigation', true, "\t\t");
113
        $ret .= $pc->getPhpCodeCommentLine('Title of page', null, "\t\t");
114
        $ret .= $xc->getXcEqualsOperator('$title', "{$language}SUBMIT_PROPOSER . '&nbsp;-&nbsp;'", null, false, "\t\t");
115
        $ret .= $xc->getXcEqualsOperator('$title', "\$GLOBALS['xoopsModule']->name()", '.', false, "\t\t");
116
        $ret .= $xc->getXcTplAssign('xoops_pagetitle', '$title', true, "\t\t");
117
        $ret .= $pc->getPhpCodeCommentLine('Description', null, "\t\t");
118
        $ret .= $uc->getUserAddMeta('description', $language, 'SUBMIT_PROPOSER', "\t\t");
119
        $ret .= $pc->getPhpCodeCommentLine('Form Create', null, "\t\t");
120
        $ret .= $xc->getXcObjHandlerCreate($tableName, "\t\t");
121
        $ret .= $xc->getXcGetForm('form', $tableName, 'Obj', "\t\t");
122
        $ret .= $xc->getXcTplAssign('form', '$form->render()', true, "\t\t");
123
124
        return $ret;
125
    }
126
127
    /**
128
     *  @public function getUserBrokenSave
129
     * @param $moduleDirname
130
     * @param $fields
131
     * @param $tableName
132
     * @param $tableSoleName
133
     * @param $tableAutoincrement
134
     * @param $language
135
     * @return string
136
     */
137
    public function getUserBrokenSave($moduleDirname, $fields, $tableName, $tableSoleName, $tableAutoincrement, $language)
138
    {
139
        $xc = Tdmcreate\Files\CreateXoopsCode::getInstance();
140
        $pc = Tdmcreate\Files\CreatePhpCode::getInstance();
141
        $fieldId = $xc->getXcSaveFieldId($fields);
0 ignored issues
show
Unused Code introduced by
The assignment to $fieldId is dead and can be removed.
Loading history...
142
        $ucfTableName = ucfirst($tableName);
0 ignored issues
show
Unused Code introduced by
The assignment to $ucfTableName is dead and can be removed.
Loading history...
143
        $ret = $pc->getPhpCodeCommentLine('Security Check');
144
        $xoopsSecurityCheck = $xc->getXcSecurityCheck();
145
        $securityError = $xc->getXcSecurityErrors();
146
        $implode = $pc->getPhpCodeImplode(',', $securityError);
147
        $redirectError = $xc->getXcRedirectHeader($tableName, '', '3', $implode, true, "\t\t\t");
148
        $ret .= $pc->getPhpCodeConditions($xoopsSecurityCheck, '', '', $redirectError, false, "\t\t");
149
        $ret .= $xc->getXcObjHandlerCreate($tableName, "\t\t");
150
151
        $ret .= $this->getSimpleString('$error = false;', "\t\t");
152
        $ret .= $this->getSimpleString("\$errorMessage = '';", "\t\t");
153
        $ret .= $pc->getPhpCodeCommentLine('Test first the validation', null, "\t\t");
154
        $ret .= $xc->getXcLoad('captcha', "\t\t");
155
        $ret .= $xc->getXcXoopsCaptcha("\t\t");
156
157
        $ret .= $pc->getPhpCodeConditions('!$xoopsCaptcha->verify()', '', '', "\t\t\t\$errorMessage .= \$xoopsCaptcha->getMessage().'<br>';\n\t\t\t\$error = true;\n", false, "\t\t");
158
159
        $ret .= $xc->getXcSaveElements($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $fields, "\t\t");
160
161
        $condElse = $pc->getPhpCodeCommentLine('Insert Data', null, "\t\t\t");
162
        $insert = $xc->getXcInsert($tableName, $tableName, 'Obj', true);
163
        $redirctHeader = $xc->getXcRedirectHeader('index', '', '2', "{$language}FORM_OK", true, "\t\t\t\t");
164
        $condElse .= $pc->getPhpCodeConditions($insert, '', '', $redirctHeader, false, "\t\t\t");
165
        $assigne = $xc->getXcTplAssign('error_message', '$errorMessage', true, "\t\t\t");
166
        $ret .= $pc->getPhpCodeConditions('$error', ' === ', 'true', $assigne, $condElse, "\t\t");
167
168
        $ret .= $pc->getPhpCodeCommentLine('Get Form Error', null, "\t\t");
169
        $ret .= $xc->getXcTplAssign('error', "\${$tableName}Obj->getHtmlErrors()", true, "\t\t");
170
        $ret .= $xc->getXcGetForm('form', $tableName, 'Obj', "\t\t");
171
        $ret .= $xc->getXcTplAssign('form', '$form->display()', true, "\t\t");
172
173
        return $ret;
174
    }
175
176
    /**
177
      *  @private function getUserSubmitSwitch
178
      *  @param $moduleDirname
179
     *  @param $tableName
180
     *  @param $tableSoleName
181
     *  @param $tableAutoincrement
182
     *  @param $language
183
     * @return bool|string
184
      */
185
    private function getUserBrokenSwitch($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $language)
186
    {
187
        $xc = Tdmcreate\Files\CreateXoopsCode::getInstance();
188
        $table = $this->getTable();
189
        $tableId = $table->getVar('table_id');
190
        $tableMid = $table->getVar('table_mid');
191
        $fields = $this->getTableFields($tableMid, $tableId);
192
        $cases = [
193
            'form' => [$this->getUserBrokenForm($tableName, $language)],
194
            'save' => [$this->getUserBrokenSave($moduleDirname, $fields, $tableName, $tableSoleName, $tableAutoincrement, $language)],
195
        ];
196
197
        return $xc->getXcSwitch('op', $cases, true, false, "\t");
198
    }
199
200
    /**
201
     *  @public function render
202
     *  @param null
203
     * @return bool|string
204
     */
205
    public function render()
206
    {
207
        $module = $this->getModule();
208
        $table = $this->getTable();
209
        $filename = $this->getFileName();
210
        $moduleDirname = $module->getVar('mod_dirname');
211
        $tableId = $table->getVar('table_id');
212
        $tableMid = $table->getVar('table_mid');
213
        $tableName = $table->getVar('table_name');
214
        $tableSoleName = $table->getVar('table_solename');
215
        $tableAutoincrement = $table->getVar('table_autoincrement');
216
        $fields = $this->getTableFields($tableMid, $tableId);
217
        $language = $this->getLanguage($moduleDirname, 'MA');
218
        $content = $this->getHeaderFilesComments($module, $filename);
219
        $content .= $this->getUserBrokenHeader($moduleDirname, $fields);
220
        $content .= $this->getUserBrokenSwitch($moduleDirname, $tableName, $tableSoleName, $tableAutoincrement, $language);
221
        $content .= $this->getInclude('footer');
222
223
        $this->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
224
225
        return $this->renderFile();
226
    }
227
}
228