src/Controller/CategoryController.php line 119

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CBRCArticle;
  4. use App\Entity\CBRCArticleCategory;
  5. use App\Entity\CBRCUser;
  6. use App\Form\Type\CBRCArticleCategoryType;
  7. use App\Form\Type\CBRCArticleType;
  8. use App\Form\Type\CBRCUserDescriptionType;
  9. use App\Form\Type\CBRCUserType;
  10. use App\Repository\CBRCArticleRepository;
  11. use App\Utilities\StatusUtilities;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  18. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  19. use Symfony\Component\HttpFoundation\Request;
  20. class CategoryController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/admin/categories", name="admin_category")
  24.      */
  25.     public function ShowCreateCategory(Request $request)
  26.     {
  27.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  28.         $categories $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->findAll();
  29.         $category = new CBRCArticleCategory();
  30.         $formcategory $this->createForm(CBRCArticleCategoryType::class, $category);
  31.         $formcategory->handleRequest($request);
  32.         if ($formcategory->isSubmitted() && $formcategory->isValid()) {
  33.             $em $this->getDoctrine()->getManager();
  34.             $em->persist($category);
  35.             $em->flush();
  36.             $this->addFlash("success""Nouvelle catégorie créée");
  37.             return $this->redirect($request->getUri());
  38.         }
  39.         if ($formcategory->isSubmitted() && !$formcategory->isValid()) {
  40.             foreach ($formcategory->getErrors(true) as $error)
  41.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  42.             $formcategory->clearErrors(true);
  43.         }
  44.         return $this->render('category/admincategories.html.twig', [
  45.             'categories' => $categories,
  46.             'formcategory' => $formcategory->createView(),
  47.             'idactive' => 5
  48.         ]);
  49.     }
  50.     /**
  51.      * @Route("/categorie/{categoryid}/edit", name="edit_category")
  52.      */
  53.     public function ShowEditCategory(int $categoryidRequest $request)
  54.     {
  55.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  56.         $category $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->find($categoryid);
  57.         $formcategory $this->createForm(CBRCArticleCategoryType::class, $category, [
  58.             'submit_label' => "Modifier",
  59.         ]);
  60.         $formcategory->handleRequest($request);
  61.         if ($formcategory->isSubmitted() && $formcategory->isValid()) {
  62.             $em $this->getDoctrine()->getManager();
  63.             $em->persist($category);
  64.             $em->flush();
  65.             $this->addFlash("success""Catégorie modifiée");
  66.             return $this->redirectToRoute('admin_category');
  67.         }
  68.         if ($formcategory->isSubmitted() && !$formcategory->isValid()) {
  69.             foreach ($formcategory->getErrors(true) as $error)
  70.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  71.             $formcategory->clearErrors(true);
  72.         }
  73.         return $this->render('category/editcategory.html.twig', [
  74.             'formcategory' => $formcategory->createView(),
  75.             'idactive' => 6
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/categorie/{categoryid}/delete", name="delete_category")
  80.      */
  81.     public function DeleteCategory(int $categoryidRequest $request)
  82.     {
  83.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  84.         $category $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->find($categoryid);
  85.         if (!$category)
  86.             throw new NotFoundHttpException();
  87.         $em $this->getDoctrine()->getManager();
  88.         $em->remove($category);
  89.         $em->flush();
  90.         return $this->redirectToRoute('admin_category');
  91.     }
  92.     public function ShowCategoryList(int $activecategoryid=-1)
  93.     {
  94.         $categories $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->findAll();
  95.         $publishedcategories=array();
  96.         foreach ($categories as $category)
  97.         {
  98.             if($category->getCountpublishedarticles()>0)
  99.                 array_push($publishedcategories$category);
  100.         }
  101.         //none category
  102.         $articles =$this->getDoctrine()->getRepository(CBRCArticle::class)->findHomeNoneCategory();
  103.         $hasnone count($articles) > 0;
  104.         return $this->render('category/categorylist.html.twig', [
  105.             'categories' => $publishedcategories,
  106.             'activecategoryid' => $activecategoryid,
  107.             'hasnone' => $hasnone,
  108.             'countnone' => count($articles),
  109.         ]);
  110.     }
  111. }
  112. ?>