Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
globalsmanager.php
1<?php
2
4
8
9abstract class GlobalsManager
10{
11 protected static $allCache;
12 protected static $allSortByVisibilityCache;
13
14 abstract protected static function getTableEntity(): string;
15
16 abstract protected static function getCacheId(): string;
17
18 abstract public static function getObjectNameForExpressions(): string;
19
20 public static function getAll(array $parameterDocumentType = []): array
21 {
22 // TODO: if the user is an admin then show all globals ?
23 return static::getAllAvailable($parameterDocumentType);
24 }
25
26 protected static function getAllAvailable(array $parameterDocumentType = []): array
27 {
28 $cacheId = static::getCacheId();
29
30 if (!isset(static::$allSortByVisibilityCache[$cacheId]))
31 {
32 $all = static::getAllRows();
33 $allSortByVisibility = [];
34 foreach ($all as $id => $property)
35 {
36 $visibility = $property['Visibility'];
37 $allSortByVisibility[$visibility][$id] = $property;
38 }
39
40 static::$allSortByVisibilityCache[$cacheId] = $allSortByVisibility;
41 }
42
43 $global = static::$allSortByVisibilityCache[$cacheId]['GLOBAL'] ?? [];
44
45 if (!$parameterDocumentType)
46 {
47 return $global;
48 }
49
50 try
51 {
52 [$moduleId, $entity, $documentType] = \CBPHelper::ParseDocumentId($parameterDocumentType);
53 }
54 catch (\CBPArgumentNullException $e)
55 {
56 return $global;
57 }
58
59 $module = static::$allSortByVisibilityCache[$cacheId][mb_strtoupper($moduleId)] ?? [];
60 $document =
61 static::$allSortByVisibilityCache[$cacheId][mb_strtoupper($moduleId) . '_' . mb_strtoupper($documentType)]
62 ?? []
63 ;
64
65 return array_merge($global, $module, $document);
66 }
67
68 protected static function getAllRows(): array
69 {
70 $cacheId = static::getCacheId();
71
72 if (!isset(static::$allCache[$cacheId]))
73 {
74 $all = [];
75 $table = static::getTableEntity();
76 if (method_exists($table, 'getList') && method_exists($table, 'convertToProperty'))
77 {
78 $rows = $table::getList();
79
80 foreach ($rows as $row)
81 {
82 $all[$row['ID']] = $table::convertToProperty($row);
83 }
84 }
85
86 static::$allCache[$cacheId] = $all;
87 }
88
89 return static::$allCache[$cacheId];
90 }
91
92 public static function upsert($id, $property, int $userId = null): bool
93 {
94 return static::upsertByProperty($id, $property, $userId)->isSuccess();
95 }
96
97 public static function upsertByProperty($id, $property, int $userId = null): Result
98 {
99 $table = static::getTableEntity();
100 if (method_exists($table, 'upsertByProperty'))
101 {
102 $result = $table::upsertByProperty($id, $property, $userId);
103
104 $cacheId = static::getCacheId();
105 static::clearStaticCache($cacheId);
106
107 return $result;
108 }
109
110 return (new Result())->addError(
111 new Error(Loc::getMessage('BIZPROC_LIB_WF_TYPE_GLOBALS_MANAGER_CAN_NOT_UPSERT'))
112 );
113 }
114
115 public static function delete($id): bool
116 {
117 $table = static::getTableEntity();
118 if (method_exists($table, 'delete'))
119 {
120 $result = $table::delete($id);
121
122 $cacheId = static::getCacheId();
123 static::clearStaticCache($cacheId);
124
125 return $result->isSuccess();
126 }
127
128 return false;
129 }
130
131 protected static function clearStaticCache($cacheId)
132 {
133 static::$allCache[$cacheId] = null;
134 static::$allSortByVisibilityCache[$cacheId] = null;
135 }
136
137 public static function getById($id)
138 {
139 $all = static::getAllRows();
140
141 return $all[$id] ?? null;
142 }
143
144 public static function getVisibleById($id, $documentType)
145 {
146 $all = static::getAllAvailable($documentType);
147
148 return $all[$id] ?? null;
149 }
150
151 public static function getValue($id)
152 {
153 $property = is_array($id) ? $id : static::getById($id);
154
155 return $property ? $property['Default'] : null;
156 }
157
158 public static function canUserRead(array $documentType, int $userId): bool
159 {
160 $user = new \CBPWorkflowTemplateUser($userId);
161 if ($user->isAdmin())
162 {
163 return true;
164 }
165
166 $canCreateAutomation = \CBPDocument::CanUserOperateDocumentType(
167 \CBPCanUserOperateOperation::CreateAutomation,
168 $user->getId(),
169 $documentType
170 );
171 if ($canCreateAutomation)
172 {
173 return true;
174 }
175
176 return \CBPDocument::CanUserOperateDocumentType(
177 \CBPCanUserOperateOperation::CreateWorkflow,
178 $user->getId(),
179 $documentType
180 );
181 }
182
183 public static function canUserUpsert(array $documentType, int $userId): bool
184 {
185 return static::canUserRead($documentType, $userId);
186 }
187
188 public static function canUserDelete(array $documentType, int $userId): bool
189 {
190 return static::canUserRead($documentType, $userId);
191 }
192
193 public static function getAvailableVisibility(array $parameterDocumentType): array
194 {
195 [$moduleId, $entity, $documentType] = \CBPHelper::ParseDocumentId($parameterDocumentType);
196 if (in_array(mb_strtoupper($moduleId), ['CRM', 'RPA']))
197 {
198 return ['GLOBAL', mb_strtoupper($moduleId), mb_strtoupper($moduleId) . '_' . mb_strtoupper($documentType)];
199 }
200
201 return ['GLOBAL'];
202 }
203
204 public static function getVisibilityShortNames(array $parameterDocumentType): array
205 {
206 $runtime = \CBPRuntime::GetRuntime();
207 $runtime->StartRuntime();
208 $documentService = $runtime->GetService("DocumentService");
209
210 // TODO: if the user is an admin then return all visibility names ?
211 [$moduleId, $entity, $documentType] = \CBPHelper::ParseDocumentId($parameterDocumentType);
212 $documentCaption = $documentService->getDocumentTypeCaption($parameterDocumentType);
213
214 $names = [];
215 $names['GLOBAL'] = Loc::getMessage(
216 'BIZPROC_LIB_WF_TYPE_GLOBAL_FIELD_VISIBILITY_SHORT_GLOBAL'
217 );
218
219 switch (mb_strtoupper($moduleId))
220 {
221 case 'CRM':
222 case 'RPA':
223 $moduleVisibility = mb_strtoupper($moduleId);
224 $documentVisibility = $documentCaption;
225 break;
226 default:
227 $moduleVisibility = '';
228 $documentVisibility = '';
229 break;
230 }
231
232 if (!$moduleVisibility)
233 {
234 return $names;
235 }
236
237 $names[mb_strtoupper($moduleId)] = $moduleVisibility;
238 $names[mb_strtoupper($moduleId) . '_' . mb_strtoupper($documentType)] = $documentVisibility;
239
240 return $names;
241 }
242
243 abstract public static function getVisibilityFullNames(array $parameterDocumentType): array;
244
245 public static function loadLanguageFile(): void
246 {
247 Loc::loadLanguageFile(__FILE__);
248 }
249}
static getAll(array $parameterDocumentType=[])
static upsertByProperty($id, $property, int $userId=null)
static getAllAvailable(array $parameterDocumentType=[])
static canUserRead(array $documentType, int $userId)
static canUserDelete(array $documentType, int $userId)
static getAvailableVisibility(array $parameterDocumentType)
static getVisibilityFullNames(array $parameterDocumentType)
static canUserUpsert(array $documentType, int $userId)
static getVisibilityShortNames(array $parameterDocumentType)
static upsert($id, $property, int $userId=null)
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29