Completed
Push — master ( 26776f...d9604e )
by Michael
11:31
created

status.php ➔ manageStatus()   F

Complexity

Conditions 14
Paths 2160

Size

Total Lines 127
Code Lines 86

Duplication

Lines 16
Ratio 12.6 %

Importance

Changes 0
Metric Value
cc 14
eloc 86
nc 2160
nop 0
dl 16
loc 127
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
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 62 and the first side effect is on line 3.

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
//$Id: status.php,v 1.7 2005/11/29 17:48:12 ackbarr Exp $
3
include('../../../include/cp_header.php');
4
include_once('admin_header.php');
5
include_once(XOOPS_ROOT_PATH . '/class/pagenav.php');
6
require_once(XHELP_CLASS_PATH . '/xhelpForm.php');
7
8
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...
9
$module_id = $xoopsModule->getVar('mid');
10
11
$start = $limit = 0;
12
if (isset($_REQUEST['limit'])) {
13
    $limit = intval($_REQUEST['limit']);
14
}
15
if (isset($_REQUEST['start'])) {
16
    $start = intval($_REQUEST['start']);
17
}
18
if (!$limit) {
19
    $limit = 15;
20
}
21
if(isset($_REQUEST['order'])){
22
    $order = $_REQUEST['order'];
23
} else {
24
    $order = "ASC";
25
}
26
if(isset($_REQUEST['sort'])) {
27
    $sort = $_REQUEST['sort'];
28
} else {
29
    $sort = "id";
30
}
31
32
$aSortBy = array('id' => _AM_XHELP_TEXT_ID, 'description' => _AM_XHELP_TEXT_DESCRIPTION, 'state' => _AM_XHELP_TEXT_STATE);
33
$aOrderBy = array('ASC' => _AM_XHELP_TEXT_ASCENDING, 'DESC' => _AM_XHELP_TEXT_DESCENDING);
34
$aLimitBy = array('10' => 10, '15' => 15, '20' => 20, '25' => 25, '50' => 50, '100' => 100);
35
36
$op = 'default';
37
38
if ( isset( $_REQUEST['op'] ) )
39
{
40
    $op = $_REQUEST['op'];
41
}
42
43
switch ( $op )
44
{
45
    case "deleteStatus":
46
        deleteStatus();
47
        break;
48
49
    case "editStatus":
50
        editStatus();
51
        break;
52
53
    case "manageStatus":
54
        manageStatus();
55
        break;
56
57
    default:
58
        header("Location: ".XHELP_ADMIN_URL."/index.php");
59
        break;
60
}
61
62
function deleteStatus()
0 ignored issues
show
Coding Style introduced by
deleteStatus uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
{
64
    if(isset($_GET['statusid'])){
65
        $statusid = intval($_GET['statusid']);
66
    } else {
67
        header("Location: ".XHELP_ADMIN_URL."/status.php?op=manageStatus");
68
    }
69
70
    $hTickets =& xhelpGetHandler('ticket');
71
    $hStatus =& xhelpGetHandler('status');
72
    $status =& $hStatus->get($statusid);
0 ignored issues
show
Bug introduced by
The variable $statusid 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...
73
74
    // Check for tickets with this status first
75
    $crit = new Criteria('status', $statusid);
76
    $ticketCount =& $hTickets->getCount($crit);
77
78
    if($ticketCount > 0){
79
        redirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, _AM_XHELP_STATUS_HASTICKETS_ERR);
80
    }
81
82
    if($hStatus->delete($status, true)){
83
        header("Location: ".XHELP_ADMIN_URL."/status.php?op=manageStatus");
84
    } else {
85
        $message = _AM_XHELP_DEL_STATUS_ERR;
86
        redirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, $message);
87
    }
88
}
89
90
function editStatus()
0 ignored issues
show
Coding Style introduced by
editStatus uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
editStatus uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
91
{
92
    if(isset($_REQUEST['statusid'])){
93
        $statusid = intval($_REQUEST['statusid']);
94
    } else {
95
        header("Location: ".XHELP_ADMIN_URL."/status.php?op=manageStatus");
96
    }
97
98
    $hStatus =& xhelpGetHandler('status');
99
    $status = $hStatus->get($statusid);
0 ignored issues
show
Bug introduced by
The variable $statusid 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...
100
101
    if(!isset($_POST['updateStatus'])){
102
        xoops_cp_header();
103
        //echo $oAdminButton->renderButtons('modTpl');
104
        $indexAdmin = new ModuleAdmin();
105
        echo $indexAdmin->addNavigation('status.php?op=editStatus');
106
107
        echo "<form method='post' action='".XHELP_ADMIN_URL."/status.php?op=editStatus&amp;statusid=".$statusid."'>";
108
        echo "<table width='100%' cellspacing='1' class='outer'>
109
              <tr><th colspan='2'><label>"._AM_XHELP_TEXT_EDIT_STATUS."</label></th></tr>";
110
        echo "<tr><td class='head' width='20%'>"._AM_XHELP_TEXT_DESCRIPTION."</td>
111
                  <td class='even'>
112
                      <input type='text' name='desc' value='".$status->getVar('description', 'e')."' class='formButton' />
113
                  </td>
114
              </tr>";
115
        echo "<tr><td class='head' width='20%'>"._AM_XHELP_TEXT_STATE."</td><td class='even'>
116
                  <select name='state'>";
117
        if($status->getVar('state') == 1){
118
            echo "<option value='1' selected='selected'>".xhelpGetState(1)."</option>
119
                          <option value='2'>".xhelpGetState(2)."</option>";
120
        } else {
121
            echo "<option value='1'>".xhelpGetState(1)."</option>
122
                          <option value='2' selected='selected'>".xhelpGetState(2)."</option>";
123
        }
124
        echo "</select></td></tr>";
125
        echo "<tr><td class='foot' colspan='2'><input type='submit' name='updateStatus' value='"._AM_XHELP_BUTTON_UPDATE."' class='formButton' /></td></tr>";
126
        echo "</table></form>";
127
128
        include_once "admin_footer.php";
129
    } else {
130 View Code Duplication
        if($_POST['desc'] == ''){  // If no description supplied
131
            $message = _AM_XHELP_MESSAGE_NO_DESC;
132
            redirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, $message);
133
        }
134
135
        $status->setVar('description', $_POST['desc']);
136
        $status->setVar('state', $_POST['state']);
137
        if($hStatus->insert($status)){
138
            header("Location: ".XHELP_ADMIN_URL."/status.php?op=manageStatus");
139
        } else {
140
            $message = _AM_MESSAGE_EDIT_STATUS_ERR;
141
            readirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, $message);
142
        }
143
    }
144
}
145
146
function manageStatus()
0 ignored issues
show
Coding Style introduced by
manageStatus uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
147
{
148
    global $aSortBy, $aOrderBy, $aLimitBy, $order, $limit, $start, $sort;
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...
149
    $hStatus =& xhelpGetHandler('status');
150
151
    if(isset($_POST['changeDefaultStatus'])){
152
        xhelpSetMeta("default_status", $_POST['default']);
153
    }
154
155
    if(isset($_POST['newStatus'])){
156 View Code Duplication
        if($_POST['desc'] == ''){  // If no description supplied
157
            $message = _AM_XHELP_MESSAGE_NO_DESC;
158
            redirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, $message);
159
        }
160
        $newStatus =& $hStatus->create();
161
162
        $newStatus->setVar('state', intval($_POST['state']));
163
        $newStatus->setVar('description', $_POST['desc']);
164
        if($hStatus->insert($newStatus)){
165
            header("Location: ".XHELP_ADMIN_URL."/status.php?op=manageStatus");
166
        } else {
167
            $message = _AM_MESSAGE_ADD_STATUS_ERR;
168
            redirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, $message);
169
        }
170
    }
171
    xoops_cp_header();
172
    //echo $oAdminButton->renderButtons('manStatus');
173
    $indexAdmin = new ModuleAdmin();
174
    echo $indexAdmin->addNavigation('status.php?op=manageStatus');
175
176
    echo "<form method='post' action='".XHELP_ADMIN_URL."/status.php?op=manageStatus'>";
177
    echo "<table width='100%' cellspacing='1' class='outer'>
178
          <tr><th colspan='2'><label>"._AM_XHELP_TEXT_ADD_STATUS."</label></th></tr>";
179
    echo "<tr><td class='head' width='20%'>"._AM_XHELP_TEXT_DESCRIPTION."</td>
180
              <td class='even'>
181
                  <input type='text' name='desc' value='' class='formButton' />
182
              </td>
183
          </tr>";
184
    echo "<tr><td class='head' width='20%'>"._AM_XHELP_TEXT_STATE."</td><td class='even'>
185
              <select name='state'>
186
              <option value='1'>".xhelpGetState(1)."</option>
187
              <option value='2'>".xhelpGetState(2)."</option>
188
          </select></td></tr>";
189
    echo "<tr><td class='foot' colspan='2'><input type='submit' name='newStatus' value='"._AM_XHELP_TEXT_ADD_STATUS."' class='formButton' /></td></tr>";
190
    echo "</table></form>";
191
192
    // Get list of existing statuses
193
    $crit = new Criteria('', '');
194
    $crit->setOrder($order);
195
    $crit->setSort($sort);
196
    $crit->setLimit($limit);
197
    $crit->setStart($start);
198
    $statuses =& $hStatus->getObjects($crit);
199
    $total = $hStatus->getCount($crit);
200
201
    $aStatuses = array();
202
    foreach($statuses as $status){
203
        $aStatuses[$status->getVar('id')] = $status->getVar('description');
204
    }
205
206
    if(!$default_status = xhelpGetMeta("default_status")){
207
        xhelpSetMeta("default_status", "1");
208
        $default_status = 1;
209
    }
210
    $form = new xhelpForm(_AM_XHELP_TEXT_DEFAULT_STATUS, 'default_status', xhelpMakeURI(XHELP_ADMIN_URL.'/status.php', array('op'=>'manageStatus')));
211
    $status_select = new XoopsFormSelect(_AM_XHELP_TEXT_STATUS, 'default', $default_status);
212
    $status_select->addOptionArray($aStatuses);
213
    $btn_tray = new XoopsFormElementTray('');
214
    $btn_tray->addElement(new XoopsFormButton('', 'changeDefaultStatus', _AM_XHELP_BUTTON_SUBMIT, 'submit'));
215
    $form->addElement($status_select);
216
    $form->addElement($btn_tray);
217
    $form->setLabelWidth('20%');
218
    echo $form->render();
219
220
    $nav = new XoopsPageNav($total, $limit, $start, 'start', "op=manageStatus&amp;limit=$limit");
221
222
    echo "<form action='". XHELP_ADMIN_URL."/status.php?op=manageStatus' style='margin:0; padding:0;' method='post'>";
223
    echo "<table width='100%' cellspacing='1' class='outer'>";
224
    echo "<tr><td align='right'>"._AM_XHELP_TEXT_SORT_BY."
225
                  <select name='sort'>";
226 View Code Duplication
    foreach($aSortBy as $value=>$text){
227
        ($sort == $value) ? $selected = "selected='selected'" : $selected = '';
228
        echo "<option value='$value' $selected>$text</option>";
229
    }
230
    echo "</select>
231
                &nbsp;&nbsp;&nbsp;
232
                  "._AM_XHELP_TEXT_ORDER_BY."
233
                  <select name='order'>";
234 View Code Duplication
    foreach($aOrderBy as $value=>$text){
235
        ($order == $value) ? $selected = "selected='selected'" : $selected = '';
236
        echo "<option value='$value' $selected>$text</option>";
237
    }
238
    echo "</select>
239
                  &nbsp;&nbsp;&nbsp;
240
                  "._AM_XHELP_TEXT_NUMBER_PER_PAGE."
241
                  <select name='limit'>";
242 View Code Duplication
    foreach($aLimitBy as $value=>$text){
243
        ($limit == $value) ? $selected = "selected='selected'" : $selected = '';
244
        echo "<option value='$value' $selected>$text</option>";
245
    }
246
    echo "</select>
247
                  <input type='submit' name='status_sort' id='status_sort' value='"._AM_XHELP_BUTTON_SUBMIT."' />
248
              </td>
249
          </tr>";
250
    echo "</table></form>";
251
252
    echo "<table width='100%' cellspacing='1' class='outer'>
253
          <tr><th colspan='4'><label>"._AM_XHELP_TEXT_MANAGE_STATUSES."</label></th></tr>";
254
    echo "<tr class='head'>
255
              <td>"._AM_XHELP_TEXT_ID."</td>
256
              <td>"._AM_XHELP_TEXT_DESCRIPTION."</td>
257
              <td>"._AM_XHELP_TEXT_STATE."</td>
258
              <td>"._AM_XHELP_TEXT_ACTIONS."</td>
259
          </tr>";
260
    foreach($statuses as $status){
261
        echo "<tr class='even'><td>".$status->getVar('id')."</td><td>".$status->getVar('description')."</td>
262
              <td>".xhelpGetState($status->getVar('state'))."</td>
263
              <td>
264
                  <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;
265
                  <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>
266
              </td></tr>";
267
    }
268
    echo "</table>";
269
    echo "<div id='status_nav'>".$nav->renderNav()."</div>";
270
    include_once "admin_footer.php";
271
272
}
273