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

file.php ➔ manageFiles()   F

Complexity

Conditions 13
Paths 648

Size

Total Lines 132
Code Lines 88

Duplication

Lines 12
Ratio 9.09 %

Importance

Changes 0
Metric Value
cc 13
eloc 88
nc 648
nop 0
dl 12
loc 132
rs 2.4575
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: file.php,v 1.10 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
7
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...
8
$module_id = $xoopsModule->getVar('mid');
9
10
$start = $limit = 0;
11
if (isset($_REQUEST['limit'])) {
12
    $limit = intval($_REQUEST['limit']);
13
}
14
if (isset($_REQUEST['start'])) {
15
    $start = intval($_REQUEST['start']);
16
}
17
if (!$limit) {
18
    $limit = 15;
19
}
20
if(isset($_REQUEST['order'])){
21
    $order = $_REQUEST['order'];
22
} else {
23
    $order = "ASC";
24
}
25
if(isset($_REQUEST['sort'])) {
26
    $sort = $_REQUEST['sort'];
27
} else {
28
    $sort = "id";
29
}
30
31
$aSortBy = array('id' => _AM_XHELP_TEXT_ID, 'ticketid' => _AM_XHELP_TEXT_TICKETID, 'filename' => _AM_XHELP_TEXT_FILENAME,
32
                 'mimetype' => _AM_XHELP_TEXT_MIMETYPE);
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 "deleteFile":
46
        deleteFile();
47
        break;
48
49
    case "deleteResolved":
50
        deleteResolved();
51
        break;
52
53
    case "manageFiles":
54
        manageFiles();
55
        break;
56
57
    default:
58
        header("Location: ".XHELP_ADMIN_URL."/index.php");
59
        break;
60
}
61
62
function deleteFile()
0 ignored issues
show
Coding Style introduced by
deleteFile 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...
Coding Style introduced by
deleteFile 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...
63
{
64
    $hFile =& xhelpGetHandler('file');
65
66
    if(!isset($_GET['fileid'])){
67
        redirect_header(XHELP_ADMIN_URL."/file.php?op=manageFiles", 3, _XHELP_MESSAGE_DELETE_FILE_ERR);
68
    }
69
    $fileid = intval($_GET['fileid']);
70
    if(!isset($_POST['ok'])){
71
        xoops_cp_header();
72
        xoops_confirm(array('op' => 'deleteFile', 'ok' => 1), XHELP_ADMIN_URL.'/file.php?fileid='. $fileid, _AM_XHELP_MSG_DELETE_FILE);
73
        xoops_cp_footer();
74
    } else {
75
76
        $file =& $hFile->get($fileid);
77
        if($hFile->delete($file, true)){
78
            header("Location: ".XHELP_ADMIN_URL."/file.php?op=manageFiles");
79
        }
80
        redirect_header(XHELP_ADMIN_URL."/file.php?op=manageFiles", 3, _XHELP_MESSAGE_DELETE_FILE_ERR);
81
    }
82
}
83
84
function deleteResolved()
0 ignored issues
show
Coding Style introduced by
deleteResolved 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...
85
{
86
87
    if(!isset($_POST['ok'])){
88
        xoops_cp_header();
89
        //echo $oAdminButton->renderButtons('manFiles');
90
        $indexAdmin = new ModuleAdmin();
91
        echo $indexAdmin->addNavigation('file.php');
92
93
        xoops_confirm(array('op' => 'deleteResolved', 'ok' => 1), XHELP_BASE_URL .'/admin/file.php', _AM_XHELP_MSG_DELETE_RESOLVED);
94
        xoops_cp_footer();
95
    } else {
96
        $hTicket =& xhelpGetHandler('ticket');
97
        $hFile =& xhelpGetHandler('file');
98
99
        $tickets =& $hTicket->getObjectsByState(1);     // Memory saver - unresolved should be less tickets
100
101
        $aTickets = array();
102
        foreach($tickets as $ticket){
103
            $aTickets[$ticket->getVar('id')] = $ticket->getVar('id');
104
        }
105
106
        // Retrieve all unresolved ticket attachments
107
        $crit = new CriteriaCompo();
108
        foreach($aTickets as $ticket){
109
            $crit->add(new Criteria('ticketid', $ticket, "!="));
110
        }
111 View Code Duplication
        if($hFile->deleteAll($crit)){
112
            header("Location: ".XHELP_ADMIN_URL."/file.php?op=manageFiles");
113
        } else {
114
            redirect_header(XHELP_ADMIN_URL."/file.php?op=manageFiles", 3, _XHELP_MESSAGE_DELETE_FILE_ERR);
115
        }
116
    }
117
}
118
119
function manageFiles()
0 ignored issues
show
Coding Style introduced by
manageFiles 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...
120
{
121
    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...
122
    $xhelpUploadDir = XHELP_UPLOAD_PATH;
123
    $dir_status =& xhelp_admin_getPathStatus($xhelpUploadDir, true);
124
125
    if($dir_status == -1){
126
        $can_upload = xhelp_admin_mkdir($xhelpUploadDir);
0 ignored issues
show
Unused Code introduced by
$can_upload 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...
127
    }
128
129
    $hFile =& xhelpGetHandler('file');
130
131
    if(isset($_POST['deleteFiles'])){   // Delete all selected files
132
        $aFiles = $_POST['files'];
133
        $crit = new Criteria('id', "(". implode($aFiles, ',') .")", "IN");
134
135
        if($hFile->deleteAll($crit)){
136
            header("Location: ".XHELP_ADMIN_URL."/file.php?op=manageFiles");
137
        }
138
        redirect_header(XHELP_ADMIN_URL."/file.php?op=manageFiles", 3, _XHELP_MESSAGE_DELETE_FILE_ERR);
139
    }
140
    xoops_cp_header();
141
    //echo $oAdminButton->renderButtons('manFiles');
142
    $indexAdmin = new ModuleAdmin();
143
    echo $indexAdmin->addNavigation('file.php?op=manageFiles');
144
145
    echo '<script type="text/javascript" src="'.XOOPS_URL.'/modules/xhelp/include/functions.js"></script>';
146
    echo "<form method='post' action='".XHELP_ADMIN_URL."/file.php?op=manageFiles'>";
147
148
    echo "<table width='100%' cellspacing='1' class='outer'>
149
          <tr><th colspan='2'><label>"._AM_XHELP_TEXT_TOTAL_USED_SPACE."</label></th></tr>";
150
151
    echo "<tr><td class='head' width='20%'>"._AM_XHELP_TEXT_ALL_ATTACH."</td>
152
              <td class='even'>".xhelpDirsize($xhelpUploadDir)."
153
              </td>
154
          </tr>";
155
156
    $resolvedSize = xhelpDirsize($xhelpUploadDir, true);
157
    echo "<tr><td class='head'>"._AM_XHELP_TEXT_RESOLVED_ATTACH."</td>
158
              <td class='even'>";
159
    if($resolvedSize > 0){
160
        echo $resolvedSize." <b>("._AM_XHELP_TEXT_DELETE_RESOLVED."
161
                  <a href='".XHELP_ADMIN_URL."/file.php?op=deleteResolved'><img src='".XHELP_IMAGE_URL."/button_delete.png' title='"._AM_XHELP_TEXT_DELETE."' name='deleteFile' /></a>)</b>";
162
    } else {
163
        echo $resolvedSize;
164
    }
165
    echo "</td>
166
          </tr>";
167
    echo "</table></form>";
168
169
    $crit = new Criteria('', '');
170
    $crit->setOrder($order);
171
    $crit->setSort($sort);
172
    $crit->setLimit($limit);
173
    $crit->setStart($start);
174
    $files =& $hFile->getObjects($crit);
175
    $total = $hFile->getCount($crit);
176
177
    $nav = new XoopsPageNav($total, $limit, $start, 'start', "op=manageFiles&amp;limit=$limit");
178
179
    echo "<form action='". XHELP_ADMIN_URL."/file.php?op=manageFiles' style='margin:0; padding:0;' method='post'>";
180
    echo "<table width='100%' cellspacing='1' class='outer'>";
181
    echo "<tr><td align='right'>"._AM_XHELP_TEXT_SORT_BY."
182
                  <select name='sort'>";
183 View Code Duplication
    foreach($aSortBy as $value=>$text){
184
        ($sort == $value) ? $selected = "selected='selected'" : $selected = '';
185
        echo "<option value='$value' $selected>$text</option>";
186
    }
187
    echo "</select>
188
                &nbsp;&nbsp;&nbsp;
189
                  "._AM_XHELP_TEXT_ORDER_BY."
190
                  <select name='order'>";
191 View Code Duplication
    foreach($aOrderBy as $value=>$text){
192
        ($order == $value) ? $selected = "selected='selected'" : $selected = '';
193
        echo "<option value='$value' $selected>$text</option>";
194
    }
195
    echo "</select>
196
                  &nbsp;&nbsp;&nbsp;
197
                  "._AM_XHELP_TEXT_NUMBER_PER_PAGE."
198
                  <select name='limit'>";
199 View Code Duplication
    foreach($aLimitBy as $value=>$text){
200
        ($limit == $value) ? $selected = "selected='selected'" : $selected = '';
201
        echo "<option value='$value' $selected>$text</option>";
202
    }
203
    echo "</select>
204
                  <input type='submit' name='file_sort' id='file_sort' value='"._AM_XHELP_BUTTON_SUBMIT."' />
205
              </td>
206
          </tr>";
207
    echo "</table></form>";
208
209
    echo "<form method='post' action='".XHELP_ADMIN_URL."/file.php?op=manageFiles'>";
210
    echo "<table width='100%' cellspacing='1' class='outer'>
211
          <tr><th colspan='6'><label>"._AM_XHELP_TEXT_MANAGE_FILES."</label></th></tr>";
212
    if($total != 0){
213
        echo "<tr class='head'>
214
                  <td>"._AM_XHELP_TEXT_ID."</td>
215
                  <td>"._AM_XHELP_TEXT_TICKETID."</td>
216
                  <td>"._AM_XHELP_TEXT_FILENAME."</td>
217
                  <td>"._AM_XHELP_TEXT_SIZE."</td>
218
                  <td>"._AM_XHELP_TEXT_MIMETYPE."</td>
219
                  <td>"._AM_XHELP_TEXT_ACTIONS."</td>
220
              </tr>";
221
222
        foreach($files as $file){
223
            $filepath = XHELP_BASE_URL.'/viewFile.php?id='. $file->getVar('id');
224
            $ticketpath = XHELP_BASE_URL.'/ticket.php?id='.$file->getVar('ticketid');
225
            $filesize = filesize($xhelpUploadDir.'/'.$file->getVar('filename'));
226
227
            echo "<tr class='even'>
228
                      <td><input type='checkbox' name='files[]' value='".$file->getVar('id')."' /> ".$file->getVar('id')."</td>
229
                      <td><a href='".$ticketpath."' target='_BLANK'>".$file->getVar('ticketid')."</a></td>
230
                      <td><a href='".$filepath."'>".$file->getVar('filename')."</a></td>
231
                      <td>".xhelpPrettyBytes($filesize)."</td>
232
                      <td>".$file->getVar('mimetype')."</td>
233
                      <td>
234
                          <a href='".XHELP_ADMIN_URL."/file.php?op=deleteFile&amp;fileid=".$file->getVar('id')."'><img src='".XOOPS_URL."/modules/xhelp/images/button_delete.png' title='"._AM_XHELP_TEXT_DELETE."' name='deleteFile' /></a>
235
                      </td>
236
                 </tr>";
237
        }
238
        echo "<tr class='foot'><td colspan='6'>
239
                                   <input type='checkbox' name='checkAllFiles' value='0' onclick='selectAll(this.form,\"files[]\",this.checked);' />
240
                                   <input type='submit' name='deleteFiles' id='deleteFiles' value='"._AM_XHELP_BUTTON_DELETE."' /></td></tr>";
241
        echo "</table></form>";
242
        echo "<div id='status_nav'>".$nav->renderNav()."</div>";
243
    } else {
244
        echo "<tr class='even'<td colspan='6'>"._AM_XHELP_TEXT_NO_FILES."</td></tr>";
245
        echo "</table></form>";
246
    }
247
248
include_once "admin_footer.php";
249
250
}
251