Completed
Push — master ( 6452b0...d576ea )
by Michael
05:58 queued 03:02
created

XoopsPageNav::XoopsPageNav()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.2
cc 4
eloc 7
nc 2
nop 5
1
<?php
2
// $Id$
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                  Copyright (c) 2000-2016 XOOPS.org                        //
6
//                       <http://xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
// Author: Kazumi Ono (AKA onokazu)                                          //
28
// URL: http://www.myweb.ne.jp/, http://xoops.org/, http://jp.xoops.org/ //
29
// Project: XOOPS Project                                                    //
30
// ------------------------------------------------------------------------- //
31
32
/**
33
 * Class to facilitate navigation in a multi page document/list
34
 *
35
 * @package              kernel
36
 * @subpackage           util
37
 *
38
 * @author               Kazumi Ono    <[email protected]>
39
 * @copyright        (c) XOOPS Project (http://xoops.org)
40
 */
41
42
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
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...
43
44
/**
45
 * Class XoopsPageNav
46
 */
47
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...
48
{
49
    /**#@+
50
     * @access    private
51
     */
52
    public $total;
53
    public $perpage;
54
    public $current;
55
    public $url;
56
    /**#@-*/
57
58
    /**
59
     * Constructor
60
     *
61
     * @param int    $total_items   Total number of items
62
     * @param int    $items_perpage Number of items per page
63
     * @param int    $current_start First item on the current page
64
     * @param string $start_name    Name for "start" or "offset"
65
     * @param string $extra_arg     Additional arguments to pass in the URL
66
     **/
67
    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...
68
    {
69
        $this->total   = (int)$total_items;
70
        $this->perpage = (int)$items_perpage;
71
        $this->current = (int)$current_start;
72
        if ($extra_arg != '' && (substr($extra_arg, -5) !== '&amp;' || substr($extra_arg, -1) !== '&')) {
73
            $extra_arg .= '&amp;';
74
        }
75
        $this->url = $_SERVER['PHP_SELF'] . '?' . $extra_arg . trim($start_name) . '=';
76
    }
77
78
    /**
79
     * Create text navigation
80
     *
81
     * @param integer $offset
82
     *
83
     * @return string
84
     **/
85
    public function renderNav($offset = 4)
86
    {
87
        $ret = '';
88
        if ($this->total <= $this->perpage) {
89
            return $ret;
90
        }
91
        $total_pages = ceil($this->total / $this->perpage);
92
        if ($total_pages > 1) {
93
            $prev = $this->current - $this->perpage;
94
            if ($prev >= 0) {
95
                $ret .= '<a href="' . $this->url . $prev . '"><u>&laquo;</u></a> ';
96
            }
97
            $counter      = 1;
98
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
99 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...
100
                if ($counter == $current_page) {
101
                    $ret .= '<b>(' . $counter . ')</b> ';
102
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1 || $counter == $total_pages) {
103
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
104
                        $ret .= '... ';
105
                    }
106
                    $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> ';
107
                    if ($counter == 1 && $current_page > 1 + $offset) {
108
                        $ret .= '... ';
109
                    }
110
                }
111
                ++$counter;
112
            }
113
            $next = $this->current + $this->perpage;
114
            if ($this->total > $next) {
115
                $ret .= '<a href="' . $this->url . $next . '"><u>&raquo;</u></a> ';
116
            }
117
        }
118
119
        return $ret;
120
    }
121
122
    /**
123
     * Create a navigational dropdown list
124
     *
125
     * @param boolean $showbutton Show the "Go" button?
126
     *
127
     * @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...
128
     **/
129
    public function renderSelect($showbutton = false)
130
    {
131
        if ($this->total < $this->perpage) {
132
            return null;
133
        }
134
        $total_pages = ceil($this->total / $this->perpage);
135
        $ret         = '';
136
        if ($total_pages > 1) {
137
            $ret = '<form name="pagenavform">';
138
            $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
139
            $counter      = 1;
140
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
141
            while ($counter <= $total_pages) {
142 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...
143
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected="selected">' . $counter . '</option>';
144
                } else {
145
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>';
146
                }
147
                ++$counter;
148
            }
149
            $ret .= '</select>';
150
            if ($showbutton) {
151
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '" />';
152
            }
153
            $ret .= '</form>';
154
        }
155
156
        return $ret;
157
    }
158
159
    /**
160
     * Create an enhanced navigational dropdown list
161
     *
162
     * @param boolean $showbutton Show the "Go" button?
163
     * @param null    $titles
164
     *
165
     * @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...
166
     */
167
    public function renderEnhancedSelect($showbutton = false, $titles = null)
168
    {
169
        if ($this->total < $this->perpage) {
170
            return null;
171
        }
172
        $total_pages = ceil($this->total / $this->perpage);
173
        $ret         = '';
174
        if ($total_pages > 1) {
175
            $ret = '<form name="pagenavform">';
176
            $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
177
            $counter      = 1;
178
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
179
            while ($counter <= $total_pages) {
180
                if (isset($titles[$counter - 1])) {
181
                    $title = $titles[$counter - 1];
182
                } else {
183
                    $title = $counter;
184
                }
185 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...
186
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected="selected">' . $title . '</option>';
187
                } else {
188
                    $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $title . '</option>';
189
                }
190
                ++$counter;
191
            }
192
            $ret .= '</select>';
193
            if ($showbutton) {
194
                $ret .= '&nbsp;<input type="submit" value="' . _GO . '" />';
195
            }
196
            $ret .= '</form>';
197
        }
198
199
        return $ret;
200
    }
201
202
    /**
203
     * Create navigation with images
204
     *
205
     * @param integer $offset
206
     *
207
     * @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...
208
     **/
209
    public function renderImageNav($offset = 4)
210
    {
211
        if ($this->total < $this->perpage) {
212
            return null;
213
        }
214
        $total_pages = ceil($this->total / $this->perpage);
215
        $ret         = '';
216
        if ($total_pages > 1) {
217
            $ret  = '<table><tr>';
218
            $prev = $this->current - $this->perpage;
219
            if ($prev >= 0) {
220
                $ret .= '<td class="pagneutral"><a href="' . $this->url . $prev . '">&lt;</a></td><td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt="" /></td>';
221
            }
222
            $counter      = 1;
223
            $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage);
224 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...
225
                if ($counter == $current_page) {
226
                    $ret .= '<td class="pagact"><b>' . $counter . '</b></td>';
227
                } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || $counter == 1 || $counter == $total_pages) {
228
                    if ($counter == $total_pages && $current_page < $total_pages - $offset) {
229
                        $ret .= '<td class="paginact">...</td>';
230
                    }
231
                    $ret .= '<td class="paginact"><a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a></td>';
232
                    if ($counter == 1 && $current_page > 1 + $offset) {
233
                        $ret .= '<td class="paginact">...</td>';
234
                    }
235
                }
236
                ++$counter;
237
            }
238
            $next = $this->current + $this->perpage;
239
            if ($this->total > $next) {
240
                $ret .= '<td><img src="' . XOOPS_URL . '/images/blank.gif" width="6" alt="" /></td><td class="pagneutral"><a href="' . $this->url . $next . '">&gt;</a></td>';
241
            }
242
            $ret .= '</tr></table>';
243
        }
244
245
        return $ret;
246
    }
247
}
248