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

namespace App\Controller;

use App\Entity\ActivitiesPlan;
use Doctrine\DBAL\Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use App\Entity\LecturerApprovedActivities;
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: 'ktvlecturerplan')]
class KtvLecturerPlanController 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_plan_index')]
    public function indexAction(Request $request)
    {

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

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

        $filter = [];

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

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

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

    /**
     * Creates a new ktvLecturer entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/new', name: 'ktvlecturer_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();
        }

        $ktvLecturer = new Ktvlecturer();
        $lecturer = $this->dbDataFilter->getLecturerFromUser();

        if ($lecturer) {
            $this->dbDataFilter->enableOnlyLecturerApprovedFilter();

            $activitiesSum = $this->em->getRepository(ActivitiesPlan::class)->findLecturerActivitiesSum($lecturer);
            $ktvLecturersSum = $this->em->getRepository(KtvLecturer::class)->findAllLecturerSum($lecturer);
        }
        $leftToPlan = ($activitiesSum['ktvHours'] ?? 99999) - ($ktvLecturersSum['ktvHours'] ?? 0);

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

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

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

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

    /**
     * Displays a form to edit an existing ktvLecturer entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/{id}/edit', name: 'ktvlecturer_plan_edit')]
    public function editAction(Request $request, KtvLecturer $ktvLecturer)
    {

        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($ktvLecturer);

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

        $leftToPlan = ($activitiesSum['ktvHours'] ?? 99999) - ($ktvLecturersSum['ktvHours'] ?? 0);

        $editForm = $this->createForm('App\Form\KtvLecturerPlanType', $ktvLecturer, [
            'isEnabledDepartmentHeadComment' => $this->isGranted('ROLE_DEPARTMENT_HEAD'),
            'maxHours' => $leftToPlan,
            'plannedHours' => $ktvLecturer->getHours(),
            'lecturerId' => $ktvLecturer->getLecturer()->getId(),
        ]);

        $editForm->handleRequest($request);

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

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

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

    /**
     * Deletes a ktvLecturer entity.
     *
     * @Method("DELETE")
     */
    #[Route(path: '/{id}', name: 'ktvlecturer_plan_delete', requirements: ['id' => '\d+'])]
    public function deleteAction(Request $request, KtvLecturer $ktvLecturer)
    {

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

        $this->dbDataFilter->enableOnlyLecturerApprovedFilter();

        $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_plan_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_plan_delete', array('id' => $ktvLecturer->getId())))
                        ->setMethod('DELETE')
                        ->getForm()
        ;
    }
}