Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
delivery_paysystem.php
1<?php
3
11
12Loc::loadMessages(__FILE__);
13
39class DeliveryPaySystemTable extends \Bitrix\Main\Entity\DataManager
40{
43
44 const ENTITY_TYPE_DELIVERY = "DELIVERY_ID";
45 const ENTITY_TYPE_PAYSYSTEM = "PAYSYSTEM_ID";
46
47 protected static $unLinked = null;
48 protected static $entityItemsFullList = array();
49 protected static $entityItemsFieldsList = array();
50
51 public static function getFilePath()
52 {
53 return __FILE__;
54 }
55
56 public static function getTableName()
57 {
58 return 'b_sale_delivery2paysystem';
59 }
60
61 public static function getMap()
62 {
63 return array(
64 'DELIVERY_ID' => array(
65 'primary' => true,
66 'data_type' => 'integer',
67 'required' => true,
68 'title' => Loc::getMessage('DELIVERY_PAYSYSTEM_ENTITY_DELIVERY_ID_FIELD'),
69 ),
70 'DELIVERY' => array(
71 'data_type' => '\Bitrix\Sale\Delivery\Services\Table',
72 'reference' => array(
73 '=this.DELIVERY_ID' => 'ref.ID'
74 )
75 ),
76 'LINK_DIRECTION' => array(
77 'primary' => true,
78 'data_type' => 'string',
79 'validation' => array(__CLASS__, 'validateLinkDirection'),
80 'required' => true,
81 'title' => Loc::getMessage('DELIVERY_PAYSYSTEM_ENTITY_LINK_DIRECTION'),
82 ),
83 'PAYSYSTEM_ID' => array(
84 'primary' => true,
85 'data_type' => 'integer',
86 'required' => true,
87 'title' => Loc::getMessage('DELIVERY_PAYSYSTEM_ENTITY_PAYSYSTEM_ID_FIELD'),
88 ),
89 'PAYSYSTEM' => array(
90 'data_type' => '\Bitrix\Sale\Internals\PaySystemActionTable',
91 'reference' => array(
92 '=this.PAYSYSTEM_ID' => 'ref.ID'
93 )
94 )
95 );
96 }
97
98 public static function validateLinkDirection()
99 {
100 return array(
101 new \Bitrix\Main\Entity\Validator\Length(1, 1),
102 );
103 }
104
114 public static function setLinks($entityId, $entityType, array $linkedIds = array(), $actualizeRestrictions = true)
115 {
116 if(intval($entityId) <= 0)
117 throw new ArgumentNullException("entityId");
118
119 if($entityType != self::ENTITY_TYPE_DELIVERY && $entityType != self::ENTITY_TYPE_PAYSYSTEM)
120 throw new ArgumentOutOfRangeException("entityType");
121
122 $result = new \Bitrix\Main\Entity\Result();
123 $con = \Bitrix\Main\Application::getConnection();
124 $sqlHelper = $con->getSqlHelper();
125 $entityId = $sqlHelper->forSql($entityId);
126 $reverseParentLinks = array();
127
128 if($entityType == self::ENTITY_TYPE_DELIVERY)
129 {
131 $reverseLinkDirection = self::LINK_DIRECTION_PAYSYSTEM_DELIVERY;
132 $reverseEntityType = self::ENTITY_TYPE_PAYSYSTEM;
133 $parentId = self::getDeliveryParentId($entityId);
134 }
135 else
136 {
138 $reverseLinkDirection = self::LINK_DIRECTION_DELIVERY_PAYSYSTEM;
139 $reverseEntityType = self::ENTITY_TYPE_DELIVERY;
140 $parentId = 0;
141
142 if(!empty($linkedIds)) // for delivery profiles
143 {
144 $reverseFieldsList = self::getEntityItemsFieldsList($reverseEntityType);
145
146 foreach($linkedIds as $id)
147 if(isset($reverseFieldsList[$id]['PARENT_ID']) && self::isValidParent($reverseFieldsList[$id]['PARENT_ID'], $reverseEntityType))
148 if(!in_array($reverseFieldsList[$id]['PARENT_ID'], $linkedIds))
149 $reverseParentLinks[] = $reverseFieldsList[$id]['PARENT_ID'];
150
151 if(!empty($reverseParentLinks))
152 {
153 $linkedIds = array_unique(array_merge($linkedIds, $reverseParentLinks));
154 }
155 }
156 }
157
158 //delete current entity links
159 $con->queryExecute(
160 "DELETE FROM ".self::getTableName().
161 " WHERE ".$entityType."=".$entityId ." AND LINK_DIRECTION='".$linkDirection."'"
162 );
163
164 //insert new links
165 if(!empty($linkedIds))
166 self::insertLinks($entityId, $linkDirection, $entityType, $linkedIds);
167
168 $glParams = array(
169 'filter' => array(
170 '=LINK_DIRECTION' => $reverseLinkDirection,
171 )
172 );
173
174 if(!empty($linkedIds))
175 $glParams['filter'][$reverseEntityType] = $linkedIds;
176
177 $res = self::getList($glParams);
178
179 $linkedToEntity = array();
180 $linkedToOther = array();
181
182 while($rec = $res->fetch())
183 {
184 if($rec[$entityType] == $entityId)
185 {
186 if(!in_array($rec[$reverseEntityType], $linkedToEntity))
187 $linkedToEntity[] = $rec[$reverseEntityType];
188 }
189 else
190 {
191 if(!in_array($rec[$reverseEntityType], $linkedToOther))
192 $linkedToOther[] = $rec[$reverseEntityType];
193 }
194 }
195
196 $reverseIdsToAdd = array_diff($linkedToOther, $linkedToEntity);
197
198 //set reverse links to current entity
199 if(!empty($reverseIdsToAdd))
200 self::insertLinks($entityId, $reverseLinkDirection, $entityType, $reverseIdsToAdd);
201
202 //delete reverse links we didn't choose
203 $glParams = array(
204 'filter' => array(
205 '=LINK_DIRECTION' => $reverseLinkDirection,
206 '='.$entityType => $entityId,
207 )
208 );
209
210 if(!empty($linkedIds))
211 $glParams['filter']['!='.$reverseEntityType] = $linkedIds;
212
213 $res = self::getList($glParams);
214
215 while($rec = $res->fetch())
216 {
217 self::delete(array(
218 'DELIVERY_ID' => $rec['DELIVERY_ID'],
219 'PAYSYSTEM_ID' => $rec['PAYSYSTEM_ID'],
220 'LINK_DIRECTION' => $rec['LINK_DIRECTION']
221 ));
222 }
223
224 self::$unLinked = null;
225
226 //Modify delivery parent links for working profile links.
227 if(!empty($linkedIds))
228 {
229 $unlinked = self::getUnlinkedEnityItems($entityType);
230 if($entityType == self::ENTITY_TYPE_DELIVERY && self::isValidParent($parentId, $entityType) && !in_array($parentId, $unlinked))
231 {
232 $parentLinks = self::getLinks($parentId, $entityType, array(), false);
233
235 $parentId,
236 $entityType,
237 array_unique(array_merge($parentLinks, $linkedIds)),
238 false
239 );
240 }
241 elseif($entityType == self::ENTITY_TYPE_PAYSYSTEM)
242 {
243 $reverseFieldsList = self::getEntityItemsFieldsList($reverseEntityType);
244 $unlinkedReverse = self::getUnlinkedEnityItems($reverseEntityType);
245 $entityList = self::getEntityItemsFullList($entityType);
246 $entityList = array_diff($entityList, array($entityId));
247
248 foreach($reverseFieldsList as $id => $fields)
249 {
250 if(intval($fields['PARENT_ID']) > 0 && in_array($fields['PARENT_ID'], $reverseParentLinks) && in_array($id, $unlinkedReverse))
251 {
253 $id,
254 $reverseEntityType,
255 $entityList,
256 false
257 );
258 }
259 }
260 }
261 }
262
263 if($actualizeRestrictions)
264 {
267 }
268
269 return $result;
270 }
271
272 protected static function isValidParent($parentId, $entityType)
273 {
274 $parentId = intval($parentId);
275
276 if($parentId <= 0)
277 return false;
278
279 if($entityType == self::ENTITY_TYPE_DELIVERY)
280 {
281 $activeDeliveryData = self::getActiveDeliveryData();
282
283 if(!in_array($parentId, array_keys($activeDeliveryData)))
284 return false;
285
286 if($activeDeliveryData[$parentId]['PARENT_CLASS_NAME'] == '\Bitrix\Sale\Delivery\Services\Group')
287 return false;
288 }
289
290 return true;
291 }
292
293 protected static function actualizeDeliveriesRestrictionByPS()
294 {
295 $con = \Bitrix\Main\Application::getConnection();
296 $sqlHelper = $con->getSqlHelper();
297
298 $restrictions = array();
299 $dbR = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array(
300 'filter' => array(
301 '=CLASS_NAME' => '\Bitrix\Sale\Delivery\Restrictions\ByPaySystem'
302 ),
303 'select' => array('SERVICE_ID')
304 ));
305
306 while($restr = $dbR->fetch())
307 $restrictions[] = $restr['SERVICE_ID'];
308
309 $deliveryList = self::getEntityItemsFullList(self::ENTITY_TYPE_DELIVERY);
310 $dLinkedToP = array();
311 $deliveriesToPs = array();
312 $linkedPS = array();
313
314 $dbP2S = DeliveryPaySystemTable::getList();
315
316 while($d2p = $dbP2S->fetch())
317 {
318 if($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_DELIVERY_PAYSYSTEM && !in_array($d2p["DELIVERY_ID"], $dLinkedToP))
319 $dLinkedToP[] = $d2p["DELIVERY_ID"];
320
321 if($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_PAYSYSTEM_DELIVERY )
322 {
323 if(!isset($deliveriesToPs[$d2p["DELIVERY_ID"]]))
324 $deliveriesToPs[$d2p["DELIVERY_ID"]] = array();
325
326 $linkedPS[] = $d2p["PAYSYSTEM_ID"];
327 $deliveriesToPs[$d2p["DELIVERY_ID"]][] = $d2p["PAYSYSTEM_ID"];
328 }
329 }
330
331 $notLinkedToPS = array_diff($deliveryList, $dLinkedToP);
332 $existLinkedPs = !empty($linkedPS);
333 $notNeedRestriction = array();
334 $needRestriction = array();
335
336 foreach($deliveryList as $id)
337 {
338 $need = true;
339
340 //DS not linked to PS and (All PS having links linked to current DS
341 if(in_array($id, $notLinkedToPS))
342 {
343 if(isset($deliveriesToPs[$id]))
344 $diff = array_diff($linkedPS, $deliveriesToPs[$id]);
345 else
346 $diff = $linkedPS;
347
348 if(!$existLinkedPs || empty($diff))
349 {
350 $notNeedRestriction[] = $id;
351 $need = false;
352 }
353 }
354
355 // DS linked to PS or exist linked PS but not linked to current DS
356 if($need)
357 $needRestriction[] = $id;
358 }
359
360 $notNeedRestriction = array_intersect($notNeedRestriction, $restrictions);
361
362 if(!empty($notNeedRestriction))
363 {
364 $sql = "";
365
366 foreach($notNeedRestriction as $deliveryId)
367 $sql .= " ".($sql == "" ? "WHERE CLASS_NAME='".$sqlHelper->forSql('\Bitrix\Sale\Delivery\Restrictions\ByPaySystem')."' AND (" : "OR " )."SERVICE_ID=".$sqlHelper->forSql($deliveryId)." AND SERVICE_TYPE=".Restrictions\Manager::SERVICE_TYPE_SHIPMENT;
368
369 $sql = "DELETE FROM ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName().$sql.")";
370 $con->queryExecute($sql);
371 }
372
373 $needRestriction = array_diff($needRestriction, $restrictions);
374
375 //let's... add missing
376 if(!empty($needRestriction))
377 {
378 $sql = "";
379
380 foreach($needRestriction as $deliveryId)
381 $sql .= ($sql == "" ? " " : ", ")."(".$sqlHelper->forSql($deliveryId).", '".$sqlHelper->forSql('\Bitrix\Sale\Delivery\Restrictions\ByPaySystem')."', ".Restrictions\Manager::SERVICE_TYPE_SHIPMENT.")";
382
383 $sql = "INSERT INTO ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName()."(SERVICE_ID, CLASS_NAME, SERVICE_TYPE) VALUES".$sql;
384 $con->queryExecute($sql);
385 }
386 }
387
388 protected static function actualizePaySystemRestrictionByDelivery()
389 {
390 $con = \Bitrix\Main\Application::getConnection();
391 $sqlHelper = $con->getSqlHelper();
392
393 $restrictions = array();
394 $dbR = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array(
395 'filter' => array(
396 '=CLASS_NAME' => '\\'.\Bitrix\Sale\Services\PaySystem\Restrictions\Delivery::class
397 ),
398 'select' => array('SERVICE_ID')
399 ));
400
401 while($restr = $dbR->fetch())
402 $restrictions[] = $restr['SERVICE_ID'];
403
404 $deliveryList = self::getEntityItemsFullList(self::ENTITY_TYPE_PAYSYSTEM);
405 $dLinkedToP = array();
406 $deliveriesToPs = array();
407 $linkedPS = array();
408
409 $dbP2S = DeliveryPaySystemTable::getList();
410
411 while($d2p = $dbP2S->fetch())
412 {
413 if($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_PAYSYSTEM_DELIVERY && !in_array($d2p["PAYSYSTEM_ID"], $dLinkedToP))
414 $dLinkedToP[] = $d2p["PAYSYSTEM_ID"];
415
416 if($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_DELIVERY_PAYSYSTEM)
417 {
418 if(!isset($deliveriesToPs[$d2p["PAYSYSTEM_ID"]]))
419 $deliveriesToPs[$d2p["PAYSYSTEM_ID"]] = array();
420
421 $linkedPS[] = $d2p["DELIVERY_ID"];
422 $deliveriesToPs[$d2p["PAYSYSTEM_ID"]][] = $d2p["DELIVERY_ID"];
423 }
424 }
425
426 $notLinkedToPS = array_diff($deliveryList, $dLinkedToP);
427 $existLinkedPs = !empty($linkedPS);
428 $notNeedRestriction = array();
429 $needRestriction = array();
430
431 foreach($deliveryList as $id)
432 {
433 $need = true;
434
435 //DS not linked to PS and (All PS having links linked to current DS
436 if(in_array($id, $notLinkedToPS))
437 {
438 if(isset($deliveriesToPs[$id]))
439 $diff = array_diff($linkedPS, $deliveriesToPs[$id]);
440 else
441 $diff = $linkedPS;
442
443 if(!$existLinkedPs || empty($diff))
444 {
445 $notNeedRestriction[] = $id;
446 $need = false;
447 }
448 }
449
450 // DS linked to PS or exist linked PS but not linked to current DS
451 if($need)
452 $needRestriction[] = $id;
453 }
454
455 $notNeedRestriction = array_intersect($notNeedRestriction, $restrictions);
456
457 if(!empty($notNeedRestriction))
458 {
459 $sql = "";
460
461 foreach($notNeedRestriction as $deliveryId)
462 $sql .= " ".($sql == "" ? "WHERE CLASS_NAME='".$sqlHelper->forSql('\Bitrix\Sale\Services\PaySystem\Restrictions\Delivery')."' AND (" : "OR " )."SERVICE_ID=".$sqlHelper->forSql($deliveryId)." AND SERVICE_TYPE=".Manager::SERVICE_TYPE_PAYMENT;
463
464 $sql = "DELETE FROM ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName().$sql.")";
465 $con->queryExecute($sql);
466 }
467
468 $needRestriction = array_diff($needRestriction, $restrictions);
469
470 //let's... add missing
471 if(!empty($needRestriction))
472 {
473 $sql = "";
474
475 foreach($needRestriction as $deliveryId)
476 $sql .= ($sql == "" ? " " : ", ")."(".$sqlHelper->forSql($deliveryId).", '".$sqlHelper->forSql('\\'.\Bitrix\Sale\Services\PaySystem\Restrictions\Delivery::class)."', ".Manager::SERVICE_TYPE_PAYMENT.")";
477
478 $sql = "INSERT INTO ".\Bitrix\Sale\Internals\ServiceRestrictionTable::getTableName()."(SERVICE_ID, CLASS_NAME, SERVICE_TYPE) VALUES".$sql;
479 $con->queryExecute($sql);
480 }
481 }
482
483 protected static function insertLinks($entityId, $linkDirection, $entityType, $linkedIds)
484 {
485 $con = \Bitrix\Main\Application::getConnection();
486 $sqlHelper = $con->getSqlHelper();
487 $entityId = (int)$entityId;
488 $linkDirection = $sqlHelper->forSql($linkDirection);
489
490 $sql = "INSERT INTO ".
492 "(DELIVERY_ID, PAYSYSTEM_ID, LINK_DIRECTION) ".
493 "VALUES";
494
495 $first = true;
496
497 foreach($linkedIds as $id)
498 {
499 if(!$first)
500 $sql .= ",";
501 else
502 $first = false;
503
504 $id = (int)$id;
505
506 if($entityType == self::ENTITY_TYPE_DELIVERY)
507 $sql .= " (".$entityId.", ".$id;
508 else
509 $sql .= " (".$id.", ".$entityId;
510
511 $sql .= ", '".$linkDirection."')";
512 }
513
514 $con->queryExecute($sql);
515 }
516
525 public static function getLinks($entityId, $entityType, array $preparedData = array(), $considerParent = true)
526 {
527 $result = array();
528
529 if(intval($entityId) <= 0)
530 return array();
531
532 if($entityType != self::ENTITY_TYPE_DELIVERY && $entityType != self::ENTITY_TYPE_PAYSYSTEM)
533 throw new ArgumentOutOfRangeException("entityType");
534
535 if($entityType == self::ENTITY_TYPE_DELIVERY)
536 {
538 $reverseLinkDirection = self::LINK_DIRECTION_PAYSYSTEM_DELIVERY;
539 $reverseEntityType = self::ENTITY_TYPE_PAYSYSTEM;
540 $parentId = self::getDeliveryParentId($entityId);
541 }
542 else
543 {
545 $reverseLinkDirection = self::LINK_DIRECTION_DELIVERY_PAYSYSTEM;
546 $reverseEntityType = self::ENTITY_TYPE_DELIVERY;
547 $parentId = 0;
548 }
549
550 if(isset($preparedData[$entityId]["DIRECT"]))
551 {
552 $result = $preparedData[$entityId]["DIRECT"];
553 }
554 else
555 {
556 $glParams = array(
557 'filter' => array(
558 "=".$entityType => $entityId,
559 "=LINK_DIRECTION" => $linkDirection
560 ),
561 'select' => array($reverseEntityType)
562 );
563
564 $res = self::getList($glParams);
565
566 while($rec = $res->fetch())
567 $result[] = $rec[$reverseEntityType];
568 }
569
570 //if entity has links they must be actual
571 if(!empty($result))
572 {
573 if($considerParent)
574 {
575 if($entityType == self::ENTITY_TYPE_PAYSYSTEM)
576 {
577 $result = self::includeDeliveryByParent($result);
578 $result = self::excludeDeliveryByParent($result);
579 }
580 elseif($entityType == self::ENTITY_TYPE_DELIVERY && $parentId > 0)
581 {
582 $result = self::considerDeliveryParent($result, $entityId, $parentId, $preparedData);
583 }
584 }
585
586 return $result;
587 }
588
589 if(isset($preparedData[$entityId]["REVERSE"]))
590 {
591 $result = $preparedData[$entityId]["REVERSE"];
592 }
593 else
594 {
595 $glParams = array(
596 'filter' => array(
597 "=".$entityType => $entityId,
598 "=LINK_DIRECTION" => $reverseLinkDirection
599 ),
600 'select' => array($reverseEntityType)
601 );
602
603 $res = self::getList($glParams);
604
605 while($rec = $res->fetch())
606 $result[] = $rec[$reverseEntityType];
607 }
608
609 $result = array_merge($result, self::getUnlinkedEnityItems($reverseEntityType, $reverseLinkDirection));
610
611 if($considerParent)
612 {
613 if($entityType == self::ENTITY_TYPE_DELIVERY && $parentId > 0)
614 {
615 $result = self::considerDeliveryParent($result, $entityId, $parentId, $preparedData);
616 }
617 elseif($entityType == self::ENTITY_TYPE_PAYSYSTEM && !empty($result))
618 {
619 $result = self::excludeDeliveryByParent($result);
620 }
621 }
622
623 return $result;
624 }
625
626 protected static function getActiveDeliveryData()
627 {
628 $result = array();
629 self::getEntityItemsFullList(self::ENTITY_TYPE_DELIVERY);
630
631 if(is_array(self::$entityItemsFieldsList[self::ENTITY_TYPE_DELIVERY]))
632 {
633 foreach(self::$entityItemsFieldsList[self::ENTITY_TYPE_DELIVERY] as $fields)
634 {
635 $result[$fields['ID']] = $fields;
636 }
637 }
638
639 return $result;
640 }
641
642 protected static function excludeDeliveryByParent(array $dlvIds)
643 {
644 $result = array();
645 $activeDeliveryData = self::getActiveDeliveryData();
646
647 foreach($dlvIds as $id)
648 {
649 if(intval($activeDeliveryData[$id]['PARENT_ID']) <= 0)
650 $result[] = $id;
651 elseif($activeDeliveryData[$id]['PARENT_CLASS_NAME'] == '\Bitrix\Sale\Delivery\Services\Group')
652 $result[] = $id;
653 elseif(in_array($activeDeliveryData[$id]['PARENT_ID'], $dlvIds))
654 $result[] = $id;
655 }
656
657 return $result;
658 }
659
660 protected static function includeDeliveryByParent(array $dlvIds)
661 {
662 $result = $dlvIds;
663 $unlinkedDlvIds = self::getUnlinkedEnityItems(self::ENTITY_TYPE_DELIVERY);
664
665 foreach(self::getActiveDeliveryData() as $id => $fields)
666 {
667 if(in_array($fields['PARENT_ID'], $dlvIds)) //is profile
668 if(in_array($id, $unlinkedDlvIds) && !in_array($id, $result)) //profile doesn't have own restriction by PS
669 $result[] = $id;
670 }
671
672 return $result;
673 }
674
675 protected static function considerDeliveryParent(array $profilePsIds, $profileId, $parentId, $preparedData)
676 {
677 if(intval($parentId) <= 0)
678 return $profilePsIds;
679
680 $result = $profilePsIds;
681 $unlinkedIds = self::getUnlinkedEnityItems(self::ENTITY_TYPE_DELIVERY);
682 $parentPSIds = self::getLinks($parentId, self::ENTITY_TYPE_DELIVERY, $preparedData);
683
684 if(!in_array($parentId, $unlinkedIds) && in_array($profileId, $unlinkedIds))
685 $result = $parentPSIds;
686 elseif(!in_array($parentId, $unlinkedIds) && !in_array($profileId, $unlinkedIds))
687 $result = array_intersect($profilePsIds, $parentPSIds);
688
689 return $result;
690 }
691 protected static function getDeliveryParentId($deliveryId)
692 {
693 $activeData = self::getActiveDeliveryData();
694
695 if(empty($activeData[$deliveryId]))
696 return 0;
697
698 $parentId = intval($activeData[$deliveryId]['PARENT_ID']);
699
700 if($parentId <= 0 || empty($activeData[$parentId]))
701 return 0;
702
703 if($activeData[$parentId]['CLASS_NAME'] == '\Bitrix\Sale\Delivery\Services\Group')
704 return 0;
705
706 return $parentId;
707 }
708
709 protected static function getUnlinkedEnityItems($entityType, $linkDirection = null)
710 {
711 if($entityType != self::ENTITY_TYPE_DELIVERY && $entityType != self::ENTITY_TYPE_PAYSYSTEM)
712 throw new ArgumentOutOfRangeException('entityType');
713
714 if($linkDirection != null)
715 if($linkDirection != self::LINK_DIRECTION_DELIVERY_PAYSYSTEM && $linkDirection != self::LINK_DIRECTION_PAYSYSTEM_DELIVERY)
716 throw new ArgumentOutOfRangeException('linkDirection');
717
718 if(!isset(self::$unLinked[$entityType]))
719 {
720 $entityList = array_flip(self::getEntityItemsFullList($entityType));
721
722 self::$unLinked[$entityType] = array(
723 self::LINK_DIRECTION_DELIVERY_PAYSYSTEM => $entityList,
724 self::LINK_DIRECTION_PAYSYSTEM_DELIVERY => $entityList
725 );
726
727 $glParams = array(
728 'group' => array($entityType, 'LINK_DIRECTION'),
729 'select' => array($entityType, 'LINK_DIRECTION')
730 );
731
732 $res = DeliveryPaySystemTable::getList($glParams);
733
734 while($row = $res->fetch())
735 {
736 if(isset(self::$unLinked[$entityType][$row['LINK_DIRECTION']][$row[$entityType]]))
737 {
738 unset(self::$unLinked[$entityType][$row['LINK_DIRECTION']][$row[$entityType]]);
739 }
740 }
741
742 self::$unLinked[$entityType][self::LINK_DIRECTION_DELIVERY_PAYSYSTEM] = array_keys(self::$unLinked[$entityType][self::LINK_DIRECTION_DELIVERY_PAYSYSTEM]);
743 self::$unLinked[$entityType][self::LINK_DIRECTION_PAYSYSTEM_DELIVERY] = array_keys(self::$unLinked[$entityType][self::LINK_DIRECTION_PAYSYSTEM_DELIVERY]);
744 }
745
746 if($linkDirection == null)
747 {
748 $result = array_intersect(self::$unLinked[$entityType][self::LINK_DIRECTION_DELIVERY_PAYSYSTEM], self::$unLinked[$entityType][self::LINK_DIRECTION_PAYSYSTEM_DELIVERY]);
749 }
750 else
751 {
752 $result = self::$unLinked[$entityType][$linkDirection];
753 }
754
755 return $result;
756 }
757
758 protected static function getEntityItemsFieldsList($entityType)
759 {
760 self::getEntityItemsFullList($entityType);
761 return self::$entityItemsFieldsList[$entityType];
762 }
763
764 protected static function getEntityItemsFullList($entityType)
765 {
766 if(isset(self::$entityItemsFullList[$entityType]))
767 return self::$entityItemsFullList[$entityType];
768
769 self::$entityItemsFullList[$entityType] = array();
770
771 if($entityType == self::ENTITY_TYPE_DELIVERY)
772 {
773 \Bitrix\Sale\Delivery\Services\Manager::getActiveList();
774 $res = Services\Table::getList(array(
775 'filter' => array(
776 'ACTIVE' => 'Y'
777 ),
778 'select' => array('*', 'PARENT_CLASS_NAME' => 'PARENT.CLASS_NAME')
779 ));
780
781 while($dsrv = $res->fetch())
782 {
783 $obj = Services\Manager::createObject($dsrv);
784
785 if ($obj && $obj->canHasChildren()) //groups
786 continue;
787
788 self::$entityItemsFullList[$entityType][] = $dsrv["ID"];
789 self::$entityItemsFieldsList[$entityType][$dsrv["ID"]] = $dsrv;
790 }
791 }
792 else
793 {
794 $dbRes = PaySystem\Manager::getList(array(
795 'filter' => array("ACTIVE" => "Y"),
796 'select' => array("ID")
797 ));
798
799 while($ps = $dbRes->fetch())
800 {
801 self::$entityItemsFullList[$entityType][] = $ps["ID"];
802 self::$entityItemsFieldsList[$entityType][$ps["ID"]] = $ps;
803 }
804 }
805
806 return self::$entityItemsFullList[$entityType];
807 }
808
809 public static function prepareData(array $entityIds, $entityType)
810 {
811 static $preparedData = array();
812
813 if(!isset($preparedData[$entityType]))
814 $preparedData[$entityType] = array();
815
816 if($entityType == self::ENTITY_TYPE_DELIVERY)
817 {
819 $reverseLinkDirection = self::LINK_DIRECTION_PAYSYSTEM_DELIVERY;
820 $reverseEntityType = self::ENTITY_TYPE_PAYSYSTEM;
821 }
822 else
823 {
825 $reverseLinkDirection = self::LINK_DIRECTION_DELIVERY_PAYSYSTEM;
826 $reverseEntityType = self::ENTITY_TYPE_DELIVERY;
827 }
828
829 if(empty($entityIds))
830 $entityIds = self::getEntityItemsFullList($entityType);
831
832 $arrdif = array_diff($entityIds, array_keys($preparedData[$entityType]));
833
834 if(is_array($arrdif) && empty($arrdif))
835 return array_intersect_key($preparedData[$entityType], $entityIds);
836
837 $glParams = array(
838 'filter' => array(
839 "=".$entityType => $arrdif
840 )
841 );
842
843 $res = DeliveryPaySystemTable::getList($glParams);
844
845 foreach($arrdif as $id)
846 {
847 $preparedData[$entityType][$id] = array(
848 "DIRECT" => array(),
849 "REVERSE" => array()
850 );
851 }
852
853 while($rec = $res->fetch())
854 {
855 if($rec["LINK_DIRECTION"] == $linkDirection)
856 $preparedData[$entityType][$rec[$entityType]]["DIRECT"][] = $rec[$reverseEntityType];
857 elseif($rec["LINK_DIRECTION"] == $reverseLinkDirection)
858 $preparedData[$entityType][$rec[$entityType]]["REVERSE"][] = $rec[$reverseEntityType];
859 }
860
861 return $preparedData[$entityType];
862 }
863}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static setLinks($entityId, $entityType, array $linkedIds=array(), $actualizeRestrictions=true)
static insertLinks($entityId, $linkDirection, $entityType, $linkedIds)
static considerDeliveryParent(array $profilePsIds, $profileId, $parentId, $preparedData)
static prepareData(array $entityIds, $entityType)
static getLinks($entityId, $entityType, array $preparedData=array(), $considerParent=true)
static getUnlinkedEnityItems($entityType, $linkDirection=null)