Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
invitationinfo.php
1<?php
2
4
8use Bitrix\Main\EO_User;
12
13class InvitationInfo implements Arrayable
14{
15 public const TYPE_REQUEST = 'request';
16 public const TYPE_CANCEL = 'cancel';
17 public const TYPE_EDIT = 'edit';
18
19 private int $eventId;
20 private int $addresserId;
21 private int $receiverId;
22 private string $type;
23 private array $changeFields;
24 private int $counterInvitations;
25 private ?array $event = null;
26
27 private ?Context $context = null;
28 private Logger $logger;
29
30 public function __construct(
31 int $eventId,
32 int $addresserId,
33 int $receiverId,
34 string $type,
35 ?array $changeFields = [],
36 ?int $counterInvitations = 0
37 )
38 {
39 $this->eventId = $eventId;
40 $this->addresserId = $addresserId;
41 $this->receiverId = $receiverId;
42 $this->type = $type;
43 $this->changeFields = $changeFields ?? [];
44 $this->counterInvitations = $counterInvitations ?? 0;
45
46 $this->init();
47 }
48
50 {
51 return $this
52 ->getFactory()
53 ->getInvitation();
54 }
55
56 public function toArray(): array
57 {
58 return [
59 'eventId' => $this->eventId,
60 'addresserId' => $this->addresserId,
61 'receiverId' => $this->receiverId,
62 'type' => $this->type,
63 'changeFields' => $this->changeFields,
64 'counterInvitation' => $this->counterInvitations,
65 ];
66 }
67
68 private function getFactory(): SenderInvitationFactory
69 {
70 $this
71 ->setEventById()
72 ->setContextByEvent();
73
74 return new SenderInvitationFactory($this->type, $this->event, $this->context, $this->counterInvitations);
75 }
76
77 private function setEventById(): static
78 {
79 if (is_null($this->event))
80 {
81 $this->event = $this->getEvent();
82 }
83
84 return $this;
85 }
86
87 private function getEvent(): ?array
88 {
89 try
90 {
91 return Helper::getEventById($this->eventId);
92 }
93 catch (SystemException $exception)
94 {
95 $this->logger->log($exception);
96 return null;
97 }
98 }
99
100 private function setContextByEvent(): static
101 {
102 $this->setEventById();
103
104 try
105 {
106 $this->context = Context::createInstance(
107 $this->getAddresser($this->event['MEETING'] ?? null),
108 $this->getReceiver(),
109 $this->changeFields
110 );
111 }
112 catch (MemberNotFoundException $exception)
113 {
114 $this->logger->log($exception);
115 }
116
117 return $this;
118 }
119
123 private function getAddresser(?string $meetingInfo): ?MailAddresser
124 {
125 $addresser = $this->getInfoAboutUser($this->addresserId);
126 if (is_null($addresser))
127 {
128 throw new MemberNotFoundException("Addresser {$this->addresserId} not found or not active");
129 }
130
132 $addresser->getId(),
133 $this->getSelectedAddresserEmail($meetingInfo) ?? $addresser->getEmail(),
134 $addresser->getName(),
135 $addresser->getLastName()
136 );
137 }
138
142 private function getReceiver(): MailReceiver
143 {
144 $receiver = $this->getInfoAboutUser($this->receiverId);
145 if (is_null($receiver))
146 {
147 throw new MemberNotFoundException("Receiver {$this->receiverId} not found or not active");
148 }
149
151 $receiver->getId(),
152 $receiver->getEmail(),
153 $receiver->getName(),
154 $receiver->getLastName()
155 );
156 }
157
158 private function getInfoAboutUser(int $userId): ?EO_User
159 {
160 try
161 {
162 $query = UserTable::query();
163 $query
164 ->setSelect(['ID', 'EMAIL', 'NAME', 'LAST_NAME', 'ACTIVE'])
165 ->where('ID', $userId)
166 ->where('ACTIVE', true);
167
168 return $query->exec()->fetchObject();
169 }
170 catch (SystemException $exception)
171 {
172 $this->logger->log($exception);
173 return null;
174 }
175 }
176
177 private function getSelectedAddresserEmail(?string $serializedMeetingInfo = null): ?string
178 {
179 $meetingInfo = unserialize($serializedMeetingInfo, ['allowed_classes' => false]);
180
181 return !empty($meetingInfo) && !empty($meetingInfo['MAIL_FROM'])
182 ? $meetingInfo['MAIL_FROM']
183 : null;
184 }
185
186 private function init(): void
187 {
188 $this->logger = new Logger();
189 }
190}
static createInstance(MailAddresser $addresser, MailReceiver $receiver, array $changeFields=null)
Definition context.php:18
__construct(int $eventId, int $addresserId, int $receiverId, string $type, ?array $changeFields=[], ?int $counterInvitations=0)
static createInstance(int $id, string $email, string $name=null, string $lastName=null)
Definition mailuser.php:15