1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Contains the Calendar_Week 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
|
|
|
* @author Lorenzo Alberton <[email protected]> |
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$ |
37
|
|
|
* @link http://pear.php.net/package/Calendar |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Allows Calendar include path to be redefined |
42
|
|
|
* @ignore |
43
|
|
|
*/ |
44
|
|
|
if (!defined('CALENDAR_ROOT')) { |
45
|
|
|
define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Load Calendar base class |
50
|
|
|
*/ |
51
|
|
|
require_once CALENDAR_ROOT.'Calendar.php'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Represents a Week and builds Days in tabular format<br> |
55
|
|
|
* <code> |
56
|
|
|
* require_once 'Calendar/Week.php'; |
57
|
|
|
* $Week = new Calendar_Week(2003, 10, 1); Oct 2003, 1st tabular week |
58
|
|
|
* echo '<tr>'; |
59
|
|
|
* while ($Day = & $Week->fetch()) { |
60
|
|
|
* if ($Day->isEmpty()) { |
61
|
|
|
* echo '<td> </td>'; |
62
|
|
|
* } else { |
63
|
|
|
* echo '<td>'.$Day->thisDay().'</td>'; |
64
|
|
|
* } |
65
|
|
|
* } |
66
|
|
|
* echo '</tr>'; |
67
|
|
|
* </code> |
68
|
|
|
* |
69
|
|
|
* @category Date and Time |
70
|
|
|
* @package Calendar |
71
|
|
|
* @author Harry Fuecks <[email protected]> |
72
|
|
|
* @author Lorenzo Alberton <[email protected]> |
73
|
|
|
* @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
74
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
75
|
|
|
* @link http://pear.php.net/package/Calendar |
76
|
|
|
*/ |
77
|
|
|
class Calendar_Week extends Calendar |
78
|
|
|
{ |
79
|
|
|
/** |
80
|
|
|
* Instance of Calendar_Table_Helper |
81
|
|
|
* @var Calendar_Table_Helper |
82
|
|
|
* @access private |
83
|
|
|
*/ |
84
|
|
|
var $tableHelper; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Stores the timestamp of the first day of this week |
88
|
|
|
* @access private |
89
|
|
|
* @var object |
90
|
|
|
*/ |
91
|
|
|
var $thisWeek; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Stores the timestamp of first day of previous week |
95
|
|
|
* @access private |
96
|
|
|
* @var object |
97
|
|
|
*/ |
98
|
|
|
var $prevWeek; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Stores the timestamp of first day of next week |
102
|
|
|
* @access private |
103
|
|
|
* @var object |
104
|
|
|
*/ |
105
|
|
|
var $nextWeek; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Used by build() to set empty days |
109
|
|
|
* @access private |
110
|
|
|
* @var boolean |
111
|
|
|
*/ |
112
|
|
|
var $firstWeek = false; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Used by build() to set empty days |
116
|
|
|
* @access private |
117
|
|
|
* @var boolean |
118
|
|
|
*/ |
119
|
|
|
var $lastWeek = false; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* First day of the week (0=sunday, 1=monday...) |
123
|
|
|
* @access private |
124
|
|
|
* @var boolean |
125
|
|
|
*/ |
126
|
|
|
var $firstDay = 1; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Constructs Week |
130
|
|
|
* |
131
|
|
|
* @param int $y year e.g. 2003 |
132
|
|
|
* @param int $m month e.g. 5 |
133
|
|
|
* @param int $d a day of the desired week |
134
|
|
|
* @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.) |
135
|
|
|
* |
136
|
|
|
* @access public |
137
|
|
|
*/ |
138
|
|
|
function Calendar_Week($y, $m, $d, $firstDay = null) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
include_once CALENDAR_ROOT.'Table/Helper.php'; |
141
|
|
|
parent::Calendar($y, $m, $d); |
142
|
|
|
$this->firstDay = $this->defineFirstDayOfWeek($firstDay); |
|
|
|
|
143
|
|
|
$this->tableHelper = new Calendar_Table_Helper($this, $this->firstDay); |
144
|
|
|
$this->thisWeek = $this->tableHelper->getWeekStart($y, $m, $d, $this->firstDay); |
|
|
|
|
145
|
|
|
$this->prevWeek = $this->tableHelper->getWeekStart( |
|
|
|
|
146
|
|
|
$y, |
147
|
|
|
$m, |
148
|
|
|
$d - $this->cE->getDaysInWeek( |
149
|
|
|
$this->thisYear(), |
150
|
|
|
$this->thisMonth(), |
151
|
|
|
$this->thisDay() |
152
|
|
|
), |
153
|
|
|
$this->firstDay |
154
|
|
|
); |
155
|
|
|
$this->nextWeek = $this->tableHelper->getWeekStart( |
|
|
|
|
156
|
|
|
$y, |
157
|
|
|
$m, |
158
|
|
|
$d + $this->cE->getDaysInWeek( |
159
|
|
|
$this->thisYear(), |
160
|
|
|
$this->thisMonth(), |
161
|
|
|
$this->thisDay() |
162
|
|
|
), |
163
|
|
|
$this->firstDay |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Defines the calendar by a timestamp (Unix or ISO-8601), replacing values |
169
|
|
|
* passed to the constructor |
170
|
|
|
* |
171
|
|
|
* @param int|string $ts Unix or ISO-8601 timestamp |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
* @access public |
175
|
|
|
*/ |
176
|
|
|
function setTimestamp($ts) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
parent::setTimestamp($ts); |
179
|
|
|
$this->thisWeek = $this->tableHelper->getWeekStart( |
|
|
|
|
180
|
|
|
$this->year, $this->month, $this->day, $this->firstDay |
|
|
|
|
181
|
|
|
); |
182
|
|
|
$this->prevWeek = $this->tableHelper->getWeekStart( |
|
|
|
|
183
|
|
|
$this->year, |
184
|
|
|
$this->month, |
185
|
|
|
$this->day - $this->cE->getDaysInWeek( |
186
|
|
|
$this->thisYear(), |
187
|
|
|
$this->thisMonth(), |
188
|
|
|
$this->thisDay() |
189
|
|
|
), |
190
|
|
|
$this->firstDay |
191
|
|
|
); |
192
|
|
|
$this->nextWeek = $this->tableHelper->getWeekStart( |
|
|
|
|
193
|
|
|
$this->year, |
194
|
|
|
$this->month, |
195
|
|
|
$this->day + $this->cE->getDaysInWeek( |
196
|
|
|
$this->thisYear(), |
197
|
|
|
$this->thisMonth(), |
198
|
|
|
$this->thisDay() |
199
|
|
|
), |
200
|
|
|
$this->firstDay |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Builds Calendar_Day objects for this Week |
206
|
|
|
* |
207
|
|
|
* @param array $sDates (optional) Calendar_Day objects representing selected dates |
208
|
|
|
* |
209
|
|
|
* @return boolean |
210
|
|
|
* @access public |
211
|
|
|
*/ |
212
|
|
|
function build($sDates = array()) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
include_once CALENDAR_ROOT.'Day.php'; |
215
|
|
|
$year = $this->cE->stampToYear($this->thisWeek); |
216
|
|
|
$month = $this->cE->stampToMonth($this->thisWeek); |
217
|
|
|
$day = $this->cE->stampToDay($this->thisWeek); |
218
|
|
|
$end = $this->cE->getDaysInWeek( |
219
|
|
|
$this->thisYear(), |
220
|
|
|
$this->thisMonth(), |
221
|
|
|
$this->thisDay() |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
for ($i=1; $i <= $end; $i++) { |
225
|
|
|
$stamp = $this->cE->dateToStamp($year, $month, $day++); |
226
|
|
|
$this->children[$i] = new Calendar_Day( |
227
|
|
|
$this->cE->stampToYear($stamp), |
228
|
|
|
$this->cE->stampToMonth($stamp), |
229
|
|
|
$this->cE->stampToDay($stamp) |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
//set empty days (@see Calendar_Month_Weeks::build()) |
234
|
|
|
if ($this->firstWeek) { |
235
|
|
|
$eBefore = $this->tableHelper->getEmptyDaysBefore(); |
236
|
|
|
for ($i=1; $i <= $eBefore; $i++) { |
237
|
|
|
$this->children[$i]->setEmpty(); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
if ($this->lastWeek) { |
241
|
|
|
$eAfter = $this->tableHelper->getEmptyDaysAfterOffset(); |
242
|
|
|
for ($i = $eAfter+1; $i <= $end; $i++) { |
243
|
|
|
$this->children[$i]->setEmpty(); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if (count($sDates) > 0) { |
248
|
|
|
$this->setSelection($sDates); |
249
|
|
|
} |
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Set as first week of the month |
255
|
|
|
* |
256
|
|
|
* @param boolean $state whether it's first or not |
257
|
|
|
* |
258
|
|
|
* @return void |
259
|
|
|
* @access private |
260
|
|
|
*/ |
261
|
|
|
function setFirst($state = true) |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
$this->firstWeek = $state; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Set as last week of the month |
268
|
|
|
* |
269
|
|
|
* @param boolean $state whether it's lasst or not |
270
|
|
|
* |
271
|
|
|
* @return void |
272
|
|
|
* @access private |
273
|
|
|
*/ |
274
|
|
|
function setLast($state = true) |
|
|
|
|
275
|
|
|
{ |
276
|
|
|
$this->lastWeek = $state; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Called from build() |
281
|
|
|
* |
282
|
|
|
* @param array $sDates Calendar_Day objects representing selected dates |
283
|
|
|
* |
284
|
|
|
* @return void |
285
|
|
|
* @access private |
286
|
|
|
*/ |
287
|
|
|
function setSelection($sDates) |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
foreach ($sDates as $sDate) { |
290
|
|
|
foreach ($this->children as $key => $child) { |
291
|
|
|
if ($child->thisDay() == $sDate->thisDay() && |
292
|
|
|
$child->thisMonth() == $sDate->thisMonth() && |
293
|
|
|
$child->thisYear() == $sDate->thisYear() |
294
|
|
|
) { |
295
|
|
|
$this->children[$key] = $sDate; |
296
|
|
|
$this->children[$key]->setSelected(); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
reset($this->children); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Returns the value for this year |
305
|
|
|
* |
306
|
|
|
* When a on the first/last week of the year, the year of the week is |
307
|
|
|
* calculated according to ISO-8601 |
308
|
|
|
* |
309
|
|
|
* @param string $format return value format ['int' | 'timestamp' | 'object' | 'array'] |
310
|
|
|
* |
311
|
|
|
* @return int e.g. 2003 or timestamp |
312
|
|
|
* @access public |
313
|
|
|
*/ |
314
|
|
|
function thisYear($format = 'int') |
|
|
|
|
315
|
|
|
{ |
316
|
|
|
if (null !== $this->thisWeek) { |
317
|
|
|
$tmp_cal = new Calendar(); |
318
|
|
|
$tmp_cal->setTimestamp($this->thisWeek); |
|
|
|
|
319
|
|
|
$first_dow = $tmp_cal->thisDay('array'); |
320
|
|
|
$days_in_week = $tmp_cal->cE->getDaysInWeek($tmp_cal->year, $tmp_cal->month, $tmp_cal->day); |
321
|
|
|
$tmp_cal->day += $days_in_week; |
322
|
|
|
$last_dow = $tmp_cal->thisDay('array'); |
323
|
|
|
|
324
|
|
|
if ($first_dow['year'] == $last_dow['year']) { |
325
|
|
|
return $first_dow['year']; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
if ($last_dow['day'] > floor($days_in_week / 2)) { |
329
|
|
|
return $last_dow['year']; |
330
|
|
|
} |
331
|
|
|
return $first_dow['year']; |
332
|
|
|
} |
333
|
|
|
return parent::thisYear(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Gets the value of the previous week, according to the requested format |
338
|
|
|
* |
339
|
|
|
* @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
340
|
|
|
* |
341
|
|
|
* @return mixed |
342
|
|
|
* @access public |
343
|
|
|
*/ |
344
|
|
|
function prevWeek($format = 'n_in_month') |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
switch (strtolower($format)) { |
347
|
|
|
case 'int': |
348
|
|
|
case 'n_in_month': |
349
|
|
|
return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1; |
350
|
|
|
case 'n_in_year': |
351
|
|
|
return $this->cE->getWeekNInYear( |
352
|
|
|
$this->cE->stampToYear($this->prevWeek), |
353
|
|
|
$this->cE->stampToMonth($this->prevWeek), |
354
|
|
|
$this->cE->stampToDay($this->prevWeek)); |
355
|
|
|
case 'array': |
356
|
|
|
return $this->toArray($this->prevWeek); |
357
|
|
|
case 'object': |
358
|
|
|
include_once CALENDAR_ROOT.'Factory.php'; |
359
|
|
|
return Calendar_Factory::createByTimestamp('Week', $this->prevWeek); |
|
|
|
|
360
|
|
|
case 'timestamp': |
361
|
|
|
default: |
362
|
|
|
return $this->prevWeek; |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Gets the value of the current week, according to the requested format |
368
|
|
|
* |
369
|
|
|
* @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
370
|
|
|
* |
371
|
|
|
* @return mixed |
372
|
|
|
* @access public |
373
|
|
|
*/ |
374
|
|
|
function thisWeek($format = 'n_in_month') |
|
|
|
|
375
|
|
|
{ |
376
|
|
|
switch (strtolower($format)) { |
377
|
|
|
case 'int': |
378
|
|
|
case 'n_in_month': |
379
|
|
|
if ($this->firstWeek) { |
380
|
|
|
return 1; |
381
|
|
|
} |
382
|
|
|
if ($this->lastWeek) { |
383
|
|
|
return $this->cE->getWeeksInMonth( |
384
|
|
|
$this->thisYear(), |
385
|
|
|
$this->thisMonth(), |
386
|
|
|
$this->firstDay); |
387
|
|
|
} |
388
|
|
|
return $this->cE->getWeekNInMonth( |
389
|
|
|
$this->thisYear(), |
390
|
|
|
$this->thisMonth(), |
391
|
|
|
$this->thisDay(), |
392
|
|
|
$this->firstDay); |
393
|
|
|
case 'n_in_year': |
394
|
|
|
return $this->cE->getWeekNInYear( |
395
|
|
|
$this->cE->stampToYear($this->thisWeek), |
396
|
|
|
$this->cE->stampToMonth($this->thisWeek), |
397
|
|
|
$this->cE->stampToDay($this->thisWeek)); |
398
|
|
|
case 'array': |
399
|
|
|
return $this->toArray($this->thisWeek); |
400
|
|
|
case 'object': |
401
|
|
|
include_once CALENDAR_ROOT.'Factory.php'; |
402
|
|
|
return Calendar_Factory::createByTimestamp('Week', $this->thisWeek); |
|
|
|
|
403
|
|
|
case 'timestamp': |
404
|
|
|
default: |
405
|
|
|
return $this->thisWeek; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Gets the value of the following week, according to the requested format |
411
|
|
|
* |
412
|
|
|
* @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
413
|
|
|
* |
414
|
|
|
* @return mixed |
415
|
|
|
* @access public |
416
|
|
|
*/ |
417
|
|
|
function nextWeek($format = 'n_in_month') |
|
|
|
|
418
|
|
|
{ |
419
|
|
|
switch (strtolower($format)) { |
420
|
|
|
case 'int': |
421
|
|
|
case 'n_in_month': |
422
|
|
|
return ($this->lastWeek) ? null : $this->thisWeek('n_in_month') +1; |
423
|
|
|
case 'n_in_year': |
424
|
|
|
return $this->cE->getWeekNInYear( |
425
|
|
|
$this->cE->stampToYear($this->nextWeek), |
426
|
|
|
$this->cE->stampToMonth($this->nextWeek), |
427
|
|
|
$this->cE->stampToDay($this->nextWeek)); |
428
|
|
|
case 'array': |
429
|
|
|
return $this->toArray($this->nextWeek); |
430
|
|
|
case 'object': |
431
|
|
|
include_once CALENDAR_ROOT.'Factory.php'; |
432
|
|
|
return Calendar_Factory::createByTimestamp('Week', $this->nextWeek); |
|
|
|
|
433
|
|
|
case 'timestamp': |
434
|
|
|
default: |
435
|
|
|
return $this->nextWeek; |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Returns the instance of Calendar_Table_Helper. |
441
|
|
|
* Called from Calendar_Validator::isValidWeek |
442
|
|
|
* |
443
|
|
|
* @return Calendar_Table_Helper |
444
|
|
|
* @access protected |
445
|
|
|
*/ |
446
|
|
|
function & getHelper() |
447
|
|
|
{ |
448
|
|
|
return $this->tableHelper; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Makes sure theres a value for $this->day |
453
|
|
|
* |
454
|
|
|
* @return void |
455
|
|
|
* @access private |
456
|
|
|
*/ |
457
|
|
|
function findFirstDay() |
|
|
|
|
458
|
|
|
{ |
459
|
|
|
if (!count($this->children) > 0) { |
460
|
|
|
$this->build(); |
461
|
|
|
foreach ($this->children as $Day) { |
462
|
|
|
if (!$Day->isEmpty()) { |
463
|
|
|
$this->day = $Day->thisDay(); |
464
|
|
|
break; |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
?> |
|
|
|
|
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.