Your IP : 3.147.53.98


Current Path : /home/bitrix/ext_www/ballu.in.ua/local/import/
Upload File :
Current File : /home/bitrix/ext_www/ballu.in.ua/local/import/Dealers2Parser.php

<?php
require_once('ServicesParser.php');

class Dealers2Parser extends ServicesParser
{
    protected $MAIN_TAG = 'shop';
    protected $targetIblock = DEALERS_IBLOCK_ID;
    protected $countries;
    protected $allCats = null;
    protected $equipments;
    protected $INFO_TAGS = array('city', 'city_id', 'desc', 'phone', 'site', 'name', 'adress', 'type');


    public function beginParsing()
    {
        parent::beginParsing();
        /*foreach ($this->cats as $key => $value) {
            foreach ($value as $catId) {
                $this->allCats[] = $catId;
            }
        }*/
        $res = CIBlockElement::GetList(array(), array("IBLOCK_ID" => 27), false, false, array("ID", "NAME", "EXTERNAL_ID"));
        while ($ar_res = $res->Fetch()) {
            $this->equipments[$ar_res['EXTERNAL_ID']] = $ar_res;
        }
        
        $res = CIBlockElement::GetList(array(), array("IBLOCK_ID" => 8), false, false, array("ID", "NAME", "EXTERNAL_ID"));
        while ($ar_res = $res->Fetch()) {
            $this->countries[$ar_res['EXTERNAL_ID']] = $ar_res;
        }
    }

    function tagClosed($parser, $name) {
        $this->inside_data = false; 
        if ($name == $this->MAIN_TAG || ($name == 'client' && ! isset($this->object['shop']))) {
            $this->parseData($name);
        }        
        if ($name == 'type') {
            if (!isset($this->equipments[$this->object['equipment']])) {
                $arFields = array(
                   "IBLOCK_ID" => 85,
                   "ACTIVE" => 'Y',
                   "EXTERNAL_ID" => $this->object['equipment'],
                   "NAME" => $this->object['type'],
                );
                if ($id = $this->el->Add($arFields)) {
                    $this->message_to_log('New ID:'.$id.' for '.$arFields['NAME']);
                    $this->equipments[$arFields['EXTERNAL_ID']] = array('ID' => $id, 'NAME' => $arFields['NAME'], 'EXTERNAL_ID' => $arFields['EXTERNAL_ID']);
                } else {
                    $this->message_to_error_log('ERROR - equipment:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
                }
            }
        }
        if ($name == 'client') {
            unset($this->object);
        }
        array_pop($this->Context);
    }    

    public function tagOpen($parser, $name, $attrs)
    {
        parent::tagOpen($parser, $name, $attrs);
        if ($name == 'shop') {
            $this->object['shop'] = $attrs;
        }
        if ($name == 'client') {
            $this->object['client'] = $attrs;
        }
        if ($name == 'type') {
            $this->object['equipment'] = $attrs['id'];
        }
        if ($name == 'city') {
            if (!isset($this->cities[$attrs['id']])) {
                $arFields = array(
                   "IBLOCK_ID" => 5,
                   "ACTIVE" => 'Y',
                   "EXTERNAL_ID" => $attrs['id'],
                   "NAME" => $attrs['name'],
                   "PROPERTY_VALUES"=>array('COUNTRY' => $this->countries[$attrs['country_id']]['ID']),
                );
                if ($id = $this->el->Add($arFields)) {
                    $this->message_to_log('New ID:'.$id.' for '.$arFields['NAME']);
                    $this->cities[$arFields['EXTERNAL_ID']] = array('ID' => $id, 'NAME' => $arFields['NAME'], 'EXTERNAL_ID' => $arFields['EXTERNAL_ID']);
                } else {
                    $this->message_to_error_log('ERROR:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
                }
            }
        }
        if ($name == 'country') {
            if (!isset($this->countries[$attrs['id']])) {
                $arFields = array(
                   "IBLOCK_ID" => 86,
                   "ACTIVE" => 'Y',
                   "EXTERNAL_ID" => $attrs['id'],
                   "NAME" => $attrs['name'],
                );
                if ($id = $this->el->Add($arFields)) {
                    $this->message_to_log('New ID:'.$id.' for '.$arFields['NAME']);
                    $this->countries[$arFields['EXTERNAL_ID']] = array('ID' => $id, 'NAME' => $arFields['NAME'], 'EXTERNAL_ID' => $arFields['EXTERNAL_ID']);
                } else {
                    $this->message_to_error_log('ERROR:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
                }
            }
        }
    }

    protected function parseData($name)
    {
        if ($name == $this->MAIN_TAG || ($name == 'client' && ! isset($this->object['shop']))) {
            foreach ($this->object as $key => &$value) {
                if (!is_array($value)) {
                    $value = htmlspecialchars_decode($value);
                }
            }
            unset($value);
            //Город
            if (isset($this->cities[$this->object['client']['city_id']])) {
                $cityId = $this->cities[$this->object['client']['city_id']]['ID'];
            }
            /*print $cityId;
            print_r($this->object);exit;*/

            //Рубрики
            /*$catsIds = array();
            if (isset($this->object['cats'])) {
                foreach ($this->object['cats'] as $key => $value) {
                    if (isset($this->cats[$value['id']])) {
                        foreach ($this->cats[$value['id']] as $catId) {
                            $catsIds[] = $catId;
                        }
                    }
                }
            } else {
                //Времененная настройка - если нет рубрик, то идет привязка ко всем рубрикам
                $catsIds = $this->allCats;
            }*/
            //Товарные категории
            $equipmentIds = array();
            if (isset($this->object['equipments'])) {
                
                foreach ($this->object['equipments'] as $key => $value) {
                    if (isset($this->equipments[$value])) {
                        $equipmentIds[] = $this->equipments[$value]['ID'];
                    }
                }
            }
            /*
            if (isset($this->object['cats'])) {
                foreach ($this->object['cats'] as $key => $value) {
                    if (isset($this->cats[$value['id']])) {
                        $catsIds[] = $this->cats[$value['id']]['ID'];
                    } else {
                        $arFields = array(
                           "IBLOCK_ID" => 18,
                           "ACTIVE" => 'Y',
                           "EXTERNAL_ID" => $value['id'],
                           "NAME" => $value['name']
                        );
                        if ($catId = $this->sl->Add($arFields)) {
                            $this->message_to_log('New ID:'.$id.' for '.$arFields['NAME']);
                            $catsIds[] = $catId;
                            $this->cats[$value['id']] = array('ID' => $catId, 'NAME' => $value['name']);
                        } else {
                            $this->message_to_error_log('ERROR:'.$this->sl->LAST_ERROR.' for '.$arFields['NAME']);
                        }
                    }
                }
            }
            */
            $phones = array();

            $is = isset($this->object['shop']) ? array($this->object['shop']['phone1'], $this->object['shop']['phone2'], $this->object['shop']['phone3'], $this->object['client']['phone']) : array($this->object['client']['phone']);
            foreach ($is as $key => $value) {
                if($value) {
                    $phones[] = $value;
                }
            }

            $arFields = array(
               "IBLOCK_ID" => $this->targetIblock,
               "ACTIVE" => 'Y',
               "NAME" => $this->object['client']['name'].
                    (isset($this->object['shop']['name']) && $this->object['shop']['name'] ? '<br/>'.$this->object['shop']['name'] : ''),
               "PREVIEW_TEXT" => isset($this->object['shop']['address']) ? $this->object['shop']['address'] : '',
               "DETAIL_TEXT" => $this->object['site_description'],
               "EXTERNAL_ID" => isset($this->object['shop']['id']) ? $this->object['shop']['id'] : 'client-'.$this->object['client']['id'],
               "PROPERTY_VALUES" => array(
                    'CITIES' => $cityId,
                    'PHONE' => implode(';', array_unique($phones)),
                    'SITE' => $this->object['client']['site1'],
                    'EQUIPMENT' => $equipmentIds,
                    //'CATEGORIES' => $catsIds,
                )
            );
            $rs = CIBlockElement::GetList(array(), array('IBLOCK_ID' => $arFields['IBLOCK_ID'], 'EXTERNAL_ID' => $arFields['EXTERNAL_ID']), false, false, array("ID"));
            if ($ar = $rs->GetNext()) {
                if (isset($arFields['PROPERTY_VALUES'])) {
                    CIBlockElement::SetPropertyValuesEx($ar['ID'], $arFields['IBLOCK_ID'], $arFields['PROPERTY_VALUES']);
                    unset($arFields['PROPERTY_VALUES']);
                }
                $arFields1 = $arFields;
                unset($arFields['IBLOCK_ID'], $arFields['ACTIVE']);
                
                if ($this->el->Update($id = $ar['ID'], $arFields)) {
                    $this->message_to_log('Update ID:'.$ar['ID'].' for '.$arFields1['NAME']);
                } else {
                    $this->message_to_error_log('UPDATE ERROR:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
                }
            } else {
                if ($id = $this->el->Add($arFields)) {
                    $this->message_to_log('New ID:'.$id.' for '.$arFields['NAME']);
                } else {
                    //print_r($this->object);exit;
                    $this->message_to_error_log('ERROR:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
                }
            }
        }
    }

    function tagData($parser, $tagData) {
        $len = count($this->Context);
        if ($len > 1 && in_array($this->Context[$len - 1], $this->INFO_TAGS)
            && ($this->Context[$len - 2] == $this->MAIN_TAG || $this->Context[$len - 2] == 'equipment')) 
        {
            if ($this->inside_data)
                $this->object[$this->Context[$len - 1]] .= $tagData;
            else
                $this->object[$this->Context[$len - 1]] = $tagData;
        }
        if ($len > 1 && ($this->Context[$len - 2] == 'client' && $this->Context[$len - 1] == 'equipment')) 
        {
            $this->object['equipments'][] = $tagData;
        }
        $this->inside_data = true;
    }
}