Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Application.php
1<?php
2
4
5use Bitrix\Im\V2\Common\ContextCustomer;
13
15{
16 use ContextCustomer;
17 private const DEFAULT_ORDER = 50;
18 private const SETTING_NAME = 'im_placement';
19
20 public function __construct(?int $userId = null)
21 {
22 $context = (new Context())->setUserId($userId);
23
24 $this->setContext($context);
25 }
26
30 public function getApplications(): array
31 {
32 $orderList = $this->getOrderList();
33 $result = [];
34 foreach ($this->generateApplicationList() as $application)
35 {
36 if ($this->isApplicationAvailable($application))
37 {
38 $application->order = $orderList[$application->id] ?? self::DEFAULT_ORDER;
39 $result[] = $application;
40 }
41 }
42
43 return $result;
44 }
45
46 public function getLoadUri(): string
47 {
48 $url = '/bitrix/components/bitrix/app.layout/lazyload.ajax.php?' . bitrix_sessid_get();
49
50 return
51 (new Uri($url))
52 ->addParams(['site' => SITE_ID])
53 ->getUri()
54 ;
55 }
56
57 public function setOrder(int $applicationsId, int $order): Result
58 {
59 $orderList = $this->getOrderList();
60 $orderList[$applicationsId] = $order;
61 $orderList = $this->filterDeletedApplications($orderList);
62
63 return $this->setOrderList($orderList);
64 }
65
66 private function getOrderList(): array
67 {
68 return \CUserOptions::GetOption('im', self::SETTING_NAME, []);
69 }
70
71 private function setOrderList(array $orderList): Result
72 {
73 $result = new Result();
74 $isSuccess = \CUserOptions::SetOption('im', self::SETTING_NAME, $orderList);
75
76 if (!$isSuccess)
77 {
78 $result->addError(new Error('Error writing the parameter order', 'SERVER_ERROR'));
79 }
80
81 return $result;
82 }
83
84 private function isApplicationAvailable(Application\Entity $application): bool
85 {
86 return
87 $this->checkRole($application->options['role'])
88 && $this->checkExtranet($application->options['extranet'])
89 ;
90 }
91
92 private function checkExtranet(string $extranetOption): bool
93 {
94 if ($this->getContext()->getUser()->isExtranet())
95 {
96 return $extranetOption === 'Y';
97 }
98
99 return true;
100 }
101
102 private function checkRole(string $roleOptions): bool
103 {
104 if (!$this->getContext()->getUser()->isAdmin())
105 {
106 return mb_strtoupper($roleOptions) === Role::USER;
107 }
108
109 return true;
110 }
111
112 private function filterDeletedApplications(array $userApplications): array
113 {
114 $applicationIdList = $this->getApplicationIdList();
115
116 $result = [];
117 foreach ($userApplications as $applicationId => $order)
118 {
119 if (in_array($applicationId, $applicationIdList, true))
120 {
121 $result[$applicationId] = $order;
122 }
123 }
124
125 return $result;
126 }
127
131 private function generateApplicationList(): \Iterator
132 {
133 foreach (Placement::getPlacementList() as $placement)
134 {
135 foreach (PlacementTable::getHandlersList($placement) as $handler)
136 {
137 yield new Application\Entity([
138 'id' => $handler['ID'],
139 'placement' => $placement,
140 'options' => $handler['OPTIONS'],
141 'restApplicationId' => $handler['APP_ID'],
142 'title' => $handler['TITLE'],
143 ]);
144 }
145 }
146 }
147
148 private function getApplicationIdList(): array
149 {
150 $result = [];
151 foreach ($this->generateApplicationList() as $application)
152 {
153 if ($this->isApplicationAvailable($application))
154 {
155 $result[] = $application->id;
156 }
157 }
158
159 return $result;
160 }
161
162 public static function getRestEntityName(): string
163 {
164 return 'placementApplications';
165 }
166
167 public function toRestFormat(array $option = []): array
168 {
169 return [
170 'items' => array_map(
171 static fn ($appEntity) => $appEntity->toRestFormat(),
172 $this->getApplications()
173 ),
174 'links' => [
175 'load' => $this->getLoadUri(),
176 ],
177 ];
178 }
179}
setOrder(int $applicationsId, int $order)