Your IP : 3.145.72.55


Current Path : /var/www/u0635749/data/www/hobbyclick.ru/src/Metods/
Upload File :
Current File : /var/www/u0635749/data/www/hobbyclick.ru/src/Metods/ConnectInIblock.php

<?php

namespace Tequila\Metods;

use Tequila\Models\LinkConnectionTable;
use Tequila\Metods\OrmMetod;
use Bitrix\Main\Loader;
use Bitrix\Main\Page\Asset;
use Bitrix\Main\Application;

class ConnectInIblock
{
    function __construct  ()
    {
        if (!Loader::includeModule('catalog') && !Loader::includeModule('iblock')){
            throw new \Bitrix\Main\SystemException('Необходимо установить модуль catalog');
        } 
        
        $this->writeToTable();
    }
    
    public function getOffer ($iblockId) {
       
        $arSelect = [ 
            "ID",
            "IBLOCK_ID",
            "NAME",
            "PROPERTY_CML2_LINK",
            "PROPERTY_hall",
            "PROPERTY_prepod",
            "PROPERTY_AGE",
            "PROPERTY_daytime",
            "PROPERTY_level",
            "CATALOG_PRICE_1"
        ];

        $arFilter = [
            "IBLOCK_ID"=>$iblockId, 
        ];

        $res = \CIBlockElement::GetList(["SORT"=>"ASC"], $arFilter, false, false, $arSelect);
        while($ob = $res->GetNextElement()) {
            $arFields = $ob->GetFields(); 
            $offer[$arFields['ID']] = $arFields; 
        }

        return $offer;
    }
 

    public function getProduct ($iblockId) {
        $arSelect = [ 
            "ID",
            "NAME",
            "IBLOCK_ID",
        ];

        $arFilter = [
            "IBLOCK_ID"=>$iblockId, 
        ];

        $res = \CIBlockElement::GetList(["SORT"=>"ASC"], $arFilter, false, false, $arSelect);
        while($ob = $res->GetNextElement()) {
            $arFields = $ob->GetFields(); 
            $property = $ob->GetProperties();
            $props = ['TREND', 'FILTER_MOTIV', 'FILTER_STRESS', 'FILTER_REGION','FILTER_GENDER','FILTER_FORMAT','RENT'];
            $result = [];
            $this->getElementsRecursive($props, $property, [], $result);
            
            if (!empty($result)) {
                foreach ($result as $el) {
                    $arFields['FILTER_MOTIV'] = $el['FILTER_MOTIV'];
                    $arFields['TREND'] = $el['TREND'];
                    $arFields['FILTER_REGION'] = $el['FILTER_REGION'];
                    $arFields['FILTER_STRESS'] = $el['FILTER_STRESS'];

                    $arFields['FILTER_GENDER'] = $el['FILTER_GENDER'];
                    $arFields['FILTER_FORMAT'] = $el['FILTER_FORMAT'];

                    $product[$arFields['ID']][] = $arFields;
                } 
            } else {
                $product[[$arFields['ID']]][] = $arFields; 
            }

        }

        return $product;
    }

    public function getElementsRecursive (array $props = [], array $element, $adds = [], &$result)
    {
        sort($props);
        if (count($props) > 0) {
            if (!empty($element[$props[0]]['VALUE'])) {
                if (is_array($element[$props[0]]['VALUE'])) {
                    foreach ($element[$props[0]]['VALUE'] as $p) {
                        $adds[$props[0]] = $p;
                        $cpProps = $props;
                        unset($cpProps[0]);
                        $this->getElementsRecursive($cpProps, $element, $adds, $result);
                    }
                } else {
                    $adds[$props[0]] = $element[$props[0]]['VALUE'];
                    unset($props[0]);
                    $this->getElementsRecursive($props, $element, $adds, $result);
                }
                
            } else {
                unset($props[0]);
                $this->getElementsRecursive($props, $element, $adds, $result);
            }
        } else {
            return $result[] = $adds;
        }
    }

    public function getHall ($iblockId) {
        $arSelect = [ 
            "ID",
            "NAME",
            "IBLOCK_ID",
        ];

        $arFilter = [
            "IBLOCK_ID"=>$iblockId, 
        ];


        $res = \CIBlockElement::GetList(["SORT"=>"ASC"], $arFilter, false, false, $arSelect);
        while($ob = $res->GetNextElement()) {
            $arFields = $ob->GetFields(); 
            $property = $ob->GetProperties();
            $props = ['METRO', 'AREA_CITY','rent'];
            $result = [];
            $this->getElementsRecursive($props, $property, [], $result);
            if (!empty($result)) {
                foreach ($result as $el) {
                    $arFields['METRO'] = $el['METRO'];
                    $arFields['AREA_CITY'] = $el['AREA_CITY'];
                    $arFields['RENT'] = $el['rent'];
                    $hall[$arFields['ID']][] = $arFields;
                } 
            }
        }

        return $hall;
    }

    public function createArr () { 
        $offers = $this->getOffer(OFFER_IBLOCK_ID);
        $product = $this->getProduct(CATALOG_IBLOCK_ID);
        $hall = $this->getHall(HALL_IBLOCK_ID);
        $result = [];

        foreach ($offers as $key => $offer) {

            if(!empty($offer['PROPERTY_HALL_VALUE'])) {
                foreach ($hall[$offer['PROPERTY_HALL_VALUE']] as $key => $arHall) {
                    $offer['METRO'] = $arHall['METRO'];
                    $offer['AREA_CITY'] = $arHall['AREA_CITY'];
                    $offer['RENT'] = $arHall['RENT'];

                    if(!empty($offer['PROPERTY_CML2_LINK_VALUE'])) {
                        foreach ($product[$offer['PROPERTY_CML2_LINK_VALUE']] as $key => $el) {
                            $offer['FILTER_MOTIV'] = $el['FILTER_MOTIV'];
                            $offer['FILTER_REGION'] = $el['FILTER_REGION'];
                            $offer['FILTER_STRESS'] = $el['FILTER_STRESS'];

                            $offer['FILTER_GENDER'] = $el['FILTER_GENDER'];
                            $offer['FILTER_FORMAT'] = $el['FILTER_FORMAT'];

                            $offer['TREND'] = $el['TREND'];
                            $arrForTable[] = $offer;
                        }
                    } else {
                        $arrForTable[] = $offer;
                    }
                }
            } elseif(!empty($offer['PROPERTY_CML2_LINK_VALUE'])) {
                    foreach ($product[$offer['PROPERTY_CML2_LINK_VALUE']] as $key => $el) {
                        $offer['FILTER_MOTIV'] = $el['FILTER_MOTIV'];
                        $offer['FILTER_REGION'] = $el['FILTER_REGION'];
                        $offer['FILTER_STRESS'] = $el['FILTER_STRESS'];

                        $offer['FILTER_GENDER'] = $el['FILTER_GENDER'];
                        $offer['FILTER_FORMAT'] = $el['FILTER_FORMAT'];

                        $offer['TREND'] = $el['TREND'];
                        $arrForTable[] = $offer;
                    }
            } else {
                $arrForTable[] = $offer;
            }
        }


        return $arrForTable;
    }

    public function addOfferFromProp($prop, $element)
    {
        if (!empty($element['PROPERTY_AGE_VALUE'])) {

        }
    }

    public function writeToTable () {
        $offers = $this->createArr();

        $connection = \Bitrix\Main\Application::getConnection();
        $connection->truncateTable('connect_tequila');


        foreach ($offers as $key => $offer) {
            if(!empty($offer['PROPERTY_DAYTIME_VALUE'])){
                foreach ($offer['PROPERTY_DAYTIME_VALUE'] as $dayTime){
                    $el =
                        [
                            'ID_OFFER' => $offer['ID'],
                            'ID_PRODUCT' => (!empty($offer['PROPERTY_CML2_LINK_VALUE'])?$offer['PROPERTY_CML2_LINK_VALUE']:-1),
                            'ID_TEACHER' => (!empty($offer['PROPERTY_PREPOD_VALUE'])?$offer['PROPERTY_PREPOD_VALUE']:-1),
                            'ID_HALL' => (!empty($offer['PROPERTY_HALL_VALUE'])?$offer['PROPERTY_HALL_VALUE']:-1),
                            'TREND' => (!empty($offer['TREND'])?$offer['TREND']:-1),
                            'PRICE' => (!empty($offer['CATALOG_PRICE_1'])?$offer['CATALOG_PRICE_1']:-1),
                            'FILTER_STRESS' => (!empty($offer['FILTER_STRESS'])?$offer['FILTER_STRESS']:-1),
                            'FILTER_REGION' => (!empty($offer['FILTER_REGION'])?$offer['FILTER_REGION']:-1),
                            'FILTER_MOTIV' => (!empty($offer['FILTER_MOTIV'])?$offer['FILTER_MOTIV']:-1),
                            'AREA_CITY' => (!empty($offer['AREA_CITY'])?$offer['AREA_CITY']:-1),
                            'METRO' => (!empty($offer['METRO'])?$offer['METRO']:-1),
                            'LEVEL' => (!empty($offer['PROPERTY_LEVEL_VALUE'])?$offer['PROPERTY_LEVEL_VALUE']:-1),
                            'AGE' => (!empty($offer['PROPERTY_AGE_VALUE'])?$offer['PROPERTY_AGE_VALUE']:-1),
                            'DAYTIME' => $dayTime,
                            'FILTER_GENDER' => (!empty($offer['FILTER_GENDER'])?$offer['FILTER_GENDER']:-1),
                            'FILTER_FORMAT' => (!empty($offer['FILTER_FORMAT'])?$offer['FILTER_FORMAT']:-1),
                            'RENT' => (!empty($offer['RENT'])?$offer['RENT']:-1),
                        ];


                    OrmMetod::addOrmEl($el);
                }
            }
            else{
                $el =
                    [
                        'ID_OFFER' => $offer['ID'],
                        'ID_PRODUCT' => (!empty($offer['PROPERTY_CML2_LINK_VALUE'])?$offer['PROPERTY_CML2_LINK_VALUE']:-1),
                        'ID_TEACHER' => (!empty($offer['PROPERTY_PREPOD_VALUE'])?$offer['PROPERTY_PREPOD_VALUE']:-1),
                        'ID_HALL' => (!empty($offer['PROPERTY_HALL_VALUE'])?$offer['PROPERTY_HALL_VALUE']:-1),
                        'TREND' => (!empty($offer['TREND'])?$offer['TREND']:-1),
                        'PRICE' => (!empty($offer['CATALOG_PRICE_1'])?$offer['CATALOG_PRICE_1']:-1),
                        'FILTER_STRESS' => (!empty($offer['FILTER_STRESS'])?$offer['FILTER_STRESS']:-1),
                        'FILTER_REGION' => (!empty($offer['FILTER_REGION'])?$offer['FILTER_REGION']:-1),
                        'FILTER_MOTIV' => (!empty($offer['FILTER_MOTIV'])?$offer['FILTER_MOTIV']:-1),
                        'AREA_CITY' => (!empty($offer['AREA_CITY'])?$offer['AREA_CITY']:-1),
                        'METRO' => (!empty($offer['METRO'])?$offer['METRO']:-1),
                        'LEVEL' => (!empty($offer['PROPERTY_LEVEL_VALUE'])?$offer['PROPERTY_LEVEL_VALUE']:-1),
                        'AGE' => (!empty($offer['PROPERTY_AGE_VALUE'])?$offer['PROPERTY_AGE_VALUE']:-1),
                        'DAYTIME' => '',
                        'FILTER_GENDER' => (!empty($offer['FILTER_GENDER'])?$offer['FILTER_GENDER']:-1),
                        'FILTER_FORMAT' => (!empty($offer['FILTER_FORMAT'])?$offer['FILTER_FORMAT']:-1),
                        'RENT' => (!empty($offer['RENT'])?$offer['RENT']:-1),
                    ];

                OrmMetod::addOrmEl($el);
            }

        }
    }
}