Merge branch 'master' of github.com:DAViCal/davical into github
[davical.git] / inc / freebusy-functions.php
blob4849c0ca774ed5c19b454a099796846fa96a919a
1 <?php
3 /**
4 * Function to include which handles building a free/busy response to
5 * be used in either the REPORT, response to a POST, or response to a
6 * a freebusy GET request.
7 */
9 include_once('vCalendar.php');
10 include_once('RRule-v2.php');
13 function get_freebusy( $path_match, $range_start, $range_end, $bin_privs = null ) {
14 global $request, $c;
16 $debugging = false;
17 // if ( $debugging ) {
18 // printf( "Path: %s\n", $path_match );
19 // print_r( $range_start );
20 // print_r( $range_end );
21 // }
23 if ( !isset($bin_privs) ) $bin_privs = $request->Privileges();
24 if ( !isset($range_start) || !isset($range_end) ) {
25 $request->DoResponse( 400, 'All valid freebusy requests MUST contain a time-range filter' );
27 $params = array( ':path_match' => $path_match, ':start' => $range_start->UTC(), ':end' => $range_end->UTC() );
28 $where = ' WHERE caldav_data.dav_name ~ :path_match ';
29 $where .= 'AND rrule_event_overlaps( dtstart, dtend, rrule, :start, :end) ';
30 $where .= "AND caldav_data.caldav_type IN ( 'VEVENT', 'VTODO' ) ";
31 $where .= "AND (calendar_item.transp != 'TRANSPARENT' OR calendar_item.transp IS NULL) ";
32 $where .= "AND (calendar_item.status != 'CANCELLED' OR calendar_item.status IS NULL) ";
33 $where .= "AND collection.is_calendar AND collection.schedule_transp = 'opaque' ";
35 if ( $bin_privs != privilege_to_bits('all') ) {
36 $where .= "AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
39 $fbtimes = array();
40 $sql = 'SELECT caldav_data.caldav_data, calendar_item.rrule, calendar_item.transp, calendar_item.status, ';
41 $sql .= "to_char(calendar_item.dtstart at time zone 'GMT',".AWLDatabase::SqlUTCFormat.') AS start, ';
42 $sql .= "to_char(calendar_item.dtend at time zone 'GMT',".AWLDatabase::SqlUTCFormat.') AS finish, ';
43 $sql .= "calendar_item.class, calendar_item.dav_id ";
44 $sql .= 'FROM caldav_data INNER JOIN calendar_item USING(dav_id,user_no,dav_name,collection_id) ';
45 $sql .= 'INNER JOIN collection USING(collection_id)';
46 $sql .= $where;
47 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= ' ORDER BY dav_id';
48 $qry = new AwlQuery( $sql, $params );
49 if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ) {
50 while( $calendar_object = $qry->Fetch() ) {
51 $extra = '';
52 if ( $calendar_object->status == 'TENTATIVE' ) {
53 $extra = ';BUSY-TENTATIVE';
55 else if ( isset($c->_workaround_client_freebusy_bug) && $c->_workaround_client_freebusy_bug ) {
56 $extra = ';BUSY';
58 // if ( $debugging ) {
59 // $extra = ';'.$calendar_object->dav_id;
60 // }
61 // dbg_error_log( "REPORT", " FreeBusy: Not transparent, tentative or cancelled: %s, %s, %s", $calendar_object->start, $calendar_object->finish, $calendar_object->class );
62 $ics = new vComponent($calendar_object->caldav_data);
63 $expanded = expand_event_instances($ics, $range_start, $range_end);
64 $expansion = $expanded->GetComponents( array('VEVENT'=>true,'VTODO'=>true,'VJOURNAL'=>true) );
65 // if ( $debugging ) echo "=================== $calendar_object->dav_id ========================\n";
66 $dtstart_type = 'DTSTART';
67 foreach( $expansion AS $k => $v ) {
68 // if ( $debugging ) print $k."\n".$v->Render();
69 $start_date = $v->GetProperty($dtstart_type);
70 if ( !isset($start_date) && $v->GetType() != 'VTODO' ) {
71 $dtstart_type = 'DUE';
72 $start_date = $v->GetProperty($dtstart_type);
74 $start_date = new RepeatRuleDateTime($start_date);
75 $duration = $v->GetProperty('DURATION');
76 $duration = ( !isset($duration) ? 'P1D' : $duration->Value());
77 $end_date = clone($start_date);
78 $end_date->modify( $duration );
79 if ( $end_date == $start_date || $end_date < $range_start || $start_date > $range_end ) {
80 // if ( $debugging )
81 // echo "-----------------------------------------------------\n";
83 continue;
85 // if ( $debugging )
86 // echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
87 $thisfb = $start_date->UTC() .'/'. $end_date->UTC() . $extra;
88 array_push( $fbtimes, $thisfb );
93 $freebusy = new vComponent();
94 $freebusy->setType('VFREEBUSY');
95 $freebusy->AddProperty('DTSTAMP', date('Ymd\THis\Z'));
96 $freebusy->AddProperty('DTSTART', $range_start->UTC());
97 $freebusy->AddProperty('DTEND', $range_end->UTC());
99 sort( $fbtimes );
100 foreach( $fbtimes AS $k => $v ) {
101 $text = explode(';',$v,2);
102 $freebusy->AddProperty( 'FREEBUSY', $text[0], (isset($text[1]) ? array('FBTYPE' => $text[1]) : null) );
105 return $freebusy;