Passed
Push — 1.10.x ( 25fff6...e4c880 )
by
unknown
59:25 queued 07:58
created

Pager_Sliding::getPageIdByOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
 * Contains the Pager_Sliding class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  HTML
31
 * @package   Pager
32
 * @author    Lorenzo Alberton <[email protected]>
33
 * @copyright 2003-2008 Lorenzo Alberton
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: Sliding.php,v 1.18 2008/01/06 13:36:22 quipo Exp $
36
 * @link      http://pear.php.net/package/Pager
37
 */
38
39
/**
40
 * Pager_Sliding - Generic data paging class  ("sliding window" style)
41
 * Usage examples can be found in the PEAR manual
42
 *
43
 * @category  HTML
44
 * @package   Pager
45
 * @author    Lorenzo Alberton <[email protected]>
46
 * @copyright 2003-2008 Lorenzo Alberton
47
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
48
 * @link      http://pear.php.net/package/Pager
49
 */
50
class Pager_Sliding extends Pager_Common
51
{
52
    /**
53
     * Constructor
54
     *
55
     * @param array $options Associative array of option names and their values
56
     *
57
     * @access public
58
     */
59
    public function __construct($options = array())
60
    {
61
        //set default Pager_Sliding options
62
        $this->_delta                 = 2;
63
        $this->_prevImg               = '&laquo;';
64
        $this->_nextImg               = '&raquo;';
65
        $this->_separator             = '|';
66
        $this->_spacesBeforeSeparator = 3;
67
        $this->_spacesAfterSeparator  = 3;
68
        $this->_curPageSpanPre        = '<b>';
69
        $this->_curPageSpanPost       = '</b>';
70
71
        //set custom options
72
        $err = $this->setOptions($options);
73
        if ($err !== PAGER_OK) {
74
            return $this->raiseError($this->errorMessage($err), $err);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
75
        }
76
        $this->build();
77
    }
78
79
    // }}}
80
    // {{{ getPageIdByOffset()
81
82
    /**
83
     * "Overload" PEAR::Pager method. VOID. Not needed here...
84
     *
85
     * @param integer $index Offset to get pageID for
86
     *
87
     * @return void
88
     * @deprecated
89
     * @access public
90
     */
91
    function getPageIdByOffset($index)
92
    {
93
    }
94
95
    // }}}
96
    // {{{ getPageRangeByPageId()
97
98
    /**
99
     * Given a PageId, it returns the limits of the range of pages displayed.
100
     * While getOffsetByPageId() returns the offset of the data within the
101
     * current page, this method returns the offsets of the page numbers interval.
102
     * E.g., if you have pageId=5 and delta=2, it will return (3, 7).
103
     * PageID of 9 would give you (4, 8).
104
     * If the method is called without parameter, pageID is set to currentPage#.
105
     *
106
     * @param integer $pageid PageID to get offsets for
107
     *
108
     * @return array  First and last offsets
109
     * @access public
110
     */
111
    function getPageRangeByPageId($pageid = null)
112
    {
113
        $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
114
        if (!isset($this->_pageData)) {
115
            $this->_generatePageData();
116
        }
117
        if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
118
            if ($this->_expanded) {
119
                $min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
120
                $max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
121
                                ($pageid - ($this->_totalPages - $this->_delta)) : 0;
122
            } else {
123
                $min_surplus = $max_surplus = 0;
124
            }
125
            return array(
126
                max($pageid - $this->_delta - $max_surplus, 1),
127
                min($pageid + $this->_delta + $min_surplus, $this->_totalPages)
128
            );
129
        }
130
        return array(0, 0);
131
    }
132
133
    // }}}
134
    // {{{ getLinks()
135
136
    /**
137
     * Returns back/next/first/last and page links,
138
     * both as ordered and associative array.
139
     *
140
     * @param integer $pageID Optional pageID. If specified, links for that page
141
     *                        are provided instead of current one.
142
     * @param string  $dummy  used to comply with parent signature (leave empty)
143
     *
144
     * @return array back/pages/next/first/last/all links
145
     * @access public
146
     */
147
    function getLinks($pageID = null, $dummy='')
148
    {
149
        if (!is_null($pageID)) {
150
            $_sav = $this->_currentPage;
151
            $this->_currentPage = $pageID;
152
153
            $this->links = '';
154
            if ($this->_totalPages > (2 * $this->_delta + 1)) {
155
                $this->links .= $this->_printFirstPage();
156
            }
157
            $this->links .= $this->_getBackLink();
158
            $this->links .= $this->_getPageLinks();
159
            $this->links .= $this->_getNextLink();
160
            if ($this->_totalPages > (2 * $this->_delta + 1)) {
161
                $this->links .= $this->_printLastPage();
162
            }
163
        }
164
165
        $back        = str_replace('&nbsp;', '', $this->_getBackLink());
166
        $next        = str_replace('&nbsp;', '', $this->_getNextLink());
167
        $pages       = $this->_getPageLinks();
168
        $first       = $this->_printFirstPage();
169
        $last        = $this->_printLastPage();
170
        $all         = $this->links;
171
        $linkTags    = $this->linkTags;
172
        $linkTagsRaw = $this->linkTagsRaw;
173
174
        if (!is_null($pageID)) {
175
            $this->_currentPage = $_sav;
176
        }
177
178
        return array(
179
            $back,
180
            $pages,
181
            trim($next),
182
            $first,
183
            $last,
184
            $all,
185
            $linkTags,
186
            'back'        => $back,
187
            'pages'       => $pages,
188
            'next'        => $next,
189
            'first'       => $first,
190
            'last'        => $last,
191
            'all'         => $all,
192
            'linktags'    => $linkTags,
193
            'linkTagsRaw' => $linkTagsRaw,
194
        );
195
    }
196
197
    // }}}
198
    // {{{ _getPageLinks()
199
200
    /**
201
     * Returns pages link
202
     *
203
     * @param string $url URL string [deprecated]
204
     *
205
     * @return string Links
206
     * @access private
207
     */
208
    function _getPageLinks($url = '')
209
    {
210
        //legacy setting... the preferred way to set an option now
211
        //is adding it to the constuctor
212
        if (!empty($url)) {
213
            $this->_path = $url;
214
        }
215
216
        //If there's only one page, don't display links
217
        if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
218
            return '';
219
        }
220
221
        $links = '';
222
        if ($this->_totalPages > (2 * $this->_delta + 1)) {
223
            if ($this->_expanded) {
224
                if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
225
                    $expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
226
                } else {
227
                    $expansion_before = 0;
228
                }
229
                for ($i = $this->_currentPage - $this->_delta - $expansion_before; $expansion_before; $expansion_before--, $i++) {
230
                    $print_separator_flag = ($i != $this->_currentPage + $this->_delta); // && ($i != $this->_totalPages - 1)
231
232
                    $this->range[$i] = false;
233
                    $this->_linkData[$this->_urlVar] = $i;
234
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
235
                           . $this->_spacesBefore
236
                           . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
237
                }
238
            }
239
240
            $expansion_after = 0;
241
            for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
242
                if ($i < 1) {
243
                    ++$expansion_after;
244
                    continue;
245
                }
246
247
                // check when to print separator
248
                $print_separator_flag = (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages));
249
250 View Code Duplication
                if ($i == $this->_currentPage) {
251
                    $this->range[$i] = true;
252
                    $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
253
                } else {
254
                    $this->range[$i] = false;
255
                    $this->_linkData[$this->_urlVar] = $i;
256
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
257
                }
258
                $links .= $this->_spacesBefore
259
                        . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
260
            }
261
262
            if ($this->_expanded && $expansion_after) {
263
                $links .= $this->_separator . $this->_spacesAfter;
264
                for ($i = $this->_currentPage + $this->_delta +1; $expansion_after; $expansion_after--, $i++) {
265
                    $print_separator_flag = ($expansion_after != 1);
266
                    $this->range[$i] = false;
267
                    $this->_linkData[$this->_urlVar] = $i;
268
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
269
                      . $this->_spacesBefore
270
                      . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
271
                }
272
            }
273
274
        } else {
275
            //if $this->_totalPages <= (2*Delta+1) show them all
276
            for ($i=1; $i<=$this->_totalPages; $i++) {
277 View Code Duplication
                if ($i != $this->_currentPage) {
278
                    $this->range[$i] = false;
279
                    $this->_linkData[$this->_urlVar] = $i;
280
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
281
                } else {
282
                    $this->range[$i] = true;
283
                    $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
284
                }
285
                $links .= $this->_spacesBefore
286
                       . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
287
            }
288
        }
289
        return $links;
290
    }
291
292
    // }}}
293
}
294
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
295