33 #####################################
35 #####################################
58 return 'LOCATION_TYPE';
110 $map = static::getMap();
111 $locLinkFld = static::getLocationLinkField();
113 if(isset($map[$locLinkFld]))
116 Assert::announceNotImplemented(
Loc::getMessage(
'SALE_LOCATION_CONNECTOR_ENTITY_LINK_FLD_NOT_FOUND_EXCEPTION'));
126 return static::getConnectType() == static::LINK_CODE;
129 #####################################
131 #####################################
140 public static function add(array $data)
142 $res = parent::add($data);
143 if($res->isSuccess())
144 static::setLinkUsage($data[static::getLinkField()], $data[static::getTypeField()],
true);
146 $GLOBALS[
'CACHE_MANAGER']->ClearByTag(
'sale-location-data');
147 static::onAfterModifiy();
160 public static function update($primary, array $data)
162 $linkFld = static::getLinkField();
164 if($primary && isset($data[$linkFld]))
165 $link = static::getByPrimary($primary)->fetch();
167 $res = parent::update($primary, $data);
168 if($res->isSuccess() && isset($data[$linkFld]) && $data[$linkFld] != $link[$linkFld])
170 static::resetLinkUsage($link[static::getLinkField()]);
171 static::setLinkUsage($data[$linkFld], $data[static::getTypeField()],
true);
174 $GLOBALS[
'CACHE_MANAGER']->ClearByTag(
'sale-location-data');
175 static::onAfterModifiy();
187 public static function delete($primary)
190 $link = static::getByPrimary($primary)->fetch();
192 $res = parent::delete($primary);
193 if($res->isSuccess())
194 static::resetLinkUsage($link[static::getLinkField()]);
196 $GLOBALS[
'CACHE_MANAGER']->ClearByTag(
'sale-location-data');
197 static::onAfterModifiy();
222 public static function updateMultipleForOwner($entityPrimary, $links = array(), $behaviour = array(
'REMOVE_ABSENT' =>
true))
224 $entityPrimary = Assert::expectStringNotNull($entityPrimary,
'$entityPrimary');
225 $links = static::checkUpdateLinks($links);
227 $updateLocations = is_array($links[static::DB_LOCATION_FLAG]);
228 $updateGroups = is_array($links[static::DB_GROUP_FLAG]) && static::getUseGroups();
231 $existed = static::getLinkedLocations($entityPrimary);
234 static::updateMultipleLinkType($entityPrimary, $links[static::DB_LOCATION_FLAG], $existed[static::DB_LOCATION_FLAG], static::DB_LOCATION_FLAG, $behaviour[
'REMOVE_ABSENT']);
237 static::updateMultipleLinkType($entityPrimary, $links[static::DB_GROUP_FLAG], $existed[static::DB_GROUP_FLAG], static::DB_GROUP_FLAG, $behaviour[
'REMOVE_ABSENT']);
239 static::resetLinkUsage($entityPrimary);
241 $GLOBALS[
'CACHE_MANAGER']->ClearByTag(
'sale-location-data');
242 static::onAfterModifiy();
248 public static function deleteAllForOwner($entityPrimary, $behaviour = array(
'BATCH_MODE' =>
false))
250 $entityPrimary = Assert::expectStringNotNull($entityPrimary,
'$entityPrimary');
252 if($behaviour[
'BATCH_MODE'])
255 $dbConnection = Main\HttpApplication::getConnection();
256 $typeField = static::getTypeField();
261 '.static::getTableName().
'
263 '.static::getLinkField().
' = \''.$dbConnection->getSqlHelper()->forSql($entityPrimary).
'\'';
268 '.$typeField.
' = \''.static::DB_LOCATION_FLAG.
'\'
269 OR
'.$typeField.' = \
''.static::DB_GROUP_FLAG.
'\'
273 $dbConnection->query($sql);
277 // get existed relations
278 $existed = static::getLinkedLocations($entityPrimary);
281 !empty($existed[static::DB_LOCATION_FLAG])
282 && is_array($existed[static::DB_LOCATION_FLAG])
285 static::updateMultipleLinkType(
288 $existed[static::DB_LOCATION_FLAG],
289 static::DB_LOCATION_FLAG,
295 static::getUseGroups()
296 && !empty($existed[static::DB_GROUP_FLAG])
297 && is_array($existed[static::DB_GROUP_FLAG])
300 static::updateMultipleLinkType(
303 $existed[static::DB_GROUP_FLAG],
304 static::DB_GROUP_FLAG,
310 static::setLinkUsage($entityPrimary, static::DB_LOCATION_FLAG, false);
311 static::setLinkUsage($entityPrimary, static::DB_GROUP_FLAG, false);
313 $GLOBALS['CACHE_MANAGER
']->ClearByTag('sale-location-data
');
314 static::onAfterModifiy();
317 public static function deleteAll()
319 $dbConnection = Main\HttpApplication::getConnection();
320 $dbConnection->query('truncate table
'.static::getTableName());
322 static::deleteLinkUsageOption();
324 $GLOBALS['CACHE_MANAGER
']->ClearByTag('sale-location-data
');
325 static::onAfterModifiy();
332 public static function resetMultipleForOwner($entityPrimary, $links = array())
334 $entityPrimary = Assert::expectStringNotNull($entityPrimary, '$entityPrimary
');
335 $links = static::checkUpdateLinks($links);
337 static::deleteAllForOwner($entityPrimary, array('BATCH_MODE
' => true));
339 $map = static::getMap();
340 $linkFld = static::getLinkField();
341 $locationLinkFld = static::getLocationLinkField();
342 $typeFld = static::getTypeField();
345 $linkFld => $map[$linkFld], // DELIVERY_ID, SITE_ID, smth else
346 $locationLinkFld => $map[$locationLinkFld] // LOCATION_ID or LOCATION_CODE or smth else
348 if($useGroups = static::getUseGroups())
349 $fields[$typeFld] = $map[$typeFld]; // LOCATION_TYPE: L or G
351 $inserter = new BlockInserter(array(
352 'tableName
' => static::getTableName(),
353 'exactFields
' => $fields
358 !empty($links[static::DB_LOCATION_FLAG])
359 && is_array($links[static::DB_LOCATION_FLAG])
362 foreach($links[static::DB_LOCATION_FLAG] as $id)
365 $linkFld => $entityPrimary,
366 $locationLinkFld => $id,
369 $data[$typeFld] = static::DB_LOCATION_FLAG;
371 $inserter->insert($data);
375 static::setLinkUsage($entityPrimary, static::DB_LOCATION_FLAG, $smthAdded);
379 !empty($links[static::DB_GROUP_FLAG])
380 && is_array($links[static::DB_GROUP_FLAG])
384 foreach($links[static::DB_GROUP_FLAG] as $id)
387 $linkFld => $entityPrimary,
388 $locationLinkFld => $id,
391 $data[$typeFld] = static::DB_GROUP_FLAG;
393 $inserter->insert($data);
397 static::setLinkUsage($entityPrimary, static::DB_GROUP_FLAG, $smthAdded);
401 $GLOBALS['CACHE_MANAGER
']->ClearByTag('sale-location-data
');
404 private static function updateMultipleLinkType($entityPrimary, $links, $existed, $linkType = self::DB_LOCATION_FLAG, $doRemove = true)
406 $useGroups = static::getUseGroups();
409 foreach($links as $k => $loc)
411 if(!isset($existed[$loc]))
414 static::getLinkField() => $entityPrimary,
415 static::getLocationLinkField() => $loc
418 $data[static::getTypeField()] = $linkType;
420 $res = static::add($data);
421 if(!$res->isSuccess())
422 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_CONNECTOR_ENTITY_CANNOT_ADD_EXCEPTION
'));
427 unset($existed[$loc]);
430 if($doRemove && !empty($existed))
432 foreach($existed as $loc => $k)
435 static::getLinkField() => $entityPrimary,
436 static::getLocationLinkField() => $loc
439 $data[static::getTypeField()] = $linkType;
441 $res = static::delete($data);
442 if(!$res->isSuccess())
443 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_CONNECTOR_ENTITY_CANNOT_DELETE_EXCEPTION
'));
448 #####################################
450 #####################################
467 public static function getConnectedLocationsQuery($entityPrimary, $parameters = array(), $behaviour = array('GET_LINKED_THROUGH_GROUPS
' => false))
469 $entityPrimary = Assert::expectStringNotNull($entityPrimary, '$entityPrimary
');
471 if(!is_array($parameters))
472 $parameters = array();
473 if(!is_array($behaviour))
474 $behaviour = array();
475 if(!isset($behaviour['GET_LINKED_THROUGH_GROUPS
']))
476 $behaviour['GET_LINKED_THROUGH_GROUPS
'] = false;
478 $useGroups = GroupTable::checkGroupUsage() && static::getUseGroups(); // check if we have groups in project and entity uses groups
479 $getLinkedThroughGroups = $behaviour['GET_LINKED_THROUGH_GROUPS
'];
481 $getLinkedThroughGroups = false;
483 $connType = static::getConnectType();
487 if (empty($parameters['select
']) || !is_array($parameters['select
']))
489 $select = ['' => 'LOCATION
'];
493 foreach($parameters['select
'] as $k => $v)
497 $select[''] = 'LOCATION
';
501 if(is_numeric($k) && mb_strpos((string)$v, '.
') === false) // is NOT a reference
506 $select[$k] = 'LOCATION.
' . $v;
513 if (!empty($parameters['filter
']) && is_array($parameters['filter
']))
515 foreach($parameters['filter
'] as $k => $v)
517 $filter['LOCATION.
' . $k] = $v;
523 if (!empty($parameters['order
']) && is_array($parameters['order
']))
525 //if($getLinkedThroughGroups)
526 Assert::announceNotImplemented('Sorry, order-over-
union clause is not implemented currently.
');
528 foreach($parameters['order
'] as $k => $v)
530 $order['LOCATION.
' . $k] = $v;
534 if (!empty($parameters['runtime
']) && is_array($parameters['runtime
']))
536 Assert::announceNotImplemented('Sorry, runtime clause is not implemented currently.
');
541 if(static::checkLinkUsage($entityPrimary, static::DB_LOCATION_FLAG))
543 $strictFilter = array_merge($filter, array(
544 '=
'.static::getLinkField() => $entityPrimary
548 $strictFilter['=
'.static::getTypeField()] = static::DB_LOCATION_FLAG;
550 $query = new Entity\Query(self::getEntity());
553 ->setFilter($strictFilter)
556 $sqls[] = $query->getQuery();
559 if(static::checkLinkUsage($entityPrimary, static::DB_GROUP_FLAG) && $getLinkedThroughGroups)
561 $query = new Entity\Query(static::getEntity());
563 if($connType == self::LINK_CODE) // entity connected by CODE
566 ->registerRuntimeField(
570 'reference
' => array(
571 '=
this.
'.static::getLocationLinkField() => 'ref.CODE
',
572 '=
this.
'.static::getTypeField() => array('?
', static::DB_GROUP_FLAG)
574 'join_type
' => 'inner
'
577 ->registerRuntimeField(
581 'reference
' => array(
582 '=
this.G.ID
' => 'ref.
'.GroupLocationTable::getLinkField(),
584 'join_type
' => 'inner
'
587 ->registerRuntimeField(
591 'reference
' => array(
592 '=
this.LG.LOCATION_ID
' => 'ref.ID
'
594 'join_type
' => 'inner
'
598 ->setFilter(array_merge($filter, array(
599 '=
'.static::getLinkField() => $entityPrimary
605 from b_sale_delivery2location DL
606 inner join b_sale_location_group G on (G.CODE = DL.LOCATION_CODE and DL.LOCATION_TYPE = static::DB_GROUP_FLAG)
607 inner join b_sale_location2location_group LG on (LG.LOCATION_GROUP_ID = G.ID)
608 inner join b_sale_location LOCATION on (LG.LOCATION_ID = LOCATION.ID)
613 else // entity connected by ID
616 ->registerRuntimeField(
620 'reference
' => array(
621 '=
this.
'.static::getLocationLinkField() => 'ref.
'.GroupLocationTable::getLinkField(),
622 '=
this.
'.static::getTypeField() => array('?
', static::DB_GROUP_FLAG)
624 'join_type
' => 'inner
'
627 ->registerRuntimeField(
631 'reference
' => array(
632 '=
this.LG.LOCATION_ID
' => 'ref.ID
'
634 'join_type
' => 'inner
'
638 ->setFilter(array_merge($filter, array(
639 '=
'.static::getLinkField() => $entityPrimary
645 from b_sale_delivery2location DL
646 inner join b_sale_location2location_group LG on (LG.LOCATION_GROUP_ID = DL.LOCATION_ID and DL.LOCATION_TYPE = static::DB_GROUP_FLAG)
647 inner join b_sale_location LOCATION on (LG.LOCATION_ID = LOCATION.ID)
653 $sqls[] = $query->getQuery();
656 if(empty($sqls)) // entity is not connected, so it means "all connected"
659 return static::unionize($sqls);
665 public static function getConnectedLocationsSql($entityPrimary, $parameters = array(), $behaviour = array('GET_LINKED_THROUGH_GROUPS
' => false))
667 return static::getConnectedLocationsQuery($entityPrimary, $parameters, $behaviour);
670 public static function getConnectedLocations($entityPrimary, $parameters = array(), $behaviour = array('GET_LINKED_THROUGH_GROUPS
' => false))
672 $query = static::getConnectedLocationsQuery($entityPrimary, $parameters, $behaviour);
675 return new DB\ArrayResult(array());
677 return static::queryPage(
679 $parameters['limit
'] ?? 0,
680 $parameters['offset
'] ?? 0
695 public static function getConnectedGroups($entityPrimary, $parameters = array())
697 $entityPrimary = Assert::expectStringNotNull($entityPrimary, '$entityPrimary
');
699 if(!static::getUseGroups())
700 Assert::announceNotSupported(Loc::getMessage('SALE_LOCATION_CONNECTOR_ENTITY_DOESNT_SUPPORT_GROUPS
'));
702 if(!is_array($parameters))
703 $parameters = array();
705 $parameters['runtime
']['C
'] = array(
706 'data_type
' => static::getEntity()->getFullName(),
707 'reference
' => array(
708 '=ref.
'.static::getLinkField() => array('?
', $entityPrimary),
709 '=ref.
'.static::getTypeField() => array('?
', static::DB_GROUP_FLAG)
711 'join_type
' => 'inner
'
714 $parameters['runtime
']['C
']['reference
']['=ref.
'.static::getLocationLinkField()] = (static::getConnectType() == self::LINK_CODE ? 'this.CODE
' : 'this.ID
');
716 return GroupTable::getList($parameters);
719 // returns list of connected entities for location with ID == $locationPrimary, with an optional filter applied
720 public static function getConnectedEntites($locationPrimary, $parameters = array())
722 return static::getConnectedEntitiesByCondition($locationPrimary, 'id', $parameters);
725 // returns list of connected entities for location with CODE == $locationPrimary, with an optional filter applied
726 public static function getConnectedEntitesByCode($locationPrimary, $parameters = array()) // getConnectedEntitiesByLocationCode
728 return static::getConnectedEntitiesByCondition($locationPrimary, 'code
', $parameters);
731 // returns sql-select query that can be embeded to another sql query in 'ID in (select * from ...)
' manner
732 public static function getConnectedEntitiesQuery($locationPrimary, $linkType = 'id', $parameters = array()) // // getConnectedEntitiesSql
734 if($linkType == 'id')
735 $locationPrimary = Assert::expectIntegerPositive($locationPrimary, '$locationPrimary
');
737 $locationPrimary = Assert::expectStringNotNull($locationPrimary, '$locationPrimary
');
739 $useGroups = GroupTable::checkGroupUsage() && static::getUseGroups(); // check if we have groups in project and entity uses groups
740 $useCodes = static::getUseCodes(); // this entity uses codes
741 $groupUseCodes = GroupLocationTable::getUseCodes(); // group entity uses codes
743 $typeFld = static::getTypeField();/*LOCATION_TYPE*/
744 $linkFld = static::getLinkField();/*DELIVERY_ID*/
745 $locationLinkFld = static::getLocationLinkField();/*LOCATION_ID*/
746 $targetPrimaryFld = static::getTargetEntityPrimaryField();/*ID*/
747 $groupLocationLinkFld = GroupLocationTable::getLocationLinkField();/*LOCATION_ID*/
748 $groupLinkFld = GroupLocationTable::getLinkField();/*LOCATION_GROUP_ID*/
750 $seachById = $linkType == 'id';
752 $dbConnection = Main\HttpApplication::getConnection();
754 if(!is_array($parameters))
755 $parameters = array();
757 if(isset($parameters['runtime
']) && is_array($parameters['runtime
']))
758 Assert::announceNotImplemented('Sorry, runtime clause is not implemented currently.
');
761 if(isset($parameters['order
']) && is_array($parameters['order
']))
762 Assert::announceNotImplemented('Sorry, order-over-
union clause is not implemented currently.');
765 if(!empty($parameters[
'filter']) && is_array($parameters[
'filter']))
766 $filter = $parameters[
'filter'];
768 $select = array(
'*');
769 if(is_array($parameters[
'select']) && !empty($parameters[
'select']))
770 $select = $parameters[
'select'];
781 $query =
new Entity\Query(static::getTargetEntityName());
783 $DLCondition = array(
784 '=this.'.$targetPrimaryFld =>
'ref.'.$linkFld
787 $DLCondition[
'=ref.'.$typeFld] = array(
'?', static::DB_LOCATION_FLAG);
790 ->registerRuntimeField(
793 'data_type' => get_called_class(),
794 'reference' => $DLCondition,
795 'join_type' =>
'inner'
798 ->registerRuntimeField(
801 'data_type' =>
'\Bitrix\Sale\Location\Location',
802 'reference' => array(
803 '=this.DL.'.$locationLinkFld =>
'ref.'.($useCodes ?
'CODE' :
'ID'),
805 'join_type' =>
'inner'
808 ->registerRuntimeField(
811 'data_type' =>
'\Bitrix\Sale\Location\Location',
812 'reference' => array(
813 '=ref.'.($seachById ?
'ID' :
'CODE') => array(
'?', $locationPrimary),
814 '>=ref.LEFT_MARGIN' =>
'this.L1.LEFT_MARGIN',
815 '<=ref.RIGHT_MARGIN' =>
'this.L1.RIGHT_MARGIN'
817 'join_type' =>
'inner'
827 $query->setGroup($select);
829 return $query->getQuery();
833 $sqls = array($query->getQuery());
835 $query =
new Entity\Query(static::getTargetEntityName());
849 ->registerRuntimeField(
852 'data_type' => get_called_class(),
853 'reference' => array(
854 '=this.'.$targetPrimaryFld =>
'ref.'.$linkFld,
855 '=ref.'.$typeFld => array(
'?', static::DB_GROUP_FLAG)
857 'join_type' =>
'inner'
864 ->registerRuntimeField(
867 'data_type' =>
'\Bitrix\Sale\Location\Group',
868 'reference' => array(
869 '=this.DL.'.$locationLinkFld =>
'ref.CODE',
871 'join_type' =>
'inner'
877 ->registerRuntimeField(
880 'data_type' =>
'\Bitrix\Sale\Location\GroupLocation',
881 'reference' => array(
882 ($useCodes ?
'=this.G.ID' :
'=this.DL.'.$locationLinkFld) =>
'ref.'.$groupLinkFld
884 'join_type' =>
'inner'
887 ->registerRuntimeField(
890 'data_type' =>
'\Bitrix\Sale\Location\Location',
891 'reference' => array(
892 '=this.GL.'.$groupLocationLinkFld =>
'ref.'.($groupUseCodes ?
'CODE' :
'ID'),
894 'join_type' =>
'inner'
897 ->registerRuntimeField(
900 'data_type' =>
'\Bitrix\Sale\Location\Location',
901 'reference' => array(
902 '=ref.'.($seachById ?
'ID' :
'CODE') => array(
'?', $locationPrimary),
903 '>=ref.LEFT_MARGIN' =>
'this.L1.LEFT_MARGIN',
904 '<=ref.RIGHT_MARGIN' =>
'this.L1.RIGHT_MARGIN'
906 'join_type' =>
'inner'
913 $sqls[] = $query->getQuery();
915 return static::unionize($sqls);
923 $sql = static::getConnectedEntitiesQuery($locationPrimary, $linkType, $parameters);
924 $res = static::queryPage($sql, $parameters[
'limit'], $parameters[
'offset']);
931 $entityPrimary = Assert::expectStringNotNull($entityPrimary,
'$entityPrimary');
934 $linkFld = static::getLocationLinkField();
935 $typeFld = static::getTypeField();
940 $res = static::getList([
942 static::getLinkField() => $entityPrimary
945 while($item = $res->fetch())
947 if ($item[$typeFld] !== static::DB_GROUP_FLAG && $item[$typeFld] !== static::DB_LOCATION_FLAG)
951 $existed[$item[$typeFld]] ??= [];
952 $existed[$item[$typeFld]][$item[$linkFld]] =
true;
971 $nodeInfo = Assert::expectArray($nodeInfo,
'$nodeInfo');
972 $entityPrimary = Assert::expectStringNotNull($entityPrimary,
'$entityPrimary');
976 if(!static::checkLinkUsageAny($entityPrimary))
978 foreach($nodeInfo as $node)
986 if(!is_array($connectors))
987 $connectors = static::getConnectedLocationsInfo($entityPrimary);
989 foreach($nodeInfo as $node)
991 $node = Assert::expectNotEmptyArray($node,
'$nodeInfo[]');
992 $node[
'ID'] = Assert::expectIntegerPositive($node[
'ID'],
'$nodeInfo[][ID]');
993 $node[
'LEFT_MARGIN'] = Assert::expectIntegerNonNegative($node[
'LEFT_MARGIN'],
'$nodeInfo[][LEFT_MARGIN]');
994 $node[
'RIGHT_MARGIN'] = Assert::expectIntegerPositive($node[
'RIGHT_MARGIN'],
'$nodeInfo[][RIGHT_MARGIN]');
996 $result[$node[
'ID']] =
false;
997 foreach($connectors as $connector)
999 if($connector[
'ID'] == $node[
'ID'])
1004 elseif($node[
'LEFT_MARGIN'] >= $connector[
'LEFT_MARGIN'] && $node[
'RIGHT_MARGIN'] <= $connector[
'RIGHT_MARGIN'])
1009 elseif($node[
'LEFT_MARGIN'] <= $connector[
'LEFT_MARGIN'] && $node[
'RIGHT_MARGIN'] >= $connector[
'RIGHT_MARGIN'])
1016 if(!$result[$node[
'ID']])
1035 public static function checkConnectionExists($entityPrimary, $locationPrimary, array $behaviour = array(
'LOCATION_LINK_TYPE' =>
'ID'))
1037 $entityPrimary = Assert::expectStringNotNull($entityPrimary,
'$entityPrimary');
1038 $locationPrimary = Assert::expectStringNotNull($locationPrimary,
'$locationPrimary');
1040 if(!isset($behaviour[
'LOCATION_LINK_TYPE']))
1042 $behaviour[
'LOCATION_LINK_TYPE'] =
'ID';
1046 $behaviour[
'LOCATION_LINK_TYPE'] = Assert::expectEnumerationMember($behaviour[
'LOCATION_LINK_TYPE'], array(
'AUTO',
'ID',
'CODE'),
'$behaviour[LOCATION_LINK_TYPE]');
1049 if(!static::checkLinkUsageAny($entityPrimary))
1054 if($behaviour[
'LOCATION_LINK_TYPE'] ==
'AUTO')
1056 $field = static::getUseCodes() ?
'CODE' :
'ID';
1060 $field = $behaviour[
'LOCATION_LINK_TYPE'];
1063 $node = LocationTable::getList(
1065 'filter' => array(
'='.$field => $locationPrimary),
1066 'select' => array(
'ID',
'LEFT_MARGIN',
'RIGHT_MARGIN'),
1071 if(!intval($node[
'ID']))
1073 throw new \Bitrix\Sale\Location\Tree\NodeNotFoundException(
false, array(
'INFO' => array($field => $locationPrimary)));
1076 $result = static::getLinkStatusForMultipleNodes(array($node), $entityPrimary);
1086 $res = static::getConnectedLocations($entityPrimary, array(
'select' => array(
1088 'LEFT_MARGIN' =>
'LEFT_MARGIN',
1089 'RIGHT_MARGIN' =>
'RIGHT_MARGIN',
1090 'LNAME' =>
'NAME.NAME',
1092 'filter' => array(
'NAME.LANGUAGE_ID' => LANGUAGE_ID)
1093 ), array(
'GET_LINKED_THROUGH_GROUPS' =>
true));
1095 while($item = $res->fetch())
1101 #####################################
1103 #####################################
1107 $entityPrimary = Assert::expectStringNotNull($entityPrimary,
'$entityPrimary');
1109 return static::checkLinkUsage($entityPrimary, static::DB_LOCATION_FLAG) || static::checkLinkUsage($entityPrimary, static::DB_GROUP_FLAG);
1122 public static function checkLinkUsage($entityPrimary, $linkType = self::DB_LOCATION_FLAG)
1124 $entityPrimary = Assert::expectStringNotNull($entityPrimary,
'$entityPrimary');
1125 $linkType = Assert::expectEnumerationMember(
1127 array(static::DB_LOCATION_FLAG, static::DB_GROUP_FLAG),
1131 if(!static::getUseLinkTracking())
1134 $useGroups = static::getUseGroups();
1136 if(!$useGroups && $linkType == static::DB_GROUP_FLAG)
1139 $usageFlags = static::getLinkUsageOptionValue();
1141 if(isset($usageFlags[$entityPrimary][$linkType]))
1142 return $usageFlags[$entityPrimary][$linkType];
1144 $strictFilter = array(
1145 static::getLinkField() => $entityPrimary
1148 $strictFilter[
'LOCATION_TYPE'] = static::DB_LOCATION_FLAG;
1150 $usageFlags[$entityPrimary][static::DB_LOCATION_FLAG] = !!static::getList(array(
1152 'filter' => $strictFilter
1157 $usageFlags[$entityPrimary][static::DB_GROUP_FLAG] = !!static::getList(array(
1160 static::getLinkField() => $entityPrimary,
1161 'LOCATION_TYPE' => static::DB_GROUP_FLAG
1166 static::setLinkUsageOptionValue($usageFlags);
1168 return $usageFlags[$entityPrimary];
1175 private static function setLinkUsage($entityPrimary, $linkType = self::DB_LOCATION_FLAG, $way =
true)
1177 if(!static::getUseLinkTracking())
1180 if(!in_array($linkType, array(static::DB_LOCATION_FLAG, static::DB_GROUP_FLAG)))
1181 $linkType = static::DB_LOCATION_FLAG;
1183 $usageFlags = static::getLinkUsageOptionValue();
1184 $usageFlags[$entityPrimary][$linkType] = $way;
1186 static::setLinkUsageOptionValue($usageFlags);
1193 private static function resetLinkUsage($entityPrimary)
1195 if(!static::getUseLinkTracking())
1198 $useGroups = static::getUseGroups();
1199 $typeFld = static::getTypeField();
1202 $groupBy[] = $typeFld;
1204 $res = static::getList(array(
1206 static::getLinkField() => $entityPrimary
1211 $hasLocationLink =
false;
1212 $hasGroupLink =
false;
1213 while($item = $res->fetch())
1215 if(isset($item[$typeFld]) && $item[$typeFld] == static::DB_GROUP_FLAG)
1216 $hasGroupLink =
true;
1218 $hasLocationLink =
true;
1221 static::setLinkUsage($entityPrimary, static::DB_LOCATION_FLAG, $hasLocationLink);
1222 static::setLinkUsage($entityPrimary, static::DB_GROUP_FLAG, $hasGroupLink);
1229 private static function getLinkUsageOptionValue()
1231 $usageFlagsOpt = Config\Option::get(
"sale", static::getLinkOptionName());
1232 if(!mb_strlen($usageFlagsOpt) || !is_array($usageFlags = unserialize($usageFlagsOpt, [
'allowed_classes' =>
false])))
1233 $usageFlags = array();
1242 private static function setLinkUsageOptionValue($usageFlags)
1244 Config\Option::set(
"sale", static::getLinkOptionName(), serialize($usageFlags),
'');
1247 private static function deleteLinkUsageOption()
1249 Config\Option::delete(
"sale", array(
'name' => static::getLinkOptionName()));
1252 private static function getLinkOptionName()
1254 return 'link_option_'.static::getTableName();
1257 #####################################
1259 #####################################
1261 private static function checkUpdateLinks($links)
1263 $useCodes = static::getUseCodes();
1264 $useGroups = static::getUseGroups();
1266 $locationArgName =
'$links['.static::DB_LOCATION_FLAG.
']';
1267 $groupArgName =
'$links['.static::DB_GROUP_FLAG.
']';
1269 if (isset($links[static::DB_LOCATION_FLAG]) && is_array($links[static::DB_LOCATION_FLAG]))
1272 $links[static::DB_LOCATION_FLAG] = Assert::expectArrayOfUniqueStringNotNull($links[static::DB_LOCATION_FLAG], $locationArgName);
1274 $links[static::DB_LOCATION_FLAG] = Assert::expectArrayOfUniqueIntegerNotNull($links[static::DB_LOCATION_FLAG], $locationArgName);
1277 if (isset($links[static::DB_GROUP_FLAG]) && is_array($links[static::DB_GROUP_FLAG]))
1280 Assert::announceNotSupported(
Loc::getMessage(
'SALE_LOCATION_CONNECTOR_ENTITY_DOESNT_SUPPORT_GROUPS'));
1283 $links[static::DB_GROUP_FLAG] = Assert::expectArrayOfUniqueStringNotNull($links[static::DB_GROUP_FLAG], $groupArgName);
1285 $links[static::DB_GROUP_FLAG] = Assert::expectArrayOfUniqueIntegerNotNull($links[static::DB_GROUP_FLAG], $groupArgName);
1300 protected static function queryPage($sql, $limit = 0, $offset = 0)
1302 $artificialNav =
false;
1303 $limit = intval($limit);
1304 $offset = intval($offset);
1308 $dbConnection = Main\HttpApplication::getConnection();
1312 if($dbConnection->getType() ==
'mysql')
1314 $sql .=
' limit '.($offset ? $offset.
', ' :
'').$limit;
1318 $artificialNav =
true;
1322 $res = $dbConnection->query($sql);
1329 while($item = $res->fetch())
1336 if($i >= $offset + $limit)
1357 if(!is_array($sqls))
1360 if(count($sqls) == 1)
1361 return array_shift($sqls);
1368 return implode(
' union ', $sqls);
1374 if(empty($denormalizedList))
1377 $useCodes = static::getUseCodes();
1379 $denormalizedList = array_flip($denormalizedList);
1381 $query =
new Entity\Query(
'Bitrix\Sale\Location\Location');
1383 $query->setSelect(array(
1387 'LNAME' =>
'NAME.NAME'
1390 $query->setFilter(array(
'NAME.LANGUAGE_ID' => LANGUAGE_ID));
1391 $query->setOrder(array(
'LEFT_MARGIN' =>
'desc'));
1393 $res = $query->exec();
1397 $relations = array();
1398 $selected = array();
1399 while($item = $res->fetch())
1401 if(!isset($relations[$item[
'ID']][
'name']))
1402 $relations[$item[
'ID']][
'name'] = $item[
'LNAME'];
1404 if(!isset($relations[$item[
'ID']][
'total']))
1405 $relations[$item[
'ID']][
'total'] = 0;
1407 if(!isset($relations[$item[
'ID']][
'selected']))
1408 $relations[$item[
'ID']][
'selected'] = 0;
1411 if($item[
'PARENT_ID'] != 0)
1413 if(!isset($relations[$item[
'PARENT_ID']][
'total']))
1414 $relations[$item[
'PARENT_ID']][
'total'] = 0;
1415 if(!isset($relations[$item[
'PARENT_ID']][
'selected']))
1416 $relations[$item[
'PARENT_ID']][
'selected'] = 0;
1419 $relations[$item[
'PARENT_ID']][
'total'] += (1 + $relations[$item[
'ID']][
'total']);
1421 if(isset($denormalizedList[$item[
'ID']]))
1423 $relations[$item[
'PARENT_ID']][
'selected'] += (1 + $relations[$item[
'ID']][
'selected']);
1424 $selected[$item[
'ID']] = $item;
1429 $removeItems = array();
1430 $removeChildrenOf = array();
1431 foreach($relations as $id => $rel)
1433 if($rel[
'total'] > 0)
1435 if($rel[
'total'] == $rel[
'selected'])
1437 if(isset($selected[$id]))
1438 $removeChildrenOf[$id] =
true;
1440 elseif($rel[
'selected'] > 0)
1441 $removeItems[$id] =
true;
1445 $normalized = array();
1446 foreach($selected as $id => $item)
1448 if(!($item[
'PARENT_ID'] && $removeChildrenOf[$item[
'PARENT_ID']]) && !$removeItems[$item[
'ID']])
1449 $normalized[] = $item[$useCodes ?
'CODE' :
'ID'];
static getMessage($code, $replace=null, $language=null)
$GLOBALS['____1444769544']