1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Structure.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Integration\HumanResources;
4
5use Bitrix\HumanResources\Builder\Structure\NodeDataBuilder;
6use Bitrix\HumanResources\Config\Feature;
7use Bitrix\HumanResources\Config\Storage;
8use Bitrix\HumanResources\Item\NodeRelation;
9use Bitrix\HumanResources\Service\Container;
10use Bitrix\HumanResources\Type\AccessCodeType;
11use Bitrix\HumanResources\Type\RelationEntitySubtype;
12use Bitrix\HumanResources\Type\RelationEntityType;
13use Bitrix\Im\V2\Chat;
14use Bitrix\Im\V2\Result;
15use Bitrix\Main\Loader;
16use Bitrix\Main\UI\EntitySelector\Converter;
17
19{
20 protected Chat $chat;
21
22 public function __construct(Chat $chat)
23 {
24 $this->chat = $chat;
25 }
26
27 public static function isTeamsAvailable(): bool
28 {
29 return Loader::includeModule('humanresources')
30 && Storage::instance()->isCompanyStructureConverted()
31 && Feature::instance()->isCrossFunctionalTeamsAvailable()
32 ;
33 }
34
35 public static function splitEntities(array $entities): array
36 {
37 $entities = static::convertEntities($entities);
38 $users = [];
39 $structureNodes = [];
40
41 foreach ($entities as $entity)
42 {
43 if (str_starts_with($entity, 'U'))
44 {
45 $users[] = (int)mb_substr($entity, 1);
46 }
47 if (static::isStructureNode($entity))
48 {
49 $structureNodes[] = $entity;
50 }
51 }
52
53 return [$users, $structureNodes];
54 }
55
56 public static function isSyncAvailable(): bool
57 {
58 return Loader::includeModule('humanresources')
59 && Storage::instance()->isCompanyStructureConverted()
60 ;
61 }
62
63 public function link(array $structureNodeIds): Result
64 {
65 $result = new Result();
66
67 if (empty($structureNodeIds))
68 {
69 return $result;
70 }
71
72 if (!Loader::includeModule('humanresources'))
73 {
74 return $result->addError(new Error(Error::LINK_ERROR));
75 }
76
77 $nodeRelationService = Container::getNodeRelationService();
78
79 foreach ($structureNodeIds as $structureNodeId)
80 {
81 try {
82 $nodeRelationService->linkEntityToNodeByAccessCode(
83 $structureNodeId,
84 RelationEntityType::CHAT,
85 $this->chat->getId(),
86 RelationEntitySubtype::fromChatType($this->chat->getType()),
87 );
88 }
89 catch (\Exception $exception)
90 {
91 $result->addError(new Error(Error::LINK_ERROR));
92 }
93 }
94
95 return $result;
96 }
97
98 public function unlink(array $structureNodeIds): Result
99 {
100 $result = new Result();
101
102 if (empty($structureNodeIds))
103 {
104 return $result;
105 }
106
107 if (!Loader::includeModule('humanresources'))
108 {
109 return $result->addError(new Error(Error::UNLINK_ERROR));
110 }
111
112 $nodeRelationService = Container::getNodeRelationService();
113
114 foreach ($structureNodeIds as $structureNodeId)
115 {
116 try {
117 $nodeRelationService->unlinkEntityFromNodeByAccessCode(
118 $structureNodeId,
119 RelationEntityType::CHAT,
120 $this->chat->getId(),
121 );
122 }
123 catch (\Exception $exception)
124 {
125 $result->addError(new Error(Error::UNLINK_ERROR));
126 }
127 }
128
129 return $result;
130 }
131
132 public function unlinkAll(): Result
133 {
134 $result = new Result();
135
136 if (!Loader::includeModule('humanresources'))
137 {
138 return $result->addError(new Error(Error::UNLINK_ERROR));
139 }
140
141 $nodeRelationRepository = Container::getNodeRelationRepository();
142
143 try
144 {
145 $nodeRelationRepository->deleteRelationByEntityTypeAndEntityIds(
146 RelationEntityType::CHAT,
147 [(int)$this->chat->getId()],
148 );
149 }
150 catch (\Exception)
151 {
152 return $result->addError(new Error(Error::UNLINK_ERROR));
153 }
154
155 return $result;
156 }
157
158 protected static function convertEntities(array $entities): array
159 {
160 if (!Loader::includeModule('ui'))
161 {
162 return [];
163 }
164
165 return Converter::convertToFinderCodes($entities);
166 }
167
171 public function getNodesAccessCodes(): array
172 {
173 $accessCodes = [];
174
175 if (!Loader::includeModule('humanresources'))
176 {
177 return $accessCodes;
178 }
179
180 $nodeRelationService = Container::getNodeRelationService();
181
182 $links = $nodeRelationService->findAllRelationsByEntityTypeAndEntityId(
183 RelationEntityType::CHAT,
184 $this->chat->getId(),
185 );
186
187 foreach ($links as $link)
188 {
189 if ($link->node === null)
190 {
191 continue;
192 }
193
194 if ($link->node->isDepartment())
195 {
196 $accessCodes[] = $link->withChildNodes
197 ? str_replace('D', 'DR', $link->node->accessCode)
198 : $link->node->accessCode;
199 }
200 if ($link->node->isTeam())
201 {
202 $accessCodes[] = $link->withChildNodes
203 ? AccessCodeType::HrTeamRecursiveType->buildAccessCode($link->nodeId)
204 : AccessCodeType::HrTeamType->buildAccessCode($link->nodeId);
205 }
206 }
207
208 return $accessCodes;
209 }
210
211 private static function isStructureNode(string $entity): bool
212 {
213 if (!Loader::includeModule('humanresources'))
214 {
215 return false;
216 }
217
218 $prefixes = [
219 ...AccessCodeType::getIntranetDepartmentTypesPrefixes(),
220 ...AccessCodeType::getTeamTypesPrefixes(),
221 ];
222 foreach ($prefixes as $prefix)
223 {
224 if (str_starts_with($entity, $prefix))
225 {
226 return true;
227 }
228 }
229
230 return false;
231 }
232}
unlink(array $structureNodeIds)
Определения Structure.php:98
static splitEntities(array $entities)
Определения Structure.php:35
static convertEntities(array $entities)
Определения Structure.php:158
link(array $structureNodeIds)
Определения Structure.php:63
Определения result.php:20
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity