3 * @file include/event.php
4 * @brief functions specific to event handling
8 use Friendica\Content\Feature;
9 use Friendica\Core\PConfig;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12 use Friendica\Util\Map;
14 require_once 'include/bbcode.php';
15 require_once 'include/datetime.php';
16 require_once 'include/conversation.php';
18 function format_event_html($ev, $simple = false) {
19 if (! ((is_array($ev)) && count($ev))) {
23 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM.
25 $event_start = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
26 $ev['start'] , $bd_format ))
27 : day_translate(datetime_convert('UTC', 'UTC',
28 $ev['start'] , $bd_format)));
30 $event_end = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
31 $ev['finish'] , $bd_format ))
32 : day_translate(datetime_convert('UTC', 'UTC',
33 $ev['finish'] , $bd_format )));
36 $o = "<h3>" . bbcode($ev['summary']) . "</h3>";
38 $o .= "<div>" . bbcode($ev['desc']) . "</div>";
40 $o .= "<h4>" . t('Starts:') . "</h4><p>" . $event_start . "</p>";
42 if (! $ev['nofinish']) {
43 $o .= "<h4>" . t('Finishes:') . "</h4><p>" . $event_end ."</p>";
46 if (strlen($ev['location'])) {
47 $o .= "<h4>" . t('Location:') . "</h4><p>" . $ev['location'] . "</p>";
53 $o = '<div class="vevent">' . "\r\n";
55 $o .= '<div class="summary event-summary">' . bbcode($ev['summary']) . '</div>' . "\r\n";
57 $o .= '<div class="event-start"><span class="event-label">' . t('Starts:') . '</span> <span class="dtstart" title="'
58 . datetime_convert('UTC', 'UTC', $ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
60 . '</span></div>' . "\r\n";
62 if (! $ev['nofinish']) {
63 $o .= '<div class="event-end" ><span class="event-label">' . t('Finishes:') . '</span> <span class="dtend" title="'
64 . datetime_convert('UTC', 'UTC', $ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
66 . '</span></div>' . "\r\n";
69 $o .= '<div class="description event-description">' . bbcode($ev['desc']) . '</div>' . "\r\n";
71 if (strlen($ev['location'])) {
72 $o .= '<div class="event-location"><span class="event-label">' . t('Location:') . '</span> <span class="location">'
73 . bbcode($ev['location'])
74 . '</span></div>' . "\r\n";
76 // Include a map of the location if the [map] BBCode is used.
77 if (strpos($ev['location'], "[map") !== false) {
78 $map = Map::byLocation($ev['location']);
79 if ($map !== $ev['location']) {
85 $o .= '</div>' . "\r\n";
90 * @brief Convert an array with event data to bbcode.
92 * @param array $ev Array which conains the event data.
93 * @return string The event as a bbcode formatted string.
95 function format_event_bbcode($ev) {
100 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
104 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
108 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
111 if (($ev['finish']) && (! $ev['nofinish'])) {
112 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
115 if ($ev['location']) {
116 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
120 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
127 * @brief Extract bbcode formatted event data from a string
128 * and convert it to html.
130 * @params: string $s The string which should be parsed for event data.
131 * @return string The html output.
133 function bbtovcal($s) {
138 $o = format_event_html($ev);
145 * @brief Extract bbcode formatted event data from a string.
147 * @params: string $s The string which should be parsed for event data.
148 * @return array The array with the event information.
150 function bbtoevent($s) {
155 if (preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is", $s, $match)) {
156 $ev['summary'] = $match[1];
160 if (preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is", $s, $match)) {
161 $ev['desc'] = $match[1];
165 if (preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is", $s, $match)) {
166 $ev['start'] = $match[1];
170 if (preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is", $s, $match)) {
171 $ev['finish'] = $match[1];
175 if (preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is", $s, $match)) {
176 $ev['location'] = $match[1];
180 if (preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is", $s, $match)) {
181 $ev['adjust'] = $match[1];
184 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
189 function sort_by_date($a) {
191 usort($a,'ev_compare');
195 function ev_compare($a,$b) {
197 $date_a = (($a['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $a['start']) : $a['start']);
198 $date_b = (($b['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $b['start']) : $b['start']);
200 if ($date_a === $date_b) {
201 return strcasecmp($a['desc'], $b['desc']);
204 return strcmp($date_a, $date_b);
208 * @brief Delete an event from the event table.
210 * Note: This function does only delete the event from the event table not its
211 * related entry in the item table.
213 * @param int $event_id Event ID.
216 function event_delete($event_id) {
217 if ($event_id == 0) {
221 dba::delete('event', array('id' => $event_id));
222 logger("Deleted event ".$event_id, LOGGER_DEBUG);
226 * @brief Store the event.
228 * Store the event in the event table and create an event item in the item table.
230 * @param array $arr Array with event data.
231 * @return int The event id.
233 function event_store($arr) {
235 require_once 'include/datetime.php';
236 require_once 'include/items.php';
237 require_once 'include/bbcode.php';
241 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
242 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
243 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
244 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
245 $arr['uri'] = (x($arr, 'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(), $arr['uid']));
246 $arr['private'] = ((x($arr, 'private')) ? intval($arr['private']) : 0);
247 $arr['guid'] = get_guid(32);
250 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
255 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
260 if (DBM::is_result($c)) {
264 // Existing event being modified.
267 // has the event actually changed?
268 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
272 if ((! DBM::is_result($r)) || ($r[0]['edited'] === $arr['edited'])) {
274 // 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.
283 q("UPDATE `event` SET
293 WHERE `id` = %d AND `uid` = %d",
295 dbesc($arr['edited']),
296 dbesc($arr['start']),
297 dbesc($arr['finish']),
298 dbesc($arr['summary']),
300 dbesc($arr['location']),
302 intval($arr['adjust']),
303 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.
335 q("INSERT INTO `event` (`uid`,`cid`,`guid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
336 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
337 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
342 dbesc($arr['created']),
343 dbesc($arr['edited']),
344 dbesc($arr['start']),
345 dbesc($arr['finish']),
346 dbesc($arr['summary']),
348 dbesc($arr['location']),
350 intval($arr['adjust']),
351 intval($arr['nofinish']),
352 dbesc($arr['allow_cid']),
353 dbesc($arr['allow_gid']),
354 dbesc($arr['deny_cid']),
355 dbesc($arr['deny_gid'])
358 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
362 if (DBM::is_result($r)) {
368 $item_arr['uid'] = $arr['uid'];
369 $item_arr['contact-id'] = $arr['cid'];
370 $item_arr['uri'] = $arr['uri'];
371 $item_arr['parent-uri'] = $arr['uri'];
372 $item_arr['guid'] = $arr['guid'];
373 $item_arr['type'] = 'activity';
374 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
375 $item_arr['contact-id'] = $contact['id'];
376 $item_arr['owner-name'] = $contact['name'];
377 $item_arr['owner-link'] = $contact['url'];
378 $item_arr['owner-avatar'] = $contact['thumb'];
379 $item_arr['author-name'] = $contact['name'];
380 $item_arr['author-link'] = $contact['url'];
381 $item_arr['author-avatar'] = $contact['thumb'];
382 $item_arr['title'] = '';
383 $item_arr['allow_cid'] = $arr['allow_cid'];
384 $item_arr['allow_gid'] = $arr['allow_gid'];
385 $item_arr['deny_cid'] = $arr['deny_cid'];
386 $item_arr['deny_gid'] = $arr['deny_gid'];
387 $item_arr['private'] = $arr['private'];
388 $item_arr['last-child'] = 1;
389 $item_arr['visible'] = 1;
390 $item_arr['verb'] = ACTIVITY_POST;
391 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
392 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
393 $item_arr['body'] = format_event_bbcode($event);
396 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
397 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
398 $item_arr['object'] .= '</object>' . "\n";
400 $item_id = item_store($item_arr);
402 q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
403 intval($event['id']),
409 call_hooks("event_created", $event['id']);
416 * @brief Create an array with translation strings used for events.
418 * @return array Array with translations strings.
420 function get_event_strings() {
422 // First day of the week (0 = Sunday).
423 $firstDay = PConfig::get(local_user(), 'system', 'first_day_of_week', 0);
426 "firstDay" => $firstDay,
427 "allday" => t("all-day"),
437 "Sunday" => t("Sunday"),
438 "Monday" => t("Monday"),
439 "Tuesday" => t("Tuesday"),
440 "Wednesday" => t("Wednesday"),
441 "Thursday" => t("Thursday"),
442 "Friday" => t("Friday"),
443 "Saturday" => t("Saturday"),
458 "January" => t("January"),
459 "February" => t("February"),
460 "March" => t("March"),
461 "April" => t("April"),
465 "August" => t("August"),
466 "September" => t("September"),
467 "October" => t("October"),
468 "November" => t("November"),
469 "December" => t("December"),
471 "today" => t("today"),
472 "month" => t("month"),
476 "noevent" => t("No events to display"),
478 "dtstart_label" => t("Starts:"),
479 "dtend_label" => t("Finishes:"),
480 "location_label" => t("Location:")
487 * @brief Removes duplicated birthday events.
489 * @param array $dates Array of possibly duplicated events.
490 * @return array Cleaned events.
492 * @todo We should replace this with a separate update function if there is some time left.
494 function event_remove_duplicates($dates) {
497 foreach ($dates as $date) {
498 if ($date['type'] == 'birthday') {
499 $dates2[$date['uid'] . "-" . $date['cid'] . "-" . $date['start']] = $date;
508 * @brief Get an event by its event ID.
510 * @param int $owner_uid The User ID of the owner of the event
511 * @param array $event_params An assoziative array with
512 * int 'event_id' => The ID of the event in the event table
513 * @param string $sql_extra
514 * @return array Query result
516 function event_by_id($owner_uid = 0, $event_params, $sql_extra = '') {
517 // Ownly allow events if there is a valid owner_id.
518 if ($owner_uid == 0) {
522 // Query for the event by event id
523 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
524 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
525 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
526 WHERE `event`.`uid` = %d AND `event`.`id` = %d $sql_extra",
528 intval($event_params["event_id"])
531 if (DBM::is_result($r)) {
532 return event_remove_duplicates($r);
537 * @brief Get all events in a specific timeframe.
539 * @param int $owner_uid The User ID of the owner of the events.
540 * @param array $event_params An assoziative array with
541 * int 'ignored' =><br>
542 * string 'start' => Start time of the timeframe.<br>
543 * string 'finish' => Finish time of the timeframe.<br>
544 * string 'adjust_start' =><br>
545 * string 'adjust_start' =>
547 * @param string $sql_extra Additional sql conditions (e.g. permission request).
549 * @return array Query results.
551 function events_by_date($owner_uid = 0, $event_params, $sql_extra = '') {
552 // Only allow events if there is a valid owner_id.
553 if ($owner_uid == 0) {
557 // Query for the event by date.
558 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
559 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
560 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
561 WHERE `event`.`uid` = %d AND event.ignore = %d
562 AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
563 OR (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s'))
566 intval($event_params["ignored"]),
567 dbesc($event_params["start"]),
568 dbesc($event_params["start"]),
569 dbesc($event_params["finish"]),
570 dbesc($event_params["adjust_start"]),
571 dbesc($event_params["adjust_start"]),
572 dbesc($event_params["adjust_finish"])
575 if (DBM::is_result($r)) {
576 return event_remove_duplicates($r);
581 * @brief Convert an array query results in an arry which could be used by the events template.
583 * @param array $arr Event query array.
584 * @return array Event array for the template.
586 function process_events($arr) {
592 foreach ($arr as $rr) {
593 $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
594 $d = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], $fmt) : datetime_convert('UTC', 'UTC', $rr['start'], $fmt));
595 $d = day_translate($d);
597 $start = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'c') : datetime_convert('UTC', 'UTC', $rr['start'], 'c'));
598 if ($rr['nofinish']) {
601 $end = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['finish'], 'c') : datetime_convert('UTC', 'UTC', $rr['finish'], 'c'));
604 $is_first = ($d !== $last_date);
608 // Show edit and drop actions only if the user is the owner of the event and the event
609 // is a real event (no bithdays).
613 if (local_user() && local_user() == $rr['uid'] && $rr['type'] == 'event') {
614 $edit = ((! $rr['cid']) ? array(System::baseUrl() . '/events/event/' . $rr['id'], t('Edit event'), '', '') : null);
615 $copy = ((! $rr['cid']) ? array(System::baseUrl() . '/events/copy/' . $rr['id'], t('Duplicate event'), '', '') : null);
616 $drop = array(System::baseUrl() . '/events/drop/' . $rr['id'], t('Delete event'), '', '');
619 $title = strip_tags(html_entity_decode(bbcode($rr['summary']), ENT_QUOTES, 'UTF-8'));
621 list($title, $_trash) = explode("<br", bbcode($rr['desc']), 2);
622 $title = strip_tags(html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
625 $html = format_event_html($rr);
626 $rr['desc'] = bbcode($rr['desc']);
627 $rr['location'] = bbcode($rr['location']);
640 'is_first' => $is_first,
643 'plink' => array($rr['plink'], t('link to source'), '', ''),
652 * @brief Format event to export format (ical/csv).
654 * @param array $events Query result for events.
655 * @param string $format The output format (ical/csv).
656 * @param string $timezone The timezone of the user (not implemented yet).
658 * @return string Content according to selected export format.
660 * @todo Implement timezone support
662 function event_format_export($events, $format = 'ical', $timezone)
664 if (!((is_array($events)) && count($events))) {
669 // Format the exported data as a CSV file.
671 header("Content-type: text/csv");
672 $o = '"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "Location"' . PHP_EOL;
674 foreach ($events as $event) {
675 /// @todo The time / date entries don't include any information about the
676 /// timezone the event is scheduled in :-/
677 $tmp1 = strtotime($event['start']);
678 $tmp2 = strtotime($event['finish']);
679 $time_format = "%H:%M:%S";
680 $date_format = "%Y-%m-%d";
682 $o .= '"' . $event['summary'] . '", "' . strftime($date_format, $tmp1) .
683 '", "' . strftime($time_format, $tmp1) . '", "' . $event['desc'] .
684 '", "' . strftime($date_format, $tmp2) .
685 '", "' . strftime($time_format, $tmp2) .
686 '", "' . $event['location'] . '"' . PHP_EOL;
690 // Format the exported data as a ics file.
692 header("Content-type: text/ics");
693 $o = 'BEGIN:VCALENDAR' . PHP_EOL
694 . 'VERSION:2.0' . PHP_EOL
695 . 'PRODID:-//friendica calendar export//0.1//EN' . PHP_EOL;
696 /// @todo include timezone informations in cases were the time is not in UTC
697 // see http://tools.ietf.org/html/rfc2445#section-4.8.3
698 // . 'BEGIN:VTIMEZONE' . PHP_EOL
699 // . 'TZID:' . $timezone . PHP_EOL
700 // . 'END:VTIMEZONE' . PHP_EOL;
701 // TODO instead of PHP_EOL CRLF should be used for long entries
702 // but test your solution against http://icalvalid.cloudapp.net/
703 // also long lines SHOULD be split at 75 characters length
704 foreach ($events as $event) {
705 if ($event['adjust'] == 1) {
710 $o .= 'BEGIN:VEVENT' . PHP_EOL;
712 if ($event['start']) {
713 $tmp = strtotime($event['start']);
714 $dtformat = "%Y%m%dT%H%M%S" . $UTC;
715 $o .= 'DTSTART:' . strftime($dtformat, $tmp) . PHP_EOL;
718 if (!$event['nofinish']) {
719 $tmp = strtotime($event['finish']);
720 $dtformat = "%Y%m%dT%H%M%S" . $UTC;
721 $o .= 'DTEND:' . strftime($dtformat, $tmp) . PHP_EOL;
724 if ($event['summary']) {
725 $tmp = $event['summary'];
726 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
727 $tmp = addcslashes($tmp, ',;');
728 $o .= 'SUMMARY:' . $tmp . PHP_EOL;
731 if ($event['desc']) {
732 $tmp = $event['desc'];
733 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
734 $tmp = addcslashes($tmp, ',;');
735 $o .= 'DESCRIPTION:' . $tmp . PHP_EOL;
738 if ($event['location']) {
739 $tmp = $event['location'];
740 $tmp = str_replace(PHP_EOL, PHP_EOL . ' ', $tmp);
741 $tmp = addcslashes($tmp, ',;');
742 $o .= 'LOCATION:' . $tmp . PHP_EOL;
745 $o .= 'END:VEVENT' . PHP_EOL;
749 $o .= 'END:VCALENDAR' . PHP_EOL;
757 * @brief Get all events for a user ID.
759 * The query for events is done permission sensitive.
760 * If the user is the owner of the calendar he/she
761 * will get all of his/her available events.
762 * If the user is only a visitor only the public events will
765 * @param int $uid The user ID.
766 * @param int $sql_extra Additional sql conditions for permission.
768 * @return array Query results.
770 function events_by_uid($uid = 0, $sql_extra = '') {
775 // The permission condition if no condition was transmitted.
776 if ($sql_extra == '') {
777 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' ";
780 // Does the user who requests happen to be the owner of the events
781 // requested? then show all of your events, otherwise only those that
782 // don't have limitations set in allow_cid and allow_gid.
783 if (local_user() == $uid) {
784 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location`, `nofinish`
785 FROM `event` WHERE `uid`= %d AND `cid` = 0 ",
789 $r = q("SELECT `start`, `finish`, `adjust`, `summary`, `desc`, `location`, `nofinish`
790 FROM `event` WHERE `uid`= %d AND `cid` = 0 $sql_extra ",
795 if (DBM::is_result($r)) {
802 * @param int $uid The user ID.
803 * @param string $format Output format (ical/csv).
804 * @return array With the results:
805 * bool 'success' => True if the processing was successful,<br>
806 * string 'format' => The output format,<br>
807 * string 'extension' => The file extension of the output format,<br>
808 * string 'content' => The formatted output content.<br>
810 * @todo Respect authenticated users with events_by_uid().
812 function event_export($uid, $format = 'ical') {
816 // We are allowed to show events.
817 // Get the timezone the user is in.
818 $r = q("SELECT `timezone` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
819 if (DBM::is_result($r)) {
820 $timezone = $r[0]['timezone'];
823 // Get all events which are owned by a uid (respects permissions).
824 $events = events_by_uid($uid);
826 // We have the events that are available for the requestor.
827 // Now format the output according to the requested format.
828 if (count($events)) {
829 $res = event_format_export($events, $format, $timezone);
832 // If there are results the precess was successfull.
837 // Get the file extension for the format.
852 'success' => $process,
854 'extension' => $file_ext,
862 * @brief Get the events widget.
864 * @return string Formated html of the evens widget.
866 function widget_events() {
869 $owner_uid = $a->data['user']['uid'];
870 // $a->data is only available if the profile page is visited. If the visited page is not part
871 // of the profile page it should be the personal /events page. So we can use $a->user.
872 $user = ($a->data['user']['nickname'] ? $a->data['user']['nickname'] : $a->user['nickname']);
875 // The permission testing is a little bit tricky because we have to respect many cases.
877 // It's not the private events page (we don't get the $owner_uid for /events).
878 if (! local_user() && ! $owner_uid) {
883 * Cal logged in user (test permission at foreign profile page).
884 * If the $owner uid is available we know it is part of one of the profile pages (like /cal).
885 * So we have to test if if it's the own profile page of the logged in user
886 * or a foreign one. For foreign profile pages we need to check if the feature
887 * for exporting the cal is enabled (otherwise the widget would appear for logged in users
888 * on foreigen profile pages even if the widget is disabled).
890 if (intval($owner_uid) && local_user() !== $owner_uid && ! Feature::isEnabled($owner_uid, "export_calendar")) {
895 * If it's a kind of profile page (intval($owner_uid)) return if the user not logged in and
896 * export feature isn't enabled.
898 if (intval($owner_uid) && ! local_user() && ! Feature::isEnabled($owner_uid, "export_calendar")) {
902 return replace_macros(get_markup_template("events_aside.tpl"), array(
903 '$etitle' => t("Export"),
904 '$export_ical' => t("Export calendar as ical"),
905 '$export_csv' => t("Export calendar as csv"),
911 * @brief Format an item array with event data to HTML.
913 * @param arr $item Array with item and event data.
914 * @return string HTML output.
916 function format_event_item($item) {
920 // Set the different time formats.
921 $dformat = t('l F d, Y \@ g:i A'); // Friday January 18, 2011 @ 8:01 AM.
922 $dformat_short = t('D g:i A'); // Fri 8:01 AM.
923 $tformat = t('g:i A'); // 8:01 AM.
925 // Convert the time to different formats.
926 $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)));
927 $dtstart_title = datetime_convert('UTC', 'UTC', $item['event-start'], (($item['event-adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s'));
928 // Format: Jan till Dec.
929 $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')));
930 // Format: 1 till 31.
931 $date_short = (($item['event-adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $item['event-start'], 'j') : datetime_convert('UTC', 'UTC', $item['event-start'], 'j'));
932 $start_time = (($item['event-adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $item['event-start'], $tformat) : datetime_convert('UTC', 'UTC', $item['event-start'], $tformat));
933 $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)));
935 // If the option 'nofinisch' isn't set, we need to format the finish date/time.
936 if (!$item['event-nofinish']) {
938 $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)));
939 $dtend_title = datetime_convert('UTC', 'UTC', $item['event-finish'], (($item['event-adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s'));
940 $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)));
941 $end_time = (($item['event-adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $item['event-finish'], $tformat) : datetime_convert('UTC', 'UTC', $item['event-finish'], $tformat));
942 // Check if start and finish time is at the same day.
943 if (substr($dtstart_title, 0, 10) === substr($dtend_title, 0, 10)) {
948 // Format the event location.
949 $evloc = event_location2array($item['event-location']);
952 if (isset($evloc['name'])) {
953 $location['name'] = prepare_text($evloc['name']);
955 // Construct the map HTML.
956 if (isset($evloc['address'])) {
957 $location['map'] = '<div class="map">' . Map::byLocation($evloc['address']) . '</div>';
958 } elseif (isset($evloc['coordinates'])) {
959 $location['map'] = '<div class="map">' . Map::byCoordinates(str_replace('/', ' ', $evloc['coordinates'])) . '</div>';
962 // Construct the profile link (magic-auth).
964 $profile_link = best_link_url($item, $sp);
967 $profile_link = zrl($profile_link);
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' => $profile_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);