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