9 include_once(
'vCalendar.php');
10 include_once(
'RRule.php');
13 function get_freebusy( $path_match, $range_start, $range_end, $bin_privs = null ) {
15 dbg_error_log(
'freebusy',
' Getting freebusy for path %s from %s to %s', $path_match, $range_start, $range_end);
17 if ( !isset($bin_privs) ) $bin_privs = $request->Privileges();
18 if ( !isset($range_start) || !isset($range_end) ) {
19 $request->DoResponse( 400,
'All valid freebusy requests MUST contain a time-range filter' );
21 $params = array(
':path_match' => $path_match,
':start' => $range_start->UTC(),
':end' => $range_end->UTC() );
22 $where =
' WHERE caldav_data.dav_name ~ :path_match ';
23 $where .=
'AND rrule_event_overlaps( dtstart, dtend, rrule, :start, :end) ';
24 $where .=
"AND caldav_data.caldav_type IN ( 'VEVENT', 'VTODO' ) ";
25 $where .=
"AND (calendar_item.transp != 'TRANSPARENT' OR calendar_item.transp IS NULL) ";
26 $where .=
"AND (calendar_item.status != 'CANCELLED' OR calendar_item.status IS NULL) ";
27 $where .=
"AND collection.is_calendar AND collection.schedule_transp = 'opaque' ";
29 if ( $bin_privs != privilege_to_bits(
'all') ) {
30 $where .=
"AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
34 $sql =
'SELECT caldav_data.caldav_data, calendar_item.rrule, calendar_item.transp, calendar_item.status, ';
35 $sql .=
"to_char(calendar_item.dtstart at time zone 'GMT',".AWLDatabase::SqlUTCFormat.
') AS start, ';
36 $sql .=
"to_char(calendar_item.dtend at time zone 'GMT',".AWLDatabase::SqlUTCFormat.
') AS finish, ';
37 $sql .=
"calendar_item.class, calendar_item.dav_id ";
38 $sql .=
'FROM caldav_data INNER JOIN calendar_item USING(dav_id,user_no,dav_name,collection_id) ';
39 $sql .=
'INNER JOIN collection USING(collection_id)';
41 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .=
' ORDER BY dav_id';
42 $qry =
new AwlQuery( $sql, $params );
43 if ( $qry->Exec(
"REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ) {
44 while( $calendar_object = $qry->Fetch() ) {
46 if ( $calendar_object->status ==
'TENTATIVE' ) {
47 $extra =
';BUSY-TENTATIVE';
49 else if ( isset($c->_workaround_client_freebusy_bug) && $c->_workaround_client_freebusy_bug ) {
53 $ics =
new vComponent($calendar_object->caldav_data);
54 $expanded = expand_event_instances($ics, $range_start, $range_end);
55 $expansion = $expanded->GetComponents( array(
'VEVENT'=>
true,
'VTODO'=>
true,
'VJOURNAL'=>
true) );
56 dbg_error_log(
"freebusy",
"=================== $calendar_object->dav_id ======================== %s -> %s, %s %s", $calendar_object->start, $calendar_object->finish, $calendar_object->class, $extra );
57 $dtstart_type =
'DTSTART';
58 foreach( $expansion AS $k => $v ) {
59 dbg_error_log(
"freebusy",
" %s: %s", $k, $v->Render() );
60 $start_date = $v->GetProperty($dtstart_type);
61 if ( !isset($start_date) && $v->GetType() !=
'VTODO' ) {
62 $dtstart_type =
'DUE';
63 $start_date = $v->GetProperty($dtstart_type);
66 $duration = $v->GetProperty(
'DURATION');
67 $duration = ( !isset($duration) ?
'P1D' : $duration->Value());
68 $end_date = clone($start_date);
69 $end_date->modify( $duration );
70 if ( $end_date == $start_date || $end_date < $range_start || $start_date > $range_end ) {
71 dbg_error_log(
"freebusy",
"-----------------------------------------------------" );
74 $thisfb = $start_date->UTC() .
'/'. $end_date->UTC() . $extra;
75 array_push( $fbtimes, $thisfb );
80 $freebusy =
new vComponent();
81 $freebusy->setType(
'VFREEBUSY');
82 $freebusy->AddProperty(
'DTSTAMP', date(
'Ymd\THis\Z'));
83 $freebusy->AddProperty(
'DTSTART', $range_start->UTC());
84 $freebusy->AddProperty(
'DTEND', $range_end->UTC());
87 foreach( $fbtimes AS $k => $v ) {
88 $text = explode(
';',$v,2);
89 $freebusy->AddProperty(
'FREEBUSY', $text[0], (isset($text[1]) ? array(
'FBTYPE' => $text[1]) : null) );