Skip to content

Instantly share code, notes, and snippets.

@demoriz
Last active December 25, 2017 16:15
Show Gist options
  • Save demoriz/dcfc56f757834df8e995f38056950646 to your computer and use it in GitHub Desktop.
Save demoriz/dcfc56f757834df8e995f38056950646 to your computer and use it in GitHub Desktop.
bitrix template 'yandex run'
<?php
/** @global CUser $USER */
/** @global CMain $APPLICATION */
/** @var int $IBLOCK_ID */
/** @var string $SETUP_SERVER_NAME */
/** @var string $SETUP_FILE_NAME */
/** @var array $V */
/** @var array|string $XML_DATA */
/** @var bool $firstStep */
/** @var int $CUR_ELEMENT_ID */
/** @var bool $finalExport */
/** @var bool $boolNeedRootSection */
/** @var int $intMaxSectionID */
/** @var string $strExportErrorMessage */
//<title>Custom Name</title>
use Bitrix\Main\Loader;
set_time_limit(0);
global $APPLICATION;
Loader::includeModule('iblock');
Loader::includeModule('catalog');
$XML_DATA = unserialize(stripslashes($XML_DATA));
if (!is_array($XML_DATA)) $XML_DATA = array();
if (!function_exists('yandex_text2xml')) {
function yandex_text2xml($text, $bHSC = false, $bDblQuote = false)
{
global $APPLICATION;
$bHSC = (true == $bHSC ? true : false);
$bDblQuote = (true == $bDblQuote ? true : false);
if ($bHSC) {
$text = htmlspecialcharsbx($text);
if ($bDblQuote)
$text = str_replace('&quot;', '"', $text);
}
$text = preg_replace('/[\x1-\x8\xB-\xC\xE-\x1F]/', '', $text);
$text = str_replace("'", '&apos;', $text);
$text = $APPLICATION->ConvertCharset($text, LANG_CHARSET, 'windows-1251');
return $text;
}
}
// sections
$arFilter = array(
'IBLOCK_ID' => $IBLOCK_ID,
'ACTIVE' => 'Y',
'IBLOCK_ACTIVE' => 'Y',
'GLOBAL_ACTIVE' => 'Y'
);
if (is_array($V) && $V[0] != 0) {
$arFilter['SECTION_ID'] = $V;
}
$arSelect = array('ID', 'NAME', 'CODE', 'IBLOCK_SECTION_ID');
$dbSections = CIBlockSection::GetList(array(), $arFilter, false, $arSelect);
$arSection = array();
while ($arSectionFields = $dbSections->GetNext()) {
$intIblockSectionID = $arSectionFields['IBLOCK_SECTION_ID'];
if (is_array($V) && array_search($intIblockSectionID, $V) !== false) {
$intIblockSectionID = 0;
}
$arSection[$arSectionFields['ID']] = array(
'name' => $arSectionFields['NAME'],
'code' => $arSectionFields['CODE'],
'parentID' => $intIblockSectionID
);
}
// elements
$arFilter = array(
'IBLOCK_ID' => $IBLOCK_ID,
'ACTIVE' => 'Y',
'IBLOCK_ACTIVE' => 'Y',
'GLOBAL_ACTIVE' => 'Y'
);
if (is_array($V) && $V[0] != 0) {
$arFilter['SECTION_ID'] = $V;
$arFilter['INCLUDE_SUBSECTIONS'] = 'Y';
}
$arSelect = array('ID', 'NAME', 'CODE', 'PREVIEW_PICTURE', 'IBLOCK_SECTION_ID', 'DETAIL_PAGE_URL');
if (is_array($XML_DATA) && is_array($XML_DATA['XML_DATA']['PARAMS'])) {
foreach ($XML_DATA['XML_DATA']['PARAMS'] as $intPropID) {
$arSelect[] = 'PROPERTY_' . $intPropID;
}
}
if (is_array($XML_DATA) && is_numeric($XML_DATA['PRICE'])) {
$arSelect[] = 'CATALOG_GROUP_' . $XML_DATA['PRICE'];
}
$dbElement = CIBlockElement::GetList(array(), $arFilter, false, false, $arSelect);
$arElements = array();
while ($arElementFields = $dbElement->GetNext()) {
$arElementFields['PREVIEW_PICTURE'] = CFile::GetPath($arElementFields['PREVIEW_PICTURE']);
}
$strXML = '<?php' . "\n";
$strXML .= 'header("Content-Type: text/xml; charset=windows-1251");' . "\n";
$strXML .= 'echo "<"."?xml version=\"1.0\" encoding=\"windows-1251\"?".">"?>';
$strXML .= '<yml_catalog date="' . date('Y-m-d H:i') . '">' . "\n";
$strXML .= "<shop>\n";
$strXML .= '<name>' . $APPLICATION->ConvertCharset(htmlspecialcharsbx(COption::GetOptionString('main', 'site_name', '')), LANG_CHARSET, 'windows-1251') . "</name>\n";
$strXML .= '<company>' . $APPLICATION->ConvertCharset(htmlspecialcharsbx(COption::GetOptionString('main', 'site_name', '')), LANG_CHARSET, 'windows-1251') . "</company>\n";
$strXML .= '<url>http://' . htmlspecialcharsbx($SETUP_SERVER_NAME) . "</url>\n";
$strXML .= "<platform>1C-Bitrix</platform>\n";
$strXML .= "<currencies>\n";
$strXML .= '<currency id="RUR" rate="1"/>' . "\n";
$strXML .= "</currencies>\n";
$strXML .= "<categories>\n";
foreach ($arSection as $intID => $arItem) {
$strXML .= '<category id="' . $intID . '"';
if ($arItem['parentID']) {
$strXML .= ' parentId="' . $arItem['parentID'] . '"';
}
$strXML .= '>' . yandex_text2xml($arItem['name'], true) . '</category>' . "\n";
}
$strXML .= "</categories>\n";
$strXML .= "<offers>\n";
foreach ($arElements as $intID => $arElement) {
$strXML .= '<offer id="' . $intID . '" available="true">' . "\n";
$strXML .= "</offer>\n";
}
$strXML .= "</offers>\n";
$strXML .= "</shop>\n";
$strXML .= "</yml_catalog>\n";
$fileXML = @fopen($_SERVER["DOCUMENT_ROOT"] . $SETUP_FILE_NAME, "wb");
if (!$fileXML) {
$strExportErrorMessage .= 'Do not open file - ' . $_SERVER["DOCUMENT_ROOT"] . $SETUP_FILE_NAME;
}
@fwrite($fileXML, $strXML);
@fclose($fileXML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment