1<?php
2/**
3 * Display a list of previous calendar items grouped by month.
4 */
5class Kronolith_Block_Prevmonthlist extends Horde_Core_Block
6{
7    /**
8     */
9    public function __construct($app, $params = array())
10    {
11        parent::__construct($app, $params);
12
13        $this->_name = _("Prior Events");
14    }
15
16    /**
17     */
18    protected function _params()
19    {
20        $params = array(
21            'calendar' => array(
22                'name' => _("Calendar"),
23                'type' => 'enum',
24                'default' => '__all'
25            ),
26            'months' => array(
27                'name' => _("Months Before"),
28                'type' => 'int',
29                'default' => 2
30            ),
31            'alarms' => array(
32                'name' => _("Show only events that have an alarm set?"),
33                'type' => 'checkbox',
34                'default' => 0
35            )
36        );
37
38        $params['calendar']['values']['__all'] = _("All Visible");
39        foreach (Kronolith::listCalendars(Horde_Perms::SHOW, true) as $id => $cal) {
40            $params['calendar']['values'][$id] = $cal->name();
41        }
42
43        return $params;
44    }
45
46    /**
47     */
48    protected function _title()
49    {
50        $url = Horde::url($GLOBALS['registry']->getInitialPage(), true);
51        if (isset($this->_params['calendar']) &&
52            $this->_params['calendar'] != '__all') {
53            $url->add('display_cal', $this->_params['calendar']);
54        }
55
56        return $url->link() . $this->getName() . '</a>';
57    }
58
59    /**
60     */
61    protected function _content()
62    {
63        global $calendar_manager, $from_block, $page_output;
64
65        $from_block = true;
66
67        $page_output->addScriptFile('tooltips.js', 'horde');
68
69        $startDate = new Horde_Date(array(
70            'year' => date('Y'),
71            'month' => date('n') - $this->_params['months'],
72            'mday' => date('j')
73        ));
74        $endDate = new Horde_Date(array(
75            'year' => date('Y'),
76            'month' => date('n'),
77            'mday' => date('j') - 1
78        ));
79
80        $current_month = '';
81
82        try {
83            if (isset($this->_params['calendar']) &&
84                $this->_params['calendar'] != '__all') {
85                $calendars = Kronolith::listCalendars();
86                if (!isset($calendars[$this->_params['calendar']])) {
87                    return _("Calendar not found");
88                }
89                if (!$calendars[$this->_params['calendar']]->hasPermission(Horde_Perms::READ)) {
90                    return _("Permission Denied");
91                }
92                list($type, $calendar) = explode('_', $this->_params['calendar']);
93                $driver = Kronolith::getDriver($type, $calendar);
94                $all_events = $driver->listEvents(
95                    $startDate, $endDate, array('show_recurrence' => true));
96            } else {
97                $all_events = Kronolith::listEvents(
98                    $startDate,
99                    $endDate,
100                    $calendar_manager->get(Kronolith::DISPLAY_CALENDARS)
101                );
102            }
103        } catch (Exception $e) {
104            return '<em>' . $e->getMessage() . '</em>';
105        }
106
107        $html = '';
108
109        /* How many days do we need to check. */
110        $days = Date_Calc::dateDiff(
111            $startDate->mday, $startDate->month, $startDate->year,
112            $endDate->mday, $endDate->month, $endDate->year
113        );
114
115        /* Loop through the days. */
116        for ($i = 0; $i <= $days; ++$i) {
117            $day = new Kronolith_Day(
118                $startDate->month, $startDate->mday + $i, $startDate->year
119            );
120            if (empty($all_events[$day->dateString()])) {
121                continue;
122            }
123
124            /* Output month header. */
125            if ($current_month != $day->month) {
126                $current_month = $day->strftime('%m');
127                $html .= '<tr><td colspan="4" class="control"><strong>'
128                    . $day->strftime('%B') . '</strong></td></tr>';
129            }
130
131            $firstevent = true;
132            $tomorrow = $day->getTomorrow();
133            foreach ($all_events[$day->dateString()] as $event) {
134
135                if ($event->start->compareDate($day) < 0) {
136                    $event->start = new Horde_Date($day);
137                }
138                if ($event->end->compareDate($tomorrow) >= 0) {
139                    $event->end = $tomorrow;
140                }
141
142                if ($event->end->compareDate($startDate) < 0) {
143                    continue;
144                }
145
146                if ($this->_params['alarms'] && !$event->alarm) {
147                    continue;
148                }
149                if ($firstevent) {
150                    $html .= '<tr><td class="text" valign="top" align="right"><strong>';
151                    if ($day->isToday()) {
152                        $html .= _("Today");
153                    } elseif ($day->isTomorrow()) {
154                        $html .= _("Tomorrow");
155                    } else {
156                        $html .= $day->mday;
157                    }
158                    $html .= '</strong>&nbsp;</td>';
159                    $firstevent = false;
160                } else {
161                    $html .= '<tr><td class="text">&nbsp;</td>';
162                }
163
164                $html .= '<td class="text" nowrap="nowrap" valign="top">';
165                if ($event->start->compareDate($startDate) < 0 &&
166                    $event->end->compareDate($startDate) > 0) {
167                    $html .= '<strong>'
168                        . htmlspecialchars($event->getLocation()) . '</strong>';
169                } else {
170                    $html .= htmlspecialchars($event->getLocation());
171                }
172                if ($event->start->compareDate($startDate) < 0 &&
173                    $event->end->compareDate($startDate) > 0) {
174                    $html .= '<strong>';
175                }
176                $html .= $event->getLink(null, true, null, true);
177                if ($event->start->compareDate($startDate) < 0 &&
178                    $event->end->compareDate($startDate) > 0) {
179                    $html .= '</strong>';
180                }
181                $html .= '</td></tr>';
182            }
183        }
184
185        if (empty($html)) {
186            return '<em>' . _("No events to display") . '</em>';
187        }
188
189        return '<table cellspacing="0" width="100%">' . $html . '</table>';
190    }
191}
192