Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
remind.php
1<?php
2
4
9
10class Remind extends BaseProperty
11{
12 public const UNIT_SECONDS = 'seconds';
13 public const UNIT_MINUTES = 'minutes';
14 public const UNIT_HOURS = 'hour';
15 public const UNIT_DAYS = 'day';
16 public const UNIT_WEEKS = 'weeks';
17 public const UNIT_MONTHS = 'months';
18 public const UNIT_YEARS = 'years';
19 public const UNIT_DATES = 'date';
20 public const UNIT_DAY_BEFORE = 'daybefore';
21 protected const MINUTE_PER_DAY = 1440;
22
26 private string $method;
30 private ?int $time = null;
34 private ?string $units = null;
38 private ?Date $specificTime = null;
42 protected ?Date $eventStart = null;
46 private ?int $daysBefore = null;
47
48 public function __construct(string $method = 'popup')
49 {
50 $this->method = $method;
51 }
52
56 public function getFields(): array
57 {
58 return [
59 'method' => $this->method,
60 'time' => $this->time,
61 'units' => $this->units,
62 'specificTime' => $this->specificTime ?? null,
63 'eventStart' => $this->eventStart ?? null,
64 'daysBefore' => $this->daysBefore ?? null
65 ];
66 }
67
72 public function toString(): string
73 {
74 if ($this->specificTime || $this->eventStart)
75 {
76 return (string)$this->getSpecificTime();
77 }
78
79 return '';
80 }
81
87 public function setTimeBeforeEvent(int $time = 15, string $units = self::UNIT_MINUTES): Remind
88 {
89 $this->time = $time;
90 $this->units = $units;
91
92 return $this;
93 }
94
99 public function setSpecificTime(Date $specificTime): Remind
100 {
101 $this->specificTime = $specificTime;
102
103 return $this;
104 }
105
106 public function setDaysBefore(int $daysBefore): Remind
107 {
108 $this->daysBefore = $daysBefore;
109
110 return $this;
111 }
112
117 public function getSpecificTime(): Date
118 {
119 if (
120 $this->specificTime === null
121 && $this->time !== null
122 && $this->units
123 && $this->eventStart !== null
124 )
125 {
126 return (clone $this->eventStart)->sub("{$this->time} {$this->units}");
127 }
128
129 if ($this->specificTime)
130 {
131 return $this->specificTime;
132 }
133
134 throw new PropertyException('It is impossible to perform this operation. You should set property $eventStart or $specificTime');
135 }
136
142 {
143 $this->eventStart = $eventStart;
144
145 return $this;
146 }
147
151 public function isBeforeEventStart(): bool
152 {
153 if ($this->checkSpecificTime())
154 {
155 try
156 {
157 return $this->getSpecificTime()->getTimestamp() <= $this->eventStart->getTimestamp();
158 }
159 catch (PropertyException $e)
160 {
161 return false;
162 }
163 }
164
165 return false;
166 }
167
173 public static function calculateDayBeforeToMinute(int $dayBefore, int $minute): int
174 {
175 return ($dayBefore * self::MINUTE_PER_DAY) - $minute;
176 }
177
181 private function checkSpecificTime(): bool
182 {
183 return $this->specificTime || $this->eventStart;
184 }
185
189 public function getTimeBeforeStartInMinutes(): int
190 {
191 if (!$this->checkSpecificTime())
192 {
193 return 0;
194 }
195
196 try
197 {
198 $delta = $this->eventStart->getTimestamp() - $this->getSpecificTime()->getTimestamp();
199
200 return $delta / 60;
201 }
202 catch (PropertyException $e)
203 {
204 return 0;
205 }
206 }
207
211 public function getRank(): int
212 {
213 $rank = 0;
214 if (!empty($this->daysBefore))
215 {
216 $rank = 100;
217 }
218 elseif (!empty($this->specificTime))
219 {
220 $rank = 10;
221 }
222 elseif(!empty($this->units))
223 {
224 $rankMap = [
225 self::UNIT_SECONDS => 1,
226 self::UNIT_MINUTES => 2,
227 self::UNIT_HOURS => 3,
228 self::UNIT_DAYS => 4,
229 self::UNIT_WEEKS => 5,
230 self::UNIT_MONTHS => 6,
231 self::UNIT_YEARS => 7,
232 ];
233 $rank = $rankMap[$this->units] ?? 0;
234 }
235
236 return $rank;
237 }
238
242 public function getMethod(): string
243 {
244 return $this->method;
245 }
246
250 public function getTime(): ?int
251 {
252 return $this->time ?? null;
253 }
254
258 public function getUnits(): ?string
259 {
260 return $this->units ?? null;
261 }
262
266 public function getDaysBefore(): ?int
267 {
268 return $this->daysBefore ?? null;
269 }
270
274 public function getEventStart(): ?Date
275 {
276 return isset($this->eventStart)
277 ? (clone $this->eventStart)
278 : null
279 ;
280 }
281
287 public function getTimeOffset(): int
288 {
289 $time = $this->getSpecificTime();
290 return 60 * intval($time->format('H'))
291 + intval($time->format('i'));
292 }
293
297 public function isSimpleType(): bool
298 {
299 return !empty($this->units);
300 }
301}
sub(string $subtractTime)
Definition date.php:140
setTimeBeforeEvent(int $time=15, string $units=self::UNIT_MINUTES)
Definition remind.php:87
__construct(string $method='popup')
Definition remind.php:48
static calculateDayBeforeToMinute(int $dayBefore, int $minute)
Definition remind.php:173