vendor/stimactiv/cms-bundle/src/Controller/PageController.php line 76

Open in your IDE?
  1. <?php
  2. namespace Stimactiv\CmsBundle\Controller;
  3. use Doctrine\ORM\EntityNotFoundException;
  4. use Stimactiv\CmsBundle\Entity\Page;
  5. use Stimactiv\CmsBundle\Entity\PageI18n;
  6. use Stimactiv\CmsBundle\Form\Handler\PageHandler;
  7. use Stimactiv\CmsBundle\Form\Type\Page\CreatePageType;
  8. use Stimactiv\CmsBundle\Form\Type\Page\EditPageType;
  9. use Stimactiv\CmsBundle\Model\PageManager;
  10. use Stimactiv\CmsBundle\Repository\Core\LangRepository;
  11. use Stimactiv\CmsBundle\Repository\PageI18nRepository;
  12. use Stimactiv\CmsBundle\Repository\PageRepository;
  13. use Stimactiv\CoreBundle\Model\UtilsManager;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route("/tableaudebord/cms/pages", name="cms_page_")
  21.  */
  22. class PageController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("/", name="index")
  26.      */
  27.     public function index(PageHandler $formHandlerLangRepository $langRepositoryPageRepository $pageRepository): Response
  28.     {
  29.         $form $this->createForm(CreatePageType::class);
  30.         $pageI18n $formHandler->create($form);
  31.         if ($pageI18n instanceof PageI18n) {
  32.             $this->addFlash('success'"La page vient d'être créée avec succès.");
  33.             return $this->redirectToRoute('cms_page_edit', ['page_id' => $pageI18n->getPage()->getId(), 'lang_id' => $pageI18n->getLang()->getId()]);
  34.         }
  35.         $lang $langRepository->findOneBy(['byDefault' => true]);
  36.         $pages $pageRepository->table($lang);
  37.         return $this->render('@StimactivCms/page/index.html.twig', [
  38.             'pages' => json_encode($pages),
  39.             'statusLabels' => Page::$_statusLabels,
  40.             'visibilityLabels' => Page::$_visibilityLabels,
  41.             'form' => $form->createView(),
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/{id}/show", name="show")
  46.      */
  47.     public function show(Page $pagePageHandler $formHandlerLangRepository $langRepositoryPageRepository $pageRepository)
  48.     {
  49.         $form $this->createForm(CreatePageType::class, null, ['parent' => $page]);
  50.         $pageI18n $formHandler->create($form$page);
  51.         if ($pageI18n instanceof PageI18n) {
  52.             return $this->redirectToRoute('cms_page_edit', ['page_id' => $pageI18n->getPage()->getId(), 'lang_id' => $pageI18n->getLang()->getId()]);
  53.         }
  54.         $lang $langRepository->findOneBy(['byDefault' => true]);
  55.         $pages $pageRepository->table($lang$page);
  56.         return $this->render('@StimactivCms/page/show.html.twig', [
  57.             'page' => $page,
  58.             'pages' => json_encode($pages),
  59.             'statusLabels' => Page::$_statusLabels,
  60.             'visibilityLabels' => Page::$_visibilityLabels,
  61.             'form' => $form->createView(),
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/{page_id}/edit/{lang_id?}", name="edit")
  66.      */
  67.     public function edit(int $page_id, ?int $lang_idLangRepository $langRepositoryPageRepository $pageRepositoryPageI18nRepository $pageI18nRepositoryPageHandler $formHandler): Response
  68.     {
  69.         $page $pageRepository->find($page_id);
  70.         if ($page === null) {
  71.             throw new EntityNotFoundException();
  72.         }
  73.         if ($lang_id) {
  74.             $lang $langRepository->find($lang_id);
  75.         } else {
  76.             $usedLanguages $langRepository->findUsedByPage($page);
  77.             $lang array_shift($usedLanguages);
  78.         }
  79.         if ($lang === null) {
  80.             throw new EntityNotFoundException();
  81.         }
  82.         if ($page->isEditable()) {
  83.             $parent $page->getParent();
  84.             if ($parent === null) {
  85.                 $languages $langRepository->findBy(['active' => true]);
  86.             } else {
  87.                 $languages $langRepository->findUsedByPage($parent);
  88.             }
  89.             $pageI18n $pageI18nRepository->findOneBy(['page' => $page'lang' => $lang]);
  90.             if ($pageI18n === null) {
  91.                 $pageI18n = new PageI18n($page$lang);
  92.             }
  93.             $form $this->createForm(EditPageType::class, null, ['page_i18n' => $pageI18n]);
  94.             if ($formHandler->update($form)) {
  95.                 $this->addFlash('success'"La page vient d'être créée avec succès.");
  96. //                if ($parent) {
  97. //                    return $this->redirectToRoute('cms_page_show', ['id' => $parent->getId()]);
  98. //                }
  99. //                return $this->redirectToRoute('cms_page_index');
  100.                 return $this->redirectToRoute('cms_page_edit', ['page_id' => $page_id'lang_id' => $lang_id]);
  101.             }
  102.             return $this->render('@StimactivCms/page/edit.html.twig', [
  103.                 'languages' => $languages,
  104.                 'current_lang' => $lang,
  105.                 'page_i18n' => $pageI18n,
  106.                 'form' => $form->createView(),
  107.             ]);
  108.         }
  109.         return $this->redirectToRoute('cms_page_index');
  110.     }
  111.     /**
  112.      * @Route("/preview", name="preview", methods={"POST"})
  113.      */
  114.     public function preview(Request $request)
  115.     {
  116.         $content json_decode($request->getContent(), true);
  117.         if (array_is_list($content)) {
  118.             return $this->render('@StimactivCms/page/preview.html.twig', [
  119.                 'blocs' => $content,
  120.                 'preview' => true,
  121.             ]);
  122.         }
  123.         return $this->render('app/frontend/blocs/_' $content['_name'] . '.html.twig', [
  124.             'bloc' => $content,
  125.             'preview' => true,
  126.         ]);
  127.     }
  128.     /**
  129.      * @Route("/{id}/update-position", name="update_position", methods={"POST"})
  130.      */
  131.     public function updatePosition(Request $requestUtilsManager $utilsManagerPage $pagePageManager $pageManager)
  132.     {
  133.         $ajax $utilsManager->cleanBool($request->request->get('ajax'));
  134.         if ($ajax || $request->isXmlHttpRequest()) {
  135.             $newPosition $request->request->get('position');
  136.             if ($pageManager->updatePosition($page$newPosition)) {
  137.                 return new JsonResponse();
  138.             }
  139.             return new JsonResponse(null500);
  140.         }
  141.         return $this->redirectToRoute('cms_page_index');
  142.     }
  143. //    /**
  144. //     * @Route("/{id}/toggle-visibility", name="toggle_visibility", methods={"POST"})
  145. //     */
  146. //    public function toggleVisibility(Request $request, UtilsManager $utilsManager, Page $page, PageManager $pageManager)
  147. //    {
  148. //        $ajax = $utilsManager->cleanBool($request->request->get('ajax'));
  149. //        if ($ajax || $request->isXmlHttpRequest()) {
  150. //            if ($pageManager->toggleVisibility($page)) {
  151. //                return new JsonResponse();
  152. //            }
  153. //            return new JsonResponse(null, 500);
  154. //        }
  155. //        return $this->redirectToRoute('cms_page_index');
  156. //    }
  157.     /**
  158.      * @Route("/{id}/delete", name="delete")
  159.      */
  160.     public function delete(Page $page)
  161.     {
  162. //        $parent = $page->getParent();
  163. //        $parentId = ($parent !== null) ? $parent->getId() : null;
  164. //
  165. //        $em = $this->getDoctrine()->getManager();
  166. //        $em->remove($page);
  167. //        $em->flush();
  168. //
  169. //        // Re-define position for others
  170. //        $pages = $em->getRepository('ACCoreBundle:Page')->findByParent($parentId, array('position' => 'ASC'));
  171. //        foreach ($pages as $key => $pageToUpdate) {
  172. //            $position = $key + 1; // start to 1
  173. //            $pageToUpdate->setPosition($position);
  174. //        }
  175. //        $em->flush();
  176.         return $this->redirectToRoute('cms_page_index');
  177.     }
  178. }