Your IP : 3.149.214.43


Current Path : /home/bitrix/initial_sites/home-comfort.in.ua/local/import/
Upload File :
Current File : /home/bitrix/initial_sites/home-comfort.in.ua/local/import/ServicesParser.php

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

class ServicesParser extends XmlParser
{
    protected $la = array();
    protected $el = null;
    protected $sl = null;
    protected $id = 0;
    protected $cats = array();
    protected $cities = array();
    protected $contactsForUser = array();
    protected $full_tag = '';
    protected $defaultStore = null;
    protected $INFO_TAGS = array('city', 'city_id', 'desc', 'phone', 'site', 'name', 'adress');
    protected $MAIN_TAG = 'service';
    protected $targetIblock = SERVICES_IBLOCK_ID;

    public function beginParsing()
    {
        parent::beginParsing();
        $this->el = new CIBlockElement;
        $this->sl = new CIBlockSection;

        $res = CIBlockElement::GetList(array(), array("IBLOCK_ID" => 5), false, false, array("ID", "NAME", "EXTERNAL_ID"));
        while ($ar_res = $res->Fetch()) {
            $this->cities[$ar_res['EXTERNAL_ID']] = $ar_res;
        }

        /*
        $res = CIBlockSection::GetList(array(), array("IBLOCK_ID" => 18), false, array("ID", "NAME", "EXTERNAL_ID"));
        while ($ar_res = $res->Fetch()) {
            $this->cats[$ar_res['EXTERNAL_ID']] = $ar_res;
        }
        */
        
        global $catsLinker;
        $this->cats = $catsLinker;
    }

    public function tagOpen($parser, $name, $attrs)
    {
        parent::tagOpen($parser, $name, $attrs);
        if ($name == 'code') {
            $this->object['cats'][] = $attrs;
        }
    }

    protected function parseData($name)
    {
        if ($name == 'service') {
            foreach ($this->object as $key => &$value) {
                if (!is_array($value)) {
                    $value = htmlspecialchars_decode($value);
                }
            }
            unset($value);
            //Город
            if (isset($this->cities[$this->object['city_id']])) {
                $cityId = $this->cities[$this->object['city_id']]['ID'];
            } else {
                $arFields = array(
                   "IBLOCK_ID" => 5,
                   "ACTIVE" => 'Y',
                   "EXTERNAL_ID" => $this->object['city_id'],
                   "NAME" => $this->object['city']
                );
                if ($cityId = $this->el->Add($arFields)) {
                    $this->message_to_log('New ID:'.$id.' for '.$arFields['NAME']);
                    $this->cities[$arFields['EXTERNAL_ID']] = array('ID' => $cityId, 'NAME' => $arFields['NAME']);
                } else {
                    $this->message_to_error_log('ERROR:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
                }
            }
            //Рубрики
            $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;
                        }
                    }
                }
            }
            /*
            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']);
                        }
                    }
                }
            }
            */
            
            $arFields = array(
               "IBLOCK_ID" => $this->targetIblock,
               "ACTIVE" => 'Y',
               "NAME" => $this->object['name'],
               "PREVIEW_TEXT" => $this->object['adress'],
               "DETAIL_TEXT" => $this->object['desc'],
               "PROPERTY_VALUES" => array(
                    'CITIES' => $cityId,
                    'PHONE' => $this->object['phone'],
                    'SITE' => $this->object['site'],
                    'CATEGORIES' => $catsIds,
                )
            );
            $rs = CIBlockElement::GetList(array(), array('IBLOCK_ID' => $arFields['IBLOCK_ID'], 'NAME' => $arFields['NAME'], 'PROPERTY_CITIES' => $cityId), 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 {
                    $this->message_to_error_log('ERROR:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
                }
            }
        }
    }

    protected function addIB($arFields)
    {
        $id = null;
        $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['EXTERNAL_ID'], $arFields['ACTIVE']);
            
            if ($this->el->Update($id = $ar['ID'], $arFields)) {
                $this->message_to_log('Update ID:'.$ar['ID'].' for '.$arFields1['EXTERNAL_ID'].' '.$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['EXTERNAL_ID'].' '.$arFields['NAME']);
            } else {
                $this->message_to_error_log('ERROR:'.$this->el->LAST_ERROR.' for '.$arFields['NAME']);
            }
        }
        return $id;
    }
}