3 * @file include/event.php
4 * @brief functions specific to event handling
8 use Friendica\Core\System;
10 require_once 'include/bbcode.php';
11 require_once 'include/map.php';
12 require_once 'include/datetime.php';
14 function format_event_html($ev, $simple = false) {
15 if (! ((is_array($ev)) && count($ev))) {
19 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM.
21 $event_start = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
22 $ev['start'] , $bd_format ))
23 : day_translate(datetime_convert('UTC', 'UTC',
24 $ev['start'] , $bd_format)));
26 $event_end = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
27 $ev['finish'] , $bd_format ))
28 : day_translate(datetime_convert('UTC', 'UTC',
29 $ev['finish'] , $bd_format )));
32 $o = "<h3>" . bbcode($ev['summary']) . "</h3>";
34 $o .= "<div>" . bbcode($ev['desc']) . "</div>";
36 $o .= "<h4>" . t('Starts:') . "</h4><p>" . $event_start . "</p>";
38 if (! $ev['nofinish']) {
39 $o .= "<h4>" . t('Finishes:') . "</h4><p>" . $event_end ."</p>";
42 if (strlen($ev['location'])) {
43 $o .= "<h4>" . t('Location:') . "</h4><p>" . $ev['location'] . "</p>";
49 $o = '<div class="vevent">' . "\r\n";
51 $o .= '<div class="summary event-summary">' . bbcode($ev['summary']) . '</div>' . "\r\n";
53 $o .= '<div class="event-start"><span class="event-label">' . t('Starts:') . '</span> <span class="dtstart" title="'
54 . datetime_convert('UTC', 'UTC', $ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
56 . '</span></div>' . "\r\n";
58 if (! $ev['nofinish']) {
59 $o .= '<div class="event-end" ><span class="event-label">' . t('Finishes:') . '</span> <span class="dtend" title="'
60 . datetime_convert('UTC', 'UTC', $ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
62 . '</span></div>' . "\r\n";
65 $o .= '<div class="description event-description">' . bbcode($ev['desc']) . '</div>' . "\r\n";
67 if (strlen($ev['location'])) {
68 $o .= '<div class="event-location"><span class="event-label">' . t('Location:') . '</span> <span class="location">'
69 . bbcode($ev['location'])
70 . '</span></div>' . "\r\n";
72 // Include a map of the location if the [map] BBCode is used.
73 if (strpos($ev['location'], "[map") !== false) {
74 $map = generate_named_map($ev['location']);
75 if ($map !== $ev['location']) {
81 $o .= '</div>' . "\r\n";
86 * @brief Convert an array with event data to bbcode.
88 * @param array $ev Array which conains the event data.
89 * @return string The event as a bbcode formatted string.
91 function format_event_bbcode($ev) {
96 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
100 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
104 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
107 if (($ev['finish']) && (! $ev['nofinish'])) {
108 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
111 if ($ev['location']) {
112 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
116 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
123 * @brief Extract bbcode formatted event data from a string
124 * and convert it to html.
126 * @params: string $s The string which should be parsed for event data.
127 * @return string The html output.
129 function bbtovcal($s) {
134 $o = format_event_html($ev);
141 * @brief Extract bbcode formatted event data from a string.
143 * @params: string $s The string which should be parsed for event data.
144 * @return array The array with the event information.
146 function bbtoevent($s) {
151 if (preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is", $s, $match)) {
152 $ev['summary'] = $match[1];
156 if (preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is", $s, $match)) {
157 $ev['desc'] = $match[1];
161 if (preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is", $s, $match)) {
162 $ev['start'] = $match[1];
166 if (preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is", $s, $match)) {
167 $ev['finish'] = $match[1];
171 if (preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is", $s, $match)) {
172 $ev['location'] = $match[1];
176 if (preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is", $s, $match)) {
177 $ev['adjust'] = $match[1];
180 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
185 function sort_by_date($a) {
187 usort($a,'ev_compare');
191 function ev_compare($a,$b) {
193 $date_a = (($a['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $a['start']) : $a['start']);
194 $date_b = (($b['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $b['start']) : $b['start']);
196 if ($date_a === $date_b) {
197 return strcasecmp($a['desc'], $b['desc']);
200 return strcmp($date_a, $date_b);
204 * @brief Delete an event from the event table.
206 * Note: This function does only delete the event from the event table not its
207 * related entry in the item table.
209 * @param int $event_id Event ID.
212 function event_delete($event_id) {
213 if ($event_id == 0) {
217 q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
218 logger("Deleted event ".$event_id, LOGGER_DEBUG);
222 * @brief Store the event.
224 * Store the event in the event table and create an event item in the item table.
226 * @param array $arr Array with event data.
227 * @return int The event id.
229 function event_store($arr) {
231 require_once 'include/datetime.php';
232 require_once 'include/items.php';
233 require_once 'include/bbcode.php';
237 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
238 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
239 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
240 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
241 $arr['uri'] = (x($arr, 'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(), $arr['uid']));
242 $arr['private'] = ((x($arr, 'private')) ? intval($arr['private']) : 0);
243 $arr['guid'] = get_guid(32);
246 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
251 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
256 if (dbm::is_result($c)) {
261 // Existing event being modified.
265 // has the event actually changed?
267 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
271 if ((! dbm::is_result($r)) || ($r[0]['edited'] === $arr['edited'])) {
273 // Nothing has changed. Grab the item id to return.
275 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
279 return ((dbm::is_result($r)) ? $r[0]['id'] : 0);
282 // The event changed. Update it.
284 $r = q("UPDATE `event` SET
294 WHERE `id` = %d AND `uid` = %d",
296 dbesc($arr['edited']),
297 dbesc($arr['start']),
298 dbesc($arr['finish']),
299 dbesc($arr['summary']),
301 dbesc($arr['location']),
303 intval($arr['adjust']),
304 intval($arr['nofinish']),
308 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
312 if (dbm::is_result($r)) {
313 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
314 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
315 $object .= '</object>' . "\n";
317 q("UPDATE `item` SET `body` = '%s', `object` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d",
318 dbesc(format_event_bbcode($arr)),
320 dbesc($arr['edited']),
325 $item_id = $r[0]['id'];
330 call_hooks("event_updated", $arr['id']);
334 // New event. Store it.
336 $r = q("INSERT INTO `event` (`uid`,`cid`,`guid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
337 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
338 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
343 dbesc($arr['created']),
344 dbesc($arr['edited']),
345 dbesc($arr['start']),
346 dbesc($arr['finish']),
347 dbesc($arr['summary']),
349 dbesc($arr['location']),
351 intval($arr['adjust']),
352 intval($arr['nofinish']),
353 dbesc($arr['allow_cid']),
354 dbesc($arr['allow_gid']),
355 dbesc($arr['deny_cid']),
356 dbesc($arr['deny_gid'])
359 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
363 if (dbm::is_result($r)) {
369 $item_arr['uid'] = $arr['uid'];
370 $item_arr['contact-id'] = $arr['cid'];
371 $item_arr['uri'] = $arr['uri'];
372 $item_arr['parent-uri'] = $arr['uri'];
373 $item_arr['guid'] = $arr['guid'];
374 $item_arr['type'] = 'activity';
375 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
376 $item_arr['contact-id'] = $contact['id'];
377 $item_arr['owner-name'] = $contact['name'];
378 $item_arr['owner-link'] = $contact['url'];
379 $item_arr['owner-avatar'] = $contact['thumb'];
380 $item_arr['author-name'] = $contact['name'];
381 $item_arr['author-link'] = $contact['url'];
382 $item_arr['author-avatar'] = $contact['thumb'];
383 $item_arr['title'] = '';
384 $item_arr['allow_cid'] = $arr['allow_cid'];
385 $item_arr['allow_gid'] = $arr['allow_gid'];
386 $item_arr['deny_cid'] = $arr['deny_cid'];
387 $item_arr['deny_gid'] = $arr['deny_gid'];
388 $item_arr['private'] = $arr['private'];
389 $item_arr['last-child'] = 1;
390 $item_arr['visible'] = 1;
391 $item_arr['verb'] = ACTIVITY_POST;
392 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
393 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
394 $item_arr['body'] = format_event_bbcode($event);
397 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
398 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
399 $item_arr['object'] .= '</object>' . "\n";
401 $item_id = item_store($item_arr);
403 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
406 //if (dbm::is_result($r))
407 // $plink = System::baseUrl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
411 //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d",
413 // intval($event['id']),
414 // intval($arr['uid']),
417 q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
418 intval($event['id']),
424 call_hooks("event_created", $event['id']);
431 * @brief Create an array with translation strings used for events.
433 * @return array Array with translations strings.
435 function get_event_strings() {
437 // First day of the week (0 = Sunday).
438 $firstDay = get_pconfig(local_user(), 'system', 'first_day_of_week');
439 if ($firstDay === false) {
444 "firstDay" => $firstDay,
445 "allday" => t("all-day"),
455 "Sunday" => t("Sunday"),
456 "Monday" => t("Monday"),
457 "Tuesday" => t("Tuesday"),
458 "Wednesday" => t("Wednesday"),
459 "Thursday" => t("Thursday"),
460 "Friday" => t("Friday"),
461 "Saturday" => t("Saturday"),
476 "January" => t("January"),
477 "February" => t("February"),
478 "March" => t("March"),
479 "April" => t("April"),
483 "August" => t("August"),
484 "September" => t("September"),
485 "October" => t("October"),
486 "November" => t("November"),
487 "December" => t("December"),
489 "today" => t("today"),
490 "month" => t("month"),
494 "noevent" => t("No events to display"),
496 "dtstart_label" => t("Starts:"),
497 "dtend_label" => t("Finishes:"),
498 "location_label" => t("Location:")
505 * @brief Removes duplicated birthday events.
507 * @param array $dates Array of possibly duplicated events.
508 * @return array Cleaned events.
510 * @todo We should replace this with a separate update function if there is some time left.
512 function event_remove_duplicates($dates) {
515 foreach ($dates as $date) {
516 if ($date['type'] == 'birthday') {
517 $dates2[$date['uid'] . "-" . $date['cid'] . "-" . $date['start']] = $date;
526 * @brief Get an event by its event ID.
528 * @param type $owner_uid The User ID of the owner of the event
529 * @param type $event_params An assoziative array with
530 * int 'event_id' => The ID of the event in the event table
531 * @param type $sql_extra
532 * @return array Query result
534 function event_by_id($owner_uid = 0, $event_params, $sql_extra = '') {
535 // Ownly allow events if there is a valid owner_id.
536 if ($owner_uid == 0) {
540 // Query for the event by event id
541 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
542 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
543 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
544 WHERE `event`.`uid` = %d AND `event`.`id` = %d $sql_extra",
546 intval($event_params["event_id"])
549 if (dbm::is_result($r)) {
550 return event_remove_duplicates($r);
555 * @brief Get all events in a specific timeframe.
557 * @param int $owner_uid The User ID of the owner of the events.
558 * @param array $event_params An assoziative array with
559 * int 'ignored' =><br>
560 * string 'start' => Start time of the timeframe.<br>
561 * string 'finish' => Finish time of the timeframe.<br>
562 * string 'adjust_start' =><br>
563 * string 'adjust_start' =>
565 * @param string $sql_extra Additional sql conditions (e.g. permission request).
567 * @return array Query results.
569 function events_by_date($owner_uid = 0, $event_params, $sql_extra = '') {
570 // Only allow events if there is a valid owner_id.
571 if ($owner_uid == 0) {
575 // Query for the event by date.
576 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
577 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
578 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
579 WHERE `event`.`uid` = %d AND event.ignore = %d
580 AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
581 OR (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s'))
584 intval($event_params["ignored"]),
585 dbesc($event_params["start"]),
586 dbesc($event_params["start"]),
587 dbesc($event_params["finish"]),
588 dbesc($event_params["adjust_start"]),
589 dbesc($event_params["adjust_start"]),
590 dbesc($event_params["adjust_finish"])
593 if (dbm::is_result($r)) {
594 return event_remove_duplicates($r);
599 * @brief Convert an array query results in an arry which could be used by the events template.
601 * @param array $arr Event query array.
602 * @return array Event array for the template.
604 function process_events($arr) {
610 foreach ($arr as $rr) {
611 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
612 $d = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], $fmt) : datetime_convert('UTC', 'UTC', $rr['start'], $fmt));
613 $d = day_translate($d);
615 $start = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'c') : datetime_convert('UTC', 'UTC', $rr['start'], 'c'));
616 if ($rr['nofinish']) {
619 $end = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['finish'], 'c') : datetime_convert('UTC', 'UTC', $rr['finish'], 'c'));
622 $is_first = ($d !== $last_date);
626 // Show edit and drop actions only if the user is the owner of the event and the event
627 // is a real event (no bithdays).
628 if (local_user() && local_user() == $rr['uid'] && $rr['type'] == 'event') {
629 $edit = ((! $rr['cid']) ? array(System::baseUrl() . '/events/event/' . $rr['id'], t('Edit event'), '', '') : null);
630 $drop = array(System::baseUrl() . '/events/drop/' . $rr['id'], t('Delete event'), '', '');
633 $title = strip_tags(html_entity_decode(bbcode($rr['summary']), ENT_QUOTES, 'UTF-8'));
635 list($title, $_trash) = explode("<br", bbcode($rr['desc']), 2);
636 $title = strip_tags(html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
639 $html = format_event_html($rr);
640 $rr['desc'] = bbcode($rr['desc']);
641 $rr['location'] = bbcode($rr['location']);
653 'is_first' => $is_first,
656 'plink' => array($rr['plink'], t('link to source'), '', ''),
665 * @brief Format event to export format (ical/csv).
667 * @param array $events Query result for events.
668 * @param string $format The output format (ical/csv).
669 * @param string $timezone The timezone of the user (not implemented yet).
671 * @return string Content according to selected export format.
673 function event_format_export ($events, $format = 'ical', $timezone) {
674 if (! ((is_array($events)) && count($events))) {
679 // Format the exported data as a CSV file.
681 header("Content-type: text/csv");
682 $o = '"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "Location"' . PHP_EOL;
684 foreach ($events as $event) {
685 /// @todo The time / date entries don't include any information about the
686 /// timezone the event is scheduled in :-/
687 $tmp1 = strtotime($event['start']);
688 $tmp2 = strtotime($event['finish']);
689 $time_format = "%H:%M:%S";
690 $date_format = "%Y-%m-%d";
692 $o .= '"' . $event['summary'] . '", "' . strftime($date_format, $tmp1) .
693 '", "' . strftime($time_format, $tmp1) . '", "' . $event['desc'] .
694 '", "' . strftime($date_format, $tmp2) .
695 '", "' . strftime($time_format, $tmp2) .
696 '", "' . $event['location'] . '"' . PHP_EOL;
700 // Format the exported data as a ics file.
702 header("Content-type: text/ics");
703 $o = 'BEGIN:VCALENDAR' . PHP_EOL
704 . 'VERSION:2.0' . PHP_EOL
705 . 'PRODID:-//friendica calendar export//0.1//EN' . PHP_EOL;
706 /// @todo include timezone informations in cases were the time is not in UTC
707 // see http://tools.ietf.org/html/rfc2445#section-4.8.3
708 // . 'BEGIN:VTIMEZONE' . PHP_EOL
709 // . 'TZID:' . $timezone . PHP_EOL
710 // . 'END:VTIMEZONE' . PHP_EOL;
711 // TODO instead of PHP_EOL CRLF should be used for long entries
712 // but test your solution against http://icalvalid.cloudapp.net/
713 // also long lines SHOULD be split at 75 characters length
714 foreach ($events as $event) {
715 if ($event['adjust'] == 1) {
720 $o .= 'BEGIN:VEVENT' . PHP_EOL;
722 if ($event['start']) {
723 $tmp = strtotime($event['start']);
724 $dtformat = "%Y%m%dT%H%M%S" . $UTC;
725 $o .= 'DTSTART:' . strftime($dtformat, $tmp) . PHP_EOL;
728 if (!$event['nofinish']) {
729 $tmp = strtotime($event['finish']);
730 $dtformat = "%Y%m%dT%H%M%S" . $UTC;
731 $o .= 'DTEND:' . strftime($dtformat, $tmp) . PHP_EOL;
734 if ($event['summary']) {
735 $tmp = $event['summary'];
736 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
737 $tmp = addcslashes($tmp, ',;');
738 $o .= 'SUMMARY:' . $tmp . PHP_EOL;
741 if ($event['desc']) {
742 $tmp = $event['desc'];
743 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
744 $tmp = addcslashes($tmp, ',;');
745 $o .= 'DESCRIPTION:' . $tmp . PHP_EOL;
748 if ($event['location']) {
749 $tmp = $event['location'];
750 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
751 $tmp = addcslashes($tmp, ',;');
752 $o .= 'LOCATION:' . $tmp . PHP_EOL;
755 $o .= 'END:VEVENT' . PHP_EOL;
759 $o .= 'END:VCALENDAR' . PHP_EOL;
767 * @brief Get all events for a user ID.
769 * The query for events is done permission sensitive.
770 * If the user is the owner of the calendar he/she
771 * will get all of his/her available events.
772 * If the user is only a visitor only the public events will
775 * @param int $uid The user ID.
776 * @param int $sql_extra Additional sql conditions for permission.
778 * @return array Query results.
780 function events_by_uid($uid = 0, $sql_extra = '') {
785 // The permission condition if no condition was transmitted.
786 if ($sql_extra == '') {
787 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' ";
790 // Does the user who requests happen to be the owner of the events
791 // requested? then show all of your events, otherwise only those that
792 // don't have limitations set in allow_cid and allow_gid.
793 if (local_user() == $uid) {
794 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location`, `nofinish`
795 FROM `event` WHERE `uid`= %d AND `cid` = 0 ",
799 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location`, `nofinish`
800 FROM `event` WHERE `uid`= %d AND `cid` = 0 $sql_extra ",
805 if (dbm::is_result($r)) {
812 * @param int $uid The user ID.
813 * @param string $format Output format (ical/csv).
814 * @return array With the results:
815 * bool 'success' => True if the processing was successful,<br>
816 * string 'format' => The output format,<br>
817 * string 'extension' => The file extension of the output format,<br>
818 * string 'content' => The formatted output content.<br>
820 * @todo Respect authenticated users with events_by_uid().
822 function event_export($uid, $format = 'ical') {
826 // We are allowed to show events.
827 // Get the timezone the user is in.
828 $r = q("SELECT `timezone` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
829 if (dbm::is_result($r)) {
830 $timezone = $r[0]['timezone'];
833 // Get all events which are owned by a uid (respects permissions).
834 $events = events_by_uid($uid);
836 // We have the events that are available for the requestor.
837 // Now format the output according to the requested format.
838 if (count($events)) {
839 $res = event_format_export($events, $format, $timezone);
842 // If there are results the precess was successfull.
847 // Get the file extension for the format.
862 'success' => $process,
864 'extension' => $file_ext,
872 * @brief Get the events widget.
874 * @return string Formated html of the evens widget.
876 function widget_events() {
879 $owner_uid = $a->data['user']['uid'];
880 // $a->data is only available if the profile page is visited. If the visited page is not part
881 // of the profile page it should be the personal /events page. So we can use $a->user.
882 $user = ($a->data['user']['nickname'] ? $a->data['user']['nickname'] : $a->user['nickname']);
885 // The permission testing is a little bit tricky because we have to respect many cases.
887 // It's not the private events page (we don't get the $owner_uid for /events).
888 if (! local_user() && ! $owner_uid) {
893 * Cal logged in user (test permission at foreign profile page).
894 * If the $owner uid is available we know it is part of one of the profile pages (like /cal).
895 * So we have to test if if it's the own profile page of the logged in user
896 * or a foreign one. For foreign profile pages we need to check if the feature
897 * for exporting the cal is enabled (otherwise the widget would appear for logged in users
898 * on foreigen profile pages even if the widget is disabled).
900 if (intval($owner_uid) && local_user() !== $owner_uid && ! feature_enabled($owner_uid, "export_calendar")) {
905 * If it's a kind of profile page (intval($owner_uid)) return if the user not logged in and
906 * export feature isn't enabled.
908 if (intval($owner_uid) && ! local_user() && ! feature_enabled($owner_uid, "export_calendar")) {
912 return replace_macros(get_markup_template("events_aside.tpl"), array(
913 '$etitle' => t("Export"),
914 '$export_ical' => t("Export calendar as ical"),
915 '$export_csv' => t("Export calendar as csv"),
921 * @brief Format an item array with event data to HTML.
923 * @param arr $item Array with item and event data.
924 * @return string HTML output.
926 function format_event_item($item) {
930 // Set the different time formats.
931 $dformat = t('l F d, Y \@ g:i A'); // Friday January 18, 2011 @ 8:01 AM.
932 $dformat_short = t('D g:i A'); // Fri 8:01 AM.
933 $tformat = t('g:i A'); // 8:01 AM.
935 // Convert the time to different formats.
936 $dtstart_dt = (($item['event-adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), $item['event-start'], $dformat)) : day_translate(datetime_convert('UTC', 'UTC', $item['event-start'], $dformat)));
937 $dtstart_title = datetime_convert('UTC', 'UTC', $item['event-start'], (($item['event-adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s'));
938 // Format: Jan till Dec.
939 $month_short = (($item['event-adjust']) ? day_short_translate(datetime_convert('UTC', date_default_timezone_get(), $item['event-start'], 'M')) : day_short_translate(datetime_convert('UTC', 'UTC', $item['event-start'], 'M')));
940 // Format: 1 till 31.
941 $date_short = (($item['event-adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $item['event-start'], 'j') : datetime_convert('UTC', 'UTC', $item['event-start'], 'j'));
942 $start_time = (($item['event-adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $item['event-start'], $tformat) : datetime_convert('UTC', 'UTC', $item['event-start'], $tformat));
943 $start_short = (($item['event-adjust']) ? day_short_translate(datetime_convert('UTC', date_default_timezone_get(), $item['event-start'], $dformat_short)) : day_short_translate(datetime_convert('UTC', 'UTC', $item['event-start'], $dformat_short)));
945 // If the option 'nofinisch' isn't set, we need to format the finish date/time.
946 if (!$item['event-nofinish']) {
948 $dtend_dt = (($item['event-adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), $item['event-finish'], $dformat)) : day_translate(datetime_convert('UTC', 'UTC', $item['event-finish'], $dformat)));
949 $dtend_title = datetime_convert('UTC', 'UTC', $item['event-finish'], (($item['event-adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s'));
950 $end_short = (($item['event-adjust']) ? day_short_translate(datetime_convert('UTC', date_default_timezone_get(), $item['event-finish'], $dformat_short)) : day_short_translate(datetime_convert('UTC', 'UTC', $item['event-finish'], $dformat_short)));
951 $end_time = (($item['event-adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $item['event-finish'], $tformat) : datetime_convert('UTC', 'UTC', $item['event-finish'], $tformat));
952 // Check if start and finish time is at the same day.
953 if (substr($dtstart_title, 0, 10) === substr($dtend_title, 0, 10)) {
958 // Format the event location.
959 $evloc = event_location2array($item['event-location']);
961 'name' => prepare_text($evloc['name'])
963 // Construct the map HTML.
964 if (isset($evloc['address'])) {
965 $location['map'] = '<div class="map">' . generate_named_map($evloc['address']) . '</div>';
966 } elseif (isset($evloc['coordinates'])) {
967 $location['map'] = '<div class="map">' . generate_map(str_replace('/', ' ', $evloc['coordinates'])) . '</div>';
970 $event = replace_macros(get_markup_template('event_stream_item.tpl'), array(
971 '$id' => $item['event-id'],
972 '$title' => prepare_text($item['event-summary']),
973 '$dtstart_label' => t('Starts:'),
974 '$dtstart_title' => $dtstart_title,
975 '$dtstart_dt' => $dtstart_dt,
976 '$finish' => $finish,
977 '$dtend_label' => t('Finishes:'),
978 '$dtend_title' => $dtend_title,
979 '$dtend_dt' => $dtend_dt,
980 '$month_short' => $month_short,
981 '$date_short' => $date_short,
982 '$same_date' => $same_date,
983 '$start_time' => $start_time,
984 '$start_short' => $start_short,
985 '$end_time' => $end_time,
986 '$end_short' => $end_short,
987 '$author_name' => $item['author-name'],
988 '$author_link' => $item['author-link'],
989 '$author_avatar' => $item['author-avatar'],
990 '$description' => prepare_text($item['event-desc']),
991 '$location_label' => t('Location:'),
992 '$show_map_label' => t('Show map'),
993 '$hide_map_label' => t('Hide map'),
994 '$map_btn_label' => t('Show map'),
995 '$location' => $location
1002 * @brief Format a string with map bbcode to an array with location data.
1004 * Note: The string must only contain location data. A string with no bbcode will be
1005 * handled as location name.
1007 * @param string $s The string with the bbcode formatted location data.
1009 * @return array The array with the location data.
1010 * 'name' => The name of the location,<br>
1011 * 'address' => The address of the location,<br>
1012 * 'coordinates' => Latitude and longitude (e.g. '48.864716,2.349014').<br>
1014 function event_location2array($s = '') {
1019 $location = array('name' => $s);
1021 // Map tag with location name - e.g. [map]Paris[/map].
1022 if (strpos($s, '[/map]') !== false) {
1023 $found = preg_match("/\[map\](.*?)\[\/map\]/ism", $s, $match);
1024 if (intval($found) > 0 && array_key_exists(1, $match)) {
1025 $location['address'] = $match[1];
1026 // Remove the map bbcode from the location name.
1027 $location['name'] = str_replace($match[0], "", $s);
1029 // Map tag with coordinates - e.g. [map=48.864716,2.349014].
1030 } elseif (strpos($s, '[map=') !== false) {
1031 $found = preg_match("/\[map=(.*?)\]/ism", $s, $match);
1032 if (intval($found) > 0 && array_key_exists(1, $match)) {
1033 $location['coordinates'] = $match[1];
1034 // Remove the map bbcode from the location name.
1035 $location['name'] = str_replace($match[0], "", $s);