<?php
namespace Stimactiv\CmsBundle\Controller;
use Doctrine\ORM\EntityNotFoundException;
use Stimactiv\CmsBundle\Entity\Page;
use Stimactiv\CmsBundle\Entity\PageI18n;
use Stimactiv\CmsBundle\Form\Handler\PageHandler;
use Stimactiv\CmsBundle\Form\Type\Page\CreatePageType;
use Stimactiv\CmsBundle\Form\Type\Page\EditPageType;
use Stimactiv\CmsBundle\Model\PageManager;
use Stimactiv\CmsBundle\Repository\Core\LangRepository;
use Stimactiv\CmsBundle\Repository\PageI18nRepository;
use Stimactiv\CmsBundle\Repository\PageRepository;
use Stimactiv\CoreBundle\Model\UtilsManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/tableaudebord/cms/pages", name="cms_page_")
*/
class PageController extends AbstractController
{
/**
* @Route("/", name="index")
*/
public function index(PageHandler $formHandler, LangRepository $langRepository, PageRepository $pageRepository): Response
{
$form = $this->createForm(CreatePageType::class);
$pageI18n = $formHandler->create($form);
if ($pageI18n instanceof PageI18n) {
$this->addFlash('success', "La page vient d'être créée avec succès.");
return $this->redirectToRoute('cms_page_edit', ['page_id' => $pageI18n->getPage()->getId(), 'lang_id' => $pageI18n->getLang()->getId()]);
}
$lang = $langRepository->findOneBy(['byDefault' => true]);
$pages = $pageRepository->table($lang);
return $this->render('@StimactivCms/page/index.html.twig', [
'pages' => json_encode($pages),
'statusLabels' => Page::$_statusLabels,
'visibilityLabels' => Page::$_visibilityLabels,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}/show", name="show")
*/
public function show(Page $page, PageHandler $formHandler, LangRepository $langRepository, PageRepository $pageRepository)
{
$form = $this->createForm(CreatePageType::class, null, ['parent' => $page]);
$pageI18n = $formHandler->create($form, $page);
if ($pageI18n instanceof PageI18n) {
return $this->redirectToRoute('cms_page_edit', ['page_id' => $pageI18n->getPage()->getId(), 'lang_id' => $pageI18n->getLang()->getId()]);
}
$lang = $langRepository->findOneBy(['byDefault' => true]);
$pages = $pageRepository->table($lang, $page);
return $this->render('@StimactivCms/page/show.html.twig', [
'page' => $page,
'pages' => json_encode($pages),
'statusLabels' => Page::$_statusLabels,
'visibilityLabels' => Page::$_visibilityLabels,
'form' => $form->createView(),
]);
}
/**
* @Route("/{page_id}/edit/{lang_id?}", name="edit")
*/
public function edit(int $page_id, ?int $lang_id, LangRepository $langRepository, PageRepository $pageRepository, PageI18nRepository $pageI18nRepository, PageHandler $formHandler): Response
{
$page = $pageRepository->find($page_id);
if ($page === null) {
throw new EntityNotFoundException();
}
if ($lang_id) {
$lang = $langRepository->find($lang_id);
} else {
$usedLanguages = $langRepository->findUsedByPage($page);
$lang = array_shift($usedLanguages);
}
if ($lang === null) {
throw new EntityNotFoundException();
}
if ($page->isEditable()) {
$parent = $page->getParent();
if ($parent === null) {
$languages = $langRepository->findBy(['active' => true]);
} else {
$languages = $langRepository->findUsedByPage($parent);
}
$pageI18n = $pageI18nRepository->findOneBy(['page' => $page, 'lang' => $lang]);
if ($pageI18n === null) {
$pageI18n = new PageI18n($page, $lang);
}
$form = $this->createForm(EditPageType::class, null, ['page_i18n' => $pageI18n]);
if ($formHandler->update($form)) {
$this->addFlash('success', "La page vient d'être créée avec succès.");
// if ($parent) {
// return $this->redirectToRoute('cms_page_show', ['id' => $parent->getId()]);
// }
// return $this->redirectToRoute('cms_page_index');
return $this->redirectToRoute('cms_page_edit', ['page_id' => $page_id, 'lang_id' => $lang_id]);
}
return $this->render('@StimactivCms/page/edit.html.twig', [
'languages' => $languages,
'current_lang' => $lang,
'page_i18n' => $pageI18n,
'form' => $form->createView(),
]);
}
return $this->redirectToRoute('cms_page_index');
}
/**
* @Route("/preview", name="preview", methods={"POST"})
*/
public function preview(Request $request)
{
$content = json_decode($request->getContent(), true);
if (array_is_list($content)) {
return $this->render('@StimactivCms/page/preview.html.twig', [
'blocs' => $content,
'preview' => true,
]);
}
return $this->render('app/frontend/blocs/_' . $content['_name'] . '.html.twig', [
'bloc' => $content,
'preview' => true,
]);
}
/**
* @Route("/{id}/update-position", name="update_position", methods={"POST"})
*/
public function updatePosition(Request $request, UtilsManager $utilsManager, Page $page, PageManager $pageManager)
{
$ajax = $utilsManager->cleanBool($request->request->get('ajax'));
if ($ajax || $request->isXmlHttpRequest()) {
$newPosition = $request->request->get('position');
if ($pageManager->updatePosition($page, $newPosition)) {
return new JsonResponse();
}
return new JsonResponse(null, 500);
}
return $this->redirectToRoute('cms_page_index');
}
// /**
// * @Route("/{id}/toggle-visibility", name="toggle_visibility", methods={"POST"})
// */
// public function toggleVisibility(Request $request, UtilsManager $utilsManager, Page $page, PageManager $pageManager)
// {
// $ajax = $utilsManager->cleanBool($request->request->get('ajax'));
// if ($ajax || $request->isXmlHttpRequest()) {
// if ($pageManager->toggleVisibility($page)) {
// return new JsonResponse();
// }
// return new JsonResponse(null, 500);
// }
// return $this->redirectToRoute('cms_page_index');
// }
/**
* @Route("/{id}/delete", name="delete")
*/
public function delete(Page $page)
{
// $parent = $page->getParent();
// $parentId = ($parent !== null) ? $parent->getId() : null;
//
// $em = $this->getDoctrine()->getManager();
// $em->remove($page);
// $em->flush();
//
// // Re-define position for others
// $pages = $em->getRepository('ACCoreBundle:Page')->findByParent($parentId, array('position' => 'ASC'));
// foreach ($pages as $key => $pageToUpdate) {
// $position = $key + 1; // start to 1
// $pageToUpdate->setPosition($position);
// }
// $em->flush();
return $this->redirectToRoute('cms_page_index');
}
}