Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
InvitationManager.php
1<?php
2
4
11
13{
14 public function __construct(private int $userId)
15 {}
16
18 {
19 $queryResult = $this->getBaseQuery()->fetchAll();
20
21 $invitations = new InvitationCollection();
22 foreach ($queryResult as $invitationFields)
23 {
24 $invitations->add($this->buildInvitation($invitationFields));
25 }
26
27 return $invitations;
28 }
29
30 private function getBaseQuery(): Query
31 {
32 return UserToGroupTable::query()
33 ->setSelect([
34 'GROUP_ID',
35 'DATE_UPDATE',
36 'INITIATED_BY_USER_ID',
37 'SENDER_NAME' => 'SENDER.NAME',
38 'SENDER_LAST_NAME' => 'SENDER.LAST_NAME',
39 'SENDER_SECOND_NAME' => 'SENDER.SECOND_NAME',
40 ])
41 ->where('USER_ID', $this->userId)
42 ->where('ROLE', UserToGroupTable::ROLE_REQUEST)
43 ->where('INITIATED_BY_TYPE', UserToGroupTable::INITIATED_BY_GROUP)
44 ->registerRuntimeField(
45 'SENDER',
46 new Reference(
47 'SENDER',
48 UserTable::class,
49 Join::on('this.INITIATED_BY_USER_ID', 'ref.ID'),
50 ['join_type' => Join::TYPE_INNER]
51 )
52 );
53 }
54
55 private function buildInvitation(array $invitationFields): Invitation
56 {
57 $nameFormat = \Bitrix\Main\Application::getInstance()->getContext()->getCulture()->getNameFormat();
58 $invitation = (new Invitation());
59
60 $senderData = [
61 'NAME' => $invitationFields['SENDER_NAME'],
62 'LAST_NAME' => $invitationFields['SENDER_LAST_NAME'],
63 'SECOND_NAME' => $invitationFields['SENDER_SECOND_NAME'],
64 ];
65 $formattedName = \CUser::FormatName($nameFormat, $senderData);
66 $sender = new Sender((int)$invitationFields['INITIATED_BY_USER_ID'], $formattedName);
67
68 $invitation
69 ->setSender($sender)
70 ->setReceiverId($this->userId)
71 ->setSpaceId((int)$invitationFields['GROUP_ID'])
72 ;
73 if ($invitationFields['DATE_UPDATE'] instanceof DateTime)
74 {
75 $invitation->setInviteDate($invitationFields['DATE_UPDATE']);
76 }
77
78 return $invitation;
79 }
80
81 public function getInvitationBySpaceId(int $spaceId): ?Invitation
82 {
83 $queryResult =
84 $this->getBaseQuery()
85 ->where('GROUP_ID', $spaceId)
86 ->fetch()
87 ;
88
89 return !empty($queryResult) ? $this->buildInvitation($queryResult) : null;
90 }
91}
setSelect(array $select)
Definition query.php:338