diff --git a/src/Controller/EmployeeController.php b/src/Controller/EmployeeController.php index 36a4fdb..11f2de5 100644 --- a/src/Controller/EmployeeController.php +++ b/src/Controller/EmployeeController.php @@ -2,17 +2,80 @@ namespace App\Controller; +use App\Entity\Employee; +use App\Form\EmployeeType; +use App\Repository\EmployeeRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; -class EmployeeController extends AbstractController +#[Route('/employee', name: 'employee')] +final class EmployeeController extends AbstractController { - #[Route('/employee', name: 'app_employee')] - public function index(): Response + #[Route(name: '_index', methods: ['GET'])] + public function index(EmployeeRepository $employeeRepository): Response { return $this->render('employee/index.html.twig', [ - 'controller_name' => 'EmployeeController', + 'employees' => $employeeRepository->findAll(), ]); } + + #[Route('/new', name: '_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager): Response + { + $employee = new Employee(); + $form = $this->createForm(EmployeeType::class, $employee); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->persist($employee); + $entityManager->flush(); + + return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('employee/new.html.twig', [ + 'employee' => $employee, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: '_show', methods: ['GET'])] + public function show(Employee $employee): Response + { + return $this->render('employee/show.html.twig', [ + 'employee' => $employee, + ]); + } + + #[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(EmployeeType::class, $employee); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('employee/edit.html.twig', [ + 'employee' => $employee, + 'form' => $form, + ]); + } + + #[Route('/{id}', name: '_delete', methods: ['POST'])] + public function delete(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response + { + if ($this->isCsrfTokenValid('delete'.$employee->getId(), $request->getPayload()->getString('_token'))) { + $entityManager->remove($employee); + $entityManager->flush(); + } + + return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER); + } } diff --git a/src/Form/EmployeeType.php b/src/Form/EmployeeType.php new file mode 100644 index 0000000..7868b3b --- /dev/null +++ b/src/Form/EmployeeType.php @@ -0,0 +1,42 @@ +add('email', EmailType::class, ['label' => 'Email Address']) + ->add('firstName', TextType::class, ['label' => 'First Name']) + ->add('lastName', TextType::class, ['label' => 'Last Name']) + ->add('password', PasswordType::class, ['label' => 'Password']) + ->add('roles', ChoiceType::class, [ + 'label' => 'Roles (comma-separated)', + 'required' => false, + 'choices' => [ + 'User' => 'ROLE_USER', + 'Admin' => 'ROLE_ADMIN', + ], + 'multiple' => true, // Allow multiple selections + 'expanded' => true, // Render as checkboxes + ]) + ->add('save', SubmitType::class, ['label' => 'Add Employee']); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Employee::class, + ]); + } +} \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 4bb4a26..ebef434 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -13,6 +13,13 @@ {% endblock %}
+ + {% for label, messages in app.flashes %} + {% for message in messages %} +