diff options
author | sten <stenmarsh938@gmail.com> | 2016-03-24 14:49:51 +0300 |
---|---|---|
committer | sten <stenmarsh938@gmail.com> | 2016-03-24 14:49:51 +0300 |
commit | 86a3221a06a5ea4267a3ff0115fc846f2c4a1d5e (patch) | |
tree | f5f676fa578417c19b19b85b535acc2485b986d5 | |
parent | ee79b3b45ddeaea99e8fe5db4ea3b1b431f5efd2 (diff) | |
download | scheduler-helper-php-86a3221a06a5ea4267a3ff0115fc846f2c4a1d5e.zip scheduler-helper-php-86a3221a06a5ea4267a3ff0115fc846f2c4a1d5e.tar.gz scheduler-helper-php-86a3221a06a5ea4267a3ff0115fc846f2c4a1d5e.tar.bz2 |
Fix incorrect recurring finishing if date on current week is out of bonds.
-rw-r--r-- | RecurringType.php | 14 | ||||
-rwxr-xr-x | SchedulerHelper.php | 10 | ||||
-rwxr-xr-x | SchedulerHelperDate.php | 30 |
3 files changed, 46 insertions, 8 deletions
diff --git a/RecurringType.php b/RecurringType.php index 229200e..4a09d59 100644 --- a/RecurringType.php +++ b/RecurringType.php @@ -34,9 +34,9 @@ class RecurringType { private $_recurring_start_date_stamp; private $_recurring_end_date_stamp; - public static $start_on_monday = true; + private $_config = array(); - public function __construct($recurringType, $recurringStartDateStamp, $recurringEndDateStamp) + public function __construct($recurringType, $recurringStartDateStamp, $recurringEndDateStamp, $config = array()) { if(is_array($recurringType)) $recurringType = self::parseRecurringDataArrayToString($recurringType); @@ -44,6 +44,7 @@ class RecurringType { $this->_fields_values = self::_parseRecurringDataString($recurringType); $this->_recurring_start_date_stamp = $recurringStartDateStamp; $this->_recurring_end_date_stamp = $recurringEndDateStamp; + $this->_config = $config; } public static function getInstance($recurringTypeString, $recurringStartDateStamp, $recurringEndDateStamp) { @@ -334,7 +335,7 @@ class RecurringType { private function _getRecurringDayStep($dateStamp, $recurringWeekDay) { $weekDay = SchedulerHelperDate::getDayOfWeek($dateStamp); - if(self::$start_on_monday) { + if($this->_config["start_on_monday"]) { $recurringWeekDay = $recurringWeekDay == 0 ? 7 : $recurringWeekDay; } $dayStep = $recurringWeekDay - $weekDay; @@ -401,6 +402,7 @@ class RecurringType { $intervalStartDateStamp = $correctedInterval["start_date_stamp"]; $intervalEndDateStamp = $correctedInterval["end_date_stamp"]; $currentRecurringStartDateStamp = $intervalStartDateStamp; + $correcterRecurringStartDateStamp = $currentRecurringStartDateStamp; $recurringDates = array(); $recurringStartDateStamp = $this->_recurring_start_date_stamp; $recurringEndDateStamp = $this->_recurring_end_date_stamp; @@ -411,7 +413,7 @@ class RecurringType { (!is_null($countDates) && ($countRecurringCycles <= $countDates)) || ( ($intervalStartDateStamp <= $currentRecurringStartDateStamp) - && ($currentRecurringStartDateStamp < $intervalEndDateStamp) + && ($correcterRecurringStartDateStamp < $intervalEndDateStamp) ) ) { $countRecurringCycles++; @@ -421,18 +423,22 @@ class RecurringType { switch($recType) { case self::REC_TYPE_DAY: $currentRecurringStartDateStamp = SchedulerHelperDate::addDays($currentRecurringStartDateStamp, $recurringTypeStep); + $correcterRecurringStartDateStamp = $currentRecurringStartDateStamp; break; case self::REC_TYPE_WEEK: $currentRecurringStartDateStamp = SchedulerHelperDate::addWeeks($currentRecurringStartDateStamp, $recurringTypeStep); + $correcterRecurringStartDateStamp = SchedulerHelperDate::weekStart($currentRecurringStartDateStamp); break; case self::REC_TYPE_MONTH: $currentRecurringStartDateStamp = SchedulerHelperDate::addMonths($currentRecurringStartDateStamp, $recurringTypeStep); + $correcterRecurringStartDateStamp = SchedulerHelperDate::monthStart($currentRecurringStartDateStamp); break; case self::REC_TYPE_YEAR: $currentRecurringStartDateStamp = SchedulerHelperDate::addYears($currentRecurringStartDateStamp, $recurringTypeStep); + $correcterRecurringStartDateStamp = SchedulerHelperDate::yearStart($currentRecurringStartDateStamp); break; } } diff --git a/SchedulerHelper.php b/SchedulerHelper.php index de5fd59..81ef4f6 100755 --- a/SchedulerHelper.php +++ b/SchedulerHelper.php @@ -33,7 +33,7 @@ abstract class DHelper extends SchedulerHelperConnector public $config = array( "debug" => true, "server_date" => false, - "start_on_monay" => true, + "start_on_monday" => true, "occurrence_timestamp_in_utc" => true ); @@ -374,8 +374,10 @@ class Helper extends DHelper implements IHelper $recField = $this->getRecurringTypeFieldName(); $startField = $this->getStartDateFieldName(); $endField = $this->getEndDateFieldName(); - - RecurringType::$start_on_monday = $this->config["start_on_monay"]; + $recConfig = array( + "start_on_monday" => $this->config["start_on_monday"] + ); + $recCount = count($recurringEvents); for($i = 0; $i < $recCount; $i++) { $eventData = $recurringEvents[$i]; @@ -384,7 +386,7 @@ class Helper extends DHelper implements IHelper $recurringTypeData = $eventData[$recField]; $recurringStartDateStamp = $this->getDateTimestamp($eventData[$startField]); $recurringEndDateStamp = $this->getDateTimestamp($eventData[$endField]); - $recurringTypeObj = new RecurringType($recurringTypeData, $recurringStartDateStamp, $recurringEndDateStamp); + $recurringTypeObj = new RecurringType($recurringTypeData, $recurringStartDateStamp, $recurringEndDateStamp, $recConfig); //Get recurring dates by parsed format. $recurringDatesStamps = $recurringTypeObj->getRecurringDates($intervalStartDateStamp, $intervalEndDateStamp); diff --git a/SchedulerHelperDate.php b/SchedulerHelperDate.php index 0836997..33635a9 100755 --- a/SchedulerHelperDate.php +++ b/SchedulerHelperDate.php @@ -118,4 +118,34 @@ class SchedulerHelperDate return self::addDate($timestamp, self::$INTERVAL_UNITS["year"], $count); } + static public function weekStart($timestamp, $startOnMonday = true){ + $shift = self::getDayOfWeek($timestamp); + if($startOnMonday){ + if($shift === 0){ + $shift = 6; + } + else{ + $shift--; + } + } + return self::addDays($timestamp, -1*$shift); + } + + static public function monthStart($timestamp){ + $date = new DateTime(); + $date->setTimestamp($timestamp); + $m = $date->format('m'); + $y = $date->format('Y'); + $date->setDate($y, $m, 1); + return $date->getTimestamp(); + } + + static public function yearStart($timestamp){ + $date = new DateTime(); + $date->setTimestamp($timestamp); + $y = $date->format('Y'); + $date->setDate($y, 1, 1); + return $date->getTimestamp(); + } + }
\ No newline at end of file |