1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
<?php
namespace DHTMLX_Scheduler;
use Exception;
class RecurringType {
//RECURRING DATA FIELDS
const FLD_REC_TYPE = "type";
const FLD_REC_TYPE_STEP = "count";
const FLD_WEEK_DAY = "day";
const FLD_WEEK_NUMBER = "count2";
const FLD_WEEK_DAYS_LIST = "days";
const FLD_REPEAT = "repeat";
//RECURRING TYPES
const REC_TYPE_DAY = "day";
const REC_TYPE_WEEK = "week";
const REC_TYPE_MONTH = "month";
const REC_TYPE_YEAR = "year";
//RECURRING VALUES
const IS_RECURRING_EXCEPTION = "";
const IS_RECURRING_BREAK = "none";
private $_fields_values = array();
private $_recurring_start_date_stamp;
private $_recurring_end_date_stamp;
public function __construct($recurringType, $recurringStartDateStamp, $recurringEndDateStamp)
{
if(is_array($recurringType))
$recurringType = self::parseRecurringDataArrayToString($recurringType);
$this->_fields_values = self::_parseRecurringDataString($recurringType);
$this->_recurring_start_date_stamp = $recurringStartDateStamp;
$this->_recurring_end_date_stamp = $recurringEndDateStamp;
}
public static function getInstance($recurringTypeString, $recurringStartDateStamp, $recurringEndDateStamp) {
return new self($recurringTypeString, $recurringStartDateStamp, $recurringEndDateStamp);
}
/**
* Get field value.
* @param $fieldName
* @return mixed
* @throws Exception
*/
private function _getFieldValue($fieldName)
{
if(!isset($this->_fields_values[$fieldName]))
throw new Exception("Field '{$fieldName}' not found.");
return $this->_fields_values[$fieldName];
}
/**
* Parse recurring data from array to string.
* @param $dataArray
* @return string
* @throws Exception
*/
static function parseRecurringDataArrayToString($dataArray)
{
$dataFields = array(
self::FLD_REC_TYPE => "each",
self::FLD_REC_TYPE_STEP => "step",
self::FLD_WEEK_NUMBER => "week_number",
self::FLD_WEEK_DAYS_LIST => "days_of_week",
self::FLD_REPEAT => "repeat"
);
$recurringTypes = array(self::REC_TYPE_DAY, self::REC_TYPE_WEEK, self::REC_TYPE_MONTH, self::REC_TYPE_YEAR);
$daysOfWeek = array("sunday" => 0, "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6);
$dataFieldsValues = array();
foreach($dataArray as $field => $value) {
switch($field) {
case $dataFields[self::FLD_REC_TYPE_STEP]:
case $dataFields[self::FLD_REPEAT]:
$value = str_replace(" ", "", $value);
$dataFieldsValues[$field] = $value;
break;
case $dataFields[self::FLD_REC_TYPE]:
$value = str_replace(" ", "", $value);
if(!in_array($value, $recurringTypes))
throw new Exception("Field '{$field}' will contains value of ".join(", ", $recurringTypes));
$dataFieldsValues[$field] = $value;
break;
case $dataFields[self::FLD_WEEK_NUMBER]:
$value = str_replace(" ", "", $value);
if(count(explode(",", $dataArray[$dataFields[self::FLD_WEEK_DAYS_LIST]])) > 1)
throw new Exception("If field {$field} not null, then field ".$dataFields[self::FLD_WEEK_DAYS_LIST]." will contains only one value of ".join(", ", array_keys($daysOfWeek)));
if(!in_array($value, $daysOfWeek))
throw new Exception("Field {$field} will contains value of ".join(",", array_keys($daysOfWeek)));
$dataFieldsValues[$field] = $value;
break;
case $dataFields[self::FLD_WEEK_DAYS_LIST]:
$weekDaysToRecurring = explode(",", $value);
$days = array();
foreach($weekDaysToRecurring as $day) {
$day = str_replace(" ", "", $day);
if(!in_array($day, $daysOfWeek))
throw new Exception("Field {$field} will contains data like 'monday,tuesday,wednesday'.");
array_push($days, $daysOfWeek[$day]);
}
$dataFieldsValues[$field] = join("," ,$days);
break;
default:
$dataFieldsValues[$field] = "";
break;
}
}
//Check required data and fill gaps of data.
$requiredFields = array(
self::FLD_REC_TYPE,
self::FLD_REC_TYPE_STEP
);
foreach($dataFields as $fieldKey => $fieldName) {
if(isset($dataFieldsValues[$fieldName]))
continue;
if(in_array($fieldKey, $requiredFields))
throw new Exception("Field '{$fieldName}' is required");
$dataFieldsValues[$fieldName] = "";
}
$recurringFormat = "%s_%s_%s_%s_%s#%s";
return sprintf(
$recurringFormat,
$dataFieldsValues[$dataFields[self::FLD_REC_TYPE]],
$dataFieldsValues[$dataFields[self::FLD_REC_TYPE_STEP]],
(!empty($dataFieldsValues[$dataFields[self::FLD_WEEK_NUMBER]])) ? $dataFieldsValues[$dataFields[self::FLD_WEEK_DAYS_LIST]] : "",
$dataFieldsValues[$dataFields[self::FLD_WEEK_NUMBER]],
(empty($dataFieldsValues[$dataFields[self::FLD_WEEK_NUMBER]])) ? $dataFieldsValues[$dataFields[self::FLD_WEEK_DAYS_LIST]] : "",
$dataFieldsValues[$dataFields[self::FLD_REPEAT]]
);
}
/**
* Parse recurring data from string.
* @param $dataStr
* @return array
*/
static private function _parseRecurringDataString($dataStr)
{
$formatPartsReg = "/(_|#)/";
$formatDaysListPartReg = "/,/";
$parsedData = array();
$parts = preg_split($formatPartsReg, $dataStr);
list(
$parsedData[self::FLD_REC_TYPE],
$parsedData[self::FLD_REC_TYPE_STEP],
$parsedData[self::FLD_WEEK_DAY],
$parsedData[self::FLD_WEEK_NUMBER],
$parsedData[self::FLD_WEEK_DAYS_LIST]
) = $parts;
if(isset($parts[5]))
{
$parsedData[self::FLD_REPEAT] = $parts[5];
}
$parsedData[self::FLD_WEEK_DAYS_LIST] = ($parsedData[self::FLD_WEEK_DAYS_LIST]) ?
preg_split($formatDaysListPartReg, $parsedData[self::FLD_WEEK_DAYS_LIST]) : array();
return $parsedData;
}
public function getRecurringTypeValue()
{
return $this->_getFieldValue(self::FLD_REC_TYPE);
}
public function getRecurringTypeStepValue()
{
return $this->_getFieldValue(self::FLD_REC_TYPE_STEP);
}
public function getWeekNumberValue()
{
return $this->_getFieldValue(self::FLD_WEEK_NUMBER);
}
public function getWeekDayValue()
{
return $this->_getFieldValue(self::FLD_WEEK_DAY);
}
public function getWeekDaysListValue()
{
return $this->_getFieldValue(self::FLD_WEEK_DAYS_LIST);
}
public function getRepeatValue()
{
return $this->_getFieldValue(self::FLD_REPEAT);
}
/**
* Correcting interval by recurring start($this->_recurring_start_date_stamp)
* and end($this->_recurring_end_date_stamp) dates.
* @param $intervalStartDateStamp
* @param $intervalEndDateStamp
* @return array
*/
private function _getCorrectedRecurringInterval($intervalStartDateStamp, $intervalEndDateStamp)
{
$recurringStartDateStamp = $this->_recurring_start_date_stamp;
$recurringEndDateStamp = $this->_recurring_end_date_stamp;
$recurringInterval = array(
"start_date_stamp" => $intervalStartDateStamp,
"end_date_stamp" => $intervalEndDateStamp
);
//Return recurring interval without correcting if it not belongs to assigned interval.
if(($intervalStartDateStamp >= $recurringEndDateStamp) || ($intervalEndDateStamp <= $recurringStartDateStamp))
return $recurringInterval;
//Correct start date interval if it smaller then recurring start date.
if($intervalStartDateStamp < $recurringStartDateStamp)
$intervalStartDateStamp = $recurringStartDateStamp;
//Correct end date interval if it smaller then recurring end date.
if($intervalEndDateStamp > $recurringEndDateStamp)
$intervalEndDateStamp = $recurringEndDateStamp;
$differenceStartDates = SchedulerHelperDate::differenceBetweenDates($intervalStartDateStamp, $recurringStartDateStamp);
$differenceEndDates = SchedulerHelperDate::differenceBetweenDates($intervalEndDateStamp, $recurringEndDateStamp);
$dateUnits = SchedulerHelperDate::$DATE_UNITS;
//Add years.
$recurringInterval["start_date_stamp"] = SchedulerHelperDate::addYears($recurringStartDateStamp, $differenceStartDates[$dateUnits["year"]]);
$recurringInterval["end_date_stamp"] = SchedulerHelperDate::addYears($recurringEndDateStamp, -$differenceEndDates[$dateUnits["year"]]);
//If recurring type is "year" then exit, else add months.
if($this->getRecurringTypeValue() == self::REC_TYPE_YEAR)
return $recurringInterval;
//Add months.
$recurringInterval["start_date_stamp"] = SchedulerHelperDate::addMonths($recurringInterval["start_date_stamp"], $differenceStartDates[$dateUnits["month"]]);
$recurringInterval["end_date_stamp"] = SchedulerHelperDate::addMonths($recurringInterval["end_date_stamp"], -$differenceEndDates[$dateUnits["month"]]);
return $recurringInterval;
}
/**
* Get step to recurring day from current day of week in date.
* @param $dateStamp
* @param $recurringWeekDay
* @return int
*/
private function _getRecurringDayStep($dateStamp, $recurringWeekDay)
{
$weekDay = SchedulerHelperDate::getDayOfWeek($dateStamp);
$dayStep = $recurringWeekDay - $weekDay;
$dayStep = ($dayStep < 0) ? (SchedulerHelperDate::DAYS_IN_WEEK - (-$dayStep)) : $dayStep;
return $dayStep;
}
/**
* Get recurring days for date.
* @param $dateStamp
* @return array
*/
private function _getRecurringDays($dateStamp)
{
$recurringDays = array();
//If recurring type has list of days, then get those days.
$recurringWeekDays = $this->getWeekDaysListValue();
if($recurringWeekDays) {
for($i = 0; $i < count($recurringWeekDays); $i++) {
$dayStep = $this->_getRecurringDayStep($dateStamp, $recurringWeekDays[$i]);
array_push($recurringDays, SchedulerHelperDate::addDays($dateStamp, $dayStep));
}
}
//Else if recurring type has day of week and step for it, then get this day.
elseif($this->getWeekDayValue() && $this->getWeekNumberValue()) {
$dayStep = $this->_getRecurringDayStep($dateStamp, $this->getWeekDayValue());
$dayStep += (SchedulerHelperDate::DAYS_IN_WEEK * ($this->getWeekNumberValue() - 1));
array_push($recurringDays, SchedulerHelperDate::addDays($dateStamp, $dayStep));
}
//Else return recurring date without change.
else
array_push($recurringDays, $dateStamp);
return $recurringDays;
}
/**
* Get recurring dates by interval or $intervalStartDateStamp and $countDates.
* @param $intervalStartDateStamp
* @param $intervalEndDateStamp
* @param null $countDates
* @return array|bool
*/
public function getRecurringDates($intervalStartDateStamp, $intervalEndDateStamp, $countDates = NULL)
{
if(!($this->getRecurringTypeStepValue() && $this->getRecurringTypeValue()))
return false;
//Correct interval by recurring interval.
$correctedInterval = $this->_getCorrectedRecurringInterval($intervalStartDateStamp, $intervalEndDateStamp);
$intervalStartDateStamp = $correctedInterval["start_date_stamp"];
$intervalEndDateStamp = $correctedInterval["end_date_stamp"];
$currentRecurringStartDateStamp = $intervalStartDateStamp;
$recurringDates = array();
//Generate dates wile next recurring date belongs to interval.
$countRecurringCycles = 0;
while(
(!is_null($countDates) && ($countRecurringCycles <= $countDates))
|| (
($intervalStartDateStamp <= $currentRecurringStartDateStamp)
&& ($currentRecurringStartDateStamp < $intervalEndDateStamp)
)
) {
$countRecurringCycles++;
$recurringDays = $this->_getRecurringDays($currentRecurringStartDateStamp);
$recurringDates = array_merge($recurringDates, $recurringDays);
$recurringTypeStep = $this->getRecurringTypeStepValue();
switch($this->getRecurringTypeValue()) {
case self::REC_TYPE_DAY:
$currentRecurringStartDateStamp = SchedulerHelperDate::addDays($currentRecurringStartDateStamp, $recurringTypeStep);
break;
case self::REC_TYPE_WEEK:
$currentRecurringStartDateStamp = SchedulerHelperDate::addWeeks($currentRecurringStartDateStamp, $recurringTypeStep);
break;
case self::REC_TYPE_MONTH:
$currentRecurringStartDateStamp = SchedulerHelperDate::addMonths($currentRecurringStartDateStamp, $recurringTypeStep);
break;
case self::REC_TYPE_YEAR:
$currentRecurringStartDateStamp = SchedulerHelperDate::addYears($currentRecurringStartDateStamp, $recurringTypeStep);
break;
}
}
return (!is_null($countDates))
? array_splice($recurringDates, (count($recurringDates) - $countDates))
: $recurringDates;
}
/**
* @param $recurringType
* @param $startDateStamp
* @param $eventLength
* @return int|NULL
*/
public static function getRecurringEndDate($recurringType, $startDateStamp, $eventLength)
{
$recurringTypeObj = self::getInstance($recurringType, $startDateStamp, NULL);
$repeatValue = $recurringTypeObj->getRepeatValue();
if(empty($repeatValue))
return ($startDateStamp + $eventLength);
$recurringStartDatesStamps = $recurringTypeObj->getRecurringDates($startDateStamp, NULL, $repeatValue);
$maxEndDateStamp = NULL;
foreach($recurringStartDatesStamps as $startDateStamp) {
$endDateStamp = $startDateStamp + $eventLength;
$maxEndDateStamp = ($endDateStamp > $maxEndDateStamp) ? $endDateStamp : $maxEndDateStamp;
}
return $maxEndDateStamp;
}
}
|