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

namespace App\Controller;

use App\Entity\ActivitiesGroup;
use App\Entity\ActivitiesGroupLecturerPositionActivities;
use App\Entity\Department;
use App\Entity\FiveYearLecturerPlan;
use App\Entity\FiveYearLecturerPlanApproval;
use App\Entity\LecturerAgreement;
use App\Entity\LecturerPosition;
use App\Form\FiveYearLecturerPlanType;
use App\Repository\FiveYearLecturerPlanRepository;
use App\Repository\LecturerAgreementRepository;
use App\Service\DbDataFilter;
use App\Service\ProcessDatesValidation;
use DateTime;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
 * @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: 'fiveyearplan')]
class FiveYearPlanController 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_HEAD')) {
            $this->dbDataFilter->enableOnlyAcademicUnitFilter();

            return;
        }

        if (
            $this->authorizationChecker->isGranted('ROLE_ACADEMIC_UNIT_PROHEAD')
            or $this->authorizationChecker->isGranted('ROLE_ACADEMIC_UNIT_PROHEAD_SCIENCE')
        ) {
            $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");
    }

    /**
     * @Route("/", name="app_five_year_plan_index")
     * @Method("GET")
     */
    #[Route('/', name: 'app_five_year_plan_index', methods: ['GET'])]
    public function index(): Response
    {

        return $this->render('fiveyearplan/index.html.twig', [
            'lecturerContracts' => $this->em->getRepository(LecturerAgreement::class)->findAllFilter(),
        ]);
    }

    #[Route('/show/{id}', name: 'app_five_year_plan_show', methods: ['GET'])]
    public function show(LecturerAgreement $lecturerAgreement): Response
    {

        return $this->render('fiveyearplan/show.html.twig', [
            'lecturerFiveYearPlan' => $this->em->getRepository(FiveYearLecturerPlan::class)->findByIndex($lecturerAgreement),
            'lecturerPositionActivities' => $this->em->getRepository(ActivitiesGroupLecturerPositionActivities::class)->findBy([
                'lecturerPosition' => $lecturerAgreement->getLecturerPosition(),
            ], [
                'ActivitiesGroupId' => 'ASC',
                'LecturerPositionActivityId' => 'ASC',
            ]),
            'lecturerAgreement' => $lecturerAgreement,
            'lecturerActivitiesGroups' => $this->em->getRepository(ActivitiesGroup::class)->findAll(),
        ]);
    }

    /**
     * Creates a new department entity.
     *
     * @Route("/change/{lecturerAgreement}/{activityId}", name="app_five_year_plan_new")
     * @Method({"GET", "POST"})
     */
    public function changeAction(
        Request $request,
        LecturerAgreement $lecturerAgreement,
        ActivitiesGroupLecturerPositionActivities $activityId,
    )
    {
        $fiveYearPlan = $this->em->getRepository(FiveYearLecturerPlan::class)->findOneBy([
            'lecturerAgreement' => $lecturerAgreement,
            'ActivitiesGroupLecturerPositionActivity' => $activityId,
        ]);

        if ($fiveYearPlan === null) {
            $fiveYearPlan = new FiveYearLecturerPlan();
        }
        $fiveYearPlan->setActivitiesGroupLecturerPositionActivity($activityId);
        $fiveYearPlan->setLecturerAgreement($lecturerAgreement);

        $form = $this->createForm(FiveYearLecturerPlanType::class, $fiveYearPlan);

        $form->handleRequest($request);

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

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

        return $this->render('fiveyearplan/change.html.twig', array(
            'form' => $form->createView(),
            'lecturerAgreement' => $lecturerAgreement,
            'activitiesGroupLecturerPositionActivity' => $activityId,
        ));
    }

    private function getSelectedYears(FiveYearLecturerPlan $fiveYearLecturerPlan): array
    {
        $selectedYears = [];

        if ($fiveYearLecturerPlan->getYear1()) {
            $selectedYears[] = 'year1';
        }
        if ($fiveYearLecturerPlan->getYear2()) {
            $selectedYears[] = 'year2';
        }
        if ($fiveYearLecturerPlan->getYear3()) {
            $selectedYears[] = 'year3';
        }
        if ($fiveYearLecturerPlan->getYear4()) {
            $selectedYears[] = 'year4';
        }
        if ($fiveYearLecturerPlan->getYear5()) {
            $selectedYears[] = 'year5';
        }

        return $selectedYears;
    }

    /**
     * @Route("/{id}/approve", name="app_five_year_plan_approve")
     * @Method({"GET", "POST"})
     */
    public function approveAction(LecturerAgreement $lecturerAgreement)
    {
        $approval = $this->em->getRepository(FiveYearLecturerPlanApproval::class)
            ->findOneBy([
                'lecturerAgreement' => $lecturerAgreement->getId(),
            ], [
                'id' => 'DESC',
            ])
        ;

        //first approval from lecturer
        if (
            empty($approval)
            && $this->authorizationChecker->isGranted('ROLE_LECTURER')
            && $lecturerAgreement->getLecturer()->getEmail() === $this->getUser()->getEmail()
        ) {
            $approval = (new FiveYearLecturerPlanApproval())
                ->setLecturerAgreement($lecturerAgreement)
                ->setRole('ROLE_LECTURER')
                ->setApproveDate(new DateTime())
                ->setUser($this->getUser())
            ;

            $this->em->persist($approval);
            $this->em->flush();

            $this->addFlash('success', "Patvirtinta");

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


        //Suderinta su katedros vedėju / akademijos / centro vadovu
        if (
            $approval->getRole() === 'ROLE_LECTURER'
            && $this->authorizationChecker->isGranted('ROLE_DEPARTMENT_HEAD')
            && $this->getUser()->getRole() === 'ROLE_DEPARTMENT_HEAD'
        ) {
            $approval = (new FiveYearLecturerPlanApproval())
                ->setLecturerAgreement($lecturerAgreement)
                ->setRole('ROLE_DEPARTMENT_HEAD')
                ->setApproveDate(new DateTime())
                ->setUser($this->getUser())
            ;

            $this->em->persist($approval);
            $this->em->flush();

            $this->addFlash('success', "Patvirtinta (Suderinta su katedros vedėju / akademijos / centro vadovu)");
            return $this->redirectToRoute('app_five_year_plan_index');
        }


        //Suderinta su prodekanu studijoms
        if (
            (
                $approval->getRole() === 'ROLE_DEPARTMENT_HEAD'
                or $approval->getRole() === 'ROLE_ACADEMIC_UNIT_PROHEAD_SCIENCE'
            )
            && $this->authorizationChecker->isGranted('ROLE_ACADEMIC_UNIT_PROHEAD')
            && $this->getUser()->getRole() === 'ROLE_ACADEMIC_UNIT_PROHEAD'
        ) {
            $approval = (new FiveYearLecturerPlanApproval())
                ->setLecturerAgreement($lecturerAgreement)
                ->setRole('ROLE_ACADEMIC_UNIT_PROHEAD')
                ->setApproveDate(new DateTime())
                ->setUser($this->getUser())
            ;

            $this->em->persist($approval);
            $this->em->flush();

            $this->addFlash('success', "Patvirtinta (Suderinta su prodekanu studijoms)");

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

        //Suderinta su prodekanu mokslui / mokslui ir menui
        if (
            (
                $approval->getRole() === 'ROLE_DEPARTMENT_HEAD'
                or $approval->getRole() === 'ROLE_ACADEMIC_UNIT_PROHEAD'
            )
            && $this->authorizationChecker->isGranted('ROLE_ACADEMIC_UNIT_PROHEAD_SCIENCE')
            && $this->getUser()->getRole() === 'ROLE_ACADEMIC_UNIT_PROHEAD_SCIENCE'
        ) {
            $approval = (new FiveYearLecturerPlanApproval())
                ->setLecturerAgreement($lecturerAgreement)
                ->setRole('ROLE_ACADEMIC_UNIT_PROHEAD_SCIENCE')
                ->setApproveDate(new DateTime())
                ->setUser($this->getUser())
            ;

            $this->em->persist($approval);
            $this->em->flush();

            $this->addFlash('success', "Patvirtinta (Suderinta su prodekanu studijoms)");
            return $this->redirectToRoute('app_five_year_plan_index');
        }

        //Suderinta su fakulteto dekanu
        if (
            (
                $approval->getRole() === 'ROLE_ACADEMIC_UNIT_PROHEAD'
                or $approval->getRole() === 'ROLE_ACADEMIC_UNIT_PROHEAD_SCIENCE'
            )
            && $this->authorizationChecker->isGranted('ROLE_ACADEMIC_UNIT_HEAD')
            && $this->getUser()->getRole() === 'ROLE_ACADEMIC_UNIT_HEAD'
        ) {
            $approval = (new FiveYearLecturerPlanApproval())
                ->setLecturerAgreement($lecturerAgreement)
                ->setRole('ROLE_ACADEMIC_UNIT_HEAD')
                ->setApproveDate(new DateTime())
                ->setUser($this->getUser())
            ;

            $this->em->persist($approval);
            $this->em->flush();

            $this->addFlash('success', "Patvirtinta (Suderinta su fakulteto dekanu)");

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


        $this->addFlash('danger', "Deja, patvirtinti nepavyko!");

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