HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/dvpis2026/dvpis.kaunokolegija.lt/src/Controller/Poll/PollController.php
<?php

namespace App\Controller\Poll;

use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use App\Entity\Poll\Poll;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;

/**
 * Poll controller.
 */
#[Route(path: 'poll_poll')]
class PollController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $em,
    ) {
    }

    /**
     * Lists all poll entities.
     *
     * @Method("GET")
     */
    #[Route(path: '/', name: 'poll_poll_index')]
    public function indexAction()
    {


        $polls = $this->em->getRepository(Poll::class)->findAll();

        return $this->render('poll/poll/index.html.twig', array(
            'polls' => $polls,
        ));
    }

    /**
     * Creates a new poll entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/new', name: 'poll_poll_new')]
    public function newAction(Request $request)
    {
        $poll = new Poll();
        $form = $this->createForm('App\Form\Poll\PollType', $poll);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->persist($poll);
            $this->em->flush();

            return $this->redirectToRoute('poll_poll_index', array('id' => $poll->getId()));
        }

        return $this->render('poll/poll/new.html.twig', array(
            'poll' => $poll,
            'form' => $form->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing poll entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/{id}/edit', name: 'poll_poll_edit')]
    public function editAction(Request $request, Poll $poll)
    {
        $deleteForm = $this->createDeleteForm($poll);
        $editForm = $this->createForm('App\Form\Poll\PollType', $poll);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->em->flush();

            return $this->redirectToRoute('poll_poll_edit', array('id' => $poll->getId()));
        }

        return $this->render('poll/poll/edit.html.twig', array(
            'poll' => $poll,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a poll entity.
     *
     * @Method("DELETE")
     */
    #[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'poll_poll_delete')]
    public function deleteAction(Request $request, Poll $poll)
    {
        $form = $this->createDeleteForm($poll);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->remove($poll);
            $this->em->flush();
        }

        return $this->redirectToRoute('poll_poll_index');
    }

    /**
     * Creates a form to delete a poll entity.
     *
     * @param Poll $poll The poll entity
     *
     * @return Form The form
     */
    private function createDeleteForm(Poll $poll)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('poll_poll_delete', array('id' => $poll->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}