File: Week.php

package info (click to toggle)
kronolith2 2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,936 kB
  • ctags: 3,577
  • sloc: php: 14,001; xml: 1,494; sql: 489; makefile: 68
file content (395 lines) | stat: -rw-r--r-- 16,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php

require_once dirname(__FILE__) . '/Day.php';

/**
 * The Kronolith_View_Week:: class provides an API for viewing weeks.
 *
 * $Horde: kronolith/lib/Views/Week.php,v 1.24.2.5 2008/03/06 20:50:53 chuck Exp $
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @author  Jan Schneider <jan@horde.org>
 * @since   Kronolith 0.1
 * @package Kronolith
 */
class Kronolith_View_Week {

    var $parsed = false;
    var $days = array();
    var $week = null;
    var $year = null;
    var $startDay = null;
    var $endDay = null;
    var $_controller = 'week.php';
    var $_sidebyside = false;
    var $_currentCalendars = array();

    /**
     * How many time slots are we dividing each hour into?
     *
     * @var integer
     */
    var $_slotsPerHour = 2;

    /**
     * How many slots do we have per day? Calculated from $_slotsPerHour.
     *
     * @see $_slotsPerHour
     * @var integer
     */
    var $_slotsPerDay;

    function Kronolith_View_Week($week = null, $year = null, $startDay = null, $endDay = null)
    {
        if (empty($year)) {
            $year = date('Y');
        }
        if (empty($week)) {
            $date = new Horde_Date(array('year' => $year, 'month' => date('n'), 'mday' => date('j')));
            $week = $date->weekOfYear();
            if (!$GLOBALS['prefs']->getValue('week_start_monday') && $date->dayOfWeek() == HORDE_DATE_SUNDAY) {
                ++$week;
            }
            if ($week > 51 && $date->month == 1) {
                --$year;
            } elseif ($week == 1 && $date->month == 12) {
                ++$year;
            }
        } else {
            $weeksInYear = Horde_Date::weeksInYear($year);
            if ($week < 1) {
                --$year;
                $week += $weeksInYear;
            } elseif ($week > $weeksInYear) {
                $week -= $weeksInYear;
                ++$year;
            }
        }

        $this->year = $year;
        $this->week = $week;

        if ($startDay === null || $endDay === null) {
            if ($this->startDay === null) {
                if ($GLOBALS['prefs']->getValue('week_start_monday')) {
                    $this->startDay = HORDE_DATE_MONDAY;
                    $this->endDay = HORDE_DATE_SUNDAY + 7;
                } else {
                    $this->startDay = HORDE_DATE_SUNDAY;
                    $this->endDay = HORDE_DATE_SATURDAY;
                }
            }
        } else {
            $this->startDay = $startDay;
            $this->endDay = $endDay;
        }

        $firstDay = Horde_Date::firstDayOfWeek($week, $year) + Date_Calc::dateToDays(1, 1, $year) - 1;

        for ($i = $this->startDay; $i <= $this->endDay; ++$i) {
            list($day, $month, $year) = explode('/', Date_Calc::daysToDate($firstDay + $i, '%d/%m/%Y'));
            $this->days[$i] = new Kronolith_View_Day($month, $day, $year, array());
        }

        list($sday, $smonth, $syear) = explode('/', Date_Calc::daysToDate($firstDay + $this->startDay, '%d/%m/%Y'));
        list($eday, $emonth, $eyear) = explode('/', Date_Calc::daysToDate($firstDay + $this->endDay, '%d/%m/%Y'));
        $startDate = new Horde_Date(array('year' => $syear, 'month' => $smonth, 'mday' => $sday));
        $endDate = new Horde_Date(array('year' => $eyear, 'month' => $emonth, 'mday' => $eday,
                                        'hour' => 23, 'min' => 59, 'sec' => 59));
        $endDate->correct();
        $allevents = Kronolith::listEvents($startDate, $endDate, $GLOBALS['display_calendars']);
        if (is_a($allevents, 'PEAR_Error')) {
            $GLOBALS['notification']->push($allevents, 'horde.error');
            $allevents = array();
        }
        for ($i = $this->startDay; $i <= $this->endDay; ++$i) {
            $this->days[$i]->setEvents(isset($allevents[$this->days[$i]->getStamp()]) ?
                                       $allevents[$this->days[$i]->getStamp()] :
                                       array());
        }
        $this->_sidebyside = $this->days[$this->startDay]->_sidebyside;
        $this->_currentCalendars = $this->days[$this->startDay]->_currentCalendars;
        $this->_slotsPerHour = $this->days[$this->startDay]->_slotsPerHour;
        $this->_slotsPerDay = $this->days[$this->startDay]->_slotsPerDay;
        $this->_slotLength = $this->days[$this->startDay]->_slotLength;
    }

    function html()
    {
        global $prefs;

        $more_timeslots = $prefs->getValue('time_between_days');
        $include_all_events = !$prefs->getValue('show_shared_side_by_side');
        $showLocation = Kronolith::viewShowLocation();
        $showTime = Kronolith::viewShowTime();

        if (!$this->parsed) {
            $this->parse();
        }

        $slots = $this->days[$this->startDay]->slots;
        $cid = 0;
        require KRONOLITH_TEMPLATES . '/week/head.inc';
        if ($this->_sidebyside) {
            require KRONOLITH_TEMPLATES . '/week/head_side_by_side.inc';
        }
        echo '</thead><tbody>';

        $event_count = 0;
        for ($j = $this->startDay; $j <= $this->endDay; ++$j) {
            foreach ($this->_currentCalendars as $cid => $cal) {
                $event_count = max($event_count, count($this->days[$j]->_all_day_events[$cid]));
                reset($this->days[$j]->_all_day_events[$cid]);
            }
        }

        if ($more_timeslots) {
            $newEventUrl = null;
        } else {
            $newEventUrl = _("All day");
        }

        $eventCategories = array();

        $row = '';
        for ($j = $this->startDay; $j <= $this->endDay; ++$j) {
            $row .= '<td class="hour rightAlign">' . ($more_timeslots ? _("All day") : '&nbsp;') . '</td>' .
                '<td colspan="' . $this->days[$j]->_totalspan . '" valign="top"><table width="100%" cellspacing="0">';
            if ($this->days[$j]->_all_day_maxrowspan > 0) {
                for ($k = 0; $k < $this->days[$j]->_all_day_maxrowspan; ++$k) {
                    $row .= '<tr>';
                    foreach ($this->days[$j]->_currentCalendars as $cid => $cal) {
                        if (count($this->days[$j]->_all_day_events[$cid]) === $k) {
                            $row .= '<td rowspan="' . ($this->days[$j]->_all_day_maxrowspan - $k) . '" width="'. round(99 / count($this->days[$j]->_currentCalendars)) . '%">&nbsp;</td>';
                        } elseif (count($this->days[$j]->_all_day_events[$cid]) > $k) {
                            $event = $this->days[$j]->_all_day_events[$cid][$k];
                            if ($event->hasPermission(PERMS_READ)) {
                                $eventCategories[$event->getCategory()] = true;
                            }

                            $row .= '<td class="week-eventbox category' . md5($event->getCategory()) . '" '
                                . 'width="' . round(99 / count($this->days[$j]->_currentCalendars)) . '%" '
                                . 'valign="top">'
                                . $event->getLink($this->days[$j]->getStamp(), true, $this->link(0, true));
                            if ($showLocation) {
                                $row .= '<div class="event-location">' . htmlspecialchars($event->getLocation()) . '</div>';
                            }
                            $row .= '</td>';
                        }
                    }
                    $row .= '</tr>';
                }
            } else {
                $row .= '<tr><td colspan="' . count($this->_currentCalendars) . '">&nbsp;</td></tr>';
            }
            $row .= '</table></td>';
        }

        $rowspan = '';
        $first_row = true;
        require KRONOLITH_TEMPLATES . '/day/all_day.inc';

        $twenty_four = $prefs->getValue('twentyFour');
        $day_hour_force = $prefs->getValue('day_hour_force');
        $day_hour_start = $prefs->getValue('day_hour_start') / 2 * $this->_slotsPerHour;
        $day_hour_end = $prefs->getValue('day_hour_end') / 2 * $this->_slotsPerHour;
        $rows = array();
        $covered = array();

        for ($i = 0; $i < $this->_slotsPerDay; ++$i) {
            if ($i >= $day_hour_end && $i > $this->last) {
                break;
            }
            if ($i < $this->first && $i < $day_hour_start) {
                continue;
            }

            if (($m = $i % $this->_slotsPerHour) != 0) {
                $time = ':' . $m * $this->_slotLength;
                $hourclass = 'halfhour';
            } else {
                $time = date($twenty_four ? 'G' : 'ga', $slots[$i]['timestamp']);
                $hourclass = 'hour';
            }

            $row = '';
            for ($j = $this->startDay; $j <= $this->endDay; ++$j) {
                // Add spacer between days, or timeslots.
                if ($more_timeslots) {
                    $row .= '<td align="right" class="' . $hourclass . '">' . $time . '</td>';
                } else {
                    $row .= '<td>&nbsp;</td>';
                }

                if (!count($this->_currentCalendars)) {
                    $row .= '<td>&nbsp;</td>';
                }
                foreach ($this->_currentCalendars as $cid => $cal) {
                    $hspan = 0;
                    foreach ($this->days[$j]->_event_matrix[$cid][$i] as $key) {
                        $event = &$this->days[$j]->_events[$key];
                        if ($include_all_events || $event->getCalendar() == $cid) {
                            // Since we've made sure that this event's
                            // overlap is a factor of the total span,
                            // we get this event's individual span by
                            // dividing the total span by this event's
                            // overlap.
                            $span = $this->days[$j]->_span[$cid] / $event->overlap;

                            // Store the indent we're starting this
                            // event at for future use.
                            if (!isset($event->indent)) {
                                $event->indent = $hspan;
                            }

                            // If the first node that we would cover
                            // is already covered, we can assume that
                            // table rendering will take care of
                            // pushing the event over. However, if the
                            // first node _isn't_ covered but any
                            // others that we would covered _are_, we
                            // only cover the available nodes.
                            if (!isset($covered[$j][$i][$event->indent])) {
                                $collision = false;
                                $available = 0;
                                for ($y = $event->indent; $y < ($span + $event->indent); ++$y) {
                                    if (isset($covered[$j][$i][$y])) {
                                        $collision = true;
                                        break;
                                    }
                                    $available++;
                                }

                                if ($collision) {
                                    $span = $available;
                                }
                            }

                            $hspan += $span;

                            $start = mktime(floor($i / $this->_slotsPerHour), ($i % $this->_slotsPerHour) * $this->_slotLength, 0,
                                            $this->days[$j]->month, $this->days[$j]->mday, $this->days[$j]->year);
                            if (((!$day_hour_force || $i >= $day_hour_start) &&
                                 $event->start->timestamp() >= $start &&
                                 $event->start->timestamp() < $start + 60 * $this->_slotLength ||
                                 $start == $this->days[$j]->getStamp()) ||
                                ($day_hour_force &&
                                 $i == $day_hour_start &&
                                 $event->start->timestamp() < $start)) {
                                if ($event->hasPermission(PERMS_READ)) {
                                    $eventCategories[$event->getCategory()] = true;
                                }

                                // Store the nodes that we're covering for
                                // this event in the coverage graph.
                                for ($x = $i; $x < ($i + $event->rowspan); ++$x) {
                                    for ($y = $event->indent; $y < $hspan; ++$y) {
                                        $covered[$j][$x][$y] = true;
                                    }
                                }

                                $row .= '<td class="week-eventbox category' . md5($event->getCategory()) . '" '
                                    . 'valign="top" '
                                    . 'width="' . floor(((90 / count($this->days)) / count($this->_currentCalendars)) * ($span / $this->days[$j]->_span[$cid])) . '%" '
                                    . 'colspan="' . $span . '" rowspan="' . $event->rowspan . '">'
                                    . $event->getLink($this->days[$j]->getStamp(), true, $this->link(0, true));
                                if ($showTime) {
                                    $row .= '<div class="event-time">' . htmlspecialchars($event->getTimeRange()) . '</div>';
                                }
                                if ($showLocation) {
                                    $row .= '<div class="event-location">' . htmlspecialchars($event->getLocation()) . '</div>';
                                }
                                $row .= '</td>';
                            }
                        }
                    }

                    $diff = $this->days[$j]->_span[$cid] - $hspan;
                    if ($diff > 0) {
                        $row .= str_repeat('<td>&nbsp;</td>', $diff);
                    }
                }
            }

            $rows[] = array('row' => $row, 'slot' => '<span class="' . $hourclass . '">' . $time . '</span>');
        }

        require_once 'Horde/Template.php';
        $template = new Horde_Template();
        $template->set('row_height', round(20 / $this->_slotsPerHour));
        $template->set('rows', $rows);
        $template->set('show_slots', !$more_timeslots, true);
        echo $template->fetch(KRONOLITH_TEMPLATES . '/day/rows.html')
            . '</tbody></table>';

        require KRONOLITH_TEMPLATES . '/category_legend.inc';
    }

    /**
     * Parse all events for all of the days that we're handling; then
     * run through the results to get the total horizontal span for
     * the week, and the latest event of the week.
     */
    function parse()
    {
        for ($i = $this->startDay; $i <= $this->endDay; ++$i) {
            $this->days[$i]->parse();
        }

        $this->totalspan = 0;
        $this->span = array();
        for ($i = $this->startDay; $i <= $this->endDay; ++$i) {
            $this->totalspan += $this->days[$i]->_totalspan;
            foreach ($this->_currentCalendars as $cid => $key) {
                if (isset($this->span[$cid])) {
                    $this->span[$cid] += $this->days[$i]->_span[$cid];
                } else {
                    $this->span[$cid] = $this->days[$i]->_span[$cid];
                }
            }
        }

        $this->last = 0;
        $this->first = $this->_slotsPerDay;
        for ($i = $this->startDay; $i <= $this->endDay; ++$i) {
            if ($this->days[$i]->last > $this->last) {
                $this->last = $this->days[$i]->last;
            }
            if ($this->days[$i]->first < $this->first) {
                $this->first = $this->days[$i]->first;
            }
        }
    }

    function getWeek($offset = 0)
    {
        $week = $this->week + $offset;
        $year = $this->year;
        $weeksInYear = Horde_Date::weeksInYear($year);
        if ($week < 1) {
            --$year;
            $week += $weeksInYear;
        } elseif ($week > $weeksInYear) {
            $week -= $weeksInYear;
            ++$year;
        }

        return array('week' => $week,
                     'year' => $year);
    }

    function link($offset = 0, $full = false)
    {
        $pair = $this->getWeek($offset);
        return Horde::applicationUrl(Util::addParameter($this->_controller,
                                                        array('week' => $pair['week'],
                                                              'year' => $pair['year'])),
                                     $full);
    }

    function getName()
    {
        return 'Week';
    }

}