177 lines
5.8 KiB
PHP
177 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Websmid\CustomHtml\Module;
|
|
|
|
use Websmid\CustomHtml\Config\Config;
|
|
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
|
use Websmid\CustomHtml\Model\CustomHtmlFaq;
|
|
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
|
use Websmid\CustomHtml\Model\CustomHtmlSlide;
|
|
use Context;
|
|
|
|
trait DisplayHooks
|
|
{
|
|
public function hookActionFrontControllerSetMedia(): void
|
|
{
|
|
$this->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 = [])
|
|
{
|
|
$groupList = $this->loadActiveGroupsWithBlocks((string) $hookName);
|
|
|
|
if (empty($groupList)) {
|
|
return [];
|
|
}
|
|
|
|
return [
|
|
'hookName' => $hookName,
|
|
'groupList' => $groupList,
|
|
];
|
|
}
|
|
|
|
private function loadActiveGroupsWithBlocks(string $hookName): array
|
|
{
|
|
$groups = CustomHtmlGroup::getActiveByHookName($hookName);
|
|
|
|
if (empty($groups)) {
|
|
return [];
|
|
}
|
|
|
|
$blocksByGroup = CustomHtmlBlock::getActiveByGroupIds(array_column($groups, 'id_ws_customhtml_group'));
|
|
$blocksByGroup = $this->attachFaqsToBlocks($blocksByGroup);
|
|
$blocksByGroup = $this->attachImageUrls($blocksByGroup);
|
|
$blocksByGroup = $this->attachSlidesToBlocks($blocksByGroup);
|
|
|
|
$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): array
|
|
{
|
|
return array_map(static function (array $blocks) {
|
|
return array_map(static function (array $block) {
|
|
if ($block['type'] === 'faq') {
|
|
$block['faqs'] = CustomHtmlFaq::getByBlockId((int) $block['id_ws_customhtml']);
|
|
}
|
|
|
|
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): array
|
|
{
|
|
return array_map(function (array $blocks) {
|
|
return array_map(function (array $block) {
|
|
if ($block['type'] === 'slider') {
|
|
$block['slides'] = $this->loadSlidesForBlock((int) $block['id_ws_customhtml']);
|
|
$block['slider_config'] = $this->buildSliderConfig($block);
|
|
$block['slider_desktop_column_width'] = $this->buildSliderDesktopColumnWidth($block);
|
|
}
|
|
|
|
return $block;
|
|
}, $blocks);
|
|
}, $blocksByGroup);
|
|
}
|
|
|
|
private function loadSlidesForBlock(int $idBlock): array
|
|
{
|
|
return array_map(static function (array $slide) {
|
|
$slide['image_url'] = CustomHtmlBlock::getBannerImageUrl($slide['image']);
|
|
$slide['show_title'] = true;
|
|
|
|
return $slide;
|
|
}, CustomHtmlSlide::getByBlockId($idBlock));
|
|
}
|
|
|
|
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']],
|
|
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
|
|
{
|
|
$columns = max(1, (int) round((float) $block['slides_per_view_desktop']));
|
|
|
|
return round(100 / $columns, 4) . '%';
|
|
}
|
|
}
|