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_HtmlWidgets 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-2007 Lorenzo Alberton
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: HtmlWidgets.php,v 1.7 2009/03/13 16:51:37 quipo Exp $
36
 * @link      http://pear.php.net/package/Pager
37
 */
38
 
39
/**
40
 * Pager_HtmlWidgets
41
 *
42
 * @category  HTML
43
 * @package   Pager
44
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
45
 * @copyright 2003-2007 Lorenzo Alberton
46
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
47
 * @link      http://pear.php.net/package/Pager
48
 */
49
class Pager_HtmlWidgets
50
{
51
    var $pager = null;
52
 
53
    // {{{ constructor
54
 
55
    /**
56
     * Constructor
57
     *
58
     * @param object &$pager Pager instance
59
     */
60
    function Pager_HtmlWidgets(&$pager)
61
    {
62
        $this->pager =& $pager;
63
    }
64
 
65
    // }}}
66
    // {{{ getPerPageSelectBox()
67
 
68
    /**
69
     * Returns a string with a XHTML SELECT menu,
70
     * useful for letting the user choose how many items per page should be
71
     * displayed. If parameter useSessions is TRUE, this value is stored in
72
     * a session var. The string isn't echoed right now so you can use it
73
     * with template engines.
74
     *
75
     * @param integer $start       starting value for the select menu
76
     * @param integer $end         ending value for the select menu
77
     * @param integer $step        step between values in the select menu
78
     * @param boolean $showAllData If true, perPage is set equal to totalItems.
79
     * @param array   $extraParams (or string $optionText for BC reasons)
80
     *                - 'optionText': text to show in each option.
81
     *                  Use '%d' where you want to see the number of pages selected.
82
     *                - 'attributes': (html attributes) Tag attributes or
83
     *                  HTML attributes (id="foo" pairs), will be inserted in the
84
     *                  <select> tag
85
     *                - 'checkMaxLimit': if true, Pager checks if $end is bigger
86
     *                  than $totalItems, and doesn't show the extra select options
87
     *                - 'autoSubmit': if TRUE, add some js code
88
     *                  to submit the form on the onChange event
89
     *
90
     * @return string xhtml select box
91
     * @access public
92
     */
93
    function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
94
    {
95
        // FIXME: needs POST support
96
        $optionText = '%d';
97
        $attributes = '';
98
        $checkMaxLimit = false;
99
        if (is_string($extraParams)) {
100
            //old behavior, BC maintained
101
            $optionText = $extraParams;
102
        } else {
103
            if (array_key_exists('optionText', $extraParams)) {
104
                $optionText = $extraParams['optionText'];
105
            }
106
            if (array_key_exists('attributes', $extraParams)) {
107
                $attributes = $extraParams['attributes'];
108
            }
109
            if (array_key_exists('checkMaxLimit', $extraParams)) {
110
                $checkMaxLimit = $extraParams['checkMaxLimit'];
111
            }
112
        }
113
 
114
        if (!strstr($optionText, '%d')) {
115
            return $this->pager->raiseError(
116
                $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
117
                ERROR_PAGER_INVALID_PLACEHOLDER
118
            );
119
        }
120
        $start = (int)$start;
121
        $end   = (int)$end;
122
        $step  = (int)$step;
123
        if (!empty($_SESSION[$this->pager->_sessionVar])) {
124
            $selected = (int)$_SESSION[$this->pager->_sessionVar];
125
        } else {
126
            $selected = $this->pager->_perPage;
127
        }
128
 
129
        if ($checkMaxLimit && $this->pager->_totalItems >= 0 && $this->pager->_totalItems < $end) {
130
            $end = $this->pager->_totalItems;
131
        }
132
 
133
        $tmp = '<select name="'.$this->pager->_sessionVar.'"';
134
        if (!empty($attributes)) {
135
            $tmp .= ' '.$attributes;
136
        }
137
        if (!empty($extraParams['autoSubmit'])) {
138
            if ('GET' == $this->pager->_httpMethod) {
139
                $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
140
                if ($this->pager->_append) {
141
                    $tmpLinkData = $this->pager->_linkData;
142
                    if (isset($tmpLinkData[$this->pager->_urlVar])) {
143
                        $tmpLinkData[$this->pager->_urlVar] = $this->pager->getCurrentPageID();
144
                    }
145
                    $tmpLinkData[$this->pager->_sessionVar] = '1';
146
                    $href = '?' . $this->pager->_http_build_query_wrapper($tmpLinkData);
147
                    $href = htmlentities($this->pager->_url, ENT_COMPAT, 'UTF-8'). preg_replace(
148
                        '/(&|&amp;|\?)('.$this->pager->_sessionVar.'=)(\d+)/',
149
                        '\\1\\2'.$selector,
150
                        htmlentities($href, ENT_COMPAT, 'UTF-8')
151
                    );
152
                } else {
153
                    $href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName), ENT_COMPAT, 'UTF-8');
154
                }
155
                $tmp .= ' onchange="document.location.href=\''
156
                     . $href .'\''
157
                     . '"';
158
            } elseif ($this->pager->_httpMethod == 'POST') {
159
                $tmp .= " onchange='"
160
                     . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
161
                     . "'";
162
                $tmp = preg_replace(
163
                    '/(input\.name = \"'.$this->pager->_sessionVar.'\"; input\.value =) \"(\d+)\";/',
164
                    '\\1 this.options[this.selectedIndex].value;',
165
                    $tmp
166
                );
167
            }
168
        }
169
 
170
 
171
        $tmp .= '>';
172
        $last = $start;
173
        for ($i=$start; $i<=$end; $i+=$step) {
174
            $last = $i;
175
            $tmp .= '<option value="'.$i.'"';
176
            if ($i == $selected) {
177
                $tmp .= ' selected="selected"';
178
            }
179
            $tmp .= '>'.sprintf($optionText, $i).'</option>';
180
        }
181
        if ($showAllData && $last != $this->pager->_totalItems) {
182
            $tmp .= '<option value="'.$this->pager->_totalItems.'"';
183
            if ($this->pager->_totalItems == $selected) {
184
                $tmp .= ' selected="selected"';
185
            }
186
            $tmp .= '>';
187
            if (empty($this->pager->_showAllText)) {
188
                $tmp .= str_replace('%d', $this->pager->_totalItems, $optionText);
189
            } else {
190
                $tmp .= $this->pager->_showAllText;
191
            }
192
            $tmp .= '</option>';
193
        }
194
        if (substr($tmp, -9, 9) !== '</option>') {
195
            //empty select
196
            $tmp .= '<option />';
197
        }
198
        $tmp .= '</select>';
199
        return $tmp;
200
    }
201
 
202
    // }}}
203
    // {{{ getPageSelectBox()
204
 
205
    /**
206
     * Returns a string with a XHTML SELECT menu with the page numbers,
207
     * useful as an alternative to the links
208
     *
209
     * @param array  $params          - 'optionText': text to show in each option.
210
     *                                  Use '%d' where you want to see the number
211
     *                                  of pages selected.
212
     *                                - 'autoSubmit': if TRUE, add some js code
213
     *                                  to submit the form on the onChange event
214
     * @param string $extraAttributes (html attributes) Tag attributes or
215
     *                                HTML attributes (id="foo" pairs), will be
216
     *                                inserted in the <select> tag
217
     *
218
     * @return string xhtml select box
219
     * @access public
220
     */
221
    function getPageSelectBox($params = array(), $extraAttributes = '')
222
    {
223
        $optionText = '%d';
224
        if (array_key_exists('optionText', $params)) {
225
            $optionText = $params['optionText'];
226
        }
227
 
228
        if (!strstr($optionText, '%d')) {
229
            return $this->pager->raiseError(
230
                $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
231
                ERROR_PAGER_INVALID_PLACEHOLDER
232
            );
233
        }
234
 
235
        $tmp = '<select name="'.$this->pager->_urlVar.'"';
236
        if (!empty($extraAttributes)) {
237
            $tmp .= ' '.$extraAttributes;
238
        }
239
        if (!empty($params['autoSubmit'])) {
240
            if ($this->pager->_httpMethod == 'GET') {
241
                $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
242
                if ($this->pager->_append) {
243
                    $href = '?' . $this->pager->_http_build_query_wrapper($this->pager->_linkData);
244
                    $href = htmlentities($this->pager->_url, ENT_COMPAT, 'UTF-8'). preg_replace(
245
                        '/(&|&amp;|\?)('.$this->pager->_urlVar.'=)(\d+)/',
246
                        '\\1\\2'.$selector,
247
                        htmlentities($href, ENT_COMPAT, 'UTF-8')
248
                    );
249
                } else {
250
                    $href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName), ENT_COMPAT, 'UTF-8');
251
                }
252
                $tmp .= ' onchange="document.location.href=\''
253
                     . $href .'\''
254
                     . '"';
255
            } elseif ($this->pager->_httpMethod == 'POST') {
256
                $tmp .= " onchange='"
257
                     . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
258
                     . "'";
259
                $tmp = preg_replace(
260
                    '/(input\.name = \"'.$this->pager->_urlVar.'\"; input\.value =) \"(\d+)\";/',
261
                    '\\1 this.options[this.selectedIndex].value;',
262
                    $tmp
263
                );
264
            }
265
        }
266
        $tmp .= '>';
267
        $start = 1;
268
        $end   = $this->pager->numPages();
269
        $selected = $this->pager->getCurrentPageID();
270
        for ($i=$start; $i<=$end; $i++) {
271
            $tmp .= '<option value="'.$i.'"';
272
            if ($i == $selected) {
273
                $tmp .= ' selected="selected"';
274
            }
275
            $tmp .= '>'.sprintf($optionText, $i).'</option>';
276
        }
277
        $tmp .= '</select>';
278
        return $tmp;
279
    }
280
 
281
    // }}}
282
}
283
?>