diff options
author | sten <stenmarsh938@gmail.com> | 2016-08-31 20:46:13 +0300 |
---|---|---|
committer | sten <stenmarsh938@gmail.com> | 2016-08-31 20:46:23 +0300 |
commit | 9f53822571b0af4483457250ecd7318e8118af5d (patch) | |
tree | 088e80face2e0073602fec47a46a6b6b4002c74e | |
parent | cf635306561a64ef60037be1759ba87c1fb8edd3 (diff) | |
download | scheduler-helper-php-9f53822571b0af4483457250ecd7318e8118af5d.zip scheduler-helper-php-9f53822571b0af4483457250ecd7318e8118af5d.tar.gz scheduler-helper-php-9f53822571b0af4483457250ecd7318e8118af5d.tar.bz2 |
Fix getting recurring when event ends before interval.
-rw-r--r-- | RecurringType.php | 5 | ||||
-rwxr-xr-x | SchedulerHelper.php | 48 | ||||
-rw-r--r-- | tests/Data_getData/RecurringBigLength/source.json | 0 | ||||
-rw-r--r-- | tests/Data_getData/RecurringBigLength/target.json | 0 |
4 files changed, 34 insertions, 19 deletions
diff --git a/RecurringType.php b/RecurringType.php index bb82a3e..f5a52b8 100644 --- a/RecurringType.php +++ b/RecurringType.php @@ -33,10 +33,11 @@ class RecurringType { private $_fields_values = array(); private $_recurring_start_date_stamp; private $_recurring_end_date_stamp; + private $_recurring_length; private $_config = array(); - public function __construct($recurringType, $recurringStartDateStamp, $recurringEndDateStamp, $config = array()) + public function __construct($recurringType, $recurringStartDateStamp, $recurringEndDateStamp, $recurringLength = 0, $config = array()) { if(is_array($recurringType)) $recurringType = self::parseRecurringDataArrayToString($recurringType); @@ -44,6 +45,7 @@ class RecurringType { $this->_fields_values = self::_parseRecurringDataString($recurringType); $this->_recurring_start_date_stamp = $recurringStartDateStamp; $this->_recurring_end_date_stamp = $recurringEndDateStamp; + $this->_recurring_length = $recurringLength; $this->_config = $config; } @@ -397,6 +399,7 @@ class RecurringType { return false; //Correct interval by recurring interval. + $intervalStartDateStamp -= $this->_recurring_length; //If event ends before interval but event is in interval because of length $correctedInterval = $this->_getCorrectedRecurringInterval($intervalStartDateStamp, $intervalEndDateStamp); $intervalStartDateStamp = $correctedInterval["start_date_stamp"]; $intervalEndDateStamp = $correctedInterval["end_date_stamp"]; diff --git a/SchedulerHelper.php b/SchedulerHelper.php index 2d8983c..9e58adf 100755 --- a/SchedulerHelper.php +++ b/SchedulerHelper.php @@ -269,33 +269,43 @@ class Helper extends DHelper implements IHelper * @return array */ private function _getRecurringEventsByInterval($startDate, $endDate) - { - $getEventsSql = " + { + $getEventsSql = " SELECT * FROM - ".$this->getTablename()." + " . $this->getTablename() . " WHERE ( - ( - ".$this->getEndDateFieldName()." >= '{$startDate}' - AND ".$this->getStartDateFieldName()." <= '{$endDate}' - ) - ) - AND ( - ".$this->getRecurringTypeFieldName()." != '".RecurringType::IS_RECURRING_EXCEPTION."' - AND ".$this->getRecurringTypeFieldName()." != '".RecurringType::IS_RECURRING_BREAK."' - AND ".$this->getRecurringTypeFieldName()." IS NOT NULL + " . $this->getRecurringTypeFieldName() . " != '" . RecurringType::IS_RECURRING_EXCEPTION . "' + AND " . $this->getRecurringTypeFieldName() . " != '" . RecurringType::IS_RECURRING_BREAK . "' + AND " . $this->getRecurringTypeFieldName() . " IS NOT NULL ) - AND ".$this->getLengthFieldName()." != '0' + AND " . $this->getLengthFieldName() . " != '0' "; - $query = $this->getConnection()->prepare($getEventsSql); - $query->execute(); + $query = $this->getConnection()->prepare($getEventsSql); + $query->execute(); $data = $query->fetchAll(); $this->closeConnection(); - return $data; - } + + $startDateFieldName = $this->getStartDateFieldName(); + $endDateFieldName = $this->getEndDateFieldName(); + $lengthFieldName = $this->getLengthFieldName(); + + $startStamp = $this->getDateTimestamp($startDate); + $endStamp = $this->getDateTimestamp($endDate); + + $data = array_filter($data, function ($el) use ($startDateFieldName, $endDateFieldName, $lengthFieldName, $startStamp, $endStamp) { + $evStart = $this->getDateTimestamp($el[$startDateFieldName]); + $evEnd = $this->getDateTimestamp($el[$endDateFieldName]); + $evLength = $el[$lengthFieldName]; + + return $evEnd + $evLength > $startStamp && $evStart < $endStamp; + }); + + return $data; + } /** * Exclude event extra data. @@ -374,6 +384,7 @@ class Helper extends DHelper implements IHelper $recField = $this->getRecurringTypeFieldName(); $startField = $this->getStartDateFieldName(); $endField = $this->getEndDateFieldName(); + $lengthField = $this->getLengthFieldName(); $recConfig = array( "start_on_monday" => $this->config["start_on_monday"] ); @@ -384,9 +395,10 @@ class Helper extends DHelper implements IHelper //Parse recurring data format. $recurringTypeData = $eventData[$recField]; + $recLength = $eventData[$lengthField]; $recurringStartDateStamp = $this->getDateTimestamp($eventData[$startField]); $recurringEndDateStamp = $this->getDateTimestamp($eventData[$endField]); - $recurringTypeObj = new RecurringType($recurringTypeData, $recurringStartDateStamp, $recurringEndDateStamp, $recConfig); + $recurringTypeObj = new RecurringType($recurringTypeData, $recurringStartDateStamp, $recurringEndDateStamp, $recLength, $recConfig); //Get recurring dates by parsed format. $recurringDatesStamps = $recurringTypeObj->getRecurringDates($intervalStartDateStamp, $intervalEndDateStamp); diff --git a/tests/Data_getData/RecurringBigLength/source.json b/tests/Data_getData/RecurringBigLength/source.json new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/Data_getData/RecurringBigLength/source.json diff --git a/tests/Data_getData/RecurringBigLength/target.json b/tests/Data_getData/RecurringBigLength/target.json new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/Data_getData/RecurringBigLength/target.json |