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

namespace App\Controller;

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

/**
* Finalproject controller.
* @Security("
   is_granted('ROLE_DEPARTMENT_HEAD')
   or is_granted('ROLE_DEPARTMENT_ADMINISTRATOR')
   or is_granted('ROLE_ACADEMIC_UNIT_PROHEAD')
   ")
*/
#[Route(path: 'finalproject')]
class FinalProjectController extends AbstractController
{
    private bool $onlyView;

    public function __construct(
        private readonly ProcessDatesValidation $dateValidator,
        private readonly EntityManagerInterface $em,
        private readonly AuthorizationCheckerInterface $authorizationChecker,
    ) {
        $this->onlyView = $this->dateValidator->isOnlyView();

        if (
            $this->authorizationChecker->isGranted('ROLE_DEPARTMENT_ADMINISTRATOR')
            && !$this->authorizationChecker->isGranted('ROLE_STUDY_DEPARTMENT')
        ) {
            $this->onlyView = true;
        }

        if ($this->em->getFilters()->isEnabled('department_filter')) {
            $this->em->getFilters()->disable('department_filter');
        }

        if ($this->em->getFilters()->isEnabled('lecturer_filter')) {
            $this->em->getFilters()->disable('lecturer_filter');
        }
    }

    /**
     * Lists all finalProject entities.
     *
     * @Method("GET")
     */
    #[Route(path: '/', name: 'finalproject_index')]
    public function indexAction()
    {
        throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");

//        $finalProjects = $this->em->getRepository(FinalProject::class)->findAll();
//
//        return $this->render('finalproject/index.html.twig', array(
//                    'finalProjects' => $finalProjects,
//            'studentCount' => $this->em
//                ->getRepository(StudentCountProgramPlan::class)
//                ->getAllByProgramCourseType(),
//        ));
    }

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

        $finalProject = new Finalproject();
        $form = $this->createForm('App\Form\FinalProjectType', $finalProject);
        $form->handleRequest($request);

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

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

        return $this->render('finalproject/new.html.twig', array(
                    'finalProject' => $finalProject,
                    'form' => $form->createView(),
                    'studiesProgramPlan' => $studiesProgramPlan,
                    'lecturerData' => json_encode($this->getLecturerData()),
            'studentCount' => $this->em
                ->getRepository(StudentCountProgramPlan::class)
                ->getAllByProgramCourseType(),
        ));
    }

    /**
     * Finds and displays a finalProject entity.
     *
     * @Method("GET")
     */
    #[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'finalproject_show')]
    public function showAction(StudiesProgramPlan $studiesProgramPlan)
    {

        return $this->render('finalproject/show.html.twig', array(
                    'studiesProgramPlan' => $studiesProgramPlan,
                    'isOnlyView' => $this->onlyView,
            'studentCount' => $this->em
                ->getRepository(StudentCountProgramPlan::class)
                ->getAllByProgramCourseType(),
        ));
    }

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

        $studiesProgramPlan = $finalProject->getStudiesProgramPlan();
        $deleteForm = $this->createDeleteForm($finalProject);
        $editForm = $this->createForm('App\Form\FinalProjectType', $finalProject);
        $editForm->handleRequest($request);

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

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

        return $this->render('finalproject/edit.html.twig', array(
                    'studiesProgramPlan' => $studiesProgramPlan,
                    'finalProject' => $finalProject,
                    'form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                    'lecturerData' => json_encode($this->getLecturerData()),
            'studentCount' => $this->em
                ->getRepository(StudentCountProgramPlan::class)
                ->getAllByProgramCourseType(),
        ));
    }

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

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

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

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

    private function getLecturerData()
    {


        $lecturerRepo = $this->em->getRepository(Lecturer::class);
        $lecturersData = $lecturerRepo->findAll();

        $lecturerDepPos = [];
        foreach ($lecturersData as $lecturerData) {
            $lecturerDepPos[$lecturerData->getId()]['position'] = $lecturerData->getLecturerPosition()?->getId();
            $lecturerDepPos[$lecturerData->getId()]['department'] = $lecturerData->getDepartment()->getId();
            $lecturerDepPos[$lecturerData->getId()]['isNotFullTime'] = $lecturerData->getIsNotFullTime();
        }

        return $lecturerDepPos;
    }
}