Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CalendarItem.php
1<?php
2
4
5use Bitrix\Im\V2\Common\ContextCustomer;
10use CCalendar;
11use CCalendarEvent;
12
13class CalendarItem implements RestEntity
14{
15 use ContextCustomer;
16
17 protected ?int $id;
18 protected int $ownerId;
19 protected int $createdBy;
20 protected string $title;
21 protected string $type;
23 protected DateTime $dateTo;
24 protected array $membersIds;
25
26 public static function getRestEntityName(): string
27 {
28 return 'calendar';
29 }
30
31 public static function initByGetListArray(array $calendarInfo): self
32 {
33 $calendar = new static();
34
35 if ($calendarInfo['DT_SKIP_TIME'] === 'Y')
36 {
37 $calendarSettings = CCalendar::GetSettings(['getDefaultForEmpty' => false]);
38 $workTimeStart = explode('.', $calendarSettings['work_time_start']);
39 $workTimeEnd = explode('.', $calendarSettings['work_time_end']);
40 $dateFrom = new DateTime($calendarInfo['DATE_FROM']);
41 $dateTo = new DateTime($calendarInfo['DATE_TO']);
42 $dateFrom->setTime((int)$workTimeStart[0], (int)($workTimeStart[1] ?? 0));
43 $dateTo->setTime((int)$workTimeEnd[0], (int)($workTimeEnd[1] ?? 0));
44 }
45 else
46 {
47 $dateFrom = DateTime::createFromTimestamp($calendarInfo['DATE_FROM_TS_UTC'] + (int)date('Z'));
48 $dateTo = DateTime::createFromTimestamp($calendarInfo['DATE_TO_TS_UTC'] + (int)date('Z'));
49 }
50 $calendar
51 ->setId($calendarInfo['ID'])
52 ->setTitle($calendarInfo['NAME'])
53 ->setType($calendarInfo['CAL_TYPE'])
54 ->setOwnerId((int)$calendarInfo['OWNER_ID'])
55 ->setCreatedBy((int)$calendarInfo['CREATED_BY'])
56 ->setDateFrom($dateFrom)
57 ->setDateTo($dateTo)
58 ->setMembersIds(array_map('intval', array_column($calendarInfo['ATTENDEE_LIST'], 'id')))
59 ;
60
61 return $calendar;
62 }
63
64 public static function initById(int $id, ?Context $context = null): self
65 {
66 $context = $context ?? Locator::getContext();
67 $checkPermissions = false;
68
69 $calendarGetList = CCalendarEvent::GetList([
70 'arFilter' => [
71 'ID' => $id,
72 'DELETED' => false,
73 ],
74 'parseRecursion' => false,
75 'fetchAttendees' => true,
76 'userId' => $context->getUserId(),
77 'fetchMeetings' => false,
78 'setDefaultLimit' => false,
79 'checkPermissions' => $checkPermissions,
80 ]);
81
82 return static::initByGetListArray($calendarGetList[0]);
83 }
84
85 public function toRestFormat(array $option = []): array
86 {
87 return [
88 'id' => $this->getId(),
89 'title' => $this->getTitle(),
90 'dateFrom' => $this->getDateFrom()->format('c'),
91 'dateTo' => $this->getDateTo()->format('c'),
92 'source' => $this->getUrl(),
93 ];
94 }
95
96 public function getUrl(): string
97 {
98 return CCalendar::GetPath($this->getType(), $this->getOwnerId()) . '?EVENT_ID=' . $this->getId();
99 }
100
101 //region Getters & setters
102
103 public function getId(): int
104 {
105 return $this->id;
106 }
107
108 public function setId(?int $id): CalendarItem
109 {
110 $this->id = $id;
111 return $this;
112 }
113
114 public function getTitle(): string
115 {
116 return $this->title;
117 }
118
119 public function setTitle(string $title): CalendarItem
120 {
121 $this->title = $title;
122 return $this;
123 }
124
125 public function getDateFrom(): DateTime
126 {
127 return $this->dateFrom;
128 }
129
131 {
132 $this->dateFrom = $dateFrom;
133 return $this;
134 }
135
136 public function getDateTo(): DateTime
137 {
138 return $this->dateTo;
139 }
140
142 {
143 $this->dateTo = $dateTo;
144 return $this;
145 }
146
147 public function getOwnerId(): int
148 {
149 return $this->ownerId;
150 }
151
152 public function setOwnerId(int $ownerId): CalendarItem
153 {
154 $this->ownerId = $ownerId;
155 return $this;
156 }
157
158 public function getCreatedBy(): int
159 {
160 return $this->createdBy;
161 }
162
164 {
165 $this->createdBy = $createdBy;
166 return $this;
167 }
168
169 public function getType(): string
170 {
171 return $this->type;
172 }
173
174 public function setType(string $type): CalendarItem
175 {
176 $this->type = $type;
177 return $this;
178 }
179
183 public function getMembersIds(): array
184 {
185 return $this->membersIds;
186 }
187
191 public function setMembersIds(array $membersIds): CalendarItem
192 {
193 $this->membersIds = array_unique($membersIds);
194 return $this;
195 }
196
197 //endregion
198}
static initById(int $id, ?Context $context=null)
static initByGetListArray(array $calendarInfo)
static createFromTimestamp($timestamp)
Definition datetime.php:246
setTime($hour, $minute, $second=0, $microseconds=0)
Definition datetime.php:144