Current Path : /home/bitrix/ext_www/shuft.com.ua/import/ |
Current File : /home/bitrix/ext_www/shuft.com.ua/import/index.php |
<? include $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php'; // require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php"); $APPLICATION->SetTitle("Каталог"); if (!$USER || !$USER->IsAdmin()) { p('admins only'); die(); } CModule::IncludeModule('iblock'); Cmodule::IncludeModule('catalog'); @ini_set('memory_limit', '4096M'); @set_time_limit(595); // ini_set('display_errors', 1); // ini_set('display_startup_errors', 1); // error_reporting(E_ALL); // $dir = './_export.e687a997b372f8f9e9172cf2c4855b30/'; $dir = './xml_files/'; $dir2 = $_SERVER['DOCUMENT_ROOT'].'/upload/'; // $tree = $dir.'tree.xml'; $tree = $dir.'tree_catalog.xml'; // $params = $dir.'params.xml'; $params = $dir.'params_catalog.xml'; function xml2assoc(&$xml){ $assoc = NULL; $n = 0; while($xml->read()){ if($xml->nodeType == \XMLReader::END_ELEMENT) { break; } if($xml->nodeType == \XMLReader::ELEMENT && !$xml->isEmptyElement){ $assoc[$n]['name'] = $xml->name; if($xml->hasAttributes) { while($xml->moveToNextAttribute()) { $assoc[$n]['atr'][$xml->name] = $xml->value; } } $assoc[$n]['val'] = xml2assoc($xml); $n++; } else if($xml->isEmptyElement) { $assoc[$n]['name'] = $xml->name; if($xml->hasAttributes) { while($xml->moveToNextAttribute()) { $assoc[$n]['atr'][$xml->name] = $xml->value; } } $assoc[$n]['val'] = false; $n++; } else if($xml->nodeType == \XMLReader::TEXT) { $assoc = $xml->value; } } return $assoc; } // чтение файла с инфой о каталоге $reader = new \XMLReader(); if (!$reader->open($tree, NULL, LIBXML_NOEMPTYTAG)) { p('Не удалось открыть файл'); } else { $data = xml2assoc($reader); $reader->close(); // p($data); // die(); $good_data = []; foreach($data[0]['val'] as $row) { $good_item = []; foreach($row['val'] as $field) { $good_item[$field['atr']['name']] = $field['val']; } // $good_item['document'] = strip_tags($good_item['document']); // $good_item['document'] = htmlspecialcharsEx($good_item['document']); $good_data[$good_item['id']] = $good_item; } unset($data); // p($good_data); } // чтение параметров $reader = new \XMLReader(); if (!$reader->open($params, NULL, LIBXML_NOEMPTYTAG)) { p('Не удалось открыть файл'); } else { $data = xml2assoc($reader); $reader->close(); // p($data); // die(); // $good_data = []; $tree_params = [ 'cat' => [], 'item' => [], 'img_chema' => [], 'img_draft' => [], 'img' => [], 'pdf' => [], ]; foreach($data[0]['val'] as $row) { $good_item = []; foreach($row['val'] as $field) { $good_item[$field['atr']['name']] = $field['val']; } // $good_data[$good_item['id']] = $good_item; if (!empty($good_data[$good_item['doc_id']]['params'][$good_item['pname']])) { p('dublicate!'); } $doc_id = $good_item['doc_id']; unset($good_item['doc_id']); $good_data[$doc_id]['params'][$good_item['pname']] = $good_item; if ($good_data[$doc_id]['type'] == 'folder_catalog_product') { $tree_params['item'][$good_item['pname']] = $good_item; } else { $tree_params['cat'][$good_item['pname']] = $good_item; } if (isset($tree_params[$good_item['pname']])) { $tree_params[$good_item['pname']][$good_item['pvalue']]++; } } unset($data); // p($good_data); } // сортировка параметров function cmp($a, $b) { if ($a == $b) { return 0; } if (strpos($a, 'param_') === 0 && strpos($b, 'param_') === 0) { $a_id = substr($a, 6); $b_id = substr($b, 6); if ($a_id == $b_id) { return 0; } return ($a_id > $b_id) ? 1 : -1; } return ($a > $b) ? 1 : -1; } // p($tree_params); uksort($tree_params['item'], "cmp"); // p($tree_params['item']); // проверка на существование файлов // if (false) { if (1) { p('----------------------------------------------------------------------'); p('not exist files: '); p('----------------------------------------------------------------------'); foreach($tree_params['img_chema'] as $img_chema => $e) { $cur_way = $dir2.'chema/big/'.$img_chema; $cur_way2 = $dir2.'draft/big/'.$img_chema; if (!file_exists($cur_way) && !file_exists($cur_way2)) { p('no file '.$cur_way); } } foreach($tree_params['img_draft'] as $img_chema => $e) { $cur_way = $dir2.'draft/big/'.$img_chema; $cur_way2 = $dir2.'chema/big/'.$img_chema; if (!file_exists($cur_way) && !file_exists($cur_way2)) { p('no file '.$cur_way); } } foreach($tree_params['img'] as $img_chema => $e) { $cur_way = $dir2.'catpreview/big/'.$img_chema; if (!file_exists($cur_way)) { p('no file '.$cur_way); } } foreach($tree_params['pdf'] as $img_chema => $e) { $cur_way = $dir2.'downloads/'.$img_chema; if (!file_exists($cur_way)) { p('no file '.$cur_way); } } p('----------------------------------------------------------------------'); // die(); } // параметры в каталоге // if (false) { if (1) { p('----------------------------------------------------------------------'); p('props: '); p('----------------------------------------------------------------------'); $arProps = []; $properties = CIBlockProperty::GetList(Array("sort"=>"asc", "name"=>"asc"), Array("ACTIVE"=>"Y", "IBLOCK_ID"=>9)); while ($prop_fields = $properties->GetNext()) { $arProps[$prop_fields['CODE']] = $prop_fields; } $SORT = 100; foreach($tree_params['item'] as $param) { $arFields = Array( "NAME" => !empty($param['param_descr']) ? $param['param_descr'] : $param['pname'], "ACTIVE" => 'Y', "SORT" => $SORT, "CODE" => strtoupper($param['pname']), "XML_ID" => $param['pname'], // "HINT" => $param['ТипХарактеристики'], // "FILTER_HINT" => $param['ЕдиницаИзмерения'], "HINT" => $param['param_dimen'], "COL_COUNT" => "50", "FILTRABLE" => "Y", "PROPERTY_TYPE" => "S", "USER_TYPE" => "", "IBLOCK_ID" => 9, "DEFAULT_VALUE" => "", ); $showData = false; $el = new CIBlockProperty; if (!isset($arProps[$arFields['CODE']])) { $arFields['SEARCHABLE'] = 'N'; $arFields['SMART_FILTER'] = 'N'; if($PRODUCT_ID = $el->Add($arFields)) { p("New ID: ".$PRODUCT_ID); } else { p("Error add: ".$el->LAST_ERROR); } p('NEW!!!!!!!!!'); $showData = true; } else { // p('OLD!!!!!!!!!'); $props = $arProps[$arFields['CODE']]; unset($props['TIMESTAMP_X']); unset($props['~TIMESTAMP_X']); $removeParams = [ 'TMP_ID', 'USER_TYPE_SETTINGS', 'USER_TYPE', ]; foreach($removeParams as $removeParam) { if (empty($props[$removeParam])) { unset($props[$removeParam]); unset($props['~'.$removeParam]); } } foreach($arFields as $prop_code => $prop_value) { if ($props['~'.$prop_code] != $prop_value) { p('unmatch!!!'); p($prop_code); p('new '.$prop_value); p('old '.$props[$prop_code]); } else { unset($arFields[$prop_code]); } } if (!empty($arFields)) { $showData = true; p('update!!!!!!'); $updateArr = array_merge($props, $arFields); // unset($updateArr['DEFAULT_VALUE']); // unset($updateArr['~DEFAULT_VALUE']); unset($updateArr['MULTIPLE_CNT']); unset($updateArr['~MULTIPLE_CNT']); unset($updateArr['LINK_IBLOCK_ID']); unset($updateArr['~LINK_IBLOCK_ID']); unset($updateArr['FILE_TYPE']); unset($updateArr['~FILE_TYPE']); unset($updateArr['WITH_DESCRIPTION']); unset($updateArr['~WITH_DESCRIPTION']); unset($updateArr['IS_REQUIRED']); unset($updateArr['~IS_REQUIRED']); p($updateArr); if(!$el->Update($props['ID'], $updateArr)) { echo $el->LAST_ERROR; } else { p('updated: '.$props['ID']); } } // p($props); } $SORT += 1; if ($showData) { p($arFields); p($param); p('-----------------------------------------------------------------------------------'); } } } // апдейт свойств для поиска. нужен был для выборочного вывода в фильтр if (false) { $properties = CIBlockProperty::GetList(Array("sort"=>"asc", "name"=>"asc"), Array("ACTIVE"=>"Y", "IBLOCK_ID"=>9)); while ($prop_fields = $properties->GetNext()) { if (strpos($prop_fields['CODE'], 'PARAM_') === 0) { p($prop_fields['NAME']); $arFields = array(); $arFields['SEARCHABLE'] = 'N'; $arFields['SMART_FILTER'] = 'N'; $arFields['IBLOCK_ID'] = 9; $ibp = new CIBlockProperty; if(!$ibp->Update($prop_fields['ID'], $arFields)) { p('error: '.$ibp->LAST_ERROR); p($prop_fields); } else { p('updated '.$prop_fields['NAME'].' ('.$prop_fields['CODE'].')'); } } } } $catalog_tree = []; foreach($good_data as $item) { if ($item['type'] != 'folder_catalog_product') { $catalog_tree[$item['type']][$item['url']] = $item; } } ksort($catalog_tree); foreach($catalog_tree as $type => $items) { ksort($catalog_tree[$type]); } $good_tree = []; $id_to_cat = []; $root_id = 0; foreach($catalog_tree as $type => $items) { foreach($items as $item) { $levels = explode('/', $item['url']); $way = array_slice($levels, 2, count($levels)-3); $name = $item['fullname'].' ['.$item['url'].']'; if (empty($way)) { $good_tree[$item['id']] = [ 'name' => $item['fullname'], 'url' => $item['url'], 'document' => $item['document'], 'child' => [], ]; $id_to_cat[$item['id']] = &$good_tree[$item['id']]; $root_id = $item['id']; } else { $cnt = count($way)-1; $cur_dir = &$id_to_cat[$root_id]; foreach($way as $way_i=>$way_id) { if ($way_i == $cnt && ($cur_dir || $cnt == 0)) { $cur_dir['child'][$item['id']] = [ 'name' => $item['fullname'], 'url' => $item['url'], 'document' => $item['document'], 'params' => $item['params'], 'child' => [], ]; if (empty($cur_dir['child'][$item['id']]['params'])) { unset($cur_dir['child'][$item['id']]['params']); } $id_to_cat[$item['id']] = &$cur_dir['child'][$item['id']]; } else { $cur_dir = &$cur_dir['child'][$way_id]; } } } } // break; } foreach($good_data as $item) { if ($item['type'] == 'folder_catalog_product') { // p($item); $levels = explode('/', $item['url']); // p($levels); $way = array_slice($levels, 2, count($levels)-4); // p($way); $last_level = $way[count($way)-1]; // p($last_level); // p($id_to_cat[$last_level]); if (empty($id_to_cat[$last_level])) { p('cat not found!'); p($item); } $id_to_cat[$last_level]['child'][$item['id']] = $item; // break; } } // p($good_tree); // die(); function add_cat($section, $id, $root_section) { global $made_cats; if (!empty($section['type']) && $section['type'] == 'folder_catalog_product') { return false; } $arFields = Array( "ACTIVE" => 'Y', "IBLOCK_SECTION_ID" => $root_section, "IBLOCK_ID" => 9, "NAME" => $section['name'], "CODE" => $id, "SORT" => 500, "DESCRIPTION" => $section['document'], "DESCRIPTION_TYPE" => 'html', "UF_PRIMECHAINE" => '', "UF_TABLE4" => '', "UF_SERNAME_1" => '', "UF_PDF_SERIES" => '', "UF_NAME_PDF_SERIES" => '', ); if (!empty($section['params'])) { foreach($section['params'] as $param) { $param['pname'] = strtoupper($param['pname']); $arFields['UF_'.$param['pname']] = $param['pvalue']; } } // p('-----------------------------------------------------------------------'); if (!isset($made_cats[$id])) { p($arFields); $bs = new CIBlockSection; $RESULT_ID = $bs->Add($arFields, false, false, false); $res = ($RESULT_ID>0); if(!$res) { p($bs->LAST_ERROR); } else { p($section['name'].' added'); } } else { $RESULT_ID = $made_cats[$id]['ID']; foreach($arFields as $field_code => $field_val) { if ($made_cats[$id][$field_code] == $field_val) { unset($arFields[$field_code]); } else { // p('old '.$made_cats[$id][$field_code]); // p('new '.$field_val); } } unset($arFields['SORT']); if (!empty($arFields)) { p('cat need update!!!'); $bs = new CIBlockSection; $res = $bs->Update($made_cats[$id]['ID'], $arFields); p($arFields); p($made_cats[$id]); } // p('cat exists'); } if (!empty($section['child'])) { foreach($section['child'] as $child_id => $child_data) { add_cat($child_data, $child_id, $RESULT_ID); } } } // добавление категорий // if (false) { if (1) { p('----------------------------------------------------------------------'); p('cats: '); p('----------------------------------------------------------------------'); $made_cats = []; $arFilter = Array("IBLOCK_ID"=>9); // $arSelect = Array("ID", "NAME", "CODE", "XML_ID"); $arSelect = Array("*", "UF_*"); $res = CIBlockSection::GetList(Array(), $arFilter, false, $arSelect); while($arFields = $res->Fetch()) { $made_cats[$arFields['CODE']] = $arFields; } $root_section = false; foreach($good_tree['100203']['child'] as $id => $item) { add_cat($item, $id, $root_section); } // CIBlockSection::ReSort(9); } // удаление всех разделов 5 уровня // if (false) { if (1) { $made_cats = []; $arFilter = Array("IBLOCK_ID"=>9, "DEPTH_LEVEL"=>5); $arSelect = Array("ID", "NAME", "CODE", "XML_ID"); $res = CIBlockSection::GetList(Array(), $arFilter, false, $arSelect); while($arFields = $res->Fetch()) { $made_cats[$arFields['CODE']] = $arFields['ID']; } if (!empty($made_cats)) { foreach($made_cats as $SECTION_ID) { CIBlockSection::Delete($SECTION_ID, false); p('removed section '.$SECTION_ID); } // p($made_cats); // CIBlockSection::ReSort(9); } } // if (false) { // if (1) { p('----------------------------------------------------------------------'); p('found items: '); p('----------------------------------------------------------------------'); $made_els = []; $arFilter = Array("IBLOCK_ID"=>9); $arSelect = Array("ID", "NAME", "CODE", "XML_ID"); $res = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect); while($arFields = $res->Fetch()) { $made_els[$arFields['CODE']] = $arFields['ID']; } foreach($good_data as $good_item) { if ($good_item['type'] == 'folder_catalog_product') { if (isset($made_els[$good_item['id']])) { // p($good_item); // break; // $arLoadProductArray = Array( // 'NAME' => $good_item['name'], // ); // p($arLoadProductArray); // $el = new CIBlockElement; // $res = $el->Update($made_els[$good_item['id']], $arLoadProductArray, false, false, false, false); // $arUpdate = []; // $arUpdate['FULLNAME'] = $good_item['fullname']; // CIBlockElement::SetPropertyValuesEx($made_els[$good_item['id']], 9, $arUpdate); unset($made_els[$good_item['id']]); } else { p('not FOUND!!!'); p($good_item); } } } foreach($made_els as $ELEMENT_CODE => $ELEMENT_ID) { // CIBlockElement::Delete($ELEMENT_ID); } p($made_els); die(); } function add_item($element, $id, $root_section) { if (!empty($element['child'])) { foreach($element['child'] as $child_id => $child_data) { add_item($child_data, $child_id, $id); } } elseif($element['type'] == 'folder_catalog_product') { global $made_cats; global $made_els; global $arProps; // p($made_cats[$root_section]); // p($root_section); // p($element); $el = new CIBlockElement; $arLoadProductArray = Array( "IBLOCK_SECTION_ID" => $made_cats[$root_section]['ID'], "IBLOCK_ID" => 9, "NAME" => strlen($element['name']) > 0 ? $element['name'] : '[empty]', "CODE" => $element['id'], "XML_ID" => $element['id'], "ACTIVE" => 'Y', "SORT" => '500', "DETAIL_TEXT" => $element['document'], "DETAIL_TEXT_TYPE" => 'html', "PROPERTY_VALUES" => [], ); foreach($arProps as $arProp) { $arLoadProductArray["PROPERTY_VALUES"][$arProp['CODE']] = ''; } foreach($element['params'] as $param) { $pname = strtoupper($param['pname']); $arLoadProductArray["PROPERTY_VALUES"][$pname] = $param['pvalue']; } if (!empty($arLoadProductArray["PROPERTY_VALUES"]['TYPE'])) { $arLoadProductArray["PROPERTY_VALUES"]['TYPE'] = $made_cats[$arLoadProductArray["PROPERTY_VALUES"]['TYPE']]['ID']; } $arLoadProductArray["PROPERTY_VALUES"]['FULLNAME'] = $element['fullname']; $arLoadProductArray["PROPERTY_VALUES"]['SERIES'] = $made_cats[$root_section]['UF_SERNAME_1']; if (isset($made_els[$arLoadProductArray["CODE"]])) { // p($arLoadProductArray); /* $arUpdate = []; if (!empty($arLoadProductArray["PROPERTY_VALUES"]['TYPE'])) { $arUpdate['TYPE'] = $made_cats[$arLoadProductArray["PROPERTY_VALUES"]['TYPE']]['ID']; // p('ex'); } else { $arUpdate['TYPE'] = false; } CIBlockElement::SetPropertyValuesEx($made_els[$arLoadProductArray["CODE"]], $arLoadProductArray["IBLOCK_ID"], $arUpdate); */ // p('CODE'); // p($arLoadProductArray["CODE"]); // p('ID'); // p($made_els[$arLoadProductArray["CODE"]]); // p('update'); // p($arLoadProductArray["PROPERTY_VALUES"]); // break; if (!empty($arLoadProductArray["PROPERTY_VALUES"])) { // $arLoadProductArray["PROPERTY_VALUES"] = ['PARAM_2' => str_replace(',', '.', $arLoadProductArray["PROPERTY_VALUES"]['PARAM_2'])]; // $arLoadProductArray["PROPERTY_VALUES"] = ['PARAM_2' => $arLoadProductArray["PROPERTY_VALUES"]['PARAM_2']]; // p('CODE'); // p($arLoadProductArray["CODE"]); // p('ID'); // p($made_els[$arLoadProductArray["CODE"]]); // p('update'); // p($arLoadProductArray["PROPERTY_VALUES"]); CIBlockElement::SetPropertyValuesEx($made_els[$arLoadProductArray["CODE"]], $arLoadProductArray["IBLOCK_ID"], $arLoadProductArray["PROPERTY_VALUES"]); } unset($arLoadProductArray["PROPERTY_VALUES"]); if($el->Update($made_els[$arLoadProductArray["CODE"]], $arLoadProductArray)) { // p("Updated ID: ".$PRODUCT_ID); } else { echo '<div class="make_error">'; p("Error add: ".$el->LAST_ERROR); echo '</div>'; } } else { if($PRODUCT_ID = $el->Add($arLoadProductArray, false, false, false)) { p("New ID: ".$PRODUCT_ID); // $made_cats[$import_data['ИдентификаторВБазе']] = $PRODUCT_ID; // $made_els_codes[$arLoadProductArray['CODE']] = $PRODUCT_ID; } else { echo '<div class="make_error">'; p("Error add: ".$el->LAST_ERROR); echo '</div>'; } p($arLoadProductArray); p('-----------------------------------------------------------------------------------'); } // break; } } // if (false) { if (1) { p('----------------------------------------------------------------------'); p('items: '); p('----------------------------------------------------------------------'); $made_cats = []; $arFilter = Array("IBLOCK_ID"=>9); $arSelect = Array("ID", "NAME", "CODE", "XML_ID", "UF_SERNAME_1"); $res = CIBlockSection::GetList(Array(), $arFilter, false, $arSelect); while($arFields = $res->Fetch()) { $made_cats[$arFields['CODE']] = $arFields; } // p($made_cats); $made_els = []; $arFilter = Array("IBLOCK_ID"=>9); $arSelect = Array("ID", "NAME", "CODE", "XML_ID"); $res = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect); while($arFields = $res->Fetch()) { $made_els[$arFields['CODE']] = $arFields['ID']; } // p($made_els); $arProps = []; $properties = CIBlockProperty::GetList(Array("sort"=>"asc", "name"=>"asc"), Array("ACTIVE"=>"Y", "IBLOCK_ID"=>9)); while ($prop_fields = $properties->GetNext()) { $arProps[$prop_fields['CODE']] = $prop_fields; } // p($arProps); $root_section = false; foreach($good_tree['100203']['child'] as $id => $item) { add_item($item, $id, $root_section); } } // if (false) { if (1) { $made_cats = []; $arFilter = Array("IBLOCK_ID"=>9); $arSelect = Array("ID", "NAME", "CODE", "XML_ID", "UF_SERNAME_1"); $res = CIBlockSection::GetList(Array(), $arFilter, false, $arSelect); while($arFields = $res->Fetch()) { $made_cats[$arFields['ID']] = $arFields['UF_SERNAME_1']; } // p($made_cats); $made_els = []; $arFilter = Array("IBLOCK_ID"=>9); $arSelect = Array("ID", "NAME", "CODE", "XML_ID", "IBLOCK_SECTION_ID"); $res = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect); while($arFields = $res->Fetch()) { if (!empty($arFields['IBLOCK_SECTION_ID']) && isset($made_cats[$arFields['IBLOCK_SECTION_ID']])) { CIBlockElement::SetPropertyValuesEx($arFields['ID'], 9, ['SERIES'=>$made_cats[$arFields['IBLOCK_SECTION_ID']]]); } } // p($made_els); } CIBlockSection::ReSort(9); $index = \Bitrix\Iblock\PropertyIndex\Manager::createIndexer(9); $index->startIndex(); $index->continueIndex(0); // создание без ограничения по времени $index->endIndex(); ?> <? // require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php"); ?>