We don't need to test for the PostgreSQL non-PDO drivers now.
[davical.git] / inc / freebusy-functions.php
blobe2184b6f801af97b41e9b9f8d6f27b9d61cb4444
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('iCalendar.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 // printf( "Path: %s\n", $path_match);
17 // print_r($range_start);
18 // print_r($range_end);
20 if ( !isset($bin_privs) ) $bin_privs = $request->Privileges();
21 if ( !isset($range_start) || !isset($range_end) ) {
22 $request->DoResponse( 400, 'All valid freebusy requests MUST contain a time-range filter' );
24 $params = array( ':path_match' => $path_match, ':start' => $range_start->UTC(), ':end' => $range_end->UTC() );
25 $where = ' WHERE caldav_data.dav_name ~ :path_match ';
26 $where .= 'AND rrule_event_overlaps( dtstart, dtend, rrule, :start, :end) ';
27 $where .= "AND caldav_data.caldav_type IN ( 'VEVENT', 'VTODO' ) ";
28 $where .= "AND (calendar_item.transp != 'TRANSPARENT' OR calendar_item.transp IS NULL) ";
29 $where .= "AND (calendar_item.status != 'CANCELLED' OR calendar_item.status IS NULL) ";
30 $where .= "AND collection.is_calendar AND collection.schedule_transp = 'opaque' ";
32 if ( $bin_privs != privilege_to_bits('all') ) {
33 $where .= "AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
36 // $debugging = true;
37 $fbtimes = array();
38 $sql = 'SELECT caldav_data.caldav_data, calendar_item.rrule, calendar_item.transp, calendar_item.status, ';
39 $sql .= "to_char(calendar_item.dtstart at time zone 'GMT',".iCalendar::SqlUTCFormat().') AS start, ';
40 $sql .= "to_char(calendar_item.dtend at time zone 'GMT',".iCalendar::SqlUTCFormat().') AS finish, ';
41 $sql .= "calendar_item.class, calendar_item.dav_id ";
42 $sql .= 'FROM caldav_data INNER JOIN calendar_item USING(dav_id,user_no,dav_name,collection_id) ';
43 $sql .= 'INNER JOIN collection USING(collection_id)';
44 $sql .= $where;
45 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= ' ORDER BY dav_id';
46 $qry = new AwlQuery( $sql, $params );
47 if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ) {
48 while( $calendar_object = $qry->Fetch() ) {
49 $extra = '';
50 if ( $calendar_object->status == 'TENTATIVE' ) {
51 $extra = ';BUSY-TENTATIVE';
53 else if ( isset($c->_workaround_client_freebusy_bug) && $c->_workaround_client_freebusy_bug ) {
54 $extra = ';BUSY';
56 // else if ( $debugging ) {
57 // $extra = ';'.$calendar_object->dav_id;
58 // }
59 dbg_error_log( "REPORT", " FreeBusy: Not transparent, tentative or cancelled: %s, %s, %s", $calendar_object->start, $calendar_object->finish, $calendar_object->class );
60 $ics = new vComponent($calendar_object->caldav_data);
61 $expanded = expand_event_instances($ics, $range_start, $range_end);
62 $expansion = $expanded->GetComponents( array('VEVENT'=>true,'VTODO'=>true,'VJOURNAL'=>true) );
63 foreach( $expansion AS $k => $v ) {
64 // echo "=====================================================\n";
65 // printf( "Type: %s\n", $v->GetType());
66 // print_r($v);
67 // echo "-----------------------------------------------------\n";
68 $start_date = $v->GetProperty('DTSTART');
69 if ( !isset($start_date) ) continue;
70 $start_date = new RepeatRuleDateTime($start_date);
71 $duration = $v->GetProperty('DURATION');
72 $duration = ( !isset($duration) ? 'P1D' : $duration->Value());
73 $end_date = clone($start_date);
74 $end_date->modify( $duration );
75 if ( $end_date == $start_date || $end_date < $range_start || $start_date > $range_end ) continue;
76 $thisfb = $start_date->UTC() .'/'. $end_date->UTC() . $extra;
77 array_push( $fbtimes, $thisfb );
82 $freebusy = new iCalComponent();
83 $freebusy->SetType('VFREEBUSY');
84 $freebusy->AddProperty('DTSTAMP', date('Ymd\THis\Z'));
85 $freebusy->AddProperty('DTSTART', $range_start->UTC());
86 $freebusy->AddProperty('DTEND', $range_end->UTC());
88 sort( $fbtimes );
89 foreach( $fbtimes AS $k => $v ) {
90 $text = explode(';',$v,2);
91 $freebusy->AddProperty( 'FREEBUSY', $text[0], (isset($text[1]) ? array('FBTYPE' => $text[1]) : null) );
94 return $freebusy;