feat(main): Add banner option
This commit is contained in:
+11
-6
@@ -11,16 +11,21 @@ class Config
|
||||
];
|
||||
|
||||
const BLOCK_TYPES = [
|
||||
'html' => 'HTML',
|
||||
'faq' => 'FAQ Accordion',
|
||||
'html' => 'HTML',
|
||||
'faq' => 'FAQ Accordion',
|
||||
'banner' => 'Banner',
|
||||
];
|
||||
|
||||
const ADMIN_CONTROLLERS = [
|
||||
'AdminCustomHtmlParent',
|
||||
'AdminCustomHtmlOverview',
|
||||
'AdminCustomHtmlBlockOverview',
|
||||
const BANNER_IMAGE_ALLOWED_EXTENSIONS = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
];
|
||||
|
||||
const BANNER_IMAGE_MAX_SIZE = 5242880;
|
||||
|
||||
// Order matters: the parent tab must be registered before its children so
|
||||
// Tab::getIdFromClassName('AdminCustomHtmlParent') resolves during install.
|
||||
const TABS = [
|
||||
|
||||
@@ -95,9 +95,13 @@ class CustomHtmlBlockController extends FrameworkBundleAdminController
|
||||
$this->addFlash('error', $this->getErrorMessageForException($e, []));
|
||||
}
|
||||
|
||||
$block = new CustomHtmlBlock($idCustomHtmlBlock);
|
||||
$currentImageUrl = Validate::isLoadedObject($block) ? CustomHtmlBlock::getBannerImageUrl($block->image) : '';
|
||||
|
||||
return $this->render('@Modules/ws_customhtml/views/templates/admin/form/customhtmlblock_edit.html.twig', [
|
||||
'customHtmlBlockForm' => $form->createView(),
|
||||
'idCustomHtmlBlock' => $idCustomHtmlBlock,
|
||||
'currentImageUrl' => $currentImageUrl,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,12 @@
|
||||
namespace Websmid\CustomHtml\Form\DataHandler;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
||||
use Websmid\CustomHtml\Config\Config;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlFaq;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Db;
|
||||
use Exception;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
@@ -21,6 +24,10 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
$block->show_title = (int) $formData['show_title'];
|
||||
$block->custom_class = (string) ($formData['custom_class'] ?? '');
|
||||
$block->content = (string) $formData['content'];
|
||||
$block->description = (string) ($formData['description'] ?? '');
|
||||
$block->extra_description = (string) ($formData['extra_description'] ?? '');
|
||||
$block->link = (string) ($formData['link'] ?? '');
|
||||
$block->image = $this->saveUploadedImage($formData['image'] ?? null, '');
|
||||
$block->active = (int) $formData['active'];
|
||||
$block->ordering = $this->getNextOrdering($idGroup);
|
||||
|
||||
@@ -47,6 +54,10 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
$block->show_title = (int) $formData['show_title'];
|
||||
$block->custom_class = (string) ($formData['custom_class'] ?? '');
|
||||
$block->content = (string) $formData['content'];
|
||||
$block->description = (string) ($formData['description'] ?? '');
|
||||
$block->extra_description = (string) ($formData['extra_description'] ?? '');
|
||||
$block->link = (string) ($formData['link'] ?? '');
|
||||
$block->image = $this->saveUploadedImage($formData['image'] ?? null, $block->image);
|
||||
$block->active = (int) $formData['active'];
|
||||
|
||||
if (!$block->update()) {
|
||||
@@ -58,6 +69,49 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and stores an uploaded banner image, replacing the previous
|
||||
* one if any. Never trusts the client-supplied filename or MIME header —
|
||||
* the real MIME type is detected server-side and a random filename is
|
||||
* generated, so nothing from the upload itself ends up on disk.
|
||||
*/
|
||||
private function saveUploadedImage(?UploadedFile $file, string $currentFilename): string
|
||||
{
|
||||
if (null === $file) {
|
||||
return $currentFilename;
|
||||
}
|
||||
|
||||
if (!$file->isValid()) {
|
||||
throw new Exception('The uploaded image could not be read.');
|
||||
}
|
||||
|
||||
if ($file->getSize() > Config::BANNER_IMAGE_MAX_SIZE) {
|
||||
throw new Exception('The uploaded image is too large (max 5 MB).');
|
||||
}
|
||||
|
||||
$mimeType = $file->getMimeType();
|
||||
$extension = array_search($mimeType, Config::BANNER_IMAGE_ALLOWED_EXTENSIONS, true);
|
||||
|
||||
if (false === $extension) {
|
||||
throw new Exception('The uploaded file is not a supported image type (jpg, png, gif, webp).');
|
||||
}
|
||||
|
||||
$targetDir = CustomHtmlBlock::getBannerImageDir();
|
||||
|
||||
if (!is_dir($targetDir) && !mkdir($targetDir, 0755, true) && !is_dir($targetDir)) {
|
||||
throw new Exception('Unable to create the banner image directory.');
|
||||
}
|
||||
|
||||
$newFilename = bin2hex(random_bytes(16)) . '.' . $extension;
|
||||
$file->move($targetDir, $newFilename);
|
||||
|
||||
if (!empty($currentFilename) && is_file($targetDir . $currentFilename)) {
|
||||
unlink($targetDir . $currentFilename);
|
||||
}
|
||||
|
||||
return $newFilename;
|
||||
}
|
||||
|
||||
private function getNextOrdering(int $idGroup): int
|
||||
{
|
||||
$maxOrdering = Db::getInstance()->getValue(
|
||||
|
||||
@@ -24,6 +24,10 @@ final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
|
||||
'show_title' => (bool) $block->show_title,
|
||||
'custom_class' => $block->custom_class,
|
||||
'content' => $block->content,
|
||||
'image' => null,
|
||||
'description' => $block->description,
|
||||
'extra_description' => $block->extra_description,
|
||||
'link' => $block->link,
|
||||
'faqs' => $this->loadFaqs((int) $id),
|
||||
'active' => (bool) $block->active,
|
||||
];
|
||||
@@ -38,6 +42,10 @@ final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
|
||||
'show_title' => false,
|
||||
'custom_class' => '',
|
||||
'content' => '',
|
||||
'image' => null,
|
||||
'description' => '',
|
||||
'extra_description' => '',
|
||||
'link' => '',
|
||||
'faqs' => [],
|
||||
'active' => true,
|
||||
];
|
||||
|
||||
@@ -6,6 +6,7 @@ use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
|
||||
@@ -35,23 +36,20 @@ class CustomHtmlBlockType extends AbstractType
|
||||
'choices' => $this->groupChoiceProvider->getChoices(),
|
||||
'required' => true,
|
||||
])
|
||||
->add('title', TextType::class, [
|
||||
'label' => $this->translator->trans('Title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => true,
|
||||
])
|
||||
->add('type', ChoiceType::class, [
|
||||
'label' => $this->translator->trans('Block type', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'choices' => array_flip(Config::BLOCK_TYPES),
|
||||
'required' => true,
|
||||
])
|
||||
->add('title', TextType::class, [
|
||||
'label' => $this->translator->trans('Title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => true,
|
||||
'row_attr' => ['class' => 'col-8'],
|
||||
])
|
||||
->add('show_title', SwitchType::class, [
|
||||
'label' => $this->translator->trans('Show title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
])
|
||||
->add('custom_class', TextType::class, [
|
||||
'label' => $this->translator->trans('Custom classes', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'attr' => ['placeholder' => 'col-md-6'],
|
||||
'row_attr' => ['class' => 'col-3'],
|
||||
])
|
||||
->add('content', TextareaType::class, [
|
||||
'label' => $this->translator->trans('Content', [], 'Modules.Wscustomhtml.Admin'),
|
||||
@@ -59,6 +57,11 @@ class CustomHtmlBlockType extends AbstractType
|
||||
'attr' => ['class' => 'autoload_rte'],
|
||||
'row_attr' => ['class' => 'customhtmlblock-type-field customhtmlblock-type-html'],
|
||||
])
|
||||
->add('image', FileType::class, [
|
||||
'label' => $this->translator->trans('Banner image', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'row_attr' => ['class' => 'customhtmlblock-type-field customhtmlblock-type-banner'],
|
||||
])
|
||||
->add('faqs', CollectionType::class, [
|
||||
'entry_type' => CustomHtmlFaqType::class,
|
||||
'allow_add' => true,
|
||||
@@ -67,6 +70,27 @@ class CustomHtmlBlockType extends AbstractType
|
||||
'prototype' => true,
|
||||
'required' => false,
|
||||
])
|
||||
->add('description', TextareaType::class, [
|
||||
'label' => $this->translator->trans('Description', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'row_attr' => ['class' => 'customhtmlblock-type-field customhtmlblock-type-banner'],
|
||||
])
|
||||
->add('extra_description', TextareaType::class, [
|
||||
'label' => $this->translator->trans('Extra description', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'row_attr' => ['class' => 'customhtmlblock-type-field customhtmlblock-type-banner'],
|
||||
])
|
||||
->add('link', TextType::class, [
|
||||
'label' => $this->translator->trans('Link', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'attr' => ['placeholder' => 'https://example.com or /internal-page'],
|
||||
'row_attr' => ['class' => 'customhtmlblock-type-field customhtmlblock-type-banner'],
|
||||
])
|
||||
->add('custom_class', TextType::class, [
|
||||
'label' => $this->translator->trans('Custom classes', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'attr' => ['placeholder' => 'col-md-6'],
|
||||
])
|
||||
->add('active', SwitchType::class, [
|
||||
'label' => $this->translator->trans('Active', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
|
||||
@@ -25,6 +25,18 @@ class CustomHtmlBlock extends ObjectModel
|
||||
/** @var string */
|
||||
public $custom_class = '';
|
||||
|
||||
/** @var string */
|
||||
public $description = '';
|
||||
|
||||
/** @var string */
|
||||
public $extra_description = '';
|
||||
|
||||
/** @var string */
|
||||
public $link = '';
|
||||
|
||||
/** @var string */
|
||||
public $image = '';
|
||||
|
||||
/** @var int */
|
||||
public $active = 1;
|
||||
|
||||
@@ -49,6 +61,10 @@ class CustomHtmlBlock extends ObjectModel
|
||||
'type' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32],
|
||||
'content' => ['type' => self::TYPE_HTML, 'validate' => 'isCleanHtml'],
|
||||
'custom_class' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 128],
|
||||
'description' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 1024],
|
||||
'extra_description' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 1024],
|
||||
'link' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 512],
|
||||
'image' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 255],
|
||||
'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
|
||||
'show_title' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
|
||||
'ordering' => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
|
||||
@@ -57,6 +73,20 @@ class CustomHtmlBlock extends ObjectModel
|
||||
],
|
||||
];
|
||||
|
||||
public static function getBannerImageDir(): string
|
||||
{
|
||||
return _PS_MODULE_DIR_ . 'ws_customhtml/views/img/banners/';
|
||||
}
|
||||
|
||||
public static function getBannerImageUrl(string $filename): string
|
||||
{
|
||||
if (empty($filename)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return _MODULE_DIR_ . 'ws_customhtml/views/img/banners/' . $filename;
|
||||
}
|
||||
|
||||
public static function getActiveByGroupIds(array $groupIds): array
|
||||
{
|
||||
$groupIds = array_map('intval', $groupIds);
|
||||
@@ -66,7 +96,8 @@ class CustomHtmlBlock extends ObjectModel
|
||||
}
|
||||
|
||||
$result = Db::getInstance()->executeS(
|
||||
'SELECT `id_ws_customhtml`, `id_ws_customhtml_group`, `title`, `type`, `content`, `custom_class`, `active`, `show_title`, `ordering`
|
||||
'SELECT `id_ws_customhtml`, `id_ws_customhtml_group`, `title`, `type`, `content`, `custom_class`,
|
||||
`description`, `extra_description`, `link`, `image`, `active`, `show_title`, `ordering`
|
||||
FROM `' . _DB_PREFIX_ . 'ws_customhtml`
|
||||
WHERE `id_ws_customhtml_group` IN (' . implode(',', $groupIds) . ')
|
||||
AND `active` = 1
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Websmid\CustomHtml\Module;
|
||||
|
||||
use Websmid\CustomHtml\Config\Config;
|
||||
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Data\GridData;
|
||||
use PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection;
|
||||
use Tools;
|
||||
@@ -21,9 +22,10 @@ trait AdminHooks
|
||||
|
||||
public function hookActionAdminControllerSetMedia(): void
|
||||
{
|
||||
$controllerName = $this->context->controller->controller_name ?? '';
|
||||
$request = SymfonyContainer::getInstance()->get('request_stack')->getCurrentRequest();
|
||||
$routeName = $request ? (string) $request->attributes->get('_route') : '';
|
||||
|
||||
if (!in_array($controllerName, Config::ADMIN_CONTROLLERS, true)) {
|
||||
if (0 !== strpos($routeName, 'customhtml')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ trait DisplayHooks
|
||||
|
||||
$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']] ?? [];
|
||||
@@ -88,4 +89,17 @@ trait DisplayHooks
|
||||
}, $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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,10 @@ trait Install
|
||||
`type` VARCHAR(32) NOT NULL DEFAULT \'html\',
|
||||
`content` LONGTEXT,
|
||||
`custom_class` VARCHAR(128) NOT NULL DEFAULT \'\',
|
||||
`description` TEXT,
|
||||
`extra_description` TEXT,
|
||||
`link` VARCHAR(512) NOT NULL DEFAULT \'\',
|
||||
`image` VARCHAR(255) NOT NULL DEFAULT \'\',
|
||||
`active` TINYINT(1) NOT NULL DEFAULT 1,
|
||||
`show_title` TINYINT(1) NOT NULL DEFAULT 0,
|
||||
`ordering` INT(11) NOT NULL DEFAULT 0,
|
||||
|
||||
Reference in New Issue
Block a user