1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
portalschedule.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
6
9
11{
12 protected const WEEK_DAYS_COUNT = 7;
13 protected const MINUTES_IN_HOUR = 60;
14 protected const SECONDS_IN_HOUR = 60 * 60;
15 protected const SECONDS_IN_MINUTE = 60;
16 protected const DAY_MAP = [
17 'MO' => 1,
18 'TU' => 2,
19 'WE' => 3,
20 'TH' => 4,
21 'FR' => 5,
22 'SA' => 6,
23 'SU' => 0,
24 ];
25 protected const DEFAULT_SETTINGS = [
26 'HOURS' => [
27 'START' => [
28 'H' => 9,
29 'M' => 0,
30 'S' => 0,
31 ],
32 'END' => [
33 'H' => 19,
34 'M' => 0,
35 'S' => 0,
36 ],
37 ],
38 'HOLIDAYS' => [],
39 'WEEKEND' => ['SA', 'SU'],
40 'WEEK_START' => 'MO',
41 ];
42
44 protected array $settings;
45
46 protected array $weekend = [];
47 protected array $holidays = [];
48 protected array $workTime = [];
49 protected int $startHour = 9;
50 protected int $startMinute = 0;
51 protected int $endHour = 19;
52 protected int $endMinute = 0;
53
54 public function __construct(?array $settings = null)
55 {
56 $this->settings = $settings ?? $this->getPortalSettings();
57
58 $this->parseWeekend();
59 $this->parseHolidays();
60 $this->parseWorkHours();
61 $this->parseWorkTime();
62 }
63
64 public function getShiftStart(?DateTime $date = null): DateTime
65 {
66 $date ??= new DateTime();
67
68 return (clone $date)->setTime($this->startHour, $this->startMinute);
69 }
70
71 public function getShiftEnd(?DateTime $date = null): DateTime
72 {
73 $date ??= new DateTime();
74
75 return (clone $date)->setTime($this->endHour, $this->endMinute);
76 }
77
78 public function getWorkDayDuration(?DateTime $date = null): int
79 {
80 $secondsInStartOfWorkDay = $this->startHour * static::SECONDS_IN_HOUR + $this->startMinute * self::SECONDS_IN_MINUTE;
81 $secondsInEndOfWorkDay = $this->endHour * static::SECONDS_IN_HOUR + $this->endMinute * self::SECONDS_IN_MINUTE;
82
83 return $secondsInEndOfWorkDay - $secondsInStartOfWorkDay;
84 }
85
86 public function isWorkTime(DateTime $date): bool
87 {
88 $start = $this->startHour * self::MINUTES_IN_HOUR + $this->startMinute;
89 $end = $this->endHour * self::MINUTES_IN_HOUR + $this->endMinute;
90 $now = (int)$date->format('H') * self::MINUTES_IN_HOUR + (int)$date->format('i');
91
92 return $now >= $start && $now <= $end;
93 }
94
95 public function isWeekend(DateTime $date): bool
96 {
97 $weekday = (int)$date->format('w');
98
99 if (isset($this->weekend[$weekday]))
100 {
101 return true;
102 }
103
104 $month = (int)$date->format('n');
105 $day = (int)$date->format('j');
106
107 return isset($this->holidays[$month . '_' . $day]);
108 }
109
110 protected function setSettings(array $settings): void
111 {
112 if (!empty($settings['HOURS']) && is_array($settings['HOURS']))
113 {
114 $hours = $settings['HOURS'];
115
116 $this->startHour = (int)$hours['START']['H'];
117 $this->startMinute = (int)$hours['START']['M'];
118 $this->endHour = (int)$hours['END']['H'];
119 $this->endMinute = (int)$hours['END']['M'];
120 }
121
122 $this->settings = $settings;
123
124 $this->weekend = [];
125 $this->parseWeekend();
126 $this->parseHolidays();
127 $this->parseWorkTime();
128 }
129
130 protected function getPortalSettings(): array
131 {
132 $settings = static::DEFAULT_SETTINGS;
134 if(empty($calendarSettings))
135 {
136 return $settings;
137 }
138
139 if (is_array($calendarSettings['week_holidays']))
140 {
141 $settings['WEEKEND'] = $calendarSettings['week_holidays'];
142 }
143
144 if ((string)$calendarSettings['year_holidays'] !== '')
145 {
146 $holidays = explode(',', $calendarSettings['year_holidays']);
147 if (is_array($holidays) && !empty($holidays))
148 {
149 foreach ($holidays as $day)
150 {
151 $day = trim($day);
152 [$day, $month] = explode('.', $day);
153
154 if ($day && $month)
155 {
156 $settings['HOLIDAYS'][] = ['M' => (int)$month, 'D' => (int)$day];
157 }
158 }
159 }
160 }
161
162 $timeStart = explode('.', (string)$calendarSettings['work_time_start']);
163
164 if (isset($timeStart[0]))
165 {
166 $settings['HOURS']['START']['H'] = (int)$timeStart[0];
167 }
168 if (isset($timeStart[1]))
169 {
170 $settings['HOURS']['START']['M'] = (int)$timeStart[1];
171 }
172
173 $timeEnd = explode('.', (string)$calendarSettings['work_time_end']);
174
175 if (isset($timeEnd[0]))
176 {
177 $settings['HOURS']['END']['H'] = (int)$timeEnd[0];
178 }
179 if (isset($timeEnd[1]))
180 {
181 $settings['HOURS']['END']['M'] = (int)$timeEnd[1];
182 }
183
184 return $settings;
185 }
186
187 public function getSettings(): array
188 {
189 return $this->settings;
190 }
191
192 private function parseWeekend(): void
193 {
194 if(!empty($this->settings['WEEKEND']) && is_array($this->settings['WEEKEND']))
195 {
196 foreach ($this->settings['WEEKEND'] as $day)
197 {
198 $this->weekend[static::DAY_MAP[$day]] = true;
199 }
200 }
201
202 if(count($this->weekend) === static::WEEK_DAYS_COUNT)
203 {
204 $this->weekend = [static::DAY_MAP['SA'] => true, static::DAY_MAP['SU'] => true];
205 }
206 }
207
208 private function parseHolidays(): void
209 {
210 $holidays = $this->settings['HOLIDAYS'] ?? [];
211
212 foreach ($holidays as $day)
213 {
214 $this->holidays[(int)$day['M'] . '_' . (int)$day['D']] = true;
215 }
216 }
217
218 private function parseWorkHours(): void
219 {
220 $startHour = $this->settings['HOURS']['START']['H'] ?? null;
221
222 if (isset($startHour))
223 {
224 $this->startHour = (int)$startHour;
225 }
226
227 $startMinute = $this->settings['HOURS']['START']['M'] ?? null;
228
229 if (isset($startMinute))
230 {
231 $this->startMinute = (int)$startMinute;
232 }
233
234 $endHour = $this->settings['HOURS']['END']['H'] ?? null;
235
236 if (isset($endHour))
237 {
238 $this->endHour = (int)$endHour;
239 }
240
241 $endMinute = $this->settings['HOURS']['END']['M'] ?? null;
242
243 if (isset($endMinute))
244 {
245 $this->endMinute = (int)$endMinute;
246 }
247 }
248
249 private function parseWorkTime(): void
250 {
251 $this->workTime = [
252 [
253 'start' => [
254 'hours' => $this->startHour,
255 'minutes' => $this->startMinute,
256 ],
257 'end' => [
258 'hours' => $this->endHour,
259 'minutes' => $this->endMinute,
260 ],
261 ],
262 ];
263 }
264}
format($format)
Определения date.php:110
$hours
Определения cron_html_pages.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$start
Определения get_search.php:9
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936