editStatus()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 55
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 38
c 0
b 0
f 0
nc 12
nop 0
dl 0
loc 55
rs 8.6897

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
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
/**
14
 * @copyright    {@link https://xoops.org/ XOOPS Project}
15
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
16
 * @author       Brian Wahoff <[email protected]>
17
 * @author       Eric Juden <[email protected]>
18
 * @author       XOOPS Development Team
19
 */
20
21
use Xmf\Module\Admin;
22
use Xmf\Request;
23
use XoopsModules\Xhelp;
24
25
require_once __DIR__ . '/admin_header.php';
26
xoops_load('XoopsPagenav');
27
// require_once XHELP_CLASS_PATH . '/Form.php';
28
29
global $xoopsModule;
30
$module_id = $xoopsModule->getVar('mid');
31
$helper    = Xhelp\Helper::getInstance();
32
33
$limit = Request::getInt('limit', 0, 'REQUEST');
34
$start = Request::getInt('start', 0, 'REQUEST');
35
36
if (!$limit) {
37
    $limit = 15;
38
}
39
if (Request::hasVar('order', 'REQUEST')) {
40
    $order = $_REQUEST['order'];
41
} else {
42
    $order = 'ASC';
43
}
44
if (Request::hasVar('sort', 'REQUEST')) {
45
    $sort = $_REQUEST['sort'];
46
} else {
47
    $sort = 'id';
48
}
49
50
$aSortBy  = [
51
    'id'          => _AM_XHELP_TEXT_ID,
52
    'description' => _AM_XHELP_TEXT_DESCRIPTION,
53
    'state'       => _AM_XHELP_TEXT_STATE,
54
];
55
$aOrderBy = ['ASC' => _AM_XHELP_TEXT_ASCENDING, 'DESC' => _AM_XHELP_TEXT_DESCENDING];
56
$aLimitBy = ['10' => 10, '15' => 15, '20' => 20, '25' => 25, '50' => 50, '100' => 100];
57
58
$op = 'default';
59
60
if (Request::hasVar('op', 'REQUEST')) {
61
    $op = $_REQUEST['op'];
62
}
63
64
switch ($op) {
65
    case 'deleteStatus':
66
        deleteStatus();
67
        break;
68
    case 'editStatus':
69
        editStatus();
70
        break;
71
    case 'manageStatus':
72
        manageStatus();
73
        break;
74
    default:
75
        $helper->redirect('admin/index.php');
76
        break;
77
}
78
79
function deleteStatus()
80
{
81
    $helper = Xhelp\Helper::getInstance();
82
    if (Request::hasVar('statusid', 'GET')) {
83
        $statusid = Request::getInt('statusid', 0, 'GET');
84
    } else {
85
        $helper->redirect('admin/status.php?op=manageStatus');
86
    }
87
88
    /** @var \XoopsModules\Xhelp\TicketHandler $ticketHandler */
89
    $ticketHandler = $helper->getHandler('Ticket');
90
    /** @var \XoopsModules\Xhelp\StatusHandler $statusHandler */
91
    $statusHandler = $helper->getHandler('Status');
92
    $status        = $statusHandler->get($statusid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $statusid does not seem to be defined for all execution paths leading up to this point.
Loading history...
93
94
    // Check for tickets with this status first
95
    $criteria    = new \Criteria('status', (string)$statusid);
96
    $ticketCount = $ticketHandler->getCount($criteria);
97
98
    if ($ticketCount > 0) {
99
        $helper->redirect('admin/status.php?op=manageStatus', 3, _AM_XHELP_STATUS_HASTICKETS_ERR);
100
    }
101
102
    if ($statusHandler->delete($status, true)) {
103
        $helper->redirect('admin/status.php?op=manageStatus');
104
    } else {
105
        $message = _AM_XHELP_DEL_STATUS_ERR;
106
        $helper->redirect('admin/status.php?op=manageStatus', 3, $message);
107
    }
108
}
109
110
function editStatus()
111
{
112
    $helper = Xhelp\Helper::getInstance();
113
    if (Request::hasVar('statusid', 'REQUEST')) {
114
        $statusid = Request::getInt('statusid', 0, 'REQUEST');
115
    } else {
116
        $helper->redirect('admin/status.php?op=manageStatus');
117
    }
118
119
    /** @var \XoopsModules\Xhelp\StatusHandler $statusHandler */
120
    $statusHandler = $helper->getHandler('Status');
121
    $status        = $statusHandler->get($statusid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $statusid does not seem to be defined for all execution paths leading up to this point.
Loading history...
122
123
    if (isset($_POST['updateStatus'])) {
124
        if ('' === $_POST['desc']) {  // If no description supplied
125
            $message = _AM_XHELP_MESSAGE_NO_DESC;
126
            $helper->redirect('admin/status.php?op=manageStatus', 3, $message);
127
        }
128
129
        $status->setVar('description', $_POST['desc']);
130
        $status->setVar('state', $_POST['state']);
131
        if ($statusHandler->insert($status)) {
132
            $helper->redirect('admin/status.php?op=manageStatus');
133
        } else {
134
            $message = _AM_MESSAGE_EDIT_STATUS_ERR;
135
            $helper->redirect('admin/status.php?op=manageStatus', 3, $message);
136
        }
137
    } else {
138
        xoops_cp_header();
139
        //echo $oAdminButton->renderButtons('modTpl');
140
        $adminObject = Admin::getInstance();
141
        $adminObject->displayNavigation('status.php?op=editStatus');
142
143
        echo "<form method='post' action='" . XHELP_ADMIN_URL . '/status.php?op=editStatus&amp;statusid=' . $statusid . "'>";
0 ignored issues
show
Bug introduced by
The constant XHELP_ADMIN_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
144
        echo "<table width='100%' cellspacing='1' class='outer'>
145
              <tr><th colspan='2'><label>" . _AM_XHELP_TEXT_EDIT_STATUS . '</label></th></tr>';
146
        echo "<tr><td class='head' width='20%'>" . _AM_XHELP_TEXT_DESCRIPTION . "</td>
147
                  <td class='even'>
148
                      <input type='text' name='desc' value='" . $status->getVar('description', 'e') . "' class='formButton'>
149
                  </td>
150
              </tr>";
151
        echo "<tr><td class='head' width='20%'>" . _AM_XHELP_TEXT_STATE . "</td><td class='even'>
152
                  <select name='state'>";
153
        if (1 == $status->getVar('state')) {
154
            echo "<option value='1' selected>" . Xhelp\Utility::getState(1) . "</option>
155
                          <option value='2'>" . Xhelp\Utility::getState(2) . '</option>';
156
        } else {
157
            echo "<option value='1'>" . Xhelp\Utility::getState(1) . "</option>
158
                          <option value='2' selected>" . Xhelp\Utility::getState(2) . '</option>';
159
        }
160
        echo '</select></td></tr>';
161
        echo "<tr><td class='foot' colspan='2'><input type='submit' name='updateStatus' value='" . _AM_XHELP_BUTTON_UPDATE . "' class='formButton'></td></tr>";
162
        echo '</table></form>';
163
164
        require_once __DIR__ . '/admin_footer.php';
165
    }
166
}
167
168
function manageStatus()
169
{
170
    global $aSortBy, $aOrderBy, $aLimitBy, $order, $limit, $start, $sort;
171
    $helper = Xhelp\Helper::getInstance();
172
    /** @var \XoopsModules\Xhelp\StatusHandler $statusHandler */
173
    $statusHandler = $helper->getHandler('Status');
174
    $helper        = Xhelp\Helper::getInstance();
175
176
    if (Request::hasVar('changeDefaultStatus', 'POST')) {
177
        Xhelp\Utility::setMeta('default_status', $_POST['default']);
178
    }
179
180
    if (Request::hasVar('newStatus', 'POST')) {
181
        if ('' === \Xmf\Request::getString('desc', '', 'POST')) {  // If no description supplied
182
            $message = _AM_XHELP_MESSAGE_NO_DESC;
183
            $helper->redirect('admin/status.php?op=manageStatus', 3, $message);
184
        }
185
        /** @var \XoopsModules\Xhelp\Status $newStatus */
186
        $newStatus = $statusHandler->create();
187
188
        $newStatus->setVar('state', Request::getInt('state', 0, 'POST'));
189
        $newStatus->setVar('description', \Xmf\Request::getString('desc', '', 'POST'));
190
        if ($statusHandler->insert($newStatus)) {
191
            $helper->redirect('admin/status.php?op=manageStatus');
192
        } else {
193
            $message = _AM_MESSAGE_ADD_STATUS_ERR;
194
            $helper->redirect('admin/status.php?op=manageStatus', 3, $message);
195
        }
196
    }
197
    xoops_cp_header();
198
    //echo $oAdminButton->renderButtons('manStatus');
199
    $adminObject = Admin::getInstance();
200
    $adminObject->displayNavigation('status.php?op=manageStatus');
201
202
    echo "<form method='post' action='" . XHELP_ADMIN_URL . "/status.php?op=manageStatus'>";
0 ignored issues
show
Bug introduced by
The constant XHELP_ADMIN_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
203
    echo "<table width='100%' cellspacing='1' class='outer'>
204
          <tr><th colspan='2'><label>" . _AM_XHELP_TEXT_ADD_STATUS . '</label></th></tr>';
205
    echo "<tr><td class='head' width='20%'>" . _AM_XHELP_TEXT_DESCRIPTION . "</td>
206
              <td class='even'>
207
                  <input type='text' name='desc' value='' class='formButton'>
208
              </td>
209
          </tr>";
210
    echo "<tr><td class='head' width='20%'>" . _AM_XHELP_TEXT_STATE . "</td><td class='even'>
211
              <select name='state'>
212
              <option value='1'>" . Xhelp\Utility::getState(1) . "</option>
213
              <option value='2'>" . Xhelp\Utility::getState(2) . '</option>
214
          </select></td></tr>';
215
    echo "<tr><td class='foot' colspan='2'><input type='submit' name='newStatus' value='" . _AM_XHELP_TEXT_ADD_STATUS . "' class='formButton'></td></tr>";
216
    echo '</table></form>';
217
218
    // Get list of existing statuses
219
    $criteria = new \Criteria('', '');
220
    $criteria->setOrder($order);
221
    $criteria->setSort($sort);
222
    $criteria->setLimit($limit);
223
    $criteria->setStart($start);
224
    $statuses = $statusHandler->getObjects($criteria);
225
    $total    = $statusHandler->getCount($criteria);
226
227
    $aStatuses = [];
228
    foreach ($statuses as $status) {
229
        $aStatuses[$status->getVar('id')] = $status->getVar('description');
230
    }
231
232
    if (!$default_status = Xhelp\Utility::getMeta('default_status')) {
233
        Xhelp\Utility::setMeta('default_status', '1');
234
        $default_status = 1;
235
    }
236
    $form          = new Xhelp\Form(_AM_XHELP_TEXT_DEFAULT_STATUS, 'default_status', Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/status.php', ['op' => 'manageStatus']));
237
    $status_select = new \XoopsFormSelect(_AM_XHELP_TEXT_STATUS, 'default', $default_status);
238
    $status_select->addOptionArray($aStatuses);
239
    $btn_tray = new \XoopsFormElementTray('');
240
    $btn_tray->addElement(new \XoopsFormButton('', 'changeDefaultStatus', _SUBMIT, 'submit'));
241
    $form->addElement($status_select);
242
    $form->addElement($btn_tray);
243
    $form->setLabelWidth('20%');
244
    echo $form->render();
245
246
    $nav = new \XoopsPageNav($total, $limit, $start, 'start', "op=manageStatus&amp;limit=$limit");
247
248
    echo "<form action='" . XHELP_ADMIN_URL . "/status.php?op=manageStatus' style='margin:0; padding:0;' method='post'>";
249
    echo $GLOBALS['xoopsSecurity']->getTokenHTML();
250
    echo "<table width='100%' cellspacing='1' class='outer'>";
251
    echo "<tr><td align='right'>" . _AM_XHELP_TEXT_SORT_BY . "
252
                  <select name='sort'>";
253
    foreach ($aSortBy as $value => $text) {
254
        ($sort == $value) ? $selected = 'selected' : $selected = '';
255
        echo "<option value='$value' $selected>$text</option>";
256
    }
257
    echo '</select>
258
                &nbsp;&nbsp;&nbsp;
259
                  ' . _AM_XHELP_TEXT_ORDER_BY . "
260
                  <select name='order'>";
261
    foreach ($aOrderBy as $value => $text) {
262
        ($order == $value) ? $selected = 'selected' : $selected = '';
263
        echo "<option value='$value' $selected>$text</option>";
264
    }
265
    echo '</select>
266
                  &nbsp;&nbsp;&nbsp;
267
                  ' . _AM_XHELP_TEXT_NUMBER_PER_PAGE . "
268
                  <select name='limit'>";
269
    foreach ($aLimitBy as $value => $text) {
270
        ($limit == $value) ? $selected = 'selected' : $selected = '';
271
        echo "<option value='$value' $selected>$text</option>";
272
    }
273
    echo "</select>
274
                  <input type='submit' name='status_sort' id='status_sort' value='" . _AM_XHELP_BUTTON_SUBMIT . "'>
275
              </td>
276
          </tr>";
277
    echo '</table></form>';
278
279
    echo "<table width='100%' cellspacing='1' class='outer'>
280
          <tr><th colspan='4'><label>" . _AM_XHELP_TEXT_MANAGE_STATUSES . '</label></th></tr>';
281
    echo "<tr class='head'>
282
              <td>" . _AM_XHELP_TEXT_ID . '</td>
283
              <td>' . _AM_XHELP_TEXT_DESCRIPTION . '</td>
284
              <td>' . _AM_XHELP_TEXT_STATE . '</td>
285
              <td>' . _AM_XHELP_TEXT_ACTIONS . '</td>
286
          </tr>';
287
    foreach ($statuses as $status) {
288
        echo "<tr class='even'><td>" . $status->getVar('id') . '</td><td>' . $status->getVar('description') . '</td>
289
              <td>' . Xhelp\Utility::getState($status->getVar('state')) . "</td>
290
              <td>
291
                  <a href='status.php?op=editStatus&amp;statusid=" . $status->getVar('id') . "'><img src='" . XHELP_IMAGE_URL . "/button_edit.png' title='" . _AM_XHELP_TEXT_EDIT . "' name='editStatus'></a>&nbsp;
292
                  <a href='status.php?op=deleteStatus&amp;statusid=" . $status->getVar('id') . "'><img src='" . XHELP_IMAGE_URL . "/button_delete.png' title='" . _AM_XHELP_TEXT_DELETE . "' name='deleteStatus'></a></td></tr>
293
              </td></tr>";
294
    }
295
    echo '</table>';
296
    echo "<div id='status_nav'>" . $nav->renderNav() . '</div>';
297
    require_once __DIR__ . '/admin_footer.php';
298
}
299