Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CalendarItem.php
1<?php
2
4
7use Bitrix\Im\Model\EO_LinkCalendar;
13
15{
16 protected string $title;
18 protected DateTime $dateTo;
19
23 public function __construct($source = null)
24 {
25 $this->initByDefault();
26
27 if (!empty($source))
28 {
29 $this->load($source);
30 }
31 }
32
37 public function setEntity(RestEntity $entity): self
38 {
39 $this->setTitle($entity->getTitle())->setDateTo($entity->getDateTo())->setDateFrom($entity->getDateFrom());
40
41 return parent::setEntity($entity);
42 }
43
44 public function getEntity(): Entity\Calendar\CalendarItem
45 {
46 $this->entity ??= Entity\Calendar\CalendarItem::initById($this->getEntityId());
47
48 return $this->entity;
49 }
50
51 public static function getByCalendarId(int $id, bool $fillEntity = true): ?self
52 {
53 $ormObject = LinkCalendarTable::query()
54 ->setSelect(['*'])
55 ->where('CALENDAR_ID', $id)
56 ->setLimit(1)
57 ->fetchObject()
58 ;
59
60 if ($ormObject === null)
61 {
62 return null;
63 }
64
65 if ($fillEntity)
66 {
67 return (new static($ormObject))->setEntity(Entity\Calendar\CalendarItem::initById($id));
68 }
69
70 return new static($ormObject);
71 }
72
73 public static function getByMessageId(int $messageId): ?self
74 {
75 $ormObject = LinkCalendarTable::query()
76 ->setSelect(['*'])
77 ->where('MESSAGE_ID', $messageId)
78 ->setLimit(1)
79 ->fetchObject()
80 ;
81
82 if ($ormObject === null)
83 {
84 return null;
85 }
86
87 return new static($ormObject);
88 }
89
90 public function save(): Result
91 {
92 $result = parent::save();
93 if ($result->isSuccess())
94 {
95 LinkCalendarIndexTable::indexInBackground([$this->getId()]);
96 }
97
98 return $result;
99 }
100
101 public function delete(): Result
102 {
103 LinkCalendarIndexTable::delete($this->getPrimaryId());
104
105 return parent::delete();
106 }
107
108 public static function getDataClass(): string
109 {
110 return LinkCalendarTable::class;
111 }
112
113 protected static function getEntityIdFieldName(): string
114 {
115 return 'CALENDAR_ID';
116 }
117
118 public static function getEntityClassName(): string
119 {
120 return Entity\Calendar\CalendarItem::class;
121 }
122
123 public static function getRestEntityName(): string
124 {
125 return 'link';
126 }
127
128 protected static function mirrorDataEntityFields(): array
129 {
130 $additionalFields = [
131 'CALENDAR_TITLE' => [
132 'field' => 'title',
133 'set' => 'setTitle',
134 'get' => 'getTitle',
135 ],
136 'CALENDAR_DATE_FROM' => [
137 'field' => 'dateFrom',
138 'set' => 'setDateFrom',
139 'get' => 'getDateFrom',
140 ],
141 'CALENDAR_DATE_TO' => [
142 'field' => 'dateTo',
143 'set' => 'setDateTo',
144 'get' => 'getDateTo',
145 ]
146 ];
147
148 return array_merge(parent::mirrorDataEntityFields(), $additionalFields);
149 }
150
151 //region Setters & getters
152
153 public function getTitle(): string
154 {
155 return $this->title;
156 }
157
158 public function setTitle(string $title): CalendarItem
159 {
160 $this->title = $title;
161 return $this;
162 }
163
164 public function getDateFrom(): DateTime
165 {
166 return $this->dateFrom;
167 }
168
170 {
171 $this->dateFrom = $dateFrom;
172 return $this;
173 }
174
175 public function getDateTo(): DateTime
176 {
177 return $this->dateTo;
178 }
179
181 {
182 $this->dateTo = $dateTo;
183 return $this;
184 }
185
186 //endregion
187}