Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
appconfiguration.php
1<?php
2
4
12
14{
15 public const REST_APPLICATION = 'REST_APPLICATION';
16 private static $entityList = [
17 self::REST_APPLICATION => 100,
18 ];
19
20 private static $accessManifest = [
21 'total',
22 'app'
23 ];
24
25 public static function getEntityList()
26 {
27 return static::$entityList;
28 }
29
30 public static function getManifestList(Event $event)
31 {
32 $manifestList = [];
33 $manifestList[] = [
34 'CODE' => 'total',
35 'VERSION' => 1,
36 'ACTIVE' => 'N',
37 'PLACEMENT' => [],
38 'USES' => [
39 'total'
40 ]
41 ];
42
43 return $manifestList;
44 }
45
46 public static function onEventExportController(Event $event)
47 {
48 $result = null;
49 $code = $event->getParameter('CODE');
50 if (
51 !static::$entityList[$code]
52 || !Manifest::isEntityAvailable($code, $event->getParameters(), static::$accessManifest)
53 )
54 {
55 return $result;
56 }
57
58 if (static::checkRequiredParams($code))
59 {
60 $step = $event->getParameter('STEP');
61 $setting = $event->getParameter('SETTING');
62 switch ($code)
63 {
64 case 'REST_APPLICATION':
65 $result = static::exportApp($step, $setting);
66 break;
67 }
68 }
69
70 return $result;
71 }
72
73 public static function onEventClearController(Event $event)
74 {
75 $result = null;
76 if (!static::checkAccessImport($event))
77 {
78 return $result;
79 }
80
81 $code = $event->getParameter('CODE');
82 if (static::checkRequiredParams($code))
83 {
84 $option = $event->getParameters();
85 switch ($code)
86 {
87 case 'REST_APPLICATION':
88 $result = static::clearApp($option);
89 break;
90 }
91 }
92
93 return $result;
94 }
95
96 public static function onEventImportController(Event $event)
97 {
98 $result = null;
99 if (!static::checkAccessImport($event))
100 {
101 return $result;
102 }
103
104 $code = $event->getParameter('CODE');
105 if (static::checkRequiredParams($code))
106 {
107 $data = $event->getParameters();
108 switch ($code)
109 {
110 case 'REST_APPLICATION':
111 $result = static::importApp($data);
112 break;
113 }
114 }
115
116 return $result;
117 }
118
119 private static function checkAccessImport(Event $event)
120 {
121 $code = $event->getParameter('CODE');
122 if (
123 !isset(static::$entityList[$code])
124 || !static::$entityList[$code]
125 || !Manifest::isEntityAvailable($code, $event->getParameters(), static::$accessManifest)
126 )
127 {
128 return false;
129 }
130
131 return true;
132 }
133
139 private static function checkRequiredParams($type)
140 {
141 return true;
142 }
143
144 //region application
145 private static function importApp($item)
146 {
147 $result = false;
148 if (!empty($item['CONTENT']['DATA']['code']))
149 {
150 $code = preg_replace('/[^a-zA-Z0-9._\-]/', '', $item['CONTENT']['DATA']['code']);
151 if ($code !== $item['CONTENT']['DATA']['code'])
152 {
153 return [
154 'ERROR_EXCEPTION' => [
155 'message' => Loc::getMessage(
156 'REST_CONFIGURATION_ERROR_UNKNOWN_APP'
157 ),
158 ],
159 ];
160 }
161
162 Application::setContextUserId((int)$item['USER_ID']);
163 $result = Application::install($code);
164 if ($result['success'])
165 {
166 $res = AppTable::getList(
167 [
168 'filter' => [
169 '=CODE' => $code,
170 '=ACTIVE' => AppTable::ACTIVE,
171 '=INSTALLED' => AppTable::NOT_INSTALLED,
172 '!=URL_INSTALL' => false,
173 ],
174 'limit' => 1
175 ]
176 );
177 if ($app = $res->fetch())
178 {
179 $res = EventTable::getList(
180 [
181 'filter' => [
182 "APP_ID" => $app['ID'],
183 "EVENT_NAME" => "ONAPPINSTALL",
184 "EVENT_HANDLER" => $app["URL_INSTALL"],
185 ],
186 'limit' => 1
187 ]
188 );
189 if (!$event = $res->fetch())
190 {
191 $res = EventTable::add(
192 [
193 "APP_ID" => $app['ID'],
194 "EVENT_NAME" => "ONAPPINSTALL",
195 "EVENT_HANDLER" => $app["URL_INSTALL"],
196 ]
197 );
198 if ($res->isSuccess())
199 {
200 Sender::bind('rest', 'OnRestAppInstall');
201 }
202
204 AppTable::update(
205 $app['ID'],
206 [
207 'INSTALLED' => AppTable::INSTALLED
208 ]
209 );
211
213
214 AppTable::install($app['ID']);
215 }
216 }
217 }
218 elseif (is_array($result) && $result['errorDescription'])
219 {
220 $result['ERROR_EXCEPTION']['message'] = Loc::getMessage(
221 'REST_CONFIGURATION_ERROR_INSTALL_APP_CONTENT_DATA',
222 [
223 '#ERROR_CODE#' => $result['error'],
224 '#ERROR_MESSAGE#' => $result['errorDescription'],
225 ]
226 );
227 }
228 else
229 {
230 $result['ERROR_EXCEPTION']['message'] = Loc::getMessage(
231 'REST_CONFIGURATION_ERROR_INSTALL_APP_CONTENT'
232 );
233 }
234 }
235 else
236 {
237 return [
238 'ERROR_EXCEPTION' => [
239 'message' => Loc::getMessage(
240 'REST_CONFIGURATION_ERROR_UNKNOWN_APP'
241 ),
242 ],
243 ];
244 }
245
246 return $result;
247 }
248
249 private static function clearApp($option)
250 {
251 $result = [
252 'NEXT' => false
253 ];
254 if ($option['CLEAR_FULL'])
255 {
256 $dbRes = AppTable::getList(
257 [
258 'order' => [
259 'ID' => 'ASC'
260 ],
261 'filter' => [
262 '=ACTIVE' => AppTable::ACTIVE,
263 "!=STATUS" => AppTable::STATUS_LOCAL,
264 '>ID' => $option['NEXT']
265 ],
266 'limit' => 5
267 ]
268 );
269
270 while ($appInfo = $dbRes->Fetch())
271 {
272 $result['NEXT'] = $appInfo['ID'];
273
274 $currentApp = Helper::getInstance()->getContextAction($appInfo['ID']);
275 if (!empty($option['CONTEXT']) && $option['CONTEXT'] === $currentApp)
276 {
277 continue;
278 }
279
280 $checkResult = AppTable::checkUninstallAvailability($appInfo['ID']);
281 if ($checkResult->isEmpty())
282 {
283 AppTable::uninstall($appInfo['ID']);
284 $appFields = [
285 'ACTIVE' => 'N',
286 'INSTALLED' => 'N',
287 ];
288 AppTable::update($appInfo['ID'], $appFields);
290 }
291 }
292 }
293
294 return $result;
295 }
296
297 public static function exportApp($step, $setting)
298 {
299 $return = [
300 'FILE_NAME' => '',
301 'CONTENT' => [],
302 'NEXT' => false
303 ];
304
305 $filter = [
306 '!=STATUS' => AppTable::STATUS_LOCAL,
307 '=ACTIVE' => AppTable::ACTIVE,
308 ];
309
310 if (is_array($setting))
311 {
312 if (!empty($setting['APP_REQUIRED']) && is_array($setting['APP_REQUIRED']))
313 {
314 $filter = [
315 [
316 'LOGIC' => 'OR',
317 $filter,
318 [
319 '=ID' => $setting['APP_REQUIRED'],
320 ],
321 ],
322 ];
323 }
324 elseif (array_key_exists('APP_USES_REQUIRED', $setting))
325 {
326 $filter = [];
327 if (!empty($setting['APP_USES_REQUIRED']))
328 {
329 $filter = [
330 '=ID' => $setting['APP_USES_REQUIRED'],
331 ];
332 }
333 }
334 }
335
336 if (!empty($filter))
337 {
338 $res = AppTable::getList(
339 [
340 'order' => [
341 'ID' => 'ASC',
342 ],
343 'filter' => $filter,
344 'select' => [
345 'ID',
346 'CODE',
347 ],
348 'limit' => 1,
349 'offset' => $step,
350 ]
351 );
352
353 if ($app = $res->Fetch())
354 {
355 $return['FILE_NAME'] = $step;
356 $return['NEXT'] = $step;
357 $return['CONTENT'] = [
358 'code' => $app['CODE'],
359 'settings' => [],
360 ];
361 }
362 }
363
364 return $return;
365 }
366 //end region application
367}
getParameter($key)
Definition event.php:80
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static log($appId, $action)
Definition applog.php:90
static install($appId)
Definition app.php:459
const STATUS_LOCAL
Definition app.php:74
static uninstall($appId, $clean=0)
Definition app.php:513
const NOT_INSTALLED
Definition app.php:63
static setSkipRemoteUpdate($v)
Definition app.php:236
static isEntityAvailable(string $entityCode, array $option, $uses=[])
Definition manifest.php:156
static bind($moduleId, $eventName)
Definition sender.php:69