3namespace Bitrix\Sale\TradingPlatform\Vk;
5use Bitrix\Iblock\SectionElementTable;
6use Bitrix\Sale\TradingPlatform;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Application;
10use Bitrix\Main\SystemException;
12Loc::loadMessages(__FILE__);
22 private static $sections;
23 private $mappedSections;
24 private $mappedAlbums;
26 private $iblocksIds =
array();
27 private $currSectionSettings =
array();
36 const VK_ICON =
'<img src="/bitrix/images/sale/vk/vk_icon.png" style="height:16px; width:16px; margin-right: 1em;" />';
37 const VK_ICON_EMPTY =
'<span style="width:16px; margin-right: 1em; display:inline-block;"></span>';
45 $this->exportId = intval($exportId);
48 $cacheManager = Application::getInstance()->getManagedCache();
49 if ($cacheManager->read(self::CACHE_TTL, $this->createCacheIdMappedSections()))
58 $this->mappedSections = $mappedSections;
60 if (!Loader::includeModule(
'iblock'))
62 throw new SystemException(
"Can't include module \"IBlock\"! " . __METHOD__);
70 private function createCacheId($cacheName =
null)
72 $cacheId = self::CACHE_ID_PREFIX .
'__' . $this->exportId;
74 return $cacheName ? $cacheId .
'__' . $cacheName : self::CACHE_ID_PREFIX;
83 $cacheName = self::CACHE_ID_SECTIONS .
84 ($onlyMapped ?
'_mapped' :
'_all');
85 return $this->createCacheId($cacheName);
94 return $this->createCacheId(self::CACHE_ID_MAPPED_SECTIONS);
103 return $this->createCacheId(self::CACHE_ID_MAPPED_SECTIONS_LIST);
112 $cacheManager = Application::getInstance()->getManagedCache();
130 $cacheManager = Application::getInstance()->getManagedCache();
133 if ($cacheManager->read(self::CACHE_TTL, $this->createCacheIdSections($onlyMapped)))
141 $filter =
array(
"IBLOCK_ID" => $iblockIds,
"ELEMENT_SUBSECTIONS" =>
"N");
145 $vkSettings = $vk->getSettings($this->exportId);
146 if (isset($vkSettings[
"EXPORT_SETTINGS"][
"ONLY_AVAILABLE_FLAG"]) && !$vkSettings[
"EXPORT_SETTINGS"][
"ONLY_AVAILABLE_FLAG"])
156 $resSections = \CIBlockSection::GetList(
157 array(
"LEFT_MARGIN" =>
"asc"),
161 "IBLOCK_ID",
"IBLOCK_SECTION_ID",
"ID",
"DEPTH_LEVEL",
"NAME",
"LEFT_MARGIN",
"RIGHT_MARGIN",
166 while ($currSection = $resSections->Fetch())
169 $sections[$currSection[
"IBLOCK_ID"]][$currSection[
"ID"]] = $currSection;
184 $sections = $sectionsList;
201 if (Loader::includeModule(
'catalog'))
204 'select' => [
'IBLOCK_ID'],
205 'filter' => [
'=PRODUCT_IBLOCK_ID' => 0],
209 $iblockIds[$row[
'IBLOCK_ID']] = $row[
'IBLOCK_ID'];
227 if (empty($this->iblocksIds))
229 foreach ($this->mappedSections as $mappedSection)
231 $id = $mappedSection[
"PARAMS"][
"IBLOCK"];
232 $this->iblocksIds[$id] = $id;
236 return $this->iblocksIds;
248 $sectionsToExport =
array();
249 foreach ($this->mappedSections as $mappedSection)
251 $params = $mappedSection[
"PARAMS"];
252 $parentParams =
$params[
"PARENT_SETTINGS"];
257 (
$params[
"INHERIT"] && $parentParams && $parentParams[
"ENABLE"])
260 $sectionsToExport[
$params[
"IBLOCK"]][$mappedSection[
"BX_ID"]] = $mappedSection[
"BX_ID"];
264 return $sectionsToExport;
268 private function getListMappedSections()
272 foreach ($this->mappedSections as $mappedSection)
274 $params = $mappedSection[
"PARAMS"];
275 $parentParams =
$params[
"PARENT_SETTINGS"];
281 "TO_ALBUM" =>
$params[
"TO_ALBUM"],
282 "BX_ID" => $mappedSection[
"BX_ID"],
283 "VK_CATEGORY" =>
$params[
"VK_CATEGORY"],
289 $result[$mappedSection[
"BX_ID"]][
"TO_ALBUM_ALIAS"] =
$params[
"TO_ALBUM_ALIAS"];
294 elseif (
$params[
"INHERIT"] && $parentParams && $parentParams[
"ENABLE"] && !$parentParams[
"INCLUDE_CHILDS"])
297 "TO_ALBUM" => $mappedSection[
"BX_ID"],
298 "BX_ID" => $mappedSection[
"BX_ID"],
299 "VK_CATEGORY" => $parentParams[
"VK_CATEGORY"],
305 elseif (
$params[
"INHERIT"] && $parentParams && $parentParams[
"ENABLE"] && $parentParams[
"INCLUDE_CHILDS"])
308 "TO_ALBUM" => $parentParams[
"TO_ALBUM"],
309 "BX_ID" => $mappedSection[
"BX_ID"],
310 "VK_CATEGORY" =>
$params[
"VK_CATEGORY"],
315 if ($parentParams[
"TO_ALBUM_ALIAS"])
317 $result[$mappedSection[
"BX_ID"]][
"TO_ALBUM_ALIAS"] = $parentParams[
"TO_ALBUM_ALIAS"];
334 $sectionsToExport =
array();
335 $sectionsAliases =
array();
337 foreach ($this->mappedSections as $mappedSection)
339 $params = $mappedSection[
"PARAMS"];
340 $parentParams =
$params[
"PARENT_SETTINGS"];
349 $sectionsAliases[
$params[
"TO_ALBUM"]] =
$params[
"TO_ALBUM_ALIAS"];
354 elseif (
$params[
"INHERIT"] && $parentParams && $parentParams[
"ENABLE"] && !$parentParams[
"INCLUDE_CHILDS"])
356 $sectionsToExport[$mappedSection[
"BX_ID"]] = $mappedSection[
"BX_ID"];
358 if ($parentParams[
"TO_ALBUM_ALIAS"])
360 $sectionsAliases[$parentParams[
"TO_ALBUM"]] = $parentParams[
"TO_ALBUM_ALIAS"];
365 elseif (
$params[
"INHERIT"] && $parentParams && $parentParams[
"ENABLE"] && $parentParams[
"INCLUDE_CHILDS"])
367 $sectionsToExport[$parentParams[
"TO_ALBUM"]] = $parentParams[
"TO_ALBUM"];
369 if ($parentParams[
"TO_ALBUM_ALIAS"])
371 $sectionsAliases[$parentParams[
"TO_ALBUM"]] = $parentParams[
"TO_ALBUM_ALIAS"];
376 return array(
"SECTIONS" => $sectionsToExport,
"ALIASES" => $sectionsAliases);
382 $sections = SectionElementTable::getList(
array(
384 "IBLOCK_ELEMENT_ID" => $pdoructsIds,
385 "ADDITIONAL_PROPERTY_ID" =>
null,
390 while ($section = $sections->fetch())
392 $result[$section[
"IBLOCK_ELEMENT_ID"]][] = $section[
"IBLOCK_SECTION_ID"];
405 $result =
'<table class="internal">';
408 <td class="internal-left">' . Loc::getMessage(
"VK_EXPORT_MAP__ALBUM_NAME") .
'</td>
409 <td style="text-align: right !important;">' . Loc::getMessage(
"VK_EXPORT_MAP__ELEMENT_CNT") .
'</td>
410 <td class="internal-right">' . Loc::getMessage(
"VK_EXPORT_MAP__SECTIONS_NAME") .
'</td>
414 foreach ($mappedSectionGroupped as $currAlbum)
418 $result .= isset($currAlbum[
"ALBUM_VK_URL"]) ?
419 '<a href="' . $currAlbum[
"ALBUM_VK_URL"] .
'">' . self::VK_ICON .
'</a>' . $currAlbum[
"TO_ALBUM_NAME"] :
420 self::VK_ICON_EMPTY . $currAlbum[
"TO_ALBUM_NAME"];
422 $result .=
'<td class="bx-digit-cell" >' . $currAlbum[
"ELEMENT_CNT"] .
'</td>';
425 if (
count($currAlbum[
"ITEMS"]) > 0)
428 foreach ($currAlbum[
"ITEMS"] as $currSection)
430 $items .=
'<div style = "margin-bottom:4px;">' . $currSection[
"NAME"];
431 $items .=
' <i>(' . Loc::getMessage(
"VK_EXPORT_MAP__ELEMENT_CNT_2") .
': ' . $currSection[
"ELEMENT_CNT"] .
')</i>';
432 $items .= isset($currSection[
"SECTION_URL"]) ?
433 ' <a href="' . $currSection[
"SECTION_URL"] .
'">' . Loc::getMessage(
"VK_EXPORT_MAP__SECTION_SETTINGS") .
'</a> ' :
441 $result .=
'<td>' . Loc::getMessage(
"VK_EXPORT_MAP__NO_SECTIONS") .
'</td>';
459 if (empty($this->mappedAlbums))
463 if (empty($this->mappedSections))
468 $sectionsUnformatted = $this->getListMappedSections();
469 $sectionsFormatted =
array();
472 if (empty($this->mappedSections))
479 $vkCategoriesList = $vkCategories->getList(
false);
481 foreach ($sectionsUnformatted as $sectionUnformatted)
483 $currSection = $sections[$sectionUnformatted[
"IBLOCK"]][$sectionUnformatted[
"BX_ID"]];
490 if (!array_key_exists($sectionUnformatted[
"TO_ALBUM"], $sectionsFormatted))
492 $sectionsFormatted[$sectionUnformatted[
"TO_ALBUM"]] =
array(
493 "TO_ALBUM" => $sectionUnformatted[
"TO_ALBUM"],
498 if (array_key_exists($sectionUnformatted[
"TO_ALBUM"], $this->mappedAlbums))
500 $albumVkId = $this->mappedAlbums[$sectionUnformatted[
"TO_ALBUM"]][
"ALBUM_VK_ID"];
501 $sectionsFormatted[$sectionUnformatted[
"TO_ALBUM"]][
"ALBUM_VK_ID"] = $albumVkId;
502 $sectionsFormatted[$sectionUnformatted[
"TO_ALBUM"]][
"ALBUM_VK_URL"] = $this->createVkAlbumLink($albumVkId);
507 if ($sectionUnformatted[
"TO_ALBUM"] == $sectionUnformatted[
"BX_ID"])
509 $sectionsFormatted[$sectionUnformatted[
"TO_ALBUM"]][
"TO_ALBUM_NAME"] = $sectionUnformatted[
"TO_ALBUM_ALIAS"] ?
510 $sectionUnformatted[
"TO_ALBUM_ALIAS"] :
511 trim($currSection[
"NAME"]);
515 $vkCategoryName = $vkCategoriesList[$sectionUnformatted[
"VK_CATEGORY"]][
'NAME'];
516 $sectionName = $currSection[
"NAME"];
517 $sectionElementCnt = $currSection[
"ELEMENT_CNT"];
520 "BX_ID" => $sectionUnformatted[
"BX_ID"],
521 "NAME" => $sectionName,
522 "VK_CATEGORY_ID" => $sectionUnformatted[
"VK_CATEGORY"],
523 "VK_CATEGORY_NAME" => $vkCategoryName,
524 "ELEMENT_CNT" => $sectionElementCnt,
525 "IBLOCK" => $sectionUnformatted[
"IBLOCK"],
526 "LEFT_MARGIN" => $currSection[
"LEFT_MARGIN"],
527 "DEPTH_LEVEL" => $currSection[
"DEPTH_LEVEL"],
530 if (isset($item[
"BX_ID"]) && isset($item[
"IBLOCK"]))
532 $item[
"SECTION_URL"] = $this->createSectionLink($item[
"IBLOCK"], $item[
"BX_ID"]);
535 $sectionsFormatted[$sectionUnformatted[
"TO_ALBUM"]][
"ITEMS"][$item[
"LEFT_MARGIN"]] = $item;
536 $sectionsFormatted[$sectionUnformatted[
"TO_ALBUM"]][
"ELEMENT_CNT"] += $sectionElementCnt;
540 $sectionsFormatted = self::sortMapElementItems($sectionsFormatted);
542 return $sectionsFormatted;
545 private static function sortMapElementItems($sectionsFormatted)
547 $sectionsSorted =
array();
548 foreach ($sectionsFormatted as $albumKey => $album)
550 if (!empty($album[
"ITEMS"]))
552 ksort($album[
"ITEMS"]);
554 $prevDepthLevel =
false;
556 foreach ($album[
"ITEMS"] as &$item)
558 if (!$prevDepthLevel)
560 $prevDepthLevel = $item[
"DEPTH_LEVEL"];
562 if ($item[
"DEPTH_LEVEL"] > $prevDepthLevel)
565 $prevDepthLevel = $item[
"DEPTH_LEVEL"];
567 elseif ($item[
"DEPTH_LEVEL"] < $prevDepthLevel)
570 $prevDepthLevel = $item[
"DEPTH_LEVEL"];
572 $item[
"NAME"] = str_repeat(
'- ', $tabsCount) . $item[
"NAME"];
576 $sectionsSorted[$albumKey] = $album;
579 return $sectionsSorted;
588 private function createVkAlbumLink($albumVkId)
591 $groupId = str_replace(
'-',
'', $vk->getGroupId($this->exportId));
610 private function createSectionLink(
$iblockId, $sectionId)
612 $sectionTabControlName =
'form_section_' .
$iblockId .
'_active_tab';
614 return \CIBlock::GetAdminSectionEditLink(
$iblockId, $sectionId,
array(
615 $sectionTabControlName =>
"SALE_TRADING_PLATFORM_edit_trading_platforms",
628 $mappedSection = $this->mappedSections[$sectionId];
629 $params = $mappedSection[
"PARAMS"];
630 $parentParams =
$params[
"PARENT_SETTINGS"];
636 (
$params[
"INHERIT"] && $parentParams && $parentParams[
"ENABLE"] && !$parentParams[
"INCLUDE_CHILDS"])
652 elseif (
$params[
"INHERIT"] && $parentParams && $parentParams[
"ENABLE"] && $parentParams[
"INCLUDE_CHILDS"])
654 return $parentParams[
"TO_ALBUM"];
678 $sectionsTree = $this->
getSections(
true, $onlyMapped);
681 $result .=
'<option value="0">' . Loc::getMessage(
"SALE_CATALOG_VK_MAIN_ALBUM") .
'</option>';
690 $result .=
'<option disabled value="-1">'.
695 foreach ($sectionsTree[
$iblockId] as $bxCategory)
697 $selected = $checkedSection == $bxCategory[
"ID"] ?
' selected' :
'';
699 '<option' . $selected .
' value="' . $bxCategory[
"ID"] .
'">' .
700 str_repeat(
'. ', $bxCategory[
"DEPTH_LEVEL"]) . $bxCategory[
"NAME"] .
719 $vkCategory = $this->mappedSections[$sectionId][
'VK_ID'];
721 if (!isset($vkCategory) || $vkCategory <= 0)
724 $settings = $vk->getSettings($this->exportId);
726 if (isset(
$settings[
"EXPORT_SETTINGS"][
"CATEGORY_DEFAULT"]))
728 $vkCategory =
$settings[
"EXPORT_SETTINGS"][
"CATEGORY_DEFAULT"];
751 $section = $sections[$sectionId];
753 $currParams = $this->mappedSections[$sectionId][
'PARAMS'];
754 $parentParams = $currParams[
"PARENT_SETTINGS"];
757 if (!$section[
"IBLOCK_SECTION_ID"])
759 $currParams[
'INHERIT'] =
false;
763 if (isset($currParams[
'INHERIT']) && !$currParams[
'INHERIT'])
765 $currParams = $currParams + $this->getDefaultExportParams($sectionId);
770 if (!empty($parentParams))
772 $currParams = $this->prepareParentSettingToShow($parentParams, $section);
777 $currParams = $this->getDefaultExportParams($sectionId);
781 $currParams[
'INHERIT'] =
true;
785 $hiddenParentParams = !empty($parentParams) ?
786 $this->prepareParentSettingToShow($parentParams, $section) :
787 $hiddenParentParams = $this->getDefaultExportParams($sectionId);
788 foreach ($hiddenParentParams as
$key => $param)
790 $currParams[
$key .
'__PARENT'] = $param;
806 $section = $sections[$sectionId];
808 $params[
"INHERIT__DISPLAY"] = $section[
"IBLOCK_SECTION_ID"] ?
'' :
' disabled ';
811 $params[
"ENABLE__DISPLAY"] =
' disabled ';
812 $params[
"TO_ALBUM__DISPLAY"] =
' disabled ';
813 $params[
"TO_ALBUM_ALIAS__DISPLAY"] =
' display:none; ';
814 $params[
"INCLUDE_CHILDS__DISPLAY"] =
" disabled ";
815 $params[
"VK_CATEGORY__DISPLAY"] =
" disabled ";
820 $params[
"ENABLE__DISPLAY"] =
'';
824 $params[
"VK_CATEGORY__DISPLAY"] =
'';
825 $params[
"TO_ALBUM__DISPLAY"] =
'';
831 $params[
"TO_ALBUM_ALIAS__DISPLAY"] =
'display: block';
834 $params[
"INCLUDE_CHILDS__DISPLAY"] =
$params[
"TO_ALBUM"] > 0 ?
"" :
" disabled ";
845 if ($param ===
false)
862 private function prepareParentSettingToShow(
$settings, $section)
864 $preparedSettings =
array();
866 $preparedSettings[
"ENABLE"] =
$settings[
"ENABLE"];
867 $preparedSettings[
"INCLUDE_CHILDS"] =
false;
871 $preparedSettings[
"TO_ALBUM"] =
$settings[
"TO_ALBUM"];
872 $preparedSettings[
"TO_ALBUM_ALIAS"] =
$settings[
"TO_ALBUM_ALIAS"];
876 $preparedSettings[
"TO_ALBUM"] = $section[
"ID"];
877 $preparedSettings[
"TO_ALBUM_ALIAS"] = $section[
"NAME"];
880 return $preparedSettings;
890 private function getDefaultExportParams($sectionId)
895 $vkSettings = $vk->getSettings($this->exportId);
896 $vkCategory = $vkSettings[
'EXPORT_SETTINGS'][
'CATEGORY_DEFAULT'];
901 "TO_ALBUM" => $sectionId,
902 "TO_ALBUM_ALIAS" => $sections[$sectionId][
"NAME"],
903 "INCLUDE_CHILDS" =>
false,
937 $iblockId = $sections[$sectionId][
"IBLOCK_ID"];
938 $currParentSettings = $this->mappedSections[$sectionId][
'PARAMS'][
'PARENT_SETTINGS'];
940 $dataToDelete =
array();
941 $settingsToSave =
array();
945 $settingsToSave[
"PARENT_SETTINGS"] = $currParentSettings;
951 (!$sections[$sectionId][
"IBLOCK_SECTION_ID"] && !
$settings[
"ENABLE"])
955 if (!$currParentSettings)
957 $dataToDelete =
array(
959 "VALUE_INTERNAL" => $sectionId,
964 $settingsToSave[
"INHERIT"] =
true;
969 $settingsToSave[
"INHERIT"] =
false;
974 $settingsToSave[
"ENABLE"] =
false;
980 $settingsToSave[
"ENABLE"] =
true;
981 $settingsToSave[
"INCLUDE_CHILDS"] =
$settings[
"INCLUDE_CHILDS"] ? true :
false;
985 $settingsToSave[
"TO_ALBUM"] =
$settings[
"TO_ALBUM"];
991 $settingsToSave[
"TO_ALBUM_ALIAS"] =
$settings[
"TO_ALBUM_ALIAS"];
995 $settingsToSave[
"TO_ALBUM_ALIAS"] =
null;
1000 $settingsToSave[
"TO_ALBUM"] = 0;
1001 $settingsToSave[
"TO_ALBUM_ALIAS"] =
null;
1008 if (!empty($dataToDelete))
1011 "TO_DELETE" =>
array(
1012 $sectionId => $dataToDelete,
1021 $sectionId =>
array(
1023 "VALUE_INTERNAL" => $sectionId,
1024 "PARAMS" => $settingsToSave,
1048 $currParentSettings = $this->mappedSections[$sectionId][
'PARAMS'][
'PARENT_SETTINGS'];
1049 $iblockId = $sections[$sectionId][
"IBLOCK_ID"];
1051 $currLeftMargin = intval($sections[$sectionId][
"LEFT_MARGIN"]);
1052 $currRightMargin = intval($sections[$sectionId][
"RIGHT_MARGIN"]);
1054 $dataToSave =
array();
1055 $dataToDelete =
array();
1059 $needDelete =
false;
1060 if (
$settings[
"INHERIT"] || (!$sections[$sectionId][
"IBLOCK_SECTION_ID"] && !
$settings[
"ENABLE"]))
1063 if (!$currParentSettings)
1074 foreach ($sections as $section)
1078 intval($section[
"LEFT_MARGIN"]) > $currLeftMargin &&
1079 intval($section[
"RIGHT_MARGIN"]) < $currRightMargin &&
1084 if ($this->mappedSections[$section[
"ID"]][
"PARAMS"][
"INHERIT"] !==
false)
1089 $dataToDelete[$section[
"ID"]] =
array(
1091 'VALUE_INTERNAL' => $section[
"ID"],
1098 $dataToSave[$section[
"ID"]] =
array(
1100 "VALUE_INTERNAL" => $section[
"ID"],
1105 "PARENT_SETTINGS" =>
array(
1107 "ENABLE" =>
$settings[
"ENABLE"] ?
true :
false,
1109 "TO_ALBUM_ALIAS" =>
$settings[
"TO_ALBUM_ALIAS"],
1110 "VK_CATEGORY" =>
$settings[
"VK_CATEGORY"],
1111 "INCLUDE_CHILDS" =>
$settings[
"INCLUDE_CHILDS"] ?
true :
false,
1120 $dataToSave[$section[
"ID"]] =
array(
1121 "VALUE_EXTERNAL" => $this->mappedSections[$section[
"ID"]][
"VK_ID"],
1122 "VALUE_INTERNAL" => $this->mappedSections[$section[
"ID"]][
"BX_ID"],
1123 "PARAMS" => $this->mappedSections[$section[
"ID"]][
"PARAMS"],
1128 $dataToSave[$section[
"ID"]][
"PARAMS"][
"PARENT_SETTINGS"] =
array(
1130 "ENABLE" =>
$settings[
"ENABLE"] ?
true :
false,
1132 "TO_ALBUM_ALIAS" =>
$settings[
"TO_ALBUM_ALIAS"],
1133 "VK_CATEGORY" =>
$settings[
"VK_CATEGORY"],
1134 "INCLUDE_CHILDS" =>
$settings[
"INCLUDE_CHILDS"] ?
true :
false,
1139 unset($dataToSave[$section[
"ID"]][
"PARAMS"][
"PARENT_SETTINGS"]);
1146 if (!empty($dataToSave))
1148 $result[
'TO_SAVE'] = $dataToSave;
1151 if (!empty($dataToDelete))
1153 $result[
'TO_DELETE'] = $dataToDelete;
static getList(array $parameters=array())
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(! $catalogEdit->isSuccess()) $iblock
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']