Files
2026-10-01 16:18:32 +03:00

159 lines
6.4 KiB
PHP

<?php
namespace Ixrs\Catalog\Support;
class ElementList
{
public static function getGlobalFilter($iBlockId, $globalFilter, &$offersFilter = [])
{
$pFilter = [];
$af = app()->factory()->iBlock()->getEntityById($iBlockId);
$iBlockFactory = app()->factory()->main()->getIBlockFactory();
$iBlockEntity = $iBlockFactory->getEntityById($iBlockId);
$propertyFactory = $af->getPropertyFactory();
if($iBlockEntity->getCatalogEntity()->hasOffers()){
$offerAf = app()->factory()->iBlock()->getEntityById($iBlockEntity->getCatalogEntity()->getOfferBlockId());
$offerPropertyFactory = $offerAf->getPropertyFactory();
foreach ($globalFilter as $key => $values){
$res = \Ixrs\Catalog\Support\IBlock::getOperationFilter($key);
// $res = \CIBlock::MkOperationFilter($key);
if(!$res) {
continue;
}
$propertyId = $res['ID'];
$propertyEntity = $propertyFactory->getEntityById($propertyId);
if(!$propertyEntity){
continue;
}
$offerPropertyEntity = $offerPropertyFactory->getCollection([
'=CODE' => $propertyEntity->getCode(),
'=XML_ID' => $propertyEntity->getXmlId(),
'=PROPERTY_TYPE' => $propertyEntity->getBaseType(),
'=USER_TYPE' => $propertyEntity->getUserType(),
'=ACTIVE' => 'Y',
])->first();
if(!$offerPropertyEntity){
continue;
}
/**
* @var array $values
*/
$oKey = $res['PREFIX'] . 'PROPERTY_' . $offerPropertyEntity->getId();
$values = (array)$values;
$pFilter[$propertyId]['E'] = [];
$pFilter[$propertyId]['O'] = [];
foreach ($values as $value){
$oValue = $value;
if($propertyEntity->getBaseType() === 'L' && $propertyEntity->getValueFactory()){
$pve = $propertyEntity->getValueFactory()->getEntityById($oValue);
$x = $pve->get('XML_ID');
if($x){
$opve = $offerPropertyEntity->getValueFactory()->getCollection(['XML_ID' => $x])->first();
$oValue = $opve?->getId();
}
else {
$oValue = null;
}
}
if($oValue){
if(isset($globalFilter[$key])){
$globalFilter[$key] = collect($globalFilter[$key])->deleteValues([$value])->toArray();
}
$pFilter[$propertyId]['E'][$key][] = $value;
$pFilter[$propertyId]['O'][$oKey][] = $oValue;
if(isset($globalFilter['OFFERS'][$oKey])){
$globalFilter['OFFERS'][$oKey] =
collect($globalFilter['OFFERS'][$oKey])->deleteValues([$oValue])->toArray();
}
}
}
//if($pFilter[$propertyId]['E'][$key] || $pFilter[$propertyId]['O'][$oKey]){
if($globalFilter[$key]){
$pFilter[$propertyId]['E'][$key] = array_merge((array)$pFilter[$propertyId]['E'][$key], $globalFilter[$key]);
unset($globalFilter[$key]);
}
if($globalFilter['OFFERS'][$oKey]){
$pFilter[$propertyId]['O'][$oKey] = array_merge((array)$pFilter[$propertyId]['O'][$oKey], $globalFilter['OFFERS'][$oKey]);
unset($globalFilter['OFFERS'][$oKey]);
}
//}
if(!$globalFilter[$key]){
unset($globalFilter[$key]);
}
if(!$globalFilter['OFFERS'][$oKey]){
unset($globalFilter['OFFERS'][$oKey]);
}
}
}
/** inject */
$oFilter = [];
if($pFilter){
foreach ($pFilter as $ePId => $item) {
$data = [];
foreach ($item['E'] as $fKey => $values) {
foreach ($values as $value){
$data[] = [$fKey => $value];
}
}
foreach ($item['O'] as $fKey => $values) {
$oFilter[$fKey] = $values;
foreach ($values as $value){
$data[] = ['=ID' => \CIBlockElement::SubQuery(
'PROPERTY_'.$iBlockEntity->getCatalogEntity()->getSkuPropertyId(),
array_merge([
'IBLOCK_ID' => $iBlockEntity->getCatalogEntity()->getOfferBlockId(),
'ACTIVE_DATE' => 'Y',
'ACTIVE' => 'Y',
], [$fKey => $value])
)];
}
}
if($data){
$globalFilter[] = array_merge([
'LOGIC' => 'OR',
], $data);
}
}
//if(!is_array($this->storage['SUB_FILTER'])){
// $this->storage['SUB_FILTER'] = [];
//}
//foreach ($oFilter as $fKey => $values) {
// if(isset($this->storage['SUB_FILTER'][$fKey])){
// $this->storage['SUB_FILTER'][$fKey] =
// collect((array)$this->storage['SUB_FILTER'][$fKey])->merge($values)->unique()->toArray();
// }
// else {
// $this->storage['SUB_FILTER'][$fKey] = $values;
// }
//}
$offersFilter = is_array($globalFilter['OFFERS'])?$globalFilter['OFFERS']:[];
foreach ($oFilter as $fKey => $values) {
if(isset($offersFilter[$fKey])){
$offersFilter[$fKey] =
collect((array)$offersFilter[$fKey])->merge($values)->unique()->toArray();
}
else {
$offersFilter[$fKey] = $values;
}
}
}
return $globalFilter;
}
}