context->controller->registerStylesheet( 'ws_customhtml_front', '/modules/' . $this->name . '/views/css/front/customhtml.css' ); $this->context->controller->registerJavascript( 'ws_customhtml_front', '/modules/' . $this->name . '/views/js/front/customhtml.js' ); } public function renderWidget($hookName = null, array $configuration = []) { if (empty($hookName)) { return false; } $idLang = (int) Context::getContext()->language->id; $templateFile = 'module:ws_customhtml/views/templates/hook/customhtml_block.tpl'; $cacheId = $this->getCacheId('ws_customhtml_' . $idLang . '_' . $hookName); if (!$this->isCached($templateFile, $cacheId)) { $variables = $this->getWidgetVariables($hookName, $configuration); if (empty($variables)) { return false; } $this->smarty->assign($variables); } return $this->fetch($templateFile, $cacheId); } public function getWidgetVariables($hookName = null, array $configuration = []) { $idLang = (int) Context::getContext()->language->id; $groupList = $this->loadActiveGroupsWithBlocks((string) $hookName, $idLang); if (empty($groupList)) { return []; } return [ 'hookName' => $hookName, 'groupList' => $groupList, ]; } private function loadActiveGroupsWithBlocks(string $hookName, int $idLang): array { $groups = CustomHtmlGroup::getActiveByHookName($hookName); if (empty($groups)) { return []; } $blocksByGroup = CustomHtmlBlock::getActiveByGroupIds(array_column($groups, 'id_ws_customhtml_group'), $idLang); $blocksByGroup = $this->attachFaqsToBlocks($blocksByGroup, $idLang); $blocksByGroup = $this->attachImageUrls($blocksByGroup); $blocksByGroup = $this->attachSlidesToBlocks($blocksByGroup, $idLang); $groups = array_map(static function (array $group) use ($blocksByGroup) { $group['blocks'] = $blocksByGroup[(int) $group['id_ws_customhtml_group']] ?? []; return $group; }, $groups); return array_values(array_filter($groups, static function (array $group) { return !empty($group['blocks']); })); } private function attachFaqsToBlocks(array $blocksByGroup, int $idLang): array { return array_map(static function (array $blocks) use ($idLang) { return array_map(static function (array $block) use ($idLang) { if ($block['type'] === 'faq') { $block['faqs'] = CustomHtmlFaq::getByBlockIdForLang((int) $block['id_ws_customhtml'], $idLang); } return $block; }, $blocks); }, $blocksByGroup); } private function attachImageUrls(array $blocksByGroup): array { return array_map(static function (array $blocks) { return array_map(static function (array $block) { if (in_array($block['type'], ['banner', 'usp'], true)) { $block['image_url'] = CustomHtmlBlock::getBannerImageUrl($block['image']); } return $block; }, $blocks); }, $blocksByGroup); } private function attachSlidesToBlocks(array $blocksByGroup, int $idLang): array { return array_map(function (array $blocks) use ($idLang) { return array_map(function (array $block) use ($idLang) { if ($block['type'] === 'slider') { $block['slides'] = $this->loadSlidesForBlock((int) $block['id_ws_customhtml'], $idLang); $block['slider_config'] = $this->buildSliderConfig($block); $block['slider_desktop_column_width'] = $this->buildSliderDesktopColumnWidth($block); } return $block; }, $blocks); }, $blocksByGroup); } private function loadSlidesForBlock(int $idBlock, int $idLang): array { return array_map(static function (array $slide) { $slide['image_url'] = CustomHtmlBlock::getBannerImageUrl($slide['image']); $slide['show_title'] = true; return $slide; }, CustomHtmlSlide::getByBlockIdForLang($idBlock, $idLang)); } private function buildSliderConfig(array $block): string { $config = [ 'slidesPerView' => (float) $block['slides_per_view_mobile'], 'spaceBetween' => 16, 'speed' => 500, 'watchOverflow' => true, ]; if ($block['slider_mode'] === 'responsive') { $config['breakpoints'] = [ Config::SLIDER_RESPONSIVE_BREAKPOINT => ['slidesPerView' => (float) $block['slides_per_view_tablet']], ]; } else { $config['breakpoints'] = [ 768 => ['slidesPerView' => (float) $block['slides_per_view_tablet']], ]; if ((float) $block['slides_per_view_desktop'] > 0) { $config['breakpoints'][992] = ['slidesPerView' => (float) $block['slides_per_view_desktop']]; } } if ((int) $block['autoplay_delay'] > 0) { $config['loop'] = true; $config['autoplay'] = [ 'delay' => (int) $block['autoplay_delay'] * 1000, 'disableOnInteraction' => false, ]; } return json_encode($config); } private function buildSliderDesktopColumnWidth(array $block): ?string { if ((float) $block['slides_per_view_desktop'] <= 0) { return null; } $columns = max(1, (int) round((float) $block['slides_per_view_desktop'])); return round(100 / $columns, 4) . '%'; } }