Subversion Repositories Applications.papyrus

Rev

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

Rev Author Line No. Line
1712 jp_milcent 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 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
 * Two constants used to guess the path- and file-name of the page
41
 * when the user doesn't set any other value
42
 */
43
class Pager_HtmlWidgets
44
{
45
    var $pager = null;
46
 
47
    // {{{ constructor
48
 
49
    function Pager_HtmlWidgets(&$pager)
50
    {
51
        $this->pager =& $pager;
52
    }
53
 
54
    // }}}
55
    // {{{ getPerPageSelectBox()
56
 
57
    /**
58
     * Returns a string with a XHTML SELECT menu,
59
     * useful for letting the user choose how many items per page should be
60
     * displayed. If parameter useSessions is TRUE, this value is stored in
61
     * a session var. The string isn't echoed right now so you can use it
62
     * with template engines.
63
     *
64
     * @param integer $start
65
     * @param integer $end
66
     * @param integer $step
67
     * @param boolean $showAllData If true, perPage is set equal to totalItems.
68
     * @param array   (or string $optionText for BC reasons)
69
     *                - 'optionText': text to show in each option.
70
     *                  Use '%d' where you want to see the number of pages selected.
71
     *                - 'attributes': (html attributes) Tag attributes or
72
     *                  HTML attributes (id="foo" pairs), will be inserted in the
73
     *                  <select> tag
74
     *                - 'checkMaxLimit': if true, Pager checks if $end is bigger
75
     *                  than $totalItems, and doesn't show the extra select options
76
     * @return string xhtml select box
77
     * @access public
78
     */
79
    function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
80
    {
81
        // FIXME: needs POST support
82
        $optionText = '%d';
83
        $attributes = '';
84
        $checkMaxLimit = false;
85
        if (is_string($extraParams)) {
86
            //old behavior, BC maintained
87
            $optionText = $extraParams;
88
        } else {
89
            if (array_key_exists('optionText', $extraParams)) {
90
                $optionText = $extraParams['optionText'];
91
            }
92
            if (array_key_exists('attributes', $extraParams)) {
93
                $attributes = $extraParams['attributes'];
94
            }
95
            if (array_key_exists('checkMaxLimit', $extraParams)) {
96
                $checkMaxLimit = $extraParams['checkMaxLimit'];
97
            }
98
        }
99
 
100
        if (!strstr($optionText, '%d')) {
101
            return $this->pager->raiseError(
102
                $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
103
                ERROR_PAGER_INVALID_PLACEHOLDER
104
            );
105
        }
106
        $start = (int)$start;
107
        $end   = (int)$end;
108
        $step  = (int)$step;
109
        if (!empty($_SESSION[$this->pager->_sessionVar])) {
110
            $selected = (int)$_SESSION[$this->pager->_sessionVar];
111
        } else {
112
            $selected = $this->pager->_perPage;
113
        }
114
 
115
        if ($checkMaxLimit && $this->pager->_totalItems > 0 && $this->pager->_totalItems < $end) {
116
            $end = $this->pager->_totalItems;
117
        }
118
 
119
        $tmp = '<select name="'.$this->pager->_sessionVar.'"';
120
        if (!empty($attributes)) {
121
            $tmp .= ' '.$attributes;
122
        }
123
        $tmp .= '>';
124
        $last = $start;
125
        for ($i=$start; $i<=$end; $i+=$step) {
126
            $last = $i;
127
            $tmp .= '<option value="'.$i.'"';
128
            if ($i == $selected) {
129
                $tmp .= ' selected="selected"';
130
            }
131
            $tmp .= '>'.sprintf($optionText, $i).'</option>';
132
        }
133
        if ($showAllData && $last != $this->pager->_totalItems) {
134
            $tmp .= '<option value="'.$this->pager->_totalItems.'"';
135
            if ($this->pager->_totalItems == $selected) {
136
                $tmp .= ' selected="selected"';
137
            }
138
            $tmp .= '>';
139
            if (empty($this->pager->_showAllText)) {
140
                $tmp .= str_replace('%d', $this->pager->_totalItems, $optionText);
141
            } else {
142
                $tmp .= $this->pager->_showAllText;
143
            }
144
            $tmp .= '</option>';
145
        }
146
        $tmp .= '</select>';
147
        return $tmp;
148
    }
149
 
150
    // }}}
151
    // {{{ getPageSelectBox()
152
 
153
    /**
154
     * Returns a string with a XHTML SELECT menu with the page numbers,
155
     * useful as an alternative to the links
156
     *
157
     * @param array   - 'optionText': text to show in each option.
158
     *                  Use '%d' where you want to see the number of pages selected.
159
     *                - 'autoSubmit': if TRUE, add some js code to submit the
160
     *                  form on the onChange event
161
     * @param string    $extraAttributes (html attributes) Tag attributes or
162
     *                  HTML attributes (id="foo" pairs), will be inserted in the
163
     *                  <select> tag
164
     * @return string xhtml select box
165
     * @access public
166
     */
167
    function getPageSelectBox($params = array(), $extraAttributes = '')
168
    {
169
        $optionText = '%d';
170
        if (array_key_exists('optionText', $params)) {
171
            $optionText = $params['optionText'];
172
        }
173
 
174
        if (!strstr($optionText, '%d')) {
175
            return $this->pager->raiseError(
176
                $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
177
                ERROR_PAGER_INVALID_PLACEHOLDER
178
            );
179
        }
180
 
181
        $tmp = '<select name="'.$this->pager->_urlVar.'"';
182
        if (!empty($extraAttributes)) {
183
            $tmp .= ' '.$extraAttributes;
184
        }
185
        if (!empty($params['autoSubmit'])) {
186
            if ($this->pager->_httpMethod == 'GET') {
187
                $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
188
                if ($this->pager->_append) {
189
                    $href = '?' . $this->pager->_http_build_query_wrapper($this->pager->_linkData);
190
                    $href = htmlentities($this->pager->_url). preg_replace(
191
                        '/(&|&amp;|\?)('.$this->pager->_urlVar.'=)(\d+)/',
192
                        '\\1\\2'.$selector,
193
                        htmlentities($href)
194
                    );
195
                } else {
196
                    $href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName));
197
                }
198
                $tmp .= ' onchange="document.location.href=\''
199
                     . $href .'\''
200
                     . '"';
201
            } elseif ($this->pager->_httpMethod == 'POST') {
202
                $tmp .= " onchange='"
203
                     . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
204
                     . "'";
205
                $tmp = preg_replace(
206
                    '/(input\.name = \"'.$this->pager->_urlVar.'\"; input\.value =) \"(\d+)\";/',
207
                    '\\1 this.options[this.selectedIndex].value;',
208
                    $tmp
209
                );
210
            }
211
        }
212
        $tmp .= '>';
213
        $start = 1;
214
        $end = $this->pager->numPages();
215
        $selected = $this->pager->getCurrentPageID();
216
        for ($i=$start; $i<=$end; $i++) {
217
            $tmp .= '<option value="'.$i.'"';
218
            if ($i == $selected) {
219
                $tmp .= ' selected="selected"';
220
            }
221
            $tmp .= '>'.sprintf($optionText, $i).'</option>';
222
        }
223
        $tmp .= '</select>';
224
        return $tmp;
225
    }
226
 
227
    // }}}
228
}
229
?>