<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");?>
<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/admin/import_xml/import.class.php");?>
<?
$step = !isset($_REQUEST['step']) ? $step = 'start' : $step = $_REQUEST['step'];
$page = !isset($_REQUEST['page']) ? $page = 0 : $page = $_REQUEST['page'];
switch($step)
{
case 'start':
$log = new ClImportConfig();
$log->SetTime('begin');
$log->ResetSession();
$log->SetTime('end');
$log->WriteLog('begin', $step, 'w+');
//header('refresh: '.$log->speed.'; url='.$log->projectUrl.'import.php?step=category_add_to_session');
header('refresh: '.$log->speed.'; url='.$log->projectUrl.'import.php?step=offers_add_to_session');
break;
case 'category_add_to_session':
$section = new ClImportCategory();
$section->SetTime('begin');
$section->AddCatsFromXml('session');
$section->SetTime('end');
$section->WriteLog('', $step, 'w+');
header('refresh: '.$log->speed.'; url='.$log->projectUrl.'import.php?step=catalog_get_parent_id');
break;
case 'catalog_get_parent_id':
$section = new ClImportCategory();
$section->SetTime('begin');
$section->GetArCatParentId();
$section->SetTime('end');
$section->WriteLog('', $step, 'w+');
header('refresh: '.$log->speed.'; url='.$log->projectUrl.'import.php?step=catalog_del_orphan');
break;
case 'catalog_del_orphan':
$section = new ClImportCategory();
$section->SetTime('begin');
$section->DelateCatOrphan();
unset($_SESSION['CATS']);
$_SESSION['CATS_WITHOUT_IDS'] = $_SESSION['CATS'] = $_SESSION['CATS_WITHOUT_ORPHAN'];
unset($_SESSION['CATS_WITHOUT_ORPHAN']);
$_SESSION['CATS_COUNT_DB'] = $_SESSION['CATS_COUNT_UPD'] = sizeof($_SESSION['CATS']);
// Формула вычисления значения переменной для выхода из цикла
$_SESSION['RESIDUE'] = $_SESSION['CATS_COUNT_DB'] - (($section->offset - 1) * intval($_SESSION['CATS_COUNT_DB']) / $section->offset) + 1;
$section->SetTime('end');
$section->WriteLog('', $step, 'w+');
header('refresh: '.$section->speed.'; url='.$log->projectUrl.'import.php?step=category_add_to_db');
break;
case 'category_add_to_db':
$section = new ClImportCategory();
$section->SetTime('begin');
foreach ($_SESSION['CATS_WITHOUT_IDS'] as $key => $arItem)
{
if (!in_array($arItem['ID'], $_SESSION['CAT_IDS']))
{
$_SESSION['CATS'][$key]['ID'] = $section->AddCats($arItem['NAME'], $arItem['UF_ID'], $arItem['UF_PARENT_ID']);
if ($_SESSION['CATS'][$key]['ID'] <> "")
{
$_SESSION['CAT_IDS'][] = $arItem['ID'];
}
if ($_SESSION['CATS_COUNT_DB'] < $_SESSION['RESIDUE'])
{
unset ($_SESSION['CATS_IDS']);
unset ($_SESSION['CATS_COUNT_DB']);
$_SESSION['COUNTER'] = 0;
$step = 'update_relation_category';
break;
}
if ($_SESSION['COUNTER'] >= $section->offset)
{
$_SESSION['COUNTER'] = 0;
$page++;
$step = 'category_add_to_db&page='.$page;
break;
}
$_SESSION['CATS_COUNT_DB']--;
$_SESSION['COUNTER']++;
}
}
$section->SetTime('end');
$section->WriteLog('', $step, 'w+');
if ($step == 'update_relation_category')
{
unset($_SESSION['CATS_WITHOUT_IDS']);
header('refresh: '.$section->speed.'; url='.$log->projectUrl.'import.php?step=update_relation_category');
}
else
{
header('refresh: '.$section->speed.'; url='.$log->projectUrl.'import.php?step='.$step);
}
break;
case 'update_relation_category':
$section = new ClImportCategory();
$section->SetTime('begin');
foreach ($_SESSION['CATS'] as $key => $arItem)
{
if (!in_array($arItem['ID'], $_SESSION['CAT_IDS']))
{
if ($section->UpdateRelation($arItem) !== false)
{
$_SESSION['CAT_IDS'][] = $arItem['ID'];
}
if ($_SESSION['CATS_COUNT_UPD'] < $_SESSION['RESIDUE'])
{
unset ($_SESSION['CATS_IDS']);
unset ($_SESSION['CATS_COUNT_UPD']);
$_SESSION['COUNTER'] = 0;
$step = 'get_offers';
break;
}
if ($_SESSION['COUNTER'] >= $section->offset)
{
$_SESSION['COUNTER'] = 0;
$page++;
$step = 'update_relation_category&page='.$page;
break;
}
$_SESSION['CATS_COUNT_UPD']--;
$_SESSION['COUNTER']++;
}
}
$section->SetTime('end');
$section->WriteLog('', $step, 'w+');
if ($step == 'offers_add_to_session')
{
header('refresh: '.$section->speed.'; url='.$section->projectUrl.'import.php?step=offers_add_to_session');
}
else
{
header('refresh: '.$section->speed.'; url='.$section->projectUrl.'import.php?step='.$step);
}
break;
case 'offers_add_to_session':
$offers = new ClImportOffer();
$offers->SetTime('begin');
$offers->AddOffersFromXml('session');
$offers->SetTime('end');
$offers->WriteLog('', $step, 'w+');
header('refresh: '.$offers->speed.'; url='.$offers->projectUrl.'import.php?step=final');
break;
case 'final':
$log = new ClImportConfig();
$log->SetTime('begin');
echo 'Работа завершена.';
$log->SetTime('end');
$log->WriteLog('end', $step, 'w+');
break;
}