]>
Commit | Line | Data |
---|---|---|
a5eae6b7 MR |
1 | <?php |
2 | /* $Id$ */ | |
3 | ||
4 | require_once 'user_validate.php'; | |
5 | ||
6 | valid_user(); | |
7 | ||
8 | include_once 'config.inc.php'; | |
9 | require_once 'helper.php'; | |
10 | ||
11 | include_once 'month_view.class.php'; | |
12 | include_once 'week_view.class.php'; | |
13 | include_once 'day_view.class.php'; | |
14 | include_once 'calendar.class.php'; | |
15 | ||
16 | abstract class View { | |
17 | protected static $start_sunday; | |
18 | protected static $root; | |
19 | protected static $start_hour; | |
20 | protected static $end_hour; | |
21 | private $heading = "<div id=\"events\"><table><tr>"; | |
22 | private $view_head_sunday = " | |
23 | <th class=\"event\">Sunday</th> | |
24 | <th class=\"event\">Monday</th> | |
25 | <th class=\"event\">Tuesday</th> | |
26 | <th class=\"event\">Wednedsday</th> | |
27 | <th class=\"event\">Thursday</th> | |
28 | <th class=\"event\">Friday</th> | |
29 | <th class=\"event\">Saturday</th> | |
30 | </tr>"; | |
31 | private $view_head_monday = " | |
32 | <th class=\"event\">Monday</th> | |
33 | <th class=\"event\">Tuesday</th> | |
34 | <th class=\"event\">Wednedsday</th> | |
35 | <th class=\"event\">Thursday</th> | |
36 | <th class=\"event\">Friday</th> | |
37 | <th class=\"event\">Saturday</th> | |
38 | <th class=\"event\">Sunday</th> | |
39 | </tr>"; | |
40 | private $view_head_day = " | |
41 | <th class=\"day_event\">__DAY__</th> | |
42 | </tr>"; | |
43 | ||
44 | function __construct($root, $start_hour, $end_hour, $start_sunday = FALSE) { | |
45 | self::$start_sunday = $start_sunday; | |
46 | self::$root = $root; | |
47 | self::$start_hour = $start_hour; | |
48 | self::$end_hour = $end_hour; | |
49 | } | |
50 | ||
51 | protected function applyLegend() { | |
52 | $calenders = $_SESSION['user_settings']->getCalendars(); | |
53 | $legend = '<p style="text-align: center">'; | |
54 | foreach ($calenders as $calender) { | |
55 | $legend .= '[<span style="background: ' . $calender->color . | |
56 | '"> </span>:' . $calender->name . ']'; | |
57 | } | |
58 | $legend .= '</p>'; | |
59 | $legend .= <<<_HELP | |
60 | <p style="text-align: center"> | |
61 | Events outside configured range is only displayed | |
62 | when Day or Week view is active | |
63 | </p> | |
64 | _HELP; | |
65 | return $legend; | |
66 | } | |
67 | ||
68 | protected function getViewHead($kindOf) { | |
69 | switch ($kindOf) { | |
70 | case 'month': | |
71 | $return = $this->heading.'<th>Week</th>'; | |
72 | if (self::$start_sunday) | |
73 | $return .= $this->view_head_sunday; | |
74 | else | |
75 | $return .= $this->view_head_monday; | |
76 | return $return; | |
77 | break; | |
78 | case 'week': | |
79 | if (self::$start_sunday) { | |
80 | $head = str_replace('</th>', '<br/>__DATE__</th>', | |
81 | $this->view_head_sunday); | |
82 | } | |
83 | else { | |
84 | $head = str_replace('</th>', '<br/>__DATE__</th>', | |
85 | $this->view_head_monday); | |
86 | } | |
87 | $head = '<th>Time</th>' . $head; | |
88 | return $this->heading . $head; | |
89 | break; | |
90 | case 'day': | |
91 | $return = $this->heading . '<th>Time</th>'; | |
92 | return $return . $this->view_head_day; | |
93 | break; | |
94 | default: trigger_error("$kindOf: ['month', 'week', 'day']", E_USER_ERROR); | |
95 | } | |
96 | } | |
97 | ||
98 | /* | |
99 | * Function get_week_number which returns the week number of the | |
100 | * given date according to ISO 8601-1988 | |
101 | * http://www.php.happycodings.com/Date_Time/code22.html | |
102 | */ | |
103 | protected function is_leap_year($year) { | |
104 | if ((($year % 4) == 0 and ($year % 100)!=0) or ($year % 400)==0) { | |
105 | return 1; | |
106 | } else { | |
107 | return 0; | |
108 | } | |
109 | } | |
110 | ||
111 | /* define ISO_WEEK_START_WDAY 1 // Monday | |
112 | * define ISO_WEEK1_WDAY 4 // Thursday | |
113 | * define YDAY_MINIMUM (-366) | |
114 | * int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7; | |
115 | * return (yday - (yday - wday + ISO_WEEK1_WDAY + | |
116 | * big_enough_multiple_of_7) % 7 + ISO_WEEK1_WDAY - | |
117 | * ISO_WEEK_START_WDAY); | |
118 | */ | |
119 | private function iso_week_days($yday, $wday) { | |
120 | return $yday - (($yday - $wday + 382) % 7) + 3; | |
121 | } | |
122 | ||
123 | protected function get_week_number($timestamp) { | |
124 | $d = getdate($timestamp); | |
125 | $days = $this->iso_week_days($d[ "yday"], $d[ "wday"]); | |
126 | ||
127 | if ($days < 0) { | |
128 | $d[ "yday"] += 365 + $this->is_leap_year(--$d[ "year"]); | |
129 | $days = $this->iso_week_days($d[ "yday"], $d[ "wday"]); | |
130 | } else { | |
131 | $d[ "yday"] -= 365 + $this->is_leap_year($d[ "year"]); | |
132 | $d2 = $this->iso_week_days($d[ "yday"], $d[ "wday"]); | |
133 | if (0 <= $d2) { | |
134 | /* $d["year"]++; */ | |
135 | $days = $d2; | |
136 | } | |
137 | } | |
138 | $adjust = 1; | |
139 | $day = date('w'); | |
140 | if ($_SESSION['user_settings']->getStartWeek() === true && $day == 0) | |
141 | $adjust = 2; | |
142 | return (int)($days / 7) + $adjust; | |
143 | } | |
144 | ||
145 | protected function getStartDay($year, $month) { | |
146 | /* date('w') returns start day counting from Sunday */ | |
147 | $start = date('w', mktime(0,0,0,$month,1,$year)); | |
148 | return $start; | |
149 | } | |
150 | ||
151 | protected function isToDay($year, $month, $day) { | |
152 | return ($year == date('Y') && $month == date('n') && $day == date('j')); | |
153 | } | |
154 | ||
155 | protected function lastDayInWeek($year, $week) { | |
156 | $weekEndDay = 0; // 0 = Sunday. | |
157 | $dayOfYear = 4 + (($week - 1) * 7); | |
158 | $date = mktime(0, 0, 0, 1, $dayOfYear, $year); | |
159 | // Find the last day of this week. | |
160 | $dayOfWeek = date("w", $date); | |
161 | $daysToAdd = ($weekEndDay - $dayOfWeek + 7) % 7; | |
162 | $date += $daysToAdd * 24*60*60; | |
163 | return (self::$start_sunday) ? $date - (24*60*60) : $date; | |
164 | } | |
165 | ||
166 | protected function dateFromDayInYear($year, $day) { | |
167 | $now = mktime(0, 0, 0, 1, 1, $year); | |
168 | return date('l, j F', strtotime("+$day days", $now)); | |
169 | } | |
170 | ||
171 | protected function makeSummary($infos) { | |
172 | $summary = 'Title: '; | |
173 | $summary .= (isset($infos['summary']) && ! empty($infos['summary'])) ? | |
174 | $infos['summary'] : 'Untitled'; | |
175 | $summary .= "\n"; | |
176 | $summary .= 'Date: '; | |
177 | $start = strtotime($infos['dates'][0]['start']); | |
178 | $date = date("D j M Y", $start); | |
179 | $end = strtotime($infos['dates'][0]['end']); | |
180 | $summary .= $date . ' ' . date("G:i", $start) . ' - ' . date("G:i", $end); | |
181 | ||
182 | return $summary; | |
183 | } | |
184 | ||
185 | protected function makeEvent($cell, $uri, $infos) { | |
186 | $history = $_SERVER['PHP_SELF']; | |
187 | $summary = (isset($infos['summary']) && ! empty($infos['summary'])) ? | |
188 | $infos['summary'] : 'Untitled'; | |
189 | $history .= (empty($_SERVER['QUERY_STRING'])) ? '' : '?'.$_SERVER['QUERY_STRING']; | |
190 | //$url = WEB_ROOT . 'events/edit_event.php?etag='. $uri; | |
191 | $url = 'etag='. $uri.'&referer='.$history.'&cal='.$infos['cal']; | |
192 | $url = WEB_ROOT . 'events/edit_event.php?' . urlencode($url); | |
193 | $c = '<span style="background: '.$infos['color'].';color: #E6E6E6; | |
194 | font-weigth: bold;"><a href="'.$url. | |
195 | '" title="'.$this->makeSummary($infos).'">'.$summary.'</a></span>'; | |
196 | return ($cell != '') ? "$cell<br/>$c" : $c; | |
197 | } | |
198 | ||
199 | protected function getEvents($range) { | |
200 | $list = array(); | |
201 | $all_events = array(); | |
202 | ||
203 | $cals = $_SESSION['user_settings']->getCalendars(); | |
204 | //file_put_contents('/tmp/davical.log', | |
205 | // __FILE__ . ": " . var_export($cals, TRUE), FILE_APPEND); | |
206 | if (! $cals) | |
207 | return NULL; | |
208 | $start = CaldavRessource::timestamp2ICal($range['start'], TRUE); | |
209 | if ($_SESSION['user_settings']->getStartWeek() === FALSE) { | |
210 | $datetime = explode('T', $start); | |
211 | //print_r($datetime); | |
212 | $day = $datetime[0] + 1; | |
213 | $start = $day.'T'.$datetime[1].'Z'; | |
214 | } | |
215 | $end = CaldavRessource::timestamp2ICal($range['end'], TRUE); | |
216 | $datetime = explode('T', $end); | |
217 | $end = $datetime[0].'T'.'235959Z'; | |
218 | //print "$start:$end<br/>"; | |
219 | foreach ($cals as $cal) { | |
220 | //var_dump($cal); | |
221 | $conf = implode_cal(decode($cal->config)); | |
222 | //var_dump($conf); | |
223 | $events = new Calendar($conf['url'], $conf['uid'], $conf['pwd']); | |
224 | //print_r($events); | |
225 | $events->getComponents($start, $end); | |
226 | //print_r($events); | |
227 | $all_events[$cal->name] = $events; | |
228 | foreach ($events as $event) { | |
229 | $dates = $event->getActiveDates(); | |
230 | //file_put_contents('/tmp/davical.log', | |
231 | //__FILE__ . ": " . var_export($dates, TRUE), FILE_APPEND); | |
232 | //var_dump($dates); | |
233 | $comp = $event->getBaseComponent(); | |
234 | //print_r($comp); | |
235 | $summary = $comp->GetPValue('SUMMARY'); | |
236 | $item = array(); | |
237 | $dtend = explode('T', $comp->GetPValue('DTEND')); | |
238 | //print_r($comp->GetPValue('DTEND')); | |
239 | //print "<br/>"; | |
240 | //print_r($dtend); | |
241 | //print "<br/>"; | |
242 | //print_r($dates); | |
243 | //print "<br/>--------------------------<br/>"; | |
244 | if (count($dates) > 0) { | |
245 | foreach ($dates as $date) { | |
246 | //print_r($date); | |
247 | //print "<br/>--------------------------<br/>"; | |
248 | //$datetime = explode('T', $date); | |
249 | //print_r($dtend); | |
250 | //print "<br/>--------------------------<br/>"; | |
251 | //if (count($datetime) > 1) { | |
252 | if (count($dtend) > 1) { | |
253 | //if (count($dtend) < 2) | |
254 | // array_push($dtend, '235959'); | |
255 | //$endtime = $datetime[0].'T'.$dtend[1]; | |
256 | $endtime = $dtend[0].'T'.$dtend[1]; | |
257 | } | |
258 | else { | |
259 | //$endtime = $datetime[0]; | |
260 | $day = strtotime($dtend[0]); | |
261 | $day = strtotime("-1 day", $day); | |
262 | $endtime = date("Ymd\T235959", $day); | |
263 | //$endtime = $endtime."T235959"; | |
264 | //$endtime = $dtend[0]."T000000"; | |
265 | } | |
266 | //print "$date:$endtime<br/>"; | |
267 | array_push($item, array('start' => $date, 'end' => $endtime)); | |
268 | } | |
269 | $list[$event->getEtag()] = array('color' => $cal->color, | |
270 | 'summary' => $summary, 'dates' => $item, 'cal' => $cal->name); | |
271 | } | |
272 | } | |
273 | } | |
274 | $_SESSION['all_events'] = $all_events; | |
275 | //var_dump($list); | |
276 | return $list; | |
277 | } | |
278 | ||
279 | protected abstract function parseDate(); | |
280 | protected abstract function getHead(); | |
281 | public abstract function getView($year = NULL, $other = NULL); | |
282 | } | |
283 | ||
284 | class PageView extends View { | |
285 | const DAY = 0; | |
286 | const WEEK = 1; | |
287 | const MONTH = 2; | |
288 | ||
289 | function __construct($root, $start_hour, $end_hour, $start_sunday = FALSE) { | |
290 | parent::__construct($root, $start_hour, $end_hour, $start_sunday); | |
291 | } | |
292 | ||
293 | public function createView($view) { | |
294 | switch ($view) { | |
295 | case (self::DAY): return new DayView(); break; | |
296 | case (self::WEEK): return new WeekView(); break; | |
297 | case (self::MONTH): return new MonthView(); break; | |
298 | default: trigger_error("$view: ['MONTH', 'WEEK', 'DAY']", E_USER_ERROR); | |
299 | } | |
300 | } | |
301 | ||
302 | protected function parseDate() { | |
303 | throw new Exception("Function can only be called from a subclass"); | |
304 | } | |
305 | ||
306 | protected function getHead() { | |
307 | throw new Exception("Function can only be called from a subclass"); | |
308 | } | |
309 | ||
310 | public function getView($year = NULL, $other = NULL) { | |
311 | throw new Exception("Function can only be called from a subclass"); | |
312 | } | |
313 | } |