Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sectionslist.php
1<?php
2
4
11
12Loc::loadMessages(__FILE__);
13
21{
22 private static $sections;
23 private $mappedSections;
24 private $mappedAlbums;
25 private $exportId;
26 private $iblocksIds = array();
27 private $currSectionSettings = array();
28
29 const CACHE_DIR = '/sale/vkexport/';
30 const CACHE_TTL = 86400;
31 const CACHE_ID_PREFIX = "vk_sectionslist_cache";
32 const CACHE_ID_SECTIONS = "iblock_sections";
33 const CACHE_ID_MAPPED_SECTIONS = "mapped_sections";
34 const CACHE_ID_MAPPED_SECTIONS_LIST = "mapped_sections_list";
35
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>';
38
43 public function __construct($exportId)
44 {
45 $this->exportId = intval($exportId);
46
47// save mapped sections in cache
48 $cacheManager = Application::getInstance()->getManagedCache();
49 if ($cacheManager->read(self::CACHE_TTL, $this->createCacheIdMappedSections()))
50 {
51 $mappedSections = $cacheManager->get($this->createCacheIdMappedSections());
52 }
53 else
54 {
55 $mappedSections = Map::getMappedSections($exportId);
56 $cacheManager->set($this->createCacheIdMappedSections(), $mappedSections);
57 }
58 $this->mappedSections = $mappedSections;
59
60 if (!Loader::includeModule('iblock'))
61 {
62 throw new SystemException("Can't include module \"IBlock\"! " . __METHOD__);
63 }
64 }
65
70 private function createCacheId($cacheName = null)
71 {
72 $cacheId = self::CACHE_ID_PREFIX . '__' . $this->exportId;
73
74 return $cacheName ? $cacheId . '__' . $cacheName : self::CACHE_ID_PREFIX;
75 }
76
81 public function createCacheIdSections($onlyMapped = true)
82 {
83 $cacheName = self::CACHE_ID_SECTIONS .
84 ($onlyMapped ? '_mapped' : '_all');
85 return $this->createCacheId($cacheName);
86 }
87
93 {
94 return $this->createCacheId(self::CACHE_ID_MAPPED_SECTIONS);
95 }
96
102 {
103 return $this->createCacheId(self::CACHE_ID_MAPPED_SECTIONS_LIST);
104 }
105
106
110 public function clearCaches()
111 {
112 $cacheManager = Application::getInstance()->getManagedCache();
113 $cacheManager->clean($this->createCacheIdSections(true));
114 $cacheManager->clean($this->createCacheIdSections(false));
115 $cacheManager->clean($this->createCacheIdMappedSections());
116 $cacheManager->clean($this->createCacheIdMappedSectionsList());
117 }
118
119
127 public function getSections($tree = false, $onlyMapped = true)
128 {
129// We can save data in cache.Cache must be reload only if sections settings will be changed.
130 $cacheManager = Application::getInstance()->getManagedCache();
131 $sections = array();
132
133 if ($cacheManager->read(self::CACHE_TTL, $this->createCacheIdSections($onlyMapped)))
134 {
135 $sections = $cacheManager->get($this->createCacheIdSections($onlyMapped));
136 }
137 else
138 {
139// get IBLOCK IDS only for mapped sections or for all catalogs
140 $iblockIds = $this->getIblockIds($onlyMapped);
141 $filter = array("IBLOCK_ID" => $iblockIds, "ELEMENT_SUBSECTIONS" => "N");
142
143// calculate all products or just active
144 $vk = Vk::getInstance();
145 $vkSettings = $vk->getSettings($this->exportId);
146 if (isset($vkSettings["EXPORT_SETTINGS"]["ONLY_AVAILABLE_FLAG"]) && !$vkSettings["EXPORT_SETTINGS"]["ONLY_AVAILABLE_FLAG"])
147 {
148 $filter["CNT_ACTIVE"] = "N";
149 }
150 else
151 {
152 $filter["CNT_ACTIVE"] = "Y";
153 }
154
155// get ALL sections for ALL catalog iblocks
156 $resSections = \CIBlockSection::GetList(
157 array("LEFT_MARGIN" => "asc"),
158 $filter,
159 true,
160 array(
161 "IBLOCK_ID", "IBLOCK_SECTION_ID", "ID", "DEPTH_LEVEL", "NAME", "LEFT_MARGIN", "RIGHT_MARGIN",
162 "ELEMENT_CNT",
163 )
164 );
165
166 while ($currSection = $resSections->Fetch())
167 {
168// output format - list or tree separated on iblocks
169 $sections[$currSection["IBLOCK_ID"]][$currSection["ID"]] = $currSection;
170 }
171
172 $cacheManager->set($this->createCacheIdSections($onlyMapped), $sections);
173 }
174
175// if not a tree - formatted to list
176 if (!$tree)
177 {
178 $sectionsList = [];
179 foreach ($sections as $iblock)
180 {
181 $sectionsList += $iblock;
182 }
183
184 $sections = $sectionsList;
185 }
186
187 return $sections;
188 }
189
190
191 protected function getIblockIds($onlyMapped)
192 {
193 $iblockIds = [];
194
195 if ($onlyMapped)
196 {
197 $iblockIds = $this->getMappedIblocks();
198 }
199 else
200 {
201 if (Loader::includeModule('catalog'))
202 {
203 $iterator = \Bitrix\Catalog\CatalogIblockTable::getList([
204 'select' => ['IBLOCK_ID'],
205 'filter' => ['=PRODUCT_IBLOCK_ID' => 0],
206 ]);
207 while ($row = $iterator->fetch())
208 {
209 $iblockIds[$row['IBLOCK_ID']] = $row['IBLOCK_ID'];
210 }
211 }
212 }
213
214return $iblockIds;
215}
216
217
223 public function getMappedIblocks()
224 {
225// todo: maybe need cache too?
226// get iblocks if they not set yet
227 if (empty($this->iblocksIds))
228 {
229 foreach ($this->mappedSections as $mappedSection)
230 {
231 $id = $mappedSection["PARAMS"]["IBLOCK"];
232 $this->iblocksIds[$id] = $id;
233 }
234 }
235
236 return $this->iblocksIds;
237 }
238
239
247 {
248 $sectionsToExport = array();
249 foreach ($this->mappedSections as $mappedSection)
250 {
251 $params = $mappedSection["PARAMS"];
252 $parentParams = $params["PARENT_SETTINGS"];
253
254// put current section to export, if them enabled and not inherit, or if parent section enabled
255 if (
256 (!$params["INHERIT"] && $params['ENABLE']) ||
257 ($params["INHERIT"] && $parentParams && $parentParams["ENABLE"])
258 )
259 {
260 $sectionsToExport[$params["IBLOCK"]][$mappedSection["BX_ID"]] = $mappedSection["BX_ID"];
261 }
262 }
263
264 return $sectionsToExport;
265 }
266
267
268 private function getListMappedSections()
269 {
270 $result = array();
271
272 foreach ($this->mappedSections as $mappedSection)
273 {
274 $params = $mappedSection["PARAMS"];
275 $parentParams = $params["PARENT_SETTINGS"];
276
277// not inherit - check pur params
278 if (!$params["INHERIT"] && $params["ENABLE"])
279 {
280 $result[$mappedSection["BX_ID"]] = array(
281 "TO_ALBUM" => $params["TO_ALBUM"],
282 "BX_ID" => $mappedSection["BX_ID"],
283 "VK_CATEGORY" => $params["VK_CATEGORY"],
284 "IBLOCK" => $params["IBLOCK"],
285 );
286// alias get from settings. If not set - do nothing (will be used default name)
287 if ($params["TO_ALBUM_ALIAS"])
288 {
289 $result[$mappedSection["BX_ID"]]["TO_ALBUM_ALIAS"] = $params["TO_ALBUM_ALIAS"];
290 }
291 }
292
293// inherit - get params from parent. If not include child - import in self album
294 elseif ($params["INHERIT"] && $parentParams && $parentParams["ENABLE"] && !$parentParams["INCLUDE_CHILDS"])
295 {
296 $result[$mappedSection["BX_ID"]] = array(
297 "TO_ALBUM" => $mappedSection["BX_ID"],
298 "BX_ID" => $mappedSection["BX_ID"],
299 "VK_CATEGORY" => $parentParams["VK_CATEGORY"],
300 "IBLOCK" => $params["IBLOCK"],
301 );
302 }
303
304// if INHERIT and parent section included childs - put section to parent to_album
305 elseif ($params["INHERIT"] && $parentParams && $parentParams["ENABLE"] && $parentParams["INCLUDE_CHILDS"])
306 {
307 $result[$mappedSection["BX_ID"]] = array(
308 "TO_ALBUM" => $parentParams["TO_ALBUM"],
309 "BX_ID" => $mappedSection["BX_ID"],
310 "VK_CATEGORY" => $params["VK_CATEGORY"],
311 "IBLOCK" => $params["IBLOCK"],
312 );
313
314// alias get from settings. If not set - do nothing (will be used default name)
315 if ($parentParams["TO_ALBUM_ALIAS"])
316 {
317 $result[$mappedSection["BX_ID"]]["TO_ALBUM_ALIAS"] = $parentParams["TO_ALBUM_ALIAS"];
318 }
319 }
320 }
321
322 return $result;
323 }
324
325
333 {
334 $sectionsToExport = array();
335 $sectionsAliases = array();
336
337 foreach ($this->mappedSections as $mappedSection)
338 {
339 $params = $mappedSection["PARAMS"];
340 $parentParams = $params["PARENT_SETTINGS"];
341
342// not inherit - check pur params
343 if (!$params["INHERIT"] && $params["ENABLE"])
344 {
345 $sectionsToExport[$params["TO_ALBUM"]] = $params["TO_ALBUM"];
346// alias get from settings. If not set - do nothing (will be used default name)
347 if ($params["TO_ALBUM_ALIAS"])
348 {
349 $sectionsAliases[$params["TO_ALBUM"]] = $params["TO_ALBUM_ALIAS"];
350 }
351 }
352
353// inherit - get params from parent. If not include child - import in self album
354 elseif ($params["INHERIT"] && $parentParams && $parentParams["ENABLE"] && !$parentParams["INCLUDE_CHILDS"])
355 {
356 $sectionsToExport[$mappedSection["BX_ID"]] = $mappedSection["BX_ID"];
357// alias get from settings. If not set - do nothing (will be used default name)
358 if ($parentParams["TO_ALBUM_ALIAS"])
359 {
360 $sectionsAliases[$parentParams["TO_ALBUM"]] = $parentParams["TO_ALBUM_ALIAS"];
361 }
362 }
363
364// if INHERIT and parent section included childs - put section to parent to_album
365 elseif ($params["INHERIT"] && $parentParams && $parentParams["ENABLE"] && $parentParams["INCLUDE_CHILDS"])
366 {
367 $sectionsToExport[$parentParams["TO_ALBUM"]] = $parentParams["TO_ALBUM"];
368// alias get from settings. If not set - do nothing (will be used default name)
369 if ($parentParams["TO_ALBUM_ALIAS"])
370 {
371 $sectionsAliases[$parentParams["TO_ALBUM"]] = $parentParams["TO_ALBUM_ALIAS"];
372 }
373 }
374 }
375
376 return array("SECTIONS" => $sectionsToExport, "ALIASES" => $sectionsAliases);
377 }
378
379
380 public function getMultiSectionsToProduct($pdoructsIds)
381 {
382 $sections = SectionElementTable::getList(array(
383 "filter" => array(
384 "IBLOCK_ELEMENT_ID" => $pdoructsIds,
385 "ADDITIONAL_PROPERTY_ID" => null,
386 ),
387 ));
388
389 $result = array();
390 while ($section = $sections->fetch())
391 {
392 $result[$section["IBLOCK_ELEMENT_ID"]][] = $section["IBLOCK_SECTION_ID"];
393 }
394
395 return $result;
396 }
397
398
402 public function getSectionMapToPrint()
403 {
404 $mappedSectionGroupped = $this->getSectionsToMap();
405 $result = '<table class="internal">';
406 $result .= '
407 <tr class="heading">
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>
411 </tr>
412 ';
413
414 foreach ($mappedSectionGroupped as $currAlbum)
415 {
416 $result .= '<tr>';
417 $result .= '<td>';
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"];
421 $result .= '</td>';
422 $result .= '<td class="bx-digit-cell" >' . $currAlbum["ELEMENT_CNT"] . '</td>';
423
424// collect SECTIONS
425 if (count($currAlbum["ITEMS"]) > 0)
426 {
427 $items = '';
428 foreach ($currAlbum["ITEMS"] as $currSection)
429 {
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> ' :
434 '';
435 $items .= '</div>';
436 }
437 $result .= '<td>' . $items . '</td>';
438 }
439 else
440 {
441 $result .= '<td>' . Loc::getMessage("VK_EXPORT_MAP__NO_SECTIONS") . '</td>';
442 }
443 $result .= '</tr>';
444 }
445 $result .= '</table>';
446
447 return $result;
448 }
449
450
457 public function getSectionsToMap()
458 {
459 if (empty($this->mappedAlbums))
460 {
461 $this->mappedAlbums = Map::getMappedAlbums($this->exportId);
462 }
463 if (empty($this->mappedSections))
464 {
465 $this->mappedSections = Map::getMappedSections($this->exportId);
466 }
467
468 $sectionsUnformatted = $this->getListMappedSections();
469 $sectionsFormatted = array();
470
471// Empty settings = empty result. It's law
472 if (empty($this->mappedSections))
473 {
474 return array();
475 }
476
477 $sections = $this->getSections(true);
478 $vkCategories = new VkCategories($this->exportId);
479 $vkCategoriesList = $vkCategories->getList(false);
480
481 foreach ($sectionsUnformatted as $sectionUnformatted)
482 {
483 $currSection = $sections[$sectionUnformatted["IBLOCK"]][$sectionUnformatted["BX_ID"]];
484
485// processing only not empty sections
486// if ($currSection["ELEMENT_CNT"] <= 0)
487// continue;
488
489// if first item to this vk-album
490 if (!array_key_exists($sectionUnformatted["TO_ALBUM"], $sectionsFormatted))
491 {
492 $sectionsFormatted[$sectionUnformatted["TO_ALBUM"]] = array(
493 "TO_ALBUM" => $sectionUnformatted["TO_ALBUM"],
494 "ITEMS" => array(),
495 "ELEMENT_CNT" => 0,
496 );
497
498 if (array_key_exists($sectionUnformatted["TO_ALBUM"], $this->mappedAlbums))
499 {
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);
503 }
504 }
505
506// create toAlbum name only from section, then root for this album. Take alias if exist, or just name
507 if ($sectionUnformatted["TO_ALBUM"] == $sectionUnformatted["BX_ID"])
508 {
509 $sectionsFormatted[$sectionUnformatted["TO_ALBUM"]]["TO_ALBUM_NAME"] = $sectionUnformatted["TO_ALBUM_ALIAS"] ?
510 $sectionUnformatted["TO_ALBUM_ALIAS"] :
511 trim($currSection["NAME"]);
512 }
513
514// format current item
515 $vkCategoryName = $vkCategoriesList[$sectionUnformatted["VK_CATEGORY"]]['NAME'];
516 $sectionName = $currSection["NAME"];
517 $sectionElementCnt = $currSection["ELEMENT_CNT"];
518
519 $item = array(
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"],
528 );
529
530 if (isset($item["BX_ID"]) && isset($item["IBLOCK"]))
531 {
532 $item["SECTION_URL"] = $this->createSectionLink($item["IBLOCK"], $item["BX_ID"]);
533 }
534
535 $sectionsFormatted[$sectionUnformatted["TO_ALBUM"]]["ITEMS"][$item["LEFT_MARGIN"]] = $item;
536 $sectionsFormatted[$sectionUnformatted["TO_ALBUM"]]["ELEMENT_CNT"] += $sectionElementCnt;
537 }
538
539// sorted sections for each album by level, add tabs to names
540 $sectionsFormatted = self::sortMapElementItems($sectionsFormatted);
541
542 return $sectionsFormatted;
543 }
544
545 private static function sortMapElementItems($sectionsFormatted)
546 {
547 $sectionsSorted = array();
548 foreach ($sectionsFormatted as $albumKey => $album)
549 {
550 if (!empty($album["ITEMS"]))
551 {
552 ksort($album["ITEMS"]);
553// add tabs to name, by depth level. Can't use base DEPTH_LEVEL, need rematch
554 $prevDepthLevel = false;
555 $tabsCount = 0;
556 foreach ($album["ITEMS"] as &$item)
557 {
558 if (!$prevDepthLevel)
559 {
560 $prevDepthLevel = $item["DEPTH_LEVEL"];
561 } //first item
562 if ($item["DEPTH_LEVEL"] > $prevDepthLevel)
563 {
564 $tabsCount++;
565 $prevDepthLevel = $item["DEPTH_LEVEL"];
566 }
567 elseif ($item["DEPTH_LEVEL"] < $prevDepthLevel)
568 {
569 $tabsCount--;
570 $prevDepthLevel = $item["DEPTH_LEVEL"];
571 }
572 $item["NAME"] = str_repeat('- ', $tabsCount) . $item["NAME"];
573 }
574 }
575
576 $sectionsSorted[$albumKey] = $album;
577 }
578
579 return $sectionsSorted;
580 }
581
582
588 private function createVkAlbumLink($albumVkId)
589 {
590 $vk = Vk::getInstance();
591 $groupId = str_replace('-', '', $vk->getGroupId($this->exportId));
592
593 if ($groupId)
594 {
595 return Vk::VK_URL . Vk::VK_URL__MARKET_PREFIX . $groupId . Vk::VK_URL__ALBUM_PREFIX . $albumVkId;
596 }
597 else
598 {
599 return false;
600 }
601 }
602
603
610 private function createSectionLink($iblockId, $sectionId)
611 {
612 $sectionTabControlName = 'form_section_' . $iblockId . '_active_tab';
613
614 return \CIBlock::GetAdminSectionEditLink($iblockId, $sectionId, array(
615 $sectionTabControlName => "SALE_TRADING_PLATFORM_edit_trading_platforms",
616 ));
617 }
618
619
626 public function getToAlbumBySection($sectionId)
627 {
628 $mappedSection = $this->mappedSections[$sectionId];
629 $params = $mappedSection["PARAMS"];
630 $parentParams = $params["PARENT_SETTINGS"];
631
632// get params from current section if them enabled
633// or from parent, if them enabled and not INCLUDE_CHILDS
634 if (
635 (!$params["INHERIT"] && $params["ENABLE"]) ||
636 ($params["INHERIT"] && $parentParams && $parentParams["ENABLE"] && !$parentParams["INCLUDE_CHILDS"])
637 )
638 {
639 if (isset($params["TO_ALBUM"]) && $params["TO_ALBUM"])
640// get param TO_ALBUM
641 {
642 return $params["TO_ALBUM"];
643 }
644 else
645// or add in current album
646 {
647 return $sectionId;
648 }
649 }
650
651// if INHERIT and parent section included childs - put section to parent to_album
652 elseif ($params["INHERIT"] && $parentParams && $parentParams["ENABLE"] && $parentParams["INCLUDE_CHILDS"])
653 {
654 return $parentParams["TO_ALBUM"];
655 }
656
657 else
658 {
659 return 0;
660 }
661 }
662
663
671 public function getSectionsSelector($checkedSection = null, $onlyMapped = true)
672 {
673// old variant - get iblocks from map. Will work only when we check at least one section
674// new variant - show ALL iblock in list
675 $iblockIds = $this->getIblockIds($onlyMapped);
676
677// get mapped sections for checking activity
678 $sectionsTree = $this->getSections(true, $onlyMapped);
679
680 $result = '';
681 $result .= '<option value="0">' . Loc::getMessage("SALE_CATALOG_VK_MAIN_ALBUM") . '</option>';
682 foreach ($iblockIds as $iblockId)
683 {
684 if (!isset($sectionsTree[$iblockId]))
685 {
686 continue;
687 }
688 $iblock = \CIBlock::GetByID($iblockId)->GetNext();
689
690 $result .= '<option disabled value="-1">'.
691 mb_strtoupper(is_array($iblock)? $iblock["NAME"] : $iblockId) .
692 '</option>';
693
694// create ITEMS for current iblock
695 foreach ($sectionsTree[$iblockId] as $bxCategory)
696 {
697 $selected = $checkedSection == $bxCategory["ID"] ? ' selected' : '';
698 $result .=
699 '<option' . $selected . ' value="' . $bxCategory["ID"] . '">' .
700 str_repeat('. ', $bxCategory["DEPTH_LEVEL"]) . $bxCategory["NAME"] .
701 '</option>';
702 }
703 }
704
705 return $result;
706 }
707
708
715 public function getVkCategory($sectionId)
716 {
717// get category from mapped
718// if not set - get from VK-settings (if set '-1' = default)
719 $vkCategory = $this->mappedSections[$sectionId]['VK_ID'];
720// todo: or get parent category
721 if (!isset($vkCategory) || $vkCategory <= 0)
722 {
723 $vk = Vk::getInstance();
724 $settings = $vk->getSettings($this->exportId);
725
726 if (isset($settings["EXPORT_SETTINGS"]["CATEGORY_DEFAULT"]))
727 {
728 $vkCategory = $settings["EXPORT_SETTINGS"]["CATEGORY_DEFAULT"];
729 }
730 else
731 {
732 $vkCategory = Vk::VERY_DEFAULT_VK_CATEGORY;
733 } //hardcoooooooooooode
734 }
735
736 return $vkCategory;
737 }
738
739
748 public function prepareSectionToShow($sectionId)
749 {
750 $sections = $this->getSections(false, false);
751 $section = $sections[$sectionId];
752
753 $currParams = $this->mappedSections[$sectionId]['PARAMS'];
754 $parentParams = $currParams["PARENT_SETTINGS"];
755
756// for root section inherit always false
757 if (!$section["IBLOCK_SECTION_ID"])
758 {
759 $currParams['INHERIT'] = false;
760 }
761
762// if not INHERIT - get own settings, Else - find parents
763 if (isset($currParams['INHERIT']) && !$currParams['INHERIT'])
764 {
765 $currParams = $currParams + $this->getDefaultExportParams($sectionId);
766 }
767 else
768 {
769// prepared for correct show to_album and album_alias
770 if (!empty($parentParams))
771 {
772 $currParams = $this->prepareParentSettingToShow($parentParams, $section);
773 }
774// if parent not set - get default
775 else
776 {
777 $currParams = $this->getDefaultExportParams($sectionId);
778 }
779
780// override parent setting
781 $currParams['INHERIT'] = true;
782 }
783
784// add hidden fields for saving PARENTS settings
785 $hiddenParentParams = !empty($parentParams) ?
786 $this->prepareParentSettingToShow($parentParams, $section) :
787 $hiddenParentParams = $this->getDefaultExportParams($sectionId);
788 foreach ($hiddenParentParams as $key => $param)
789 {
790 $currParams[$key . '__PARENT'] = $param;
791 }
792
793 return $currParams;
794 }
795
803 public function prepareSettingsVisibility($params, $sectionId)
804 {
805 $sections = $this->getSections(false, false);
806 $section = $sections[$sectionId];
807// always hide inherit for root sections
808 $params["INHERIT__DISPLAY"] = $section["IBLOCK_SECTION_ID"] ? '' : ' disabled ';
809
810// default
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 ";
816
817// show params only if NOT inherit
818 if (isset($params["INHERIT"]) && !$params["INHERIT"])
819 {
820 $params["ENABLE__DISPLAY"] = '';
821// if not enable - not params
822 if (isset($params["ENABLE"]) && $params["ENABLE"])
823 {
824 $params["VK_CATEGORY__DISPLAY"] = '';
825 $params["TO_ALBUM__DISPLAY"] = '';
826
827// if not common album
828// ALIAS can be showed only if checked TO ALBUM selector
829 if (isset($params["TO_ALBUM"]) && $params["TO_ALBUM"] > 0 && $params["TO_ALBUM"] == $sectionId)
830 {
831 $params["TO_ALBUM_ALIAS__DISPLAY"] = 'display: block';
832 }
833
834 $params["INCLUDE_CHILDS__DISPLAY"] = $params["TO_ALBUM"] > 0 ? "" : " disabled ";
835 }
836 }
837
838// change bool values to CHECKED
839 foreach ($params as $key => $param)
840 {
841 if ($param === true)
842 {
843 $params[$key] = 'checked';
844 }
845 if ($param === false)
846 {
847 $params[$key] = '';
848 }
849 }
850
851 return $params;
852
853 }
854
862 private function prepareParentSettingToShow($settings, $section)
863 {
864 $preparedSettings = array();
865
866 $preparedSettings["ENABLE"] = $settings["ENABLE"];
867 $preparedSettings["INCLUDE_CHILDS"] = false; // always false, tak pravilno
868 $preparedSettings["VK_CATEGORY"] = $settings["VK_CATEGORY"] > 0 ? $settings["VK_CATEGORY"] : Vk::VK_CATEGORY_TO_CHANGE;
869 if ($settings["INCLUDE_CHILDS"])
870 {
871 $preparedSettings["TO_ALBUM"] = $settings["TO_ALBUM"];
872 $preparedSettings["TO_ALBUM_ALIAS"] = $settings["TO_ALBUM_ALIAS"];
873 }
874 else
875 {
876 $preparedSettings["TO_ALBUM"] = $section["ID"];
877 $preparedSettings["TO_ALBUM_ALIAS"] = $section["NAME"];
878 }
879
880 return $preparedSettings;
881 }
882
883
890 private function getDefaultExportParams($sectionId)
891 {
892 $sections = $this->getSections(false, false);
893
894 $vk = Vk::getInstance();
895 $vkSettings = $vk->getSettings($this->exportId);
896 $vkCategory = $vkSettings['EXPORT_SETTINGS']['CATEGORY_DEFAULT'];
897
898 return array(
899 "INHERIT" => true,
900 "ENABLE" => false,
901 "TO_ALBUM" => $sectionId,
902 "TO_ALBUM_ALIAS" => $sections[$sectionId]["NAME"],
903 "INCLUDE_CHILDS" => false,
904 "VK_CATEGORY" => $vkCategory ? $vkCategory : Vk::VK_CATEGORY_TO_CHANGE,
905 );
906 }
907
908
912 public function setCurrSectionSettings($settings)
913 {
914// todo: there we must clear sectionsettings cache
915 if (is_array($settings) && !empty($settings))
916 {
917 $this->currSectionSettings = $settings;
918 }
919 }
920
929 public function prepareSectionToSave($sectionId)
930 {
931 $settings = $this->currSectionSettings;
932 if (empty($settings))
933 {
934 return false;
935 }
936 $sections = $this->getSections(false, false);
937 $iblockId = $sections[$sectionId]["IBLOCK_ID"];
938 $currParentSettings = $this->mappedSections[$sectionId]['PARAMS']['PARENT_SETTINGS'];
939
940 $dataToDelete = array();
941 $settingsToSave = array();
942
943// common params to save
944 $settingsToSave["IBLOCK"] = $iblockId;
945 $settingsToSave["PARENT_SETTINGS"] = $currParentSettings;
946
947// if INHERIT - only one flag, no need settings
948// or if ROOT sections (newer have parent) and not enable - delete them from mapping
949 if (
950 (isset($settings["INHERIT"]) && $settings["INHERIT"]) ||
951 (!$sections[$sectionId]["IBLOCK_SECTION_ID"] && !$settings["ENABLE"])
952 )
953 {
954// if INHERIT and have not parent settings - delete this item
955 if (!$currParentSettings)
956 {
957 $dataToDelete = array(
958 "VALUE_EXTERNAL" => $settings["VK_CATEGORY"] ? $settings["VK_CATEGORY"] : Vk::VERY_DEFAULT_VK_CATEGORY,
959 "VALUE_INTERNAL" => $sectionId,
960 );
961 }
962 else
963 {
964 $settingsToSave["INHERIT"] = true;
965 }
966 }
967 else
968 {
969 $settingsToSave["INHERIT"] = false;
970
971// if disable - only flag
972 if (!$settings["ENABLE"])
973 {
974 $settingsToSave["ENABLE"] = false;
975 }
976
977// ENABLE and not INHERIT
978 else
979 {
980 $settingsToSave["ENABLE"] = true;
981 $settingsToSave["INCLUDE_CHILDS"] = $settings["INCLUDE_CHILDS"] ? true : false;
982 $settingsToSave["VK_CATEGORY"] = $settings["VK_CATEGORY"] != 0 ? $settings["VK_CATEGORY"] : Vk::VK_CATEGORY_TO_CHANGE;
983 if (isset($settings["TO_ALBUM"]) && $settings["TO_ALBUM"] > 0)
984 {
985 $settingsToSave["TO_ALBUM"] = $settings["TO_ALBUM"];
986 if (
987 isset($settings["TO_ALBUM_ALIAS"]) && $settings["TO_ALBUM_ALIAS"] &&
988 $settings["TO_ALBUM"] == $sectionId
989 )
990 {
991 $settingsToSave["TO_ALBUM_ALIAS"] = $settings["TO_ALBUM_ALIAS"];
992 }
993 else
994 {
995 $settingsToSave["TO_ALBUM_ALIAS"] = null;
996 }
997 }
998 else
999 {
1000 $settingsToSave["TO_ALBUM"] = 0;
1001 $settingsToSave["TO_ALBUM_ALIAS"] = null;
1002 }
1003 }
1004 }
1005
1006// VALUE_EXTERNAL is required, but we might be have not this value. Get some default
1007
1008 if (!empty($dataToDelete))
1009 {
1010 return array(
1011 "TO_DELETE" => array(
1012 $sectionId => $dataToDelete,
1013 ),
1014 );
1015 }
1016
1017 else
1018 {
1019 return array(
1020 "TO_SAVE" => array(
1021 $sectionId => array(
1022 "VALUE_EXTERNAL" => $settings["VK_CATEGORY"] ? $settings["VK_CATEGORY"] : Vk::VERY_DEFAULT_VK_CATEGORY,
1023 "VALUE_INTERNAL" => $sectionId,
1024 "PARAMS" => $settingsToSave,
1025 ),
1026 ),
1027 );
1028 }
1029 }
1030
1031
1039 public function prepareChildsToSave($sectionId)
1040 {
1041 $settings = $this->currSectionSettings;
1042 if (empty($settings))
1043 {
1044 return false;
1045 }
1046
1047 $sections = $this->getSections();
1048 $currParentSettings = $this->mappedSections[$sectionId]['PARAMS']['PARENT_SETTINGS'];
1049 $iblockId = $sections[$sectionId]["IBLOCK_ID"];
1050
1051 $currLeftMargin = intval($sections[$sectionId]["LEFT_MARGIN"]);
1052 $currRightMargin = intval($sections[$sectionId]["RIGHT_MARGIN"]);
1053
1054 $dataToSave = array();
1055 $dataToDelete = array();
1056
1057// if INHERIT changed to true - set parent settings instead settings of current section
1058// or if ROOT section, then not enable - delete mapping childs
1059 $needDelete = false;
1060 if ($settings["INHERIT"] || (!$sections[$sectionId]["IBLOCK_SECTION_ID"] && !$settings["ENABLE"]))
1061 {
1062// if have not parent settings - needed delete childs
1063 if (!$currParentSettings)
1064 {
1065 $needDelete = true;
1066 }
1067 else
1068 {
1069 $settings = $currParentSettings;
1070 }
1071 }
1072
1073
1074 foreach ($sections as $section)
1075 {
1076// find CHILDS
1077 if (
1078 intval($section["LEFT_MARGIN"]) > $currLeftMargin &&
1079 intval($section["RIGHT_MARGIN"]) < $currRightMargin &&
1080 $section["IBLOCK_ID"] == $iblockId
1081 )
1082 {
1083// only if childs inherit or empty settings
1084 if ($this->mappedSections[$section["ID"]]["PARAMS"]["INHERIT"] !== false)
1085 {
1086// get childs to delete
1087 if ($needDelete)
1088 {
1089 $dataToDelete[$section["ID"]] = array(
1090 'VALUE_EXTERNAL' => $settings["VK_CATEGORY"] ? $settings["VK_CATEGORY"] : Vk::VK_CATEGORY_TO_CHANGE,
1091 'VALUE_INTERNAL' => $section["ID"],
1092 );
1093 }
1094
1095// get childs to add to mapping
1096 else
1097 {
1098 $dataToSave[$section["ID"]] = array(
1099 "VALUE_EXTERNAL" => $settings["VK_CATEGORY"] ? $settings["VK_CATEGORY"] : Vk::VK_CATEGORY_TO_CHANGE,
1100 "VALUE_INTERNAL" => $section["ID"],
1101 "PARAMS" => array(
1102 "INHERIT" => true,
1103 "IBLOCK" => $iblockId,
1104// save parent settings for get inherit params in future
1105 "PARENT_SETTINGS" => array(
1106 "INHERIT" => false,
1107 "ENABLE" => $settings["ENABLE"] ? true : false,
1108 "TO_ALBUM" => $settings["TO_ALBUM"],
1109 "TO_ALBUM_ALIAS" => $settings["TO_ALBUM_ALIAS"],
1110 "VK_CATEGORY" => $settings["VK_CATEGORY"],
1111 "INCLUDE_CHILDS" => $settings["INCLUDE_CHILDS"] ? true : false,
1112 ),
1113 ),
1114 );
1115 }
1116 }
1117 else
1118 {
1119// if we catch first not inherit child - put parent setting (save other values) to them and end cycle
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"],
1124 );
1125// add parent settings to child params if parent section not deleted
1126 if (!$needDelete)
1127 {
1128 $dataToSave[$section["ID"]]["PARAMS"]["PARENT_SETTINGS"] = array(
1129 "INHERIT" => false,
1130 "ENABLE" => $settings["ENABLE"] ? true : false,
1131 "TO_ALBUM" => $settings["TO_ALBUM"],
1132 "TO_ALBUM_ALIAS" => $settings["TO_ALBUM_ALIAS"],
1133 "VK_CATEGORY" => $settings["VK_CATEGORY"],
1134 "INCLUDE_CHILDS" => $settings["INCLUDE_CHILDS"] ? true : false,
1135 );
1136 }
1137 else
1138 {
1139 unset($dataToSave[$section["ID"]]["PARAMS"]["PARENT_SETTINGS"]);
1140 }
1141 }
1142 }
1143 }
1144
1145 $result = array();
1146 if (!empty($dataToSave))
1147 {
1148 $result['TO_SAVE'] = $dataToSave;
1149 }
1150
1151 if (!empty($dataToDelete))
1152 {
1153 $result['TO_DELETE'] = $dataToDelete;
1154 }
1155
1156 return $result;
1157 }
1158}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getMappedSections($exportId, $sectionId=NULL)
Definition map.php:421
static getMappedAlbums($exportId)
Definition map.php:368
getSectionsSelector($checkedSection=null, $onlyMapped=true)
getSections($tree=false, $onlyMapped=true)