PageNav::renderImageNav()   C
last analyzed

Complexity

Conditions 15
Paths 6

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 27
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 38
rs 5.9166

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