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

namespace App\Controller;

use App\Entity\ActivitiesPlan;
use App\Form\TmmvLecturerPlanType;
use Doctrine\DBAL\Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use App\Entity\LecturerApprovedActivities;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Entity\TmmvLecturer;
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: 'tmmvlecturerplan')]
class TmmvLecturerPlanController 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 tmmvLecturer entities.
     *
     * @Method("GET")
     */
    #[Route(path: '/', name: 'tmmvlecturer_plan_index')]
    public function indexAction(Request $request)
    {


        $tmmvLecturers = $this->em->getRepository(TmmvLecturer::class)->findAll();

        $lecturer = $this->dbDataFilter->getLecturerFromUser();
        $activities = $activitiesSum = $tmmvLecturersSum = [];
        if ($lecturer) {
            $activities = $this->em->getRepository(ActivitiesPlan::class)->findLecturerActivities($lecturer);
            $activitiesSum = $this->em->getRepository(ActivitiesPlan::class)->findLecturerActivitiesSum($lecturer);
            $tmmvLecturersSum = $this->em->getRepository(TmmvLecturer::class)->findAllLecturerSum($lecturer);
        }

        $filter = [];

        if (!empty($request->query)) {
            $filter = $request->query;
        }

        $tableSearch = '';
        if (!empty($filter->get('lecturer'))) {
            $tableSearch = $filter->get('lecturer');
        }

        return $this->render('tmmvlecturerplan/index.html.twig', array(
                    'tmmvLecturers' => $tmmvLecturers,
                    'tableSearch' => $tableSearch,
                    'activitiesPlans' => $activities,
                    'activitiesPlansSum' => $activitiesSum,
                    'tmmvLecturersSum' => $tmmvLecturersSum['tmmvHours'] ?? 0,
                    'lectApprovals' => $this->em->getRepository(LecturerApprovedActivities::class)->getListByLecturerId(),
        ));
    }

    /**
     * Creates a new tmmvLecturer entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/new', name: 'tmmvlecturer_plan_new')]
    public function newAction(Request $request)
    {

        if (!$this->dateValidator->isActiveProcessPlan()) {
            throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
        }

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

        $this->dbDataFilter->enableOnlyLecturerApprovedFilter();

        $tmmvLecturer = new Tmmvlecturer();

        $lecturer = $this->dbDataFilter->getLecturerFromUser();
        if ($lecturer) {
            $activitiesSum = $this->em->getRepository(ActivitiesPlan::class)->findLecturerActivitiesSum($lecturer);
            $tmmvLecturersSum = $this->em->getRepository(TmmvLecturer::class)->findAllLecturerSum($lecturer);
        }
        $leftToPlan = ($activitiesSum['tmmvHours'] ?? 99999) - ($tmmvLecturersSum['tmmvHours'] ?? 0);

        $form = $this->createForm('App\Form\TmmvLecturerPlanType', $tmmvLecturer, [
            'isEnabledDepartmentHeadComment' => $this->isGranted('ROLE_DEPARTMENT_HEAD'),
            'maxHours' => $leftToPlan,
            'plannedHours' => $tmmvLecturer->getHours(),
            'lecturerId' => null,
        ]);
        $form->handleRequest($request);

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

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

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

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

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

        $this->dbDataFilter->enableOnlyLecturerApprovedFilter();

        $deleteForm = $this->createDeleteForm($tmmvLecturer);

        $lecturer = $this->dbDataFilter->getLecturerFromUser();
        if ($lecturer) {
            $activitiesSum = $this->em->getRepository(ActivitiesPlan::class)->findLecturerActivitiesSum($lecturer);
            $tmmvLecturersSum = $this->em->getRepository(TmmvLecturer::class)->findAllLecturerSum($lecturer);
        }

        $leftToPlan = ($activitiesSum['tmmvHours'] ?? 99999) - ($tmmvLecturersSum['tmmvHours'] ?? 0);

        $editForm = $this->createForm(
            TmmvLecturerPlanType::class,
            $tmmvLecturer,
            [
                'isEnabledDepartmentHeadComment' => $this->isGranted('ROLE_DEPARTMENT_HEAD'),
                'maxHours' => $leftToPlan,
                'plannedHours' => $tmmvLecturer->getHours(),
                'lecturerId' => $tmmvLecturer->getLecturer()?->getId(),
            ]
        );

        $editForm->handleRequest($request);

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

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

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

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

        $this->dbDataFilter->enableOnlyLecturerApprovedFilter();

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

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

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