src/Controller/NewsCategoryController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\NewsCategory;
  4. use App\Repository\NewsCategoryRepository;
  5. use App\Service\Helper\HtmlTreeGenerator;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Oz\Toolboxe\Model\AjaxNotification;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/news/category")
  15.  */
  16. class NewsCategoryController extends AbstractController
  17. {
  18.     /**
  19.      * @var NewsCategoryRepository
  20.      */
  21.     private $newsCategoryRepository;
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     private $em;
  26.     public function __construct(NewsCategoryRepository $newsCategoryRepositoryEntityManagerInterface $em)
  27.     {
  28.         $this->newsCategoryRepository $newsCategoryRepository;
  29.         $this->em                     $em;
  30.     }
  31.     /**
  32.      * @Route("/", name="NEWS_CATEGORY_INDEX")
  33.      */
  34.     public function index(): Response
  35.     {
  36.         $newsCategoryRootNode $this->newsCategoryRepository->getNewsCategoryRootNodes();
  37.         if (!$newsCategoryRootNode) {
  38.             $newsCategoryRootNode = (new NewsCategory())
  39.                 ->setTitle(NewsCategory::ROOT_NODE_NAME);
  40.             $this->em->persist($newsCategoryRootNode);
  41.             $this->em->flush();
  42.         }
  43.         $htmlTree HtmlTreeGenerator::generate($newsCategoryRootNode);
  44.         //dd($htmlTree);
  45. //        $nodes = $this->newsCategoryRepository->findAll();
  46. //        $toto  = HtmlTreeGenerator::generateChoices($nodes, $this->newsCategoryRepository);
  47. //        dump($toto);
  48.         return $this->render('news_category/index.html.twig', [
  49.             'htmlTree' => $htmlTree,
  50.         ]);
  51.     }
  52.     /**
  53.      * @Route("/change-category", name="AJAX_CHANGE_CATEGORY", methods={"POST"}, options={"expose"=true})
  54.      *
  55.      * @param Request $request
  56.      *
  57.      * @return JsonResponse
  58.      */
  59.     public function moveCategory(Request $request): JsonResponse
  60.     {
  61.         $data $request->request->all();
  62.         $parentNodeId $data['parentNodeId'];
  63.         $parentNode   null;
  64.         if ($parentNodeId !== '#') {
  65.             $parentNode $this->newsCategoryRepository->find($parentNodeId);
  66.         }
  67.         $nodeId    $data['node']['id'];
  68.         $movedNode $this->newsCategoryRepository->find($nodeId);
  69.         if (!$movedNode) {
  70.             return $this->json([
  71.                 'status' => false,
  72.                 'data'   => $data,
  73.             ]);
  74.         }
  75.         $movedNode->setParent($parentNode);
  76.         $this->newsCategoryRepository->recover();
  77.         $this->em->flush();
  78.         return $this->json([
  79.             'status' => true,
  80.         ]);
  81.     }
  82.     /**
  83.      * @Route("/ajax-create-node", name="AJAX_CREATE_NODE", methods={"POST"}, options={"expose"=true})
  84.      *
  85.      * @param Request $request
  86.      *
  87.      * @return JsonResponse
  88.      */
  89.     public function ajaxCreateNode(Request $request): JsonResponse
  90.     {
  91.         $nodeData $request->request->get('node');
  92. //        dump($nodeData);
  93.         $parentNode $this->newsCategoryRepository->find($nodeData['parent']);
  94.         $text       ucfirst(trim($nodeData['text']));
  95.         if (!strlen($text)) {
  96.             return $this->json([
  97.                 'status'                     => false,
  98.                 AjaxNotification::AJAX_INDEX => new AjaxNotification('error''Vous devez ajouter un nom de dossier valide.'),
  99.             ]);
  100.         }
  101.         if ($nodeExist $this->newsCategoryRepository->findOneBy([
  102.             'parent' => $parentNode,
  103.             'title'  => $text,
  104.         ])) {
  105.             return $this->json([
  106.                 'status'                     => false,
  107.                 'exist'                      => true,
  108.                 'node'                       => [
  109.                     'title' => $nodeExist->getTitle(),
  110.                     'id'    => $nodeExist->getId(),
  111.                 ],
  112.                 AjaxNotification::AJAX_INDEX => new AjaxNotification('error'sprintf('Le dossier <strong>%s</strong> existe déja.'$nodeExist->getTitle())),
  113.             ]);
  114.         }
  115.         dump($parentNode);
  116.         $node = (new NewsCategory())
  117.             ->setTitle($text)
  118.             ->setParent($parentNode);
  119.         if (!$parentNode) {
  120.             $node->setRoot(null);
  121.         }
  122.         $this->em->persist($node);
  123.         $this->em->flush();
  124.         return $this->json([
  125.             'status' => true,
  126.             'node'   => [
  127.                 'title' => $node->getTitle(),
  128.                 'id'    => $node->getId(),
  129.             ],
  130.         ]);
  131.     }
  132.     /**
  133.      * @Route("/ajax-remove-node", name="AJAX_REMOVE_NODE", methods={"POST"}, options={"expose"=true})
  134.      *
  135.      * @param Request $request
  136.      *
  137.      * @return Response
  138.      */
  139.     public function ajaxRemoveNode(Request $request): JsonResponse
  140.     {
  141.         $nodeId $request->request->get('nodeId');
  142.         $node $this->newsCategoryRepository->getCategoryAndNews($nodeId);
  143.         if (!$node) {
  144.             return $this->json([
  145.                 'status'                     => false,
  146.                 AjaxNotification::AJAX_INDEX => new AjaxNotification('error'sprintf('Impossible de trouver le dossier')),
  147.             ]);
  148.         }
  149.         $title $node->getTitle();
  150.         if($node->getLvl() === 0){
  151.             return $this->json([
  152.                 'status'                     => false,
  153.                 AjaxNotification::AJAX_INDEX => new AjaxNotification('error'sprintf('Impossible de supprimer le dossier racine.')),
  154.             ]);
  155.         }
  156.         $this->em->remove($node);
  157.         $this->em->flush();
  158.         return $this->json([
  159.             'status'                     => true,
  160.             AjaxNotification::AJAX_INDEX => new AjaxNotification('success'sprintf('Dossier %s supprimé'$title)),
  161.         ]);
  162.     }
  163.     /**
  164.      * @Route("/ajax-rename-node", name="AJAX_RENAME_NODE", methods={"POST"}, options={"expose"=true})
  165.      *
  166.      * @param Request $request
  167.      *
  168.      * @return JsonResponse
  169.      */
  170.     public function ajaxRenameNode(Request $request): JsonResponse
  171.     {
  172.         $nodeData $request->request->get('node');
  173.         $node     $this->newsCategoryRepository->find($nodeData['id']);
  174.         if (!$node) {
  175.             return $this->json([
  176.                 'status'                     => false,
  177.                 AjaxNotification::AJAX_INDEX => new AjaxNotification('error'sprintf('Impossible de trouver le dossier.')),
  178.             ]);
  179.         }
  180.         $text ucfirst(trim($nodeData['text']));
  181.         $node->setTitle($text);
  182.         $this->em->flush();
  183.         return $this->json([
  184.             'status'                     => true,
  185.             AjaxNotification::AJAX_INDEX => new AjaxNotification('success'sprintf('Traitement du dossier "%s" OK !'$node->getTitle())),
  186.             'node'                       => [
  187.                 'title' => $node->getTitle(),
  188.                 'id'    => $node->getId(),
  189.             ],
  190.         ]);
  191.     }
  192.     /**
  193.      * @Route("/{id}/get-news", name="GET_NEWS_BY_CATEGORY", methods={"POST"}, options={"expose"=true})
  194.      */
  195.     public function getNewsByCategory(string $id): JsonResponse
  196.     {
  197.         $category $this->newsCategoryRepository->getCategoryAndNews($id);
  198.         return $this->json([
  199.             'status' => true,
  200.             'html'   => $this->renderView('news/windows-news.html.twig', [
  201.                 'category' => $category,
  202.             ]),
  203.         ]);
  204.     }
  205. }