File: /var/www/dvpis2026/dvpis.kaunokolegija.lt/src/Controller/DepartmentController.php
<?php
namespace App\Controller;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Entity\Department;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Department controller.
*
* @Security("is_granted('ROLE_STUDY_DEPARTMENT')")
*/
#[Route(path: 'department')]
class DepartmentController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
) {
}
/**
* Lists all department entities.
*
* @Method("GET")
*/
#[Route(path: '/', name: 'department_index')]
public function indexAction()
{
$departments = $this->em->getRepository(Department::class)->findAll();
return $this->render('department/index.html.twig', array(
'departments' => $departments,
));
}
/**
* Creates a new department entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/new', name: 'department_new')]
public function newAction(Request $request)
{
$department = new Department();
$form = $this->createForm('App\Form\DepartmentType', $department);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($department);
$this->em->flush();
return $this->redirectToRoute('department_index', array('id' => $department->getId()));
}
return $this->render('department/new.html.twig', array(
'department' => $department,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing department entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/{id}/edit', name: 'department_edit')]
public function editAction(Request $request, Department $department)
{
$deleteForm = $this->createDeleteForm($department);
$editForm = $this->createForm('App\Form\DepartmentType', $department);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->em->flush();
return $this->redirectToRoute('department_index', array('id' => $department->getId()));
}
return $this->render('department/edit.html.twig', array(
'department' => $department,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a department entity.
*
* @Method("DELETE")
*/
#[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'department_delete')]
public function deleteAction(Request $request, Department $department)
{
$form = $this->createDeleteForm($department);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->remove($department);
try {
$this->em->flush();
} catch (Exception $ex) {
$this->addFlash('warning', "Ištrinti įrašo nepavyko! Jis gali turėti susijusių įrašų." . $ex->getMessage());
}
}
return $this->redirectToRoute('department_index');
}
/**
* Creates a form to delete a department entity.
*
* @param Department $department The department entity
*
* @return Form The form
*/
private function createDeleteForm(Department $department)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('department_delete', array('id' => $department->getId())))
->setMethod('DELETE')
->getForm()
;
}
}