Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
restmanager.php
1<?php
2
3
4namespace Bitrix\Sale\Rest;
5
6
14
16{
17
18//RegisterModuleDependences("rest", "onRestGetModule", "sale", "\\Bitrix\\Sale\\Rest\\RestManager", "onRestGetModule");
19//RegisterModuleDependences("rest", "OnRestServiceBuildDescription", "sale", "\\Bitrix\\Sale\\Rest\\RestManager", "onRestServiceBuildDescription");
22
23// public static function onRestGetModule()
24// {
25// return ['MODULE_ID' => 'sale'];
26// }
27
28 public static function onRestServiceBuildDescription()
29 {
30 Loader::includeModule('sale');
31
32 return [
33 'sale' => [
34 \CRestUtil::EVENTS=>[
35 'OnSaleOrderSaved'=>[
36 'sale',
37 'OnSaleOrderSaved',
38 [
39 RestManager::class,
40 'processEvent'
41 ],
42 [
43 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
44 ]
45 ],
46 'OnSaleBeforeOrderDelete'=>[
47 'sale',
48 'OnSaleBeforeOrderDelete',
49 [
50 RestManager::class,
51 'processEvent'
52 ],
53 [
54 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
55 ]
56 ],
57
58 'OnPropertyValueEntitySaved'=>[
59 'sale',
60 'OnSalePropertyValueEntitySaved',
61 [
62 RestManager::class,
63 'processEvent'
64 ],
65 [
66 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
67 ]
68 ],
69 'OnPaymentEntitySaved'=>[
70 'sale',
71 'OnSalePaymentEntitySaved',
72 [
73 RestManager::class,
74 'processEvent'
75 ],
76 [
77 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
78 ]
79 ],
80 'OnShipmentEntitySaved'=>[
81 'sale',
82 'OnSaleShipmentEntitySaved',
83 [
84 RestManager::class,
85 'processEvent'
86 ],
87 [
88 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
89 ]
90 ],
91 'OnOrderEntitySaved'=>[
92 'sale',
93 'OnSaleOrderEntitySaved',
94 [
95 RestManager::class,
96 'processEvent'
97 ],
98 [
99 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
100 ]
101 ],
102 'OnPropertyValueDeleted'=>[
103 'sale',
104 'OnSalePropertyValueDeleted',
105 [
106 RestManager::class,
107 'processEvent'
108 ],
109 [
110 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
111 ]
112 ],
113 'OnPaymentDeleted'=>[
114 'sale',
115 'OnSalePaymentDeleted',
116 [
117 RestManager::class,
118 'processEvent'
119 ],
120 [
121 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
122 ]
123 ],
124 'OnShipmentDeleted'=>[
125 'sale',
126 'OnSaleShipmentDeleted',
127 [
128 RestManager::class,
129 'processEvent'
130 ],
131 [
132 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
133 ]
134 ],
135 'OnOrderDeleted'=>[
136 'sale',
137 'OnSaleOrderEntitySaved',
138 [
139 RestManager::class,
140 'processEvent'
141 ],
142 [
143 'category' => \Bitrix\Rest\Sqs::CATEGORY_CRM
144 ]
145 ],
146 ]
147 ]
148 ];
149 }
150
151 public static function processEvent(array $params, array $handlerFields)
152 {
153 $event = $params[0];
154 $eventName = $handlerFields['EVENT_NAME'];
155 $eventHandler = $handlerFields['EVENT_HANDLER'];
156
157 $instance = Manager::getInstance();
158
159 LoggerDiag::addMessage('processEvent', var_export([
160 'processEvent [process-01]'=> [
161 'eventName'=>$eventName,
162 'action'=>$instance->getAction()
163 ]
164 ], true));
165
166 switch(mb_strtolower($eventName))
167 {
168 case 'onsaleordersaved':
169
171 $entity = $event->getParameters()['ENTITY'];
172
173 // ,
174 // , .. onsaleordersaved
175 // deleted onsalebeforeorderdeleterest
176 // ,
177 if($instance->getAction() == Manager::ACTION_IMPORT || $instance->getAction() == Manager::ACTION_DELETED)
178 {
179 //
180 throw new RestException("Event stopped");
181 }
182 elseif($instance->isExecutedHandler($eventHandler))
183 {
184 //
185 throw new RestException("Event stopped");
186 }
187
188
189 if($entity->getId() <= 0)
190 {
191 throw new RestException("Could not find entity ID in fields of event \"{$eventName}\"");
192 }
193
194 // , |
195 $parameters = ['FIELDS' => ['ID' => $entity->getId(), 'XML_ID' => $entity->getField('XML_ID'), 'ACTION' => Synchronizer::MODE_SAVE]];
196
197 LoggerDiag::addMessage(mb_strtolower($eventName), var_export([
198 'processEvent [process-02]' => [
199 'parameters' => $parameters
200 ]
201 ], true));
202
203 // , ,
204 $instance->pushHandlerExecuted($eventHandler);
205
206 return $parameters;
207 break;
208 case 'onsalebeforeorderdelete':
209
211 $entity = $event->getParameters()['ENTITY'];
212
213 // ,
214 // ,
215 // (onpropertyvaluedeleted)
216 if($instance->getAction() == Manager::ACTION_IMPORT || $instance->getAction() == Manager::ACTION_DELETED)
217 {
218 throw new RestException("Event stopped");
219 }
220
221 //TODO: chack - action deleted onsaleordersavedrest.
222 $instance->setAction(Manager::ACTION_DELETED);
223
224 // , |
225 $parameters = ['FIELDS' => ['ID' => $entity->getId(), 'XML_ID' => $entity->getField('XML_ID'), 'ACTION' => Synchronizer::MODE_DELETE]];
226
227 LoggerDiag::addMessage(mb_strtolower($eventName), var_export([
228 'processEvent [process-03]' => [
229 'parameters' => $parameters
230 ]
231 ], true));
232
233 return $parameters;
234 break;
235
236 case 'onpropertyvalueentitysaved':
237 case 'onpaymententitysaved':
238 case 'onshipmententitysaved':
239 case 'onorderentitysaved':
240 case 'onpropertyvaluedeleted':
241 case 'onpaymentdeleted':
242 case 'onshipmentdeleted':
243 case 'onorderdeleted':
244
246 $entity = $event->getParameters()['ENTITY'];
247 $entityId = 0;
248 if($entity !== null)
249 {
250 $entityId = $entity->getId();
251 }
252 elseif(isset($event->getParameters()['VALUES']))
253 {
254 $entityId = $event->getParameters()['VALUES']['ID'];
255 }
256
257 $parameters = ['FIELDS' => ['ID' => $entityId]];
258
259 LoggerDiag::addMessage(mb_strtolower($eventName), var_export([
260 'processEvent [process-04]' => [
261 'parameters' => $parameters
262 ]
263 ], true));
264
265 return $parameters;
266 break;
267 default:
268 throw new RestException("The Event \"{$eventName}\" is not supported in current context");
269 }
270 }
271}