106 lines
3.2 KiB
PHP
106 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace Websmid\CustomHtml\Module;
|
|
|
|
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
|
use Websmid\CustomHtml\Model\CustomHtmlFaq;
|
|
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
|
use Context;
|
|
|
|
trait DisplayHooks
|
|
{
|
|
public function hookActionFrontControllerSetMedia(): void
|
|
{
|
|
$this->context->controller->registerStylesheet(
|
|
'ws_customhtml_front',
|
|
'/modules/' . $this->name . '/views/css/front/customhtml.css'
|
|
);
|
|
}
|
|
|
|
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->attachBannerImageUrls($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 attachBannerImageUrls(array $blocksByGroup): array
|
|
{
|
|
return array_map(static function (array $blocks) {
|
|
return array_map(static function (array $block) {
|
|
if ($block['type'] === 'banner') {
|
|
$block['image_url'] = CustomHtmlBlock::getBannerImageUrl($block['image']);
|
|
}
|
|
|
|
return $block;
|
|
}, $blocks);
|
|
}, $blocksByGroup);
|
|
}
|
|
}
|