|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Description: demonstrates a decorator used to "attach a payload" to a selection |
|
4
|
|
|
* to make it available when iterating over calendar children. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
//if you use ISO-8601 dates, switch to PearDate engine |
|
8
|
|
|
define('CALENDAR_ENGINE', 'PearDate'); |
|
9
|
|
|
|
|
10
|
|
|
if (!@include 'Calendar/Calendar.php') { |
|
11
|
|
|
define('CALENDAR_ROOT', '../../'); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
require_once CALENDAR_ROOT . 'Month/Weekdays.php'; |
|
15
|
|
|
require_once CALENDAR_ROOT . 'Day.php'; |
|
16
|
|
|
require_once CALENDAR_ROOT . 'Decorator.php'; |
|
17
|
|
|
|
|
18
|
|
|
// accepts multiple entries |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class DiaryEvent. |
|
22
|
|
|
*/ |
|
23
|
|
|
class DiaryEvent extends Calendar_Decorator |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
public $entries = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param $calendar |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($calendar) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($calendar); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param $entry |
|
37
|
|
|
*/ |
|
38
|
|
|
public function addEntry($entry) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->entries[] = $entry; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getEntry() |
|
47
|
|
|
{ |
|
48
|
|
|
$entry = each($this->entries); |
|
|
|
|
|
|
49
|
|
|
if ($entry) { |
|
|
|
|
|
|
50
|
|
|
return $entry['value']; |
|
51
|
|
|
} else { |
|
52
|
|
|
reset($this->entries); |
|
53
|
|
|
|
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Class MonthPayload_Decorator. |
|
61
|
|
|
*/ |
|
62
|
|
|
class MonthPayload_Decorator extends Calendar_Decorator |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
//Calendar engine |
|
65
|
|
|
public $cE; |
|
66
|
|
|
public $tableHelper; |
|
67
|
|
|
|
|
68
|
|
|
public $year; |
|
69
|
|
|
public $month; |
|
70
|
|
|
public $firstDay = false; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $events |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function build($events = []) |
|
78
|
|
|
{ |
|
79
|
|
|
require_once CALENDAR_ROOT . 'Day.php'; |
|
80
|
|
|
require_once CALENDAR_ROOT . 'Table/Helper.php'; |
|
81
|
|
|
|
|
82
|
|
|
$this->tableHelper = new Calendar_Table_Helper($this, $this->firstDay); |
|
|
|
|
|
|
83
|
|
|
$this->cE = $this->getEngine(); |
|
84
|
|
|
$this->year = $this->thisYear(); |
|
85
|
|
|
$this->month = $this->thisMonth(); |
|
86
|
|
|
|
|
87
|
|
|
$daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month); |
|
88
|
|
|
for ($i = 1; $i <= $daysInMonth; ++$i) { |
|
89
|
|
|
$Day = new Calendar_Day(2000, 1, 1); // Create Day with dummy values |
|
90
|
|
|
$Day->setTimestamp($this->cE->dateToStamp($this->year, $this->month, $i)); |
|
91
|
|
|
$this->children[$i] = new DiaryEvent($Day); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
if (count($events) > 0) { |
|
94
|
|
|
$this->setSelection($events); |
|
95
|
|
|
} |
|
96
|
|
|
Calendar_Month_Weekdays::buildEmptyDaysBefore(); |
|
|
|
|
|
|
97
|
|
|
Calendar_Month_Weekdays::shiftDays(); |
|
|
|
|
|
|
98
|
|
|
Calendar_Month_Weekdays::buildEmptyDaysAfter(); |
|
|
|
|
|
|
99
|
|
|
Calendar_Month_Weekdays::setWeekMarkers(); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param $events |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setSelection($events) |
|
108
|
|
|
{ |
|
109
|
|
|
$daysInMonth = $this->cE->getDaysInMonth($this->year, $this->month); |
|
110
|
|
|
for ($i = 1; $i <= $daysInMonth; ++$i) { |
|
111
|
|
|
$stamp1 = $this->cE->dateToStamp($this->year, $this->month, $i); |
|
112
|
|
|
$stamp2 = $this->cE->dateToStamp($this->year, $this->month, $i + 1); |
|
113
|
|
|
foreach ($events as $event) { |
|
114
|
|
|
if (($stamp1 >= $event['start'] && $stamp1 < $event['end']) |
|
115
|
|
|
|| ($stamp2 >= $event['start'] |
|
116
|
|
|
&& $stamp2 < $event['end']) |
|
117
|
|
|
|| ($stamp1 <= $event['start'] && $stamp2 > $event['end'])) { |
|
118
|
|
|
$this->children[$i]->addEntry($event); |
|
119
|
|
|
$this->children[$i]->setSelected(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function fetch() |
|
129
|
|
|
{ |
|
130
|
|
|
$child = each($this->children); |
|
|
|
|
|
|
131
|
|
|
if ($child) { |
|
|
|
|
|
|
132
|
|
|
return $child['value']; |
|
133
|
|
|
} else { |
|
134
|
|
|
reset($this->children); |
|
135
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// Calendar instance used to get the dates in the preferred format: |
|
142
|
|
|
// you can switch Calendar Engine and the example still works |
|
143
|
|
|
$cal = new Calendar(); |
|
144
|
|
|
|
|
145
|
|
|
$events = []; |
|
146
|
|
|
//add some events |
|
147
|
|
|
$events[] = [ |
|
148
|
|
|
'start' => $cal->cE->dateToStamp(2004, 6, 1, 10), |
|
149
|
|
|
'end' => $cal->cE->dateToStamp(2004, 6, 1, 12), |
|
150
|
|
|
'desc' => 'Important meeting', |
|
151
|
|
|
]; |
|
152
|
|
|
$events[] = [ |
|
153
|
|
|
'start' => $cal->cE->dateToStamp(2004, 6, 1, 21), |
|
154
|
|
|
'end' => $cal->cE->dateToStamp(2004, 6, 1, 23, 59), |
|
155
|
|
|
'desc' => 'Dinner with the boss', |
|
156
|
|
|
]; |
|
157
|
|
|
$events[] = [ |
|
158
|
|
|
'start' => $cal->cE->dateToStamp(2004, 6, 5), |
|
159
|
|
|
'end' => $cal->cE->dateToStamp(2004, 6, 10, 23, 59), |
|
160
|
|
|
'desc' => 'Holidays!', |
|
161
|
|
|
]; |
|
162
|
|
|
|
|
163
|
|
|
$Month = new Calendar_Month_Weekdays(2004, 6); |
|
164
|
|
|
$MonthDecorator = new MonthPayload_Decorator($Month); |
|
165
|
|
|
$MonthDecorator->build($events); |
|
166
|
|
|
|
|
167
|
|
|
?> |
|
168
|
|
|
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
169
|
|
|
<html> |
|
170
|
|
|
<head> |
|
171
|
|
|
<title> Calendar </title> |
|
172
|
|
|
<style text="text/css"> |
|
173
|
|
|
table { |
|
174
|
|
|
border-collapse: collapse; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
caption { |
|
178
|
|
|
font-family: verdana, sans-serif; |
|
179
|
|
|
font-size: 14pt; |
|
180
|
|
|
padding-bottom: 4pt; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
th { |
|
184
|
|
|
font-family: verdana, sans-serif; |
|
185
|
|
|
font-size: 11px; |
|
186
|
|
|
text-align: center; |
|
187
|
|
|
background-color: #e7e3e7; |
|
188
|
|
|
padding: 5pt; |
|
189
|
|
|
line-height: 150%; |
|
190
|
|
|
border: 1px solid #ccc; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
td { |
|
194
|
|
|
font-family: verdana, sans-serif; |
|
195
|
|
|
font-size: 11px; |
|
196
|
|
|
text-align: left; |
|
197
|
|
|
vertical-align: top; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
td.calCell { |
|
201
|
|
|
border: 1px solid #b5bece; |
|
202
|
|
|
padding: 3px; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
td.calCellEmpty { |
|
206
|
|
|
background-color: #f3f3f7; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
td.calCellBusy { |
|
210
|
|
|
background-color: #efeffa; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
div.dayNumber { |
|
214
|
|
|
text-align: right; |
|
215
|
|
|
background-color: #f8f8f8; |
|
216
|
|
|
border-bottom: 1px solid #ccc; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
ul { |
|
220
|
|
|
margin-left: 0; |
|
221
|
|
|
margin-top: 5pt; |
|
222
|
|
|
padding: 0 10pt 0 12pt; |
|
223
|
|
|
list-style-type: square; |
|
224
|
|
|
} |
|
225
|
|
|
</style> |
|
226
|
|
|
</head> |
|
227
|
|
|
|
|
228
|
|
|
<body> |
|
229
|
|
|
|
|
230
|
|
|
<h2>Sample Calendar Payload Decorator (using <?php echo CALENDAR_ENGINE; ?> engine)</h2> |
|
231
|
|
|
<table class="calendar" width="98%" cellspacing="0" cellpadding="0"> |
|
232
|
|
|
<caption> |
|
233
|
|
|
<?php echo $MonthDecorator->thisMonth() . ' / ' . $MonthDecorator->thisYear(); ?> |
|
234
|
|
|
</caption> |
|
235
|
|
|
<tr> |
|
236
|
|
|
<th>Monday</th> |
|
237
|
|
|
<th>Tuesday</th> |
|
238
|
|
|
<th>Wednesday</th> |
|
239
|
|
|
<th>Thursday</th> |
|
240
|
|
|
<th>Friday</th> |
|
241
|
|
|
<th>Saturday</th> |
|
242
|
|
|
<th>Sunday</th> |
|
243
|
|
|
</tr> |
|
244
|
|
|
<?php |
|
245
|
|
|
while ($Day = $MonthDecorator->fetch()) { |
|
246
|
|
|
if ($Day->isFirst()) { |
|
247
|
|
|
echo "<tr>\n"; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
echo '<td class="calCell'; |
|
251
|
|
|
if ($Day->isSelected()) { |
|
252
|
|
|
echo ' calCellBusy'; |
|
253
|
|
|
} elseif ($Day->isEmpty()) { |
|
254
|
|
|
echo ' calCellEmpty'; |
|
255
|
|
|
} |
|
256
|
|
|
echo '">'; |
|
257
|
|
|
echo '<div class="dayNumber">' . $Day->thisDay() . '</div>'; |
|
258
|
|
|
|
|
259
|
|
|
if ($Day->isEmpty()) { |
|
260
|
|
|
echo ' '; |
|
261
|
|
|
} else { |
|
262
|
|
|
echo '<div class="dayContents"><ul>'; |
|
263
|
|
|
while ($entry = $Day->getEntry()) { |
|
264
|
|
|
echo '<li>' . $entry['desc'] . '</li>'; |
|
265
|
|
|
//you can print the time range as well |
|
266
|
|
|
} |
|
267
|
|
|
echo '</ul></div>'; |
|
268
|
|
|
} |
|
269
|
|
|
echo '</td>'; |
|
270
|
|
|
|
|
271
|
|
|
if ($Day->isLast()) { |
|
272
|
|
|
echo "</tr>\n"; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
?> |
|
276
|
|
|
</table> |
|
277
|
|
|
</body> |
|
278
|
|
|
</html> |
|
279
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.