|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Shopsys\FrameworkBundle\Controller\Admin; |
| 6 | + |
| 7 | +use Shopsys\FrameworkBundle\Component\ConfirmDelete\ConfirmDeleteResponseFactory; |
| 8 | +use Shopsys\FrameworkBundle\Component\Domain\Domain; |
| 9 | +use Shopsys\FrameworkBundle\Component\Router\Security\Annotation\CsrfProtection; |
| 10 | +use Shopsys\FrameworkBundle\Form\Admin\Seo\SeoPageFormType; |
| 11 | +use Shopsys\FrameworkBundle\Model\Seo\Page\Exception\DefaultSeoPageCannotBeDeletedException; |
| 12 | +use Shopsys\FrameworkBundle\Model\Seo\Page\Exception\SeoPageNotFoundException; |
| 13 | +use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageDataFactory; |
| 14 | +use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageFacade; |
| 15 | +use Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageGridFactory; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpFoundation\Response; |
| 18 | +use Symfony\Component\Routing\Annotation\Route; |
| 19 | + |
| 20 | +class SeoPageController extends AdminBaseController |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain |
| 24 | + * @param \Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageGridFactory $seoPageGridFactory |
| 25 | + * @param \Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageDataFactory $seoPageDataFactory |
| 26 | + * @param \Shopsys\FrameworkBundle\Model\Seo\Page\SeoPageFacade $seoPageFacade |
| 27 | + * @param \Shopsys\FrameworkBundle\Component\ConfirmDelete\ConfirmDeleteResponseFactory $confirmDeleteResponseFactory |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + protected readonly Domain $domain, |
| 31 | + protected readonly SeoPageGridFactory $seoPageGridFactory, |
| 32 | + protected readonly SeoPageDataFactory $seoPageDataFactory, |
| 33 | + protected readonly SeoPageFacade $seoPageFacade, |
| 34 | + protected readonly ConfirmDeleteResponseFactory $confirmDeleteResponseFactory, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @Route("/seo/page/list") |
| 40 | + * @return \Symfony\Component\HttpFoundation\Response |
| 41 | + */ |
| 42 | + public function listAction(): Response |
| 43 | + { |
| 44 | + $grid = $this->seoPageGridFactory->create($this->domain->getId()); |
| 45 | + |
| 46 | + return $this->render('@ShopsysFramework/Admin/Content/Seo/Page/list.html.twig', [ |
| 47 | + 'gridView' => $grid->createView(), |
| 48 | + ]); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @Route("/seo/page/new") |
| 53 | + * @param \Symfony\Component\HttpFoundation\Request $request |
| 54 | + * @return \Symfony\Component\HttpFoundation\Response |
| 55 | + */ |
| 56 | + public function newAction(Request $request): Response |
| 57 | + { |
| 58 | + $seoPageData = $this->seoPageDataFactory->create(); |
| 59 | + |
| 60 | + $form = $this->createForm(SeoPageFormType::class, $seoPageData, [ |
| 61 | + 'seoPage' => null, |
| 62 | + ]); |
| 63 | + $form->handleRequest($request); |
| 64 | + |
| 65 | + if ($form->isSubmitted() && $form->isValid()) { |
| 66 | + $seoPage = $this->seoPageFacade->create($seoPageData); |
| 67 | + |
| 68 | + $this |
| 69 | + ->addSuccessFlashTwig( |
| 70 | + t('SEO Page <strong><a href="{{ url }}">{{ name }}</a></strong> created'), |
| 71 | + [ |
| 72 | + 'name' => $seoPage->getPageName(), |
| 73 | + 'url' => $this->generateUrl('admin_seopage_edit', ['id' => $seoPage->getId()]), |
| 74 | + ], |
| 75 | + ); |
| 76 | + |
| 77 | + return $this->redirectToRoute('admin_seopage_list'); |
| 78 | + } |
| 79 | + |
| 80 | + if ($form->isSubmitted() && !$form->isValid()) { |
| 81 | + $this->addErrorFlashTwig(t('Please check the correctness of all data filled.')); |
| 82 | + } |
| 83 | + |
| 84 | + return $this->render('@ShopsysFramework/Admin/Content/Seo/Page/new.html.twig', [ |
| 85 | + 'form' => $form->createView(), |
| 86 | + ]); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @Route("/seo/page/edit/{id}", requirements={"id" = "\d+"}) |
| 91 | + * @param int $id |
| 92 | + * @param \Symfony\Component\HttpFoundation\Request $request |
| 93 | + * @return \Symfony\Component\HttpFoundation\Response |
| 94 | + */ |
| 95 | + public function editAction(int $id, Request $request): Response |
| 96 | + { |
| 97 | + $seoPage = $this->seoPageFacade->getById($id); |
| 98 | + $seoPageData = $this->seoPageDataFactory->createFromSeoPage($seoPage); |
| 99 | + |
| 100 | + $form = $this->createForm(SeoPageFormType::class, $seoPageData, [ |
| 101 | + 'seoPage' => $seoPage, |
| 102 | + ]); |
| 103 | + $form->handleRequest($request); |
| 104 | + |
| 105 | + if ($form->isSubmitted() && $form->isValid()) { |
| 106 | + $this->seoPageFacade->edit($id, $seoPageData); |
| 107 | + |
| 108 | + $this |
| 109 | + ->addSuccessFlashTwig( |
| 110 | + t('SEO Page <strong><a href="{{ url }}">{{ name }}</a></strong> modified'), |
| 111 | + [ |
| 112 | + 'name' => $seoPage->getPageName(), |
| 113 | + 'url' => $this->generateUrl('admin_seopage_edit', ['id' => $seoPage->getId()]), |
| 114 | + ], |
| 115 | + ); |
| 116 | + |
| 117 | + return $this->redirectToRoute('admin_seopage_list'); |
| 118 | + } |
| 119 | + |
| 120 | + if ($form->isSubmitted() && !$form->isValid()) { |
| 121 | + $this->addErrorFlashTwig(t('Please check the correctness of all data filled.')); |
| 122 | + } |
| 123 | + |
| 124 | + return $this->render('@ShopsysFramework/Admin/Content/Seo/Page/edit.html.twig', [ |
| 125 | + 'form' => $form->createView(), |
| 126 | + 'seoPage' => $seoPage, |
| 127 | + ]); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @Route("/seo/page/delete/{id}", requirements={"id" = "\d+"}) |
| 132 | + * @CsrfProtection |
| 133 | + * @param int $id |
| 134 | + * @return \Symfony\Component\HttpFoundation\Response |
| 135 | + */ |
| 136 | + public function deleteAction(int $id): Response |
| 137 | + { |
| 138 | + try { |
| 139 | + $seoPage = $this->seoPageFacade->getById($id); |
| 140 | + $this->seoPageFacade->delete($id); |
| 141 | + |
| 142 | + $this->addSuccessFlashTwig( |
| 143 | + t('SEO Page <strong>{{ name }}</strong> removed'), |
| 144 | + [ |
| 145 | + 'name' => $seoPage->getPageName(), |
| 146 | + ], |
| 147 | + ); |
| 148 | + } catch (SeoPageNotFoundException) { |
| 149 | + $this->addErrorFlash(t('Selected SEO page does not exist')); |
| 150 | + } catch (DefaultSeoPageCannotBeDeletedException) { |
| 151 | + $this->addErrorFlash(t('Selected SEO page cannot be deleted')); |
| 152 | + } |
| 153 | + |
| 154 | + return $this->redirectToRoute('admin_seopage_list'); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @Route("/seo/page/delete-confirm/{id}", requirements={"id" = "\d+"}) |
| 159 | + * @param int $id |
| 160 | + * @return \Symfony\Component\HttpFoundation\Response |
| 161 | + */ |
| 162 | + public function deleteConfirmAction(int $id): Response |
| 163 | + { |
| 164 | + $message = t('Do you really want to remove this SEO page?'); |
| 165 | + |
| 166 | + return $this->confirmDeleteResponseFactory->createDeleteResponse($message, 'admin_seopage_delete', $id); |
| 167 | + } |
| 168 | +} |
0 commit comments