Subversion Repositories eFlore/Applications.coel-consultation

Rev

Rev 36 | Details | Compare with Previous | Last modification | View Log | RSS feed

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