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) {
15 if(! ((is_array($ev)) && count($ev)))
18 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
20 $event_start = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
21 $ev['start'] , $bd_format ))
22 : day_translate(datetime_convert('UTC', 'UTC',
23 $ev['start'] , $bd_format)));
25 $event_end = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
26 $ev['finish'] , $bd_format ))
27 : day_translate(datetime_convert('UTC', 'UTC',
28 $ev['finish'] , $bd_format )));
31 $o = "<h3>".bbcode($ev['summary'])."</h3>";
33 $o .= "<p>".bbcode($ev['desc'])."</p>";
35 $o .= "<h4>".t('Starts:')."</h4><p>".$event_start."</p>";
38 $o .= "<h4>".t('Finishes:')."</h4><p>".$event_end."</p>";
40 if(strlen($ev['location']))
41 $o .= "<h4>".t('Location:')."</h4><p>".$ev['location']."</p>";
46 $o = '<div class="vevent">' . "\r\n";
49 $o .= '<p class="summary event-summary">' . bbcode($ev['summary']) . '</p>' . "\r\n";
51 $o .= '<p class="description event-description">' . bbcode($ev['desc']) . '</p>' . "\r\n";
53 $o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
54 . datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
56 . '</abbr></p>' . "\r\n";
59 $o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
60 . datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
62 . '</abbr></p>' . "\r\n";
64 if(strlen($ev['location'])){
65 $o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">'
66 . bbcode($ev['location'])
67 . '</span></p>' . "\r\n";
69 if (strpos($ev['location'], "[map") !== False) {
70 $map = generate_named_map($ev['location']);
71 if ($map!==$ev['location']) $o.=$map;
76 $o .= '</div>' . "\r\n";
81 function parse_event($h) {
83 require_once('include/Scrape.php');
84 require_once('include/html2bbcode');
86 $h = '<html><body>' . $h . '</body></html>';
92 $dom = HTML5_Parser::parse($h);
93 } catch (DOMException $e) {
94 logger('parse_event: parse error: ' . $e);
100 $items = $dom->getElementsByTagName('*');
102 foreach($items as $item) {
103 if(attribute_contains($item->getAttribute('class'), 'vevent')) {
104 $level2 = $item->getElementsByTagName('*');
105 foreach($level2 as $x) {
106 if(attribute_contains($x->getAttribute('class'),'dtstart') && $x->getAttribute('title')) {
107 $ret['start'] = $x->getAttribute('title');
108 if(! strpos($ret['start'],'Z'))
109 $ret['adjust'] = true;
111 if(attribute_contains($x->getAttribute('class'),'dtend') && $x->getAttribute('title'))
112 $ret['finish'] = $x->getAttribute('title');
114 if(attribute_contains($x->getAttribute('class'),'description'))
115 $ret['desc'] = $x->textContent;
116 if(attribute_contains($x->getAttribute('class'),'location'))
117 $ret['location'] = $x->textContent;
124 if((x($ret,'desc')) && ((strpos($ret['desc'],'<') !== false) || (strpos($ret['desc'],'>') !== false))) {
125 $config = HTMLPurifier_Config::createDefault();
126 $config->set('Cache.DefinitionImpl', null);
127 $purifier = new HTMLPurifier($config);
128 $ret['desc'] = html2bbcode($purifier->purify($ret['desc']));
131 if((x($ret,'location')) && ((strpos($ret['location'],'<') !== false) || (strpos($ret['location'],'>') !== false))) {
132 $config = HTMLPurifier_Config::createDefault();
133 $config->set('Cache.DefinitionImpl', null);
134 $purifier = new HTMLPurifier($config);
135 $ret['location'] = html2bbcode($purifier->purify($ret['location']));
139 $ret['start'] = datetime_convert('UTC','UTC',$ret['start']);
141 $ret['finish'] = datetime_convert('UTC','UTC',$ret['finish']);
147 function format_event_bbcode($ev) {
152 $o .= '[event-summary]' . $ev['summary'] . '[/event-summary]';
155 $o .= '[event-description]' . $ev['desc'] . '[/event-description]';
158 $o .= '[event-start]' . $ev['start'] . '[/event-start]';
160 if(($ev['finish']) && (! $ev['nofinish']))
161 $o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
164 $o .= '[event-location]' . $ev['location'] . '[/event-location]';
167 $o .= '[event-adjust]' . $ev['adjust'] . '[/event-adjust]';
174 function bbtovcal($s) {
178 $o = format_event_html($ev);
183 function bbtoevent($s) {
188 if(preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is",$s,$match))
189 $ev['summary'] = $match[1];
191 if(preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is",$s,$match))
192 $ev['desc'] = $match[1];
194 if(preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is",$s,$match))
195 $ev['start'] = $match[1];
197 if(preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is",$s,$match))
198 $ev['finish'] = $match[1];
200 if(preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is",$s,$match))
201 $ev['location'] = $match[1];
203 if(preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is",$s,$match))
204 $ev['adjust'] = $match[1];
205 $ev['nofinish'] = (((x($ev, 'start') && $ev['start']) && (!x($ev, 'finish') || !$ev['finish'])) ? 1 : 0);
211 function sort_by_date($a) {
213 usort($a,'ev_compare');
218 function ev_compare($a,$b) {
220 $date_a = (($a['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$a['start']) : $a['start']);
221 $date_b = (($b['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$b['start']) : $b['start']);
223 if($date_a === $date_b)
224 return strcasecmp($a['desc'],$b['desc']);
226 return strcmp($date_a,$date_b);
229 function event_delete($event_id) {
233 q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
234 logger("Deleted event ".$event_id, LOGGER_DEBUG);
237 function event_store($arr) {
239 require_once('include/datetime.php');
240 require_once('include/items.php');
241 require_once('include/bbcode.php');
245 $arr['created'] = (($arr['created']) ? $arr['created'] : datetime_convert());
246 $arr['edited'] = (($arr['edited']) ? $arr['edited'] : datetime_convert());
247 $arr['type'] = (($arr['type']) ? $arr['type'] : 'event' );
248 $arr['cid'] = ((intval($arr['cid'])) ? intval($arr['cid']) : 0);
249 $arr['uri'] = (x($arr,'uri') ? $arr['uri'] : item_new_uri($a->get_hostname(),$arr['uid']));
250 $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0);
253 $c = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
258 $c = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
266 // Existing event being modified
270 // has the event actually changed?
272 $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
276 if((! count($r)) || ($r[0]['edited'] === $arr['edited'])) {
278 // Nothing has changed. Grab the item id to return.
280 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
284 return((count($r)) ? $r[0]['id'] : 0);
287 // The event changed. Update it.
289 $r = q("UPDATE `event` SET
299 WHERE `id` = %d AND `uid` = %d",
301 dbesc($arr['edited']),
302 dbesc($arr['start']),
303 dbesc($arr['finish']),
304 dbesc($arr['summary']),
306 dbesc($arr['location']),
308 intval($arr['adjust']),
309 intval($arr['nofinish']),
313 $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
318 $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
319 $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
320 $object .= '</object>' . "\n";
323 q("UPDATE `item` SET `body` = '%s', `object` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d",
324 dbesc(format_event_bbcode($arr)),
326 dbesc($arr['edited']),
331 $item_id = $r[0]['id'];
335 call_hooks("event_updated", $arr['id']);
341 // New event. Store it.
343 $r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
344 `adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
345 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
349 dbesc($arr['created']),
350 dbesc($arr['edited']),
351 dbesc($arr['start']),
352 dbesc($arr['finish']),
353 dbesc($arr['summary']),
355 dbesc($arr['location']),
357 intval($arr['adjust']),
358 intval($arr['nofinish']),
359 dbesc($arr['allow_cid']),
360 dbesc($arr['allow_gid']),
361 dbesc($arr['deny_cid']),
362 dbesc($arr['deny_gid'])
366 $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
375 $item_arr['uid'] = $arr['uid'];
376 $item_arr['contact-id'] = $arr['cid'];
377 $item_arr['uri'] = $arr['uri'];
378 $item_arr['parent-uri'] = $arr['uri'];
379 $item_arr['guid'] = $arr['guid'];
380 $item_arr['type'] = 'activity';
381 $item_arr['wall'] = (($arr['cid']) ? 0 : 1);
382 $item_arr['contact-id'] = $contact['id'];
383 $item_arr['owner-name'] = $contact['name'];
384 $item_arr['owner-link'] = $contact['url'];
385 $item_arr['owner-avatar'] = $contact['thumb'];
386 $item_arr['author-name'] = $contact['name'];
387 $item_arr['author-link'] = $contact['url'];
388 $item_arr['author-avatar'] = $contact['thumb'];
389 $item_arr['title'] = '';
390 $item_arr['allow_cid'] = $arr['allow_cid'];
391 $item_arr['allow_gid'] = $arr['allow_gid'];
392 $item_arr['deny_cid'] = $arr['deny_cid'];
393 $item_arr['deny_gid'] = $arr['deny_gid'];
394 $item_arr['private'] = $arr['private'];
395 $item_arr['last-child'] = 1;
396 $item_arr['visible'] = 1;
397 $item_arr['verb'] = ACTIVITY_POST;
398 $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
399 $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
400 $item_arr['body'] = format_event_bbcode($event);
403 $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
404 $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
405 $item_arr['object'] .= '</object>' . "\n";
407 $item_id = item_store($item_arr);
409 $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
413 // $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
417 //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d",
419 // intval($event['id']),
420 // intval($arr['uid']),
423 q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
424 intval($event['id']),
430 call_hooks("event_created", $event['id']);
436 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) $firstDay=0;
442 "firstDay" => $firstDay,
450 "Sunday" => t("Sunday"),
451 "Monday" => t("Monday"),
452 "Tuesday" => t("Tuesday"),
453 "Wednesday" => t("Wednesday"),
454 "Thursday" => t("Thursday"),
455 "Friday" => t("Friday"),
456 "Saturday" => t("Saturday"),
469 "January" => t("January"),
470 "February" => t("February"),
471 "March" => t("March"),
472 "April" => t("April"),
476 "August" => t("August"),
477 "September" => t("September"),
478 "October" => t("October"),
479 "November" => t("November"),
480 "December" => t("December"),
481 "today" => t("today"),
482 "month" => t("month"),
491 * @brief Get an event by its event ID
493 * @param type $owner_uid The User ID of the owner of the event
494 * @param type $event_params An assoziative array with
495 * int 'event_id' => The ID of the event in the event table
496 * @param type $sql_extra
497 * @return array Query result
499 function event_by_id($owner_uid = 0, $event_params, $sql_extra = '') {
500 // ownly allow events if there is a valid owner_id
504 // query for the event by event id
505 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
506 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
507 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
508 WHERE `event`.`uid` = %d AND `event`.`id` = %d $sql_extra",
510 intval($event_params["event_id"])
519 * @brief Get all events in a specific timeframe
521 * @param int $owner_uid The User ID of the owner of the events
522 * @param array $event_params An assoziative array with
524 * string 'start' => Start time of the timeframe
525 * string 'finish' => Finish time of the timeframe
526 * string 'adjust_start' =>
527 * string 'adjust_start' =>
529 * @param string $sql_extra Additional sql conditions (e.g. permission request)
530 * @return array Query results
532 function events_by_date($owner_uid = 0, $event_params, $sql_extra = '') {
533 // ownly allow events if there is a valid owner_id
537 // query for the event by date
538 $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
539 `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
540 LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
541 WHERE `event`.`uid` = %d AND event.ignore = %d
542 AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
543 OR (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s'))
546 intval($event_params["ignored"]),
547 dbesc($event_params["start"]),
548 dbesc($event_params["start"]),
549 dbesc($event_params["finish"]),
550 dbesc($event_params["adjust_start"]),
551 dbesc($event_params["adjust_start"]),
552 dbesc($event_params["adjust_finish"])
560 * @brief Convert an array query results in an arry which could be used by the events template
562 * @param array $arr Event query array
563 * @return array Event array for the template
565 function process_events ($arr) {
571 foreach($arr as $rr) {
573 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
574 $d = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], $fmt) : datetime_convert('UTC','UTC',$rr['start'],$fmt));
575 $d = day_translate($d);
577 $start = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'c') : datetime_convert('UTC','UTC',$rr['start'],'c'));
578 if ($rr['nofinish']){
581 $end = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['finish'], 'c') : datetime_convert('UTC','UTC',$rr['finish'],'c'));
585 $is_first = ($d !== $last_date);
588 $edit = ((! $rr['cid']) ? array(App::get_baseurl().'/events/event/'.$rr['id'],t('Edit event'),'','') : null);
589 $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
591 list($title, $_trash) = explode("<br",bbcode($rr['desc']),2);
592 $title = strip_tags(html_entity_decode($title,ENT_QUOTES,'UTF-8'));
595 $html = format_event_html($rr);
596 $rr['desc'] = bbcode($rr['desc']);
597 $rr['location'] = bbcode($rr['location']);
607 'is_first'=>$is_first,
610 'plink' => array($rr['plink'],t('link to source'),'',''),