Subversion Repositories Applications.papyrus

Rev

Rev 1713 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
848 florian 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 <l dot alberton at quipo dot it>
33
 * @copyright  2003-2006 Lorenzo Alberton
34
 * @license    http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version    CVS: $Id$
36
 * @link       http://pear.php.net/package/Pager
37
 */
38
 
39
/**
40
 * require PEAR::Pager_Common base class
41
 */
42
require_once 'Pager/Common.php';
43
 
44
/**
45
 * Pager_Sliding - Generic data paging class  ("sliding window" style)
46
 * Usage examples can be found in the PEAR manual
47
 *
48
 * @category   HTML
49
 * @package    Pager
50
 * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
51
 * @copyright  2003-2005 Lorenzo Alberton
52
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
53
 * @link       http://pear.php.net/package/Pager
54
 */
55
class Pager_Sliding extends Pager_Common
56
{
57
    // {{{ Pager_Sliding()
58
 
59
    /**
60
     * Constructor
61
     *
62
     * @param array $options    An associative array of option names
63
     *                          and their values
64
     * @access public
65
     */
66
    function Pager_Sliding($options = array())
67
    {
68
        //set default Pager_Sliding options
69
        $this->_delta                 = 2;
70
        $this->_prevImg               = '&laquo;';
71
        $this->_nextImg               = '&raquo;';
72
        $this->_separator             = '|';
73
        $this->_spacesBeforeSeparator = 3;
74
        $this->_spacesAfterSeparator  = 3;
75
        $this->_curPageSpanPre        = '<b><u>';
76
        $this->_curPageSpanPost       = '</u></b>';
77
 
78
        //set custom options
79
        $err = $this->setOptions($options);
80
        if ($err !== PAGER_OK) {
81
            return $this->raiseError($this->errorMessage($err), $err);
82
        }
83
        $this->build();
84
    }
85
 
86
    // }}}
87
    // {{{ build()
88
 
89
    /**
90
     * Generate or refresh the links and paged data after a call to setOptions()
91
     *
92
     * @access public
93
     */
94
    function build()
95
    {
96
        //reset
97
        $this->_pageData = array();
98
        $this->links = '';
99
 
100
        $this->_generatePageData();
101
        $this->_setFirstLastText();
102
 
103
        if ($this->_totalPages > (2 * $this->_delta + 1)) {
104
            $this->links .= $this->_printFirstPage();
105
        }
106
 
107
        $this->links .= $this->_getBackLink();
108
        $this->links .= $this->_getPageLinks();
109
        $this->links .= $this->_getNextLink();
110
 
111
        $this->linkTags .= $this->_getFirstLinkTag();
112
        $this->linkTags .= $this->_getPrevLinkTag();
113
        $this->linkTags .= $this->_getNextLinkTag();
114
        $this->linkTags .= $this->_getLastLinkTag();
115
 
116
        if ($this->_totalPages > (2 * $this->_delta + 1)) {
117
            $this->links .= $this->_printLastPage();
118
        }
119
    }
120
 
121
    // }}}
122
    // {{{ getPageIdByOffset()
123
 
124
    /**
125
     * "Overload" PEAR::Pager method. VOID. Not needed here...
126
     * @param integer $index Offset to get pageID for
127
     * @deprecated
128
     * @access public
129
     */
130
    function getPageIdByOffset($index=null) { }
131
 
132
    // }}}
133
    // {{{ getPageRangeByPageId()
134
 
135
    /**
136
     * Given a PageId, it returns the limits of the range of pages displayed.
137
     * While getOffsetByPageId() returns the offset of the data within the
138
     * current page, this method returns the offsets of the page numbers interval.
139
     * E.g., if you have pageId=5 and delta=2, it will return (3, 7).
140
     * PageID of 9 would give you (4, 8).
141
     * If the method is called without parameter, pageID is set to currentPage#.
142
     *
143
     * @param integer PageID to get offsets for
144
     * @return array  First and last offsets
145
     * @access public
146
     */
147
    function getPageRangeByPageId($pageid = null)
148
    {
149
        $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
150
        if (!isset($this->_pageData)) {
151
            $this->_generatePageData();
152
        }
153
        if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
154
            if ($this->_expanded) {
155
                $min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
156
                $max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
157
                                ($pageid - ($this->_totalPages - $this->_delta)) : 0;
158
            } else {
159
                $min_surplus = $max_surplus = 0;
160
            }
161
            return array(
162
                max($pageid - $this->_delta - $max_surplus, 1),
163
                min($pageid + $this->_delta + $min_surplus, $this->_totalPages)
164
            );
165
        }
166
        return array(0, 0);
167
    }
168
 
169
    // }}}
170
    // {{{ getLinks()
171
 
172
    /**
173
     * Returns back/next/first/last and page links,
174
     * both as ordered and associative array.
175
     *
176
     * @param integer $pageID Optional pageID. If specified, links
177
     *                for that page are provided instead of current one.
178
     * @return array back/pages/next/first/last/all links
179
     * @access public
180
     */
181
    function getLinks($pageID = null)
182
    {
183
        if ($pageID != null) {
184
            $_sav = $this->_currentPage;
185
            $this->_currentPage = $pageID;
186
 
187
            $this->links = '';
188
            if ($this->_totalPages > (2 * $this->_delta + 1)) {
189
                $this->links .= $this->_printFirstPage();
190
            }
191
            $this->links .= $this->_getBackLink();
192
            $this->links .= $this->_getPageLinks();
193
            $this->links .= $this->_getNextLink();
194
            if ($this->_totalPages > (2 * $this->_delta + 1)) {
195
                $this->links .= $this->_printLastPage();
196
            }
197
        }
198
 
199
        $back  = str_replace('&nbsp;', '', $this->_getBackLink());
200
        $next  = str_replace('&nbsp;', '', $this->_getNextLink());
201
        $pages = $this->_getPageLinks();
202
        $first = $this->_printFirstPage();
203
        $last  = $this->_printLastPage();
204
        $all   = $this->links;
205
        $linkTags = $this->linkTags;
206
 
207
        if ($pageID != null) {
208
            $this->_currentPage = $_sav;
209
        }
210
 
211
        return array(
212
            $back,
213
            $pages,
214
            trim($next),
215
            $first,
216
            $last,
217
            $all,
218
            $linkTags,
219
            'back'  => $back,
220
            'pages' => $pages,
221
            'next'  => $next,
222
            'first' => $first,
223
            'last'  => $last,
224
            'all'   => $all,
225
            'linktags' => $linkTags
226
        );
227
    }
228
 
229
    // }}}
230
    // {{{ _getPageLinks()
231
 
232
    /**
233
     * Returns pages link
234
     *
235
     * @return string Links
236
     * @access private
237
     */
238
    function _getPageLinks($url = '')
239
    {
240
        //legacy setting... the preferred way to set an option now
241
        //is adding it to the constuctor
242
        if (!empty($url)) {
243
            $this->_path = $url;
244
        }
245
 
246
        //If there's only one page, don't display links
247
        if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
248
            return '';
249
        }
250
 
251
        $links = '';
252
        if ($this->_totalPages > (2 * $this->_delta + 1)) {
253
            if ($this->_expanded) {
254
                if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
255
                    $expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
256
                } else {
257
                    $expansion_before = 0;
258
                }
259
                for ($i = $this->_currentPage - $this->_delta - $expansion_before; $expansion_before; $expansion_before--, $i++) {
260
                    $print_separator_flag = ($i != $this->_currentPage + $this->_delta); // && ($i != $this->_totalPages - 1)
261
 
262
                    $this->range[$i] = false;
263
                    $this->_linkData[$this->_urlVar] = $i;
264
                    $links .= $this->_renderLink($this->_altPage.' '.$i, $i)
265
                           . $this->_spacesBefore
266
                           . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
267
                }
268
            }
269
 
270
            $expansion_after = 0;
271
            for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
272
                if ($i < 1) {
273
                    ++$expansion_after;
274
                    continue;
275
                }
276
 
277
                // check when to print separator
278
                $print_separator_flag = (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages));
279
 
280
                if ($i == $this->_currentPage) {
281
                    $this->range[$i] = true;
282
                    $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
283
                } else {
284
                    $this->range[$i] = false;
285
                    $this->_linkData[$this->_urlVar] = $i;
286
                    $links .= $this->_renderLink($this->_altPage.' '.$i, $i);
287
                }
288
                $links .= $this->_spacesBefore
289
                        . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
290
            }
291
 
292
            if ($this->_expanded && $expansion_after) {
293
                $links .= $this->_separator . $this->_spacesAfter;
294
                for ($i = $this->_currentPage + $this->_delta +1; $expansion_after; $expansion_after--, $i++) {
295
                    $print_separator_flag = ($expansion_after != 1);
296
                    $this->range[$i] = false;
297
                    $this->_linkData[$this->_urlVar] = $i;
298
                    $links .= $this->_renderLink($this->_altPage.' '.$i, $i)
299
                      . $this->_spacesBefore
300
                      . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
301
                }
302
            }
303
 
304
        } else {
305
            //if $this->_totalPages <= (2*Delta+1) show them all
306
            for ($i=1; $i<=$this->_totalPages; $i++) {
307
                if ($i != $this->_currentPage) {
308
                    $this->range[$i] = false;
309
                    $this->_linkData[$this->_urlVar] = $i;
310
                    $links .= $this->_renderLink($this->_altPage.' '.$i, $i);
311
                } else {
312
                    $this->range[$i] = true;
313
                    $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
314
                }
315
                $links .= $this->_spacesBefore
316
                       . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
317
            }
318
        }
319
        return $links;
320
    }
321
 
322
    // }}}
323
}
324
?>