<?php
namespace App\Controller;
use App\Entity\CBRCArticle;
use App\Entity\CBRCArticleCategory;
use App\Entity\CBRCUser;
use App\Form\Type\CBRCArticleCategoryType;
use App\Form\Type\CBRCArticleType;
use App\Form\Type\CBRCUserDescriptionType;
use App\Form\Type\CBRCUserType;
use App\Repository\CBRCArticleRepository;
use App\Utilities\StatusUtilities;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Request;
class CategoryController extends AbstractController
{
/**
* @Route("/admin/categories", name="admin_category")
*/
public function ShowCreateCategory(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$categories = $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->findAll();
$category = new CBRCArticleCategory();
$formcategory = $this->createForm(CBRCArticleCategoryType::class, $category);
$formcategory->handleRequest($request);
if ($formcategory->isSubmitted() && $formcategory->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->flush();
$this->addFlash("success", "Nouvelle catégorie créée");
return $this->redirect($request->getUri());
}
if ($formcategory->isSubmitted() && !$formcategory->isValid()) {
foreach ($formcategory->getErrors(true) as $error)
$this->addFlash('danger', "(" . $error->getOrigin()->getName() . ") " . $error->getMessage());
$formcategory->clearErrors(true);
}
return $this->render('category/admincategories.html.twig', [
'categories' => $categories,
'formcategory' => $formcategory->createView(),
'idactive' => 5
]);
}
/**
* @Route("/categorie/{categoryid}/edit", name="edit_category")
*/
public function ShowEditCategory(int $categoryid, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$category = $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->find($categoryid);
$formcategory = $this->createForm(CBRCArticleCategoryType::class, $category, [
'submit_label' => "Modifier",
]);
$formcategory->handleRequest($request);
if ($formcategory->isSubmitted() && $formcategory->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->flush();
$this->addFlash("success", "Catégorie modifiée");
return $this->redirectToRoute('admin_category');
}
if ($formcategory->isSubmitted() && !$formcategory->isValid()) {
foreach ($formcategory->getErrors(true) as $error)
$this->addFlash('danger', "(" . $error->getOrigin()->getName() . ") " . $error->getMessage());
$formcategory->clearErrors(true);
}
return $this->render('category/editcategory.html.twig', [
'formcategory' => $formcategory->createView(),
'idactive' => 6
]);
}
/**
* @Route("/categorie/{categoryid}/delete", name="delete_category")
*/
public function DeleteCategory(int $categoryid, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$category = $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->find($categoryid);
if (!$category)
throw new NotFoundHttpException();
$em = $this->getDoctrine()->getManager();
$em->remove($category);
$em->flush();
return $this->redirectToRoute('admin_category');
}
public function ShowCategoryList(int $activecategoryid=-1)
{
$categories = $this->getDoctrine()->getRepository(CBRCArticleCategory::class)->findAll();
$publishedcategories=array();
foreach ($categories as $category)
{
if($category->getCountpublishedarticles()>0)
array_push($publishedcategories, $category);
}
//none category
$articles =$this->getDoctrine()->getRepository(CBRCArticle::class)->findHomeNoneCategory();
$hasnone = count($articles) > 0;
return $this->render('category/categorylist.html.twig', [
'categories' => $publishedcategories,
'activecategoryid' => $activecategoryid,
'hasnone' => $hasnone,
'countnone' => count($articles),
]);
}
}
?>