Completed
Pull Request — master (#16)
by Michael
01:51
created

XoopsPageNav::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 5
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
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 (http://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
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
32
/**
33
 * Class XoopsPageNav
34
 */
35
class XoopsPageNav
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
36
{
37
    /**#@+
38
     * @access    private
39
     */
40
    public $total;
41
    public $perpage;
42
    public $current;
43
    public $url;
44
    /**#@-*/
45
46
    /**
47
     * Constructor
48
     *
49
     * @param int    $total_items   Total number of items
50
     * @param int    $items_perpage Number of items per page
51
     * @param int    $current_start First item on the current page
52
     * @param string $start_name    Name for "start" or "offset"
53
     * @param string $extra_arg     Additional arguments to pass in the URL
54
     **/
55
    public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '')
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER 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...
56
    {
57
        $this->total   = (int)$total_items;
58
        $this->perpage = (int)$items_perpage;
59
        $this->current = (int)$current_start;
60
        if ('' !== $extra_arg && ('&amp;' !== substr($extra_arg, -5) || '&' !== substr($extra_arg, -1))) {
61
            $extra_arg .= '&amp;';
62
        }
63
        $this->url = $_SERVER['PHP_SELF'] . '?' . $extra_arg . trim($start_name) . '=';
64
    }
65
66
    /**
67
     * Create text navigation
68
     *
69
     * @param integer $offset
70
     *
71
     * @return string
72
     **/
73
    public function renderNav($offset = 4)
74
    {
75
        $ret = '';
76
        if ($this->total <= $this->perpage) {
77
            return $ret;
78
        }
79
        $total_pages = ceil($this->total / $this->perpage);
80
        if ($total_pages > 1) {
81
            $prev = $this->current - $this->perpage;
82
            if ($prev >= 0) {
83
                $ret .= '<a href="' . $this->url . $prev . '"><u>&laquo;</u></a> ';
84
            }
85
            $counter      = 1;
86
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
87 View Code Duplication
            while ($counter <= $total_pages) {
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...
88
                if ($counter == $current_page) {
89
                    $ret .= '<b>(' . $counter . ')</b> ';
90
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter
91
                          || $counter == $total_pages) {
92
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
93
                        $ret .= '... ';
94
                    }
95
                    $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> ';
96
                    if (1 == $counter && $current_page > 1 + $offset) {
97
                        $ret .= '... ';
98
                    }
99
                }
100
                ++$counter;
101
            }
102
            $next = $this->current + $this->perpage;
103
            if ($this->total > $next) {
104
                $ret .= '<a href="' . $this->url . $next . '"><u>&raquo;</u></a> ';
105
            }
106
        }
107
108
        return $ret;
109
    }
110
111
    /**
112
     * Create a navigational dropdown list
113
     *
114
     * @param boolean $showbutton Show the "Go" button?
115
     *
116
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
117
     **/
118
    public function renderSelect($showbutton = false)
119
    {
120
        if ($this->total < $this->perpage) {
121
            return null;
122
        }
123
        $total_pages = ceil($this->total / $this->perpage);
124
        $ret         = '';
125
        if ($total_pages > 1) {
126
            $ret          = '<form name="pagenavform">';
127
            $ret          .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
128
            $counter      = 1;
129
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
130
            while ($counter <= $total_pages) {
131 View Code Duplication
                if ($counter == $current_page) {
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...
132
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $counter . '</option>';
133
                } else {
134
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>';
135
                }
136
                ++$counter;
137
            }
138
            $ret .= '</select>';
139
            if ($showbutton) {
140
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
141
            }
142
            $ret .= '</form>';
143
        }
144
145
        return $ret;
146
    }
147
148
    /**
149
     * Create an enhanced navigational dropdown list
150
     *
151
     * @param boolean $showbutton Show the "Go" button?
152
     * @param null    $titles
153
     *
154
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
155
     */
156
    public function renderEnhancedSelect($showbutton = false, $titles = null)
157
    {
158
        if ($this->total < $this->perpage) {
159
            return null;
160
        }
161
        $total_pages = ceil($this->total / $this->perpage);
162
        $ret         = '';
163
        if ($total_pages > 1) {
164
            $ret          = '<form name="pagenavform">';
165
            $ret          .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
166
            $counter      = 1;
167
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
168
            while ($counter <= $total_pages) {
169
                if (isset($titles[$counter - 1])) {
170
                    $title = $titles[$counter - 1];
171
                } else {
172
                    $title = $counter;
173
                }
174 View Code Duplication
                if ($counter == $current_page) {
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...
175
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $title . '</option>';
176
                } else {
177
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $title . '</option>';
178
                }
179
                ++$counter;
180
            }
181
            $ret .= '</select>';
182
            if ($showbutton) {
183
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '">';
184
            }
185
            $ret .= '</form>';
186
        }
187
188
        return $ret;
189
    }
190
191
    /**
192
     * Create navigation with images
193
     *
194
     * @param integer $offset
195
     *
196
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
197
     **/
198
    public function renderImageNav($offset = 4)
199
    {
200
        if ($this->total < $this->perpage) {
201
            return null;
202
        }
203
        $total_pages = ceil($this->total / $this->perpage);
204
        $ret         = '';
205
        if ($total_pages > 1) {
206
            $ret  = '<table><tr>';
207
            $prev = $this->current - $this->perpage;
208
            if ($prev >= 0) {
209
                $ret .= '<td class="pagneutral"><a href="' . $this->url . $prev . '">&lt;</a></td><td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td>';
210
            }
211
            $counter      = 1;
212
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
213 View Code Duplication
            while ($counter <= $total_pages) {
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...
214
                if ($counter == $current_page) {
215
                    $ret .= '<td class="pagact"><b>' . $counter . '</b></td>';
216
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter
217
                          || $counter == $total_pages) {
218
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
219
                        $ret .= '<td class="paginact">...</td>';
220
                    }
221
                    $ret .= '<td class="paginact"><a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a></td>';
222
                    if (1 == $counter && $current_page > 1 + $offset) {
223
                        $ret .= '<td class="paginact">...</td>';
224
                    }
225
                }
226
                ++$counter;
227
            }
228
            $next = $this->current + $this->perpage;
229
            if ($this->total > $next) {
230
                $ret .= '<td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt=""></td><td class="pagneutral"><a href="' . $this->url . $next . '">></a></td>';
231
            }
232
            $ret .= '</tr></table>';
233
        }
234
235
        return $ret;
236
    }
237
}
238