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