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/KtvLecturerController.php
<?php

namespace App\Controller;

use Doctrine\DBAL\Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Entity\KtvLecturer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\Service\ProcessDatesValidation;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\DbDataFilter;

/**
 * @Security("
    is_granted('ROLE_LECTURER')
    or is_granted('ROLE_DEPARTMENT_ADMINISTRATOR')
    or is_granted('ROLE_DEPARTMENT_HEAD')
    or is_granted('ROLE_ACADEMIC_UNIT_PROHEAD')
")
 */
#[Route(path: 'ktvlecturer')]
class KtvLecturerController extends AbstractController
{
    public function __construct(
        private readonly ProcessDatesValidation $dateValidator,
        private readonly EntityManagerInterface $em,
        private readonly DbDataFilter $dbDataFilter,
        private readonly AuthorizationCheckerInterface $authorizationChecker
    ) {

        if ($this->authorizationChecker->isGranted('ROLE_ACADEMIC_UNIT_PROHEAD')) {
            $this->dbDataFilter->enableOnlyAcademicUnitFilter();

            return;
        }

        if ($this->authorizationChecker->isGranted('ROLE_DEPARTMENT_HEAD')) {
            $this->dbDataFilter->enableOnlyDepartmentFilter();

            return;
        }

        if ($this->authorizationChecker->isGranted('ROLE_DEPARTMENT_ADMINISTRATOR')) {
            $this->dbDataFilter->enableOnlyDepartmentFilter();

            return;
        }

        if ($this->authorizationChecker->isGranted('ROLE_LECTURER')) {
            $this->dbDataFilter->enableOnlyLecturerFilter();

            return;
        }

        throw new AccessDeniedHttpException("Jūsų naudotojas negali pasiekti šio turinio");
    }

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


        $ktvLecturers = $this->em->getRepository(KtvLecturer::class)->findAll();

        return $this->render('ktvlecturer/index.html.twig', [
            'ktvLecturers' => $ktvLecturers,
        ]);
    }

    /**
     * Creates a new ktvLecturer entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/new', name: 'ktvlecturer_new')]
    public function newAction(Request $request)
    {
        if (!$this->dateValidator->isActiveFact()) {
            throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
        }

        if ($this->dbDataFilter->getUserInfo()->getRole() === "ROLE_DEPARTMENT_ADMINISTRATOR") {
            $this->dbDataFilter->enableOnlyLecturerFilter();
        }

        $ktvLecturer = new Ktvlecturer();
        $form = $this->createForm(
            'App\Form\KtvLecturerNewType',
            $ktvLecturer,
            ['isEnabledDepartmentHeadComment' => $this->isGranted('ROLE_DEPARTMENT_HEAD'),]
        );
        $form->handleRequest($request);

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

            return $this->redirectToRoute('ktvlecturer_index', ['id' => $ktvLecturer->getId()]);
        }

        return $this->render('ktvlecturer/new.html.twig', [
            'ktvLecturer' => $ktvLecturer,
            'form' => $form->createView(),
        ]);
    }

    /**
     * Displays a form to edit an existing ktvLecturer entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/{id}/edit', name: 'ktvlecturer_edit')]
    public function editAction(Request $request, KtvLecturer $ktvLecturer)
    {
        if (!$this->dateValidator->isActiveFact()) {
            throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
        }

        if (empty($ktvLecturer->getDescriptionFact())) {
            $ktvLecturer->setDescriptionFact($ktvLecturer->getDescription());
        }

        $deleteForm = $this->createDeleteForm($ktvLecturer);
        $editForm = $this->createForm(
            'App\Form\KtvLecturerEditType',
            $ktvLecturer,
            ['isEnabledDepartmentHeadComment' => $this->isGranted('ROLE_DEPARTMENT_HEAD'),]
        );
        $editForm->handleRequest($request);

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

            return $this->redirectToRoute('ktvlecturer_edit', ['id' => $ktvLecturer->getId()]);
        }

        return $this->render('ktvlecturer/edit.html.twig', [
            'ktvLecturer' => $ktvLecturer,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ]);
    }

    /**
     * Deletes a ktvLecturer entity.
     *
     * @Method("DELETE")
     */
    #[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'ktvlecturer_delete')]
    public function deleteAction(Request $request, KtvLecturer $ktvLecturer)
    {
        if (!$this->dateValidator->isActiveFact()) {
            throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
        }

        $form = $this->createDeleteForm($ktvLecturer);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->remove($ktvLecturer);
            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('ktvlecturer_index');
    }

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