Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
room.php
1<?php
2
4
11
12Loc::loadMessages(__FILE__);
13
14class Room
15{
16 public const TYPE = 'location';
17
19 private ?int $id = null;
21 private ?int$locationId = null;
23 private ?int $capacity = null;
25 private string $necessity = 'N';
27 private string $name = '';
29 private string $type = self::TYPE;
31 private string $color = '#9dcf00';
33 private ?int $ownerId = null;
35 private ?int $createdBy = null;
37 private ?int $categoryId = null;
39 private ?array $access = [];
41 private ?Error $error = null;
42
48 public static function createInstanceFromParams($params): Room
49 {
50 $params = [
51 'ID' => $params['ID'] ?? null,
52 'LOCATION_ID' => $params['LOCATION_ID'] ?? null,
53 'CAPACITY' => $params['CAPACITY'] ?? null,
54 'NECESSITY' => $params['NECESSITY'] ?? null,
55 'NAME' => $params['NAME'] ?? null,
56 'COLOR' => $params['COLOR'] ?? null,
57 'OWNER_ID' => $params['OWNER_ID'] ?? null,
58 'ACCESS' => $params['ACCESS'] ?? null,
59 'CATEGORY_ID' => $params['CATEGORY_ID'] ?? null,
60 ];
61
62 $room = new self();
63 $room->setId($params['ID'])
64 ->setLocationId($params['LOCATION_ID'])
65 ->setCapacity($params['CAPACITY'])
66 ->setNecessity($params['NECESSITY'])
67 ->setName($params['NAME'])
68 ->setColor($params['COLOR'])
69 ->setOwnerId($params['OWNER_ID'])
70 ->setCreatedBy()
71 ->setAccess($params['ACCESS'])
72 ->setCategoryId($params['CATEGORY_ID'])
73 ;
74
75 return $room;
76 }
77
81 public function createInstance(): Room
82 {
83 return new self();
84 }
85
91 public function setId(?int $id = null): Room
92 {
93 $this->id = $id;
94
95 return $this;
96 }
97
103 public function setLocationId(?int $locationId = null): Room
104 {
105 $this->locationId = $locationId;
106
107 return $this;
108
109 }
110
116 public function setCapacity(?int $capacity = null): Room
117 {
118 $this->capacity = $capacity;
119
120 return $this;
121 }
122
128 public function setNecessity(?string $necessity = 'N'): Room
129 {
130 $this->necessity = ($necessity === 'Y') ? 'Y' : 'N';
131
132 return $this;
133 }
134
140 public function setName(?string $name = ''): Room
141 {
142 $this->name = Manager::checkRoomName($name);
143
144 return $this;
145 }
146
152 public function setColor(?string $color = ''): Room
153 {
154 $this->color = \CCalendar::Color($color);
155
156 return $this;
157 }
158
164 public function setOwnerId(?int $ownerId = null): Room
165 {
166 $this->ownerId = $ownerId;
167
168 return $this;
169 }
170
174 public function setCreatedBy(): Room
175 {
176 $this->createdBy = \CCalendar::GetCurUserId();
177
178 return $this;
179 }
180
186 public function setAccess(?array $access = []): Room
187 {
188 $this->access = $access;
189
190 return $this;
191 }
192
198 public function setCategoryId(?int $categoryId = null): Room
199 {
200 $this->categoryId = $categoryId ?: null;
201
202 return $this;
203 }
204
210 private function addError($error)
211 {
212 $this->error = $error;
213 }
214
218 public function getId(): ?int
219 {
220 return $this->id;
221 }
222
226 public function getLocationId(): ?int
227 {
228 return $this->locationId;
229 }
230
234 public function getCapacity(): ?int
235 {
236 return $this->capacity;
237 }
238
242 public function getNecessity(): ?string
243 {
244 return $this->necessity;
245 }
246
250 public function getName(): ?string
251 {
252 return $this->name;
253 }
254
258 public function getType(): ?string
259 {
260 return $this->type;
261 }
262
266 public function getColor(): ?string
267 {
268 return $this->color;
269 }
270
274 public function getOwnerId(): ?int
275 {
276 return $this->ownerId;
277 }
278
282 public function getCreatedBy(): ?int
283 {
284 return $this->createdBy;
285 }
286
290 public function getAccess(): ?array
291 {
292 return $this->access;
293 }
294
298 public function getCategoryId(): ?int
299 {
300 return $this->categoryId;
301 }
302
306 public function getError(): ?Error
307 {
308 return $this->error;
309 }
310
315 public function create(): Room
316 {
317 $section = SectionTable::add([
318 'CAL_TYPE' => $this->type,
319 'NAME' => Emoji::encode($this->name),
320 'COLOR' => $this->color,
321 'OWNER_ID' => $this->ownerId,
322 'SORT' => 100,
323 'CREATED_BY' => $this->createdBy,
324 'DATE_CREATE' => new DateTime(),
325 'TIMESTAMP_X' => new DateTime(),
326 'ACTIVE' => 'Y',
327 ]);
328 if (!$section->isSuccess())
329 {
330 $this->addError(new Error(Loc::getMessage('EC_ROOM_SAVE_ERROR')));
331
332 return $this;
333 }
334 $this->setId($section->getId());
335
336 $location = LocationTable::add(
337 [
338 'SECTION_ID' => $this->id,
339 'NECESSITY' => $this->necessity,
340 'CAPACITY' => $this->capacity,
341 'CATEGORY_ID' => $this->categoryId,
342 ]
343 );
344 if (!$location->isSuccess())
345 {
346 SectionTable::delete($this->id);
347 $this->addError(new Error(Loc::getMessage('EC_ROOM_SAVE_ERROR')));
348
349 return $this;
350 }
351
352 return $this;
353 }
354
359 public function update(): Room
360 {
361 $section = SectionTable::update(
362 $this->id,
363 [
364 'NAME' => Emoji::encode($this->name),
365 'COLOR' => $this->color,
366 'TIMESTAMP_X' => new DateTime(),
367 ]
368 );
369 if (!$section->isSuccess())
370 {
371 $this->addError(new Error(Loc::getMessage('EC_ROOM_SAVE_ERROR')));
372
373 return $this;
374 }
375 $this->setId($section->getId());
376
377 $location = LocationTable::update(
378 $this->locationId,
379 [
380 'NECESSITY' => $this->necessity,
381 'CAPACITY' => $this->capacity,
382 'CATEGORY_ID' => $this->categoryId,
383 ]
384 );
385 if (!$location->isSuccess())
386 {
387 $this->addError(new Error(Loc::getMessage('EC_ROOM_SAVE_ERROR')));
388
389 return $this;
390 }
391
392 return $this;
393 }
394
399 public function delete(): Room
400 {
401 $section = SectionTable::delete($this->id);
402 if (!$section->isSuccess())
403 {
404 $this->addError(new Error(Loc::getMessage('EC_ROOM_DELETE_ERROR')));
405
406 return $this;
407 }
408
409 $location = LocationTable::delete($this->locationId);
410 if (!$location->isSuccess())
411 {
412 $this->addError(new Error(Loc::getMessage('EC_ROOM_DELETE_ERROR')));
413
414 return $this;
415 }
416
417 return $this;
418 }
419}
static checkRoomName(?string $name)
Definition manager.php:632
static createInstanceFromParams($params)
Definition room.php:48
setColor(?string $color='')
Definition room.php:152
setLocationId(?int $locationId=null)
Definition room.php:103
setName(?string $name='')
Definition room.php:140
setId(?int $id=null)
Definition room.php:91
setAccess(?array $access=[])
Definition room.php:186
setNecessity(?string $necessity='N')
Definition room.php:128
setOwnerId(?int $ownerId=null)
Definition room.php:164
setCategoryId(?int $categoryId=null)
Definition room.php:198
setCapacity(?int $capacity=null)
Definition room.php:116
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29