Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
controller.php
1<?php
2
3
5
6
17/*
18 * Error code notation x(category1) xxx(category2) xxx(code category) xxxxx(code) - 2 000 403 00010
19 * category1:
20 * Intrnalizer - 1
21 * Controller - 2
22 * Externalazer - 3
23 * */
24class Controller extends Engine\Controller
25{
26 protected function isCrmModuleInstalled()
27 {
28 return ModuleManager::isModuleInstalled('crm');
29 }
30
31 protected function processBeforeAction(Engine\Action $action)
32 {
33 $r = $this->checkPermission($action->getName());
34 if($r->isSuccess())
35 {
36 if($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
37 {
38 $internalizer = \Bitrix\Crm\Order\Rest\Internalizer::buildByAction($action, [], $this->getScope());
39 }
40 else
41 {
42 $internalizer = \Bitrix\Sale\Rest\Internalizer::buildByAction($action, [], $this->getScope());
43 }
44
45 $r = $internalizer->process();
46
47 if($r->isSuccess())
48 {
49 $action->setArguments($r->getData()['data']);
50 return parent::processBeforeAction($action);
51 }
52 else
53 {
54 $this->addErrors($r->getErrors());
55 return null;
56 }
57 }
58 else
59 {
60 $this->addErrors($r->getErrors());
61 return null;
62 }
63 }
64
65 protected function processAfterAction(Engine\Action $action, $result)
66 {
67 $externalizer = null;
68 if($this->errorCollection->count()==0)
69 {
70 if($result instanceof Engine\Response\DataType\Page || is_array($result))
71 {
72 $data = $result instanceof Engine\Response\DataType\Page ?
73 [$result->getId()=>$result->getItems()]:$result;
74
75 if($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
76 {
77 $externalizer = \Bitrix\Crm\Order\Rest\Externalizer::buildByAction($action, $data, $this->getScope());
78 }
79 else
80 {
81 $externalizer = \Bitrix\Sale\Rest\Externalizer::buildByAction($action, $data, $this->getScope());
82 }
83 }
84 }
85 else
86 {
87 return parent::processAfterAction($action, $result);
88 }
89
90 if($externalizer instanceof ModificationFieldsBase)
91 {
93 {
94 // nothing
95 }
96 else if($this->getScope() == Engine\Controller::SCOPE_AJAX)
97 {
98 $externalizer->setFormat([
99 ModificationFieldsBase::TO_WHITE_LIST,
100 ModificationFieldsBase::SORTING_KEYS
101 ]);
102 }
103
105 {
106 return $result instanceof Engine\Response\DataType\Page ?
107 $externalizer->getPage($result):$externalizer;
108 }
109 else if($this->getScope() == Engine\Controller::SCOPE_AJAX)
110 {
111 return $externalizer->toArray();
112 }
113 }
114
115 return parent::processAfterAction($action, $result);
116 }
117
118 protected static function getApplication()
119 {
121 global $APPLICATION;
122
123 return $APPLICATION;
124 }
125
126 protected static function getNavData($start, $bORM = false)
127 {
128 if($start >= 0)
129 {
130 return ($bORM ?
131 ['limit' => \IRestService::LIST_LIMIT, 'offset' => intval($start)]
132 :['nPageSize' => \IRestService::LIST_LIMIT, 'iNumPage' => intval($start / \IRestService::LIST_LIMIT) + 1]
133 );
134 }
135 else
136 {
137 return ($bORM ?
138 ['limit' => \IRestService::LIST_LIMIT]
139 :['nTopCount' => \IRestService::LIST_LIMIT]
140 );
141 }
142 }
143
144 public function getBuilder(SettingsContainer $settings=null)
145 {
146 $settings = $settings === null? $this->getSettingsContainerDefault():$settings;
147
148 return ($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
149 ? new \Bitrix\Crm\Order\Builder\OrderBuilderRest($settings)
150 : new \Bitrix\Sale\Helpers\Order\Builder\OrderBuilderRest($settings);
151 }
152
153 protected function getSettingsContainerDefault()
154 {
155 return new SettingsContainer([
156 'deleteClientsIfNotExists' => true,
157 'deleteTradeBindingIfNotExists' => true,
158 'deletePaymentIfNotExists' => true,
159 'deleteShipmentIfNotExists' => true,
160 'deleteShipmentItemIfNotExists' => true,
161 'deletePropertyValuesIfNotExists' => true,
162 'createDefaultPaymentIfNeed' => false,
163 'createDefaultShipmentIfNeed' => false,
164 'createUserIfNeed' => SettingsContainer::SET_ANONYMOUS_USER,
165 'cacheProductProviderData' => false,
166 'propsFiles' => $this->getFielsPropertyValuesFromRequest(),
167 'acceptableErrorCodes' => []
168 ]);
169 }
170
172 {
173 $orderProperties = [];
174
175 if(isset($this->request->getFileList()['fields']['PROPERTIES']))
176 {
177 foreach ($this->request->getFileList()['fields']['PROPERTIES'] as $orderPropId => $arFileData)
178 {
179 if (is_array($arFileData))
180 {
181 foreach ($arFileData as $param_name => $value)
182 {
183 if (is_array($value))
184 {
185 foreach ($value as $nIndex => $val)
186 {
187 if ($arFileData["name"][$nIndex] <> '')
188 $orderProperties[$orderPropId][$nIndex][$param_name] = $val;
189 }
190 }
191 else
192 $orderProperties[$orderPropId][$param_name] = $value;
193 }
194 }
195 }
196 }
197 return $orderProperties;
198 }
199
200 protected function toArray(\Bitrix\Sale\Order $order, array $fields=[])
201 {
202 // , ,
203 $fields = array_merge($fields, $this->getAdditionalFields($order));
204
205 if($this->isCrmModuleInstalled() && Loader::includeModule('crm'))
206 {
207 $director = new \Bitrix\Crm\Order\Rest\Normalizer\Director();
208 $normalizer = new \Bitrix\Crm\Order\Rest\Normalizer\ObjectNormalizer($fields);
209 }
210 else
211 {
212 $director = new \Bitrix\Sale\Rest\Normalizer\Director();
213 $normalizer = new \Bitrix\Sale\Rest\Normalizer\ObjectNormalizer($fields);
214 }
215
216 return $director->normalize($normalizer, $order);
217 }
218
219 private function getAdditionalFields(\Bitrix\Sale\Order $order)
220 {
221 $ixInternal = [];
222 //region fill internal Index
223 foreach(\Bitrix\Sale\PersonType::getList(['select'=>['ID', 'XML_ID']]) as $row)
224 $ixInternal['personType'][$row['ID']] = $row['XML_ID'];
225
226 foreach(\Bitrix\Sale\OrderStatus::getList(['select'=>['ID', 'XML_ID']]) as $row)
227 $ixInternal['orderStatus'][$row['ID']] = $row['XML_ID'];
228
229 foreach(\Bitrix\Sale\Property::getList(['select'=>['ID', 'XML_ID']])->fetchAll() as $row)
230 $ixInternal['properties'][$row['ID']] = $row['XML_ID'];
231
232 foreach(\Bitrix\Sale\PaySystem\Manager::getList(['select'=>['ID', 'XML_ID', 'IS_CASH']])->fetchAll() as $row)
233 {
234 $ixInternal['paySystems'][$row['ID']]['XML_ID'] = $row['XML_ID'];
235 $ixInternal['paySystems'][$row['ID']]['IS_CASH'] = $row['IS_CASH'];
236 }
237
238 foreach(\Bitrix\Sale\Delivery\Services\Manager::getActiveList() as $row)
239 $ixInternal['deliverySystems'][$row['ID']] = $row['XML_ID'];
240
241 foreach(\Bitrix\Sale\DeliveryStatus::getList(['select'=>['ID', 'XML_ID']]) as $row)
242 $ixInternal['deliveryStatus'][$row['ID']] = $row['XML_ID'];
243
244 foreach(\Bitrix\Sale\TradingPlatformTable::getList(['select'=>['ID', 'XML_ID']])->fetchAll() as $row)
245 $ixInternal['tradingPlatform'][$row['ID']] = $row['XML_ID'];
246 //endregion
247
248 $r['ORDER'][$order->getInternalId()] = [
249 'PERSON_TYPE_XML_ID'=>$ixInternal['personType'][$order->getPersonTypeId()],
250 'STATUS_XML_ID'=>$ixInternal['orderStatus'][$order->getField('STATUS_ID')]];
251
252 foreach ($order->getPropertyCollection() as $property)
253 $r['PROPERTIES'][$property->getInternalIndex()] = ['ORDER_PROPS_XML_ID'=>$ixInternal['properties'][$property->getPropertyId()]];
254
255 foreach ($order->getPaymentCollection() as $payment)
256 $r['PAYMENTS'][$payment->getInternalIndex()] = [
257 'PAY_SYSTEM_XML_ID'=>$ixInternal['paySystems'][$payment->getPaymentSystemId()]['XML_ID'],
258 'PAY_SYSTEM_IS_CASH'=>$ixInternal['paySystems'][$payment->getPaymentSystemId()]['IS_CASH']
259 ];
260
262 foreach ($order->getShipmentCollection() as $shipment)
263 {
264 $shipmentIndex = $shipment->getInternalIndex();
265 $r['SHIPMENTS'][$shipmentIndex] = [
266 'DELIVERY_XML_ID'=>$ixInternal['deliverySystems'][$shipment->getDeliveryId()],
267 'STATUS_XML_ID'=>$ixInternal['deliveryStatus'][$shipment->getField('STATUS_ID')]];
268 }
269
271 foreach ($order->getTradeBindingCollection() as $binding)
272 if($binding->getTradePlatform() !== null)
273 $r['TRADE_BINDINGS'][$binding->getInternalIndex()] = ['TRADING_PLATFORM_XML_ID'=>$ixInternal['tradingPlatform'][$binding->getTradePlatform()->getId()]];
274
275 return $r;
276 }
277
278 private function checkPermission($name)
279 {
280 $r = new Result();
281
282 if($name == 'add')
283 {
284 $r = $this->checkCreatePermissionEntity();
285 }
286 elseif ($name == 'update')
287 {
288 $r = $this->checkUpdatePermissionEntity();
289 }
290 elseif ($name == 'list')
291 {
292 $r = $this->checkReadPermissionEntity();
293 }
294 elseif ($name == 'getfields'){}
295 elseif ($name == 'get')
296 {
297 $r = $this->checkReadPermissionEntity();
298 }
299 elseif ($name == 'delete')
300 {
301 $r = $this->checkDeletePermissionEntity();
302 }
303 elseif ($name == 'modify')
304 {
305 $r = $this->checkModifyPermissionEntity();
306 }
307 elseif ($name == 'tryadd')
308 {
309 $r = $this->checkCreatePermissionEntity();
310 }
311 elseif ($name == 'tryupdate')
312 {
313 $r = $this->checkUpdatePermissionEntity();
314 }
315 elseif ($name == 'trymodify')
316 {
317 $r = $this->checkModifyPermissionEntity();
318 }
319 else
320 {
321 $r = $this->checkPermissionEntity($name);
322 }
323
324 return $r;
325 }
326
327 protected function checkReadPermissionEntity()
328 {
329 $r = new Result();
330
331 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
332 if ($saleModulePermissions == "D")
333 {
334 $r->addError(new Error('Access Denied', 200040300010));
335 }
336 return $r;
337 }
338
339 protected function checkModifyPermissionEntity()
340 {
341 $r = new Result();
342
343 $saleModulePermissions = self::getApplication()->GetGroupRight("sale");
344 if ($saleModulePermissions < "W")
345 {
346 $r->addError(new Error('Access Denied', 200040300020));
347 }
348 return $r;
349 }
350
351 protected function checkCreatePermissionEntity()
352 {
353 return $this->checkModifyPermissionEntity();
354 }
355
356 protected function checkUpdatePermissionEntity()
357 {
358 return $this->checkModifyPermissionEntity();
359 }
360
361 protected function checkDeletePermissionEntity()
362 {
363 return $this->checkModifyPermissionEntity();
364 }
365
366 protected function checkPermissionEntity($name)
367 {
368 throw new NotImplementedException('Check permission entity. The method '.$name.' is not implemented.');
369 }
370}
toArray(\Bitrix\Sale\Order $order, array $fields=[])
processBeforeAction(Engine\Action $action)
processAfterAction(Engine\Action $action, $result)
static getNavData($start, $bORM=false)
getBuilder(SettingsContainer $settings=null)
static getList(array $parameters=[])