feat(main): Groups and blocks

This commit is contained in:
2026-08-10 16:13:15 +02:00
parent 6aff506cd6
commit 635c13a0ed
39 changed files with 1188 additions and 237 deletions
@@ -0,0 +1,134 @@
<?php
namespace Websmid\CustomHtml\Controller;
use Exception;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Voter\PageVoter;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Websmid\CustomHtml\Grid\Definition\Factory\CustomHtmlBlockGridDefinitionFactory;
use Websmid\CustomHtml\Grid\Filters\CustomHtmlBlockFilters;
use Websmid\CustomHtml\Model\CustomHtmlBlock;
use Validate;
class CustomHtmlBlockController extends FrameworkBundleAdminController
{
public function indexAction(Request $request, CustomHtmlBlockFilters $filters): Response
{
if ($request->query->has('idGroup')) {
$filters->addFilter(['id_ws_customhtml_group' => (int) $request->query->get('idGroup')]);
$filters->setNeedsToBePersisted(false);
}
$gridFactory = $this->get('websmid.customhtml.grid.customhtmlblock_grid_factory');
$grid = $gridFactory->getGrid($filters);
return $this->render('@Modules/ws_customhtml/views/templates/admin/grid/customhtmlblock.html.twig', [
'customHtmlBlockGrid' => $this->presentGrid($grid),
'layoutHeaderToolbarBtn' => $this->getToolbarButtons($request->get('_legacy_controller')),
]);
}
public function searchAction(Request $request): RedirectResponse
{
return $this->get('prestashop.bundle.grid.response_builder')->buildSearchResponse(
$this->get('websmid.customhtml.grid.definition.factory.customhtmlblock_definition_factory'),
$request,
CustomHtmlBlockGridDefinitionFactory::GRID_ID,
'customhtmlblock_index'
);
}
public function createAction(Request $request): Response
{
$formBuilder = $this->get('websmid.customhtml.form.customhtmlblock_data_form_builder');
$formHandler = $this->get('websmid.customhtml.form.customhtmlblock_form_handler');
$form = $formBuilder->getForm();
if ($request->isMethod('GET') && $request->query->has('idGroup')) {
$form->get('id_ws_customhtml_group')->setData((int) $request->query->get('idGroup'));
}
$form->handleRequest($request);
try {
$handlerResult = $formHandler->handle($form);
if (null !== $handlerResult->getIdentifiableObjectId()) {
$this->addFlash('success', $this->trans('Successful creation.', 'Admin.Notifications.Success'));
return $this->redirectToRoute('customhtmlblock_index');
}
} catch (Exception $e) {
$this->addFlash('error', $this->getErrorMessageForException($e, []));
}
return $this->render('@Modules/ws_customhtml/views/templates/admin/form/customhtmlblock_create.html.twig', [
'customHtmlBlockForm' => $form->createView(),
]);
}
public function editAction(Request $request, int $idCustomHtmlBlock): Response
{
$formBuilder = $this->get('websmid.customhtml.form.customhtmlblock_data_form_builder');
$formHandler = $this->get('websmid.customhtml.form.customhtmlblock_form_handler');
$form = $formBuilder->getFormFor($idCustomHtmlBlock);
try {
$form->handleRequest($request);
$handlerResult = $formHandler->handleFor($idCustomHtmlBlock, $form);
if ($handlerResult->isSubmitted() && $handlerResult->isValid()) {
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
return $this->redirectToRoute('customhtmlblock_index');
}
} catch (Exception $e) {
$this->addFlash('error', $this->getErrorMessageForException($e, []));
}
return $this->render('@Modules/ws_customhtml/views/templates/admin/form/customhtmlblock_edit.html.twig', [
'customHtmlBlockForm' => $form->createView(),
'idCustomHtmlBlock' => $idCustomHtmlBlock,
]);
}
public function deleteAction(Request $request, int $idCustomHtmlBlock): RedirectResponse
{
$block = new CustomHtmlBlock($idCustomHtmlBlock);
if (!Validate::isLoadedObject($block)) {
$this->addFlash('error', $this->trans('Unable to find the block.', 'Modules.Wscustomhtml.Admin'));
return $this->redirectToRoute('customhtmlblock_index');
}
$block->delete();
$this->addFlash('success', $this->trans('Successful deletion.', 'Admin.Notifications.Success'));
return $this->redirectToRoute('customhtmlblock_index');
}
private function getToolbarButtons(?string $securitySubject): array
{
$toolbarButtons = [];
if (!$securitySubject || !$this->isGranted(PageVoter::CREATE, $securitySubject)) {
return $toolbarButtons;
}
$toolbarButtons['customhtmlblock_create'] = [
'href' => $this->generateUrl('customhtmlblock_create'),
'desc' => $this->trans('New block', 'Admin.Actions'),
'icon' => 'add_circle_outline',
'class' => 'btn-primary',
'floating_class' => '',
];
return $toolbarButtons;
}
}