From e03b95f43d3e332cfbd1a21dfcb001ce6d6792b6 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Sat, 30 Oct 2010 17:36:40 +0200 Subject: [PATCH] code cleanup --- helpers/SG_iCal_Freq.php | 122 +++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/helpers/SG_iCal_Freq.php b/helpers/SG_iCal_Freq.php index 28dc39e..9057c8a 100755 --- a/helpers/SG_iCal_Freq.php +++ b/helpers/SG_iCal_Freq.php @@ -1,10 +1,10 @@ 'yearly', 'interval'=>1); protected $start = 0; protected $freq = ''; - + /** * Constructs a new Freqency-rule - * @param $rule string + * @param $rule string * @param $start int Unix-timestamp (important!) */ public function __construct( $rule, $start ) { $this->start = $start; - + $rules = array(); foreach( explode(';', $rule) AS $v) { list($k, $v) = explode('=', $v); @@ -50,7 +50,7 @@ class SG_iCal_Freq { $this->rules['until'] = strtotime($this->rules['until']); } $this->freq = strtolower($this->rules['freq']); - + foreach( $this->knownRules AS $rule ) { if( isset($this->rules['by' . $rule]) ) { @@ -59,7 +59,7 @@ class SG_iCal_Freq { } } } - + if(!$this->simpleMode) { if(! (isset($this->rules['byday']) || isset($this->rules['bymonthday']) || isset($this->rules['byyearday']))) { $this->rules['bymonthday'] = date('d', $this->start); @@ -74,7 +74,7 @@ class SG_iCal_Freq { $this->rules['until'] = $n; } } - + /** * Returns the previous (most recent) occurrence of the rule from the * given offset @@ -91,7 +91,7 @@ class SG_iCal_Freq { } return $t1; } - + /** * Returns the next occurrence of this rule after the given offset * @param int $offset @@ -113,36 +113,36 @@ class SG_iCal_Freq { } return $next; } - + /** - * Calculates the next time after the given offset that the rule + * Calculates the next time after the given offset that the rule * will apply. * * The approach to finding the next is as follows: * First we establish a timeframe to find timestamps in. This is * between $offset and the end of the period that $offset is in. - * - * We then loop though all the rules (that is a Prerule in the + * + * We then loop though all the rules (that is a Prerule in the * current freq.), and finds the smallest timestamp inside the * timeframe. * * If we find something, we check if the date is a valid recurrence - * (with validDate). If it is, we return it. Otherwise we try to + * (with validDate). If it is, we return it. Otherwise we try to * find a new date inside the same timeframe (but using the new- * found date as offset) * - * If no new timestamps were found in the period, we try in the + * If no new timestamps were found in the period, we try in the * next period * * @param int $offset * @return int */ public function findNext($offset) { - $echo = false; + $debug = false; //make sure the offset is valid if( $offset === false || (isset($this->rules['until']) && $offset > $this->rules['until']) ) { - if($echo) echo 'STOP: ' . date('r', $offset) . "\n"; + if($debug) echo 'STOP: ' . date('r', $offset) . "\n"; return false; } @@ -150,12 +150,12 @@ class SG_iCal_Freq { //set the timestamp of the offset (ignoring hours and minutes unless we want them to be //part of the calculations. - if($echo) echo 'O: ' . date('r', $offset) . "\n"; + if($debug) echo 'O: ' . date('r', $offset) . "\n"; $hour = (in_array($this->freq, array('hourly','minutely')) && $offset > $this->start) ? date('H', $offset) : date('H', $this->start); $minute = (($this->freq == 'minutely' || isset($this->rules['byminute'])) && $offset > $this->start) ? date('i', $offset) : date('i', $this->start); $t = mktime($hour, $minute, date('s', $this->start), date('m', $offset), date('d', $offset), date('Y',$offset)); - if($echo) echo 'START: ' . date('r', $t) . "\n"; - + if($debug) echo 'START: ' . date('r', $t) . "\n"; + if( $this->simpleMode ) { if( $offset < $t ) { return $t; @@ -166,10 +166,10 @@ class SG_iCal_Freq { } return $next; } - + $eop = $this->findEndOfPeriod($offset); - if($echo) echo 'EOP: ' . date('r', $eop) . "\n"; - + if($debug) echo 'EOP: ' . date('r', $eop) . "\n"; + foreach( $this->knownRules AS $rule ) { if( $found && isset($this->rules['by' . $rule]) ) { if( $this->isPrerule($rule, $this->freq) ) { @@ -180,7 +180,7 @@ class SG_iCal_Freq { if( $imm === false ) { break; } - if($echo) echo strtoupper($rule) . ': ' . date('r', $imm) . ' A: ' . ((int) ($imm > $offset && $imm < $eop)) . "\n"; + if($debug) echo strtoupper($rule) . ': ' . date('r', $imm) . ' A: ' . ((int) ($imm > $offset && $imm < $eop)) . "\n"; if( $imm > $offset && $imm < $eop && ($_t == null || $imm < $_t) ) { $_t = $imm; } @@ -198,18 +198,18 @@ class SG_iCal_Freq { return $this->start; } else if( $found && ($t != $offset)) { if( $this->validDate( $t ) ) { - if($echo) echo 'OK' . "\n"; + if($debug) echo 'OK' . "\n"; return $t; } else { - if($echo) echo 'Invalid' . "\n"; + if($debug) echo 'Invalid' . "\n"; return $this->findNext($t); } } else { - if($echo) echo 'Not found' . "\n"; + if($debug) echo 'Not found' . "\n"; return $this->findNext( $this->findStartingPoint( $offset, $this->rules['interval'] ) ); - } + } } - + /** * Finds the starting point for the next rule. It goes $interval * 'freq' forward in time since the given offset @@ -229,14 +229,14 @@ class SG_iCal_Freq { } $sp = strtotime($t, $offset); - + if( $truncate ) { $sp = $this->truncateToPeriod($sp, $this->freq); } - + return $sp; } - + /** * Finds the earliest timestamp posible outside this perioid * @param int $offset @@ -245,11 +245,11 @@ class SG_iCal_Freq { public function findEndOfPeriod($offset) { return $this->findStartingPoint($offset, 1); } - + /** * Resets the timestamp to the beginning of the * period specified by freq - * + * * Yes - the fall-through is on purpose! * * @param int $time @@ -283,7 +283,7 @@ class SG_iCal_Freq { $d = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']); return $d; } - + /** * Applies the BYDAY rule to the given timestamp * @param string $rule @@ -293,24 +293,24 @@ class SG_iCal_Freq { private function ruleByday($rule, $t) { $dir = ($rule{0} == '-') ? -1 : 1; $dir_t = ($dir == 1) ? 'next' : 'last'; - - + + $d = $this->weekdays[substr($rule,-2)]; $s = $dir_t . ' ' . $d . ' ' . date('H:i:s',$t); - + if( $rule == substr($rule, -2) ) { if( date('l', $t) == ucfirst($d) ) { $s = 'today ' . date('H:i:s',$t); } - + $_t = strtotime($s, $t); - + if( $_t == $t && in_array($this->freq, array('monthly', 'yearly')) ) { // Yes. This is not a great idea.. but hey, it works.. for now $s = 'next ' . $d . ' ' . date('H:i:s',$t); $_t = strtotime($s, $_t); } - + return $_t; } else { $_f = $this->freq; @@ -323,10 +323,10 @@ class SG_iCal_Freq { $_t = $this->truncateToPeriod($t, $this->freq); } $this->freq = $_f; - + $c = preg_replace('/[^0-9]/','',$rule); $c = ($c == '') ? 1 : $c; - + $n = $_t; while($c > 0 ) { if( $dir == 1 && $c == 1 && date('l', $t) == ucfirst($d) ) { @@ -335,11 +335,11 @@ class SG_iCal_Freq { $n = strtotime($s, $n); $c--; } - + return $n; } } - + private function ruleBymonth($rule, $t) { $_t = mktime(date('H',$t), date('i',$t), date('s',$t), $rule, date('d', $t), date('Y', $t)); if( $t == $_t && isset($this->rules['byday']) ) { @@ -349,14 +349,14 @@ class SG_iCal_Freq { return $_t; } } - + private function ruleBymonthday($rule, $t) { if( $rule < 0 ) { $rule = date('t', $t) + $rule + 1; } return mktime(date('H',$t), date('i',$t), date('s',$t), date('m', $t), $rule, date('Y', $t)); } - + private function ruleByyearday($rule, $t) { if( $rule < 0 ) { $_t = $this->findEndOfPeriod(); @@ -376,12 +376,12 @@ class SG_iCal_Freq { } else { $_t = $this->truncateToPeriod($t, $this->freq); $d = '+'; - } + } $sub = (date('W', $_t) == 1) ? 2 : 1; $s = $d . abs($rule - $sub) . ' weeks ' . date('H:i:s',$t); $_t = strtotime($s, $_t); - + return $_t; } @@ -389,17 +389,17 @@ class SG_iCal_Freq { $_t = mktime($rule, date('i',$t), date('s',$t), date('m',$t), date('d', $t), date('Y', $t)); return $_t; } - + private function ruleByminute($rule, $t) { $_t = mktime(date('h',$t), $rule, date('s',$t), date('m',$t), date('d', $t), date('Y', $t)); return $_t; } - + private function validDate( $t ) { if( isset($this->rules['until']) && $t > $this->rules['until'] ) { return false; } - + if( isset($this->rules['bymonth']) ) { $months = explode(',', $this->rules['bymonth']); if( !in_array(date('m', $t), $months)) { @@ -438,10 +438,10 @@ class SG_iCal_Freq { return false; } } - + return true; } - + private function isPrerule($rule, $freq) { if( $rule == 'year') return false; @@ -456,11 +456,11 @@ class SG_iCal_Freq { return true; if( $rule == 'day' && in_array($freq, array('yearly', 'monthly', 'weekly'))) return true; - if( $rule == 'hour' && in_array($freq, array('yearly', 'monthly', 'weekly', 'daily'))) + if( $rule == 'hour' && in_array($freq, array('yearly', 'monthly', 'weekly', 'daily'))) return true; - if( $rule == 'minute' ) + if( $rule == 'minute' ) return true; - + return false; } }