Completed
Pull Request — master (#126)
by Gino
03:40
created

UserSubmit::render()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 26
rs 8.8571
c 5
b 2
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: UserSubmit.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class UserSubmit.
27
 */
28
class UserSubmit extends TDMCreateFile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /*
43
    *  @static function getInstance
44
    *  @param null
45
    */
46
    /**
47
     * @return UserSubmit
48
     */
49
    public static function getInstance()
50
    {
51
        static $instance = false;
52
        if (!$instance) {
53
            $instance = new self();
54
        }
55
56
        return $instance;
57
    }
58
59
    /*
60
    *  @public function write
61
    *  @param string $module
62
    *  @param mixed $table
63
    *  @param string $filename
64
    */
65
    /**
66
     * @param $module
67
     * @param $table
68
     * @param $filename
69
     */
70
    public function write($module, $table, $filename)
71
    {
72
        $this->setModule($module);
73
        $this->setTable($table);
74
        $this->setFileName($filename);
75
    }
76
77
    /*
78
     * @public function getUserSubmitHeader    
79
     * @param $moduleDirname
80
     *
81
     * @return string
82
     */
83
    public function getUserSubmitHeader($moduleDirname)
84
    {
85
        $xc = TDMCreateXoopsCode::getInstance();
86
        $pc = TDMCreatePhpCode::getInstance();
87
        $uc = UserXoopsCode::getInstance();
88
        $t = "\t";
89
        $ret = $this->getInclude();
90
        $ret .= $xc->getXcLoadLanguage('admin');
91
        $ret .= $pc->getPhpCodeCommentLine('It recovered the value of argument op in URL$');
92
        $ret .= $xc->getXcXoopsRequest('op', 'op', 'form');
93
        $ret .= $pc->getPhpCodeCommentLine('Template');
94
        $ret .= $uc->getUserTplMain($moduleDirname, 'submit');
95
        $ret .= $pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
96
        $ret .= $xc->getXcAddStylesheet();
97
        $ret .= "\$permSubmit = \$gpermHandler->checkRight('{$moduleDirname}_ac', 4, \$groups, \$GLOBALS['xoopsModule']->getVar('mid')) ? true : false;\n";
98
        $ret .= $pc->getPhpCodeCommentLine('Redirection if not permissions');
99
        $condIf = $xc->getXcRedirectHeader('index', '', '2', '_NOPERM', true, $t);
100
        $condIf .= $this->getSimpleString('exit();', $t);
101
102
        $ret .= $pc->getPhpCodeConditions('$permSubmit', ' == ', 'false', $condIf, false);
103
104
        return $ret;
105
    }
106
107
    /*
108
     * @public function getAdminPagesList
109
     * @param $tableName
110
     * @param $language
111
     *
112
     * @return string
113
     */
114
    public function getUserSubmitForm($tableName, $language)
115
    {
116
        $xc = TDMCreateXoopsCode::getInstance();
117
        $pc = TDMCreatePhpCode::getInstance();
118
        $uc = UserXoopsCode::getInstance();
119
        $ret = $pc->getPhpCodeCommentLine('Mavigation');
120
        $ret .= $xc->getXcEqualsOperator('$navigation', "{$language}SUBMIT_PROPOSER");
121
        $ret .= $xc->getXcTplAssign('navigation', '$navigation');
122
        $ret .= $pc->getPhpCodeCommentLine('Title of page');
123
        $ret .= $xc->getXcEqualsOperator('$title', "{$language}SUBMIT_PROPOSER . '&nbsp;-&nbsp;'");
124
        $ret .= $xc->getXcEqualsOperator('$title', "\$GLOBALS['xoopsModule']->name()", '.');
125
        $ret .= $xc->getXcTplAssign('xoops_pagetitle', '$title');
126
        $ret .= $pc->getPhpCodeCommentLine('Description');
127
        $ret .= $uc->getUserAddMeta('description', $language, 'SUBMIT_PROPOSER');
128
        $ret .= $pc->getPhpCodeCommentLine('Form Create');
129
        $ret .= $xc->getXcObjHandlerCreate($tableName);
130
        $ret .= $xc->getXcGetForm('form', $tableName, 'Obj');
131
        $ret .= $xc->getXcTplAssign('form', '$form->render()');
132
133
        return $ret;
134
    }
135
136
    /*
137
    *  @public function getUserSubmitSave
138
    *  @param string $moduleDirname
139
    *  @param string $tableName
140
    */
141
    /**
142
     * @param $moduleDirname
143
     * @param $table_id
144
     * @param $tableName
145
     *
146
     * @return string
147
     */
148
    public function getUserSubmitSave($moduleDirname, $fields, $tableName, $tableSubmit, $tableAutoincrement, $language)
149
    {
150
        $xc = TDMCreateXoopsCode::getInstance();
151
        $pc = TDMCreatePhpCode::getInstance();
152
        $ucfTableName = ucfirst($tableName);
0 ignored issues
show
Unused Code introduced by
$ucfTableName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
        $t = "\t";
154
        $ret = $pc->getPhpCodeCommentLine('Security Check', $t);
155
        $xoopsSecurityCheck = $xc->getXcSecurityCheck();
156
        $securityError = $xc->getXcSecurityErrors();
157
        $implode = $pc->getPhpCodeImplode(',', $securityError);
158
        $redirectError = $xc->getXcRedirectHeader($tableName, '', '3', $implode, true, $t."\t");
159
        $ret .= $pc->getPhpCodeConditions('!'.$xoopsSecurityCheck, '', '', $redirectError, false, $t);
160
        $ret .= $xc->getXcObjHandlerCreate($tableName, $t);
161
        $autoincrement = in_array(1, $tableAutoincrement) ? $tableAutoincrement : 0;
162
        if (in_array(1, $tableSubmit)) {
163
            $ret .= $xc->getXcSaveElements($moduleDirname, $tableName, $autoincrement, $fields, $t);
164
        }
165
        $ret .= $pc->getPhpCodeCommentLine('Insert Data', $t);
166
        $insert = $xc->getXcInsert($tableName, $tableName, 'Obj', 'Handler');
167
        $confirmOk = $xc->getXcRedirectHeader('index', '', '2', "{$language}FORM_OK", true, $t."\t");
168
        $ret .= $pc->getPhpCodeConditions($insert, '', '', $confirmOk, false, $t);
169
170
        $ret .= $pc->getPhpCodeCommentLine('Get Form Error');
171
        $ret .= $xc->getXcTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
172
        $ret .= $xc->getXcGetForm('form', $tableName, 'Obj');
173
        $ret .= $xc->getXcTplAssign('form', '$form->display()');
174
175
        return $ret;
176
    }
177
178
    /*
179
    *  @public function getUserSubmitFooter
180
    *  @param null
181
    */
182
    /**
183
     * @return string
184
     */
185
    public function getUserSubmitFooter($moduleDirname, $language)
186
    {
187
        $pc = TDMCreatePhpCode::getInstance();
188
        $uc = UserXoopsCode::getInstance();
189
        $stuModuleDirname = strtoupper($moduleDirname);
0 ignored issues
show
Unused Code introduced by
$stuModuleDirname is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
        $ret = $pc->getPhpCodeCommentLine('Breadcrumbs');
191
        $ret .= $uc->getUserBreadcrumbs($language, 'SUBMIT');
192
        $ret .= $this->getInclude('footer');
193
194
        return $ret;
195
    }
196
197
    /*
198
     *  @private function getUserSubmitSwitch
199
     *  @param $moduleDirname
200
     *  @param $tableId
201
     *  @param $tableMid
202
     *  @param $tableName
203
     *  @param $tableAutoincrement
204
     *  @param $language
205
     *
206
     * @return string
207
     */
208
    private function getUserSubmitSwitch($moduleDirname, $tableId, $tableMid, $tableName, $tableSubmit, $tableAutoincrement, $language)
209
    {
210
        $xc = TDMCreateXoopsCode::getInstance();
211
        $fields = $this->getTableFields($tableMid, $tableId);
212
        $cases = array('form' => array($this->getUserSubmitForm($tableName, $language)),
213
                    'save' => array($this->getUserSubmitSave($moduleDirname, $fields, $tableName, $tableSubmit, $tableAutoincrement, $language)), );
214
215
        return $xc->getXcSwitch('op', $cases, true);
216
    }
217
218
    /*
219
    *  @public function render
220
    *  @param null
221
    */
222
    /**
223
     * @return bool|string
224
     */
225
    public function render()
226
    {
227
        $module = $this->getModule();
228
        $table = $this->getTable();
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
229
        $filename = $this->getFileName();
230
        $moduleDirname = $module->getVar('mod_dirname');
231
        $tables = $this->getTableTables($module->getVar('mod_id'));
232
        $tableSubmit = array();
233
        $tableAutoincrement = array();
234
        foreach (array_keys($tables) as $t) {
235
            $tableId = $tables[$t]->getVar('table_id');
236
            $tableMid = $tables[$t]->getVar('table_mid');
237
            $tableName = $tables[$t]->getVar('table_name');
238
            $tableSubmit[] = $tables[$t]->getVar('table_submit');
239
            $tableAutoincrement[] = $tables[$t]->getVar('table_autoincrement');
240
        }
241
        $language = $this->getLanguage($moduleDirname, 'MA');
242
        $content = $this->getHeaderFilesComments($module, $filename);
243
        $content .= $this->getUserSubmitHeader($moduleDirname);
244
        $content .= $this->getUserSubmitSwitch($moduleDirname, $tableId, $tableMid, $tableName, $tableSubmit, $tableAutoincrement, $language);
0 ignored issues
show
Bug introduced by
The variable $tableId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $tableMid does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $tableName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
245
        $content .= $this->getUserSubmitFooter($moduleDirname, $language);
246
247
        $this->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
248
249
        return $this->renderFile();
250
    }
251
}
252