feat(main): Add slider block type
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
|
||||
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
|
||||
@@ -70,6 +72,7 @@ trait DisplayHooks
|
||||
$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']] ?? [];
|
||||
@@ -107,4 +110,67 @@ trait DisplayHooks
|
||||
}, $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) . '%';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,15 @@ trait Install
|
||||
`description` TEXT,
|
||||
`extra_description` TEXT,
|
||||
`link` VARCHAR(512) NOT NULL DEFAULT \'\',
|
||||
`link_text` VARCHAR(128) NOT NULL DEFAULT \'\',
|
||||
`image` VARCHAR(255) NOT NULL DEFAULT \'\',
|
||||
`alert_type` VARCHAR(16) NOT NULL DEFAULT \'info\',
|
||||
`timeout` INT(11) NOT NULL DEFAULT 0,
|
||||
`slider_mode` VARCHAR(16) NOT NULL DEFAULT \'always\',
|
||||
`slides_per_view_mobile` DECIMAL(4,2) NOT NULL DEFAULT 1,
|
||||
`slides_per_view_tablet` DECIMAL(4,2) NOT NULL DEFAULT 2,
|
||||
`slides_per_view_desktop` DECIMAL(4,2) NOT NULL DEFAULT 3,
|
||||
`autoplay_delay` INT(11) NOT NULL DEFAULT 0,
|
||||
`active` TINYINT(1) NOT NULL DEFAULT 1,
|
||||
`show_title` TINYINT(1) NOT NULL DEFAULT 0,
|
||||
`ordering` INT(11) NOT NULL DEFAULT 0,
|
||||
@@ -92,6 +100,23 @@ trait Install
|
||||
);
|
||||
';
|
||||
|
||||
$sqlToExec[] = '
|
||||
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_slide (
|
||||
`id_ws_customhtml_slide` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`id_ws_customhtml` INT(11) NOT NULL,
|
||||
`title` VARCHAR(256) NOT NULL DEFAULT \'\',
|
||||
`description` TEXT,
|
||||
`extra_description` TEXT,
|
||||
`link` VARCHAR(512) NOT NULL DEFAULT \'\',
|
||||
`link_text` VARCHAR(128) NOT NULL DEFAULT \'\',
|
||||
`image` VARCHAR(255) NOT NULL DEFAULT \'\',
|
||||
`ordering` INT(11) NOT NULL DEFAULT 0,
|
||||
`date_add` DATETIME NOT NULL,
|
||||
`date_upd` DATETIME NOT NULL,
|
||||
KEY `id_ws_customhtml` (`id_ws_customhtml`)
|
||||
);
|
||||
';
|
||||
|
||||
foreach ($sqlToExec as $sqlBlock) {
|
||||
Db::getInstance()->execute(trim($sqlBlock));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user