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

namespace App\Controller;

use App\Entity\AcademicGroup;
use App\Entity\AcademicGroupPlan;
use App\Entity\AcademicUnit;
use App\Entity\Department;
use App\Entity\Lecturer;
use App\Entity\LecturerPlan;
use App\Entity\StudiesModule;
use App\Entity\StudiesProgram;
use App\Entity\StudiesProgramPlan;
use App\Entity\YearInformation;
use App\Service\DbDataFilter;
use App\Service\ProcessDatesValidation;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
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\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
* Lecturerplan controller.
*
* @Security("
is_granted('ROLE_DEPARTMENT_HEAD')
or is_granted('ROLE_ACADEMIC_UNIT_PROHEAD')
or is_granted('ROLE_LECTURER')
")
*/
#[Route(path: 'lecturerplan')]
class LecturerPlanController extends AbstractController
{
    private bool $onlyView;

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

        if ($this->authorizationChecker->isGranted('ROLE_ACADEMIC_UNIT_PROHEAD')) {
            $this->dbDataFilter->enableAcademicUnitFilter();
            return;
        }

        if ($this->authorizationChecker->isGranted('ROLE_DEPARTMENT_HEAD')) {
            $this->dbDataFilter->enableDepartmentFilter();
            return;
        }

        if ($this->authorizationChecker->isGranted('ROLE_DEPARTMENT_ADMINISTRATOR')) {
            $this->dbDataFilter->enableDepartmentFilter();
            $this->onlyView = true;
            return;
        }

        if ($this->authorizationChecker->isGranted('ROLE_LECTURER')) {
            $this->dbDataFilter->enableOnlyLecturerFilter();
            $this->onlyView = true;
            return;
        }

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

    /**
     * Lists all lecturerPlan entities.
     *
     * @Method("GET")
     */
    #[Route(path: '/', name: 'lecturerplan_index')]
    public function indexAction(Request $request)
    {
        $filter = [];

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

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

//        $lecturerPlans = $this->em->getRepository(LecturerPlan::class)->findAllQueryBuilder($filter);
        $lecturerPlans = $this->em->getRepository(LecturerPlan::class)->findAllQueryBuilderJSON($filter);

        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');
        }
        $academicGroups = $this->em->getRepository(AcademicGroup::class)->getAllAcademicGroupsByName();
        $studiesProgramPlans = $this->em->getRepository(StudiesProgramPlan::class)->getAllByProgramFormCourse();
        $studiesModules = $this->em->getRepository(StudiesModule::class)->findAll();
        $studiesPrograms = $this->em->getRepository(StudiesProgram::class)->findAll();
        $departments = $this->em->getRepository(Department::class)->findAll();
        $academicUnits = $this->em->getRepository(AcademicUnit::class)->findAll();

        $lecturerPlanJson = [];
        foreach ($lecturerPlans as $aLecturerPlans) {
            /** @var AcademicGroupPlan $lecturerPlan */
            foreach ($aLecturerPlans->getAcademicGroupPlans() as $lecturerPlan) {
                $stream = [];

                foreach ($aLecturerPlans->getAcademicGroupPlans() as $lecturerPlanGroup) {
                    $stream[] = (string)$lecturerPlanGroup->getAcademicGroup();
                }
                $contactHours = $aLecturerPlans->getTheoryHours() + $aLecturerPlans->getPracticGroup() + $aLecturerPlans->getPracticGroup1() + $aLecturerPlans->getPracticGroup2() + $aLecturerPlans->getConsultationHours() + $aLecturerPlans->getEgzamHours();
                $lecturerPlanJson[] = [
                    (string)$lecturerPlan->getAcademicGroup(),
                    (string)$lecturerPlan->getAcademicGroup()->getStudiesForm(),
                    (string)$lecturerPlan->getStudiesProgramPlan()->getSemester(),
                    (string)$aLecturerPlans->getLecturer(),
                    (string)$aLecturerPlans->getLecturerPosition(),
                    (string)$aLecturerPlans->getStudentCount(),
                    '<a href="' . $this->generateUrl('academicgroupplan_show', ['id' => $lecturerPlan->getId()]) . '">' . $lecturerPlan->getStudiesProgramPlan()->getStudiesModule() . '</a>',
                    $aLecturerPlans->getTheoryHours(),
                    $aLecturerPlans->getPracticGroup(),
                    $aLecturerPlans->getPracticGroup1(),
                    $aLecturerPlans->getPracticGroup2(),
                    $aLecturerPlans->getConsultationHours(),
                    $aLecturerPlans->getEgzamHours(),
                    implode(",", $stream)
                    . '(' . count($aLecturerPlans->getAcademicGroupPlans()) . ')',
                    $contactHours,
                    $aLecturerPlans->getScoreHours(),
                    $aLecturerPlans->getPrepareContactHours(),
                    $aLecturerPlans->getAdditionalConsultationHours(),
                    $aLecturerPlans->getStudiesModuleDescriptionHours(),
                    $aLecturerPlans->getStudiesModulePreparationHours(),
                    $aLecturerPlans->getStudiesModuleRenewHours(),
                    $contactHours +
                    $aLecturerPlans->getScoreHours() +
                    $aLecturerPlans->getPrepareContactHours() +
                    $aLecturerPlans->getAdditionalConsultationHours() +
                    $aLecturerPlans->getStudiesModuleDescriptionHours() +
                    $aLecturerPlans->getStudiesModulePreparationHours() +
                    $aLecturerPlans->getStudiesModuleRenewHours(),
                ];
            }
        }

        return $this->render('lecturerplan/index.html.twig', array(
            'academicUnits' => $academicUnits,
            'departments' => $departments,
            'studiesPrograms' => $studiesPrograms,
            'studiesModules' => $studiesModules,
            'academicGroups' => $academicGroups,
            'studiesProgramPlans' => $studiesProgramPlans,
//                    'lecturerPlans' => $lecturerPlans,
            'lecturerPlanJson' => json_encode($lecturerPlanJson),
            'tableSearch' => $tableSearch,
        ));
    }

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

        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');
        }

        $lecturerPlan = new LecturerPlan();
        $form = $this->createForm('App\Form\LecturerPlanType', $lecturerPlan);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $lecturerPlan->addAcademicGroupPlan($academicGroupPlan);

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

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

        $yearInformation = $this->em->getRepository(YearInformation::class)->findActual();

        return $this->render('lecturerplan/new.html.twig', array(
            'academicGroupPlan' => $academicGroupPlan,
            'lecturerData' => json_encode($this->getLecturerData()),
            'lecturerPlan' => $lecturerPlan,
            'form' => $form->createView(),
            'yearInformation' => $yearInformation,
        ));
    }

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



        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');
        }

        $deleteForm = $this->createDeleteForm($lecturerPlan);
        $editForm = $this->createForm('App\Form\LecturerPlanType', $lecturerPlan);
        $editForm->handleRequest($request);

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

            return $this->redirectToRoute('lecturerplan_edit', array('id' => $lecturerPlan->getId(), 'a_id' => $academicGroupPlan->getId()));
        }

        $lecturerPlanAcademicGroups = $lecturerPlan->getAcademicGroupPlans();

        $academicGroups = [];
        foreach ($lecturerPlanAcademicGroups as $lecturerPlanAcademicGroup) {
            $academicGroups[$lecturerPlanAcademicGroup->getAcademicGroup()->getId()] = $lecturerPlanAcademicGroup->getAcademicGroup()->getId();
        }

        $streamAcademicGroupPlans = $this->em->getRepository(AcademicGroupPlan::class)->getOtherGroupsPlansById($academicGroups);
        $this->em->getFilters()->enable('department_filter');

        $yearInformation = $this->em->getRepository(YearInformation::class)->findActual();

        return $this->render('lecturerplan/edit.html.twig', array(
            'lecturerPlan' => $lecturerPlan,
            'academicGroupPlan' => $academicGroupPlan,
            'streamAcademicGroupPlans' => $streamAcademicGroupPlans,
            'lecturerData' => json_encode($this->getLecturerData()),
            'form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
            'yearInformation' => $yearInformation,
        ));
    }

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

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

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

    /**
     * Deletes a lecturerPlan entity.
     *
     * @ParamConverter("academicGroupPlan", options={"mapping": {"a_id" : "id"}})
     * @ParamConverter("lecturerPlan", options={"mapping": {"id" : "id"}})
     * @ParamConverter("callBack", options={"mapping": {"cb_id" : "id"}})
     * @Method("GET")
     */
    #[Route(path: '/{a_id}/delete/{id}/cb/{cb_id}', name: 'stream_lecturerplan_delete')]
    public function deleteStreamAction(Request $request, AcademicGroupPlan $academicGroupPlan, AcademicGroupPlan $callBack, LecturerPlan $lecturerPlan)
    {

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



        $persist = $lecturerPlan->removeAcademicGroupPlan($academicGroupPlan);

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


        return $this->redirectToRoute('lecturerplan_edit', ['a_id' => $callBack->getId(), 'id' => $lecturerPlan->getId()]);
    }

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

    private function getLecturerData()
    {
        $lecturersData = $this->em->getRepository(Lecturer::class)->findAll();

        $lecturerDepPos = [];
        foreach ($lecturersData as $lecturerData) {
            $positions = [];
            foreach ( $lecturerData->getLecturerAgreements() as $lecturerAgreement) {
                $positions[$lecturerAgreement->getLecturerPosition()->getId()] = $lecturerAgreement->getLecturerPosition()->getName();
            }

            $lecturerDepPos[$lecturerData->getId()]['positions'] = $positions;
            $lecturerDepPos[$lecturerData->getId()]['isNotFullTime'] = $lecturerData->getIsNotFullTime();
        }

        return $lecturerDepPos;
    }
}