Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
scope.php
1<?php
2
4
16use CUserOptions;
17
22class Scope
23{
24 protected const CODE_USER = 'U';
25 protected const CODE_PROJECT = 'SG';
26 protected const CODE_DEPARTMENT = 'DR';
27
28 protected const TYPE_USER = 'user';
29 protected const TYPE_PROJECT = 'project';
30 protected const TYPE_DEPARTMENT = 'department';
31
32 protected $user;
33 protected static $instance = null;
34
38 public static function getInstance(): Scope
39 {
40 if (self::$instance === null)
41 {
42 Loader::includeModule('ui');
43 self::$instance = ServiceLocator::getInstance()->get('ui.entityform.scope');
44 }
45 return self::$instance;
46 }
47
53 public function getUserScopes(string $entityTypeId, ?string $moduleId = null): array
54 {
55 static $results = [];
56 $key = $entityTypeId . '-' . $moduleId;
57
58 if (!isset($results[$key]))
59 {
60 $result = [];
61 $scopeIds = $this->getScopesIdByUser($moduleId);
62 $entityTypeIds = ($this->getEntityTypeIdMap()[$entityTypeId] ?? [$entityTypeId]);
63
64 if (!empty($scopeIds))
65 {
66 $scopes = EntityFormConfigTable::getList([
67 'select' => [
68 'ID',
69 'NAME',
70 'ACCESS_CODE' => '\Bitrix\Ui\EntityForm\EntityFormConfigAcTable:CONFIG.ACCESS_CODE'
71 ],
72 'filter' => [
73 '@ID' => $scopeIds,
74 '@ENTITY_TYPE_ID' => $entityTypeIds
75 ]
76 ]);
77 foreach ($scopes as $scope)
78 {
79 $result[$scope['ID']]['NAME'] = HtmlFilter::encode($scope['NAME']);
80 if (!isset($result[$scope['ID']]['ACCESS_CODES'][$scope['ACCESS_CODE']]))
81 {
82 $accessCode = new AccessCode($scope['ACCESS_CODE']);
83 $member = (new DataProvider())->getEntity($accessCode->getEntityType(),
84 $accessCode->getEntityId());
85 $result[$scope['ID']]['ACCESS_CODES'][$scope['ACCESS_CODE']] = $scope['ACCESS_CODE'];
86 $result[$scope['ID']]['MEMBERS'][$scope['ACCESS_CODE']] = $member->getMetaData();
87 }
88 }
89 }
90 $results[$key] = $result;
91 }
92
93 return $results[$key];
94 }
95
96 protected function getEntityTypeIdMap(): array
97 {
98 return [
99 'lead_details' => ['lead_details', 'returning_lead_details'],
100 'returning_lead_details' => ['lead_details', 'returning_lead_details'],
101 ];
102 }
103
108 public function isHasScope(int $scopeId): bool
109 {
110 return in_array($scopeId, $this->getScopesIdByUser());
111 }
112
116 protected function getUser()
117 {
118 if ($this->user === null)
119 {
120 global $USER;
121 $this->user = $USER;
122 }
123 return $this->user;
124 }
125
126 private function getScopesIdByUser(?string $moduleId = null): array
127 {
128 $accessCodes = $this->getUser()->GetAccessCodes();
129 $this->prepareAccessCodes($accessCodes);
130
131 $params = [
132 'select' => [
133 'CONFIG_ID'
134 ]
135 ];
136
137 if(
138 !$moduleId
139 ||
140 (
141 ($scopeAccess = ScopeAccess::getInstance($moduleId))
142 && !$scopeAccess->isAdmin()
143 )
144 )
145 {
146 $params['filter'] = ['@ACCESS_CODE' => $accessCodes];
147 }
148
149 $scopes = EntityFormConfigAcTable::getList($params)->fetchAll();
150
151 $result = [];
152 if (count($scopes))
153 {
154 foreach ($scopes as $scope)
155 {
156 $result[] = $scope['CONFIG_ID'];
157 }
158 }
159
160 return array_unique($result);
161 }
162
163 protected function prepareAccessCodes(array &$accessCodes): void
164 {
165 $accessCodes = array_filter($accessCodes, static fn($code) => mb_strpos($code, 'CHAT') !== 0);
166
167 foreach ($accessCodes as &$accessCode)
168 {
169 $accessCode = preg_replace('|^(SG\d*?)(_[K,A,M])$|', '$1', $accessCode);
170 }
171 unset($accessCode);
172 }
173
178 public function getScopeById(int $scopeId): ?array
179 {
180 if ($row = EntityFormConfigTable::getRowById($scopeId))
181 {
182 return (is_array($row['CONFIG']) ? $row['CONFIG'] : null);
183 }
184 return null;
185 }
186
191 public function getById(int $scopeId): ?array
192 {
193 return EntityFormConfigTable::getRowById($scopeId);
194 }
195
200 public function removeByIds(iterable $ids): void
201 {
202 foreach ($ids as $id)
203 {
204 $this->removeById($id);
205 }
206 }
207
212 private function removeById(int $id): DeleteResult
213 {
214 $this->removeScopeMembers($id);
215 return EntityFormConfigTable::delete($id);
216 }
217
225 public function setScope(string $categoryName, string $guid, string $scope, int $userScopeId = 0): void
226 {
227 $this->setScopeToUser($categoryName, $guid, $scope, $userScopeId);
228 }
229
230 public function setScopeConfig(
231 string $category,
232 string $entityTypeId,
233 string $name,
234 array $accessCodes,
235 array $config,
236 array $params = []
237 )
238 {
239 if (empty($name))
240 {
241 $errors['name'] = new Error(Loc::getMessage('FIELD_REQUIRED'));
242 }
243 if (empty($accessCodes))
244 {
245 $errors['accessCodes'] = new Error(Loc::getMessage('FIELD_REQUIRED'));
246 }
247 if (!empty($errors))
248 {
249 return $errors;
250 }
251
252 $this->formatAccessCodes($accessCodes);
253
254 $result = EntityFormConfigTable::add([
255 'CATEGORY' => $category,
256 'ENTITY_TYPE_ID' => $entityTypeId,
257 'NAME' => $name,
258 'CONFIG' => $config,
259 'COMMON' => ($params['common'] ?? 'Y'),
260 ]);
261
262 if ($result->isSuccess())
263 {
264 $configId = $result->getId();
265 foreach ($accessCodes as $ac)
266 {
267 EntityFormConfigAcTable::add([
268 'ACCESS_CODE' => $ac['id'],
269 'CONFIG_ID' => $configId,
270 ]);
271 }
272
273 $forceSetToUsers = ($params['forceSetToUsers'] ?? false);
274 if (mb_strtoupper($forceSetToUsers) === 'FALSE')
275 {
276 $forceSetToUsers = false;
277 }
278
279 $this->forceSetScopeToUsers($accessCodes, [
280 'forceSetToUsers' => $forceSetToUsers,
281 'categoryName' => ($params['categoryName'] ?? ''),
282 'entityTypeId' => $entityTypeId,
283 'configId' => $configId,
284 ]);
285
286 return $configId;
287 }
288
289 return $result->getErrors();
290 }
291
295 protected function formatAccessCodes(array &$accessCodes): void
296 {
297 foreach ($accessCodes as $key => $item)
298 {
299 if ($item['entityId'] === self::TYPE_USER)
300 {
301 $accessCodes[$key]['id'] = self::CODE_USER . (int)$accessCodes[$key]['id'];
302 }
303 elseif ($item['entityId'] === self::TYPE_DEPARTMENT)
304 {
305 $accessCodes[$key]['id'] = self::CODE_DEPARTMENT . (int)$accessCodes[$key]['id'];
306 }
307 elseif ($item['entityId'] === self::TYPE_PROJECT)
308 {
309 $accessCodes[$key]['id'] = self::CODE_PROJECT . (int)$accessCodes[$key]['id'];
310 }
311 else{
312 unset($accessCodes[$key]);
313 }
314 }
315 }
316
321 protected function forceSetScopeToUsers(array $accessCodes = [], array $params = []): void
322 {
323 if ($params['forceSetToUsers'] && $params['categoryName'])
324 {
325 $userIdPattern = '/^U(\d+)$/';
326 foreach ($accessCodes as $ac)
327 {
328 $matches = [];
329 if (preg_match($userIdPattern, $ac['id'], $matches))
330 {
331 $this->setScopeToUser(
332 $params['categoryName'],
333 $params['entityTypeId'],
335 $params['configId'],
336 $matches[1]
337 );
338 }
339 }
340 }
341 }
342
350 protected function setScopeToUser(
351 string $categoryName,
352 string $guid,
353 string $scope,
354 int $userScopeId,
355 ?int $userId = null
356 ): void
357 {
358 $scope = (isset($scope) ? strtoupper($scope) : EntityEditorConfigScope::UNDEFINED);
359
361 {
362 if ($scope === EntityEditorConfigScope::CUSTOM && $userScopeId)
363 {
364 $value = [
365 'scope' => $scope,
366 'userScopeId' => $userScopeId
367 ];
368 }
369 else
370 {
371 $value = $scope;
372 }
373
374 $userId = ($userId ?? false);
375 CUserOptions::SetOption($categoryName, "{$guid}_scope", $value, false, $userId);
376 }
377 }
378
379 public function updateScopeConfig(int $id, array $config)
380 {
381 return EntityFormConfigTable::update($id, [
382 'CONFIG' => $config
383 ]);
384 }
385
386 public function updateScopeName(int $id, string $name): UpdateResult
387 {
388 return EntityFormConfigTable::update($id, [
389 'NAME' => $name,
390 ]);
391 }
392
398 public function updateScopeAccessCodes(int $configId, array $accessCodes = []): array
399 {
400 $this->removeScopeMembers($configId);
401
402 foreach ($accessCodes as $ac => $type)
403 {
404 EntityFormConfigAcTable::add([
405 'ACCESS_CODE' => $ac,
406 'CONFIG_ID' => $configId,
407 ]);
408 }
409
410 return $this->getScopeMembers($configId);
411 }
412
417 public function getScopeMembers(int $configId): array
418 {
419 $accessCodes = EntityFormConfigAcTable::getList([
420 'select' => ['ACCESS_CODE'],
421 'filter' => ['=CONFIG_ID' => $configId]
422 ])->fetchAll();
423 $result = [];
424 if (count($accessCodes))
425 {
426 foreach ($accessCodes as $accessCodeEntity)
427 {
428 $accessCode = new AccessCode($accessCodeEntity['ACCESS_CODE']);
429 $member = (new DataProvider())->getEntity($accessCode->getEntityType(), $accessCode->getEntityId());
430 $result[$accessCodeEntity['ACCESS_CODE']] = $member->getMetaData();
431 }
432 }
433 return $result;
434 }
435
439 private function removeScopeMembers(int $configId): void
440 {
441 $entity = EntityFormConfigAcTable::getEntity();
442 $connection = $entity->getConnection();
443
444 $filter = ['CONFIG_ID' => $configId];
445
446 $connection->query(sprintf(
447 'DELETE FROM %s WHERE %s',
448 $connection->getSqlHelper()->quote($entity->getDBTableName()),
449 Query::buildFilterSql($entity, $filter)
450 ));
451 }
452
453}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getInstance(string $moduleId, int $userId=null)
getScopeMembers(int $configId)
Definition scope.php:417
getById(int $scopeId)
Definition scope.php:191
getUserScopes(string $entityTypeId, ?string $moduleId=null)
Definition scope.php:53
formatAccessCodes(array &$accessCodes)
Definition scope.php:295
updateScopeAccessCodes(int $configId, array $accessCodes=[])
Definition scope.php:398
prepareAccessCodes(array &$accessCodes)
Definition scope.php:163
updateScopeName(int $id, string $name)
Definition scope.php:386
isHasScope(int $scopeId)
Definition scope.php:108
setScopeConfig(string $category, string $entityTypeId, string $name, array $accessCodes, array $config, array $params=[])
Definition scope.php:230
setScope(string $categoryName, string $guid, string $scope, int $userScopeId=0)
Definition scope.php:225
getScopeById(int $scopeId)
Definition scope.php:178
forceSetScopeToUsers(array $accessCodes=[], array $params=[])
Definition scope.php:321
setScopeToUser(string $categoryName, string $guid, string $scope, int $userScopeId, ?int $userId=null)
Definition scope.php:350
removeByIds(iterable $ids)
Definition scope.php:200
updateScopeConfig(int $id, array $config)
Definition scope.php:379