1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class ManyMonthsCalendar extends ViewableData |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
//settings |
9
|
|
|
private static $day_names = array( 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); |
10
|
|
|
private static $month_names = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); |
11
|
|
|
private static $previous_month_nav_text = '«'; |
12
|
|
|
private static $next_month_nav_text = '»'; |
13
|
|
|
private static $start_day_of_the_week = 1; |
14
|
|
|
private static $show_year_in_calendar = true; |
15
|
|
|
private static $enable_navigation = true; |
16
|
|
|
private static $calendar_day_format = "j"; // see http://nz.php.net/manual/en/function.date.php for formatting options |
17
|
|
|
private static $table_cell_spacing = 0; // see http://nz.php.net/manual/en/function.date.php for formatting options |
18
|
|
|
|
19
|
|
|
//variables to be added at runtime |
20
|
|
|
private $events = array(); |
21
|
|
|
private $calendarName = "MMC"; |
22
|
|
|
private $pageLink = "pleasesetlink"; |
23
|
|
|
|
24
|
|
|
//internal variables |
25
|
|
|
private static $count_of_calenders_shown = 0; |
26
|
|
|
private $month = 1; |
27
|
|
|
private $year = 1974; |
28
|
|
|
private $timeStamp = 0; |
29
|
|
|
private $daysInMonth = 25; //set to unrealistic number to make sure that it actually gets set correctly |
30
|
|
|
|
31
|
|
View Code Duplication |
public static function setDayNames($dayArray) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
if (!is_array($dayArray) || count($dayArray) != 7) { |
34
|
|
|
debug::show($dayArray); |
35
|
|
|
user_error('There should be seven days in the array passed tot he setDayNames function in the ManyMonthsCalendar Class', E_USER_ERROR); |
36
|
|
|
} |
37
|
|
|
self::$day_names = $dayArray; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public static function setMonthNames($monthArray) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if (!is_array($monthArray) || count($monthArray) != 12) { |
43
|
|
|
debug::show($monthArray); |
44
|
|
|
user_error('There should be twelve months in the array passed tot he setMonthNames function in the ManyMonthsCalendar Class', E_USER_ERROR); |
45
|
|
|
} |
46
|
|
|
self::$month_names = $monthArray; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function setPreviousMonthNavText($text) |
50
|
|
|
{ |
51
|
|
|
self::$previous_month_nav_text = $text; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function setNextMonthNavText($text) |
55
|
|
|
{ |
56
|
|
|
self::$next_month_nav_text = $text; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function setStartDayOfTheWeek($numericDay) |
60
|
|
|
{ |
61
|
|
|
if (is_int($numericDay) && $numericDay >= 0 && $numericDay <=6) { |
62
|
|
|
self::$start_day_of_the_week = $numericDay; |
63
|
|
|
for ($i = 0 ; $i < $numericDay ; $i++) { |
64
|
|
|
array_unshift(self::$day_names, array_pop(self::$day_names)); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
debug::show($numericDay); |
68
|
|
|
user_error('Start Day of the Week should be between 0 and 6, 0=monday, 1=sunday, 2=saturday etc...)', E_USER_ERROR); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function setShowYearInCalendar($booleanValue) |
73
|
|
|
{ |
74
|
|
|
self::$show_year_in_calendar = $booleanValue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function setEnableNavigation($booleanValue) |
78
|
|
|
{ |
79
|
|
|
self::$enable_navigation = $booleanValue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function setCalendarDayFormat($format) |
83
|
|
|
{ |
84
|
|
|
self::$calendar_day_format = $format; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function setTableCellSpacing($pixels) |
88
|
|
|
{ |
89
|
|
|
if (is_int($pixels) && $pixels >= 0 && $pixels <=500) { |
90
|
|
|
self::$table_cell_spacing = $pixels; |
91
|
|
|
} else { |
92
|
|
|
debug::show($pixels); |
93
|
|
|
user_error('Please set the right number of pixels for table spacing (e.g. 0, 1, 2 or 3)', E_USER_ERROR); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function __construct($pageLink, $name, $year = 0, $month = 0) |
98
|
|
|
{ |
99
|
|
|
$this->pageLink = $pageLink; |
100
|
|
|
// Assign name to calendar |
101
|
|
|
if (strpos($name, ' ') || strpos($name, '_') || is_numeric(substr($name, 0, 1))) { |
102
|
|
|
debug::show($name); |
103
|
|
|
user_error('Calendar should have a valid CSS name in the ManyMonthsCalendar Class', E_USER_ERROR); |
104
|
|
|
} |
105
|
|
|
$this->calendarName = $name; |
106
|
|
|
|
107
|
|
|
// Set day, month and year of calendar |
108
|
|
|
$this->month = (0 == $month) ? date('n') : $month; |
|
|
|
|
109
|
|
|
$this->year = (0 == $year) ? date('Y') : $year; |
|
|
|
|
110
|
|
|
|
111
|
|
|
// Check for valid input |
112
|
|
|
if (!preg_match('~[0-9]{4}~', $this->year)) { |
113
|
|
|
debug::show($this->year); |
114
|
|
|
user_error('Year should be a valid number in the ManyMonthsCalendar Class', E_USER_ERROR); |
115
|
|
|
} |
116
|
|
|
if (!is_numeric($this->month) || $this->month < 0 || $this->month > 13) { |
117
|
|
|
debug::show($this->month); |
118
|
|
|
user_error('Month should be a valid number in the ManyMonthsCalendar Class', E_USER_ERROR); |
119
|
|
|
} |
120
|
|
|
// Set the current timestamp |
121
|
|
|
$this->timeStamp = mktime(1, 1, 1, $this->month, 1, $this->year); |
122
|
|
|
// Set the number of days in the current month |
123
|
|
|
$this->daysInMonth = date('t', $this->timeStamp); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
public function addEvent($timeStamp, $title, $link = '') |
129
|
|
|
{ |
130
|
|
|
$this->events[$timeStamp][$this->cleanEventTitle($title)] = array( |
131
|
|
|
'Title' => $title, |
132
|
|
|
'Link' => $link |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function removeEvent($timeStamp, $title) |
137
|
|
|
{ |
138
|
|
|
if (isset($this->events[$timeStamp][$this->cleanEventTitle($title)])) { |
139
|
|
|
unset($this->events[$timeStamp][$this->cleanEventTitle($title)]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function ManyMonthsCalendar() |
144
|
|
|
{ |
145
|
|
|
Requirements::themedCSS("ManyMonthsCalendar"); |
146
|
|
|
//make sure that all arrays added to calendarArray are DoSets in their own right |
147
|
|
|
self::$count_of_calenders_shown++; |
148
|
|
|
$calendarArray = array(); |
149
|
|
|
//general variables |
150
|
|
|
$calendarArray["TableID"] = $this->calenderName.'-'.self::$count_of_calenders_shown; |
|
|
|
|
151
|
|
|
$calendarArray["TableClass"] = $this->calenderName; |
|
|
|
|
152
|
|
|
$calendarArray["EnableNavigation"] = self::$enable_navigation; |
153
|
|
|
$calendarArray["ColSpan"] = 5; |
154
|
|
|
$calendarArray["PageLink"] = $this->pageLink; |
155
|
|
|
$calendarArray["MonthName"] = $this->getMonthName(); |
156
|
|
|
if (self::$show_year_in_calendar) { |
157
|
|
|
$calendarArray["YearName"] = $this->year; |
158
|
|
|
} |
159
|
|
|
$calendarArray["CellSpacing"] = self::$table_cell_spacing; |
160
|
|
|
$calendarArray["PreviousMonthNavText"] = self::$previous_month_nav_text; |
161
|
|
|
$calendarArray["NextMonthNavText"] = self::$next_month_nav_text; |
162
|
|
|
if (self::$enable_navigation) { |
163
|
|
|
$previousMonth = explode('-', date('n-Y', strtotime('-1 month', $this->timeStamp))); |
164
|
|
|
$calendarArray["PreviousMonthMonthNumber"] = $previousMonth[0]; |
165
|
|
|
$calendarArray["PreviousMonthYearNumber"] = $previousMonth[1]; |
166
|
|
|
$nextYear = explode('-', date('n-Y', strtotime('+1 month', $this->timeStamp))); |
167
|
|
|
$calendarArray["NextMonthMonthNumber"] = $nextYear[0]; |
168
|
|
|
$calendarArray["NextMonthYearNumber"] = $nextYear[1]; |
169
|
|
|
$calendarArray["ColSpan"] = 7; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$doSetDayNames = new DataObjectSet(); |
173
|
|
|
//day name |
174
|
|
|
foreach (self::$day_names as $name) { |
175
|
|
|
$doSetDayNames->push(new ArrayData(array("DayName" => $name))); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
$calendarArray["DayNames"] = $doSetDayNames; |
178
|
|
|
|
179
|
|
|
$extraDaysToBeAddedFromPreviousMonth = date('N', $this->timeStamp) + self::$start_day_of_the_week - 1; |
180
|
|
|
$position = 0; |
181
|
|
|
|
182
|
|
|
$doSetDates = new DataObjectSet(); |
183
|
|
|
// previous month |
184
|
|
|
for ($e = 1 ; $e <= $extraDaysToBeAddedFromPreviousMonth ; $e++) { |
185
|
|
|
$position++; |
186
|
|
|
$timeStamp = $this->makeTimeStamp("-" . intval($extraDaysToBeAddedFromPreviousMonth -$e) . " days"); |
187
|
|
|
$doSetDates->push($this->getDateDataObject($position, $timeStamp, false)); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// current Month |
191
|
|
|
for ($i = 1 ; $i <= $this->daysInMonth ; $i++) { |
192
|
|
|
$position++; |
193
|
|
|
$timeStamp = $this->makeTimeStamp("+".($i-1)." days"); |
194
|
|
|
$doSetDates->push($this->getDateDataObject($position, $timeStamp)); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// next month |
198
|
|
|
for ($e2 = 1 ; $e2 < (7 - (($e + $this->daysInMonth -1) % 7)) ; $e2++) { |
199
|
|
|
$position++; |
200
|
|
|
$timeStamp = $this->makeTimeStamp("+$e2 days"); |
201
|
|
|
$doSetDates->push($this->getDateDataObject($position, $timeStamp, false)); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
$calendarArray["Days"] = $doSetDates; |
204
|
|
|
return new ArrayData($calendarArray); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
private function getDateDataObject($position, $timeStamp, $currentMonth = true) |
208
|
|
|
{ |
209
|
|
|
$array = array(); |
210
|
|
|
$array["Day"] = date(self::$calendar_day_format, $timeStamp); |
211
|
|
|
if ($currentMonth) { |
212
|
|
|
$array["OutsideCurrentMonth"] = true; |
213
|
|
|
} else { |
214
|
|
|
$array["OutsideCurrentMonth"] = false; |
215
|
|
|
} |
216
|
|
|
$eventsDoSet = new DataObjectSet(); |
217
|
|
|
if (isset($this->events[$timeStamp])) { |
218
|
|
|
if (is_array($this->events[$timeStamp])) { |
219
|
|
|
foreach ($this->events[$timeStamp] as $event) { |
220
|
|
|
$eventsDoSet->push(new ArrayData($event)); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
$array["Events"] = $eventsDoSet; |
225
|
|
|
if (($position % 7) == 0 /*&& $position < */) { |
226
|
|
|
$array["LastDayOfTheWeek"] = true; |
227
|
|
|
} |
228
|
|
|
if ($timeStamp == mktime(1, 1, 1, date('n'), date('j'), date('Y'))) { |
229
|
|
|
$array["IsCurrentDay"] = true; |
230
|
|
|
} else { |
231
|
|
|
$array["IsCurrentDay"] = false; |
232
|
|
|
} |
233
|
|
|
if ($position % 2) { |
234
|
|
|
$array["EvenCol"] = true; |
235
|
|
|
} |
236
|
|
|
if (round($position / 7) % 2) { |
237
|
|
|
$array["EvenRow"] = false; |
238
|
|
|
} |
239
|
|
|
return new ArrayData($array); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
private function cleanEventTitle($title) |
243
|
|
|
{ |
244
|
|
|
$title = eregi_replace("[^[:alnum:]]", " ", $title); |
245
|
|
|
$title = trim(eregi_replace(" +", "", $title)); //removes excess spaces |
246
|
|
|
return $title; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
private function makeTimeStamp($offset) |
250
|
|
|
{ |
251
|
|
|
return strtotime($offset, $this->timeStamp); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
private function getMonthName() |
255
|
|
|
{ |
256
|
|
|
return ucwords(self::$month_names[$this->month-1]); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function forTemplate() |
260
|
|
|
{ |
261
|
|
|
return $this->renderWith('ManyMonthsCalendar'); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.