|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Contains the Calendar_Decorator_Wrapper class. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP versions 4 and 5 |
|
9
|
|
|
* |
|
10
|
|
|
* LICENSE: Redistribution and use in source and binary forms, with or without |
|
11
|
|
|
* modification, are permitted provided that the following conditions are met: |
|
12
|
|
|
* 1. Redistributions of source code must retain the above copyright |
|
13
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
14
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in the |
|
16
|
|
|
* documentation and/or other materials provided with the distribution. |
|
17
|
|
|
* 3. The name of the author may not be used to endorse or promote products |
|
18
|
|
|
* derived from this software without specific prior written permission. |
|
19
|
|
|
* |
|
20
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED |
|
21
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
22
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
23
|
|
|
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY |
|
24
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
25
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
26
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
27
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
28
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
29
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30
|
|
|
* |
|
31
|
|
|
* @category Date and Time |
|
32
|
|
|
* |
|
33
|
|
|
* @author Harry Fuecks <[email protected]> |
|
34
|
|
|
* @copyright 2003-2007 Harry Fuecks |
|
35
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
|
36
|
|
|
* |
|
37
|
|
|
* @link http://pear.php.net/package/Calendar |
|
38
|
|
|
*/ |
|
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
|
|
|
* |
|
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
|
|
|
* |
|
50
|
|
|
* @link http://pear.php.net/package/Calendar |
|
51
|
|
|
*/ |
|
52
|
|
|
class Calendar_Table_Helper |
|
53
|
|
|
{ |
|
54
|
|
|
/** |
|
55
|
|
|
* Instance of the Calendar object being helped. |
|
56
|
|
|
* |
|
57
|
|
|
* @var object |
|
58
|
|
|
*/ |
|
59
|
|
|
public $calendar; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Instance of the Calendar_Engine. |
|
63
|
|
|
* |
|
64
|
|
|
* @var object |
|
65
|
|
|
*/ |
|
66
|
|
|
public $cE; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* First day of the week. |
|
70
|
|
|
* |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
public $firstDay; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The seven days of the week named. |
|
77
|
|
|
* |
|
78
|
|
|
* @var array |
|
79
|
|
|
*/ |
|
80
|
|
|
public $weekDays; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Days of the week ordered with $firstDay at the beginning. |
|
84
|
|
|
* |
|
85
|
|
|
* @var array |
|
86
|
|
|
*/ |
|
87
|
|
|
public $daysOfWeek = []; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Days of the month built from days of the week. |
|
91
|
|
|
* |
|
92
|
|
|
* @var array |
|
93
|
|
|
*/ |
|
94
|
|
|
public $daysOfMonth = []; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Number of weeks in month. |
|
98
|
|
|
* |
|
99
|
|
|
* @var int |
|
100
|
|
|
*/ |
|
101
|
|
|
public $numWeeks = null; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Number of emtpy days before real days begin in month. |
|
105
|
|
|
* |
|
106
|
|
|
* @var int |
|
107
|
|
|
*/ |
|
108
|
|
|
public $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
|
|
|
public function __construct($calendar, $firstDay = null) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->calendar = $calendar; |
|
119
|
|
|
$this->cE = $calendar->getEngine(); |
|
120
|
|
|
if (null === $firstDay) { |
|
121
|
|
|
$firstDay = $this->cE->getFirstDayOfWeek($this->calendar->thisYear(), $this->calendar->thisMonth(), $this->calendar->thisDay()); |
|
122
|
|
|
} |
|
123
|
|
|
$this->firstDay = $firstDay; |
|
124
|
|
|
$this->setFirstDay(); |
|
125
|
|
|
$this->setDaysOfMonth(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Constructs $this->daysOfWeek based on $this->firstDay. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setFirstDay() |
|
132
|
|
|
{ |
|
133
|
|
|
$weekDays = $this->cE->getWeekDays($this->calendar->thisYear(), $this->calendar->thisMonth(), $this->calendar->thisDay()); |
|
134
|
|
|
$endDays = []; |
|
135
|
|
|
$tmpDays = []; |
|
136
|
|
|
$begin = false; |
|
137
|
|
|
foreach ($weekDays as $day) { |
|
138
|
|
|
if ($begin) { |
|
139
|
|
|
$endDays[] = $day; |
|
140
|
|
|
} elseif ($day === $this->firstDay) { |
|
141
|
|
|
$begin = true; |
|
142
|
|
|
$endDays[] = $day; |
|
143
|
|
|
} else { |
|
144
|
|
|
$tmpDays[] = $day; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
$this->daysOfWeek = array_merge($endDays, $tmpDays); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Constructs $this->daysOfMonth. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setDaysOfMonth() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->daysOfMonth = $this->daysOfWeek; |
|
156
|
|
|
$daysInMonth = $this->cE->getDaysInMonth($this->calendar->thisYear(), $this->calendar->thisMonth()); |
|
157
|
|
|
$firstDayInMonth = $this->cE->getFirstDayInMonth($this->calendar->thisYear(), $this->calendar->thisMonth()); |
|
158
|
|
|
$this->emptyBefore = 0; |
|
159
|
|
|
foreach ($this->daysOfMonth as $dayOfWeek) { |
|
160
|
|
|
if ($firstDayInMonth == $dayOfWeek) { |
|
161
|
|
|
break; |
|
162
|
|
|
} |
|
163
|
|
|
++$this->emptyBefore; |
|
164
|
|
|
} |
|
165
|
|
|
$this->numWeeks = ceil(($daysInMonth + $this->emptyBefore) / $this->cE->getDaysInWeek($this->calendar->thisYear(), $this->calendar->thisMonth(), $this->calendar->thisDay())); |
|
166
|
|
|
for ($i = 1; $i < $this->numWeeks; ++$i) { |
|
167
|
|
|
$this->daysOfMonth = array_merge($this->daysOfMonth, $this->daysOfWeek); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns the first day of the month. |
|
173
|
|
|
* |
|
174
|
|
|
* @return int |
|
175
|
|
|
* |
|
176
|
|
|
* @see Calendar_Engine_Interface::getFirstDayOfWeek() |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getFirstDay() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->firstDay; |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns the order array of days in a week. |
|
185
|
|
|
* |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getDaysOfWeek() |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->daysOfWeek; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns the number of tabular weeks in a month. |
|
195
|
|
|
* |
|
196
|
|
|
* @return int |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getNumWeeks() |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->numWeeks; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Returns the number of real days + empty days. |
|
205
|
|
|
* |
|
206
|
|
|
* @return int |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getNumTableDaysInMonth() |
|
209
|
|
|
{ |
|
210
|
|
|
return count($this->daysOfMonth); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Returns the number of empty days before the real days begin. |
|
215
|
|
|
* |
|
216
|
|
|
* @return int |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getEmptyDaysBefore() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->emptyBefore; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Returns the index of the last real day in the month. |
|
225
|
|
|
* |
|
226
|
|
|
* @todo Potential performance optimization with static |
|
227
|
|
|
* |
|
228
|
|
|
* @return int |
|
229
|
|
|
*/ |
|
230
|
|
|
public function getEmptyDaysAfter() |
|
231
|
|
|
{ |
|
232
|
|
|
// Causes bug when displaying more than one month |
|
233
|
|
|
//static $index; |
|
234
|
|
|
//if (!isset($index)) { |
|
235
|
|
|
$index = $this->getEmptyDaysBefore() + $this->cE->getDaysInMonth($this->calendar->thisYear(), $this->calendar->thisMonth()); |
|
236
|
|
|
|
|
237
|
|
|
//} |
|
238
|
|
|
return $index; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Returns the index of the last real day in the month, relative to the |
|
243
|
|
|
* beginning of the tabular week it is part of. |
|
244
|
|
|
* |
|
245
|
|
|
* @return int |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getEmptyDaysAfterOffset() |
|
248
|
|
|
{ |
|
249
|
|
|
$eAfter = $this->getEmptyDaysAfter(); |
|
250
|
|
|
|
|
251
|
|
|
return $eAfter - ($this->cE->getDaysInWeek($this->calendar->thisYear(), $this->calendar->thisMonth(), $this->calendar->thisDay()) * ($this->numWeeks - 1)); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Returns the timestamp of the first day of the current week. |
|
256
|
|
|
* |
|
257
|
|
|
* @param int $y year |
|
258
|
|
|
* @param int $m month |
|
259
|
|
|
* @param int $d day |
|
260
|
|
|
* @param int $firstDay first day of the week (default 1 = Monday) |
|
261
|
|
|
* |
|
262
|
|
|
* @return int timestamp |
|
263
|
|
|
*/ |
|
264
|
|
|
public function getWeekStart($y, $m, $d, $firstDay = 1) |
|
265
|
|
|
{ |
|
266
|
|
|
$dow = $this->cE->getDayOfWeek($y, $m, $d); |
|
267
|
|
|
if ($dow > $firstDay) { |
|
268
|
|
|
$d -= ($dow - $firstDay); |
|
269
|
|
|
} |
|
270
|
|
|
if ($dow < $firstDay) { |
|
271
|
|
|
$d -= ($this->cE->getDaysInWeek($this->calendar->thisYear(), $this->calendar->thisMonth(), $this->calendar->thisDay()) - $firstDay + $dow); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
return $this->cE->dateToStamp($y, $m, $d); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|