Passed
Push — master ( 6d0bec...700d12 )
by Michael
05:09 queued 02:42
created

XoopsPageNav   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 47
eloc 107
c 0
b 0
f 0
dl 0
loc 201
rs 8.64

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
C renderNav() 0 36 15
C renderImageNav() 0 38 15
B renderEnhancedSelect() 0 33 7
B renderSelect() 0 28 6

How to fix   Complexity   

Complex Class

Complex classes like XoopsPageNav often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use XoopsPageNav, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
/**
21
 * Class to facilitate navigation in a multi page document/list
22
 *
23
 * @package              kernel
24
 * @subpackage           util
25
 *
26
 * @author               Kazumi Ono    <[email protected]>
27
 * @copyright        (c) XOOPS Project (https://xoops.org)
28
 */
29
30
/**
31
 * Class XoopsPageNav
32
 */
33
class XoopsPageNav
34
{
35
    /**#@+
36
     * @access    private
37
     */
38
    public $total;
39
    public $perpage;
40
    public $current;
41
    public $url;
42
    /**#@-*/
43
44
    /**
45
     * Constructor
46
     *
47
     * @param int    $total_items   Total number of items
48
     * @param int    $items_perpage Number of items per page
49
     * @param int    $current_start First item on the current page
50
     * @param string $start_name    Name for "start" or "offset"
51
     * @param string $extra_arg     Additional arguments to pass in the URL
52
     **/
53
    public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '')
54
    {
55
        $this->total   = (int)$total_items;
56
        $this->perpage = (int)$items_perpage;
57
        $this->current = (int)$current_start;
58
        if ('' !== $extra_arg && ('&amp;' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) {
59
            $extra_arg .= '&amp;';
60
        }
61
        $this->url = $_SERVER['SCRIPT_NAME'] . '?' . $extra_arg . trim($start_name) . '=';
62
    }
63
64
    /**
65
     * Create text navigation
66
     *
67
     * @param int $offset
68
     *
69
     * @return string
70
     **/
71
    public function renderNav($offset = 4)
72
    {
73
        $ret = '';
74
        if ($this->total <= $this->perpage) {
75
            return $ret;
76
        }
77
        $total_pages = ceil($this->total / $this->perpage);
78
        if ($total_pages > 1) {
79
            $prev = $this->current - $this->perpage;
80
            if ($prev >= 0) {
81
                $ret .= '<a href="' . $this->url . $prev . '"><u>&laquo;</u></a> ';
82
            }
83
            $counter      = 1;
84
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
85
            while ($counter <= $total_pages) {
86
                if ($counter == $current_page) {
87
                    $ret .= '<b>(' . $counter . ')</b> ';
88
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter
89
                          || $counter == $total_pages) {
90
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
91
                        $ret .= '... ';
92
                    }
93
                    $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> ';
94
                    if (1 == $counter && $current_page > 1 + $offset) {
95
                        $ret .= '... ';
96
                    }
97
                }
98
                ++$counter;
99
            }
100
            $next = $this->current + $this->perpage;
101
            if ($this->total > $next) {
102
                $ret .= '<a href="' . $this->url . $next . '"><u>&raquo;</u></a> ';
103
            }
104
        }
105
106
        return $ret;
107
    }
108
109
    /**
110
     * Create a navigational dropdown list
111
     *
112
     * @param bool $showbutton Show the "Go" button?
113
     *
114
     * @return string
115
     **/
116
    public function renderSelect($showbutton = false)
117
    {
118
        if ($this->total < $this->perpage) {
119
            return null;
120
        }
121
        $total_pages = ceil($this->total / $this->perpage);
122
        $ret         = '';
123
        if ($total_pages > 1) {
124
            $ret          = '<form name="pagenavform">';
125
            $ret          .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
126
            $counter      = 1;
127
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
128
            while ($counter <= $total_pages) {
129
                if ($counter == $current_page) {
130
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $counter . '</option>';
131
                } else {
132
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>';
133
                }
134
                ++$counter;
135
            }
136
            $ret .= '</select>';
137
            if ($showbutton) {
138
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
139
            }
140
            $ret .= '</form>';
141
        }
142
143
        return $ret;
144
    }
145
146
    /**
147
     * Create an enhanced navigational dropdown list
148
     *
149
     * @param bool $showbutton Show the "Go" button?
150
     * @param null $titles
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $titles is correct as it would always require null to be passed?
Loading history...
151
     *
152
     * @return string
153
     */
154
    public function renderEnhancedSelect($showbutton = false, $titles = null)
155
    {
156
        if ($this->total < $this->perpage) {
157
            return null;
158
        }
159
        $total_pages = ceil($this->total / $this->perpage);
160
        $ret         = '';
161
        if ($total_pages > 1) {
162
            $ret          = '<form name="pagenavform">';
163
            $ret          .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
164
            $counter      = 1;
165
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
166
            while ($counter <= $total_pages) {
167
                if (isset($titles[$counter - 1])) {
168
                    $title = $titles[$counter - 1];
169
                } else {
170
                    $title = $counter;
171
                }
172
                if ($counter == $current_page) {
173
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $title . '</option>';
174
                } else {
175
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $title . '</option>';
176
                }
177
                ++$counter;
178
            }
179
            $ret .= '</select>';
180
            if ($showbutton) {
181
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
182
            }
183
            $ret .= '</form>';
184
        }
185
186
        return $ret;
187
    }
188
189
    /**
190
     * Create navigation with images
191
     *
192
     * @param int $offset
193
     *
194
     * @return string
195
     **/
196
    public function renderImageNav($offset = 4)
197
    {
198
        if ($this->total < $this->perpage) {
199
            return null;
200
        }
201
        $total_pages = ceil($this->total / $this->perpage);
202
        $ret         = '';
203
        if ($total_pages > 1) {
204
            $ret  = '<table><tr>';
205
            $prev = $this->current - $this->perpage;
206
            if ($prev >= 0) {
207
                $ret .= '<td class="pagneutral"><a href="' . $this->url . $prev . '">&lt;</a></td><td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td>';
208
            }
209
            $counter      = 1;
210
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
211
            while ($counter <= $total_pages) {
212
                if ($counter == $current_page) {
213
                    $ret .= '<td class="pagact"><b>' . $counter . '</b></td>';
214
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter
215
                          || $counter == $total_pages) {
216
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
217
                        $ret .= '<td class="paginact">...</td>';
218
                    }
219
                    $ret .= '<td class="paginact"><a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a></td>';
220
                    if (1 == $counter && $current_page > 1 + $offset) {
221
                        $ret .= '<td class="paginact">...</td>';
222
                    }
223
                }
224
                ++$counter;
225
            }
226
            $next = $this->current + $this->perpage;
227
            if ($this->total > $next) {
228
                $ret .= '<td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td><td class="pagneutral"><a href="' . $this->url . $next . '">></a></td>';
229
            }
230
            $ret .= '</tr></table>';
231
        }
232
233
        return $ret;
234
    }
235
}
236