File: /var/www/dvpis2026/dvpis.kaunokolegija.lt/src/Controller/FieldOfStudyController.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\FieldOfStudy;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Fieldofstudy controller.
*
* @Security("is_granted('ROLE_STUDY_DEPARTMENT')")
*/
#[Route(path: 'fieldofstudy')]
class FieldOfStudyController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
) {
}
/**
* Lists all fieldOfStudy entities.
*
* @Method("GET")
*/
#[Route(path: '/', name: 'fieldofstudy_index')]
public function indexAction()
{
$fieldOfStudies = $this->em->getRepository(FieldOfStudy::class)->findAll();
return $this->render('fieldofstudy/index.html.twig', array(
'fieldOfStudies' => $fieldOfStudies,
));
}
/**
* Creates a new fieldOfStudy entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/new', name: 'fieldofstudy_new')]
public function newAction(Request $request)
{
$fieldOfStudy = new Fieldofstudy();
$form = $this->createForm('App\Form\FieldOfStudyType', $fieldOfStudy);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($fieldOfStudy);
$this->em->flush();
return $this->redirectToRoute('fieldofstudy_index', array('id' => $fieldOfStudy->getId()));
}
return $this->render('fieldofstudy/new.html.twig', array(
'fieldOfStudy' => $fieldOfStudy,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing fieldOfStudy entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/{id}/edit', name: 'fieldofstudy_edit')]
public function editAction(Request $request, FieldOfStudy $fieldOfStudy)
{
$deleteForm = $this->createDeleteForm($fieldOfStudy);
$editForm = $this->createForm('App\Form\FieldOfStudyType', $fieldOfStudy);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->em->flush();
return $this->redirectToRoute('fieldofstudy_index', array('id' => $fieldOfStudy->getId()));
}
return $this->render('fieldofstudy/edit.html.twig', array(
'fieldOfStudy' => $fieldOfStudy,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a fieldOfStudy entity.
*
* @Method("DELETE")
*/
#[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'fieldofstudy_delete')]
public function deleteAction(Request $request, FieldOfStudy $fieldOfStudy)
{
$form = $this->createDeleteForm($fieldOfStudy);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->remove($fieldOfStudy);
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('fieldofstudy_index');
}
/**
* Creates a form to delete a fieldOfStudy entity.
*
* @param FieldOfStudy $fieldOfStudy The fieldOfStudy entity
*
* @return Form The form
*/
private function createDeleteForm(FieldOfStudy $fieldOfStudy)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('fieldofstudy_delete', array('id' => $fieldOfStudy->getId())))
->setMethod('DELETE')
->getForm()
;
}
}