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/dvpis2025/dvpis.kaunokolegija.lt/src/Controller/BlogController.php
<?php

namespace App\Controller;

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

/**
 * Blog controller.
 *
 * @Security("is_granted('ROLE_STUDY_DEPARTMENT')")
 */
#[Route(path: 'blog')]
class BlogController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $em,
    ) {
    }

    /**
     * Lists all blog entities.
     *
     * @Method("GET")
     */
    #[Route(path: '/', name: 'blog_index')]
    public function indexAction()
    {
        $blogs = $this->em->getRepository(Blog::class)->findAll();

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

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

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

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

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

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

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

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

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

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

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

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

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