Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
metauserprovider.php
1<?
2
4
10
12{
13 private const SUPPORTED_IDS = [self::ALL_USERS, self::OTHER_USERS];
14
15 private const ALL_USERS = 'all-users';
16 private const OTHER_USERS = 'other-users';
17
18 public function __construct(array $options = [])
19 {
20 parent::__construct();
21
22 $internalizeOption = function (array $options, string $key): void {
23 if (isset($options[$key]))
24 {
25 $this->options[$key] = is_array($options[$key]) ? $options[$key] : [];
26 if (
27 !isset($this->options[$key]['allowView'])
28 || !is_bool($this->options[$key]['allowView'])
29 )
30 {
31 $this->options[$key]['allowView'] = null;
32 }
33 }
34 };
35
36 foreach (self::SUPPORTED_IDS as $id)
37 {
38 $internalizeOption($options, $id);
39 }
40 }
41
42 public function isAvailable(): bool
43 {
44 return $GLOBALS['USER']->isAuthorized();
45 }
46
47 public function fillDialog(Dialog $dialog): void
48 {
49 if (!self::canViewAllUsers())
50 {
51 return;
52 }
53
54 $options = $this->getOptions();
55 $ids = [];
56 foreach (self::SUPPORTED_IDS as $id)
57 {
58 if (isset($options[$id]) && $options[$id]['allowView'] !== false)
59 {
60 $ids[] = $id;
61 }
62 }
63
64 foreach (self::getMetaUsers($ids, $options) as $metaUser)
65 {
66 $dialog->addRecentItem($metaUser);
67 }
68 }
69
70 public function getItems(array $ids): array
71 {
72 return self::getMetaUsers($ids, $this->getOptions());
73 }
74
75 public function getSelectedItems(array $ids): array
76 {
77 $options = [];
78 foreach (self::SUPPORTED_IDS as $id)
79 {
80 $options[$id] = [
81 'allowView' => true,
82 'deselectable' => self::canViewAllUsers(),
83 ];
84 }
85
86 return self::getMetaUsers($ids, array_merge($options, $this->getOptions()));
87 }
88
89 public static function canViewAllUsers(): bool
90 {
91 $intranetInstalled = ModuleManager::isModuleInstalled('intranet');
92
93 return !$intranetInstalled || ($intranetInstalled && UserProvider::isIntranetUser());
94 }
95
96 public static function getMetaUsers(array $ids, $options = []): array
97 {
98 $users = [];
99
100 $sort = 1;
101 foreach ($ids as $id)
102 {
103 if (!isset($users[$id]) && in_array($id, self::SUPPORTED_IDS, true))
104 {
105 $itemOptions =
106 isset($options[$id]) && is_array($options[$id]) ? $options[$id]: []
107 ;
108
109 $canView =
110 isset($itemOptions['allowView']) && is_bool($itemOptions['allowView'])
111 ? $itemOptions['allowView'] :
113 ;
114
115 if ($canView)
116 {
117 $itemOptions['sort'] ??= $sort;
118 $users[$id] = self::getMetaUserItem($id, $itemOptions);
119 $sort++;
120 }
121 }
122 }
123
124 return array_values($users);
125 }
126
127 public static function getAllUsersItem(array $options = []): Item
128 {
129 return self::getMetaUserItem(self::ALL_USERS, $options);
130 }
131
132 private static function getMetaUserItem(string $id, array $options = []): Item
133 {
134 $title = isset($options['title']) && is_string($options['title']) ? $options['title'] : '';
135 if (empty($title))
136 {
137 $title = self::getTitle($id);
138 }
139
140 $deselectable =
141 isset($options['deselectable']) && is_bool($options['deselectable']) ? $options['deselectable'] : true
142 ;
143
144 $searchable =
145 isset($options['searchable']) && is_bool($options['searchable']) ? $options['searchable'] : false
146 ;
147
148 $availableInRecentTab =
149 isset($options['availableInRecentTab']) && is_bool($options['availableInRecentTab'])
150 ? $options['availableInRecentTab']
151 : true
152 ;
153
154 $sort = isset($options['sort']) && is_numeric($options['sort']) ? (int)$options['sort'] : 1;
155
156 return new Item([
157 'id' => $id,
158 'entityId' => 'meta-user',
159 'entityType' => $id,
160 'title' => $title,
161 'searchable' => $searchable,
162 'saveable' => false,
163 'deselectable' => $deselectable,
164 'availableInRecentTab' => $availableInRecentTab,
165 'sort' => $sort,
166 ]);
167 }
168
169 private static function getTitle(string $id): ?string
170 {
171 $intranetInstalled = ModuleManager::isModuleInstalled('intranet');
172
173 if ($id === self::ALL_USERS)
174 {
175 return Loc::getMessage(
176 $intranetInstalled ? 'SOCNET_ENTITY_SELECTOR_ALL_EMPLOYEES' : 'SOCNET_ENTITY_SELECTOR_ALL_USERS'
177 );
178 }
179
180 if ($id === self::OTHER_USERS)
181 {
182 return Loc::getMessage(
183 $intranetInstalled ? 'SOCNET_ENTITY_SELECTOR_OTHER_EMPLOYEES' : 'SOCNET_ENTITY_SELECTOR_OTHER_USERS'
184 );
185 }
186
187 return null;
188 }
189}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1444769544']
Definition license.php:1