feat(main): Add slider block type
This commit is contained in:
@@ -6,6 +6,7 @@ use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandl
|
||||
use Websmid\CustomHtml\Config\Config;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlFaq;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlSlide;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Db;
|
||||
use Exception;
|
||||
@@ -27,8 +28,14 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
$block->description = (string) ($formData['description'] ?? '');
|
||||
$block->extra_description = (string) ($formData['extra_description'] ?? '');
|
||||
$block->link = (string) ($formData['link'] ?? '');
|
||||
$block->link_text = (string) ($formData['link_text'] ?? '');
|
||||
$block->alert_type = (string) ($formData['alert_type'] ?? 'info');
|
||||
$block->timeout = (int) ($formData['timeout'] ?? 0);
|
||||
$block->slider_mode = (string) ($formData['slider_mode'] ?? Config::SLIDER_DEFAULT_MODE);
|
||||
$block->slides_per_view_mobile = (float) ($formData['slides_per_view_mobile'] ?? Config::SLIDER_DEFAULT_SLIDES_PER_VIEW_MOBILE);
|
||||
$block->slides_per_view_tablet = (float) ($formData['slides_per_view_tablet'] ?? Config::SLIDER_DEFAULT_SLIDES_PER_VIEW_TABLET);
|
||||
$block->slides_per_view_desktop = (float) ($formData['slides_per_view_desktop'] ?? Config::SLIDER_DEFAULT_SLIDES_PER_VIEW_DESKTOP);
|
||||
$block->autoplay_delay = (int) ($formData['autoplay_delay'] ?? 0);
|
||||
$block->image = $this->saveUploadedImage($formData['image'] ?? null, '');
|
||||
$block->active = (int) $formData['active'];
|
||||
$block->ordering = $this->getNextOrdering($idGroup);
|
||||
@@ -38,6 +45,7 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
}
|
||||
|
||||
$this->saveFaqs((int) $block->id, $formData['faqs'] ?? []);
|
||||
$this->saveSlides((int) $block->id, $formData['slides'] ?? []);
|
||||
|
||||
return $block->id;
|
||||
}
|
||||
@@ -59,8 +67,14 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
$block->description = (string) ($formData['description'] ?? '');
|
||||
$block->extra_description = (string) ($formData['extra_description'] ?? '');
|
||||
$block->link = (string) ($formData['link'] ?? '');
|
||||
$block->link_text = (string) ($formData['link_text'] ?? '');
|
||||
$block->alert_type = (string) ($formData['alert_type'] ?? 'info');
|
||||
$block->timeout = (int) ($formData['timeout'] ?? 0);
|
||||
$block->slider_mode = (string) ($formData['slider_mode'] ?? Config::SLIDER_DEFAULT_MODE);
|
||||
$block->slides_per_view_mobile = (float) ($formData['slides_per_view_mobile'] ?? Config::SLIDER_DEFAULT_SLIDES_PER_VIEW_MOBILE);
|
||||
$block->slides_per_view_tablet = (float) ($formData['slides_per_view_tablet'] ?? Config::SLIDER_DEFAULT_SLIDES_PER_VIEW_TABLET);
|
||||
$block->slides_per_view_desktop = (float) ($formData['slides_per_view_desktop'] ?? Config::SLIDER_DEFAULT_SLIDES_PER_VIEW_DESKTOP);
|
||||
$block->autoplay_delay = (int) ($formData['autoplay_delay'] ?? 0);
|
||||
$block->image = $this->saveUploadedImage($formData['image'] ?? null, $block->image);
|
||||
$block->active = (int) $formData['active'];
|
||||
|
||||
@@ -69,6 +83,7 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
}
|
||||
|
||||
$this->saveFaqs((int) $id, $formData['faqs'] ?? []);
|
||||
$this->saveSlides((int) $id, $formData['slides'] ?? []);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -143,4 +158,37 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
$faq->add();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Slides are fully replaced on every save, like FAQ items — except a
|
||||
* slide's image can't round-trip through its file input, so the previous
|
||||
* filename is looked up by the slide's hidden id and reused whenever no
|
||||
* new file was uploaded for that row.
|
||||
*/
|
||||
private function saveSlides(int $idBlock, array $slides): void
|
||||
{
|
||||
$existingImages = [];
|
||||
|
||||
foreach (CustomHtmlSlide::getByBlockId($idBlock) as $existingSlide) {
|
||||
$existingImages[(int) $existingSlide['id_ws_customhtml_slide']] = $existingSlide['image'];
|
||||
}
|
||||
|
||||
CustomHtmlSlide::deleteByBlockId($idBlock);
|
||||
|
||||
foreach (array_values($slides) as $position => $slideData) {
|
||||
$existingId = (int) ($slideData['id'] ?? 0);
|
||||
$existingImage = $existingImages[$existingId] ?? '';
|
||||
|
||||
$slide = new CustomHtmlSlide();
|
||||
$slide->id_ws_customhtml = $idBlock;
|
||||
$slide->title = (string) ($slideData['title'] ?? '');
|
||||
$slide->description = (string) ($slideData['description'] ?? '');
|
||||
$slide->extra_description = (string) ($slideData['extra_description'] ?? '');
|
||||
$slide->link = (string) ($slideData['link'] ?? '');
|
||||
$slide->link_text = (string) ($slideData['link_text'] ?? '');
|
||||
$slide->image = $this->saveUploadedImage($slideData['image'] ?? null, $existingImage);
|
||||
$slide->ordering = $position;
|
||||
$slide->add();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user