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