166 lines
6.7 KiB
PHP
166 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace Websmid\CustomHtml\Controller;
|
|
|
|
use Exception;
|
|
use PrestaShop\PrestaShop\Core\Grid\Position\Exception\PositionUpdateException;
|
|
use PrestaShop\PrestaShop\Core\Grid\Position\GridPositionUpdaterInterface;
|
|
use PrestaShop\PrestaShop\Core\Grid\Position\PositionUpdateFactoryInterface;
|
|
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 Websmid\CustomHtml\Model\CustomHtmlFaq;
|
|
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, []));
|
|
}
|
|
|
|
$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,
|
|
]);
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
CustomHtmlFaq::deleteByBlockId($idCustomHtmlBlock);
|
|
$block->delete();
|
|
$this->addFlash('success', $this->trans('Successful deletion.', 'Admin.Notifications.Success'));
|
|
|
|
return $this->redirectToRoute('customhtmlblock_index');
|
|
}
|
|
|
|
public function updatePositionAction(int $idGroup, Request $request): RedirectResponse
|
|
{
|
|
$positionDefinition = $this->get('websmid.customhtml.grid.position.customhtmlblock_position_definition');
|
|
|
|
try {
|
|
$positionUpdateFactory = $this->get(PositionUpdateFactoryInterface::class);
|
|
$positionUpdate = $positionUpdateFactory->buildPositionUpdate([
|
|
'positions' => $request->request->all('positions'),
|
|
'parentId' => $idGroup,
|
|
], $positionDefinition);
|
|
|
|
$updater = $this->get(GridPositionUpdaterInterface::class);
|
|
$updater->update($positionUpdate);
|
|
|
|
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
|
|
} catch (PositionUpdateException $e) {
|
|
$this->addFlash('error', $this->getErrorMessageForException($e, []));
|
|
}
|
|
|
|
return $this->redirectToRoute('customhtmlblock_index', ['idGroup' => $idGroup]);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|