Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Group.php
1<?php
3
5use Bitrix\UI\Avatar;
7
8class Group
9{
10 protected int $id;
11 protected array $data;
13
14 public function __construct(int $id)
15 {
16 if ($id > 0 && ($this->data = Avatar\Model\GroupTable::getById($id)->fetch()))
17 {
18 $this->id = $id;
19 if (is_subclass_of($this->data['OWNER_TYPE'], DefaultOwner::class))
20 {
21 $this->owner = new $this->data['OWNER_TYPE']($this->data['OWNER_ID']);
22 }
23 else
24 {
25 throw new Main\ArgumentTypeException("Mask group ({$this->data['OWNER_TYPE']}) is unreachable.");
26 }
27 }
28 else
29 {
30 throw new Main\ObjectNotFoundException("Mask group id ($id) is not found.");
31 }
32 }
33
34 public function getId(): int
35 {
36 return $this->id;
37 }
38
39 public function isEditableBy(Avatar\Mask\Consumer $consumer): bool
40 {
41 if ($consumer->isAdmin())
42 {
43 return true;
44 }
45 if ($this->getOwner() instanceof Owner\User && $this->getOwner()->getId() === $consumer->getId())
46 {
47 return true;
48 }
49 return false;
50 }
51
52 public function update(array $data): Main\Result
53 {
54 $dataToSave = array_intersect_key($data, ['TITLE' => null, 'DESCRIPTION' => null]);
55 if (!empty($dataToSave))
56 {
57 GroupTable::update($this->getId(), $dataToSave);
58 }
59
60 return new Main\Result();
61 }
62
63 public function delete(): Main\Result
64 {
65 return GroupTable::delete($this->getId());
66 }
67
68 public function getOwner(): DefaultOwner
69 {
70 return $this->owner;
71 }
72
73 public static function createOrGet(DefaultOwner $owner, string $title, ?string $description = null): ?Group
74 {
75 if ($group = GroupTable::getList([
76 'select' => ['ID', 'DESCRIPTION'],
77 'filter' => [
78 '=OWNER_TYPE' => get_class($owner),
79 '=OWNER_ID' => $owner->getId(),
80 '=TITLE' => $title
81 ]
82 ])->fetch())
83 {
84 $groupId = $group['ID'];
85 if ($group['DESCRIPTION'] != $description)
86 {
87 GroupTable::update($groupId, ['DESCRIPTION' => $description]);
88 }
89 }
90 else
91 {
92 $groupId = GroupTable::add(['fields' => [
93 'OWNER_TYPE' => get_class($owner),
94 'OWNER_ID' => $owner->getId(),
95 'TITLE' => $title,
96 'DESCRIPTION' => $description
97 ]])->getId();
98 };
99 if ($groupId > 0)
100 {
101 return static::getInstance($groupId);
102 }
103 return null;
104 }
105
106 public static function getInstance($id): ?Group
107 {
108 try
109 {
110 return new static($id);
111 }
112 catch (Main\ObjectNotFoundException $exception)
113 {
114 return null;
115 }
116 }
117}
isEditableBy(Avatar\Mask\Consumer $consumer)
Definition Group.php:39
static createOrGet(DefaultOwner $owner, string $title, ?string $description=null)
Definition Group.php:73