PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/blocks/bitrix/store.catalog.sections_carousel/class.php

https://gitlab.com/alexprowars/bitrix
PHP | 393 lines | 333 code | 29 blank | 31 comment | 45 complexity | d12e00a26b73a20ed96ec672d76a068c MD5 | raw file
  1. <?php
  2. if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true)
  3. {
  4. die();
  5. }
  6. use Bitrix\Landing\Hook\Page\Settings;
  7. use Bitrix\Main\ModuleManager;
  8. use Bitrix\Main\Loader;
  9. use Bitrix\Iblock;
  10. use Bitrix\Catalog;
  11. class StoreCatalogSectionsCarousel extends \Bitrix\Landing\LandingBlock
  12. {
  13. protected $iblockIncluded;
  14. protected $catalogIncluded;
  15. /**
  16. * Set cart position (top, left, ...).
  17. * @return void
  18. */
  19. protected function setCartPosition()
  20. {
  21. if (!isset($this->params['CART_POSITION']))
  22. {
  23. $this->params['CART_POSITION_HORIZONTAL'] = 'left';
  24. $this->params['CART_POSITION_VERTICAL'] = 'bottom';
  25. }
  26. else
  27. {
  28. switch ($this->params['CART_POSITION'])
  29. {
  30. case 'TC':
  31. {
  32. $this->params['CART_POSITION_HORIZONTAL'] = 'hcenter';
  33. $this->params['CART_POSITION_VERTICAL'] = 'top';
  34. break;
  35. }
  36. case 'TR':
  37. {
  38. $this->params['CART_POSITION_HORIZONTAL'] = 'right';
  39. $this->params['CART_POSITION_VERTICAL'] = 'top';
  40. break;
  41. }
  42. case 'CR':
  43. {
  44. $this->params['CART_POSITION_HORIZONTAL'] = 'right';
  45. $this->params['CART_POSITION_VERTICAL'] = 'vcenter';
  46. break;
  47. }
  48. case 'BR':
  49. {
  50. $this->params['CART_POSITION_HORIZONTAL'] = 'right';
  51. $this->params['CART_POSITION_VERTICAL'] = 'bottom';
  52. break;
  53. }
  54. case 'BC':
  55. {
  56. $this->params['CART_POSITION_HORIZONTAL'] = 'hcenter';
  57. $this->params['CART_POSITION_VERTICAL'] = 'bottom';
  58. break;
  59. }
  60. case 'BL':
  61. {
  62. $this->params['CART_POSITION_HORIZONTAL'] = 'left';
  63. $this->params['CART_POSITION_VERTICAL'] = 'bottom';
  64. break;
  65. }
  66. case 'CL':
  67. {
  68. $this->params['CART_POSITION_HORIZONTAL'] = 'left';
  69. $this->params['CART_POSITION_VERTICAL'] = 'vcenter';
  70. break;
  71. }
  72. case 'TL':
  73. {
  74. $this->params['CART_POSITION_HORIZONTAL'] = 'left';
  75. $this->params['CART_POSITION_VERTICAL'] = 'top';
  76. break;
  77. }
  78. default:
  79. {
  80. $this->params['CART_POSITION_HORIZONTAL'] = 'left';
  81. $this->params['CART_POSITION_VERTICAL'] = 'bottom';
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * Method, which will be called once time.
  88. * @param array Params array.
  89. * @return void
  90. */
  91. public function init(array $params = [])
  92. {
  93. $this->iblockIncluded = Loader::includeModule('iblock');
  94. $this->catalogIncluded = Loader::includeModule('catalog');
  95. $this->params = Settings::getDataForSite(
  96. $params['site_id']
  97. );
  98. $this->setCartPosition();
  99. $this->params['SITE_ID'] = $params['site_id'];
  100. $this->params['LANDING_ID'] = $params['landing_id'];
  101. $this->params['LANDING_SECTION_ID'] = ($this->params['SECTION_ID'] ?? 0);
  102. $this->params['ALLOW_SEO_DATA'] = 'Y';
  103. // calc variables
  104. $sectionId = 0;
  105. $sectionCode = '';
  106. $variables = \Bitrix\Landing\Landing::getVariables();
  107. if (isset($variables['sef'][0]))
  108. {
  109. $sectionCode = $variables['sef'][0];
  110. if ($this->iblockIncluded)
  111. {
  112. $sectionId = (int)\CIBlockFindTools::GetSectionIDByCodePath(
  113. $this->params['IBLOCK_ID'],
  114. $sectionCode
  115. );
  116. }
  117. }
  118. // check section id restricted
  119. if ($this->params['SECTION_ID'])
  120. {
  121. if ($this->iblockIncluded && $sectionId > 0)
  122. {
  123. $allowed = false;
  124. $res = \CIBlockSection::getNavChain(
  125. $this->params['IBLOCK_ID'],
  126. $sectionId,
  127. [
  128. 'ID'
  129. ]
  130. );
  131. while ($row = $res->fetch())
  132. {
  133. if ($row['ID'] == $this->params['SECTION_ID'])
  134. {
  135. $allowed = true;
  136. break;
  137. }
  138. }
  139. if (!$allowed)
  140. {
  141. $sectionId = -1;
  142. $sectionCode = -1;
  143. }
  144. }
  145. else
  146. {
  147. $sectionId = $this->params['SECTION_ID'];
  148. }
  149. }
  150. // actions for edit mode
  151. $editMode = \Bitrix\Landing\Landing::getEditMode();
  152. $setStatus404 = $editMode ? 'N' : 'Y';
  153. $setTitle = $editMode || ($sectionId == $this->params['SECTION_ID']) ? 'N' : 'Y';
  154. $siteId = null;
  155. if ($editMode && isset($landing))
  156. {
  157. $siteId = $landing->getSmnSiteId();
  158. }
  159. if (!$siteId)
  160. {
  161. $siteId = \Bitrix\Landing\Manager::getMainSiteId();
  162. }
  163. // check for show cart, personal section, and compare
  164. $showCart = false;
  165. $this->params['SHOW_PERSONAL_LINK'] = 'N';
  166. $this->params['ADD_TO_BASKET_ACTION'] = 'BUY';
  167. $this->params['SECTION_URL'] = '#system_catalog#SECTION_CODE_PATH#/';
  168. $this->params['DETAIL_URL'] = '#system_catalogitem/#ELEMENT_CODE#/';
  169. if (!$editMode && ModuleManager::isModuleInstalled('sale'))
  170. {
  171. $syspages = \Bitrix\Landing\Syspage::get(
  172. $params['site_id']
  173. );
  174. if (
  175. isset($syspages['compare']) &&
  176. $this->params['DISPLAY_COMPARE'] != 'N'
  177. )
  178. {
  179. $this->params['DISPLAY_COMPARE'] = 'Y';
  180. }
  181. else
  182. {
  183. $this->params['DISPLAY_COMPARE'] = 'N';
  184. }
  185. if (isset($syspages['cart']))
  186. {
  187. $showCart = true;
  188. $this->params['ADD_TO_BASKET_ACTION'] = 'ADD';
  189. }
  190. if (isset($syspages['personal']))
  191. {
  192. $this->params['SHOW_PERSONAL_LINK'] = 'Y';
  193. }
  194. if (!isset($syspages['catalog']))
  195. {
  196. $this->params['SECTION_URL'] = $this->params['DETAIL_URL'] = '';
  197. }
  198. }
  199. else
  200. {
  201. $this->params['DISPLAY_COMPARE'] = 'N';
  202. }
  203. $this->params['HIDE_DETAIL_URL'] = ($this->params['DETAIL_URL'] == '') ? 'Y' : 'N';
  204. $this->params['SECTION_ID'] = $sectionId;
  205. $this->params['SECTION_CODE'] = $sectionCode;
  206. $this->params['SHOW_CART'] = $showCart;
  207. $this->params['SET_404'] = $setStatus404;
  208. $this->params['SET_TITLE'] = $setTitle;
  209. $this->params['SITE_ID'] = $siteId;
  210. $this->params['EDIT_MODE'] = $editMode;
  211. }
  212. /**
  213. * Method, which executes just before block.
  214. * @param \Bitrix\Landing\Block $block Block instance.
  215. * @return void
  216. */
  217. public function beforeView(\Bitrix\Landing\Block $block)
  218. {
  219. if (!defined('LANDING_TMP_CATALOG_SHOWED'))
  220. {
  221. define('LANDING_TMP_CATALOG_SHOWED', true);
  222. $this->params['FIRST_TIME'] = true;
  223. }
  224. else
  225. {
  226. $this->params['FIRST_TIME'] = false;
  227. }
  228. $this->params['ACTION_VARIABLE'] = 'action_' . $block->getId();
  229. $this->params['FILTER_NAME'] = 'carouselFilter';
  230. $this->params['CATALOG_FILTER_NAME'] = 'catalogCarouselFilter';
  231. $this->params['SECTIONS_FILTER_NAME'] = 'sectionCarouselFilter';
  232. $this->correctParams();
  233. $this->setElementCarouselFilter();
  234. $this->setSectionCarouselFilter();
  235. }
  236. /**
  237. * Method for clear carousel filter. Page has contain some section blocks.
  238. *
  239. * @internal
  240. *
  241. * @param string $name
  242. * @return void
  243. */
  244. private function clearCarouselFilter(string $name): void
  245. {
  246. if (isset($GLOBALS[$name]))
  247. {
  248. $GLOBALS[$name] = [];
  249. }
  250. }
  251. private function setCarouselFilter(string $name, array $filter): void
  252. {
  253. if ($name === '')
  254. {
  255. return;
  256. }
  257. $currentFilter = $GLOBALS[$name] ?? [];
  258. if (!is_array($currentFilter))
  259. {
  260. $currentFilter = [];
  261. }
  262. $GLOBALS[$name] = array_merge(
  263. $currentFilter,
  264. $filter
  265. );
  266. }
  267. private function setElementCarouselFilter(): void
  268. {
  269. $filterName = $this->get('FILTER_NAME');
  270. $catalogFilterName = $this->get('CATALOG_FILTER_NAME');
  271. $listFilter = [];
  272. $catalogFilter = [];
  273. $currentElementId = $this->getCurrentElement();
  274. if (!empty($currentElementId))
  275. {
  276. $listFilter['!=ID'] = $currentElementId;
  277. $catalogFilter['!=ID'] = $currentElementId;
  278. }
  279. $currentSectionId = (int)$this->get('SECTION_ID');
  280. if (
  281. $currentSectionId > 0
  282. && $currentSectionId !== (int)$this->get('LANDING_SECTION_ID')
  283. )
  284. {
  285. $catalogFilter['!=IBLOCK_SECTION_ID'] = $currentSectionId;
  286. }
  287. if ($this->catalogIncluded)
  288. {
  289. if (class_exists('\Bitrix\Catalog\Product\SystemField\ProductMapping'))
  290. {
  291. $listFilter = Catalog\Product\SystemField\ProductMapping::getExtendedFilterByArea(
  292. $listFilter,
  293. Catalog\Product\SystemField\ProductMapping::MAP_LANDING
  294. );
  295. $catalogFilter = Catalog\Product\SystemField\ProductMapping::getExtendedFilterByArea(
  296. $catalogFilter,
  297. Catalog\Product\SystemField\ProductMapping::MAP_LANDING
  298. );
  299. }
  300. }
  301. if (!empty($listFilter) && $filterName !== null)
  302. {
  303. $this->setCarouselFilter($filterName, $listFilter);
  304. }
  305. if (!empty($catalogFilter) && $catalogFilterName !== null)
  306. {
  307. $this->setCarouselFilter($catalogFilterName, $catalogFilter);
  308. }
  309. }
  310. private function setSectionCarouselFilter(): void
  311. {
  312. $filterName = $this->get('SECTIONS_FILTER_NAME');
  313. $this->clearCarouselFilter($filterName);
  314. $currentSectionId = (int)$this->get('SECTION_ID');
  315. if ($currentSectionId > 0)
  316. {
  317. $this->setCarouselFilter($filterName, ['!=ID' => $currentSectionId]);
  318. }
  319. }
  320. /**
  321. * Returns element Id, if current page - is detail.
  322. *
  323. * @return int|null
  324. */
  325. private function getCurrentElement(): ?int
  326. {
  327. $result = 0;
  328. if (isset($GLOBALS['CURRENT_PRODUCT_ID']))
  329. {
  330. $result = (int)$GLOBALS['CURRENT_PRODUCT_ID'];
  331. }
  332. return ($result > 0 ? $result : null);
  333. }
  334. private function correctParams(): void
  335. {
  336. if (!$this->iblockIncluded)
  337. {
  338. return;
  339. }
  340. $currentElementId = $this->getCurrentElement();
  341. if (!empty($currentElementId))
  342. {
  343. $this->params['ALLOW_SEO_DATA'] = 'N';
  344. $iterator = Iblock\ElementTable::getList([
  345. 'select' => ['ID', 'IBLOCK_SECTION_ID'],
  346. 'filter' => [
  347. '=ID' => $currentElementId,
  348. '=IBLOCK_ID' => (int)$this->get('IBLOCK_ID')
  349. ]
  350. ]);
  351. $row = $iterator->fetch();
  352. unset($iterator);
  353. if (!empty($row))
  354. {
  355. $sectionId = (int)$row['IBLOCK_SECTION_ID'];
  356. if ($sectionId > 0)
  357. {
  358. $this->params['SECTION_ID'] = $sectionId;
  359. }
  360. $this->params['SECTION_CODE'] = '';
  361. }
  362. }
  363. }
  364. }