From fd4639aba04a70af7666b068b874ce67849db94b Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Tue, 12 May 2009 14:46:49 +1200 Subject: [PATCH] Switch to using RRule SQL functions. --- inc/caldav-REPORT-calquery.php | 16 ++-------------- inc/caldav-REPORT-freebusy.php | 8 +------- inc/freebusy-GET.php | 15 +++++---------- 3 files changed, 8 insertions(+), 31 deletions(-) diff --git a/inc/caldav-REPORT-calquery.php b/inc/caldav-REPORT-calquery.php index 28077393..6252b7d6 100644 --- a/inc/caldav-REPORT-calquery.php +++ b/inc/caldav-REPORT-calquery.php @@ -119,20 +119,8 @@ function SqlFilterFragment( $filter, $components, $property = null, $parameter = $finish_column = 'dtstart'; // The column we compare against the END attribute $start = $v->GetAttribute("start"); $finish = $v->GetAttribute("end"); - if ( isset($start) && isset($finish) ) { - $sql .= sprintf( "AND ( (%s >= %s::timestamp with time zone AND %s <= %s::timestamp with time zone) ", - $finish_column, qpg($start), $start_column, qpg($finish)); - $sql .= sprintf( "OR calculate_later_timestamp(%s::timestamp with time zone,%s,rrule) <= %s::timestamp with time zone ", qpg($start), $finish_column, qpg($finish) ); - $sql .= sprintf( "OR calculate_later_timestamp(%s::timestamp with time zone,%s,rrule) <= %s::timestamp with time zone ", qpg($start), $start_column, qpg($finish) ); - $sql .= sprintf( "OR event_has_exceptions(caldav_data.caldav_data) )" ); - } - else if ( isset($start) ) { - $sql .= sprintf( "AND (%s >= %s::timestamp with time zone ", $finish_column, qpg($start)); - $sql .= sprintf( "OR calculate_later_timestamp(%s::timestamp with time zone,%s,rrule) >= %s::timestamp with time zone ", qpg($start), $finish_column, qpg($start) ); - $sql .= sprintf( "OR event_has_exceptions(caldav_data.caldav_data) )" ); - } - else if ( isset( $finish ) ) { - $sql .= sprintf( "AND (%s <= %s::timestamp with time zone ", $start_column, qpg($finish) ); + if ( isset($start) || isset($finish) ) { + $sql .= "AND (rrule_event_overlaps( dtstart, dtend, rrule, ".qpg($start).", ".qpg($finish)." ) "; $sql .= sprintf( "OR event_has_exceptions(caldav_data.caldav_data) )" ); } break; diff --git a/inc/caldav-REPORT-freebusy.php b/inc/caldav-REPORT-freebusy.php index 52d6354b..5470b2aa 100644 --- a/inc/caldav-REPORT-freebusy.php +++ b/inc/caldav-REPORT-freebusy.php @@ -13,13 +13,7 @@ if ( ! ( isset($fbq_start) || isset($fbq_end) ) ) { $request->DoResponse( 400, 'All valid freebusy requests MUST contain a time-range filter' ); } $where = " WHERE caldav_data.dav_name ~ ? "; -if ( isset( $fbq_start ) ) { - $where .= "AND (dtend >= ".qpg($fbq_start)."::timestamp with time zone "; - $where .= "OR calculate_later_timestamp(".qpg($fbq_start)."::timestamp with time zone,dtend,rrule) >= ".qpg($fbq_start)."::timestamp with time zone) "; -} -if ( isset( $fbq_end ) ) { - $where .= "AND dtstart <= ".qpg($fbq_end)."::timestamp with time zone "; -} +$where .= "AND rrule_event_overlaps( dtstart, dtend, rrule, ".qpg($fbq_start).", ".qpg($fbq_end)." ) "; $where .= "AND caldav_data.caldav_type IN ( 'VEVENT', 'VFREEBUSY' ) "; $where .= "AND (calendar_item.transp != 'TRANSPARENT' OR calendar_item.transp IS NULL) "; $where .= "AND (calendar_item.status != 'CANCELLED' OR calendar_item.status IS NULL) "; diff --git a/inc/freebusy-GET.php b/inc/freebusy-GET.php index 9319db28..9ecaf183 100644 --- a/inc/freebusy-GET.php +++ b/inc/freebusy-GET.php @@ -6,16 +6,12 @@ include_once("RRule.php"); * We need to allow GET of start & finish so we can have a consistent regression test result set. And it might be useful * to people as well... */ -if ( isset($_GET['start']) && preg_match( '/^[12][0-9]{3}(0[0-9]|1[012])[0123][0-9]T[0-2][0-9]([0-5][0-9]){2}$/', $_GET['start'] )) { - $start = $_GET['start']; -} -else { +param_to_global('start', '/^[12][0-9]{3}(0[0-9]|1[012])[0123][0-9]T[0-2][0-9]([0-5][0-9]){2}$/' ); +if ( !isset($start) ) { $start = date( "Ymd\THis", time() - (86400 * 30) ); } -if ( isset($_GET['finish']) && preg_match( '/^[12][0-9]{3}(0[0-9]|1[012])[0123][0-9]T[0-2][0-9]([0-5][0-9]){2}$/', $_GET['finish'] )) { - $finish = $_GET['finish']; -} -else { +param_to_global('finish', '/^[12][0-9]{3}(0[0-9]|1[012])[0123][0-9]T[0-2][0-9]([0-5][0-9]){2}$/' ); +if ( !isset($finish) ) { $finish = date( "Ymd\THis", time() + (86400 * 200) ); } @@ -25,8 +21,7 @@ if ( isset($request->by_email) ) { else { $where = "WHERE caldav_data.user_no = $request->user_no AND caldav_data.dav_name ~ ".qpg("^".$request->path)." "; } -$where .= "AND (dtend >= '$start'::timestamp with time zone OR calculate_later_timestamp('$start'::timestamp with time zone,dtend,rrule) >= '$start'::timestamp with time zone) "; -$where .= "AND dtstart <= '$finish'::timestamp with time zone "; +$where .= "AND rrule_event_overlaps( dtstart, dtend, rrule, ".qpg($start).", ".qpg($finish)." ) "; $where .= "AND caldav_data.caldav_type IN ( 'VEVENT', 'VFREEBUSY' ) "; $where .= "AND (calendar_item.transp != 'TRANSPARENT' OR calendar_item.transp IS NULL) "; $where .= "AND (calendar_item.status != 'CANCELLED' OR calendar_item.status IS NULL) ";