Completed
Push — master ( ad6325...4bb5f3 )
by Michael
02:50
created

attachments.php ➔ showAttachments()   F

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 147
Code Lines 85

Duplication

Lines 5
Ratio 3.4 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
cc 30
eloc 85
c 6
b 0
f 2
nc 13519872
nop 0
dl 5
loc 147
rs 2

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
2
/**
3
 * admin/attachments.php - file attachment review tool
4
 *
5
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
6
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
7
 * @since      1.0
8
 * @author     Richard Griffith <[email protected]>
9
 * @package    gwiki
10
 */
11
include __DIR__ . '/header.php';
12
//include_once dirname(__DIR__) . '/include/functions.php';
13
14
echo $moduleAdmin->addNavigation(basename(__FILE__));
15
16
/**
17
 * @param      $string
18
 * @param bool $trim
19
 *
20
 * @return string
21
 */
22 View Code Duplication
function cleaner($string, $trim = true)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
23
{
24
    //  $string=stripcslashes($string);
25
    $string = html_entity_decode($string);
26
    $string = strip_tags($string);
27
    if ($trim) {
28
        $string = trim($string);
29
    }
30
    $string = stripslashes($string);
31
32
    return $string;
33
}
34
35
function showAttachments()
36
{
37
    global $xoopsDB, $wikiPage;
38
    $dir = basename(dirname(__DIR__));
39
    /*
40
    gwiki_page_files
41
      file_id int(10) NOT NULL AUTO_INCREMENT,
42
      keyword varchar(128) NOT NULL DEFAULT '',
43
      file_name varchar(128) NOT NULL DEFAULT '',
44
      file_path varchar(255) NOT NULL DEFAULT '',
45
      file_type varchar(128) NOT NULL DEFAULT '',
46
      file_icon varchar(64) NOT NULL DEFAULT '',
47
      file_size int(10) NOT NULL DEFAULT '0',
48
      file_upload_date int(10) NOT NULL DEFAULT '0',
49
      file_description text,
50
      file_uid int(10) NOT NULL DEFAULT '0',
51
    */
52
    $kw = '';
53
    $fn = '';
54
    $ty = '';
55
    $ds = '';
56
    if (!empty($_GET['kw'])) {
57
        $kw = cleaner($_GET['kw']);
58
    }
59
    if (!empty($_GET['fn'])) {
60
        $fn = cleaner($_GET['fn']);
61
    }
62
    if (!empty($_GET['ty'])) {
63
        $ty = cleaner($_GET['ty']);
64
    }
65
    if (!empty($_GET['ds'])) {
66
        $ds = cleaner($_GET['ds']);
67
    }
68
69
    $q_kw = '%' . $wikiPage->escapeForDB($kw) . '%';
70
    $q_fn = '%' . $wikiPage->escapeForDB($fn) . '%';
71
    $q_ty = '%' . $wikiPage->escapeForDB($ty) . '%';
72
    $q_ds = '%' . $wikiPage->escapeForDB($ds) . '%';
73
74
    $likeclause = '';
75
    if (!empty($kw)) {
76
        $likeclause .= (empty($likeclause) ? '' : ' and ') . " keyword like '{$q_kw}' ";
77
    }
78
    if (!empty($fn)) {
79
        $likeclause .= (empty($likeclause) ? '' : ' and ') . " file_name like '{$q_fn}' ";
80
    }
81
    if (!empty($ty)) {
82
        $likeclause .= (empty($likeclause) ? '' : ' and ') . " file_type like '{$q_ty}' ";
83
    }
84
    if (!empty($ds)) {
85
        $likeclause .= (empty($likeclause) ? '' : ' and ') . " file_description like '{$q_ds}' ";
86
    }
87
    //$whereclause=(empty($likeclause)?'':' where '.$likeclause);
88
    $whereclause = (empty($likeclause) ? '' : " where {$likeclause}");
89
90
    echo <<<EOT
91
<style>
92
div.pagination.default {display:inline;}
93
form {display:inline;}
94
</style>
95
EOT;
96
    $total = 0;
97
    $limit = 10;
98
    $start = 0;
99
    if (!empty($_GET['start'])) {
100
        $start = (int)$_GET['start'];
101
    }
102
103
    $sql    = 'SELECT count(*) FROM ' . $xoopsDB->prefix('gwiki_page_files') . $whereclause;
104
    $result = $xoopsDB->query($sql);
105
    if ($result) {
106
        $myrow = $xoopsDB->fetchRow($result);
107
        $total = $myrow[0];
108
    }
109
110
    adminTableStart(_AD_GWIKI_FILES_LIST, 9);
111
    echo '<tr><form method="get">' . '<td><input type="text" name="kw" size="10" value="' . $kw . '"></td>' . '<td><input type="text" name="fn" size="10" value="' . $fn . '"></td>' . '<td>&nbsp;</td>'
112
         . '<td><input type="text" name="ty" size="10" value="' . $ty . '"></td>' . '<td>&nbsp;</td>' . '<td>&nbsp;</td>' . '<td>&nbsp;</td>' . '<td><input type="text" name="ds" size="10" value="'
113
         . $ds . '"></td>' . '<td><input type="submit" value="' . _AD_GWIKI_FILES_FILTER . '"></td>' . '</form></tr>';
114
    echo '<tr class="head">' . '<th>' . _AD_GWIKI_FILES_KEYWORD . '</th>' . '<th>' . _AD_GWIKI_FILES_NAME . '</th>' . '<th>' . _AD_GWIKI_FILES_PATH . '</th>' . '<th>' . _AD_GWIKI_FILES_TYPE . '</th>'
115
         . '<th>' . _AD_GWIKI_FILES_ICON . '</th>' . '<th>' . _AD_GWIKI_FILES_SIZE . '</th>' . '<th>' . _AD_GWIKI_FILES_DATE . '</th>' . '<th>' . _AD_GWIKI_FILES_DESC . '</th>' . '<th>'
116
         . _AD_GWIKI_FILES_UID . '</th>' . '</tr>';
117
118
    $sql = 'SELECT * FROM ' . $xoopsDB->prefix('gwiki_page_files');
119
    $sql .= $whereclause;
120
    $sql .= ' ORDER BY file_upload_date DESC ';
121
122
    $result = $xoopsDB->query($sql, $limit, $start);
123
124
    for ($i = 0, $iMax = $xoopsDB->getRowsNum($result); $i < $iMax; ++$i) {
125
        $row = $xoopsDB->fetchArray($result);
126
        /*
127
        gwiki_page_files
128
          file_id int(10) NOT NULL AUTO_INCREMENT,
129
          keyword varchar(128) NOT NULL DEFAULT '',
130
          file_name varchar(128) NOT NULL DEFAULT '',
131
          file_path varchar(255) NOT NULL DEFAULT '',
132
          file_type varchar(128) NOT NULL DEFAULT '',
133
          file_icon varchar(64) NOT NULL DEFAULT '',
134
          file_size int(10) NOT NULL DEFAULT '0',
135
          file_upload_date int(10) NOT NULL DEFAULT '0',
136
          file_description text,
137
          file_uid int(10) NOT NULL DEFAULT '0',
138
        */
139
        echo '<tr class="' . (($i % 2) ? 'even' : 'odd') . '"><td><a href="../edit.php?page=' . $row['keyword'] . '">' . htmlspecialchars($row['keyword'], ENT_QUOTES) . '</a></td>' . '<td>'
140
             . htmlspecialchars($row['file_name'], ENT_QUOTES) . '</td>' . '<td><a href="' . XOOPS_URL . '/uploads/' . $dir . '/' . $row['file_path'] . '">' . htmlspecialchars($row['file_path'],
141
                                                                                                                                                                                ENT_QUOTES)
142
             . '</a></td>' . '<td>' . htmlspecialchars($row['file_type'], ENT_QUOTES) . '</td>' . '<td><img src="' . XOOPS_URL . '/modules/' . $dir . '/assets/icons/16px/' . $row['file_icon']
143
             . '.png" alt="' . $row['file_icon'] . '" title="' . $row['file_icon'] . '" /></td>' . '<td>' . htmlspecialchars($row['file_size'], ENT_QUOTES) . '</td>' . '<td>' . date('Y-m-d',
144
                                                                                                                                                                                      $row['file_upload_date'])
145
             . '</td>' . '<td>' . htmlspecialchars($row['file_description'], ENT_QUOTES) . '</td>' . '<td>' . $wikiPage->getUserName($row['file_uid']) . '</td>' . '</tr>';
146
    }
147
    if ($i === 0) {
148
        echo '<tr class="odd"><td colspan="9">' . _AD_GWIKI_FILES_EMPTY . '</td></tr>';
149
    }
150
151
    // set up pagenav
152
    $endarray = array();
153
    $pager    = '';
154
    if ($total > $limit) {
155
        include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
156
        $likenav = '';
157
        if (!empty($kw)) {
158
            $likenav .= (empty($likenav) ? '' : '&') . "kw={$kw}";
159
        }
160
        if (!empty($fn)) {
161
            $likenav .= (empty($likenav) ? '' : '&') . "fn={$fn}";
162
        }
163
        if (!empty($ty)) {
164
            $likenav .= (empty($likenav) ? '' : '&') . "ty={$ty}";
165
        }
166
        if (!empty($ds)) {
167
            $likenav .= (empty($likenav) ? '' : '&') . "ds={$ds}";
168
        }
169
        $nav = new xoopsPageNav($total, $limit, $start, 'start', $likenav);
170 View Code Duplication
        if ((int)($total / $limit) < 5) {
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...
171
            $pager = $nav->renderNav();
172
        } else {
173
            $pager = _AD_GWIKI_PAGENAV . $nav->renderSelect(false);
174
        }
175
    }
176
    if (!empty($pager)) {
177
        $endarray['!PREFORMATTED!'] = $pager;
178
    }
179
180
    adminTableEnd($endarray);
181
}
182
183
showAttachments();
184
185
include __DIR__ . '/footer.php';
186