Your IP : 216.73.216.86


Current Path : /var/www/homesaver/www/bitrix/modules/webdebug.excel/admin/
Upload File :
Current File : /var/www/homesaver/www/bitrix/modules/webdebug.excel/admin/_webdebug_excel_ajax_import.php.back

<?
define("NO_KEEP_STATISTIC", true); 
define("NOT_CHECK_PERMISSIONS", true);
define("PUBLIC_AJAX_MODE", true); 
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/webdebug.excel/prolog.php');
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/webdebug.excel/include.php");
IncludeModuleLangFile(__FILE__);

if (COption::GetOptionString('webdebug.excel','use_version')==2) {
	print '!!';
	?><script>WebdebugExcelStopImport("<?=GetMessage('WEBDEBUG_EXCEL_ERROR_VERSION')?>");</script><?
	die();
}

$Stop = false;
$StopMessage = false;
$ClearStatus = false;

$IsStart = isset($_REQUEST["start"]) && $_REQUEST["start"]=="Y";

$IterateItems = 0;
$Time1 = WebdebugExcelGetmicrotimeX();
if (trim($_REQUEST["excelfile"])!="" && substr($_REQUEST["excelfile"],0,1)=="/" && file_exists($_SERVER["DOCUMENT_ROOT"].$_REQUEST["excelfile"])) {
	if (trim($_REQUEST["profile"])!="" && is_numeric($_REQUEST["profile"]) && $_REQUEST["profile"]>0) {
		if (CModule::IncludeModule("webdebug.excel")) {
			$Excel = new CWebdebugExcel($_SERVER["DOCUMENT_ROOT"].$_REQUEST["excelfile"]);
			if ($IsStart) {
				$Excel->l(sprintf(GetMessage('WEBDEBUG_EXCEL_IMPORT_LOG_01') ,CUser::GetID(), CUser::GetLogin()));
			}
			$resProfile = CWebdebugExcelProfile::GetList(false,array("ID"=>$_REQUEST["profile"]));
			if ($arProfile = $resProfile->GetNext()) {
				if (isset($arProfile["PARAMS"]) && trim($arProfile["PARAMS"])!="") {
					$ProfileID = $arProfile["ID"];
					$Excel->l(sprintf(GetMessage('WEBDEBUG_EXCEL_IMPORT_LOG_01_PROFILE'),$ProfileID, $arProfile["NAME"]));
					$Excel->l(sprintf(GetMessage('WEBDEBUG_EXCEL_IMPORT_LOG_01_FILENAME'), $_REQUEST["excelfile"], CFile::FormatSize(filesize($_SERVER["DOCUMENT_ROOT"].$_REQUEST["excelfile"]))));
					if (isset($_REQUEST['clear-status']) && $_REQUEST['clear-status']=='Y') {
						unset($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]);
					}
					if (isset($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]) && is_array($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]) && !empty($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID])) {
						$IsStart = false;
					}
					$Breaked = false;
					$Cancel = false;
					if ($IsStart) {
            foreach(GetModuleEvents("webdebug.excel", "OnImportStart", true) as $arEvent) {
							if (ExecuteModuleEventEx($arEvent, array(&$arProfile))===false) $Cancel = true;
            }
					} else {
            foreach(GetModuleEvents("webdebug.excel", "OnImportContinue", true) as $arEvent) {
							if (ExecuteModuleEventEx($arEvent, array(&$arProfile))===false) $Cancel = true;
            }
					}
					$arProfile["PARAMS"] = unserialize($arProfile["~PARAMS"]);
					if ($IsStart) {
						if ($arProfile["PARAMS"]["DEACTIVE_MISSING"]=="Y" || $arProfile["PARAMS"]["ACTIVATE_IMPORTED"]=="Y") {
							$Excel->SetImportID($ProfileID);
						} else {
							$Excel->ClearImportID($ProfileID);
						}
					}
					if (!$arProfile["PARAMS"]["STEPTIME"] || $arProfile["PARAMS"]["STEPTIME"]<1) {
						die("Set steptime!");
					}
					set_time_limit($arProfile["PARAMS"]["STEPTIME"]+30);
					$TimeLimit = $arProfile["PARAMS"]["STEPTIME"];
					$arCurrencies = $Excel->GetCurrencyList();
					// Set params
					$SectionMethod = $arProfile["PARAMS"]["SECTION_METHOD"];
					if ($SectionMethod=="method2") {
						$arLevelColors = explode("\n",$arProfile["PARAMS"]["SECTION_COLOR"]);
						foreach ($arLevelColors as $ColorKey => $ColorVal) {
							$ColorVal = trim($ColorVal);
							if (substr($ColorVal,0,1)=="#") {
								$ColorVal = substr($ColorVal,1);
							}
							$arLevelColors[$ColorKey] = ToUpper($ColorVal);
						}
					}
					// Start process
					$arSheets = $Excel->GetSheetList(false);
					if (!$Cancel) {
						foreach ($arSheets as $SheetIndex => $SheetName) {
							$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["CURRENT_SHEET"] = $SheetName;
							$arLastSections = array();
							$LastDepth = 0;
							if (isset($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID][$SheetIndex]["LAST_SECTIONS_DEPTH"]) && is_array($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID][$SheetIndex]["LAST_SECTIONS_DEPTH"])) {
								$arLastSections = $_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID][$SheetIndex]["LAST_SECTIONS_DEPTH"];
								$LastDepth = $_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID][$SheetIndex]["LAST_SECTIONS_DEPTH_LEVEL"];
							}
							if (!$Excel->GetSheetImported($ProfileID, $SheetIndex)) {
								$IBlockID = $arProfile["PARAMS"]["IBLOCK_ID_FOR_SHEET_".$SheetIndex];
								$arIBlockFields = CWebdebugExcel::GetIBlockFields($IBlockID);
								$arIBlockTranslitParamsElement = CWebdebugExcel::GetIBlockTranslitSettings($arIBlockFields);
								$arIBlockTranslitParamsSection = CWebdebugExcel::GetIBlockTranslitSettings($arIBlockFields, true);
								$HeaderColumn = $arProfile["PARAMS"]["HEADER_COLUMN_".$SheetIndex];
								$HeaderRow = $arProfile["PARAMS"]["HEADER_ROW_".$SheetIndex];
								$ElementColumn = $arProfile["PARAMS"]["ELEMENT_COLUMN_".$SheetIndex];
								$ElementRow = $arProfile["PARAMS"]["ELEMENT_ROW_".$SheetIndex];
								$HeaderUniq = $arProfile["PARAMS"]["HEADER_UNIQ_".$SheetIndex];
								// skip sheets if they're not set
								if ($arProfile["PARAMS"]["SHEET_".$SheetIndex."_ACTIVE"]!="Y"
									|| (!is_numeric($IBlockID) || $IBlockID<=0)
									|| (!is_numeric($HeaderColumn) || $HeaderColumn<=0)
									|| (!is_numeric($HeaderRow) || $HeaderRow<=0)
									|| (!is_numeric($ElementColumn) || $ElementColumn<=0)
									|| (!is_numeric($ElementRow) || $ElementRow<=0)
									|| (!is_numeric($HeaderUniq) || $HeaderUniq<0)
								) continue;
								if (($arProfile["PARAMS"]["DEACTIVE_MISSING"]=="Y" || $arProfile["PARAMS"]["ACTIVATE_IMPORTED"]=="Y") && $IBlockID) {
									CWebdebugExcel::AddImportIDProperty($IBlockID, false);
								} elseif ($IBlockID) {
									CWebdebugExcel::AddImportIDProperty($IBlockID, true);
								}
								$arHeaders = $Excel->GetSheetHeaders($SheetIndex, $HeaderColumn-1, $HeaderRow);
								$LineIndex = $ElementRow;
								$LineCount = $Excel->GetLinesCount($SheetIndex, $ElementRow);
								$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_ALL"] = $LineCount;
								// Get name column
								$NameColumn = false;
								foreach ($arHeaders as $arHeader) {
									if ($arProfile["PARAMS"]["SHEET_".$SheetIndex."_COLUMN_".$arHeader["COLUMN"]]=="NAME") {
										$NameColumn = $arHeader["COLUMN"];
										break;
									}
								}
								do {
									// skip done items
									if (!$Excel->GetLineImported($ProfileID, $SheetIndex, $LineIndex)) {
										// Get section by color method
										$LineIsSection = false;
										if ($SectionMethod=="method2" && $NameColumn) {
											$Color = $Excel->GetCellBackgroundColor($SheetIndex, $NameColumn, $LineIndex);
											if ($Color!="FFFFFF") {
												foreach ($arLevelColors as $LevelDepth => $LevelColor) {
													if ($LevelColor==$Color) {
														$arLastSections[$LevelDepth] = array(
															"LINE" => $LineIndex,
															"UNIQ" => $Excel->GetValue($SheetIndex, $HeaderUniq, $LineIndex),
															"NAME" => $Excel->GetValue($SheetIndex, $NameColumn, $LineIndex),
														);
														
														foreach ($arHeaders as $arHeader) {
															$SectionFieldCode = $arProfile["PARAMS"]["SHEET_".$SheetIndex."_S_COLUMN_".$arHeader["COLUMN"]];
															if ($SectionFieldCode!='') {
																$arLastSections[$LevelDepth]['DATA'][$SectionFieldCode] = $Excel->GetValue($SheetIndex, $arHeader["COLUMN"], $LineIndex);
															}
															/*
															if (trim($SectionFieldCode)!='') {
																$arLastSections[$LevelDepth][$SectionFieldCode] = '[OK]'.$Excel->GetValue($SheetIndex, $arHeader["COLUMN"], $LineIndex);
																$Excel->l('XXX:['.$arHeader["COLUMN"].'],['.$LineIndex.'],['.$SectionFieldCode.'],['.$arLastSections[$LevelDepth][$SectionFieldCode].']');
															}
															*/
														}
														
														/*
														foreach ($arColumns as $ColumnID => $arField) {
															$Separator = $arParams["SHEET_{$SheetIndex}_S_COLUMN_{$ColumnID}_SEPARATOR"];
															if ($arField["FIELD"]=="CODE") {
																$arFields["CODE"] = self::ProcessValue($arValues[$ColumnID],$arField["TYPE"],$Separator);
															} elseif ($arField["FIELD"]=="SORT") {
																$arFields["SORT"] = self::ProcessValue($arValues[$ColumnID],$arField["TYPE"],$Separator);
															} elseif ($arField["FIELD"]=="DETAIL_TEXT") {
																$arFields["DETAIL_TEXT"] = self::ProcessValue($arValues[$ColumnID],$arField["TYPE"],$Separator);
															} elseif ($arField["FIELD"]=="DETAIL_TEXT_TYPE") {
																$arFields["DETAIL_TEXT_TYPE"] = ToLower($arValues[$ColumnID]);
															} elseif ($arField["FIELD"]=="PREVIEW_PICTURE") {
																$arFields["PREVIEW_PICTURE"] = self::ProcessValue($arValues[$ColumnID],$arField["TYPE"],$Separator,"F");
															} elseif ($arField["FIELD"]=="DETAIL_PICTURE") {
																$arFields["DETAIL_PICTURE"] = self::ProcessValue($arValues[$ColumnID],$arField["TYPE"],$Separator,"F");
															}
														}
														*/
														
														#if (trim($arLastSections[$LevelDepth]['UNIQ'])=='') {
															#$arLastSections[$LevelDepth]['UNIQ'] = MD5($arLastSections[$LevelDepth]['NAME']);
														#}
														$LastDepth = $LevelDepth;
														$LineIsSection = true;
														continue;
													}
												}
												$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID][$SheetIndex]["LAST_SECTIONS_DEPTH"] = $arLastSections;
												// Get Uniq
												$MD5_Data = "";
												foreach ($arLastSections as $Key => $arSection) {
													$MD5_Data .= trim(ToLower($arSection["UNIQ"]));
													#$arLastSections[$Key]["UNIQ"] = MD5($MD5_Data);
												}
												$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID][$SheetIndex]["LAST_SECTIONS_DEPTH_LEVEL"] = $LastDepth;
											}
										}
										if (!$LineIsSection) {
											$arValues = array();
											foreach ($arHeaders as $arHeader) {
												$Value = $Excel->GetValue($SheetIndex,$arHeader["COLUMN"],$LineIndex,true,$arProfile['PARAMS']['USE_HTML_FORMATTING']=='Y');
												$arValues[$arHeader["COLUMN"]] = $Value;
											}
											$arColumns = array();
											foreach ($arHeaders as $arHeader) {
												$arColumns[$arHeader["COLUMN"]] = array(
													"FIELD" => $arProfile["PARAMS"]["SHEET_".$SheetIndex."_COLUMN_".$arHeader["COLUMN"]],
													"TYPE" => $arProfile["PARAMS"]["SHEET_".$SheetIndex."_COLUMN_".$arHeader["COLUMN"]."_TYPE"],
												);
											}
											// Get section by first method
											if ($SectionMethod=="method1") {
												$LastDepth = 0;
												$arSectionsDepth = array();
												for($i=1;$i<=IntVal($arProfile["PARAMS"]["MAX_DEPTH"]);$i++) {
													$SectionFieldFound = false;
													foreach ($arHeaders as $arHeader) {
														if ($arProfile["PARAMS"]["SHEET_".$SheetIndex."_COLUMN_".$arHeader["COLUMN"]]=="SECTION".$i) {
															$SectionName = $Excel->GetValue($SheetIndex, $arHeader["COLUMN"], $LineIndex);
															if ($SectionName=="") continue;
															$SectionCode = MD5($SectionName);
															$SectionCode = $Excel->Translit($SectionName);
															$SectionCode = $Excel->FindUniqueCode($SectionCode, $IBlockID, true);
															$arSectionsDepth[] = array(
																"LINE" => $LineIndex,
																#"UNIQ" =>$SectionCode,
																"NAME" => $SectionName,
															);
															$SectionFieldFound = true;
														}
													}
													$LastDepth++;
													if (!$SectionFieldFound) {
														break;
													}
												}
												$arLastSections = $arSectionsDepth;
												// Get Uniq 
												$MD5_Data = "";
												foreach ($arLastSections as $Key => $arSection) {
													$MD5_Data .= trim(ToLower(trim($arSection["NAME"])));
													$arLastSections[$Key]["UNIQ"] = MD5($MD5_Data);
												}
											}
											$arIBlockProps = CWebdebugExcel::GetIBlockProps($IBlockID,true);
											$Excel->ProcessLine($arValues, $arHeaders, $arColumns, $IBlockID, $arLastSections, $LastDepth+1, $HeaderUniq, $arProfile["PARAMS"], $arIBlockTranslitParamsElement, $arIBlockTranslitParamsSection, $arIBlockProps, $SheetIndex, $arCurrencies, $ProfileID, $LineIndex, $arIBlockFields);
											$IterateItems++;
											$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_DONE"] = IntVal($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_DONE"])+1;
											$Excel->SetLineImported($ProfileID, $SheetIndex, $LineIndex);
										}
									}
									$LineIndex++;
									// break, if time is over
									if (WebdebugExcelGetmicrotimeX()-$Time1>=$TimeLimit) {
										$Breaked = true;
										break 2;
									}
								} while ($LineIndex < $LineCount+$ElementRow);
								// Deactivate
								if ($arProfile["PARAMS"]["DEACTIVE_MISSING"]=="Y") {
									$resItems = CIBlockElement::GetList(array(),array("IBLOCK_ID"=>$IBlockID,"ACTIVE"=>"Y","!PROPERTY_WD_EXCEL_IMPORT_ID"=>CWebdebugExcel::GetImportID($ProfileID)),false,false,array("ID"));
									$IBlockElement = new CIBlockElement;
									while ($arItem = $resItems->GetNext(false,false)) {
										$arFields = array(
											"ACTIVE" => "N",
										);
										$IBlockElement->Update($arItem["ID"],$arFields);
										if (WebdebugExcelGetmicrotimeX()-$Time1>=$TimeLimit) {
											$Breaked = true;
											break;
										}
									}
								}
								// Activate
								if ($arProfile["PARAMS"]["ACTIVATE_IMPORTED"]=="Y") {
									$resItems = CIBlockElement::GetList(array(),array("IBLOCK_ID"=>$IBlockID,"ACTIVE"=>"N","PROPERTY_WD_EXCEL_IMPORT_ID"=>CWebdebugExcel::GetImportID($ProfileID)),false,false,array("ID"));
									$IBlockElement = new CIBlockElement;
									while ($arItem = $resItems->GetNext(false,false)) {
										$arFields = array(
											"ACTIVE" => "Y",
										);
										$IBlockElement->Update($arItem["ID"],$arFields);
										if (WebdebugExcelGetmicrotimeX()-$Time1>=$TimeLimit) {
											$Breaked = true;
											break;
										}
									}
								}
								$Excel->SetSheetImported($ProfileID, $SheetIndex);
							} else {
								$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_DONE"] = 0;
							}
						}
					}
					if ($Cancel) {
						$Stop = true;
						$StopMessage = GetMessage('WEBDEBUG_EXCEL_IMPORT_CANCELED');
						$ClearStatus = true;
					}
					if (!$Breaked && !$Cancel) {
						$Stop = true;
						$StopMessage = GetMessage('WEBDEBUG_EXCEL_IMPORT_DONE');
						// Clear all statuses (if fully completed)
						$ClearStatus = true;
						CWebdebugExcel::ClearImportID($ProfileID);
            foreach(GetModuleEvents("webdebug.excel", "OnImportDone", true) as $arEvent) {
							ExecuteModuleEventEx($arEvent, array($arProfile));
            }
						$Excel->l(GetMessage('WEBDEBUG_EXCEL_IMPORT_LOG_02'));
					}
				} else {
					$Stop = true;
					$StopMessage = GetMessage('WEBDEBUG_EXCEL_ERROR_PROFILE_NOT_SET');
				}
			} else {
				$Stop = true;
				$StopMessage = GetMessage('WEBDEBUG_EXCEL_ERROR_PROFILE_NOT_FOUND');
			}
		} else {
			$Stop = true;
			$StopMessage = GetMessage('WEBDEBUG_EXCEL_ERROR_NO_MODULE');
		}
	} else {
		$Stop = true;
		$StopMessage = GetMessage('WEBDEBUG_EXCEL_ERROR_NO_FILE');
	}
} else {
	$Stop = true;
	$StopMessage = GetMessage('WEBDEBUG_EXCEL_ERROR_WRONG_FILE');
}

?>

<?
	$Percent = "";
	if ($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_ALL"]>0) {
		$Percent = 100 * $_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_DONE"] / $_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_ALL"];
	}
	$Percent = " (".IntVal($Percent)."%)";
?>

<script type="text/javascript">
<?if($Stop):?>
	WebdebugExcelStopImport("<?=$StopMessage?><?=GetMessage('WEBDEBUG_EXCEL_STATUS_ERROS_COUNT')?><?=IntVal($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]['ERRORS_COUNT'])?>");
<?else:?>
	WebdebugExcelDoImport(0, true, "<b><?=$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["CURRENT_SHEET"]?></b>: <?=GetMessage('WEBDEBUG_EXCEL_STATUS_TEXT_LEFT')?> <?=$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_DONE"]?>/<?=$_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]["COUNT_ALL"]?> <?=GetMessage('WEBDEBUG_EXCEL_STATUS_TEXT_RIGHT')?><?=$Percent?><?=GetMessage('WEBDEBUG_EXCEL_STATUS_ERROS_COUNT')?><?=IntVal($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]['ERRORS_COUNT'])?>");
<?endif?>
</script>

<?
if($ClearStatus) {
	unset($_SESSION["WEBDEBUG"]["EXCEL"]["STATUS"][$ProfileID]);
}
?>