From e291ccea00dfd6640bd106235c167299f398e216 Mon Sep 17 00:00:00 2001 From: Jason Oster Date: Thu, 27 May 2010 15:47:24 +0000 Subject: Merge version-2-4 --- functions/parse/recur_functions.php | 260 ++++++++++++++++++++---------------- 1 file changed, 142 insertions(+), 118 deletions(-) (limited to 'functions') diff --git a/functions/parse/recur_functions.php b/functions/parse/recur_functions.php index 76cb30b..7cf8075 100644 --- a/functions/parse/recur_functions.php +++ b/functions/parse/recur_functions.php @@ -8,45 +8,50 @@ If multiple BYxxx rule parts are specified, then after evaluating the specified We will use two kinds of functions - those that restrict the date to allowed values and those that expand allowed values */ -function add_recur($times,$freq=''){ +function add_recur($times, $freq = '') { global $recur_data; global $count, $mArray_begin, $mArray_end, $except_dates, $start_date, $start_date_unixtime,$end_range_unixtime,$until_unixtime, $day_offset, $current_view; if (!is_array($times)) $times = array($times); /* BYMONTH, BYWEEKNO, BYYEARDAY, BYMONTHDAY, BYDAY, BYHOUR, BYMINUTE, BYSECOND and BYSETPOS */ #dump_times($times); - if ($current_view != 'search'){ - $times = restrict_bymonth($times,$freq); - # $times = restrict_byweekno($times,$freq); - $times = restrict_byyearday($times,$freq); - $times = restrict_bymonthday($times,$freq); - $times = restrict_byday($times,$freq); - if($start_date_unixtime > $mArray_begin) $times[] = $start_date_unixtime; - $times = restrict_bysetpos($times,$freq);#echo "restrict_bysetpos"; + if ($current_view != 'search') { + $times = restrict_bymonth($times, $freq); + # $times = restrict_byweekno($times, $freq); + $times = restrict_byyearday($times, $freq); + $times = restrict_bymonthday($times, $freq); + $times = restrict_byday($times, $freq); + if ($start_date_unixtime > $mArray_begin) $times[] = $start_date_unixtime; + $times = restrict_bysetpos($times, $freq); $times = array_unique($times); sort($times); } - $until_date = date("Ymd",$end_range_unixtime); - foreach ($times as $time){ - #echo "time:". date("Ymd",$time)." $until_date
\n"; + foreach ($times as $time) { if (!isset($time) || $time == '') continue; - $date = date("Ymd",$time); - $time = strtotime("$date 12:00:00"); + + // Why are we arbitrarily setting the time to noon? + $date = date('Ymd', $time); + $time = strtotime($date . ' 12:00:00'); + $until_date = date('Ymd', $until_unixtime); + $until_unixtime = strtotime($until_date . ' 12:00:00'); + # day offset fixes shifts across day boundaries due to time diffs. # These are already fixed for the initial instance, but need to be fixed for recurrences - if (date("Ymd", $time) != $start_date) $time = $time + $day_offset * (24*60*60); - if(isset($time) - && $time != '' - && !in_array($time, $recur_data) - && !in_array($date, $except_dates) - && $time <= $until_unixtime - && $time >= $start_date_unixtime - && $date <= $until_date - ){ - $count--; #echo "\n.$count\n"; - if($time >= $mArray_begin - && $time <= $mArray_end - && $count >= 0 - ) $recur_data[] = $time; + if (date('Ymd', $time) != $start_date) $time = $time + ($day_offset * 24 * 60 * 60); + if ((isset($time)) && + (!in_array($time, $recur_data)) && + (!in_array($date, $except_dates)) && + ($time <= $until_unixtime) && + ($time >= $start_date_unixtime) && + ($date <= $until_date)) { + $count--; + if (($time >= $mArray_begin) && + ($time <= $mArray_end) + ($count >= 0)) { + $recur_data[] = $time; + } + } + else if (in_array($date, $except_dates)) { + $count--; } else if (in_array($date, $except_dates)) { $count--; @@ -55,74 +60,81 @@ function add_recur($times,$freq=''){ #dump_times($recur_data); return; } -function expand_bymonth($time){ + +function expand_bymonth($time) { global $bymonth, $byweekno, $bymonthday, $year, $start_unixtime, $freq_type; - if(!empty($byweekno)) return $time; - if(empty($bymonth)) $bymonth = array(date("m", $start_unixtime)); - $d = date("d",$start_unixtime); + if (!empty($byweekno)) return $time; + if (empty($bymonth)) $bymonth = array(date('m', $start_unixtime)); + $d = date('d', $start_unixtime); if (!empty($bymonthday)) $d = 1; - foreach ($bymonth as $m){ - $time = mktime(12,0,0,$m,$d,$year); #echo "exm:".date("Ymd",$time)."\n"; + foreach ($bymonth as $m) { + $time = mktime(12, 0, 0, $m, $d, $year); $times[] = $time; } return $times; } -function expand_byweekno($times){ + +function expand_byweekno($times) { global $byweekno, $year, $freq_type, $wkst, $wkst3char; # byweekno is only used when repeat is yearly # when it's set, the input times are irrelevant # it also needs a byday. if ($freq_type != 'year') return $times; if (empty($byweekno)) return $times; - $total_weeks = date("W",mktime(12,0,0,12,24,$year) ) +1; - $w1_start = strtotime("this $wkst3char", mktime(12,0,0,1,1,$year) ); - foreach($byweekno as $weekno){ - if($weekno < 0) $weekno = $weekno + $total_weeks; - #echo "\n $wkst3char w1st:".date("Ymd", $w1_start)." ".date("Ymd", mktime(12,0,0,1,1,$year))." weekno:$weekno"; - $new_times[] = strtotime("+".(($weekno-1)*7)."days",$w1_start); + $total_weeks = date('W', mktime(12, 0, 0, 12, 24, $year)) + 1; + $w1_start = strtotime('this ' . $wkst3char, mktime(12, 0, 0, 1, 1, $year)); + foreach ($byweekno as $weekno) { + if ($weekno < 0) { + $weekno = $weekno + $total_weeks; + } + $new_times[] = strtotime('+' . (($weekno - 1) * 7) . ' days', $w1_start); } #dump_times($new_times); return $new_times; } -function expand_byyearday($times){ +function expand_byyearday($times) { global $byyearday, $year; if (empty($byyearday)) return $times; $py = $year-1; $ny = $year+1; $new_times = array(); - foreach($times as $time){ - foreach($byyearday as $yearday){ - if($yearday > 0){ $day = strtotime("+$yearday days Dec 31, $py");#echo "\n".date("Ymd",$day)." = +$yearday days Dec 31, $py"; - }else $day = strtotime("Jan 1 $ny $yearday days"); - if(date("Y",$day == $year)) $new_times[] = $day; + foreach ($times as $time){ + foreach ($byyearday as $yearday) { + if ($yearday > 0) { + $day = strtotime('+' . $yearday . ' days Dec 31, ' . $py); + } + else { + $day = strtotime('Jan 1 ' . $ny . ' ' . $yearday . ' days'); + } + if (date('Y', $day == $year)) $new_times[] = $day; } } # dump_times($new_times); return $new_times; } -function expand_bymonthday($times){ +function expand_bymonthday($times) { global $bymonthday, $year; if (empty($bymonthday)) return $times; - foreach($times as $time){ - $month = date('m',$time); - foreach($bymonthday as $monthday){ - if($monthday < 0) $monthday = date("t",$time) + $monthday +1; - $new_times[] = mktime(12,0,0,$month,$monthday,$year); - #echo "monthday:$monthday\n"; + foreach ($times as $time) { + $month = date('m', $time); + foreach ($bymonthday as $monthday) { + if ($monthday < 0) { + $monthday = (date('t', $time) + $monthday + 1); + } + $new_times[] = mktime(12, 0, 0, $month, $monthday, $year); } } return $new_times; } -function expand_byday($time){ +function expand_byday($time) { global $freq_type, $byday, $bymonth,$byweekno, $wkst3char, $year, $month, $start_unixtime, $summary; if (empty($byday)) return array($time); $times = array(); - $the_sunday = dateOfWeek(date("Ymd",$time), $wkst3char); - #echo "
$summary $freq_type ".print_r($byday,true)."$wkst3char $the_sunday
"; - foreach($byday as $key=>$day) { + $the_sunday = dateOfWeek(date('Ymd', $time), $wkst3char); + foreach ($byday as $key => $day) { /* set $byday_arr [0] => byday string, e.g. 4TH [1] => sign/modifier @@ -131,28 +143,30 @@ function expand_byday($time){ */ ereg ('([-\+]{0,1})?([0-9]+)?([A-Z]{2})', $day, $byday_arr); $on_day = two2threeCharDays($byday_arr[3]); - switch($freq_type){ + switch ($freq_type) { case 'week': #need to find the first day of the appropriate week. - $next_date_time = strtotime("this $on_day",strtotime($the_sunday)) + (12 * 60 * 60); + $next_date_time = strtotime('this ' . $on_day, strtotime($the_sunday)) + (12 * 60 * 60); $times[] = $next_date_time; break; case 'month': - $time = mktime(12,0,0,$month,1,$year); + $time = mktime(12, 0, 0, $month, 1, $year); + // FIXME: Is this supposed to fall through? case 'year': - if(empty($byweekno)){ - $week_arr = array(1,2,3,4,5); + if (empty($byweekno)) { + $week_arr = array(1, 2, 3, 4, 5); if(isset($byday_arr[2]) && $byday_arr[2] !='') $week_arr = array($byday_arr[2]); - $month_start = strtotime(date("Ym01",$time)) - (24 * 60 * 60); - $month_end = strtotime(date("Ymt",$time))+ (36 * 60 * 60); - if($freq_type == 'year' && empty($bymonth)){ - $month_start = mktime(12,0,0,1,0,$year); - $month_end = mktime(12,0,0,1,1,$year+1); + $month_start = strtotime(date('Ym01', $time)) - (24 * 60 * 60); + $month_end = strtotime(date('Ymt', $time)) + (36 * 60 * 60); + if ($freq_type == 'year' && empty($bymonth)) { + $month_start = mktime(12, 0, 0, 1, 0, $year); + $month_end = mktime(12, 0, 0, 1, 1, $year + 1); } - $month_start_day = strtolower(date("D", $month_start)); - foreach($week_arr as $week){ - #echo "
$summary ".$byday_arr[1].$week.$on_day." st:".date("Ymd",$month_start)." t:".date("Ymd",$time)."\n";
-						if($byday_arr[1] == '-') $next_date_time = strtotime($byday_arr[1].$week.$on_day,$month_end);
+					$month_start_day = strtolower(date('D', $month_start));
+					foreach ($week_arr as $week) {
+						if ($byday_arr[1] == '-') {
+							$next_date_time = strtotime($byday_arr[1] . $week . $on_day, $month_end);
+						}
 						else {
 							# we need this special offset in case the event day coincides with the month start day
 							# eg: month starts on a Sunday, our event is the Nth Sunday of the month... without this
@@ -161,76 +175,82 @@ function expand_byday($time){
 							$next_date_time = strtotime($byday_arr[1] . $week . $on_day, ($month_start + $special_offset));
 						}
 						# check that we're still in the same month
-						if (date("m",$next_date_time) == date("m",$time) ) $times[] = $next_date_time;
+						if (date('m', $next_date_time) == date('m', $time)) $times[] = $next_date_time;
 					}
-				}else{
+				}
+				else {
 					# byweekno should act like freq_type = week
-					$next_date_time = strtotime("this $on_day",strtotime($the_sunday)) + (12 * 60 * 60);
+					$next_date_time = strtotime('this ' . $on_day, strtotime($the_sunday)) + (12 * 60 * 60);
 					$times[] = $next_date_time;
 				}
 				break;
 			default:
-				$month_start = strtotime(date("Ym01",$time));
-				$next_date_time = strtotime($byday_arr[1].$byday_arr[2].$on_day, $month_start);
+				$month_start = strtotime(date('Ym01', $time));
+				$next_date_time = strtotime($byday_arr[1] . $byday_arr[2] . $on_day, $month_start);
+				break;
 		}
 	}
-	#echo "exp byday";dump_times($times);
 	return $times;
 }
 
-
-function restrict_bymonth($times,$freq=''){
+function restrict_bymonth($times, $freq = '') {
 	global $bymonth, $byyearday;
 	if (empty($bymonth) || !empty($byyearday)) return $times;
-	$new_times=array();
-	foreach ($times as $time){
-		if(in_array(date("m", $time), $bymonth)) $new_times[] = $time;
+	$new_times = array();
+	foreach ($times as $time) {
+		if (in_array(date('m', $time), $bymonth)) $new_times[] = $time;
 	}
 	return $new_times;
 }
-function restrict_byweekno($times,$freq=''){
+
+function restrict_byweekno($times, $freq = '') {
 	global $byweekno;
-	if(empty($byweekno)) return $times;
-	$new_times=array();
-	foreach ($times as $time) if(in_array(date("W", $time), $byweekno)) $new_times[] = $time;
+	if (empty($byweekno)) return $times;
+	$new_times = array();
+	foreach ($times as $time) {
+		if (in_array(date('W', $time), $byweekno)) $new_times[] = $time;
+	}
 	return $new_times;
 
 }
-function restrict_byyearday($times,$freq=''){
+
+function restrict_byyearday($times, $freq = '') {
 	global $byyearday;
-	if(empty($byyearday)) return $times;
-	$new_times=array();
-	foreach ($times as $time){
-		foreach ($byyearday as $yearday){
-			if($yearday < 0){
-				$yearday = 365 + $yearday +1;
-				if(date("L",$time)) $yearday += 1;
+	if (empty($byyearday)) return $times;
+	$new_times = array();
+	foreach ($times as $time) {
+		foreach ($byyearday as $yearday) {
+			if ($yearday < 0) {
+				$yearday = 365 + $yearday + 1;
+				if (date('L', $time)) $yearday += 1;
 			}
 			$yearday_arr[] = $yearday;
 		}
-		# date(z,$time) gives 0 for Jan 1
-		if(in_array((date("z", $time)+1), $yearday_arr)) $new_times[] = $time;
+		# date('z', $time) gives 0 for Jan 1
+		if (in_array((date('z', $time) + 1), $yearday_arr)) $new_times[] = $time;
 	}
 	return $new_times;
 }
 
-function restrict_bymonthday($times,$freq=''){
+function restrict_bymonthday($times, $freq = '') {
 	global $bymonthday;
-	if(empty($bymonthday)) return $times;
-	$new_times=array();
-	foreach ($times as $time){
-		foreach ($bymonthday as $monthday){
-			if($monthday < 0) $monthday = date("t",$time) + $monthday +1;
+	if (empty($bymonthday)) return $times;
+	$new_times = array();
+	foreach ($times as $time) {
+		foreach ($bymonthday as $monthday) {
+			if ($monthday < 0) {
+				$monthday = date('t', $time) + $monthday + 1;
+			}
 			$monthday_arr[] = $monthday;
 		}
-		if(in_array(date("j", $time), $monthday_arr)) $new_times[] = $time;
+		if (in_array(date('j', $time), $monthday_arr)) $new_times[] = $time;
 	}
 	return $new_times;
 }
-function restrict_byday($times,$freq=''){
+function restrict_byday($times, $freq = '') {
 	global $byday;
-	if(empty($byday)) return $times;
-	foreach($byday as $key=>$day) {
+	if (empty($byday)) return $times;
+	foreach ($byday as $key => $day) {
 		/* set $byday_arr
 				[0] => byday string, e.g. 4TH
 				[1] => sign/modifier
@@ -240,27 +260,31 @@ function restrict_byday($times,$freq=''){
 		ereg ('([-\+]{0,1})?([0-9]{1})?([A-Z]{2})', $day, $byday_arr);
 		$byday3[] = two2threeCharDays($byday_arr[3]);
 	}
-	$new_times=array();
-	foreach ($times as $time) if(in_array(strtolower(date("D", $time)), $byday3)) $new_times[] = $time;
+	$new_times = array();
+	foreach ($times as $time) {
+		if (in_array(strtolower(date('D', $time)), $byday3)) $new_times[] = $time;
+	}
 	return $new_times;
 }
 
-function restrict_bysetpos($times,$freq=''){
+function restrict_bysetpos($times, $freq = '') {
 	global $bysetpos;
-	if(empty($bysetpos)) return $times;
+	if (empty($bysetpos)) return $times;
 	sort($times);
-	$new_times=array();
-	foreach($bysetpos as $setpos){
-		$new_times[] = implode('',array_slice($times, $setpos, 1));
+	$new_times = array();
+	foreach ($bysetpos as $setpos) {
+		$new_times[] = implode('', array_slice($times, $setpos, 1));
 	}
 	return $new_times;
 }
 
 # for diagnostics
-function dump_times($times){
+function dump_times($times) {
 	global $summary;
-	echo "
$summary times:";
+	echo '
' . $summary . ' times:';
 	#var_dump($times);
-	foreach($times as $time) echo "\ndate:".date("Y-m-d H:i:s",$time);
-	echo "
"; + foreach ($times as $time) { + echo "\ndate:" . date('Y-m-d H:i:s', $time); + } + echo '
'; } -- cgit v1.2.3