Your IP : 13.59.244.1


Current Path : /home/bitrix/ext_www/easy-comfort.com.ua/local/modules/webprofy.importer/
Upload File :
Current File : /home/bitrix/ext_www/easy-comfort.com.ua/local/modules/webprofy.importer/ajax.php

<?php
use Bitrix\Main\Loader;
use Webprofy\Logger;
use Webprofy\Reader;
use Webprofy\Import\Manager;

require_once($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_admin_before.php");

/**
 * Шаг 2, получение настроек. Удобнее получать через ajax, выбрав информацию о справочниках из конфига
 */
if($_POST['step_two'] && $_POST['template']){
	$templatePath = __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . $_POST['template'];
	Loader::includeModule('iblock');
	Loader::includeModule('highloadblock');
	if(file_exists($templatePath)){
		$fileData = json_decode(file_get_contents($templatePath), true);
		$hls = [];

		foreach($fileData['propertyGroups'] as $data){
			if($data['type'] == 'CATALOG'){
				$hls[$data['groupId']] = $data['groupName'];
			}
		}
	}

	$iblocksForLinks = Array();
	foreach($fileData['properties']['element'] as $element) {
		if($element['metadata']['type'] == 'link_to_element') {
			$iblocksForLinks[] = Array('name' => $element['display']['name'], 'code' => $element['display']['code']);
		}
	}

	if(!empty($hls)){
		$existsHls = \Bitrix\Highloadblock\HighloadBlockTable::getList()->fetchAll();
	}?>

	<h2>Шаг 3. Настройки</h2>
	<div>
		<h3>Выберите инфоблок</h3>
		<?= GetIBlockDropDownList(false, 'iblock_type', 'iblock_id', false, 'class="adm-detail-iblock-types"', 'class="reference"'); ?>
		<? if(count($iblocksForLinks) > 0) { ?>
			<br />
			<br />
			<h3>Инфоблоки для ссылок</h3>
			<p>В контент-плане найдены поля, которые являются ссылками на другие сущности, укажите с какими инфоблоками следует их связать.</p>
			<? foreach($iblocksForLinks as $iblock) { ?>
				<div style="margin-top: 15px;">
					<div style="margin: 10px 0 5px; font-weight: bold;"><?=$iblock['name']?>:</div>
					<?=GetIBlockDropDownList(false, 'iblock_type_link['.$iblock['code'].']', 'iblock_id_link['.$iblock['code'].']', false, 'class="adm-detail-iblock-types"', 'class="reference"');?>
				</div>
			<? } ?>
		<? } ?>
		<br />
		<br />
	</div>
	<? if(!empty($hls) && !empty($existsHls)){
		foreach($hls as $key => $item){
				?>
				<div>
					<h3>Выберите справочник для <?= $item ?></h3>
					<select name="<?= $key ?>" class="reference">
						<option value="">Не выбрано</option>
						<? foreach($existsHls as $existsHl){
							?>
							<option value="<?= $existsHl['ID'] ?>"><?= $existsHl['NAME'] ?></option>
						<?
						} ?>
					</select>
				</div>
			<?
		}
	}

	if(!empty($hls) && empty($existsHls)){
		echo '<p class="import-container__log-error">Необходимо создать ' .'<a href="/bitrix/admin/highloadblock_index.php" target="_blank">новый справочник и обновить страницу</a></p>';
	}
}

Loader::includeModule('webprofy.importer');
Logger::cleanLog();
if($_POST['file_xlsx']){
	set_time_limit(0);
	//Очистить лог

	//Новый файл, сохраняем в системе
	if(is_array($_POST['file_xlsx']) && isset($_POST['file_xlsx']['tmp_name'])){
		$fileId = \CFile::SaveFile(\CFile::MakeFileArray($_POST['file_xlsx']['tmp_name']), 'importer');
		$filePath = \CFile::GetPath($fileId);
	}else{
		$filePath = $_POST['file_xlsx'];
	}

//	unset($_POST['file_xlsx']);

	if(is_array($_POST['file_zip']) && isset($_POST['file_zip']['tmp_name'])){
		$_POST["file_zip"]["tmp_name"] = rtrim($_SERVER["DOCUMENT_ROOT"], "/") . $_POST["file_zip"]["tmp_name"];
		$zipFileId = \CFile::SaveFile($_POST["file_zip"], "importer");
		$zipFile = \CFile::GetPath($zipFileId);
	}else{
		$zipFile = ltrim($_POST['file_zip'], "/");
	}


	if(!empty($zipFile)){
		$zipFilePath = rtrim($_SERVER["DOCUMENT_ROOT"], "/") . DIRECTORY_SEPARATOR . $zipFile;
		$zip = new \ZipArchive;
		$unZipFilesPath = dirname($zipFilePath) . DIRECTORY_SEPARATOR;
		if ($zip->open($zipFilePath) === TRUE) {
			$zip->extractTo($unZipFilesPath);
			$zip->close();
		}
	}
	unset($_POST['file_zip']);


	//Получение данных из файла
	$data = Reader::getExcelData($_SERVER['DOCUMENT_ROOT'] . $filePath);

	//Получение настроек из конфига
	$config = Reader::getConfig(__DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . $_POST['template']);

	//Начало импорта
	$manager = new Manager($data, $config, $_POST, $unZipFilesPath);
	$manager->import();
}