Your IP : 18.118.7.166


Current Path : /home/bitrix/ext_www/klimatlend.ua/local/lib/Epages/Modules/Google/
Upload File :
Current File : /home/bitrix/ext_www/klimatlend.ua/local/lib/Epages/Modules/Google/ProductsModel.php

<?php

namespace Epages\Modules\Google;

use Epages\Modules\Google\Interfaces\FeedProfileInterface;
use Bitrix\Main\Loader;

class ProductsModel {
    protected $iblock;
    protected $price;
    protected $additionalSelect;
    protected $additionalFilter;

    protected $defaultSelect = [
        'ID',
        'IBLOCK_ID',
        'NAME',
        'PREVIEW_PICTURE',
        'PREVIEW_TEXT',
        'DETAIL_PICTURE',
        'DETAIL_TEXT',
        'DETAIL_PAGE_URL',
        'QUANTITY',
    ];

    public function __construct(FeedProfileInterface $catalogData)
    {
        $this->domain = $catalogData->getLink();
        $this->iblock = $catalogData->getCatalogIblock();
        $this->price = $catalogData->getCatalogPrice();
        $this->additionalSelect = $catalogData->getAdditionalSelectFields();
        $this->additionalFilter = $catalogData->getAdditionalFilter();

    }

    public function getProducts() {
        $products = $this->getProductsData();

        $properties = $this->getProperties();

        foreach ($products as $productKey => $product) {
            foreach ($product as $productField => $productFieldValue) {
                if (is_array($productFieldValue)) { // Props
                    $products[$productKey][$productField] = $this->propertyValuesFormat($productField, $productFieldValue['VALUE'], $properties);
                } else { // Fields
                    $products[$productKey][$productField] = $this->propertyValuesFormat($productField, $productFieldValue, $properties);
                }
            }
        }
        
        return $products;

    }

    public function getProductsData() {
        Loader::includeModule('iblock');

        $select = $this->defaultSelect;

        $select[] = 'CATALOG_GROUP_'.$this->price;
        $select[] = 'CATALOG_CURRENCY_'.$this->price;

        if (!empty($this->additionalSelect)) {
            foreach ($this->additionalSelect as $selectField) {
                $select[] = $selectField;
            }
        }
        $select[] = 'PROPERTY_*';

        $filter = ['IBLOCK_ID' => $this->iblock, 'ACTIVE' => 'Y'];

        if (!empty($this->additionalFilter)) {
            foreach ($this->additionalFilter as $filterKey => $filterField) {
                $filter[$filterKey] = $filterField;
            }
        }

        $productsQuery = \CIBlockElement::GetList(
            ['SORT' => 'ASC', 'ID' => 'DESC'],
            $filter,
            false,
            false,
            $select
        );


		$result = [];

        while ($product = $productsQuery->GetNextElement()) {

            $fields = $product->GetFields();
            $properties = $product->GetProperties();

            $fields['CATALOG_PRICE_'.$this->price] = $fields['CATALOG_PRICE_'.$this->price].' '.$fields['CATALOG_CURRENCY_'.$this->price];

            $result[] = array_merge($fields, $properties);

    }

        return $result;
    }

    public function getProperties() {
        Loader::includeModule('iblock');

        $propertiesQuery = \CIBlockProperty::GetList([], ['IBLOCK_ID' => $this->iblock]);

        while ($property = $propertiesQuery->GetNext(true, false)) {
            $availableProperties[$property['CODE']] = $property;
        }

        return $availableProperties;
    }

    public function propertyValuesFormat($propertyKey, $propertyValues, $properties) {
        $propertyCode = str_replace(['PROPERTY_', '_VALUE'], '', $propertyKey);
        if (array_key_exists($propertyCode, $properties)) {
            $currentProperty = $properties[$propertyCode];

            switch ($currentProperty['PROPERTY_TYPE']) {
                case 'F':
                    if (is_array($propertyValues)) {
                        $result = [];

                        foreach ($propertyValues as $propertyValue) {
                            $path = \CFile::GetPath($propertyValue);
                            $result[] = (!empty($path)) ? $this->domain.$path : null;
                        }

                        return $result;
                    } else {
                        $path = \CFile::GetPath($propertyValues);
                        return (!empty($path)) ? $this->domain.$path : null;
                    }
                    break;

                default:
                    return $propertyValues;
                    break;
            }
        } else {

            if (strpos($propertyKey, '_PICTURE') !== false) {
                $path = \CFile::GetPath($propertyValues);
                $propertyValues = (!empty($path)) ? $this->domain.$path : null;
            }

            if (strpos($propertyKey, 'DETAIL_PAGE_URL') !== false) {
                $propertyValues = $this->domain.$propertyValues;
            }

            return $propertyValues;
        }
    }
}