Skip to content

Instantly share code, notes, and snippets.

@EscApp2
Created April 19, 2024 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EscApp2/726c1bd06667299abedc0637cbb9508b to your computer and use it in GitHub Desktop.
Save EscApp2/726c1bd06667299abedc0637cbb9508b to your computer and use it in GitHub Desktop.
union element
<?php
class makeIblockElementUnion{
static function GetListFilter($IBLOCK_ID, $arFilter, $arUnionBy, $limit, $arInputSort = array()){
\Bitrix\Main\Loader::includeModule('iblock');
$CIBlockElement = \Bitrix\Iblock\Iblock::wakeUp($IBLOCK_ID)->getEntityDataClass();
if($CIBlockElement){
if(empty($arInputSort)){
$arInputSort["ID"] = "ASC";
}
$shortSelect = array('ID', 'IBLOCK_ID', "NAME", "IBLOCK_SECTION_ID");
foreach ($arInputSort as $index => $value)
{
if (!in_array($index, $shortSelect))
{
if(stripos($index,'PROPERTY_')!==false && stripos($index,'_PROPERTY_')===false) {
$arPropertyKey = explode('PROPERTY_', $index);
$arPropertyKey = array_values($arPropertyKey);
$CODE = $arPropertyKey[1];
$shortSelect[] = $CODE;
}else{
$shortSelect[] = $index;
}
}
}
$arSort = self::convertFilterToD7($arInputSort);
//pre($CIBlockElement::getMap());
$obQuery = $CIBlockElement::query();
$onefirst = array_values($arUnionBy)[0];
if(empty($onefirst)){
$onefirst = array();
}
self::__addD7Filter(array_merge($arFilter,$onefirst), $obQuery);
$obQuery->setSelect($shortSelect);
$obQuery->setOrder($arSort);
$obQuery->setLimit($limit);
$i = 0;
foreach($arUnionBy as $one){
$i++;
if($i == 1){
//skip first
continue;
}
$defObQuery = $CIBlockElement::query();
$defObQuery->setSelect($shortSelect);
$defObQuery->setOrder($arSort);
$defObQuery->setLimit($limit);
self::__addD7Filter(array_merge($arFilter,$one), $defObQuery);
$obQuery->union($defObQuery);
}
$rsElement = $obQuery->exec();
$arReturn = array();
//$UNION_PAGE_COUNT = $rsElement->getSelectedRowsCount();
while($row = $rsElement->fetch()){
$arReturn[]=$row['ID'];
}
return $arReturn;
}
return array();
}
static function __getRawNavigation($arNavParams, $totalCount = false){
if(is_numeric($arNavParams)){
$_arNavParams = array(
"nPageSize" => $arNavParams,
//"nTopCount" => $arParams["NEWS_COUNT"],
"bDescPageNumbering" => false,
"bShowAll" => false,
);
}else{
$_arNavParams = $arNavParams;
}
$arNavigation = \CDBResult::GetNavParams($_arNavParams, false, 1);
$arNavigation['PAGE_NUMBER'] = isset($_REQUEST['PAGEN_' . $arNavigation['PAGEN']])
&& intval($_REQUEST['PAGEN_' . $arNavigation['PAGEN']])
? intval($_REQUEST['PAGEN_' . $arNavigation['PAGEN']])
: 1;
$arNavigation['LIMIT'] = $_arNavParams["nPageSize"]??$_arNavParams["nTopCount"];
$arNavigation['OFFSET'] = ($arNavigation['PAGE_NUMBER'] - 1) * $arNavigation['LIMIT'];
if(!empty($totalCount)){
$navResult = new \CIBlockResult();
$navResult->NavPageCount = ceil($totalCount / $arNavigation['LIMIT']);
$navResult->NavPageNomer = $arNavigation['PAGE_NUMBER'];
$navResult->NavNum = $arNavigation['PAGEN'];
$navResult->NavPageSize = $arNavigation['LIMIT'];
$navResult->NavRecordCount = $totalCount;
$arNavigation['NAV_RESULT'] = $navResult;
}
return $arNavigation;
}
static function convertFilterToD7($arFilter, &$obQuery=false){
$newFilter = array();
if (!function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle): bool
{
return strlen($needle) === 0 || strpos($haystack, $needle) === 0;
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool
{
return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle;
}
}
if($arFilter['INCLUDE_SUBSECTIONS'] && $obQuery){
//https://dev.1c-bitrix.ru/support/forum/forum6/topic137384/
$INCLUDE_SUBSECTIONS = $arFilter['INCLUDE_SUBSECTIONS'];
unset($arFilter['INCLUDE_SUBSECTIONS']);
$SECTION_ID = $arFilter['SECTION_ID'];
unset($arFilter['SECTION_ID']);
if($INCLUDE_SUBSECTIONS == "Y" && !empty($SECTION_ID)){
$obQuery->registerRuntimeField("LINK",[
"data_type" =>\Bitrix\Iblock\SectionElementTable::class,
'reference' => [
'=this.ID' => 'ref.IBLOCK_ELEMENT_ID',
],
'join_type' => 'inner',
]);
$obQuery->registerRuntimeField("PARENT",[
"data_type" =>\Bitrix\Iblock\SectionTable::class,
'reference' => [
'=this.LINK.IBLOCK_SECTION_ID' => 'ref.ID',
],
'join_type' => 'inner',
]);
$obQuery->registerRuntimeField("ROOT",[
"data_type" =>\Bitrix\Iblock\SectionTable::class,
'reference' => [
'=this.PARENT.IBLOCK_ID' => 'ref.IBLOCK_ID',
'<=ref.LEFT_MARGIN' => 'this.PARENT.LEFT_MARGIN',
'>=ref.RIGHT_MARGIN' => 'this.PARENT.RIGHT_MARGIN',
],
'join_type' => 'inner',
]);
$arFilter['ROOT.ID'] = $SECTION_ID; // Раздел, в котором ищем включая подразделы.
$arFilter['==LINK.ADDITIONAL_PROPERTY_ID'] = NULL;
}
}
$addCatalog = false;
foreach($arFilter as $key=>$val){
if(is_array($val)){
$val = self::convertFilterToD7($val);
}
$catalogField = false;
if(str_starts_with($key,"CATALOG_")){
$addCatalog = true;
$catalogField = true;
}
$now = new \Bitrix\Main\Type\DateTime();
if(stripos($key,'PROPERTY_')!==false && stripos($key,'_PROPERTY_')===false){
$arPropertyKey = explode('PROPERTY_',$key);
$arPropertyKey = array_values($arPropertyKey);
$OPERATION = $arPropertyKey[0];
if($OPERATION == "!"){
$OPERATION = "!=";
}
$CODE = $arPropertyKey[1];
$key = 'SKIP';
if(str_ends_with($CODE,"_VALUE")){
$CODE = str_replace("_VALUE","",$CODE);
$newFilter[$OPERATION.$CODE.".ITEM.VALUE"] = $val;
}else{
$newFilter[$OPERATION.$CODE.".VALUE"] = $val;
}
}
if($catalogField){
$arPropertyKey = explode('CATALOG_',$key);
$arPropertyKey = array_values($arPropertyKey);
$OPERATION = $arPropertyKey[0];
if($OPERATION == "!"){
$OPERATION = "!=";
}
$CODE = $arPropertyKey[1];
$newFilter[$OPERATION."CATALOG.".$CODE] = $val;
$key = 'SKIP';
}
//$key = strval($key);
switch ($key) {
case 'IBLOCK_LID':
$newFilter['IBLOCK.LID'] = $val;
break;
case 'SECTION_ID':
$newFilter['IBLOCK_SECTION.ID'] = $val;
break;
case 'CHECK_PERMISSIONS':
//skip
break;
case 'ACTIVE_DATE':
if($val == "Y"){
$subQuery = array(
array(
"LOGIC"=>"OR",
'<=ACTIVE_FROM'=>$now,
'ACTIVE_FROM'=>false,
),
array(
"LOGIC"=>"OR",
'>=ACTIVE_TO'=>$now,
'ACTIVE_TO'=>false,
),
);
}elseif($val == "N"){
$subQuery = array(
array(
"LOGIC"=>"OR",
'>=ACTIVE_FROM'=>$now,
'<=ACTIVE_TO'=>$now,
)
);
}
$newFilter[] = $subQuery;
break;
case 'SKIP':
//skip
break;
default:
$newFilter[$key] = $val;
}
}
if($addCatalog && $obQuery){
$obQuery->registerRuntimeField("CATALOG",[
"data_type" =>\Bitrix\Catalog\ProductTable::class,
'reference' => [
'=this.ID' => 'ref.ID',
],
'join_type' => 'inner',
]);
}
return $newFilter;
}
static function __addD7Filter($arOldFilter, &$obQuery){
$arD7Filter = self::convertFilterToD7($arOldFilter, $obQuery);
$arSetFilter = $arAddFilter = array();
foreach($arD7Filter as $key=>$val){
if(is_numeric($key)){
$arAddFilter[$key] = $val;
}else{
$arSetFilter[$key] = $val;
}
}
$obQuery->setFilter($arSetFilter);
foreach($arAddFilter as $key=>$val){
$obQuery->addFilter((int)$key, $val);
}
return (int)$key;
}
}
<?
$arElements = makeIblockElementUnion::GetListFilter(1, array(
'ACTIVE' => 'Y',
"INCLUDE_SUBSECTIONS"=>"Y",
'!PROPERTY_ALLOW'=> 'Y',
'!PROPERTY_ON_ROAD'=> 'Y',
//'!PROPERTY_PROFITABLE_VALUE'=> 'Да',
'CATALOG_TYPE'=>1,
), array(
array("SECTION_ID"=>8),//гидравлические тележки
array("SECTION_ID"=>9),//электрические тележки,
array("SECTION_ID"=>10),// Электрические штабелеры
array("SECTION_ID"=>54),//ножничные подъемники,
array("SECTION_ID"=>579),//поломоечные машины,
array("SECTION_ID"=>117),//захваты
array("SECTION_ID"=>113),//удлиниители вил
array("SECTION_ID"=>949),//акб для складской техники
),1, array('PROPERTY_SORT_CAT'=>"DESC",'PROPERTY_MINIMUM_PRICE'=>'ASC'));
if(count($arElements)>0){
$GLOBALS['arrProfitableFilter'] = array('ID'=>$arElements);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment