1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Description: demonstrates a decorator to provide simple output formatting |
5
|
|
|
* on the month while still allowing the days to be accessed via the decorator |
6
|
|
|
* In practice you _wouldn't_ do this - each decorator comes with a performance |
7
|
|
|
* hit for extra method calls. For this example some simple functions could help |
8
|
|
|
* format the month while the days are accessed via the normal Month object. |
9
|
|
|
*/ |
10
|
|
|
if (!@require_once __DIR__ . '/Calendar/Calendar.php') { |
11
|
|
|
define('CALENDAR_ROOT', '../../'); |
12
|
|
|
} |
13
|
|
|
require_once CALENDAR_ROOT . 'Month/Weekdays.php'; |
14
|
|
|
require_once CALENDAR_ROOT . 'Decorator.php'; |
15
|
|
|
|
16
|
|
|
// Decorate a Month with methods to improve formatting |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class MonthDecorator. |
20
|
|
|
*/ |
21
|
|
|
class MonthDecorator extends Calendar_Decorator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param object $Month |
25
|
|
|
* |
26
|
|
|
* @internal param $Calendar_Month |
27
|
|
|
*/ |
28
|
|
|
public function __construct(&$Month) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($Month); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Override the prevMonth method to format the output. |
35
|
|
|
*/ |
36
|
|
|
public function prevMonth() |
37
|
|
|
{ |
38
|
|
|
$prevStamp = parent::prevMonth(true); |
|
|
|
|
39
|
|
|
|
40
|
|
|
// Build the URL for the previous month |
41
|
|
|
return $_SERVER['SCRIPT_NAME'] . '?y=' . date('Y', $prevStamp) . '&m=' . date('n', $prevStamp) . '&d=' . date('j', $prevStamp); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Override the thisMonth method to format the output. |
46
|
|
|
*/ |
47
|
|
|
public function thisMonth() |
48
|
|
|
{ |
49
|
|
|
$thisStamp = parent::thisMonth(true); |
|
|
|
|
50
|
|
|
|
51
|
|
|
// A human readable string from this month |
52
|
|
|
return date('F Y', $thisStamp); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Override the nextMonth method to format the output. |
57
|
|
|
*/ |
58
|
|
|
public function nextMonth() |
59
|
|
|
{ |
60
|
|
|
$nextStamp = parent::nextMonth(true); |
|
|
|
|
61
|
|
|
|
62
|
|
|
// Build the URL for next month |
63
|
|
|
return $_SERVER['SCRIPT_NAME'] . '?y=' . date('Y', $nextStamp) . '&m=' . date('n', $nextStamp) . '&d=' . date('j', $nextStamp); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!isset($_GET['y'])) { |
68
|
|
|
$_GET['y'] = date('Y'); |
69
|
|
|
} |
70
|
|
|
if (!isset($_GET['m'])) { |
71
|
|
|
$_GET['m'] = date('n'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Creata a month as usual |
75
|
|
|
$Month = new Calendar_Month_Weekdays($_GET['y'], $_GET['m']); |
76
|
|
|
|
77
|
|
|
// Pass it to the decorator and use the decorator from now on... |
78
|
|
|
$MonthDecorator = new MonthDecorator($Month); |
79
|
|
|
$MonthDecorator->build(); |
80
|
|
|
?> |
81
|
|
|
|
82
|
|
|
<!DOCTYPE html> |
83
|
|
|
<html> |
84
|
|
|
<head> |
85
|
|
|
<title> A Simple Decorator </title> |
86
|
|
|
</head> |
87
|
|
|
<body> |
88
|
|
|
<h1>A Simple Decorator</h1> |
89
|
|
|
<table> |
90
|
|
|
<caption><?php echo $MonthDecorator->thisMonth(); ?></caption> |
91
|
|
|
<?php |
92
|
|
|
while (false !== ($Day = $MonthDecorator->fetch())) { |
93
|
|
|
if ($Day->isFirst()) { |
94
|
|
|
echo "\n<tr>\n"; |
95
|
|
|
} |
96
|
|
|
if ($Day->isEmpty()) { |
97
|
|
|
echo '<td> </td>'; |
98
|
|
|
} else { |
99
|
|
|
echo '<td>' . $Day->thisDay() . '</td>'; |
100
|
|
|
} |
101
|
|
|
if ($Day->isLast()) { |
102
|
|
|
echo "\n</tr>\n"; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
?> |
106
|
|
|
<tr> |
107
|
|
|
<td><a href="<?php echo $MonthDecorator->prevMonth(); ?>">Prev</a></td> |
108
|
|
|
<td colspan="5"> </td> |
109
|
|
|
<td><a href="<?php echo $MonthDecorator->nextMonth(); ?>">Next</a></td> |
110
|
|
|
</tr> |
111
|
|
|
</table> |
112
|
|
|
</body> |
113
|
|
|
</html> |
114
|
|
|
|