File: /var/www/dvpis2025/dvpis.kaunokolegija.lt/src/Controller/PositionController.php
<?php
namespace App\Controller;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use App\Entity\Position;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Position controller.
*/
#[Route(path: 'position')]
class PositionController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
) {
}
/**
* Lists all position entities.
*
* @Method("GET")
*/
#[Route(path: '/', name: 'position_index')]
public function indexAction()
{
$positions = $this->em->getRepository(Position::class)->findAll();
return $this->render('position/index.html.twig', array(
'positions' => $positions,
));
}
/**
* Creates a new position entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/new', name: 'position_new')]
public function newAction(Request $request)
{
$position = new Position();
$form = $this->createForm('App\Form\PositionType', $position);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($position);
$this->em->flush();
return $this->redirectToRoute('position_index', array('id' => $position->getId()));
}
return $this->render('position/new.html.twig', array(
'position' => $position,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing position entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/{id}/edit', name: 'position_edit')]
public function editAction(Request $request, Position $position)
{
$deleteForm = $this->createDeleteForm($position);
$editForm = $this->createForm('App\Form\PositionType', $position);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->em->flush();
return $this->redirectToRoute('position_edit', array('id' => $position->getId()));
}
return $this->render('position/edit.html.twig', array(
'position' => $position,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a position entity.
*
* @Method("DELETE")
*/
#[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'position_delete')]
public function deleteAction(Request $request, Position $position)
{
$form = $this->createDeleteForm($position);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->remove($position);
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('position_index');
}
/**
* Creates a form to delete a position entity.
*
* @param Position $position The position entity
*
* @return Form The form
*/
private function createDeleteForm(Position $position)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('position_delete', array('id' => $position->getId())))
->setMethod('DELETE')
->getForm()
;
}
}