<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Entity\Status;
use App\Form\StatusType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class ConfigController extends AbstractController
{
/**
* @Route("/config", name="config")
*/
public function index(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
if(!$this->getUser()):
return $this->redirect('/login');
else:
if(!in_array('ROLE_ADMIN', $this->getUser()->getRoles())):
return $this->redirectToRoute('home');
endif;
endif;
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository(User::class)->findBy([], ['id' => 'ASC']);
$Users = new User();
$formusers = $this->createForm(RegistrationFormType::class, $Users);
$formusers->handleRequest($request);
if ($formusers->isSubmitted() && $formusers->isValid()) {
$Users->setRoles(['ROLE_USER']);
// encode the plain password
$Users->setPassword(
$userPasswordHasher->hashPassword(
$Users,
$formusers->get('plainPassword')->getData()
)
);
$entityManager->persist($Users);
$entityManager->flush();
return $this->redirectToRoute('config');
}
/****/
$em = $this->getDoctrine()->getManager();
$status = $em->getRepository(Status::class)->findBy([], ['title' => 'ASC']);
$Status = new Status();
$formstatus = $this->createForm(StatusType::class, $Status);
$formstatus->handleRequest($request);
if ($formstatus->isSubmitted() && $formstatus->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Status);
$entityManager->flush();
return $this->redirectToRoute('config');
}
/***/
return $this->render('config/index.html.twig', [
'controller_name' => 'ConfigController',
'users' => $users,
'formusers' => $formusers->createView(),
'status' => $status,
'formstatus' => $formstatus->createView()
]);
}
/**
* @Route("/config/user/{id}", name="deleteuser")
*/
function DeleteUser(Request $request, User $user){
$em = $this->getDoctrine()->getManager();
$em->remove($user);
$em->flush();
return $this->redirectToRoute('config', ['success' => 'ok']);
}
/**
* @Route("/config/status/{id}", name="deletestatus")
*/
function DeleteStatus(Request $request, Status $status){
$em = $this->getDoctrine()->getManager();
$em->remove($status);
$em->flush();
return $this->redirectToRoute('config', ['success' => 'ok']);
}
}