Your IP : 3.142.172.255


Current Path : /home/bitrix/ext_www/klimatlend.ua/local/lib/Epages/IBlock/
Upload File :
Current File : /home/bitrix/ext_www/klimatlend.ua/local/lib/Epages/IBlock/ElementHelper.php

<?php

namespace Epages\IBlock;

use CIBlockElement;
use CIBlockSection;
use CModule;
use CCatalogProduct;

CModule::IncludeModule('iblock');
CModule::IncludeModule('catalog');

class ElementHelper
{
    const SERVICES_IBLOCK_ID = 32;
    const CATALOG_IBLOCK_ID = 22;
    const PROPERTY_SERVICES_CODE = 'SERVICES';

    /**
     * Получение дополнительных услуг по товару
     *
     * @param $arItemsId - id товаров
     * @return array - id услуг
     */
    public function getServices($arItemsId)
    {
        $arData = $this->getServicesId($arItemsId);
        $arServicesId = $arData['SERVICES_ID'];
        $arSectionServicesId = $this->getServicesFromSection($arData['EMPTY_SERRVICES']);
        if (!empty($arSectionServicesId)) {
            $arServicesId = array_unique(array_merge($arServicesId, $arSectionServicesId));
        }

        return $this->getServiceInformation($arServicesId);
    }

    /**
     * Получение id дополнительных услуг
     *
     * @param $arItemsId - id товаров
     * @return array - id услуг
     */
    public function getServicesId($arItemsId)
    {
        $arOrderEl = array('SORT' => 'ASC');
        $arFilterEl = array('IBLOCK_ID' => self::CATALOG_IBLOCK_ID, 'ACTIVE' => 'Y', 'ID' => $arItemsId);
        $arSelectFieldsEl = array('ID', 'NAME', 'PROPERTY_'.self::PROPERTY_SERVICES_CODE);
        $rsElements = CIBlockElement::GetList(
            $arOrderEl,
            $arFilterEl,
            array('PROPERTY_'.self::PROPERTY_SERVICES_CODE, 'ID'),
            false,
            $arSelectFieldsEl
        );

        $arServicesId = array();
        $arEmptyServices = array();
        while ($arElement = $rsElements->GetNext()) {
            if (!empty($arElement['PROPERTY_'.self::PROPERTY_SERVICES_CODE.'_VALUE'])) {
                $arServicesId[] = $arElement['PROPERTY_'.self::PROPERTY_SERVICES_CODE.'_VALUE'];
            } else {
                $arEmptyServices[] = $arElement['ID'];
            }
        }
        $arData['SERVICES_ID'] = $arServicesId;
        $arData['EMPTY_SERRVICES'] = $arEmptyServices;

        return $arData;
    }

    public function getServiceInformation($arServicesId)
    {
        global $USER;

        $arOrderEl = array('SORT' => 'ASC');
        $arFilterEl = array('IBLOCK_ID' => self::SERVICES_IBLOCK_ID, 'ACTIVE' => 'Y', 'ID' => $arServicesId);
        $arSelectFieldsEl = array('ID', 'NAME');
        $rsElements = CIBlockElement::GetList(
            $arOrderEl,
            $arFilterEl,
            false,
            false,
            $arSelectFieldsEl
        );

        $arServices = array();
        $totalPrice = 0;
        while ($arElement = $rsElements->GetNext()) {
            $arPrice = CCatalogProduct::GetOptimalPrice($arElement['ID'], 1, $USER->GetUserGroupArray(), 'N');
            $arElement['PRICE'] = $arPrice['RESULT_PRICE'];
            $arElement['PRICE']['DISCOUNT_PRICE_FORMATED'] = CurrencyFormat(
                $arElement['PRICE']['DISCOUNT_PRICE'],
                $arElement['PRICE']['CURRENCY']
            );
            $totalPrice += $arElement['PRICE']['DISCOUNT_PRICE'];

            $arServices['ITEMS'][] = $arElement;
        }

        $arServices['TOTAL_PRICE'] = $totalPrice;
        $arServices['TOTAL_PRICE_FORMATED'] = CurrencyFormat(
            $totalPrice,
            'UAH'
        );

        return $arServices;
    }

    // Получение списка услуг из разделов
    public function getServicesFromSection($arItemsId)
    {
        $arOrderEl = array('SORT' => 'ASC');
        $arFilterEl = array('IBLOCK_ID' => self::CATALOG_IBLOCK_ID, 'ID' => $arItemsId);
        $arSelectFieldsEl = array('NAME', 'IBLOCK_SECTION_ID', 'ID');
        $rsElements = CIBlockElement::GetList(
            $arOrderEl,
            $arFilterEl,
            false,
            false,
            $arSelectFieldsEl
        );

        while ($arElement = $rsElements->GetNext()) {
            $resSection = CIBlockSection::GetNavChain(
                false,
                $arElement['IBLOCK_SECTION_ID'],
                array('ID', 'NAME')
            );

            $arSections = array();
            while ($arSection = $resSection->GetNext()) {
                $arSortSect = array('SORT' => 'ASC');
                $arFilterSect = array('IBLOCK_ID' => self::CATALOG_IBLOCK_ID, 'ID' => $arSection['ID']);
                $rsSections = CIBlockSection::GetList(
                    $arSortSect,
                    $arFilterSect,
                    false,
                    array('ID', 'NAME', 'UF_' . self::PROPERTY_SERVICES_CODE));
                while ($arSection = $rsSections->GetNext()) {
                    $arSections[] = $arSection;
                }
            }

            $arSections = array_reverse($arSections);

            foreach ($arSections as $arSect) {
                if (!empty($arSect['UF_SERVICES'][0])) {
                    $arServicesId = $arSect['UF_SERVICES'];
                    break;
                }
            }
        }

        return $arServicesId;
    }
}