Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.alberton@quipo.it>
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
 * 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.alberton@quipo.it>
51
 * @copyright 2003-2008 Lorenzo Alberton
52
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
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 Associative array of option names and their values
63
     *
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>';
76
        $this->_curPageSpanPost       = '</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
    // {{{ getPageIdByOffset()
88
 
89
    /**
90
     * "Overload" PEAR::Pager method. VOID. Not needed here...
91
     *
92
     * @param integer $index Offset to get pageID for
93
     *
94
     * @return void
95
     * @deprecated
96
     * @access public
97
     */
98
    function getPageIdByOffset($index)
99
    {
100
    }
101
 
102
    // }}}
103
    // {{{ getPageRangeByPageId()
104
 
105
    /**
106
     * Given a PageId, it returns the limits of the range of pages displayed.
107
     * While getOffsetByPageId() returns the offset of the data within the
108
     * current page, this method returns the offsets of the page numbers interval.
109
     * E.g., if you have pageId=5 and delta=2, it will return (3, 7).
110
     * PageID of 9 would give you (4, 8).
111
     * If the method is called without parameter, pageID is set to currentPage#.
112
     *
113
     * @param integer $pageid PageID to get offsets for
114
     *
115
     * @return array  First and last offsets
116
     * @access public
117
     */
118
    function getPageRangeByPageId($pageid = null)
119
    {
120
        $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
121
        if (!isset($this->_pageData)) {
122
            $this->_generatePageData();
123
        }
124
        if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
125
            if ($this->_expanded) {
126
                $min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
127
                $max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
128
                                ($pageid - ($this->_totalPages - $this->_delta)) : 0;
129
            } else {
130
                $min_surplus = $max_surplus = 0;
131
            }
132
            return array(
133
                max($pageid - $this->_delta - $max_surplus, 1),
134
                min($pageid + $this->_delta + $min_surplus, $this->_totalPages)
135
            );
136
        }
137
        return array(0, 0);
138
    }
139
 
140
    // }}}
141
    // {{{ getLinks()
142
 
143
    /**
144
     * Returns back/next/first/last and page links,
145
     * both as ordered and associative array.
146
     *
147
     * @param integer $pageID Optional pageID. If specified, links for that page
148
     *                        are provided instead of current one.
149
     * @param string  $dummy  used to comply with parent signature (leave empty)
150
     *
151
     * @return array back/pages/next/first/last/all links
152
     * @access public
153
     */
154
    function getLinks($pageID = null, $dummy='')
155
    {
156
        if (!is_null($pageID)) {
157
            $_sav = $this->_currentPage;
158
            $this->_currentPage = $pageID;
159
 
160
            $this->links = '';
161
            if ($this->_totalPages > (2 * $this->_delta + 1)) {
162
                $this->links .= $this->_printFirstPage();
163
            }
164
            $this->links .= $this->_getBackLink();
165
            $this->links .= $this->_getPageLinks();
166
            $this->links .= $this->_getNextLink();
167
            if ($this->_totalPages > (2 * $this->_delta + 1)) {
168
                $this->links .= $this->_printLastPage();
169
            }
170
        }
171
 
172
        $back        = str_replace('&nbsp;', '', $this->_getBackLink());
173
        $next        = str_replace('&nbsp;', '', $this->_getNextLink());
174
        $pages       = $this->_getPageLinks();
175
        $first       = $this->_printFirstPage();
176
        $last        = $this->_printLastPage();
177
        $all         = $this->links;
178
        $linkTags    = $this->linkTags;
179
        $linkTagsRaw = $this->linkTagsRaw;
180
 
181
        if (!is_null($pageID)) {
182
            $this->_currentPage = $_sav;
183
        }
184
 
185
        return array(
186
            $back,
187
            $pages,
188
            trim($next),
189
            $first,
190
            $last,
191
            $all,
192
            $linkTags,
193
            'back'        => $back,
194
            'pages'       => $pages,
195
            'next'        => $next,
196
            'first'       => $first,
197
            'last'        => $last,
198
            'all'         => $all,
199
            'linktags'    => $linkTags,
200
            'linkTagsRaw' => $linkTagsRaw,
201
        );
202
    }
203
 
204
    // }}}
205
    // {{{ _getPageLinks()
206
 
207
    /**
208
     * Returns pages link
209
     *
210
     * @param string $url URL string [deprecated]
211
     *
212
     * @return string Links
213
     * @access private
214
     */
215
    function _getPageLinks($url = '')
216
    {
217
        //legacy setting... the preferred way to set an option now
218
        //is adding it to the constuctor
219
        if (!empty($url)) {
220
            $this->_path = $url;
221
        }
222
 
223
        //If there's only one page, don't display links
224
        if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
225
            return '';
226
        }
227
 
228
        $links = '';
229
        if ($this->_totalPages > (2 * $this->_delta + 1)) {
230
            if ($this->_expanded) {
231
                if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
232
                    $expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
233
                } else {
234
                    $expansion_before = 0;
235
                }
236
                for ($i = $this->_currentPage - $this->_delta - $expansion_before; $expansion_before; $expansion_before--, $i++) {
237
                    $print_separator_flag = ($i != $this->_currentPage + $this->_delta); // && ($i != $this->_totalPages - 1)
238
 
239
                    $this->range[$i] = false;
240
                    $this->_linkData[$this->_urlVar] = $i;
241
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
242
                           . $this->_spacesBefore
243
                           . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
244
                }
245
            }
246
 
247
            $expansion_after = 0;
248
            for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
249
                if ($i < 1) {
250
                    ++$expansion_after;
251
                    continue;
252
                }
253
 
254
                // check when to print separator
255
                $print_separator_flag = (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages));
256
 
257
                if ($i == $this->_currentPage) {
258
                    $this->range[$i] = true;
259
                    $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
260
                } else {
261
                    $this->range[$i] = false;
262
                    $this->_linkData[$this->_urlVar] = $i;
263
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
264
                }
265
                $links .= $this->_spacesBefore
266
                        . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
267
            }
268
 
269
            if ($this->_expanded && $expansion_after) {
270
                $links .= $this->_separator . $this->_spacesAfter;
271
                for ($i = $this->_currentPage + $this->_delta +1; $expansion_after; $expansion_after--, $i++) {
272
                    $print_separator_flag = ($expansion_after != 1);
273
                    $this->range[$i] = false;
274
                    $this->_linkData[$this->_urlVar] = $i;
275
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
276
                      . $this->_spacesBefore
277
                      . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
278
                }
279
            }
280
 
281
        } else {
282
            //if $this->_totalPages <= (2*Delta+1) show them all
283
            for ($i=1; $i<=$this->_totalPages; $i++) {
284
                if ($i != $this->_currentPage) {
285
                    $this->range[$i] = false;
286
                    $this->_linkData[$this->_urlVar] = $i;
287
                    $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
288
                } else {
289
                    $this->range[$i] = true;
290
                    $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
291
                }
292
                $links .= $this->_spacesBefore
293
                       . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
294
            }
295
        }
296
        return $links;
297
    }
298
 
299
    // }}}
300
}
301
?>