1 | <?php |
||
2 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |
||
3 | |||
4 | /** |
||
5 | * Contains the Calendar_Decorator_Wrapper 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 <[email protected]> |
||
33 | * @copyright 2003-2007 Harry Fuecks |
||
34 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
||
35 | * @version CVS: $Id$ |
||
36 | * @link http://pear.php.net/package/Calendar |
||
37 | */ |
||
38 | namespace PEAR\Calendar\Table; |
||
39 | |||
40 | /** |
||
41 | * Used by Calendar_Month_Weekdays, Calendar_Month_Weeks and Calendar_Week to |
||
42 | * help with building the calendar in tabular form |
||
43 | * |
||
44 | * @category Date and Time |
||
45 | * @package Calendar |
||
46 | * @author Harry Fuecks <[email protected]> |
||
47 | * @copyright 2003-2007 Harry Fuecks |
||
48 | * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
||
49 | * @link http://pear.php.net/package/Calendar |
||
50 | * @access public |
||
51 | */ |
||
52 | class Helper |
||
53 | { |
||
54 | /** |
||
55 | * Instance of the Calendar object being helped. |
||
56 | * @var object |
||
57 | * @access private |
||
58 | */ |
||
59 | var $calendar; |
||
60 | |||
61 | /** |
||
62 | * Instance of the Calendar_Engine |
||
63 | * @var object |
||
64 | * @access private |
||
65 | */ |
||
66 | var $cE; |
||
67 | |||
68 | /** |
||
69 | * First day of the week |
||
70 | * @access private |
||
71 | * @var string |
||
72 | */ |
||
73 | var $firstDay; |
||
74 | |||
75 | /** |
||
76 | * The seven days of the week named |
||
77 | * @access private |
||
78 | * @var array |
||
79 | */ |
||
80 | var $weekDays; |
||
81 | |||
82 | /** |
||
83 | * Days of the week ordered with $firstDay at the beginning |
||
84 | * @access private |
||
85 | * @var array |
||
86 | */ |
||
87 | var $daysOfWeek = array(); |
||
88 | |||
89 | /** |
||
90 | * Days of the month built from days of the week |
||
91 | * @access private |
||
92 | * @var array |
||
93 | */ |
||
94 | var $daysOfMonth = array(); |
||
95 | |||
96 | /** |
||
97 | * Number of weeks in month |
||
98 | * @var int |
||
99 | * @access private |
||
100 | */ |
||
101 | var $numWeeks = null; |
||
102 | |||
103 | /** |
||
104 | * Number of emtpy days before real days begin in month |
||
105 | * @var int |
||
106 | * @access private |
||
107 | */ |
||
108 | var $emptyBefore = 0; |
||
109 | |||
110 | /** |
||
111 | * Constructs Calendar_Table_Helper |
||
112 | * |
||
113 | * @param object &$calendar Calendar_Month_Weekdays, Calendar_Month_Weeks, Calendar_Week |
||
114 | * @param int $firstDay (optional) first day of the week e.g. 1 for Monday |
||
115 | * |
||
116 | * @access protected |
||
117 | */ |
||
118 | function __construct(& $calendar, $firstDay=null) |
||
0 ignored issues
–
show
|
|||
119 | { |
||
120 | $this->calendar = & $calendar; |
||
121 | $this->cE = & $calendar->getEngine(); |
||
122 | if (is_null($firstDay)) { |
||
123 | $firstDay = $this->cE->getFirstDayOfWeek( |
||
124 | $this->calendar->thisYear(), |
||
125 | $this->calendar->thisMonth(), |
||
126 | $this->calendar->thisDay() |
||
127 | ); |
||
128 | } |
||
129 | $this->firstDay = $firstDay; |
||
130 | $this->setFirstDay(); |
||
131 | $this->setDaysOfMonth(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Constructs $this->daysOfWeek based on $this->firstDay |
||
136 | * |
||
137 | * @return void |
||
138 | * @access private |
||
139 | */ |
||
140 | function setFirstDay() |
||
0 ignored issues
–
show
|
|||
141 | { |
||
142 | $weekDays = $this->cE->getWeekDays( |
||
143 | $this->calendar->thisYear(), |
||
144 | $this->calendar->thisMonth(), |
||
145 | $this->calendar->thisDay() |
||
146 | ); |
||
147 | $endDays = array(); |
||
148 | $tmpDays = array(); |
||
149 | $begin = false; |
||
150 | foreach ($weekDays as $day) { |
||
151 | if ($begin) { |
||
152 | $endDays[] = $day; |
||
153 | } else if ($day === $this->firstDay) { |
||
154 | $begin = true; |
||
155 | $endDays[] = $day; |
||
156 | } else { |
||
157 | $tmpDays[] = $day; |
||
158 | } |
||
159 | } |
||
160 | $this->daysOfWeek = array_merge($endDays, $tmpDays); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Constructs $this->daysOfMonth |
||
165 | * |
||
166 | * @return void |
||
167 | * @access private |
||
168 | */ |
||
169 | function setDaysOfMonth() |
||
0 ignored issues
–
show
|
|||
170 | { |
||
171 | $this->daysOfMonth = $this->daysOfWeek; |
||
172 | $daysInMonth = $this->cE->getDaysInMonth( |
||
173 | $this->calendar->thisYear(), $this->calendar->thisMonth()); |
||
174 | $firstDayInMonth = $this->cE->getFirstDayInMonth( |
||
175 | $this->calendar->thisYear(), $this->calendar->thisMonth()); |
||
176 | $this->emptyBefore=0; |
||
177 | foreach ($this->daysOfMonth as $dayOfWeek) { |
||
178 | if ($firstDayInMonth == $dayOfWeek) { |
||
179 | break; |
||
180 | } |
||
181 | $this->emptyBefore++; |
||
182 | } |
||
183 | $this->numWeeks = ceil( |
||
184 | ($daysInMonth + $this->emptyBefore) |
||
185 | / |
||
186 | $this->cE->getDaysInWeek( |
||
187 | $this->calendar->thisYear(), |
||
188 | $this->calendar->thisMonth(), |
||
189 | $this->calendar->thisDay() |
||
190 | ) |
||
191 | ); |
||
192 | for ($i=1; $i < $this->numWeeks; $i++) { |
||
193 | $this->daysOfMonth = |
||
194 | array_merge($this->daysOfMonth, $this->daysOfWeek); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Returns the first day of the month |
||
200 | * |
||
201 | * @return int |
||
202 | * @access protected |
||
203 | * @see CalendarEngineInterface::getFirstDayOfWeek() |
||
204 | */ |
||
205 | function getFirstDay() |
||
0 ignored issues
–
show
|
|||
206 | { |
||
207 | return $this->firstDay; |
||
0 ignored issues
–
show
|
|||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Returns the order array of days in a week |
||
212 | * |
||
213 | * @return int |
||
214 | * @access protected |
||
215 | */ |
||
216 | function getDaysOfWeek() |
||
0 ignored issues
–
show
|
|||
217 | { |
||
218 | return $this->daysOfWeek; |
||
0 ignored issues
–
show
|
|||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Returns the number of tabular weeks in a month |
||
223 | * |
||
224 | * @return int |
||
225 | * @access protected |
||
226 | */ |
||
227 | function getNumWeeks() |
||
0 ignored issues
–
show
|
|||
228 | { |
||
229 | return $this->numWeeks; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Returns the number of real days + empty days |
||
234 | * |
||
235 | * @return int |
||
236 | * @access protected |
||
237 | */ |
||
238 | function getNumTableDaysInMonth() |
||
0 ignored issues
–
show
|
|||
239 | { |
||
240 | return count($this->daysOfMonth); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Returns the number of empty days before the real days begin |
||
245 | * |
||
246 | * @return int |
||
247 | * @access protected |
||
248 | */ |
||
249 | function getEmptyDaysBefore() |
||
0 ignored issues
–
show
|
|||
250 | { |
||
251 | return $this->emptyBefore; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Returns the index of the last real day in the month |
||
256 | * |
||
257 | * @todo Potential performance optimization with static |
||
258 | * @return int |
||
259 | * @access protected |
||
260 | */ |
||
261 | function getEmptyDaysAfter() |
||
0 ignored issues
–
show
|
|||
262 | { |
||
263 | // Causes bug when displaying more than one month |
||
264 | //static $index; |
||
265 | //if (!isset($index)) { |
||
266 | $index = $this->getEmptyDaysBefore() + $this->cE->getDaysInMonth( |
||
267 | $this->calendar->thisYear(), $this->calendar->thisMonth()); |
||
268 | //} |
||
269 | return $index; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Returns the index of the last real day in the month, relative to the |
||
274 | * beginning of the tabular week it is part of |
||
275 | * |
||
276 | * @return int |
||
277 | * @access protected |
||
278 | */ |
||
279 | function getEmptyDaysAfterOffset() |
||
0 ignored issues
–
show
|
|||
280 | { |
||
281 | $eAfter = $this->getEmptyDaysAfter(); |
||
282 | return $eAfter - ( |
||
283 | $this->cE->getDaysInWeek( |
||
284 | $this->calendar->thisYear(), |
||
285 | $this->calendar->thisMonth(), |
||
286 | $this->calendar->thisDay() |
||
287 | ) * ($this->numWeeks-1)); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Returns the timestamp of the first day of the current week |
||
292 | * |
||
293 | * @param int $y year |
||
294 | * @param int $m month |
||
295 | * @param int $d day |
||
296 | * @param int $firstDay first day of the week (default 1 = Monday) |
||
297 | * |
||
298 | * @return int timestamp |
||
299 | */ |
||
300 | function getWeekStart($y, $m, $d, $firstDay=1) |
||
0 ignored issues
–
show
|
|||
301 | { |
||
302 | $dow = $this->cE->getDayOfWeek($y, $m, $d); |
||
303 | if ($dow > $firstDay) { |
||
304 | $d -= ($dow - $firstDay); |
||
305 | } |
||
306 | if ($dow < $firstDay) { |
||
307 | $d -= ( |
||
308 | $this->cE->getDaysInWeek( |
||
309 | $this->calendar->thisYear(), |
||
310 | $this->calendar->thisMonth(), |
||
311 | $this->calendar->thisDay() |
||
312 | ) - $firstDay + $dow); |
||
313 | } |
||
314 | return $this->cE->dateToStamp($y, $m, $d); |
||
315 | } |
||
316 | } |
||
317 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.