3 * @file include/event.php
4 * @brief functions specific to event handling
7 require_once 'include/bbcode.php';
8 require_once 'include/map.php';
9 require_once 'include/datetime.php';
11 function format_event_html($ev, $simple = false) {
13 if (! ((is_array($ev)) && count($ev))) {
17 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
19 $event_start = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
20 $ev['start'] , $bd_format ))
21 : day_translate(datetime_convert('UTC', 'UTC',
22 $ev['start'] , $bd_format)));
24 $event_end = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
25 $ev['finish'] , $bd_format ))
26 : day_translate(datetime_convert('UTC', 'UTC',
27 $ev['finish'] , $bd_format )));
30 $o = "<h3>" . bbcode($ev['summary']) . "</h3>";
32 $o .= "<p>" . bbcode($ev['desc']) . "</p>";
34 $o .= "<h4>" . t('Starts:') . "</h4><p>" . $event_start . "</p>";
36 if (! $ev['nofinish']) {
37 $o .= "<h4>" . t('Finishes:') . "</h4><p>" . $event_end ."</p>";
40 if (strlen($ev['location'])) {
41 $o .= "<h4>" . t('Location:') . "</h4><p>" . $ev['location'] . "</p>";
47 $o = '<div class="vevent">' . "\r\n";
50 $o .= '<p class="summary event-summary">' . bbcode($ev['summary']) . '</p>' . "\r\n";
52 $o .= '<p class="description event-description">' . bbcode($ev['desc']) . '</p>' . "\r\n";
54 $o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
55 . datetime_convert('UTC', 'UTC', $ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
57 . '</abbr></p>' . "\r\n";
59 if (! $ev['nofinish']) {
60 $o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
61 . datetime_convert('UTC', 'UTC', $ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
63 . '</abbr></p>' . "\r\n";
66 if (strlen($ev['location'])) {
67 $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">'
68 . bbcode($ev['location'])
69 . '</span></p>' . "\r\n";
71 // Include a map of the location if the [map] BBCode is used
72 if (strpos($ev['location'], "[map") !== false) {
73 $map = generate_named_map($ev['location']);
74 if ($map !== $ev['location']) {
80 $o .= '</div>' . "\r\n";
85 function parse_event($h) {
87 require_once('include/Scrape.php');
88 require_once('include/html2bbcode');
90 $h = '<html><body>' . $h . '</body></html>';
96 $dom = HTML5_Parser::parse($h);
97 } catch (DOMException $e) {
98 logger('parse_event: parse error: ' . $e);
104 $items = $dom->getElementsByTagName('*');
106 foreach ($items as $item) {
107 if (attribute_contains($item->getAttribute('class'), 'vevent')) {
108 $level2 = $item->getElementsByTagName('*');
109 foreach ($level2 as $x) {
110 if (attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
111 $ret['start'] = $x->getAttribute('title');
112 if (! strpos($ret['start'],'Z'))
113 $ret['adjust'] = true;
115 if (attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
116 $ret['finish'] = $x->getAttribute('title');
118 if (attribute_contains($x->getAttribute('class'),'description'))
119 $ret['desc'] = $x->textContent;
120 if (attribute_contains($x->getAttribute('class'),'location'))
121 $ret['location'] = $x->textContent;
128 if ((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
129 $config = HTMLPurifier_Config::createDefault();
130 $config->set('Cache.DefinitionImpl', null);
131 $purifier = new HTMLPurifier($config);
132 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
135 if ((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
136 $config = HTMLPurifier_Config::createDefault();
137 $config->set('Cache.DefinitionImpl', null);
138 $purifier = new HTMLPurifier($config);
139 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
143 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
144 if (x($ret,'finish'))
145 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
151 function format_event_bbcode($ev) {
155 if ($ev['summary']) {
156 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
160 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
164 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
167 if (($ev['finish']) && (! $ev['nofinish'])) {
168 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
171 if ($ev['location']) {
172 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
176 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
182 function bbtovcal($s) {
187 $o = format_event_html($ev);
193 function bbtoevent($s) {
198 if (preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is", $s, $match)) {
199 $ev['summary'] = $match[1];
203 if (preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is", $s, $match)) {
204 $ev['desc'] = $match[1];
208 if (preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is", $s, $match)) {
209 $ev['start'] = $match[1];
213 if (preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is", $s, $match)) {
214 $ev['finish'] = $match[1];
218 if (preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is", $s, $match)) {
219 $ev['location'] = $match[1];
223 if (preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is", $s, $match)) {
224 $ev['adjust'] = $match[1];
227 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
233 function sort_by_date($a) {
235 usort($a,'ev_compare');
239 function ev_compare($a,$b) {
241 $date_a = (($a['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $a['start']) : $a['start']);
242 $date_b = (($b['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $b['start']) : $b['start']);
244 if ($date_a === $date_b) {
245 return strcasecmp($a['desc'], $b['desc']);
248 return strcmp($date_a, $date_b);
251 function event_delete($event_id) {
252 if ($event_id == 0) {
256 q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
257 logger("Deleted event ".$event_id, LOGGER_DEBUG);
260 function event_store($arr) {
262 require_once 'include/datetime.php';
263 require_once 'include/items.php';
264 require_once 'include/bbcode.php';
268 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
269 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
270 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
271 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
272 $arr['uri'] = (x($arr, 'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(), $arr['uid']));
273 $arr['private'] = ((x($arr, 'private')) ? intval($arr['private']) : 0);
274 $arr['guid'] = get_guid(32);
277 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
282 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
287 if (dbm::is_result($c)) {
292 // Existing event being modified
296 // has the event actually changed?
298 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
302 if ((! dbm::is_result($r)) || ($r[0]['edited'] === $arr['edited'])) {
304 // Nothing has changed. Grab the item id to return.
306 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
310 return ((dbm::is_result($r)) ? $r[0]['id'] : 0);
313 // The event changed. Update it.
315 $r = q("UPDATE `event` SET
325 WHERE `id` = %d AND `uid` = %d",
327 dbesc($arr['edited']),
328 dbesc($arr['start']),
329 dbesc($arr['finish']),
330 dbesc($arr['summary']),
332 dbesc($arr['location']),
334 intval($arr['adjust']),
335 intval($arr['nofinish']),
339 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
343 if (dbm::is_result($r)) {
344 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
345 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
346 $object .= '</object>' . "\n";
348 q("UPDATE `item` SET `body` = '%s', `object` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d",
349 dbesc(format_event_bbcode($arr)),
351 dbesc($arr['edited']),
356 $item_id = $r[0]['id'];
361 call_hooks("event_updated", $arr['id']);
366 // New event. Store it.
368 $r = q("INSERT INTO `event` (`uid`,`cid`,`guid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
369 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
370 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
375 dbesc($arr['created']),
376 dbesc($arr['edited']),
377 dbesc($arr['start']),
378 dbesc($arr['finish']),
379 dbesc($arr['summary']),
381 dbesc($arr['location']),
383 intval($arr['adjust']),
384 intval($arr['nofinish']),
385 dbesc($arr['allow_cid']),
386 dbesc($arr['allow_gid']),
387 dbesc($arr['deny_cid']),
388 dbesc($arr['deny_gid'])
392 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
396 if (dbm::is_result($r)) {
402 $item_arr['uid'] = $arr['uid'];
403 $item_arr['contact-id'] = $arr['cid'];
404 $item_arr['uri'] = $arr['uri'];
405 $item_arr['parent-uri'] = $arr['uri'];
406 $item_arr['guid'] = $arr['guid'];
407 $item_arr['type'] = 'activity';
408 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
409 $item_arr['contact-id'] = $contact['id'];
410 $item_arr['owner-name'] = $contact['name'];
411 $item_arr['owner-link'] = $contact['url'];
412 $item_arr['owner-avatar'] = $contact['thumb'];
413 $item_arr['author-name'] = $contact['name'];
414 $item_arr['author-link'] = $contact['url'];
415 $item_arr['author-avatar'] = $contact['thumb'];
416 $item_arr['title'] = '';
417 $item_arr['allow_cid'] = $arr['allow_cid'];
418 $item_arr['allow_gid'] = $arr['allow_gid'];
419 $item_arr['deny_cid'] = $arr['deny_cid'];
420 $item_arr['deny_gid'] = $arr['deny_gid'];
421 $item_arr['private'] = $arr['private'];
422 $item_arr['last-child'] = 1;
423 $item_arr['visible'] = 1;
424 $item_arr['verb'] = ACTIVITY_POST;
425 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
426 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
427 $item_arr['body'] = format_event_bbcode($event);
430 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
431 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
432 $item_arr['object'] .= '</object>' . "\n";
434 $item_id = item_store($item_arr);
436 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
439 //if (dbm::is_result($r))
440 // $plink = App::get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
444 //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d",
446 // intval($event['id']),
447 // intval($arr['uid']),
450 q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
451 intval($event['id']),
457 call_hooks("event_created", $event['id']);
463 function get_event_strings() {
465 // First day of the week (0 = Sunday)
466 $firstDay = get_pconfig(local_user(), 'system', 'first_day_of_week');
467 if ($firstDay === false) {
472 "firstDay" => $firstDay,
473 "allday" => t("all-day"),
483 "Sunday" => t("Sunday"),
484 "Monday" => t("Monday"),
485 "Tuesday" => t("Tuesday"),
486 "Wednesday" => t("Wednesday"),
487 "Thursday" => t("Thursday"),
488 "Friday" => t("Friday"),
489 "Saturday" => t("Saturday"),
504 "January" => t("January"),
505 "February" => t("February"),
506 "March" => t("March"),
507 "April" => t("April"),
511 "August" => t("August"),
512 "September" => t("September"),
513 "October" => t("October"),
514 "November" => t("November"),
515 "December" => t("December"),
517 "today" => t("today"),
518 "month" => t("month"),
522 "noevent" => t("No events to display"),
524 "dtstart_label" => t("Starts:"),
525 "dtend_label" => t("Finishes:"),
526 "location_label" => t("Location:")
533 * @brief Removes duplicated birthday events
535 * @param array $dates Array of possibly duplicated events
536 * @return array Cleaned events
538 * @todo We should replace this with a separate update function if there is some time left
540 function event_remove_duplicates($dates) {
543 foreach ($dates AS $date) {
544 if ($date['type'] == 'birthday') {
545 $dates2[$date['uid'] . "-" . $date['cid'] . "-" . $date['start']] = $date;
554 * @brief Get an event by its event ID
556 * @param type $owner_uid The User ID of the owner of the event
557 * @param type $event_params An assoziative array with
558 * int 'event_id' => The ID of the event in the event table
559 * @param type $sql_extra
560 * @return array Query result
562 function event_by_id($owner_uid = 0, $event_params, $sql_extra = '') {
563 // ownly allow events if there is a valid owner_id
564 if ($owner_uid == 0) {
568 // Query for the event by event id
569 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
570 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
571 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
572 WHERE `event`.`uid` = %d AND `event`.`id` = %d $sql_extra",
574 intval($event_params["event_id"])
577 if (dbm::is_result($r)) {
578 return event_remove_duplicates($r);
583 * @brief Get all events in a specific timeframe
585 * @param int $owner_uid The User ID of the owner of the events
586 * @param array $event_params An assoziative array with
588 * string 'start' => Start time of the timeframe
589 * string 'finish' => Finish time of the timeframe
590 * string 'adjust_start' =>
591 * string 'adjust_start' =>
593 * @param string $sql_extra Additional sql conditions (e.g. permission request)
594 * @return array Query results
596 function events_by_date($owner_uid = 0, $event_params, $sql_extra = '') {
597 // Only allow events if there is a valid owner_id
598 if ($owner_uid == 0) {
602 // Query for the event by date
603 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
604 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
605 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
606 WHERE `event`.`uid` = %d AND event.ignore = %d
607 AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
608 OR (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s'))
611 intval($event_params["ignored"]),
612 dbesc($event_params["start"]),
613 dbesc($event_params["start"]),
614 dbesc($event_params["finish"]),
615 dbesc($event_params["adjust_start"]),
616 dbesc($event_params["adjust_start"]),
617 dbesc($event_params["adjust_finish"])
620 if (dbm::is_result($r)) {
621 return event_remove_duplicates($r);
626 * @brief Convert an array query results in an arry which could be used by the events template
628 * @param array $arr Event query array
629 * @return array Event array for the template
631 function process_events($arr) {
637 foreach ($arr as $rr) {
639 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
640 $d = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], $fmt) : datetime_convert('UTC', 'UTC', $rr['start'], $fmt));
641 $d = day_translate($d);
643 $start = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'c') : datetime_convert('UTC', 'UTC', $rr['start'], 'c'));
644 if ($rr['nofinish']) {
647 $end = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['finish'], 'c') : datetime_convert('UTC', 'UTC', $rr['finish'], 'c'));
650 $is_first = ($d !== $last_date);
654 // Show edit and drop actions only if the user is the owner of the event and the event
655 // is a real event (no bithdays)
656 if (local_user() && local_user() == $rr['uid'] && $rr['type'] == 'event') {
657 $edit = ((! $rr['cid']) ? array(App::get_baseurl() . '/events/event/' . $rr['id'], t('Edit event'), '', '') : null);
658 $drop = array(App::get_baseurl() . '/events/drop/' . $rr['id'], t('Delete event'), '', '');
661 $title = strip_tags(html_entity_decode(bbcode($rr['summary']), ENT_QUOTES, 'UTF-8'));
663 list($title, $_trash) = explode("<br", bbcode($rr['desc']), 2);
664 $title = strip_tags(html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
667 $html = format_event_html($rr);
668 $rr['desc'] = bbcode($rr['desc']);
669 $rr['location'] = bbcode($rr['location']);
681 'is_first' => $is_first,
684 'plink' => array($rr['plink'], t('link to source'), '', ''),
693 * @brief Format event to export format (ical/csv)
695 * @param array $events Query result for events
696 * @param string $format The output format (ical/csv)
697 * @param string $timezone The timezone of the user (not implemented yet)
699 * @return string Content according to selected export format
701 function event_format_export ($events, $format = 'ical', $timezone) {
702 if (! ((is_array($events)) && count($events))) {
707 // Format the exported data as a CSV file
709 header("Content-type: text/csv");
710 $o = '"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "Location"' . PHP_EOL;
712 foreach ($events as $event) {
713 /// @todo The time / date entries don't include any information about the
714 /// timezone the event is scheduled in :-/
715 $tmp1 = strtotime($event['start']);
716 $tmp2 = strtotime($event['finish']);
717 $time_format = "%H:%M:%S";
718 $date_format = "%Y-%m-%d";
720 $o .= '"' . $event['summary'] . '", "' . strftime($date_format, $tmp1) .
721 '", "' . strftime($time_format, $tmp1) . '", "' . $event['desc'] .
722 '", "' . strftime($date_format, $tmp2) .
723 '", "' . strftime($time_format, $tmp2) .
724 '", "' . $event['location'] . '"' . PHP_EOL;
728 // Format the exported data as a ics file
730 header("Content-type: text/ics");
731 $o = 'BEGIN:VCALENDAR' . PHP_EOL
732 . 'VERSION:2.0' . PHP_EOL
733 . 'PRODID:-//friendica calendar export//0.1//EN' . PHP_EOL;
734 /// @todo include timezone informations in cases were the time is not in UTC
735 // see http://tools.ietf.org/html/rfc2445#section-4.8.3
736 // . 'BEGIN:VTIMEZONE' . PHP_EOL
737 // . 'TZID:' . $timezone . PHP_EOL
738 // . 'END:VTIMEZONE' . PHP_EOL;
739 // TODO instead of PHP_EOL CRLF should be used for long entries
740 // but test your solution against http://icalvalid.cloudapp.net/
741 // also long lines SHOULD be split at 75 characters length
742 foreach ($events as $event) {
744 if ($event['adjust'] == 1) {
749 $o .= 'BEGIN:VEVENT' . PHP_EOL;
751 if ($event['start']) {
752 $tmp = strtotime($event['start']);
753 $dtformat = "%Y%m%dT%H%M%S" . $UTC;
754 $o .= 'DTSTART:' . strftime($dtformat, $tmp) . PHP_EOL;
757 if (!$event['nofinish']) {
758 $tmp = strtotime($event['finish']);
759 $dtformat = "%Y%m%dT%H%M%S" . $UTC;
760 $o .= 'DTEND:' . strftime($dtformat, $tmp) . PHP_EOL;
763 if ($event['summary']) {
764 $tmp = $event['summary'];
765 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
766 $tmp = addcslashes($tmp, ',;');
767 $o .= 'SUMMARY:' . $tmp . PHP_EOL;
770 if ($event['desc']) {
771 $tmp = $event['desc'];
772 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
773 $tmp = addcslashes($tmp, ',;');
774 $o .= 'DESCRIPTION:' . $tmp . PHP_EOL;
777 if ($event['location']) {
778 $tmp = $event['location'];
779 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
780 $tmp = addcslashes($tmp, ',;');
781 $o .= 'LOCATION:' . $tmp . PHP_EOL;
784 $o .= 'END:VEVENT' . PHP_EOL;
788 $o .= 'END:VCALENDAR' . PHP_EOL;
796 * @brief Get all events for a user ID
798 * The query for events is done permission sensitive
799 * If the user is the owner of the calendar he/she
800 * will get all of his/her available events.
801 * If the user is only a visitor only the public events will
804 * @param int $uid The user ID
805 * @param int $sql_extra Additional sql conditions for permission
807 * @return array Query results
809 function events_by_uid($uid = 0, $sql_extra = '') {
814 // The permission condition if no condition was transmitted
815 if ($sql_extra == '') {
816 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' ";
819 // Does the user who requests happen to be the owner of the events
820 // requested? then show all of your events, otherwise only those that
821 // don't have limitations set in allow_cid and allow_gid
822 if (local_user() == $uid) {
823 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location`, `nofinish`
824 FROM `event` WHERE `uid`= %d AND `cid` = 0 ",
828 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location`, `nofinish`
829 FROM `event` WHERE `uid`= %d AND `cid` = 0 $sql_extra ",
834 if (dbm::is_result($r)) {
841 * @param int $uid The user ID
842 * @param string $format Output format (ical/csv)
843 * @return array With the results
844 * bool 'success' => True if the processing was successful
845 * string 'format' => The output format
846 * string 'extension' => The file extension of the output format
847 * string 'content' => The formatted output content
849 * @todo Respect authenticated users with events_by_uid()
851 function event_export($uid, $format = 'ical') {
855 // We are allowed to show events
856 // get the timezone the user is in
857 $r = q("SELECT `timezone` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
858 if (dbm::is_result($r)) {
859 $timezone = $r[0]['timezone'];
862 // Get all events which are owned by a uid (respects permissions);
863 $events = events_by_uid($uid);
865 // We have the events that are available for the requestor
866 // now format the output according to the requested format
867 if (count($events)) {
868 $res = event_format_export($events, $format, $timezone);
871 // If there are results the precess was successfull
876 // Get the file extension for the format
891 'success' => $process,
893 'extension' => $file_ext,
901 * @brief Get the events widget
903 * @return string Formated html of the evens widget
905 function widget_events() {
908 $owner_uid = $a->data['user']['uid'];
909 // $a->data is only available if the profile page is visited. If the visited page is not part
910 // of the profile page it should be the personal /events page. So we can use $a->user
911 $user = ($a->data['user']['nickname'] ? $a->data['user']['nickname'] : $a->user['nickname']);
914 // The permission testing is a little bit tricky because we have to respect many cases
916 // It's not the private events page (we don't get the $owner_uid for /events)
917 if (! local_user() && ! $owner_uid) {
921 // Cal logged in user (test permission at foreign profile page)
922 // If the $owner uid is available we know it is part of one of the profile pages (like /cal)
923 // So we have to test if if it's the own profile page of the logged in user
924 // or a foreign one. For foreign profile pages we need to check if the feature
925 // for exporting the cal is enabled (otherwise the widget would appear for logged in users
926 // on foreigen profile pages even if the widget is disabled)
927 if (intval($owner_uid) && local_user() !== $owner_uid && ! feature_enabled($owner_uid, "export_calendar")) {
931 // If it's a kind of profile page (intval($owner_uid)) return if the user not logged in and
932 // export feature isn't enabled
933 if (intval($owner_uid) && ! local_user() && ! feature_enabled($owner_uid, "export_calendar")) {
937 return replace_macros(get_markup_template("events_aside.tpl"), array(
938 '$etitle' => t("Export"),
939 '$export_ical' => t("Export calendar as ical"),
940 '$export_csv' => t("Export calendar as csv"),