Subversion Repositories Applications.papyrus

Rev

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_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
     * @return string xhtml select box
75
     * @access public
76
     */
77
    function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
78
    {
79
        // FIXME: needs POST support
80
        $optionText = '%d';
81
        $attributes = '';
82
        if (is_string($extraParams)) {
83
            //old behavior, BC maintained
84
            $optionText = $extraParams;
85
        } else {
86
            if (array_key_exists('optionText', $extraParams)) {
87
                $optionText = $extraParams['optionText'];
88
            }
89
            if (array_key_exists('attributes', $extraParams)) {
90
                $attributes = $extraParams['attributes'];
91
            }
92
        }
93
 
94
        if (!strstr($optionText, '%d')) {
95
            return $this->pager->raiseError(
96
                $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
97
                ERROR_PAGER_INVALID_PLACEHOLDER
98
            );
99
        }
100
        $start = (int)$start;
101
        $end   = (int)$end;
102
        $step  = (int)$step;
103
        if (!empty($_SESSION[$this->pager->_sessionVar])) {
104
            $selected = (int)$_SESSION[$this->pager->_sessionVar];
105
        } else {
106
            $selected = $this->pager->_perPage;
107
        }
108
 
109
        $tmp = '<select name="'.$this->pager->_sessionVar.'"';
110
        if (!empty($attributes)) {
111
            $tmp .= ' '.$attributes;
112
        }
113
        $tmp .= '>';
114
        for ($i=$start; $i<=$end; $i+=$step) {
115
            $tmp .= '<option value="'.$i.'"';
116
            if ($i == $selected) {
117
                $tmp .= ' selected="selected"';
118
            }
119
            $tmp .= '>'.sprintf($optionText, $i).'</option>';
120
        }
121
        if ($showAllData && $end < $this->pager->_totalItems) {
122
            $tmp .= '<option value="'.$this->pager->_totalItems.'"';
123
            if ($this->pager->_totalItems == $selected) {
124
                $tmp .= ' selected="selected"';
125
            }
126
            $tmp .= '>';
127
            if (empty($this->pager->_showAllText)) {
128
                $tmp .= str_replace('%d', $this->pager->_totalItems, $optionText);
129
            } else {
130
                $tmp .= $this->pager->_showAllText;
131
            }
132
            $tmp .= '</option>';
133
        }
134
        $tmp .= '</select>';
135
        return $tmp;
136
    }
137
 
138
    // }}}
139
    // {{{ getPageSelectBox()
140
 
141
    /**
142
     * Returns a string with a XHTML SELECT menu with the page numbers,
143
     * useful as an alternative to the links
144
     *
145
     * @param array   - 'optionText': text to show in each option.
146
     *                  Use '%d' where you want to see the number of pages selected.
147
     *                - 'autoSubmit': if TRUE, add some js code to submit the
148
     *                  form on the onChange event
149
     * @param string    $extraAttributes (html attributes) Tag attributes or
150
     *                  HTML attributes (id="foo" pairs), will be inserted in the
151
     *                  <select> tag
152
     * @return string xhtml select box
153
     * @access public
154
     */
155
    function getPageSelectBox($params = array(), $extraAttributes = '')
156
    {
157
        $optionText = '%d';
158
        if (array_key_exists('optionText', $params)) {
159
            $optionText = $params['optionText'];
160
        }
161
 
162
        if (!strstr($optionText, '%d')) {
163
            return $this->pager->raiseError(
164
                $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
165
                ERROR_PAGER_INVALID_PLACEHOLDER
166
            );
167
        }
168
 
169
        $tmp = '<select name="'.$this->pager->_urlVar.'"';
170
        if (!empty($extraAttributes)) {
171
            $tmp .= ' '.$extraAttributes;
172
        }
173
        if (!empty($params['autoSubmit'])) {
174
            if ($this->pager->_httpMethod == 'GET') {
175
                $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
176
                if ($this->pager->_append) {
177
                    $href = '?' . $this->pager->_http_build_query_wrapper($this->pager->_linkData);
178
                    $href = htmlentities($this->pager->_url). preg_replace(
179
                        '/(&|&amp;|\?)('.$this->pager->_urlVar.'=)(\d+)/',
180
                        '\\1\\2'.$selector,
181
                        htmlentities($href)
182
                    );
183
                } else {
184
                    $href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName));
185
                }
186
                $tmp .= ' onchange="document.location.href=\''
187
                     . $href .'\''
188
                     . '"';
189
            } elseif ($this->pager->_httpMethod == 'POST') {
190
                $tmp .= " onchange='"
191
                     . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
192
                     . "'";
193
                $tmp = preg_replace(
194
                    '/(input\.name = \"'.$this->pager->_urlVar.'\"; input\.value =) \"(\d+)\";/',
195
                    '\\1 this.options[this.selectedIndex].value;',
196
                    $tmp
197
                );
198
            }
199
        }
200
        $tmp .= '>';
201
        $start = 1;
202
        $end = $this->pager->numPages();
203
        $selected = $this->pager->getCurrentPageID();
204
        for ($i=$start; $i<=$end; $i++) {
205
            $tmp .= '<option value="'.$i.'"';
206
            if ($i == $selected) {
207
                $tmp .= ' selected="selected"';
208
            }
209
            $tmp .= '>'.sprintf($optionText, $i).'</option>';
210
        }
211
        $tmp .= '</select>';
212
        return $tmp;
213
    }
214
 
215
    // }}}
216
}
217
?>