1: <?php
2: 3: 4:
5: class Kronolith_Block_Summary extends Horde_Core_Block
6: {
7: 8:
9: public function __construct($app, $params = array())
10: {
11: parent::__construct($app, $params);
12:
13: if (!isset($this->_params['days'])) {
14: $this->_params['days'] = 7;
15: }
16:
17: $this->_name = _("Calendar Summary");
18: }
19:
20: 21:
22: protected function _params()
23: {
24: $params = array(
25: 'calendar' => array(
26: 'name' => _("Calendar"),
27: 'type' => 'enum',
28: 'default' => '__all'
29: ),
30: 'days' => array(
31: 'name' => _("The time span to show"),
32: 'type' => 'enum',
33: 'default' => 7,
34: 'values' => array(
35: 1 => '1 ' . _("day"),
36: 2 => '2 ' . _("days"),
37: 3 => '3 ' . _("days"),
38: 4 => '4 ' . _("days"),
39: 5 => '5 ' . _("days"),
40: 6 => '6 ' . _("days"),
41: 7 => '1 ' . _("week"),
42: 14 => '2 ' . _("weeks"),
43: 21 => '3 ' . _("weeks"),
44: 28 => '4 ' . _("weeks")
45: )
46: ),
47: 'maxevents' => array(
48: 'name' => _("Maximum number of events to display (0 = no limit)"),
49: 'type' => 'int',
50: 'default' => 0
51: ),
52: 'alarms' => array(
53: 'name' => _("Show only events that have an alarm set?"),
54: 'type' => 'checkbox',
55: 'default' => 0
56: )
57: );
58:
59: $params['calendar']['values']['__all'] = _("All Visible");
60: foreach (Kronolith::listCalendars(Horde_Perms::SHOW, true) as $id => $cal) {
61: $params['calendar']['values'][$id] = $cal->name();
62: }
63:
64: return $params;
65: }
66:
67: 68:
69: protected function _title()
70: {
71: $url = Horde::url($GLOBALS['registry']->getInitialPage(), true);
72: if (isset($this->_params['calendar']) &&
73: $this->_params['calendar'] != '__all') {
74: $url->add('display_cal', $this->_params['calendar']);
75: }
76: return $url->link()
77: . htmlspecialchars($GLOBALS['registry']->get('name')) . '</a>';
78: }
79:
80: 81:
82: protected function _content()
83: {
84: Horde::addScriptFile('tooltips.js', 'horde');
85:
86: $now = new Horde_Date($_SERVER['REQUEST_TIME']);
87: $today = date('j');
88:
89: $startDate = new Horde_Date(array('year' => date('Y'), 'month' => date('n'), 'mday' => date('j')));
90: $endDate = new Horde_Date(array('year' => date('Y'), 'month' => date('n'), 'mday' => date('j') + $this->_params['days']));
91:
92: try {
93: if (isset($this->_params['calendar']) &&
94: $this->_params['calendar'] != '__all') {
95: $calendars = Kronolith::listCalendars();
96: if (!isset($calendars[$this->_params['calendar']])) {
97: return _("Calendar not found");
98: }
99: if (!$calendars[$this->_params['calendar']]->hasPermission(Horde_Perms::READ)) {
100: return _("Permission Denied");
101: }
102: list($type, $calendar) = explode('_', $this->_params['calendar'], 2);
103: $driver = Kronolith::getDriver($type, $calendar);
104: $all_events = Kronolith::sortEvents($driver->listEvents($startDate, $endDate, true));
105: } else {
106: $all_events = Kronolith::listEvents($startDate, $endDate,
107: $GLOBALS['display_calendars']);
108: }
109: } catch (Exception $e) {
110: return '<em>' . $e->getMessage() . '</em>';
111: }
112:
113: $html = '';
114: $iMax = $today + $this->_params['days'];
115: $firstday = true;
116: $totalevents = 0;
117: for ($i = $today; $i < $iMax; ++$i) {
118: $day = new Kronolith_Day(date('n'), $i);
119: $date_stamp = $day->dateString();
120: if (empty($all_events[$date_stamp])) {
121: continue;
122: }
123:
124: $firstevent = true;
125: $tomorrow = $day->getTomorrow();
126: foreach ($all_events[$date_stamp] as $event) {
127: if (!empty($this->_params['maxevents']) &&
128: $totalevents >= $this->_params['maxevents']) {
129: break 2;
130: }
131:
132: if ($event->start->compareDate($day) < 0) {
133: $event->start = $day;
134: }
135: if ($event->end->compareDate($tomorrow) >= 0) {
136: $event->end = $tomorrow;
137: }
138: if ($event->end->compareDate($now) < 0) {
139: continue;
140: }
141:
142: if (!empty($this->_params['alarms']) && !$event->alarm) {
143: continue;
144: }
145: $event_active = $event->start->compareDateTime($now) < 0 &&
146: $event->end->compareDateTime($now) > 0;
147:
148: if ($firstevent) {
149: if (!$firstday) {
150: $html .= '<tr><td colspan="3" style="line-height:2px"> </td></tr>';
151: }
152: $html .= '<tr><td colspan="3" class="control"><strong>';
153: if ($day->isToday()) {
154: $dayname = _("Today");
155: } elseif ($day->isTomorrow()) {
156: $dayname = _("Tomorrow");
157: } elseif ($day->diff() < 7) {
158: $dayname = $day->strftime('%A');
159: } else {
160: $dayname = $day->strftime($GLOBALS['prefs']->getValue('date_format'));
161: }
162: $url = Horde::url('day.php', true)
163: ->setRaw(false)
164: ->add('date', $day->dateString());
165: if (isset($this->_params['calendar']) &&
166: $this->_params['calendar'] != '__all') {
167: $url->add('display_cal', $this->_params['calendar']);
168: }
169: $html .= $url->link(array('title' => sprintf(_("Goto %s"), $dayname)))
170: . $dayname . '</a></strong></td></tr>';
171: $firstevent = false;
172: $firstday = false;
173: }
174: $html .= '<tr class="linedRow"><td class="text nowrap" valign="top">';
175: if ($event_active) {
176: $html .= '<strong>';
177: }
178: if ($event->isAllDay()) {
179: $time = _("All day");
180: } else {
181: $time = $event->start->format($GLOBALS['prefs']->getValue('twentyFour') ? 'H:i' : 'h:ia')
182: . '-' . $event->end->format($GLOBALS['prefs']->getValue('twentyFour') ? 'H:i' : 'h:ia');
183: }
184: $html .= $time;
185: if ($event_active) {
186: $html .= '</strong>';
187: }
188: $html .= ' </td>';
189:
190: $html .= '<td class="text" valign="top">';
191: if ($event_active) {
192: $html .= '<strong>';
193: }
194: $html .= ' ' . $event->getLink(null, true, null, true, true);
195: if ($event_active) {
196: $html .= '</strong>';
197: }
198: $html .= '</td></tr>';
199: $totalevents++;
200: }
201: }
202:
203: if (empty($html)) {
204: return '<em>' . _("No events to display") . '</em>';
205: }
206:
207: return '<table cellspacing="0" width="100%">' . $html . '</table>';
208: }
209:
210: }
211: