1 | <?php |
---|
2 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |
---|
3 | |
---|
4 | /** |
---|
5 | * Contains the Calendar_Util_Uri 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 Date and Time |
---|
31 | * @package Calendar |
---|
32 | * @author Harry Fuecks <hfuecks@phppatterns.com> |
---|
33 | * @author Lorenzo Alberton <l.alberton@quipo.it> |
---|
34 | * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
---|
35 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
---|
36 | * @version CVS: $Id: Uri.php 300729 2010-06-24 12:05:53Z quipo $ |
---|
37 | * @link http://pear.php.net/package/Calendar |
---|
38 | */ |
---|
39 | |
---|
40 | /** |
---|
41 | * Utility to help building HTML links for navigating the calendar<br /> |
---|
42 | * <code> |
---|
43 | * $Day = new Calendar_Day(2003, 10, 23); |
---|
44 | * $Uri = new Calendar_Util_Uri('year', 'month', 'day'); |
---|
45 | * echo $Uri->prev($Day,'month'); // Displays year=2003&month=10 |
---|
46 | * echo $Uri->prev($Day,'day'); // Displays year=2003&month=10&day=22 |
---|
47 | * $Uri->seperator = '/'; |
---|
48 | * $Uri->scalar = true; |
---|
49 | * echo $Uri->prev($Day,'month'); // Displays 2003/10 |
---|
50 | * echo $Uri->prev($Day,'day'); // Displays 2003/10/22 |
---|
51 | * </code> |
---|
52 | * |
---|
53 | * @category Date and Time |
---|
54 | * @package Calendar |
---|
55 | * @author Harry Fuecks <hfuecks@phppatterns.com> |
---|
56 | * @author Lorenzo Alberton <l.alberton@quipo.it> |
---|
57 | * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
---|
58 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
---|
59 | * @link http://pear.php.net/package/Calendar |
---|
60 | * @access public |
---|
61 | */ |
---|
62 | class Calendar_Util_Uri |
---|
63 | { |
---|
64 | /** |
---|
65 | * Uri fragments for year, month, day etc. |
---|
66 | * @var array |
---|
67 | * @access private |
---|
68 | */ |
---|
69 | var $uris = array(); |
---|
70 | |
---|
71 | /** |
---|
72 | * String to separate fragments with. |
---|
73 | * Set to just & for HTML. |
---|
74 | * For a scalar URL you might use / as the seperator |
---|
75 | * @var string (default XHTML &) |
---|
76 | * @access public |
---|
77 | */ |
---|
78 | var $separator = '&'; |
---|
79 | |
---|
80 | /** |
---|
81 | * To output a "scalar" string - variable names omitted. |
---|
82 | * Used for urls like index.php/2004/8/12 |
---|
83 | * @var boolean (default false) |
---|
84 | * @access public |
---|
85 | */ |
---|
86 | var $scalar = false; |
---|
87 | |
---|
88 | /** |
---|
89 | * Constructs Calendar_Decorator_Uri |
---|
90 | * The term "fragment" means <i>name</i> of a calendar GET variables in the URL |
---|
91 | * |
---|
92 | * @param string $y URI fragment for year |
---|
93 | * @param string $m (optional) URI fragment for month |
---|
94 | * @param string $d (optional) URI fragment for day |
---|
95 | * @param string $h (optional) URI fragment for hour |
---|
96 | * @param string $i (optional) URI fragment for minute |
---|
97 | * @param string $s (optional) URI fragment for second |
---|
98 | * |
---|
99 | * @access public |
---|
100 | */ |
---|
101 | function Calendar_Util_Uri($y, $m=null, $d=null, $h=null, $i=null, $s=null) |
---|
102 | { |
---|
103 | $this->setFragments($y, $m, $d, $h, $i, $s); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Sets the URI fragment names |
---|
108 | * |
---|
109 | * @param string $y URI fragment for year |
---|
110 | * @param string $m (optional) URI fragment for month |
---|
111 | * @param string $d (optional) URI fragment for day |
---|
112 | * @param string $h (optional) URI fragment for hour |
---|
113 | * @param string $i (optional) URI fragment for minute |
---|
114 | * @param string $s (optional) URI fragment for second |
---|
115 | * |
---|
116 | * @return void |
---|
117 | * @access public |
---|
118 | */ |
---|
119 | function setFragments($y, $m=null, $d=null, $h=null, $i=null, $s=null) |
---|
120 | { |
---|
121 | if (!is_null($y)) $this->uris['Year'] = $y; |
---|
122 | if (!is_null($m)) $this->uris['Month'] = $m; |
---|
123 | if (!is_null($d)) $this->uris['Day'] = $d; |
---|
124 | if (!is_null($h)) $this->uris['Hour'] = $h; |
---|
125 | if (!is_null($i)) $this->uris['Minute'] = $i; |
---|
126 | if (!is_null($s)) $this->uris['Second'] = $s; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Gets the URI string for the previous calendar unit |
---|
131 | * |
---|
132 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
---|
133 | * @param string $unit calendar unit (year|month|week|day|hour|minute|second) |
---|
134 | * |
---|
135 | * @return string |
---|
136 | * @access public |
---|
137 | */ |
---|
138 | function prev($Calendar, $unit) |
---|
139 | { |
---|
140 | $method = 'prev'.$unit; |
---|
141 | $stamp = $Calendar->{$method}('timestamp'); |
---|
142 | return $this->buildUriString($Calendar, $method, $stamp); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Gets the URI string for the current calendar unit |
---|
147 | * |
---|
148 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
---|
149 | * @param string $unit calendar unit (year|month|week|day|hour|minute|second) |
---|
150 | * |
---|
151 | * @return string |
---|
152 | * @access public |
---|
153 | */ |
---|
154 | function this($Calendar, $unit) |
---|
155 | { |
---|
156 | $method = 'this'.$unit; |
---|
157 | $stamp = $Calendar->{$method}('timestamp'); |
---|
158 | return $this->buildUriString($Calendar, $method, $stamp); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Gets the URI string for the next calendar unit |
---|
163 | * |
---|
164 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
---|
165 | * @param string $unit calendar unit (year|month|week|day|hour|minute|second) |
---|
166 | * |
---|
167 | * @return string |
---|
168 | * @access public |
---|
169 | */ |
---|
170 | function next($Calendar, $unit) |
---|
171 | { |
---|
172 | $method = 'next'.$unit; |
---|
173 | $stamp = $Calendar->{$method}('timestamp'); |
---|
174 | return $this->buildUriString($Calendar, $method, $stamp); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Build the URI string |
---|
179 | * |
---|
180 | * @param object $Calendar subclassed from Calendar e.g. Calendar_Month |
---|
181 | * @param string $method method substring |
---|
182 | * @param int $stamp timestamp |
---|
183 | * |
---|
184 | * @return string build uri string |
---|
185 | * @access private |
---|
186 | */ |
---|
187 | function buildUriString($Calendar, $method, $stamp) |
---|
188 | { |
---|
189 | $uriString = ''; |
---|
190 | $cE = & $Calendar->getEngine(); |
---|
191 | $separator = ''; |
---|
192 | foreach ($this->uris as $unit => $uri) { |
---|
193 | $call = 'stampTo'.$unit; |
---|
194 | $uriString .= $separator; |
---|
195 | if (!$this->scalar) { |
---|
196 | $uriString .= $uri.'='; |
---|
197 | } |
---|
198 | $uriString .= $cE->{$call}($stamp); |
---|
199 | $separator = $this->separator; |
---|
200 | } |
---|
201 | return $uriString; |
---|
202 | } |
---|
203 | } |
---|
204 | ?> |
---|