Your IP : 18.116.69.203


Current Path : /home/bitrix/ext_www/dev.shuft.com.ua/import/
Upload File :
Current File : /home/bitrix/ext_www/dev.shuft.com.ua/import/news.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 = './full/';
$tree = $dir.'tree_full.xml';
// $params = $dir.'params_full.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']);
		
		if ($good_item['type'] != 'folder' || (strpos($good_item['url'], '/about/news/') !== 0 && strpos($good_item['url'], '/about/press/') !== 0)) {
			continue;
		}
		$good_data[$good_item['id']] = $good_item;
		
	}
	
	unset($data);
	
	// p($good_data);
	
}

foreach($good_data as $good_item) {
	p(str_repeat('-', 100));
	p('<b>Название: </b>'.$good_item['fullname']);
	p('<b>Ссылка: </b>'.$good_item['url']);
	p('<b>Данные html: </b>');
	echo '<div style="border: 1px solid gray; padding: 10px;">';
	p(htmlspecialcharsEx($good_item['document']));
	echo '</div>';
	p(str_repeat('-', 100));
}

die();


/* $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);
	
} */

// foreach($tree_params['img_chema'] as $img_chema => $e) {
	// $cur_way = $dir.'chema/big/'.$img_chema;
	// $cur_way2 = $dir.'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 = $dir.'draft/big/'.$img_chema;
	// $cur_way2 = $dir.'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 = $dir.'catpreview/big/'.$img_chema;
	// if (!file_exists($cur_way)) {
		// p('no file '.$cur_way);
	// }
// }

// foreach($tree_params['pdf'] as $img_chema => $e) {
	// $cur_way = $dir.'downloads/'.$img_chema;
	// if (!file_exists($cur_way)) {
		// p('no file '.$cur_way);
	// }
// }

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) {
	$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,
		);
		

		$el = new CIBlockProperty;
		
		if($PRODUCT_ID = $el->Add($arFields)) {
			p("New ID: ".$PRODUCT_ID);
			$SORT += 1;
		} else {
			p("Error add: ".$el->LAST_ERROR);
		}
		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'] = 'Y';
			$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'].')');
			}
		}
	}
}

// die();

$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]);
}

// p($catalog_tree);


$good_tree = [];
$id_to_cat = [];
$root_id = 0;

foreach($catalog_tree as $type => $items) {
	foreach($items as $item) {
		$levels = explode('/', $item['url']);
		// p($levels);
		$way = array_slice($levels, 2, count($levels)-3);
		// p($way);
		$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!');
		}
		$id_to_cat[$last_level]['child'][$item['id']] = $item;
		// break;
	}
}

// p($good_tree);



function add_cat($section, $id, $root_section) {
	
	$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',
	);
	
	if (!empty($section['params'])) {
		foreach($section['params'] as $param) {
			$param['pname'] = strtoupper($param['pname']);
			$arFields['UF_'.$param['pname']] = $param['pvalue'];
		}
	}

	p('-----------------------------------------------------------------------');
	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');
		if (!empty($section['child'])) {
			foreach($section['child'] as $child_id => $child_data) {
				add_cat($child_data, $child_id, $RESULT_ID);
			}
		}
	}
}

if (false) {
	$root_section = false;
	foreach($good_tree['100203']['child'] as $id => $item) {
		add_cat($item, $id, $root_section);
	}

	CIBlockSection::ReSort(9);
}


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;
		
		// p($made_cats[$root_section]);
		// p($root_section);
		p($element);
		
		$el = new CIBlockElement;
			
		$arLoadProductArray = Array(
			"IBLOCK_SECTION_ID" => $made_cats[$root_section],
			"IBLOCK_ID"      => 9,
			"NAME"           => strlen($element['fullname']) > 0 ? $element['fullname'] : '[empty]',
			"CODE"           => $element['id'],
			"XML_ID"         => $element['id'],
			"ACTIVE"         => 'Y',
			"SORT"    => '500',
			"DETAIL_TEXT"    => $element['document'],
			"DETAIL_TEXT_TYPE"    => 'html',
			"PROPERTY_VALUES"    => [],
		);
		
		foreach($element['params'] as $param) {
			$pname = strtoupper($param['pname']);
			$arLoadProductArray["PROPERTY_VALUES"][$pname] = $param['pvalue'];
		}
		
		if (isset($made_els[$arLoadProductArray["CODE"]])) {
			
			// p($arLoadProductArray);
			
			$arUpdate = [];
			if (!empty($arLoadProductArray["PROPERTY_VALUES"]['TYPE'])) {
				$arUpdate['TYPE'] = $made_cats[$arLoadProductArray["PROPERTY_VALUES"]['TYPE']];
				// p('ex');
			} else {
				$arUpdate['TYPE'] = false;
			}
			
			// p('CODE');
			// p($arLoadProductArray["CODE"]);
			// p('ID');
			// p($made_els[$arLoadProductArray["CODE"]]);
			// p('update');
			// p($arUpdate);
			
			CIBlockElement::SetPropertyValuesEx($made_els[$arLoadProductArray["CODE"]], $arLoadProductArray["IBLOCK_ID"], $arUpdate);
			
		} 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) {
	
	$made_cats = [];
	
	$arFilter = Array("IBLOCK_ID"=>9);
	$arSelect = Array("ID", "NAME", "CODE", "XML_ID");
	$res = CIBlockSection::GetList(Array(), $arFilter, false, $arSelect);  
	while($arFields = $res->Fetch()) {
		$made_cats[$arFields['CODE']] = $arFields['ID'];
	}
	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);
	
	$root_section = false;
	foreach($good_tree['100203']['child'] as $id => $item) {
		add_item($item, $id, $root_section);
	}
}

// CIBlockSection::ReSort(9);

?>


<?
// require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");
?>