Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventcontroller.php
1<?php
2
3namespace Bitrix\Rest\Preset;
4
9
15{
16 private static $skipMode = false;
17 private static $tmpAppList = [];
18 private static $tmpUsesLangAppList = [];
19 private static $tmpApList = [];
20 private static $tmpApPermissionList = [];
21
26 public static function disableEvents()
27 {
28 static::$skipMode = true;
29 return true;
30 }
31
36 public static function enableEvents()
37 {
38 static::$skipMode = false;
39 return true;
40 }
41
47 public static function onAddApp(Event $event)
48 {
49 if (!static::$skipMode)
50 {
51 $id = intVal($event->getParameter('id'));
52 $fields = $event->getParameter('fields');
53 if ($id > 0 && $fields['STATUS'] == AppTable::STATUS_LOCAL)
54 {
55 $scope = explode(',', $fields['SCOPE']);
56 $result = IntegrationTable::add(
57 [
58 'ELEMENT_CODE' => Element::DEFAULT_APPLICATION,
59 'TITLE' => $fields['APP_NAME'],
60 'APP_ID' => $fields['APP_ID'],
61 'SCOPE' => $scope,
62 'APPLICATION_ONLY_API' => 'Y',
63 'APPLICATION_NEEDED' => 'Y',
64 'QUERY_NEEDED' => 'N',
65 'OUTGOING_NEEDED' => 'N',
66 'WIDGET_NEEDED' => 'N',
67 'BOT_NEEDED' => 'N',
68 ]
69 );
70
71 if ($result->isSuccess())
72 {
73 $integrationID = $result->getId();
74 static::$tmpAppList[$id] = $integrationID;
75 }
76 }
77 }
78 }
79
85 public static function onAddAppLang(Event $event)
86 {
87 if (!static::$skipMode)
88 {
89 $id = intVal($event->getParameter('id'));
90 $fields = $event->getParameter('fields');
91 if ($id > 0 && !empty(static::$tmpAppList[$fields['APP_ID']]) && !isset(static::$tmpUsesLangAppList[$fields['APP_ID']]))
92 {
93 static::$tmpUsesLangAppList[$fields['APP_ID']] = $fields['LANGUAGE_ID'];
94 try
95 {
96 $integrationId = static::$tmpAppList[$fields['APP_ID']];
97 IntegrationTable::update(
98 $integrationId,
99 [
100 'APPLICATION_ONLY_API' => 'N',
101 ]
102 );
103 }
104 catch (\Exception $e)
105 {
106 }
107 }
108 }
109 }
110
116 public static function onAfterAddEvent(Event $event)
117 {
118 if (!static::$skipMode)
119 {
120 $id = intVal($event->getParameter('id'));
121 $fields = $event->getParameter('fields');
122 if ($id > 0 && !$fields['APP_ID'] > 0 && !$fields['INTEGRATION_ID'] > 0)
123 {
124 $result = IntegrationTable::add(
125 [
126 'ELEMENT_CODE' => Element::DEFAULT_OUT_WEBHOOK,
127 'TITLE' => $fields['TITLE'],
128 'USER_ID' => $fields['USER_ID'],
129 'APPLICATION_TOKEN' => $fields['APPLICATION_TOKEN'],
130 'OUTGOING_EVENTS' => [
131 $fields['EVENT_NAME']
132 ],
133 'OUTGOING_HANDLER_URL' => $fields['EVENT_HANDLER'],
134 'APPLICATION_NEEDED' => 'N',
135 'QUERY_NEEDED' => 'N',
136 'OUTGOING_NEEDED' => 'Y',
137 'WIDGET_NEEDED' => 'N',
138 'BOT_NEEDED' => 'N',
139 ]
140 );
141
142 if ($result->isSuccess())
143 {
144 EventTable::update(
145 $result->getId(),
146 [
147 'INTEGRATION_ID' => $result->getId()
148 ]
149 );
150 }
151 }
152 }
153 }
154
160 public static function onAfterAddAp(Event $event)
161 {
162 if (!static::$skipMode)
163 {
164 $id = intVal($event->getParameter('id'));
165 if ($id > 0)
166 {
168 $fields = $event->getParameter('fields');
169 $result = IntegrationTable::add(
170 [
171 'ELEMENT_CODE' => Element::DEFAULT_IN_WEBHOOK,
172 'TITLE' => $fields['TITLE'],
173 'PASSWORD_ID' => $id,
174 'USER_ID' => $fields['USER_ID'],
175 'SCOPE' => [],
176 'APPLICATION_NEEDED' => 'N',
177 'QUERY_NEEDED' => 'Y',
178 'OUTGOING_NEEDED' => 'N',
179 'WIDGET_NEEDED' => 'N',
180 'BOT_NEEDED' => 'N',
181
182 ]
183 );
184 if ($result->isSuccess())
185 {
186 $integrationID = $result->getId();
187 static::$tmpApList[$id] = $integrationID;
188 }
189 }
190 }
191 }
192
198 public static function onAfterAddApPermission(Event $event)
199 {
200 if (!static::$skipMode)
201 {
202 $id = intVal($event->getParameter('id'));
203 if ($id > 0)
204 {
206 $fields = $event->getParameter('fields');
207 if (!isset(static::$tmpApList[$fields['PASSWORD_ID']]) ||
208 !isset(static::$tmpApPermissionList[$fields['PASSWORD_ID']]))
209 {
210 $res = IntegrationTable::getList(
211 [
212 'filter' => [
213 'PASSWORD_ID' => $fields['PASSWORD_ID']
214 ],
215 'select' => [
216 'ID',
217 'PASSWORD_ID',
218 'SCOPE'
219 ]
220 ]
221 );
222 if ($integration = $res->fetch())
223 {
224 static::$tmpApList[$integration['PASSWORD_ID']] = $integration['ID'];
225 static::$tmpApPermissionList[$integration['PASSWORD_ID']] = $integration['SCOPE'];
226 }
227 }
228
229 if (
230 array_key_exists($fields['PASSWORD_ID'], static::$tmpApPermissionList)
231 && !in_array($fields['PERM'], static::$tmpApPermissionList[$fields['PASSWORD_ID']])
232 )
233 {
234 static::$tmpApPermissionList[$fields['PASSWORD_ID']][] = $fields['PERM'];
235
236 try
237 {
238 $integrationId = static::$tmpApList[$fields['PASSWORD_ID']];
239 $scopeList = static::$tmpApPermissionList[$fields['PASSWORD_ID']];
240 IntegrationTable::update(
241 $integrationId,
242 [
243 'SCOPE' => $scopeList
244 ]
245 );
246 }
247 catch (\Exception $e)
248 {
249 }
250 }
251 }
252 }
253 }
254}
getParameter($key)
Definition event.php:80
const STATUS_LOCAL
Definition app.php:74