feat(main): Add banner option
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user