<?php
/**
 * Information systems.
 *
 * @package HostCMS
 * @version 7.x
 * @copyright © 2005-2026, https://www.hostcms.ru
 */
require_once('../../../../bootstrap.php');

Core_Auth::authorization($sModule = 'informationsystem');

$oInformationsystem = Core_Entity::factory('Informationsystem', Core_Array::getRequest('informationsystem_id', 0));
$oInformationsystem_Dir = $oInformationsystem->Informationsystem_Dir;
$informationsystem_group_id = Core_Array::getRequest('informationsystem_group_id', 0);
$oInformationsystem_Group = Core_Entity::factory('Informationsystem_Group', $informationsystem_group_id);
$oAdmin_Form_Controller = Admin_Form_Controller::create();
$oAdmin_Form_Entity_Breadcrumbs = Admin_Form_Entity::factory('Breadcrumbs');

// Контроллер формы
$oAdmin_Form_Controller
	->module(Core_Module_Abstract::factory($sModule))
	->setUp()
	->path('/{admin}/informationsystem/item/import/index.php');

ob_start();

$oAdmin_View = Admin_View::create();
$oAdmin_View
	->module(Core_Module_Abstract::factory($sModule))
	->pageTitle(Core::_('Informationsystem_Item.import'));

// Первая крошка на список магазинов
$oAdmin_Form_Entity_Breadcrumbs->add(
	Admin_Form_Entity::factory('Breadcrumb')
		->name(Core::_('Informationsystem.menu'))
		->href($oAdmin_Form_Controller->getAdminLoadHref(
			'/{admin}/informationsystem/index.php'
		))
		->onclick($oAdmin_Form_Controller->getAdminLoadAjax(
			'/{admin}/informationsystem/index.php'
		))
);

// Крошки по директориям магазинов
if ($oInformationsystem_Dir->id)
{
	$oInformationsystemDirBreadcrumbs = $oInformationsystem_Dir;

	$aBreadcrumbs = array();

	do
	{
		$aBreadcrumbs[] = Admin_Form_Entity::factory('Breadcrumb')
		->name($oInformationsystemDirBreadcrumbs->name)
		->href($oAdmin_Form_Controller->getAdminLoadHref(
				'/{admin}/informationsystem/index.php', NULL, NULL, "informationsystem_dir_id={$oInformationsystemDirBreadcrumbs->id}"
		))
		->onclick($oAdmin_Form_Controller->getAdminLoadAjax(
				'/{admin}/informationsystem/index.php', NULL, NULL, "informationsystem_dir_id={$oInformationsystemDirBreadcrumbs->id}"
		));
	}
	while ($oInformationsystemDirBreadcrumbs = $oInformationsystemDirBreadcrumbs->getParent());

	$aBreadcrumbs = array_reverse($aBreadcrumbs);

	foreach ($aBreadcrumbs as $oBreadcrumb)
	{
		$oAdmin_Form_Entity_Breadcrumbs->add($oBreadcrumb);
	}
}

// Крошка на список товаров и групп товаров магазина
$oAdmin_Form_Entity_Breadcrumbs->add(
Admin_Form_Entity::factory('Breadcrumb')
	->name($oInformationsystem->name)
	->href($oAdmin_Form_Controller->getAdminLoadHref(
			'/{admin}/informationsystem/item/index.php', NULL, NULL, "informationsystem_id={$oInformationsystem->id}"
	))
	->onclick($oAdmin_Form_Controller->getAdminLoadAjax(
			'/{admin}/informationsystem/item/index.php', NULL, NULL, "informationsystem_id={$oInformationsystem->id}"
	))
);

// Крошки по группам товаров
if ($oInformationsystem_Group->id)
{
	$oInformationsystemGroupBreadcrumbs = $oInformationsystem_Group;

	$aBreadcrumbs = array();

	do
	{
		$aBreadcrumbs[] = Admin_Form_Entity::factory('Breadcrumb')
			->name($oInformationsystemGroupBreadcrumbs->name)
			->href($oAdmin_Form_Controller->getAdminLoadHref(
				'/{admin}/informationsystem/item/index.php', NULL, NULL, "informationsystem_id={$oInformationsystem->id}&informationsystem_group_id={$oInformationsystemGroupBreadcrumbs->id}"
			))
			->onclick($oAdmin_Form_Controller->getAdminLoadAjax(
				'/{admin}/informationsystem/item/index.php', NULL, NULL, "informationsystem_id={$oInformationsystem->id}&informationsystem_group_id={$oInformationsystemGroupBreadcrumbs->id}"
			));
	}
	while ($oInformationsystemGroupBreadcrumbs = $oInformationsystemGroupBreadcrumbs->getParent());

	$aBreadcrumbs = array_reverse($aBreadcrumbs);

	foreach ($aBreadcrumbs as $oBreadcrumb)
	{
		$oAdmin_Form_Entity_Breadcrumbs->add($oBreadcrumb);
	}
}

// Крошка на текущую форму
$oAdmin_Form_Entity_Breadcrumbs->add(
Admin_Form_Entity::factory('Breadcrumb')
	->name(Core::_('Informationsystem_Item.import'))
	->href($oAdmin_Form_Controller->getAdminLoadHref(
		$oAdmin_Form_Controller->getPath(), NULL, NULL, "informationsystem_id={$oInformationsystem->id}&informationsystem_group_id={$oInformationsystem_Group->id}"
	))
	->onclick($oAdmin_Form_Controller->getAdminLoadAjax(
		$oAdmin_Form_Controller->getPath(), NULL, NULL, "informationsystem_id={$oInformationsystem->id}&informationsystem_group_id=	{$oInformationsystem_Group->id}"
	))
);

// Формируем массивы данных
$aLangConstNames = array(
	Core::_('Informationsystem_Exchange.!download'),
	Core::_('Informationsystem_Exchange.group_id'),
	Core::_('Informationsystem_Exchange.group_name'),
	Core::_('Informationsystem_Exchange.group_path'),
	Core::_('Informationsystem_Exchange.group_sorting'),
	Core::_('Informationsystem_Exchange.group_description'),
	Core::_('Informationsystem_Exchange.group_seo_title'),
	Core::_('Informationsystem_Exchange.group_seo_description'),
	Core::_('Informationsystem_Exchange.group_seo_keywords'),
	Core::_('Informationsystem_Exchange.group_image_large'),
	Core::_('Informationsystem_Exchange.group_image_small'),
	Core::_('Informationsystem_Exchange.group_active'),
	Core::_('Informationsystem_Exchange.group_guid'),
	Core::_('Informationsystem_Exchange.group_parent_guid'),

	Core::_('Informationsystem_Exchange.item_id'),
	Core::_('Informationsystem_Exchange.item_name'),
	Core::_('Informationsystem_Exchange.item_datetime'),
	Core::_('Informationsystem_Exchange.item_description'),
	Core::_('Informationsystem_Exchange.item_text'),
	Core::_('Informationsystem_Exchange.item_image_large'),
	Core::_('Informationsystem_Exchange.item_image_small'),
	Core::_('Informationsystem_Exchange.item_tags'),
	Core::_('Informationsystem_Exchange.item_active'),
	Core::_('Informationsystem_Exchange.item_sorting'),
	Core::_('Informationsystem_Exchange.item_path'),
	Core::_('Informationsystem_Exchange.item_seo_title'),
	Core::_('Informationsystem_Exchange.item_seo_description'),
	Core::_('Informationsystem_Exchange.item_seo_keywords'),
	Core::_('Informationsystem_Exchange.item_indexing'),
	Core::_('Informationsystem_Exchange.item_end_datetime'),
	Core::_('Informationsystem_Exchange.item_start_datetime'),
	Core::_('Informationsystem_Exchange.item_additional_group'),
	Core::_('Informationsystem_Exchange.item_guid'),
	Core::_('Informationsystem_Exchange.item_siteuser_id'),
);

$aColors = array(
	'#F5F5F5',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',
	'#E7A1B0',

	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
	'#92C7C7',
);

$aEntities = array(
	'',
	'informationsystem_groups_id',
	'informationsystem_groups_value',
	'informationsystem_groups_path',
	'informationsystem_groups_order',
	'informationsystem_groups_description',
	'informationsystem_groups_seo_title',
	'informationsystem_groups_seo_description',
	'informationsystem_groups_seo_keywords',
	'informationsystem_groups_image',
	'informationsystem_groups_small_image',
	'informationsystem_groups_activity',
	'informationsystem_groups_guid',
	'informationsystem_groups_parent_guid',

	'informationsystem_items_item_id',
	'informationsystem_items_name',
	'informationsystem_items_date_time',
	'informationsystem_items_description',
	'informationsystem_items_text',
	'informationsystem_items_image',
	'informationsystem_items_small_image',
	'informationsystem_items_label',
	'informationsystem_items_is_active',
	'informationsystem_items_order',
	'informationsystem_items_path',
	'informationsystem_items_seo_title',
	'informationsystem_items_seo_description',
	'informationsystem_items_seo_keywords',
	'informationsystem_items_indexation',
	'informationsystem_items_putend_date',
	'informationsystem_items_putoff_date',
	'additional_group',
	'informationsystem_items_guid',
	'site_users_id',
);

$aGroupProperties = Core_Entity::factory('Informationsystem_Group_Property_List', $oInformationsystem->id)->Properties->findAll();
foreach ($aGroupProperties as $oGroupProperty)
{
	$oPropertyDir = $oGroupProperty->Property_Dir;

	$aLangConstNames[] = $oGroupProperty->name . " [" . ($oPropertyDir->id ? $oPropertyDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
	$aColors[] = "#E6E6FA";
	$aEntities[] = 'prop_group-' . $oGroupProperty->id;

	if ($oGroupProperty->type == 2)
	{
		// Description
		$aLangConstNames[] = Core::_('Informationsystem_Item.import_file_description', $oGroupProperty->name) . " [" . ($oPropertyDir->id ? $oPropertyDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
		$aColors[] = "#E6E6FA";
		$aEntities[] = 'propdesc-' . $oGroupProperty->id;

		// Small Image
		$aLangConstNames[] = Core::_('Informationsystem_Item.import_small_images', $oGroupProperty->name) .  " [" . ($oPropertyDir->id ? $oPropertyDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
		$aColors[] = "#E6E6FA";
		$aEntities[] = 'propsmall-' . $oGroupProperty->id;
	}
}

$aItemProperties = Core_Entity::factory('Informationsystem_Item_Property_List', $oInformationsystem->id)->Properties->findAll();
foreach ($aItemProperties as $oItemProperty)
{
	$oPropertyDir = $oItemProperty->Property_Dir;

	$aLangConstNames[] = $oItemProperty->name . " [" . ($oPropertyDir->id ? $oPropertyDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
	$aColors[] = "#FFE4E1";
	$aEntities[] = 'prop-' . $oItemProperty->id;

	if ($oItemProperty->type == 2)
	{
		// Description
		$aLangConstNames[] = Core::_('Informationsystem_Item.import_file_description', $oItemProperty->name) . " [" . ($oPropertyDir->id ? $oPropertyDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
		$aColors[] = "#FFE4E1";
		$aEntities[] = 'propdesc-' . $oItemProperty->id;

		// Small Image
		$aLangConstNames[] = Core::_('Informationsystem_Item.import_small_images', $oItemProperty->name) . " [" . ($oPropertyDir->id ? $oPropertyDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
		$aColors[] = "#FFE4E1";
		$aEntities[] = 'propsmall-' . $oItemProperty->id;
	}
}

if (Core::moduleIsActive('field'))
{
	$aGroupFields = Field_Controller::getFields('informationsystem_group', $oInformationsystem->site_id);
	foreach ($aGroupFields as $oGroupField)
	{
		$oFieldDir = $oGroupField->Field_Dir;

		$aLangConstNames[] = $oGroupField->name . " [" . ($oFieldDir->id ? $oFieldDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
		$aColors[] = "#BBCEF2";
		$aEntities[] = 'field_group-' . $oGroupField->id;

		if ($oGroupField->type == 2)
		{
			// Description
			$aLangConstNames[] = Core::_('Informationsystem_Item.import_file_description', $oGroupField->name) . " [" . ($oFieldDir->id ? $oFieldDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
			$aColors[] = "#BBCEF2";
			$aEntities[] = 'fielddesc-' . $oGroupField->id;

			// Small Image
			$aLangConstNames[] = Core::_('Informationsystem_Item.import_small_images', $oGroupField->name) .  " [" . ($oFieldDir->id ? $oFieldDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
			$aColors[] = "#BBCEF2";
			$aEntities[] = 'fieldsmall-' . $oGroupField->id;
		}
	}

	$aItemFields = Field_Controller::getFields('informationsystem_item', $oInformationsystem->site_id);
	foreach ($aItemFields as $oItemField)
	{
		$oFieldDir = $oItemField->Field_Dir;

		$aLangConstNames[] = $oItemField->name . " [" . ($oFieldDir->id ? $oFieldDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
		$aColors[] = "#BBCEF2";
		$aEntities[] = 'field-' . $oItemField->id;

		if ($oItemField->type == 2)
		{
			// Description
			$aLangConstNames[] = Core::_('Informationsystem_Item.import_file_description', $oItemField->name) . " [" . ($oFieldDir->id ? $oFieldDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
			$aColors[] = "#BBCEF2";
			$aEntities[] = 'fielddesc-' . $oItemField->id;

			// Small Image
			$aLangConstNames[] = Core::_('Informationsystem_Item.import_small_images', $oItemField->name) . " [" . ($oFieldDir->id ? $oFieldDir->name : Core::_('Informationsystem_Item.root_folder')) . "]";
			$aColors[] = "#BBCEF2";
			$aEntities[] = 'fieldsmall-' . $oItemField->id;
		}
	}
}

$oUserCurrent = Core_Auth::getCurrentUser();

$oAdmin_Form_Entity_Form = Admin_Form_Entity::factory('Form')
	->controller($oAdmin_Form_Controller)
	->action($oAdmin_Form_Controller->getPath())
	->enctype('multipart/form-data');

$oAdmin_View->addChild($oAdmin_Form_Entity_Breadcrumbs);

$windowId = $oAdmin_Form_Controller->getWindowId();

// Количество полей
$iFieldCount = 0;

$sOnClick = NULL;

if ($oAdmin_Form_Controller->getAction() == 'show_form')
{
	if (!$oUserCurrent->read_only && !$oUserCurrent->only_access_my_own)
	{
		/*$sFileName = isset($_FILES['csv_file']) && intval($_FILES['csv_file']['size']) > 0
			? $_FILES['csv_file']['tmp_name']
			: CMS_FOLDER . Core_Array::getPost('alternative_file_pointer');*/

		$sFileName = $sTmpPath = NULL;

		// Uploaded File
		if (isset($_FILES['csv_file']) && intval($_FILES['csv_file']['size']) > 0)
		{
			$sFileName = $_FILES['csv_file']['tmp_name'];
		}
		// External file
		else
		{
			$altFile = Core_Array::getPost('alternative_file_pointer');

			if (strpos($altFile, 'http://') === 0 || strpos($altFile, 'https://') === 0)
			{
				$Core_Http = Core_Http::instance('curl')
					->url($altFile)
					->port(80)
					->timeout(20)
					->execute();

				$aHeaders = $Core_Http->parseHeaders();

				if ($Core_Http->parseHttpStatusCode($aHeaders['status']) != 200)
				{
					Core_Message::show('Wrong status: ' . htmlspecialchars($aHeaders['status']), "error");
				}

				$sFileName = $sTmpPath = tempnam(CMS_FOLDER . TMP_DIR, 'CSV');

				Core_File::write($sTmpPath, $Core_Http->getDecompressedBody());
			}
			else
			{
				$sFileName = CMS_FOLDER . $altFile;
			}
		}

		if (Core_File::isFile($sFileName) && is_readable($sFileName))
		{
			Core_Event::notify('Informationsystem_Item_Import.oBeforeImportCSV', NULL, array($sFileName));

			// Обработка CSV-файла
			$sTmpFileName = CMS_FOLDER . TMP_DIR . 'file_' . time() . '.csv';

			try {
				Core_File::upload($sFileName, $sTmpFileName);

				if ($fInputFile = fopen($sTmpFileName, 'rb'))
				{
					$sSeparator = Core_Array::getPost('import_separator');

					switch ($sSeparator)
					{
						case 0:
							$sSeparator = ',';
						break;
						case 1:
						default:
							$sSeparator = ';';
						break;
						case 2:
							$sSeparator = "\t";
						break;
						case 3:
							$sSeparator = Core_Array::getPost('import_separator_text');
						break;
					}

					$sLimiter = Core_Array::getPost('limiter');

					switch ($sLimiter)
					{
						case 0:
						default:
							$sLimiter = '"';
						break;
						case 1:
							$sLimiter = Core_Array::getPost('limiter');
						break;
					}

					$sLocale = Core_Array::getPost('import_encoding');
					$oInformationsystem_Item_Import_Csv_Controller = new Informationsystem_Item_Import_Csv_Controller(
						$oInformationsystem->id,
						Core_Array::getPost('informationsystem_groups_parent_id', 0)
					);

					$oInformationsystem_Item_Import_Csv_Controller
						->encoding($sLocale)
						->separator($sSeparator)->limiter($sLimiter);

					$aCsvLine = $oInformationsystem_Item_Import_Csv_Controller->getCSVLine($fInputFile);

					$iFieldCount = is_array($aCsvLine) ? count($aCsvLine) : 0;

					fclose($fInputFile);

					if ($iFieldCount)
					{
						$iValuesCount = count($aLangConstNames);

						$pos = 0;

						$oMainTab = Admin_Form_Entity::factory('Tab')->name('main');

						$oMainTab->add(
							Admin_Form_Entity::factory('Div')
								->class('row')
								->add(
									Admin_Form_Entity::factory('Span')
										->value(Core::_('shop_exchange.clear_matches'))
										->divAttr(array('class' => 'col-xs-12'))
										->class('pull-right pointer underline-dashed darkgray margin-right-20')
										->onclick('$("#' . $windowId . ' select[id^=field_id_]").val("").trigger("change")')
								)
						);

						for($i = 0; $i < $iFieldCount; $i++)
						{
							$oCurrentRow = Admin_Form_Entity::factory('Div')
								->class('row')
								->add(Admin_Form_Entity::factory('Span')
									->value($aCsvLine[$i])
									->divAttr(array('class' => 'form-group col-xs-12 col-sm-6'))
								);

							$aOptions = array();

							$isset_selected = FALSE;

							// Генерируем выпадающий список с цветными элементами
							for($j = 0; $j < $iValuesCount; $j++)
							{
								$aCsvLine[$i] = trim($aCsvLine[$i]);

								if (!$isset_selected
								&& (mb_strtolower($aCsvLine[$i]) == mb_strtolower($aLangConstNames[$j])
								|| (strlen($aLangConstNames[$j]) > 0
								&& strlen($aCsvLine[$i]) > 0
								&&
								(strpos($aCsvLine[$i], $aLangConstNames[$j]) !== FALSE
								|| strpos($aLangConstNames[$j], $aCsvLine[$i]) !== FALSE)
								// Чтобы не было срабатывания "Город" -> "Городской телефон"
								// Если есть целиком подходящее поле
								&& !array_search($aCsvLine[$i], $aLangConstNames))
								))
								{
									$selected = $aEntities[$j];

									// Для исключения двойного указания selected для одного списка
									$isset_selected = TRUE;
								}
								elseif (!$isset_selected)
								{
									$selected = -1;
								}

								$aOptions[$aEntities[$j]] = array(
									'value' => $aLangConstNames[$j],
									'attr' => array('style' => 'background-color: ' . (!empty($aColors[$pos]) ? $aColors[$j] : '#F5F5F5'))
								);

								$pos++;
							}

							$pos = 0;

							$oCurrentRow
								->class('d-flex align-items-center import-row')
								->add(Core_Html_Entity::factory('I')->class('fa-solid fa-circle fa-xs'))
								->add(
									Admin_Form_Entity::factory('Select')
										->name("field{$i}")
										->options($aOptions)
										->value($selected)
										->divAttr(array('class' => 'form-group col-xs-12 col-sm-6'))
										->class('form-control import-select')
										->onchange('setIColor(this)')
								);

							$oMainTab->add($oCurrentRow);
						}

						$oMainTab->add(Admin_Form_Entity::factory('Div')->class('row')
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('informationsystem_group_id')->value($oInformationsystem_Group->id))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('csv_filename')->value($sTmpFileName))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('import_separator')->value($sSeparator))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('limiter')->value($sLimiter))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('firstlineheader')->value(isset($_POST['firstlineheader']) ? 1 : 0))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('locale')->value($sLocale))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('import_max_time')->value(Core_Array::getPost('import_max_time')))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('import_max_count')->value(Core_Array::getPost('import_max_count')))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('import_load_files_path')->value(Core_Array::getPost('import_load_files_path')))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('import_action_items')->value(Core_Array::getPost('import_action_items')))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('informationsystem_groups_parent_id')->value(Core_Array::getPost('informationsystem_groups_parent_id')))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('search_event_indexation')->value(isset($_POST['search_event_indexation']) ? 1 : 0))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('import_action_delete_image')->value(isset($_POST['import_action_delete_image']) ? 1 : 0))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('delete_property_values')->value(isset($_POST['delete_property_values']) ? 1 : 0))
							->add(Core_Html_Entity::factory('Input')->type('hidden')->name('delete_field_values')->value(isset($_POST['delete_field_values']) ? 1 : 0))
						);

						$oAdmin_Form_Entity_Form->add($oMainTab);
					}
					else
					{
						throw new Core_Exception("File is empty!");
					}
				}
				else
				{
					throw new Core_Exception("Can't open file");
				}

			} catch (Exception $exc) {
				Core_Message::show($exc->getMessage(), "error");
			}

			$sOnClick = $oAdmin_Form_Controller->getAdminSendForm('start_import');
		}
		else
		{
			Core_Message::show(Core::_('Informationsystem_Item.file_does_not_specified'), "error");
			$sOnClick = "";
		}
	}
	else
	{
		Core_Message::show(Core::_('User.demo_mode'), "error");
	}
}
elseif ($oAdmin_Form_Controller->getAction() == 'start_import')
{
	if (!$oUserCurrent->read_only)
	{
		Core_Session::start();

		if (isset($_SESSION['Informationsystem_Item_Import_Csv_Controller']))
		{
			$Informationsystem_Item_Import_Csv_Controller = $_SESSION['Informationsystem_Item_Import_Csv_Controller'];
			unset($_SESSION['Informationsystem_Item_Import_Csv_Controller']);

			$iNextSeekPosition = $Informationsystem_Item_Import_Csv_Controller->seek;
		}
		else
		{
			$Informationsystem_Item_Import_Csv_Controller = new Informationsystem_Item_Import_Csv_Controller(Core_Array::getRequest('informationsystem_id', 0), Core_Array::getRequest('informationsystem_groups_parent_id', 0));

			$aConformity = array();

			foreach ($_POST as $iKey => $sValue)
			{
				if (strpos($iKey, "field") === 0)
				{
					$aConformity[] = $sValue;
				}
			}

			$iNextSeekPosition = 0;

			$sCsvFilename = basename(Core_Array::getPost('csv_filename', '', 'str'));

			$Informationsystem_Item_Import_Csv_Controller
				->file($sCsvFilename)
				->encoding(Core_Array::getPost('locale', 'UTF-8'))
				->csv_fields($aConformity)
				->time(Core_Array::getPost('import_max_time'))
				->step(Core_Array::getPost('import_max_count'))
				->separator(Core_Array::getPost('import_separator'))
				->limiter(Core_Array::getPost('limiter'))
				->imagesPath(Core_Array::getPost('import_load_files_path'))
				->importAction(Core_Array::getPost('import_action_items'))
				->searchIndexation(Core_Array::getPost('search_event_indexation'))
				->deleteImage(Core_Array::getPost('import_action_delete_image'))
				->deletePropertyValues(Core_Array::getPost('delete_property_values') == 1)
				->deleteFieldValues(Core_Array::getPost('delete_field_values') == 1)
			;

			if (Core_Array::getPost('firstlineheader', 0))
			{
				$fInputFile = fopen($Informationsystem_Item_Import_Csv_Controller->getFilePath(), 'rb');
				@fgetcsv($fInputFile, 0, $Informationsystem_Item_Import_Csv_Controller->separator, $Informationsystem_Item_Import_Csv_Controller->limiter, "\\");
				$iNextSeekPosition = ftell($fInputFile);
				fclose($fInputFile);
			}
		}

		$Informationsystem_Item_Import_Csv_Controller->seek = $iNextSeekPosition;

		ob_start();

		if (($iNextSeekPosition = $Informationsystem_Item_Import_Csv_Controller->import()) !== FALSE)
		{
			$Informationsystem_Item_Import_Csv_Controller->seek = $iNextSeekPosition;

			if ($Informationsystem_Item_Import_Csv_Controller->importAction == 3)
			{
				$Informationsystem_Item_Import_Csv_Controller->importAction = 1;
			}

			$_SESSION['Informationsystem_Item_Import_Csv_Controller'] = $Informationsystem_Item_Import_Csv_Controller;

			$sRedirectAction = $oAdmin_Form_Controller->getAdminLoadAjax('/{admin}/informationsystem/item/import/index.php', 'start_import', NULL, "informationsystem_id={$oInformationsystem->id}&informationsystem_group_id={$informationsystem_group_id}");

			showStat($Informationsystem_Item_Import_Csv_Controller);
		}
		else
		{
			$sRedirectAction = "";
			Core_Message::show(Core::_('Informationsystem_Item.msg_download_complete'));
			showStat($Informationsystem_Item_Import_Csv_Controller);

			$Informationsystem_Item_Import_Csv_Controller->deleteUploadedFile();
		}

		$oAdmin_Form_Entity_Form->add(
			Admin_Form_Entity::factory('Code')->html(ob_get_clean())
		);

		Core_Session::close();

		if ($sRedirectAction)
		{
			$iRedirectTime = 1000;
			Core_Html_Entity::factory('Script')
				->type('text/javascript')
				->value('setTimeout(function (){ ' . $sRedirectAction . '}, ' . $iRedirectTime . ')')
				->execute();
		}

		$sOnClick = "";
	}
	else
	{
		Core_Message::show(Core::_('User.demo_mode'), "error");
	}
}
else
{
	$oMainTab = Admin_Form_Entity::factory('Tab')->name('main');

	$aConfig = Core_Config::instance()->get('informationsystem_csv', array()) + array(
		'maxTime' => 20,
		'maxCount' => 100,
		'separator' => ';',
		'limiter' => '"'
	);

	$aSeparator = array(
		0 => ',',
		1 => ';',
		2 => "\t"
	);

	$separator_text = '';
	$separatorOptionKey = array_search($aConfig['separator'], $aSeparator, TRUE);
	if ($separatorOptionKey === FALSE)
	{
		$separatorOptionKey = 3;
		$separator_text = $aConfig['separator'];
	}

	$aLimiter = array(
		0 => '"'
	);

	$limiter_text = '';
	$limiterOptionKey = array_search($aConfig['limiter'], $aLimiter, TRUE);
	if ($limiterOptionKey === FALSE)
	{
		$limiterOptionKey = 1;
		$limiter_text = $aConfig['limiter'];
	}

	$oAdmin_Form_Entity_Form->add(
		$oMainTab->add(
			Admin_Form_Entity::factory('Div')->class('row')->add(Admin_Form_Entity::factory('File')
				->name("csv_file")
				->caption(Core::_('Informationsystem_Item.import_list_file'))
				->largeImage(array('show_params' => FALSE))
				->smallImage(array('show' => FALSE))
				->divAttr(array('class' => 'col-xs-12 col-sm-6 col-md-5'))
			)
			->add(
				Admin_Form_Entity::factory('Input')
					->name("alternative_file_pointer")
					->divAttr(array('class' => 'form-group col-xs-12 col-sm-6 col-md-5'))
					->caption(Core::_('Informationsystem_Item.alternative_file_pointer_form_import'))
			)
			->add(Admin_Form_Entity::factory('Select')
				->name("import_encoding")
				->options(array(
					'UTF-8' => Core::_('Informationsystem_Item.input_file_encoding1'),
					'Windows-1251' => Core::_('Informationsystem_Item.input_file_encoding0')
				))
				->divAttr(array('class' => 'form-group col-xs-12 col-sm-4 col-md-2'))
				->caption(Core::_('Informationsystem_Item.import_encoding'))
			)
		)
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(Admin_Form_Entity::factory('Checkbox')
			->name("firstlineheader")
			->caption(Core::_('Informationsystem_Item.import_list_name_field_f'))
			->value(1)
			->checked(TRUE)
			->divAttr(array('class' => 'form-group col-xs-12 col-sm-6 ')))
		)
		->add(Admin_Form_Entity::factory('Div')->class('row')
			->add(Admin_Form_Entity::factory('Radiogroup')
				->radio(array(
					Core::_('Informationsystem_Item.import_separator1'),
					Core::_('Informationsystem_Item.import_separator2'),
					Core::_('Informationsystem_Item.import_separator3'),
					Core::_('Informationsystem_Item.import_separator4')
				))
				->ico(array(
					'fa-solid fa-asterisk fa-fw',
					'fa-solid fa-asterisk fa-fw',
					'fa-solid fa-asterisk fa-fw',
					'fa-solid fa-asterisk fa-fw'
				))
				->caption(Core::_('Informationsystem_Item.import_separator'))
				->divAttr(array('class' => 'no-padding-right form-group col-xs-10 col-sm-9 rounded-radio-group', 'id' => 'import_separator'))
				->name('import_separator')
				->value($separatorOptionKey)
				->add(
					Admin_Form_Entity::factory('Input')
						->name("import_separator_text")
						->value($separator_text)
						//->caption('&nbsp;')
						->size(3)
						->divAttr(array('class' => 'form-group d-inline-block margin-left-10'))
				)
			)
		)
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(
				Admin_Form_Entity::factory('Radiogroup')
					->radio(array(
						Core::_('Informationsystem_Item.limiter1'),
						Core::_('Informationsystem_Item.limiter2')
					))
					->ico(array(
						'fa-solid fa-quote-right fa-fw',
						'fa-solid fa-bolt fa-fw'
					))
					->caption(Core::_('Informationsystem_Item.limiter'))
					->name('limiter')
					->value($limiterOptionKey)
					->divAttr(array('class' => 'no-padding-right form-group col-xs-10 col-sm-9 rounded-radio-group'))
					->add(
						Admin_Form_Entity::factory('Input')
							->name("limiter")
							->value($limiter_text)
							//->caption('&nbsp;')
							->size(3)
							->divAttr(array('class' => 'form-group d-inline-block margin-left-10'))
					)
			)
		)
		->add(Admin_Form_Entity::factory('Div')->class('row')
			->add(Admin_Form_Entity::factory('Select')
			->name("informationsystem_groups_parent_id")
			->options(array(' … ') + Informationsystem_Item_Controller_Edit::fillInformationsystemGroup($oInformationsystem->id))
			->filter(TRUE)
			->divAttr(array('class' => 'form-group col-xs-12 col-sm-12'))
			->caption(Core::_('Informationsystem_Item.import_parent_group'))
			->value($oInformationsystem_Group->id)))
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(Admin_Form_Entity::factory('Input')
			->name("import_load_files_path")
			->divAttr(array('class' => 'form-group col-xs-12 col-sm-6'))
			->caption(Core::_('Informationsystem_Item.import_images_path'))))
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(
			Admin_Form_Entity::factory('Radiogroup')
				->radio(array(
					1 => Core::_('Informationsystem_Item.import_action_items1'),
					2 => Core::_('Informationsystem_Item.import_action_items2'),
					3 => Core::_('Informationsystem_Item.import_action_items0')
				))
				->ico(array(
					1 => 'fa-solid fa-refresh fa-fw',
					2 => 'fa-solid fa-ban fa-fw',
					3 => 'fa-solid fa-trash fa-fw',
				))
				->caption(Core::_('Informationsystem_Item.import_action_items'))
				->name('import_action_items')
				->divAttr(array('id' => 'import_action_items','class' => 'form-group col-xs-12 rounded-radio-group'))
				->value(1)
				->onclick("if (this.value == 3) { res = confirm('" . Core::_('Informationsystem_Item.empty_informationsystem') . "'); if (!res) { return false; } } ")
			)
		)
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(Admin_Form_Entity::factory('Code')
			->html("<script>$(function() {
				$('#{$windowId} #import_action_items').buttonset();
			});</script>")))
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(Admin_Form_Entity::factory('Checkbox')
			->name("import_action_delete_image")
			->caption(Core::_('Informationsystem_Item.import_action_delete_image'))
			->divAttr(array('id' => 'import_action_delete_image','class' => 'form-group col-xs-12'))))
		->add(
			Admin_Form_Entity::factory('Div')->class('row')->add(
				Admin_Form_Entity::factory('Checkbox')
					->name("delete_property_values")
					->class('form-control colored-danger times')
					->caption(Core::_('Informationsystem_Item.delete_property_values'))
					->divAttr(array('class' => 'form-group col-xs-12 col-md-6'))
					->value(1)
			)
			->add(
				Admin_Form_Entity::factory('Checkbox')
					->name("delete_field_values")
					->class('form-control colored-danger times')
					->caption(Core::_('Informationsystem_Item.delete_field_values'))
					->divAttr(array('class' => 'form-group col-xs-12 col-md-6'))
					->value(1)
			)
		)
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(Admin_Form_Entity::factory('Checkbox')
			->name("search_event_indexation")
			->caption(Core::_('Informationsystem_Item.search_event_indexation_import'))
			->divAttr(array('id' => 'search_event_indexation','class' => 'form-group col-xs-12'))))
		->add(Admin_Form_Entity::factory('Div')->class('row')->add(Admin_Form_Entity::factory('Input')
			->name("import_max_time")
			->caption(Core::_('Informationsystem_Item.import_max_time'))
			->value($aConfig['maxTime'])
			->divAttr(array('id' => 'import_max_time', 'class' => 'form-group col-xs-12 col-sm-6 col-md-3')))
			->add(Admin_Form_Entity::factory('Input')
			->name("import_max_count")
			->caption(Core::_('Informationsystem_Item.import_max_count'))
			->value($aConfig['maxCount'])
			->divAttr(array('id' => 'import_max_count', 'class' => 'form-group col-xs-12 col-sm-6 col-md-3')))))
	;

	$sOnClick = $oAdmin_Form_Controller->getAdminSendForm('show_form');

	Core_Session::start();
	unset($_SESSION['csv_params']);
	unset($_SESSION['Informationsystem_Item_Import_Csv_Controller']);
	Core_Session::close();
}

function showStat($Informationsystem_Item_Import_Csv_Controller)
{
	echo Core::_('Informationsystem_Item.count_insert_item') . ' &#151; <b>' . $Informationsystem_Item_Import_Csv_Controller->getInsertedItemsCount() . '</b><br/>';
	echo Core::_('Informationsystem_Item.count_update_item') . ' &#151; <b>' . $Informationsystem_Item_Import_Csv_Controller->getUpdatedItemsCount() . '</b><br/>';
	echo Core::_('Informationsystem_Item.create_catalog') . ' &#151; <b>' . $Informationsystem_Item_Import_Csv_Controller->getInsertedGroupsCount() . '</b><br/>';
	echo Core::_('Informationsystem_Item.update_catalog') . ' &#151; <b>' . $Informationsystem_Item_Import_Csv_Controller->getUpdatedGroupsCount() . '</b><br/>';
}

if ($sOnClick)
{
	$oAdmin_Form_Entity_Form->add(
		Admin_Form_Entity::factory('Button')
		->name('show_form')
		->type('submit')
		->value(Core::_('Informationsystem_Item.import_button_load'))
		->class('applyButton btn btn-blue')
		->onclick($sOnClick)
	);
}

$oAdmin_Form_Entity_Form->add(
	Core_Html_Entity::factory('Script')
		->type("text/javascript")
		->value("(function($){
			$('select.import-select').each(function(index, obj){
				setIColor(obj);
			})
		})(jQuery);")
);

$oAdmin_Form_Entity_Form->execute();
$content = ob_get_clean();

ob_start();
$oAdmin_View
	->content($content)
	->show();

Core_Skin::instance()
	->answer()
	->ajax(Core_Array::getRequest('_', FALSE))
	//->content(iconv("UTF-8", "UTF-8//IGNORE//TRANSLIT", ob_get_clean()))
	->module($sModule)
	->content(ob_get_clean())
	->title(Core::_('Informationsystem_Item.import'))
	->execute();