Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manifest.php
1<?php
2
4
8
10{
11 public const ACCESS_TYPE_IMPORT = 'import';
12 public const ACCESS_TYPE_EXPORT = 'export';
13 public const ON_REST_APP_CONFIGURATION_GET_MANIFEST = 'OnRestApplicationConfigurationGetManifest';
14 public const ON_REST_APP_CONFIGURATION_GET_MANIFEST_SETTING = 'OnRestApplicationConfigurationGetManifestSetting';
15 public const PROPERTY_REST_IMPORT_AVAILABLE = 'REST_IMPORT_AVAILABLE';
16 private static $manifestList = [];
17
18 public static function getList()
19 {
20 if (empty(static::$manifestList))
21 {
22 $event = new Event('rest', static::ON_REST_APP_CONFIGURATION_GET_MANIFEST);
23 EventManager::getInstance()->send($event);
24 foreach ($event->getResults() as $eventResult)
25 {
26 $manifestList = $eventResult->getParameters();
27 if (is_array($manifestList))
28 {
29 static::$manifestList = array_merge(static::$manifestList, $manifestList);
30 }
31 }
32 }
33
34 return static::$manifestList;
35 }
36
37 public static function callEventInit($code, $params = [])
38 {
39 $result = [];
40 $manifest = static::get($code);
41
42 if ($manifest !== false && isset($params['TYPE']) && isset($params['CONTEXT_USER']))
43 {
44 $step = intval($params['STEP']);
45 $setting = new Setting($params['CONTEXT_USER']);
46 if ($step === 0)
47 {
48 $setting->delete(Setting::SETTING_MANIFEST);
49 }
50
51 $event = new Event(
52 'rest',
53 static::ON_REST_APP_CONFIGURATION_GET_MANIFEST_SETTING,
54 [
55 'CODE' => $manifest['CODE'],
56 'MANIFEST' => $manifest,
57 'TYPE' => $params['TYPE'],
58 'CONTEXT' => $params['CONTEXT'] ?? false,
59 'CONTEXT_USER' => $params['CONTEXT_USER'],
60 'STEP' => $step,
61 'NEXT' => isset($params['NEXT']) ? $params['NEXT'] : null,
62 'ITEM_CODE' => $params['ITEM_CODE'] ? : null,
63 'ADDITIONAL_OPTION' => is_array($params['ADDITIONAL_OPTION']) ? $params['ADDITIONAL_OPTION'] : [],
64 'SETTING' => $setting->get(Setting::SETTING_MANIFEST),
65 'USER_ID' => $setting->get(Setting::SETTING_USER_ID) ?? 0,
66 ]
67 );
68 EventManager::getInstance()->send($event);
69 foreach ($event->getResults() as $eventResult)
70 {
71 $parameters = $eventResult->getParameters();
72 if (isset($parameters['SETTING']))
73 {
74 $setting->set(Setting::SETTING_MANIFEST, $parameters['SETTING']);
75 }
76
77 $result[] = [
78 'NEXT' => isset($parameters['NEXT']) ? $parameters['NEXT'] : false,
79 'ERROR_MESSAGES' => $parameters['ERROR_MESSAGES'] ?? null,
80 'ERROR_ACTION' => $parameters['ERROR_ACTION'] ?? null
81 ];
82 }
83 }
84 return $result;
85 }
86
87 public static function get($code)
88 {
89 $result = null;
90 if ($code != '')
91 {
92 $manifestList = static::getList();
93 $key = array_search($code, array_column($manifestList, 'CODE'));
94 if ($key !== false)
95 {
96 $result = $manifestList[$key];
97 }
98 }
99
100 return $result;
101 }
102
111 public static function checkAccess(string $type, $manifestCode = ''): array
112 {
113 $result = [
114 'result' => false,
115 'message' => '',
116 ];
117
118 if (\CRestUtil::isAdmin())
119 {
120 $result['result'] = true;
121 }
122 elseif (!empty($manifestCode))
123 {
124 $manifest = static::get($manifestCode);
125 try
126 {
127 if (
128 !empty($manifest['ACCESS']['MODULE_ID'])
129 && is_array($manifest['ACCESS']['CALLBACK'])
130 && Loader::includeModule($manifest['ACCESS']['MODULE_ID'])
131 && is_callable($manifest['ACCESS']['CALLBACK'])
132 )
133 {
134 $access = call_user_func($manifest['ACCESS']['CALLBACK'], $type, $manifest);
135 $result['result'] = $access['result'] === true;
136 $result['message'] = (is_string($access['message']) && $access['message'] !== '') ? $access['message'] : '';
137 }
138 }
139 catch (\Exception $exception)
140 {
141 }
142 }
143
144 return $result;
145 }
146
156 public static function isEntityAvailable(string $entityCode, array $option, $uses = []): bool
157 {
158 $manifest = [];
159 if (!empty($option['IMPORT_MANIFEST']['USES']))
160 {
161 $manifest = $option['IMPORT_MANIFEST'];
162 }
163 elseif (!empty($option['MANIFEST']['USES']))
164 {
165 $manifest = $option['MANIFEST'];
166 }
167
168 if (empty($manifest['USES']))
169 {
170 return false;
171 }
172
173 $access = array_intersect($manifest['USES'], $uses);
174 if (!$access)
175 {
176 return false;
177 }
178
179 return true;
180 }
181
187 public static function isRestImportAvailable(string $entityCode): bool
188 {
189 $manifest = static::get($entityCode);
190 return isset($manifest[static::PROPERTY_REST_IMPORT_AVAILABLE]) && $manifest[static::PROPERTY_REST_IMPORT_AVAILABLE] === 'Y';
191 }
192}
send($sender=null)
Definition event.php:139
static callEventInit($code, $params=[])
Definition manifest.php:37
static checkAccess(string $type, $manifestCode='')
Definition manifest.php:111
static isRestImportAvailable(string $entityCode)
Definition manifest.php:187
static isEntityAvailable(string $entityCode, array $option, $uses=[])
Definition manifest.php:156