summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexKlimenkov <shurick.klimenkov@gmail.com>2016-08-31 20:52:28 +0300
committerGitHub <noreply@github.com>2016-08-31 20:52:28 +0300
commitd276ce08991385c5853ad24eff0fe38c25ee41d1 (patch)
tree4914c5a9669da7ee9e332a04bdd9156334e0da2f
parentc5dadfc3de449081ce6b0f10ae2ef2795fd575e1 (diff)
parenta664bd255cdede492e0c175be8054c2e863c622e (diff)
downloadscheduler-helper-php-origin/master.zip
scheduler-helper-php-origin/master.tar.gz
scheduler-helper-php-origin/master.tar.bz2
Merge pull request #31 from mperednya/masterHEADorigin/masterorigin/HEADmaster
Fix recurring getting when rec is finished before interval.
-rw-r--r--RecurringType.php5
-rwxr-xr-xSchedulerHelper.php50
-rw-r--r--tests/Data_getData/RecurringBigLength/source.json8
-rw-r--r--tests/Data_getData/RecurringBigLength/target.json3
4 files changed, 46 insertions, 20 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 6b2dd1a..9e58adf 100755
--- a/SchedulerHelper.php
+++ b/SchedulerHelper.php
@@ -235,7 +235,7 @@ class Helper extends DHelper implements IHelper
OR ".$this->getRecurringTypeFieldName()." = '".RecurringType::IS_RECURRING_BREAK."'
OR ".$this->getRecurringTypeFieldName()." IS NULL
)
- AND ".$this->getLengthFieldName()." = '0'
+ AND (".$this->getLengthFieldName()." = '0' OR ".$this->getLengthFieldName()." is NULL)
";
$query = $this->getConnection()->prepare($getEventsSql);
@@ -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..046c10b
--- /dev/null
+++ b/tests/Data_getData/RecurringBigLength/source.json
@@ -0,0 +1,8 @@
+{
+ "settings": {
+ "start_date": "2016-08-12 17:55:00",
+ "end_date": "2016-08-15 23:59:59"
+ },
+ "data":[{ "id": "105", "start_date": "2016-08-09 11:40:00", "end_date": "2016-08-12 11:40:00", "text": "not working", "rec_type": "day_1___#3", "event_pid": "0", "event_length": "169200", "type": "1", "rec_pattern": "day_1___"},
+ { "id": "106", "start_date": "2016-08-09 11:20:00", "end_date": "2016-08-13 10:40:00", "text": "this works", "rec_type": "", "event_pid": "", "event_length": "", "type": "1", "rec_pattern": ""}]
+} \ No newline at end of file
diff --git a/tests/Data_getData/RecurringBigLength/target.json b/tests/Data_getData/RecurringBigLength/target.json
new file mode 100644
index 0000000..9b8ae2b
--- /dev/null
+++ b/tests/Data_getData/RecurringBigLength/target.json
@@ -0,0 +1,3 @@
+{
+ "data": [{"start_date":"2016-08-11 11:40:00","end_date":"2016-08-13 10:40:00","event_pid":"0","text":"not working","id":"105","type":"1","rec_type":"day_1___#3","event_length":"169200"},{"id":"106","start_date":"2016-08-09 11:20:00","end_date":"2016-08-13 10:40:00","text":"this works","rec_type":"","event_pid":"","event_length":"","type":"1"}]
+} \ No newline at end of file