]>
Commit | Line | Data |
---|---|---|
a5eae6b7 MR |
1 | <?php |
2 | /* $Id$ */ | |
3 | include_once 'view.class.php'; | |
4 | require_once 'caldavresource.class.php'; | |
5 | include_once 'dateformat.class.php'; | |
6 | require_once 'helper.php'; | |
7 | ||
8 | class WeekView extends View { | |
9 | ||
10 | private $url; | |
11 | private $p_year; | |
12 | private $privious; | |
13 | private $year_and_week; | |
14 | private $n_year; | |
15 | private $next; | |
16 | private $year; | |
17 | private $week; | |
18 | private $range; | |
19 | private $calendar; | |
20 | private $etags = array(); | |
21 | private $top = "<table style=\"width: 100%\"> | |
22 | <tr> | |
23 | <td style=\"width: 33%; text-align: left;\"><a href= | |
24 | \"__URL__?year=__P_YEAR__&week=__PRIVIOUS__\"> | |
25 | <img style=\"border: 0\" src=\"__WEB_ROOT__/pixmaps/left.png\" | |
26 | alt=\"Privious\"/></a></td> | |
27 | <td style=\"width: 33%; font-size: 2.5em; font-weight: bold; | |
28 | text-align: center\">__YEAR_AND_WEEK__</td> | |
29 | <td style=\"width: 33%; text-align: right;\"> | |
30 | <a href=\"__URL__?year=__N_YEAR__&week=__NEXT__\"> | |
31 | <img style=\"border: 0\" src=\"__WEB_ROOT__/pixmaps/right.png\" | |
32 | alt=\"next\"/></a></td> | |
33 | </tr></table>"; | |
34 | private $bottom; | |
35 | ||
36 | function __construct(Calendar $calendar = NULL) { | |
37 | $this->url = parent::$root.'/navigate/show_week.php'; | |
38 | $this->calendar = $calendar; | |
39 | $this->range = array(); | |
40 | } | |
41 | ||
42 | protected function parseDate() { | |
43 | if ($this->year && $this->week) { | |
44 | $week = $this->week; | |
45 | if ($week < 0 || $week > 53) { | |
46 | $week = $this->get_week_number(time()); | |
47 | } | |
48 | $year = $this->year; | |
49 | } | |
50 | else { | |
51 | $week = $this->get_week_number(time()); | |
52 | $year = date('Y'); | |
53 | } | |
54 | $this->privious = $week - 1; | |
55 | $this->p_year = $year; | |
56 | if ($this->privious == 0) { | |
57 | $this->p_year -= 1; | |
58 | if (date("w", strtotime($this->p_year."/12/31")) > 3) | |
59 | $this->privious = 53; | |
60 | else | |
61 | $this->privious = 52; | |
62 | } | |
63 | $this->next = $week + 1; | |
64 | $this->n_year = $year; | |
65 | if ($this->next > 51) { | |
66 | if ($this->get_week_number(strtotime("$year/12/31")) == 1 || | |
67 | $this->next > 53) { | |
68 | $this->next = 1; | |
69 | $this->n_year += 1; | |
70 | } | |
71 | } | |
72 | ||
73 | $this->year_and_week = "$year Week $week"; | |
74 | $this->year = $year; | |
75 | $this->week = $week; | |
76 | } | |
77 | ||
78 | protected function getHead() { | |
79 | $this->parseDate(); | |
80 | $head = str_replace('__URL__', $this->url, $this->top); | |
81 | $head = str_replace('__P_YEAR__', $this->p_year, $head); | |
82 | $head = str_replace('__PRIVIOUS__', $this->privious, $head); | |
83 | $head = str_replace('__WEB_ROOT__', parent::$root, $head); | |
84 | $head = str_replace('__YEAR_AND_WEEK__', $this->year_and_week, $head); | |
85 | $head = str_replace('__N_YEAR__', $this->n_year, $head); | |
86 | $head = str_replace('__NEXT__', $this->next, $head); | |
87 | $this->bottom = $head; | |
88 | return $this->applyLegend() . $head; | |
89 | } | |
90 | ||
91 | private function addDates($head) { | |
92 | $timestamp = $this->lastDayInWeek($this->year, $this->week); | |
93 | $i = 7; | |
94 | $this->range['end'] = $timestamp; | |
95 | do { | |
96 | $date = date('Y/m/d', $timestamp); | |
97 | $timestamp = strtotime('-1 day', $timestamp); | |
98 | $head = substr_replace($head, $date, strrpos($head, '__DATE__'), 8); | |
99 | $i--; | |
100 | } while ($i > 0); | |
101 | $this->range['start'] = $timestamp; | |
102 | return $head; | |
103 | } | |
104 | ||
105 | private function getCell($events, $day, $progress, $excluded = FALSE) { | |
106 | $cell = ''; | |
107 | //$etags = array(); | |
108 | if ($events) { | |
109 | //print var_export($events,true)."<br/>"; | |
110 | $dateFormat = new dateFormat(); | |
111 | foreach ($events as $etag =>$infos) { | |
112 | foreach ($infos['dates'] as $time) { | |
113 | //print "{$time['start']}:{$time['end']}<br/>"; | |
114 | //print $time['end']."<br/>"; | |
115 | CaldavRessource::fix_allday_event($time['start'],$time['end']); | |
116 | //print $time['end']."<br/>"; | |
117 | //print "{$time['start']}:{$time['end']}<br/>"; | |
118 | $time['start'] = $dateFormat->UTC2Client($time['start']); | |
119 | $time['end'] = $dateFormat->UTC2Client($time['end']); | |
120 | //print $time['end']."<br/>"; | |
121 | if ($excluded) { | |
122 | $ts = strtotime("+$day day +".parent::$start_hour." hour", | |
123 | $this->range['start']); | |
124 | $ical_start = CaldavRessource::timestamp2ICal($ts); | |
125 | //print parent::$end_hour; | |
126 | $end = (parent::$end_hour == 24) ? 23 : parent::$end_hour; | |
127 | $ts = strtotime("+$day day +".$end." hour", | |
128 | $this->range['start']); | |
129 | $ical_end = CaldavRessource::timestamp2ICal($ts); | |
130 | $time_start = (strrchr($time['start'], 'Z')) ? | |
131 | trim($time['start'], "Z") : $time['start']; | |
132 | $time_end = (strrchr($time['end'], 'Z')) ? | |
133 | trim($time['end'], "Z") : $time['end']; | |
134 | //print $time_end."\n"; | |
135 | $parts = explode("T", $time_end); | |
136 | if (substr($parts[1], 0, 2) == "00") { | |
137 | $parts[1] = "24" . substr($parts[1], 2, 4); | |
138 | $time_end = $parts[0] . "T" . $parts[1]; | |
139 | } | |
140 | //print "b[$time_start : $time_end]<br/>"; | |
141 | //print "a[$ical_start : $ical_end] [$time_start : $time_end]<br/>"; | |
142 | if ((CaldavRessource::cmptime( | |
143 | $ical_start, $time_start) > 0 && | |
144 | CaldavRessource::cmpdate($ical_start, $time_end) == 0) | |
145 | || | |
146 | (CaldavRessource::cmptime( | |
147 | $ical_end, $time_end) < 0 && | |
148 | CaldavRessource::cmpdate($ical_end, $time_start) == 0)) { | |
149 | //print "b[$ical_start : $ical_end] [$time_start : $time_end]<br/>"; | |
150 | if (! in_array($etag, $this->etags)) { | |
151 | array_push($this->etags, $etag); | |
152 | $cell = $this->makeEvent($cell, $etag, $infos); | |
153 | //print "$cell<br/>"; | |
154 | //print "exc: cell: $etag<br/>"; | |
155 | //print "exc: ".var_export($this->etags, true)."<br/>"; | |
156 | } | |
157 | } | |
158 | } | |
159 | else { | |
160 | $ts = strtotime("+$day day +$progress hour", | |
161 | $this->range['start']); | |
162 | $icalDate = CaldavRessource::timestamp2ICal($ts, TRUE); | |
163 | //print "$icalDate {$time['start']}:{$time['end']}<br/>"; | |
164 | $s_cmp = CaldavRessource::datecmp($icalDate, $time['start']); | |
165 | $e_cmp = CaldavRessource::datecmp($icalDate, $time['end']); | |
166 | //print "$s_cmp:$e_cmp<br/>"; | |
167 | if ($s_cmp >= 0 && $e_cmp < 0) { | |
168 | //if (! in_array($etag, $this->etags)) { | |
169 | $cell = $this->makeEvent($cell, $etag, $infos); | |
170 | //print "$cell<br/>"; | |
171 | //print "inc: cell: $etag<br/>"; | |
172 | //print "inc: ".var_export($this->etags, true)."<br/>"; | |
173 | //} | |
174 | } | |
175 | } | |
176 | } | |
177 | } | |
178 | } | |
179 | return ($cell == '') ? ' ' : $cell; | |
180 | } | |
181 | ||
182 | public function getView($year = NULL, $week = NULL) { | |
183 | $this->year = $year; | |
184 | $this->week = $week; | |
185 | $calendar = $this->getHead(); | |
186 | $calendar .= '<br />'; | |
187 | $calendar .= $this->addDates($this->getViewHead('week')); | |
188 | if ($this->get_week_number(time()) == $week || $week == NULL) { | |
189 | $day = date('w'); | |
190 | if ($_SESSION['user_settings']->getStartWeek() === FALSE) { | |
191 | if ($day == 0) | |
192 | $day = 6; | |
193 | else | |
194 | $day--; | |
195 | } | |
196 | } | |
197 | else | |
198 | $day = -1; | |
199 | $events = $this->getEvents($this->range); | |
200 | //var_dump($events); | |
201 | //var_dump($_SESSION['all_events']); | |
202 | $calendar .= "<tr><td class=\"time\">Excluded</td>\n"; | |
203 | $excluded = array(); | |
204 | for ($d = 0; $d < 7; $d++) { | |
205 | $cell = $this->getCell($events, $d + 1, 0, TRUE); | |
206 | if ($d == $day && ($year == date('Y') || $year == NULL)) { | |
207 | $calendar .= "<td class=\"today\">$cell</td>\n"; | |
208 | } | |
209 | else | |
210 | $calendar .= "<td>$cell</td>\n"; | |
211 | //print_r($cell); | |
212 | //if ($d < 6) { | |
213 | // print "clean etags<br/>"; | |
214 | // $this->etags = array(); | |
215 | //} | |
216 | array_push($excluded, $this->etags); | |
217 | $this->etags = array(); | |
218 | } | |
219 | $calendar .= '</tr>'; | |
220 | for ($i = parent::$start_hour; $i < parent::$end_hour; $i++) { | |
221 | $s_time = ($i < 10) ? "0$i" : $i; | |
222 | $e_time = ($i < 9) ? '0'.($i + 1) : ($i + 1); | |
223 | $calendar .= "<tr><td class=\"time\">$s_time - $e_time</td>\n"; | |
224 | for ($d = 0; $d < 7; $d++) { | |
225 | $this->etags = $excluded[$d]; | |
226 | $progress = $i; | |
227 | $cell = $this->getCell($events, $d + 1, $progress); | |
228 | //print_r($cell); | |
229 | if ($d == $day && ($year == date('Y') || $year == NULL)) { | |
230 | $calendar .= "<td class=\"today\">$cell</td>\n"; | |
231 | } | |
232 | else | |
233 | $calendar .= "<td>$cell</td>\n"; | |
234 | } | |
235 | $calendar .= '</tr>'; | |
236 | } | |
237 | $calendar .= '</table></div>'; | |
238 | $calendar .= '<br />'; | |
239 | $calendar .= $this->bottom; | |
240 | //file_put_contents('/tmp/davical.log', $calendar); | |
241 | return $calendar; | |
242 | } | |
243 | ||
244 | function get_week_number($timestamp) { | |
245 | return parent::get_week_number($timestamp); | |
246 | } | |
247 | } |