Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
mapper.php
1
<?php
2
3
namespace
Bitrix\Calendar\Sharing\Link\Rule
;
4
5
use
Bitrix\Calendar\Internals\SharingLinkRuleTable
;
6
use
Bitrix\Calendar\Internals\SharingObjectRuleTable
;
7
use
Bitrix\Main
;
8
use
Bitrix\Main\Localization\Loc
;
9
10
class
Mapper
11
{
12
protected
const
DEFAULT_SLOT_SIZE
= 60;
13
protected
const
DEFAULT_FROM
= 540;
14
protected
const
DEFAULT_TO
= 1080;
15
protected
const
DEFAULT_WORKDAYS
= [1, 2, 3, 4, 5];
16
17
public
function
convertToArray
(?
Rule
$rule =
null
): array
18
{
19
if
(is_null($rule))
20
{
21
$rule =
new
Rule
();
22
}
23
24
$slotSize = $rule->getSlotSize() ?? self::DEFAULT_SLOT_SIZE;
25
26
$ranges = $this->
getSortedRanges
($rule->getRanges() ?? []);
27
$rangeArrays = [];
28
foreach
($ranges as $range)
29
{
30
$weekdays = $range->getWeekdays() ?? $this->
getWorkdays
();
31
$rangeArrays[] = [
32
'from'
=> $range->getFrom() ?? $this->
getDefaultFrom
(),
33
'to'
=> $range->getTo() ?? $this->
getDefaultTo
(),
34
'weekdays'
=> $weekdays,
35
'weekdaysTitle'
=> $this->
getWeekdaysTitle
($weekdays),
36
];
37
}
38
if
(empty($rangeArrays))
39
{
40
$weekdays = $this->
getWorkdays
();
41
$rangeArrays = [
42
[
43
'from'
=> $this->
getDefaultFrom
(),
44
'to'
=> $this->
getDefaultTo
(),
45
'weekdays'
=> $weekdays,
46
'weekdaysTitle'
=> $this->
getWeekdaysTitle
($weekdays),
47
],
48
];
49
}
50
51
return
[
52
'slotSize'
=> $slotSize,
53
'ranges'
=> $rangeArrays,
54
];
55
}
56
57
public
function
buildRuleFromArray
(array $ruleArray):
Rule
58
{
59
$slotSize = $ruleArray[
'slotSize'
] ?? self::DEFAULT_SLOT_SIZE;
60
61
$rangeArrays = $ruleArray[
'ranges'
] ?? [];
62
$ranges = [];
63
foreach
($rangeArrays as $rangeArray)
64
{
65
$from = $rangeArray[
'from'
] ?? $this->
getDefaultFrom
();
66
$to = $rangeArray[
'to'
] ?? $this->
getDefaultTo
();
67
$weekdays = $rangeArray[
'weekdays'
] ?? $this->
getWorkdays
();
68
$ranges[] = (
new
Range
())
69
->setFrom($from)
70
->setTo($to)
71
->setWeekdays($weekdays);
72
}
73
if
(empty($ranges))
74
{
75
$ranges = [
76
(
new
Range
())
77
->setFrom($this->
getDefaultFrom
())
78
->setTo($this->
getDefaultTo
())
79
->setWeekdays($this->
getWorkdays
())
80
];
81
}
82
83
return
(
new
Rule
())
84
->setSlotSize($slotSize)
85
->setRanges($this->
getSortedRanges
($ranges));
86
}
87
88
public
function
getFromLinkObjectRule
(
LinkObjectRule
$objectRule):
Rule
89
{
90
$ruleEOCollection =
null
;
91
92
if
(is_int($objectRule->
getLinkId
()))
93
{
94
$ruleEOCollection = $this->
getRuleEOByLinkId
($objectRule->
getLinkId
());
95
}
96
97
if
($ruleEOCollection ===
null
|| empty($ruleEOCollection->getAll()))
98
{
99
$ruleEOCollection = $this->
getRuleEOByObject
($objectRule->
getObjectId
(), $objectRule->
getObjectType
());
100
}
101
102
return
$this->
convertToObject
($ruleEOCollection);
103
}
104
105
protected
function
getRuleEOByLinkId
(
int
$linkId): ?Main\ORM\Objectify\
Collection
106
{
107
return
SharingLinkRuleTable::query()
108
->setSelect([
'*'
])
109
->where(
'LINK_ID'
, $linkId)
110
->exec()->fetchCollection();
111
}
112
113
protected
function
getRuleEOByObject
(
int
$objectId,
string
$objectType): ?Main\ORM\Objectify\
Collection
114
{
115
return
SharingObjectRuleTable::query()
116
->setSelect([
'*'
])
117
->where(
'OBJECT_ID'
, $objectId)
118
->where(
'OBJECT_TYPE'
, $objectType)
119
->exec()->fetchCollection();
120
}
121
122
protected
function
convertToObject
($ruleEOCollection):
Rule
123
{
124
if
(!empty($ruleEOCollection->getAll()))
125
{
126
$slotSize = $ruleEOCollection->getAll()[0]->
getSlotSize
();
127
$ranges = array_map(
static
fn($ruleEO) => (
new
Range
())
128
->setWeekdays(array_map(
'intval'
, explode(
','
, $ruleEO->getWeekdays())))
129
->setFrom($ruleEO->getTimeFrom())
130
->setTo($ruleEO->getTimeTo())
131
, $ruleEOCollection->getAll());
132
133
return
(
new
Rule
())
134
->setSlotSize($slotSize)
135
->setRanges($ranges)
136
;
137
}
138
139
$slotSize = self::DEFAULT_SLOT_SIZE;
140
$ranges = [
141
(
new
Range
())
142
->setFrom($this->
getDefaultFrom
())
143
->setTo($this->
getDefaultTo
())
144
->setWeekdays($this->
getWorkdays
())
145
];
146
147
return
(
new
Rule
())
148
->setSlotSize($slotSize)
149
->setRanges($this->
getSortedRanges
($ranges));
150
}
151
152
public
function
saveForLinkObject
(
Rule
$rule,
LinkObjectRule
$linkObjectRule): void
153
{
154
$rows = array_map(
static
fn($range) => [
155
'LINK_ID'
=> $linkObjectRule->
getLinkId
(),
156
'SLOT_SIZE'
=> $rule->
getSlotSize
(),
157
'WEEKDAYS'
=> implode(
','
, $range->getWeekdays()),
158
'TIME_FROM'
=> $range->getFrom(),
159
'TIME_TO'
=> $range->getTo(),
160
], $rule->
getRanges
());
161
SharingLinkRuleTable::deleteByFilter([
162
'LINK_ID'
=> $linkObjectRule->
getLinkId
(),
163
]);
164
SharingLinkRuleTable::addMulti($rows,
true
);
165
166
$rows = array_map(
static
fn($range) => [
167
'OBJECT_ID'
=> $linkObjectRule->
getObjectId
(),
168
'OBJECT_TYPE'
=> $linkObjectRule->
getObjectType
(),
169
'SLOT_SIZE'
=> $rule->
getSlotSize
(),
170
'WEEKDAYS'
=> implode(
','
, $range->getWeekdays()),
171
'TIME_FROM'
=> $range->getFrom(),
172
'TIME_TO'
=> $range->getTo(),
173
], $rule->
getRanges
());
174
SharingObjectRuleTable::deleteByFilter([
175
'OBJECT_ID'
=> $linkObjectRule->
getObjectId
(),
176
'OBJECT_TYPE'
=> $linkObjectRule->
getObjectType
(),
177
]);
178
SharingObjectRuleTable::addMulti($rows,
true
);
179
}
180
181
public
function
deleteLinkRule
(
int
$linkId): void
182
{
183
SharingLinkRuleTable::deleteByFilter([
184
'LINK_ID'
=> $linkId,
185
]);
186
}
187
188
public
function
getChanges
(?
Rule
$rule): array
189
{
190
$defaultRule = $this->
convertToArray
();
191
$ruleArray = $this->
convertToArray
($rule);
192
193
$changes = [
194
'customDays'
=>
'N'
,
195
'customLength'
=>
'N'
,
196
];
197
198
if
($defaultRule[
'slotSize'
] !== $ruleArray[
'slotSize'
])
199
{
200
$changes[
'customLength'
] =
'Y'
;
201
}
202
203
if
($defaultRule[
'ranges'
] !== $ruleArray[
'ranges'
])
204
{
205
$changes[
'customDays'
] =
'Y'
;
206
}
207
208
return
$changes;
209
}
210
211
protected
function
getDefaultFrom
(): int
212
{
213
$settings = \CCalendar::GetSettings();
214
if
(!isset($settings[
'work_time_start'
]))
215
{
216
return
self::DEFAULT_FROM;
217
}
218
219
return
$this->
getMinutesFromTimeString
($settings[
'work_time_start'
]);
220
}
221
222
protected
function
getDefaultTo
(): int
223
{
224
$settings = \CCalendar::GetSettings();
225
if
(!isset($settings[
'work_time_end'
]))
226
{
227
return
self::DEFAULT_TO;
228
}
229
230
return
$this->
getMinutesFromTimeString
($settings[
'work_time_end'
]);
231
}
232
233
protected
function
getMinutesFromTimeString
(
string
$timeString): int
234
{
235
$time = strtotime(str_replace(
'.'
,
':'
,
"$timeString:00"
));
236
return
(
int
)date(
'H'
, $time) * 60 + (int)date(
'i'
, $time);
237
}
238
243
protected
function
getSortedRanges
(array $ranges): array
244
{
245
usort($ranges, fn($a, $b) => $this->
compareRanges
($a, $b));
246
247
return
$ranges;
248
}
249
255
protected
function
compareRanges
(
Range
$range1,
Range
$range2): int
256
{
257
$weekdaysWeight1 = $this->
getWeekdaysWeight
($range1->
getWeekdays
());
258
$weekdaysWeight2 = $this->
getWeekdaysWeight
($range2->
getWeekdays
());
259
260
if
($weekdaysWeight1 !== $weekdaysWeight2)
261
{
262
return
$weekdaysWeight1 - $weekdaysWeight2;
263
}
264
265
if
($range1->
getFrom
() !== $range2->
getFrom
())
266
{
267
return
$range1->
getFrom
() - $range2->
getFrom
();
268
}
269
270
return
$range1->
getTo
() - $range2->
getTo
();
271
}
272
277
protected
function
getWeekdaysWeight
(array $weekdays): int
278
{
279
$weekStart = $this->
getWeekStart
();
280
$mappedWeekdays = array_map(
static
fn($w) => $w < $weekStart ? $w + 10 : $w, $weekdays);
281
sort($mappedWeekdays);
282
283
$weight = 0;
284
foreach
($mappedWeekdays as $index => $w)
285
{
286
$weight += $w * 10 ** (10 - $index);
287
}
288
289
return
$weight;
290
}
291
296
protected
function
getWeekdaysTitle
(array $weekdays): string
297
{
298
$workdays = $this->
getWorkdays
();
299
sort($weekdays);
300
sort($workdays);
301
if
(implode(
','
, $weekdays) === implode(
','
, $workdays))
302
{
303
return
Loc::getMessage
(
'CALENDAR_SHARING_WORKDAYS'
);
304
}
305
306
return
$this->
formatWeekdays
($weekdays);
307
}
308
313
protected
function
formatWeekdays
(array $weekdays): string
314
{
315
$weekdaysLoc = $this->
getWeekdaysLoc
();
316
$weekdays = array_map(
static
fn($weekday) => $weekdaysLoc[$weekday], $this->
getSortedWeekdays
($weekdays));
317
318
return
implode(
', '
, $weekdays);
319
}
320
325
protected
function
getSortedWeekdays
(array $weekdays): array
326
{
327
$weekStart = $this->
getWeekStart
();
328
$mappedWeekdays = array_map(
static
fn($w) => $w < $weekStart ? $w + 10 : $w, $weekdays);
329
usort($mappedWeekdays,
static
fn($a, $b) => $a - $b);
330
331
return
array_map(
static
fn($w) => $w % 10, $mappedWeekdays);
332
}
333
337
protected
function
getWorkdays
(): array
338
{
339
$settings = \CCalendar::GetSettings();
340
if
(empty($settings[
'week_holidays'
]))
341
{
342
return
self::DEFAULT_WORKDAYS;
343
}
344
345
$holidays = array_map(
static
function
(
string
$weekday) {
346
return \CCalendar::IndByWeekDay($weekday);
347
}, $settings[
'week_holidays'
]);
348
349
return
array_values(array_diff([0, 1, 2, 3, 4, 5, 6], $holidays));
350
}
351
355
protected
function
getWeekStart
(): int
356
{
357
$weekStart = \CCalendar::GetWeekStart();
358
359
return \CCalendar::IndByWeekDay($weekStart);
360
}
361
365
protected
function
getWeekdaysLoc
(): array
366
{
367
$dayLength = 60 * 60 * 24;
368
$now = time();
369
$dayOfWeek = FormatDate(
'w'
, $now);
370
$sunday = $now - $dayOfWeek * $dayLength;
371
$weekdays = [0, 1, 2, 3, 4, 5, 6];
372
373
return
array_map(
static
fn($weekday) => FormatDate(
'D'
, $sunday + $weekday * $dayLength), $weekdays);
374
}
375
}
Bitrix\Calendar\Core\Mappers\Mapper
Definition
mapper.php:12
Bitrix\Calendar\Core\Mappers\Mapper\convertToObject
convertToObject($objectEO)
Bitrix\Calendar\Internals\SharingLinkRuleTable
Definition
sharinglinkruletable.php:28
Bitrix\Calendar\Internals\SharingObjectRuleTable
Definition
sharingobjectruletable.php:29
Bitrix\Calendar\Sharing\Link\Rule\LinkObjectRule
Definition
linkobjectrule.php:6
Bitrix\Calendar\Sharing\Link\Rule\LinkObjectRule\getObjectId
getObjectId()
Definition
linkobjectrule.php:11
Bitrix\Calendar\Sharing\Link\Rule\LinkObjectRule\getLinkId
getLinkId()
Definition
linkobjectrule.php:16
Bitrix\Calendar\Sharing\Link\Rule\LinkObjectRule\getObjectType
getObjectType()
Bitrix\Calendar\Sharing\Link\Rule\Mapper\buildRuleFromArray
buildRuleFromArray(array $ruleArray)
Definition
mapper.php:57
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getWeekdaysTitle
getWeekdaysTitle(array $weekdays)
Definition
mapper.php:296
Bitrix\Calendar\Sharing\Link\Rule\Mapper\DEFAULT_SLOT_SIZE
const DEFAULT_SLOT_SIZE
Definition
mapper.php:12
Bitrix\Calendar\Sharing\Link\Rule\Mapper\DEFAULT_FROM
const DEFAULT_FROM
Definition
mapper.php:13
Bitrix\Calendar\Sharing\Link\Rule\Mapper\convertToObject
convertToObject($ruleEOCollection)
Definition
mapper.php:122
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getWorkdays
getWorkdays()
Definition
mapper.php:337
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getRuleEOByLinkId
getRuleEOByLinkId(int $linkId)
Definition
mapper.php:105
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getChanges
getChanges(?Rule $rule)
Definition
mapper.php:188
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getFromLinkObjectRule
getFromLinkObjectRule(LinkObjectRule $objectRule)
Definition
mapper.php:88
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getWeekStart
getWeekStart()
Definition
mapper.php:355
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getSortedRanges
getSortedRanges(array $ranges)
Definition
mapper.php:243
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getDefaultTo
getDefaultTo()
Definition
mapper.php:222
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getDefaultFrom
getDefaultFrom()
Definition
mapper.php:211
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getWeekdaysLoc
getWeekdaysLoc()
Definition
mapper.php:365
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getSortedWeekdays
getSortedWeekdays(array $weekdays)
Definition
mapper.php:325
Bitrix\Calendar\Sharing\Link\Rule\Mapper\convertToArray
convertToArray(?Rule $rule=null)
Definition
mapper.php:17
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getRuleEOByObject
getRuleEOByObject(int $objectId, string $objectType)
Definition
mapper.php:113
Bitrix\Calendar\Sharing\Link\Rule\Mapper\formatWeekdays
formatWeekdays(array $weekdays)
Definition
mapper.php:313
Bitrix\Calendar\Sharing\Link\Rule\Mapper\DEFAULT_WORKDAYS
const DEFAULT_WORKDAYS
Definition
mapper.php:15
Bitrix\Calendar\Sharing\Link\Rule\Mapper\deleteLinkRule
deleteLinkRule(int $linkId)
Definition
mapper.php:181
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getWeekdaysWeight
getWeekdaysWeight(array $weekdays)
Definition
mapper.php:277
Bitrix\Calendar\Sharing\Link\Rule\Mapper\compareRanges
compareRanges(Range $range1, Range $range2)
Definition
mapper.php:255
Bitrix\Calendar\Sharing\Link\Rule\Mapper\getMinutesFromTimeString
getMinutesFromTimeString(string $timeString)
Definition
mapper.php:233
Bitrix\Calendar\Sharing\Link\Rule\Mapper\saveForLinkObject
saveForLinkObject(Rule $rule, LinkObjectRule $linkObjectRule)
Definition
mapper.php:152
Bitrix\Calendar\Sharing\Link\Rule\Mapper\DEFAULT_TO
const DEFAULT_TO
Definition
mapper.php:14
Bitrix\Calendar\Sharing\Link\Rule\Range
Definition
range.php:6
Bitrix\Calendar\Sharing\Link\Rule\Range\getFrom
getFrom()
Definition
range.php:16
Bitrix\Calendar\Sharing\Link\Rule\Range\getWeekdays
getWeekdays()
Definition
range.php:40
Bitrix\Calendar\Sharing\Link\Rule\Range\getTo
getTo()
Definition
range.php:28
Bitrix\Calendar\Sharing\Link\Rule\Rule
Definition
rule.php:6
Bitrix\Calendar\Sharing\Link\Rule\Rule\getRanges
getRanges()
Definition
rule.php:25
Bitrix\Calendar\Sharing\Link\Rule\Rule\getSlotSize
getSlotSize()
Definition
rule.php:13
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Objectify\Collection
Definition
collection.php:33
Bitrix\Calendar\Sharing\Link\Rule
Definition
factory.php:2
Bitrix\Main
modules
calendar
lib
sharing
link
rule
mapper.php
Создано системой
1.10.0