Current Path : /var/www/u0635749/data/www/hobbyclick.ru/public/ |
Current File : /var/www/u0635749/data/www/hobbyclick.ru/public/two-quick-import.php |
<? require_once($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php"); use Bitrix\Highloadblock\HighloadBlockTable as HLBT; use \Bitrix\Iblock\SectionTable; use Bitrix\Iblock\ElementTable; CModule::IncludeModule('iblock'); CModule::IncludeModule('catalog'); CModule::IncludeModule('highloadblock'); class TwoQuickImport { private $strExportFrom = 'https://tequiladance.ru/two-quick-export.php', $arCity = [1243], $arDateTime = [ 'ВС' => 'DATE_0', 'ПН' => 'DATE_1', 'ВТ' => 'DATE_2', 'СР' => 'DATE_3', 'ЧТ' => 'DATE_4', 'ПТ' => 'DATE_5', 'СБ' => 'DATE_6', ], $arImportCodes = [ 'REGIONS' => [ 'NAME' => 'Регионы', 'CODE' => 'REGIONS', 'HL_ID' => 5 ], 'METROS' => [ 'NAME' => 'Метро', 'CODE' => 'METROS', 'HL_ID' => 4 ], 'ADDRESSES' => [ 'NAME' => 'Адреса', 'CODE' => 'ADDRESSES', 'IBLOCK_ID' => 15 ], 'SCHEDULE' => [ 'NAME' => 'Расписание', 'CODE' => 'SCHEDULE', 'IBLOCK_ID' => 16 ], 'TEACHERS' => [ 'NAME' => 'Преподаватели', 'CODE' => 'TEACHERS', 'IBLOCK_ID' => 13 ], 'WAYS' => [ 'NAME' => 'Направления', 'CODE' => 'WAYS', 'IBLOCK_ID' => 5 ], ]; private function GetEntityDataClass($HlBlockId) { if (empty($HlBlockId) || $HlBlockId < 1) { return false; } $hlblock = HLBT::getById($HlBlockId)->fetch(); $entity = HLBT::compileEntity($hlblock); $entity_data_class = $entity->getDataClass(); return $entity_data_class; } private function saveToHLBlock($HlBlockId, $arFields) { $entity_data_class = $this->GetEntityDataClass($HlBlockId); $elem = $entity_data_class::getList( [ 'select' => ['ID'], 'filter' => [ 'UF_XML_ID' => $arFields['UF_XML_ID'] ] ])->fetch(); if ($elem) { $result = $entity_data_class::update($elem['ID'], $arFields); } else { $result = $entity_data_class::add($arFields); } return $result; } /** * @param $iblockID * @param $arFields */ private function saveToIblock($iblockID, $arFields) { $el = new CIBlockElement(); $arParams = array("replace_space" => "-", "replace_other" => "-"); $trans = Cutil::translit($arFields['NAME'], "ru", $arParams); $arFields['CODE'] = $arFields['CODE'] ?: $trans; $arFields['IBLOCK_ID'] = $iblockID; //$arFields['ACTIVE'] = 'Y'; $arFields['MODIFIED_BY'] = $GLOBALS['USER']->GetID(); if($arFields['XML_ID']){ $select = [ 'ID', 'XML_ID', ]; $filter = [ '=IBLOCK_ID' => $iblockID, '=XML_ID' => $arFields['XML_ID'], ]; $res = ElementTable::getList([ 'select' => $select, 'filter' => $filter, ]); if ($ob = $res->fetch()) { $el->Update($ob['ID'],$arFields); echo 'Updated ID: ' . $ob['ID']; }else{ if ($PRODUCT_ID = $el->Add($arFields)) { echo 'New ID: ' . $PRODUCT_ID; } else { echo 'Error: ' . $el->LAST_ERROR; } } }else{ if ($PRODUCT_ID = $el->Add($arFields)) { echo 'New ID: ' . $PRODUCT_ID; } else { echo 'Error: ' . $el->LAST_ERROR; } } } /** * @param $iBlockID * @param $arSections * @throws */ private function saveToIBlockSections($iBlockID, $arSections) { $section = new CIBlockSection(); $arSavedSection = []; $select = [ 'XML_ID', 'ID', ]; $filter = [ '=IBLOCK_ID' => $iBlockID, ]; $res = SectionTable::getList([ 'select' => $select, 'filter' => $filter, ]); while ($ob = $res->fetch()) { $arSavedSection[$ob['XML_ID']] = $ob['ID']; } foreach ($arSections as $arSection) { $xml_id = sprintf('tekila_%s', $arSection['ID']); if (!$arSection['CODE']) { $arParams = array("replace_space" => "-", "replace_other" => "-"); $arSection['CODE'] = Cutil::translit($arSection['NAME'], "ru", $arParams); } $arFields = [ 'IBLOCK_ID' => $iBlockID, 'CODE' => $arSection['CODE'], 'XML_ID' => $xml_id, 'ACTIVE' => $arSection['ACTIVE'], 'UF_CITY_DOMEN' => $this->arCity, 'NAME' => $arSection['NAME'], 'DESCRIPTION' => $arSection['DESCRIPTION'], 'DESCRIPTION_TYPE' => 'html', ]; if (!$arSavedSection[$xml_id]) { if ($id = $section->Add($arFields)) { $arSavedSection[$xml_id] = $id; } } else { $section->Update($arSavedSection[$xml_id], $arFields); } } return $arSavedSection; } /** * @param $arParams * @return string */ private function getLink($arParams) { return sprintf('%s?%s', $this->strExportFrom, http_build_query($arParams, '', '&')); } /** * @param $code * @return bool|mixed */ private function getDataByCode($code) { $obSiteData = file_get_contents($this->getLink(['iblock_code' => $code])); if ($obSiteData) { $arData = json_decode($obSiteData, true); return $arData; } return false; } /** * */ private function importRegions() { $arOldData = $this->getDataByCode('REGIONS'); foreach ($arOldData['IBLOCK_DATA']['ITEMS'] as $arItem) { $arFields = [ 'UF_NAME' => $arItem['NAME'], 'UF_DESCRIPTION' => $arItem['PREVIEW_TEXT'], 'UF_FULL_DESCRIPTION' => $arItem['DETAIL_TEXT'], 'UF_XML_ID' => $arItem['CODE'], ]; /*TODO Расскоментировать когда будет готов и проверен импорт, метод сохраняет элемент*/ $this->saveToHLBlock($this->arImportCodes['REGIONS']['HL_ID'],$arFields); } } /** * @param $HL_ID * @param $arItems * @return array * @throws \Bitrix\Main\ArgumentException */ private function getHLBlockLinks($HL_ID, $arItems) { $result = []; $arNames = array_map(function ($item) { return $item['NAME']; }, $arItems); $entity_data_class = $this->GetEntityDataClass($HL_ID); $rsData = $entity_data_class::getList(array( 'order' => array('UF_NAME' => 'ASC'), 'select' => array('*'), 'filter' => array('UF_NAME' => $arNames) )); while ($el = $rsData->fetch()) { $result[] = $el['UF_XML_ID']; } return $result; } /** * @param $iblockID * @param $arItems * @return mixed */ private function getIblockLinks($iblockID, $arItems) { if($arItems){ $result = []; $arNames = array_map(function ($item) { return $item['NAME']; }, $arItems); $select = array('ID', 'NAME'); $filter = array('IBLOCK_ID' => $iblockID, 'NAME' => $arNames); $res = CIBlockElement::GetList(array(), $filter, false, false, $select); while ($ob = $res->Fetch()) { $result[] = $ob['ID']; } if (count($result) > 1) { return $result; } elseif (count($result) == 1) { return reset($result); } else { return false; } }else{ return false; } } /** * @param $iblockID * @param $arItems * @return mixed */ private function getIblockLinksXmlId($iblockID, $arItems) { $result = []; foreach ($arItems as $item){ $arXmlIds[] =sprintf('tekila_%s',$item['ID']); } if($arXmlIds){ $select = array('ID', 'NAME'); $filter = array('IBLOCK_ID' => $iblockID, 'XML_ID' => $arXmlIds); $res = CIBlockElement::GetList(array(), $filter, false, false, $select); while ($ob = $res->Fetch()) { $result[] = $ob['ID']; } } return $result; } /** * */ private function importMetros() { $arOldData = $this->getDataByCode('METROS'); foreach ($arOldData['IBLOCK_DATA']['ITEMS'] as $arItem) { $arFields = [ 'UF_NAME' => $arItem['NAME'], 'UF_DESCRIPTION' => $arItem['PREVIEW_TEXT'], 'UF_FULL_DESCRIPTION' => $arItem['DETAIL_TEXT'], 'UF_XML_ID' => $arItem['CODE'], ]; $this->saveToHLBlock($this->arImportCodes['METROS']['HL_ID'],$arFields); } } /** * */ private function importAddresses() { $arOldData = $this->getDataByCode('ADDRESSES'); foreach ($arOldData['IBLOCK_DATA']['ITEMS'] as $arItem) { $arPics = []; if ($arItem['PROPERTIES']['CAPTURE']['VALUE']) { foreach ($arItem['PROPERTIES']['CAPTURE']['VALUE'] as $path) { $arPics[] = CFile::MakeFileArray($path); } } $arFields = [ 'NAME' => $arItem['NAME'], 'XML_ID' => sprintf('tekila_%s',$arItem['ID']), 'PREVIEW_PICTURE' => $arPics[0] ?: '', 'PREVIEW_TEXT' => $arItem['PREVIEW_TEXT'], 'PREVIEW_TEXT_TYPE' => 'html', 'PROPERTY_VALUES' => [ 'KARTA' => $arItem['PROPERTIES']['KARTA']['VALUE'], 'AREA_CITY' => $this->getHLBlockLinks($this->arImportCodes['REGIONS']['HL_ID'], $arItem['LINKED_PROPS_VALUES']['AREA_NEW']['ITEMS'])[0], 'METRO' => $this->getHLBlockLinks($this->arImportCodes['METROS']['HL_ID'], $arItem['LINKED_PROPS_VALUES']['METRO']['ITEMS']), 'address' => $arItem['PROPERTIES']['ADDRESS']['VALUE']?:$arItem['NAME'], 'IDB24' => $arItem['PROPERTIES']['IDB24']['VALUE'], 'CAPTURE' => $arPics, 'CITY_DOMEN' => $this->arCity, 'NEAR_HALLS' => $arItem['PROPERTIES']['NEAR_HALLS']['VALUE'], ], 'CODE' => $arItem['CODE'], ]; $this->saveToIblock($this->arImportCodes['ADDRESSES']['IBLOCK_ID'],$arFields); } } /** * @param $iBlockId * @return array **/ private function getListProps($iBlockId) { $arProps = []; $properties = CIBlockProperty::GetList(array("sort" => "asc", "name" => "asc"), array("ACTIVE" => "Y", "IBLOCK_ID" => $iBlockId)); while ($prop_fields = $properties->GetNext()) { $arProps[$prop_fields['CODE']] = $prop_fields; if ($prop_fields['PROPERTY_TYPE'] == 'L') { $arLists[] = $prop_fields['CODE']; } } if ($arLists) { $res = CIBlockPropertyEnum::GetList(array("SORT" => "DESC"), array("IBLOCK_ID" => $iBlockId, 'CODE' => $arLists)); while ($arVal = $res->Fetch()) { $arProps[$arVal['PROPERTY_CODE']]['VALUES'][$arVal['ID']] = $arVal['VALUE']; } } return $arProps; } /** * */ private function importSchedules() { $arProps = $this->getListProps($this->arImportCodes['SCHEDULE']['IBLOCK_ID']); $arOldData = $this->getDataByCode('SCHEDULE'); $arListProps = [ 'AGE', 'level', ]; foreach ($arOldData['IBLOCK_DATA']['ITEMS'] as $arItem) { $arDays = []; if ($arItem['PROPERTIES']['godot']['VALUE'] == 0 && $arItem['PROPERTIES']['goddo']['VALUE'] == 0) { $arItem['PROPERTIES']['AGE']['VALUE'] = 'Любой'; } else { $arItem['PROPERTIES']['AGE']['VALUE'] = sprintf('%s-%s', $arItem['PROPERTIES']['godot']['VALUE'], $arItem['PROPERTIES']['goddo']['VALUE']); } $arLists = $this->getListValues($arListProps, $arProps, $arItem); if ($arItem['PROPERTIES']['daytime']['VALUE']) { foreach ($arItem['PROPERTIES']['daytime']['VALUE'] as $day) { $expDay = explode('-', $day); if ($this->arDateTime[$expDay[0]]) { $arDays[$this->arDateTime[$expDay[0]]][] = sprintf('%s-%s', $expDay[1], $expDay[2]); } } } $arFields = [ 'NAME' => $arItem['NAME'], 'ACTIVE' => $arItem['ACTIVE'], 'XML_ID' => sprintf('tekila_%s', $arItem['ID']), 'PROPERTY_VALUES' => [ 'CML2_LINK' => $this->getIblockLinksXmlId($this->arImportCodes['WAYS']['IBLOCK_ID'], $arItem['LINKED_PROPS_VALUES']['style']['ITEMS'])[0], 'CITY_DOMEN' => $this->arCity, 'godot' => $arItem['PROPERTIES']['godot']['VALUE'], 'goddo' => $arItem['PROPERTIES']['goddo']['VALUE'], 'AGE' => $arLists['AGE'], 'prepod' => $this->getIblockLinksXmlId($this->arImportCodes['TEACHERS']['IBLOCK_ID'], $arItem['LINKED_PROPS_VALUES']['prepod']['ITEMS'])[0], 'hall' => $this->getIblockLinksXmlId($this->arImportCodes['ADDRESSES']['IBLOCK_ID'], $arItem['LINKED_PROPS_VALUES']['addr']['ITEMS'])[0], 'level' => $arLists['level'], 'daytime' => $arItem['PROPERTIES']['daytime']['VALUE'], 'info' => $arItem['PROPERTIES']['info']['VALUE'], 'abon' => $arItem['PROPERTIES']['abon']['VALUE'], ], 'CODE' => $arItem['CODE'], ]; if ($arDays) { $arFields['PROPERTY_VALUES'] = array_merge($arFields['PROPERTY_VALUES'], $arDays); } $this->saveToIblock($this->arImportCodes['SCHEDULE']['IBLOCK_ID'],$arFields); } } /** * */ private function importTeachers() { $arOldData = $this->getDataByCode('TEACHERS'); foreach ($arOldData['IBLOCK_DATA']['ITEMS'] as $arItem) { $arFields = [ 'NAME' => $arItem['NAME'], 'XML_ID' => sprintf('tekila_%s', $arItem['ID']), 'PREVIEW_TEXT' => $arItem['~PREVIEW_TEXT'], 'PREVIEW_TEXT_TYPE' => 'html', 'DETAIL_TEXT' => $arItem['~DETAIL_TEXT'], 'DETAIL_TEXT_TYPE' => 'html', 'PREVIEW_PICTURE' => CFile::MakeFileArray($arItem['PREVIEW_PICTURE']), 'DETAIL_PICTURE' => CFile::MakeFileArray($arItem['PREVIEW_PICTURE']), 'CODE' => $arItem['CODE'], 'PROPERTY_VALUES' => [ 'CITY_DOMEN' => $this->arCity, ], ]; $this->saveToIblock($this->arImportCodes['TEACHERS']['IBLOCK_ID'],$arFields); } } /** * @param $arProps * @param $arItem * @param $arPerformProps * @return array * @throws */ private function getListValues($arProps, &$arPerformProps, $arItem) { $arList = []; $ibpEnum = new CIBlockPropertyEnum; foreach ($arProps as $arProp) { $valId = false; $val = $arItem['PROPERTIES'][$arProp]['VALUE']; if (is_array($val)) { foreach ($val as $item) { if (in_array($item, $arPerformProps[$arProp]['VALUES'])) { $valId[] = array_search($item, $arPerformProps[$arProp]['VALUES']); } else { if ($id = $ibpEnum->Add(array( 'PROPERTY_ID' => $arPerformProps[$arProp]['ID'], 'VALUE' => $item ))) { $valId[] = $id; $arPerformProps[$arProp]['VALUES'][$id] = $item; } } } } else { if (in_array($val, $arPerformProps[$arProp]['VALUES'])) { $valId = array_search($val, $arPerformProps[$arProp]['VALUES']); } else { if ($valId = $ibpEnum->Add(array( 'PROPERTY_ID' => $arPerformProps[$arProp]['ID'], 'VALUE' => $val ))) { $arPerformProps[$arProp]['VALUES'][$valId] = $val; } } } $arList[$arProp] = $valId; } return $arList; } /** * */ private function importWays() { $arOldData = $this->getDataByCode('WAYS'); $arSavedSections = []; $arProps = $this->getListProps($this->arImportCodes['WAYS']['IBLOCK_ID']); if ($arOldData['IBLOCK_DATA']['SECTIONS']) { $arSavedSections = $this->saveToIBlockSections($this->arImportCodes['WAYS']['IBLOCK_ID'], $arOldData['IBLOCK_DATA']['SECTIONS']); } $arListProps = [ 'FILTER_GENDER', 'FILTER_MOTIV', 'FILTER_LOAD', 'FILTER_REGION', 'MAN_SUIT', 'WOMAN_SUIT', ]; if ($arOldData['IBLOCK_DATA']['ITEMS'] && $arSavedSections) { foreach ($arOldData['IBLOCK_DATA']['ITEMS'] as $arItem) { $sectionXmlId = sprintf('tekila_%s', $arItem['IBLOCK_SECTION_ID']); $xmlId = sprintf('tekila_%s', $arItem['ID']); $arVideo = []; if ($arItem['PROPERTIES']['VIDEO']['~VALUE']) { foreach ($arItem['PROPERTIES']['VIDEO']['~VALUE'] as $arValue) { preg_match('#embed\/(.*?)\?#', $arValue['TEXT'], $arMatch); $link = sprintf('https://www.youtube.com/watch?v=%s', $arMatch[1]); $arVideo[] = $link; } } $arLists = $this->getListValues($arListProps, $arProps, $arItem); $arFields = [ 'ACTIVE' => $arItem['ACTIVE'], 'NAME' => $arItem['NAME'], 'XML_ID' => $xmlId, 'IBLOCK_SECTION_ID' => $arSavedSections[$sectionXmlId] ?: false, 'PREVIEW_TEXT' => $arItem['~PREVIEW_TEXT'], 'PREVIEW_TEXT_TYPE' => 'html', 'DETAIL_TEXT' => $arItem['~DETAIL_TEXT'], 'DETAIL_TEXT_TYPE' => 'html', 'PREVIEW_PICTURE' => CFile::MakeFileArray($arItem['PREVIEW_PICTURE']), 'DETAIL_PICTURE' => CFile::MakeFileArray($arItem['PREVIEW_PICTURE']), 'PROPERTY_VALUES' => [ 'VIDEO' => $arItem['PROPERTIES']['VIDEO']['VALUE'], 'LINK_VIDEO' => $arVideo, 'FILTER_GENDER' => $arLists['FILTER_GENDER'], 'FILTER_MOTIV' => $arLists['FILTER_MOTIV'], 'FILTER_REGION' => $arLists['FILTER_REGION'], 'FILTER_LOAD' => $arLists['FILTER_LOAD'], 'MAN_SUIT' => $arLists['MAN_SUIT'], 'WOMAN_SUIT' => $arLists['WOMAN_SUIT'], 'CITY_DOMEN' => $this->arCity, 'SIMILAR' => $this->getIblockLinks($this->arImportCodes['WAYS']['IBLOCK_ID'], $arItem['LINKED_PROPS_VALUES']['SIMILAR']['ITEMS']), ], 'CODE' => $arItem['CODE'], ]; $this->saveToIblock($this->arImportCodes['WAYS']['IBLOCK_ID'],$arFields); } } } /** * */ public function import() { $this->importRegions(); $this->importMetros(); $this->importAddresses(); $this->importWays(); $this->importTeachers(); $this->importSchedules(); } } $obImport = new TwoQuickImport; $arImportData = $obImport->import();