Completed
Pull Request — master (#4)
by Michael
01:27
created

manage.php ➔ addEditForm()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 45
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 36
nc 2
nop 13
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 49 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * ****************************************************************************
4
 * MYIFRAME - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
6
 * ****************************************************************************
7
 */
8
9
include __DIR__ . '/../../../include/cp_header.php';
10
include_once XOOPS_ROOT_PATH . '/modules/myiframe/include/functions.php';
11
12
// Verify if the table is up to date
13
if (!myiframe_FieldExists('frame_frameid', $xoopsDB->prefix('myiframe'))) {
14
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . ' CHANGE `frameid` `frame_frameid` INT( 8 ) NOT NULL AUTO_INCREMENT');
15
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `created` `frame_created` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0'");
16
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `uid` `frame_uid` MEDIUMINT( 8 ) UNSIGNED NOT NULL DEFAULT '0'");
17
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . ' CHANGE `longdesc` `frame_description` VARCHAR( 255 ) NOT NULL');
18
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . ' CHANGE `width` `frame_width` VARCHAR( 15 ) NOT NULL');
19
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . ' CHANGE `height` `frame_height` VARCHAR( 15 ) NOT NULL');
20
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `align` `frame_align` SMALLINT( 2 ) NOT NULL DEFAULT '0'");
21
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `frameborder` `frame_frameborder` SMALLINT( 3 ) NOT NULL DEFAULT '0'");
22
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `marginwidth` `frame_marginwidth` SMALLINT( 3 ) NOT NULL DEFAULT '0'");
23
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `marginheight` `frame_marginheight` SMALLINT( 3 ) NOT NULL DEFAULT '0'");
24
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `scrolling` `frame_scrolling` SMALLINT( 1 ) NOT NULL DEFAULT '0'");
25
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . " CHANGE `hits` `frame_hits` INT( 8 ) UNSIGNED NOT NULL DEFAULT '0'");
26
    $result = $xoopsDB->queryF('ALTER TABLE ' . $xoopsDB->prefix('myiframe') . ' CHANGE `url` `frame_url` VARCHAR( 255 ) NOT NULL');
27
    header('Location : ' . XOOPS_URL . '/modules/system/admin.php?fct=modulesadmin&op=update&module=myiframe');
28
}
29
30
$module_id     = $xoopsModule->getVar('mid');
31
$op            = 'default';
32
$iframeHandler = xoops_getModuleHandler('myiframe', 'myiframe');
33
34
/**
35
 * @param $frameid
36
 * @param $Action
37
 * @param $FormTitle
38
 * @param $longdesc
39
 * @param $width
40
 * @param $height
41
 * @param $align
42
 * @param $frameborder
43
 * @param $marginwidth
44
 * @param $marginheight
45
 * @param $scrolling
46
 * @param $url
47
 * @param $LabelSubmitButton
48
 */
49
function addEditForm($frameid, $Action, $FormTitle, $longdesc, $width, $height, $align, $frameborder, $marginwidth, $marginheight, $scrolling, $url, $LabelSubmitButton)
50
{
51
    include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
52
    global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
53
54
    $sform = new XoopsThemeForm($FormTitle, 'indexform', XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/admin/manage.php');
55
    $sform->addElement(new XoopsFormText(_AM_MYIFRAME_DESC, 'longdesc', 50, 255, $longdesc), false);
56
    $sform->addElement(new XoopsFormText(_AM_MYIFRAME_WIDTH, 'width', 10, 15, $width), false);
57
    $sform->addElement(new XoopsFormText(_AM_MYIFRAME_HEIGHT, 'height', 10, 15, $height), false);
58
59
    $selalign = new XoopsFormSelect(_AM_MYIFRAME_ALIGN, 'align', $align);
60
    $selalign->addOption(1, _AM_MYIFRAME_ALIGN_TOP);
61
    $selalign->addOption(2, _AM_MYIFRAME_ALIGN_MIDDLE);
62
    $selalign->addOption(3, _AM_MYIFRAME_ALIGN_BOTTOM);
63
    $selalign->addOption(4, _AM_MYIFRAME_ALIGN_LEFT);
64
    $selalign->addOption(5, _AM_MYIFRAME_ALIGN_RIGHT);
65
    $selalign->setValue($align);
66
    $sform->addElement($selalign);
67
68
    $sform->addElement(new XoopsFormText(_AM_MYIFRAME_FRAMEBORDER, 'frameborder', 3, 3, $frameborder), false);
69
    $sform->addElement(new XoopsFormText(_AM_MYIFRAME_MARGINWIDTH, 'marginwidth', 3, 3, $marginwidth), false);
70
    $sform->addElement(new XoopsFormText(_AM_MYIFRAME_MARGINHEIGHT, 'marginheight', 3, 3, $marginheight), false);
71
72
    $selscroll = new XoopsFormSelect(_AM_MYIFRAME_SCROLLING, 'scrolling', $scrolling);
73
    $selscroll->addOption(1, _YES);
74
    $selscroll->addOption(2, _NO);
75
    $selscroll->addOption(3, _AM_MYIFRAME_AUTO);
76
    $selscroll->setValue($scrolling);
77
    $sform->addElement($selscroll);
78
79
    $sform->addElement(new XoopsFormText(_AM_MYIFRAME_URL, 'url', 50, 255, $url), true);
80
    $sform->addElement(new XoopsFormHidden('op', $Action), false);
81
    if (!empty($frameid)) {
82
        $sform->addElement(new XoopsFormHidden('frameid', $frameid), false);
83
    }
84
85
    $button_tray = new XoopsFormElementTray('', '');
86
    $submit_btn  = new XoopsFormButton('', 'submit', $LabelSubmitButton, 'submit');
87
    $button_tray->addElement($submit_btn);
88
    $cancel_btn = new XoopsFormButton('', 'reset', _AM_MYIFRAME_RESETBUTTON, 'reset');
89
    $button_tray->addElement($cancel_btn);
90
    $sform->addElement($button_tray);
91
    $sform->display();
92
    include_once __DIR__ . '/admin_footer.php';
93
}
94
95
// ******************************************************************************************************************************************
96
// **** Main ********************************************************************************************************************************
97
// ******************************************************************************************************************************************
98
99
if (isset($_POST['op'])) {
100
    $op = $_POST['op'];
101
} elseif (isset($_GET['op'])) {
102
    $op = $_GET['op'];
103
}
104
105
switch ($op) {
106
    case 'verifybeforeedit':
107
        if (isset($_POST['submit']) && $_POST['submit'] !== '') {
108 View Code Duplication
            if ($_POST['longdesc'] === '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
                xoops_cp_header();
110
                $adminObject = \Xmf\Module\Admin::getInstance();
111
                $adminObject->displayNavigation(basename(__FILE__));
112
                echo "<table width='100%' border='0' cellspacing='1' class='outer'>\n";
113
                echo '<tr><td class="odd">';
114
                echo "<a href='manage.php'><h4>" . _AM_MYIFRAME_CONFIG . '</h4></a>';
115
                echo _AM_MYIFRAME_ERROR_ADD_INDEX;
116
                echo '</td></tr></table>';
117
                include_once __DIR__ . '/admin_footer.php';
118
                xoops_cp_footer();
119
                exit();
120
            }
121
122
            $frameid = $_POST['frameid'];
123
            $frame   = $iframeHandler->get($frameid);
124
            $frame->unsetNew();
125
            $frame->setVar('frame_description', $_POST['longdesc']);
126
            $frame->setVar('frame_width', $_POST['width']);
127
            $frame->setVar('frame_height', $_POST['height']);
128
            $frame->setVar('frame_align', $_POST['align']);
129
            $frame->setVar('frame_frameborder', $_POST['frameborder']);
130
            $frame->setVar('frame_marginwidth', $_POST['marginwidth']);
131
            $frame->setVar('frame_marginheight', $_POST['marginheight']);
132
            $frame->setVar('frame_scrolling', $_POST['scrolling']);
133
            $frame->setVar('frame_url', $_POST['url']);
134
            $frame->setVar('frame_uid', $xoopsUser->getVar('uid'));
135
            $res = $iframeHandler->insert($frame);
136
            if (!$res) {
137
                redirect_header('manage.php', 1, _AM_MYIFRAME_ERROR_MODIFY_DB);
138
                exit();
139
            }
140
            redirect_header('manage.php', 1, _AM_MYIFRAME_DBUPDATED);
141
            exit();
142
        }
143
        break;
144
145
    case 'edit':
146
        xoops_cp_header();
147
        $adminObject = \Xmf\Module\Admin::getInstance();
148
        $adminObject->displayNavigation(basename(__FILE__));
149
        if (isset($_GET['frameid'])) {
150
            $frameid = (int)$_GET['frameid'];
151
            $frame   = $iframeHandler->get($frameid);
152
            AddEditForm($frameid, 'verifybeforeedit', _AM_MYIFRAME_CONFIG, $frame->getVar('frame_description', 'e'), $frame->getVar('frame_width', 'e'), $frame->getVar('frame_height', 'e'), $frame->getVar('frame_align', 'e'),
153
                        $frame->getVar('frame_frameborder', 'e'), $frame->getVar('frame_marginwidth', 'e'), $frame->getVar('frame_marginheight', 'e'), $frame->getVar('frame_scrolling', 'e'), $frame->getVar('frame_url', 'e'), _AM_MYIFRAME_UPDATE);
154 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            xoops_cp_header();
156
            $adminObject = \Xmf\Module\Admin::getInstance();
157
            $adminObject->displayNavigation(basename(__FILE__));
158
            echo "<table width='100%' border='0' cellspacing='1' class='outer'>\n";
159
            echo '<tr><td class="odd">';
160
            echo "<a href='manage.php'><h4>" . _AM_MYIFRAME_CONFIG . '</h4></a>';
161
            echo _AM_MYIFRAME_ERROR_ADD_INDEX;
162
            echo "</td></tr></table>\n";
163
            include_once __DIR__ . '/admin_footer.php';
164
            xoops_cp_footer();
165
            exit();
166
        }
167
        break;
168
169
    case 'delete':
170
        if (!isset($_POST['ok'])) {
171
            xoops_cp_header();
172
            $adminObject = \Xmf\Module\Admin::getInstance();
173
            $adminObject->displayNavigation(basename(__FILE__));
174
            echo '<h4>' . _AM_MYIFRAME_CONFIG . '</h4>';
175
            xoops_confirm(array(
176
                              'op'      => 'delete',
177
                              'frameid' => (int)$_GET['frameid'],
178
                              'ok'      => 1
179
                          ), 'manage.php', _AM_MYIFRAME_RUSUREDEL);
180
            include_once __DIR__ . '/admin_footer.php';
181
        } else {
182
            if (empty($_POST['frameid'])) {
183
                redirect_header('manage.php', 2, _AM_MYIFRAME_ERROR_ADD_INDEX);
184
                exit();
185
            }
186
            $frameid = (int)$_POST['frameid'];
187
            $critere = new Criteria('frame_frameid', $frameid, '=');
188
            $iframeHandler->deleteAll($critere);
189
            redirect_header('manage.php', 1, _AM_MYIFRAME_DBUPDATED);
190
            exit();
191
        }
192
        break;
193
194
    case 'verifytoadd':
195
        if (isset($_POST['submit']) && $_POST['submit'] !== '') {
196
            if ($_POST['url'] === '') {
197
                xoops_cp_header();
198
                $adminObject = \Xmf\Module\Admin::getInstance();
199
                $adminObject->displayNavigation(basename(__FILE__));
200
                echo "<table width='100%' border='0' cellspacing='1' class='outer'>\n";
201
                echo '<tr><td class="odd">';
202
                echo "<a href='manage.php'><h4>" . _AM_MYIFRAME_CONFIG . '</h4></a>';
203
                echo _AM_MYIFRAME_ERROR_ADD_INDEX;
204
                echo "</td></tr></table>\n";
205
                include_once __DIR__ . '/admin_footer.php';
206
                xoops_cp_footer();
207
                $adminObject = \Xmf\Module\Admin::getInstance();
208
                $adminObject->displayNavigation(basename(__FILE__));
209
                exit();
210
            }
211
            $frame = $iframeHandler->create(true);
212
            $frame->setVar('frame_description', $_POST['longdesc']);
213
            $frame->setVar('frame_width', $_POST['width']);
214
            $frame->setVar('frame_height', $_POST['height']);
215
            $frame->setVar('frame_align', $_POST['align']);
216
            $frame->setVar('frame_frameborder', $_POST['frameborder']);
217
            $frame->setVar('frame_marginwidth', $_POST['marginwidth']);
218
            $frame->setVar('frame_marginheight', $_POST['marginheight']);
219
            $frame->setVar('frame_scrolling', $_POST['scrolling']);
220
            $frame->setVar('frame_url', $_POST['url']);
221
            $frame->setVar('frame_created', time());
222
            $frame->setVar('frame_uid', $xoopsUser->getVar('uid'));
223
            $res = $iframeHandler->insert($frame);
224
            if (!$res) {
225
                redirect_header('manage.php', 1, _AM_MYIFRAME_ERROR_ADD_INDEX);
226
                exit();
227
            }
228
            redirect_header('manage.php', 1, _AM_MYIFRAME_ADDED_OK);
229
        }
230
        break;
231
232
    case 'addframe':
233
        xoops_cp_header();
234
        $adminObject = \Xmf\Module\Admin::getInstance();
235
        $adminObject->displayNavigation(basename(__FILE__));
236
        AddEditForm(0, 'verifytoadd', _AM_MYIFRAME_CONFIG, '', '100%', '', '', '0', '0', '0', 1, '', _AM_MYIFRAME_ADDBUTTON);
237
        break;
238
239
    case 'default':
240
    default:
241
        xoops_cp_header();
242
        $adminObject = \Xmf\Module\Admin::getInstance();
243
        $adminObject->displayNavigation(basename(__FILE__));
244
        echo '<h4>' . _AM_MYIFRAME_CONFIG . "</h4><br />\n";
245
        echo "<table width='100%' border='0' cellspacing='1' class='outer'>\n";
246
        echo "<tr><th align='center'>" . _AM_MYIFRAME_ID . "</th><th align='center'>" . _AM_MYIFRAME_DESC . "</th><th align='center'>" . _AM_MYIFRAME_CREATED . "</th><th align='center'>" . _AM_MYIFRAME_HITS . "</th><th align='center'>"
247
             . _AM_MYIFRAME_ACTION . "</th></tr>\n";
248
        $critere = new Criteria('1', '1', '=');
249
        $critere->setSort('frame_description');
250
        $frarray = $iframeHandler->getObjects($critere);
251
        $class   = 'even';
252
        $baseurl = XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/admin/manage.php';
253
        if (count($frarray) > 0) {
254
            foreach ($frarray as $frame) {
255
                $action_edit   = "<a href='" . $baseurl . '?op=edit&frameid=' . $frame->getVar('frame_frameid') . "'><img src='../assets/images/edit.png' alt='" . _AM_MYIFRAME_EDIT . "'></a>";
256
                $action_delete = "<a href='" . $baseurl . '?op=delete&frameid=' . $frame->getVar('frame_frameid') . "'><img src='../assets/images/delete.png' alt='" . _AM_MYIFRAME_DELETE . "'></a>";
257 View Code Duplication
                if (xoops_trim($frame->getVar('frame_description') === '')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
                    $liendesc = $frame->getVar('frame_url');
259
                } else {
260
                    $liendesc = "<a href='" . XOOPS_URL . '/modules/myiframe/index.php?iframeid=' . $frame->getVar('frame_frameid') . "'>" . $frame->getVar('frame_description') . '</a>';
261
                }
262
                echo "<tr class='" . $class . "'><td align='center'>" . $frame->getVar('frame_frameid') . "</td><td align='center'>" . $liendesc . "</td><td align='center'>" . formatTimestamp($frame->getVar('frame_created'))
263
                     . "</td><td align='center'>" . $frame->getVar('frame_hits') . "</td><td align='center'>" . $action_edit . '&nbsp;-&nbsp;' . $action_delete . "</td></tr>\n";
264
                $class = ($class === 'even') ? 'odd' : 'even';
265
            }
266
        }
267
        echo "<tr class='" . $class . "'><td colspan='5' align='center'><form name='faddframe' method='post' action='manage.php'><input type='hidden' name='op' value='addframe'><input type='submit' name='submit' value='" . _AM_MYIFRAME_ADD
268
             . "'></td></tr>";
269
        echo '</table>';
270
        include_once __DIR__ . '/admin_footer.php';
271
        break;
272
}
273
274
xoops_cp_footer();
275