Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
methodschedule.php
1<?php
9
11
15
16Loc::loadMessages(__FILE__);
17
18class MethodSchedule implements iMethod
19{
21 protected $monthsOfYear = array();
22
24 protected $daysOfMonth = array();
25
27 protected $daysOfWeek = array();
28
30 protected $hours = 0;
31
33 protected $minutes = 0;
34
36 private $letter;
37
43 public function __construct(Entity\Letter $letter)
44 {
45 $this->letter = $letter;
46 }
47
54 public function setDaysOfMonth(array $days)
55 {
56 $this->daysOfMonth = $days;
57 return $this;
58 }
59
66 public function setDaysOfWeek(array $days)
67 {
68 $this->daysOfWeek = $days;
69 return $this;
70 }
71
78 public function setMonthsOfYear(array $months)
79 {
80 $this->monthsOfYear = $months;
81 return $this;
82 }
83
91 public function setTime($hours, $minutes)
92 {
93 $this->hours = $hours;
94 $this->minutes = $minutes;
95 return $this;
96 }
97
98 private function getDateTimeByData(array $months = [], array $days = [])
99 {
100 if (empty($months))
101 {
102 for ($i = 1; $i <= 12; $i++)
103 {
104 $months[] = $i;
105 }
106 }
107 if (empty($days))
108 {
109 for ($i = 1; $i <= 31; $i++)
110 {
111 $days[] = $i;
112 }
113 }
114 foreach ([false, true] as $nextYear)
115 {
116 foreach ($months as $month)
117 {
118 foreach ($days as $day)
119 {
120 $date = $this->getDateTime($month, $day, $nextYear);
121 if ($this->checkDateTime($date))
122 {
123 return $date;
124 }
125 }
126 }
127 }
128
129 return null;
130 }
131
132 private function checkDateTime(DateTime $date = null)
133 {
134 static $current = null;
135 if ($current === null)
136 {
137 $current = time();
138 }
139
140 if (!$date)
141 {
142 return false;
143 }
144
145 return $current < $date->getTimestamp();
146 }
147
148 private function getDateTime($month = null, $day = null, $nextYear = false)
149 {
150 if ($month === null && $day === null)
151 {
152 $date = new DateTime();
153 }
154 else
155 {
157 mktime(0, 0, 0, $month, $day, date('Y') + ($nextYear ? 1 : 0))
158 );
159 }
160
161 return $date->setTime((int) $this->hours, (int) $this->minutes);
162 }
163
170 public function getNextDate()
171 {
172 if (empty($this->daysOfWeek) && empty($this->monthsOfYear) && empty($this->daysOfMonth))
173 {
174 return null;
175 }
176
177 if (!empty($this->monthsOfYear) || !empty($this->daysOfMonth))
178 {
179 $date = $this->getDateTimeByData($this->monthsOfYear, $this->daysOfMonth);
180 }
181 else
182 {
183 $date = $this->getDateTime();
184 for($i = 0; $i < 7; $i++)
185 {
186 if ($i > 0)
187 {
188 $date->add("+1 days");
189 }
190
191 if (!$this->checkDateTime($date))
192 {
193 continue;
194 }
195
196 $day = (int) date('w', $date->getTimestamp());
197 $day = $day === 0 ? 7 : $day;
198 if (in_array($day, $this->daysOfWeek))
199 {
200 break;
201 }
202 }
203 }
204
205 return $date;
206 }
207
213 public function apply()
214 {
215 $this->letter->set('MONTHS_OF_YEAR', implode(',', $this->monthsOfYear));
216 $this->letter->set('DAYS_OF_MONTH', implode(',', $this->daysOfMonth));
217 $this->letter->set('DAYS_OF_WEEK', implode(',', $this->daysOfWeek));
218 $this->letter->set('TIMES_OF_DAY', $this->hours ? ($this->hours . ':' . $this->minutes) : null);
219 $this->letter->set('REITERATE', 'Y');
220 $this->letter->set('AUTO_SEND_TIME', $this->getNextDate());
221 $this->letter->save();
222 $this->letter->getState()->wait($this);
223
224 }
225
231 public function revoke()
232 {
233 $this->letter->set('MONTHS_OF_YEAR', null);
234 $this->letter->set('DAYS_OF_MONTH', null);
235 $this->letter->set('DAYS_OF_WEEK', null);
236 $this->letter->set('TIMES_OF_DAY', null);
237 $this->letter->set('REITERATE', 'N');
238 }
239
245 public function getCode()
246 {
247 return Method::SCHEDULE;
248 }
249
256 public static function parseDaysOfMonth($daysOfMonth)
257 {
258 $result = [];
259 if ($daysOfMonth <> '')
260 {
261 $days = explode(",", $daysOfMonth);
262 $found = [];
263 foreach ($days as $day)
264 {
265 $day = trim($day);
266 if (preg_match("/^(\d{1,2})$/", $day, $found))
267 {
268 if (intval($found[1]) < 1 || intval($found[1]) > 31)
269 {
270 return [];
271 }
272 else
273 {
274 $result[] = intval($found[1]);
275 }
276 }
277 elseif (preg_match("/^(\d{1,2})-(\d{1,2})$/", $day, $found))
278 {
279 if (intval($found[1]) < 1 || intval($found[1]) > 31 || intval($found[2]) < 1 || intval($found[2]) > 31 || intval($found[1]) >= intval($found[2]))
280 {
281 return [];
282 }
283 else
284 {
285 for ($i = intval($found[1]); $i <= intval($found[2]); $i++)
286 {
287 $result[] = intval($i);
288 }
289 }
290 }
291 else
292 {
293 return [];
294 }
295 }
296 }
297 else
298 {
299 return [];
300 }
301
302 return $result;
303 }
304
311 public static function parseDaysOfWeek($daysOfWeek)
312 {
313 if($daysOfWeek == '')
314 {
315 return [];
316 }
317
318 $result = [];
319 $days = explode(",", $daysOfWeek);
320 foreach($days as $day)
321 {
322 $day = trim($day);
323 $found = [];
324 if(
325 preg_match("/^(\d)$/", $day, $found)
326 && $found[1] >= 1
327 && $found[1] <= 7
328 )
329 {
330 $result[]=intval($found[1]);
331 }
332 else
333 {
334 return [];
335 }
336 }
337
338 return $result;
339 }
340
347 public static function parseMonthsOfYear($monthsOfYear)
348 {
349 if($monthsOfYear == '')
350 {
351 return [];
352 }
353
354 $result = [];
355 $months = explode(",", $monthsOfYear);
356 foreach($months as $month)
357 {
358 $month = trim($month);
359 $found = [];
360 if(
361 preg_match("/^(\d{1,2})$/", $month, $found)
362 && $found[1] >= 1
363 && $found[1] <= 12
364 )
365 {
366 $result[]=intval($found[1]);
367 }
368 else
369 {
370 return [];
371 }
372 }
373
374 return $result;
375 }
376
383 public static function parseTimesOfDay($time)
384 {
385 if($time == '')
386 {
387 return null;
388 }
389
390 $time = trim($time);
391 $found = [];
392 if(
393 preg_match("/^(\d{1,2}):(\d{1,2})$/", $time, $found)
394 && $found[1] <= 23
395 && $found[2] <= 59
396 )
397 {
398 return [$found[1], $found[2]];
399 }
400 else
401 {
402 return null;
403 }
404 }
405
411 public static function getTimeList()
412 {
413 $list = [];
414 $timesOfDayHours = ['00', '30'];
415 for ($hour = 0; $hour < 24; $hour++)
416 {
417 $hourPrint = str_pad($hour, 2, "0", STR_PAD_LEFT);
418 foreach ($timesOfDayHours as $timePartHour)
419 {
420 $list[] = $hourPrint . ":" . $timePartHour;
421 }
422 }
423
424 return $list;
425 }
426}
static loadMessages($file)
Definition loc.php:64
static createFromTimestamp($timestamp)
Definition datetime.php:246