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

namespace App\Controller;

use App\Entity\AcademicUnit;
use App\Entity\Department;
use App\Entity\StudiesModule;
use App\Entity\StudiesProgram;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Entity\AcademicGroupPlan;
use App\Entity\StudiesProgramPlan;
use App\Entity\LecturerPlan;
use App\Entity\AcademicGroup;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
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 App\Entity\YearInformation;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\DbDataFilter;

/**
* Academicgroupplan controller.
*
* @Security("
   is_granted('ROLE_DEPARTMENT_HEAD')
   or is_granted('ROLE_DEPARTMENT_ADMINISTRATOR')
   or is_granted('ROLE_ACADEMIC_UNIT_PROHEAD')
   or is_granted('ROLE_LECTURER')
   ")
*/
#[Route(path: 'academicgroupplan')]
class AcademicGroupPlanController 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->isActivePlan();

        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 academicGroupPlan entities.
    * @Security("
       is_granted('ROLE_DEPARTMENT_ADMINISTRATOR')
       or is_granted('ROLE_DEPARTMENT_HEAD')
       or is_granted('ROLE_ACADEMIC_UNIT_PROHEAD')
        ")
    * @Method("GET")
    */
    #[Route(path: '/', name: 'academicgroupplan_index')]
    public function indexAction(Request $request)
    {

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

        $filter = [];

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

        $academicGroupPlans = $this->em->getRepository(AcademicGroupPlan::class)->getAllPlansByIdQueryBuilder($filter);
        $academicGroupLecturerPlans = $this->em->getRepository(AcademicGroupPlan::class)->getPlansList();

        return $this->render('academicgroupplan/index.html.twig', array(
                    'academicUnits' => $academicUnits,
                    'departments' => $departments,
                    'studiesPrograms' => $studiesPrograms,
                    'studiesModules' => $studiesModules,
                    'academicGroups' => $academicGroups,
                    'studiesProgramPlans' => $studiesProgramPlans,
                    'academicGroupPlans' => $academicGroupPlans,
                    'academicGroupLecturerPlans' => $academicGroupLecturerPlans,
        ));
    }

    /**
     * Lists all academicGroupPlan entities.
     *
     * @Method("GET")
     */
    #[Route(path: '/generate_plan', name: 'academicgroupplan_generate')]
    public function generatePlanAction()
    {
        if ($this->onlyView) {
            throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
        }

        $academicGroups = $this->em->getRepository(AcademicGroup::class)->getAllAcademicGroupsByName();
        $studiesProgramPlans = $this->em->getRepository(StudiesProgramPlan::class)->getAllByProgramFormCourse();

        $academicGroupPlans = $this->em->getRepository(AcademicGroupPlan::class)->findAll();

        $existingGroupPlans = [];

        foreach ($academicGroupPlans as $academicGroupPlan) {
            $existingGroupPlans[$academicGroupPlan->getAcademicGroup()->getId()][$academicGroupPlan->getStudiesProgramPlan()->getId()] = 1;
        }

        foreach ($academicGroups as $academicGroup) {
            $key = $academicGroup->getStudiesProgram()->getShortName() . '_' . $academicGroup->getStudiesForm() . '_' . $academicGroup->getCourse();
            if (!empty($studiesProgramPlans[$key])) {
                foreach ($studiesProgramPlans[$key] as $studiesProgramPlan) {
                    if (empty($existingGroupPlans[$academicGroup->getId()][$studiesProgramPlan->getId()])) {
                        $academicGroupPlan = new AcademicGroupPlan();
                        $academicGroupPlan->setAcademicGroup($academicGroup);
                        $academicGroupPlan->setStudiesProgramPlan($studiesProgramPlan);

                        $this->em->persist($academicGroupPlan);
                    }
                }
            }
        }

        $this->em->flush();

        return $this->redirect($this->generateUrl('academicgroupplan_index'));
    }

    /**
     * Copy academicGroupPlan lecturerplan to history entities.
     *
     * @Method("GET")
     */
    #[Route(path: '/copy_plan', name: 'academicgroupplan_copy')]
    public function copyPlanAction()
    {
        if ($this->onlyView) {
            throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
        }



        try {
            $query = 'DROP TABLE IF EXISTS academic_group_plan_lecturer_plan_history; ';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->execute();

            $query = 'DROP TABLE IF EXISTS lecturer_plan_history; ';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->execute();

            $query = 'DROP TABLE IF EXISTS final_egzam_history; ';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->execute();

            $query = 'DROP TABLE IF EXISTS final_project_history; ';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->execute();

            $query = 'CREATE TABLE academic_group_plan_lecturer_plan_history LIKE academic_group_plan_lecturer_plan; ';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->execute();

            $query = 'ALTER TABLE `academic_group_plan_lecturer_plan_history` CHANGE `lecturer_plan_id` `lecturer_plan_history_id` INT(11) NOT NULL; ';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->execute();

            $query = 'CREATE TABLE lecturer_plan_history LIKE lecturer_plan;';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->execute();

            $query = 'INSERT INTO lecturer_plan_history SELECT * FROM lecturer_plan;';
            $statement = $this->em->getConnection()->prepare($query);
            $lecturerPlanHistoryRowCount = $statement->executeStatement();

            $query = 'INSERT INTO academic_group_plan_lecturer_plan_history SELECT * FROM academic_group_plan_lecturer_plan;';
            $statement = $this->em->getConnection()->prepare($query);
            $academicGroupPlanLecturerPlanRowCount = $statement->executeStatement();

            $query = 'CREATE TABLE final_egzam_history LIKE final_egzam;';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->executeStatement();

            $query = 'CREATE TABLE final_project_history LIKE final_project;';
            $statement = $this->em->getConnection()->prepare($query);
            $statement->executeStatement();

            $query = 'INSERT INTO final_egzam_history SELECT * FROM final_egzam;';
            $statement = $this->em->getConnection()->prepare($query);
            $finalEgzamRowCount = $statement->executeStatement();

            $query = 'INSERT INTO final_project_history SELECT * FROM final_project;';
            $statement = $this->em->getConnection()->prepare($query);
            $finalProjectRowCount = $statement->executeStatement();

            $query = '   
                ALTER TABLE academic_group_plan_lecturer_plan_history ADD CONSTRAINT FK_3E8002A6E8014C7D FOREIGN KEY (academic_group_plan_id) REFERENCES academic_group_plan (id) ON DELETE CASCADE;
                ALTER TABLE academic_group_plan_lecturer_plan_history ADD CONSTRAINT FK_3E8002A6A4DE99C5 FOREIGN KEY (lecturer_plan_history_id) REFERENCES lecturer_plan_history (id) ON DELETE CASCADE;
                DROP INDEX idx_a09f4a82e8014c7d ON academic_group_plan_lecturer_plan_history;
                CREATE INDEX IDX_3E8002A6E8014C7D ON academic_group_plan_lecturer_plan_history (academic_group_plan_id);
                DROP INDEX idx_a09f4a82835f57ac ON academic_group_plan_lecturer_plan_history;
                CREATE INDEX IDX_3E8002A6A4DE99C5 ON academic_group_plan_lecturer_plan_history (lecturer_plan_history_id);
                ALTER TABLE final_egzam_history ADD CONSTRAINT FK_166E0940BA2D8762 FOREIGN KEY (lecturer_id) REFERENCES lecturer (id);
                ALTER TABLE final_egzam_history ADD CONSTRAINT FK_166E0940AE80F5DF FOREIGN KEY (department_id) REFERENCES department (id);
                ALTER TABLE final_egzam_history ADD CONSTRAINT FK_166E09409DCFCA1D FOREIGN KEY (lecturer_position_id) REFERENCES lecturer_position (id);
                ALTER TABLE final_egzam_history ADD CONSTRAINT FK_166E0940F6EB264D FOREIGN KEY (finance_source_id) REFERENCES finance_source (id);
                ALTER TABLE final_egzam_history ADD CONSTRAINT FK_166E0940E8014C7D FOREIGN KEY (academic_group_plan_id) REFERENCES academic_group_plan (id);
                DROP INDEX idx_269a398fba2d8762 ON final_egzam_history;
                CREATE INDEX IDX_166E0940BA2D8762 ON final_egzam_history (lecturer_id);
                DROP INDEX idx_269a398fae80f5df ON final_egzam_history;
                CREATE INDEX IDX_166E0940AE80F5DF ON final_egzam_history (department_id);
                DROP INDEX idx_269a398f9dcfca1d ON final_egzam_history;
                CREATE INDEX IDX_166E09409DCFCA1D ON final_egzam_history (lecturer_position_id);
                DROP INDEX idx_269a398ff6eb264d ON final_egzam_history;
                CREATE INDEX IDX_166E0940F6EB264D ON final_egzam_history (finance_source_id);
                DROP INDEX idx_269a398fe8014c7d ON final_egzam_history;
                CREATE INDEX IDX_166E0940E8014C7D ON final_egzam_history (academic_group_plan_id);
                ALTER TABLE final_project_history ADD CONSTRAINT FK_32F9A85EBA2D8762 FOREIGN KEY (lecturer_id) REFERENCES lecturer (id);
                ALTER TABLE final_project_history ADD CONSTRAINT FK_32F9A85EAE80F5DF FOREIGN KEY (department_id) REFERENCES department (id);
                ALTER TABLE final_project_history ADD CONSTRAINT FK_32F9A85E9DCFCA1D FOREIGN KEY (lecturer_position_id) REFERENCES lecturer_position (id);
                ALTER TABLE final_project_history ADD CONSTRAINT FK_32F9A85EF6EB264D FOREIGN KEY (finance_source_id) REFERENCES finance_source (id);
                ALTER TABLE final_project_history ADD CONSTRAINT FK_32F9A85EE8014C7D FOREIGN KEY (academic_group_plan_id) REFERENCES academic_group_plan (id);
                DROP INDEX idx_ac905ba2ba2d8762 ON final_project_history;
                CREATE INDEX IDX_32F9A85EBA2D8762 ON final_project_history (lecturer_id);
                DROP INDEX idx_ac905ba2ae80f5df ON final_project_history;
                CREATE INDEX IDX_32F9A85EAE80F5DF ON final_project_history (department_id);
                DROP INDEX idx_ac905ba29dcfca1d ON final_project_history;
                CREATE INDEX IDX_32F9A85E9DCFCA1D ON final_project_history (lecturer_position_id);
                DROP INDEX idx_ac905ba2f6eb264d ON final_project_history;
                CREATE INDEX IDX_32F9A85EF6EB264D ON final_project_history (finance_source_id);
                DROP INDEX idx_ac905ba2e8014c7d ON final_project_history;
                CREATE INDEX IDX_32F9A85EE8014C7D ON final_project_history (academic_group_plan_id);
                ALTER TABLE lecturer_plan_history ADD CONSTRAINT FK_65FCFBDBBA2D8762 FOREIGN KEY (lecturer_id) REFERENCES lecturer (id);
                ALTER TABLE lecturer_plan_history ADD CONSTRAINT FK_65FCFBDBAE80F5DF FOREIGN KEY (department_id) REFERENCES department (id);
                ALTER TABLE lecturer_plan_history ADD CONSTRAINT FK_65FCFBDB9DCFCA1D FOREIGN KEY (lecturer_position_id) REFERENCES lecturer_position (id);
                ALTER TABLE lecturer_plan_history ADD CONSTRAINT FK_65FCFBDBF6EB264D FOREIGN KEY (finance_source_id) REFERENCES finance_source (id);
                DROP INDEX idx_9020e860ba2d8762 ON lecturer_plan_history;
                CREATE INDEX IDX_65FCFBDBBA2D8762 ON lecturer_plan_history (lecturer_id);
                DROP INDEX idx_9020e860ae80f5df ON lecturer_plan_history;
                CREATE INDEX IDX_65FCFBDBAE80F5DF ON lecturer_plan_history (department_id);
                DROP INDEX idx_9020e8609dcfca1d ON lecturer_plan_history;
                CREATE INDEX IDX_65FCFBDB9DCFCA1D ON lecturer_plan_history (lecturer_position_id);
                DROP INDEX idx_9020e860f6eb264d ON lecturer_plan_history;
                CREATE INDEX IDX_65FCFBDBF6EB264D ON lecturer_plan_history (finance_source_id);
            ';

            $statement = $this->em->getConnection()->prepare($query);
            $statement->executeStatement();

            $this->addFlash(
                'info',
                'Kopijos išsaugojimas PAVYKO!'
                . ' (Įrašų skaičiai kopijose: '
                . ' Final Egzam: ' . $finalEgzamRowCount
                . ' Final Project: ' . $finalProjectRowCount
                . ' AcademicGroupPlanLecturerPlan: ' . $academicGroupPlanLecturerPlanRowCount
                . ' lecturerPlan: ' . $lecturerPlanHistoryRowCount
                . ')'
            );

            if (
                $finalEgzamRowCount === 0
                || $finalProjectRowCount === 0
                || $academicGroupPlanLecturerPlanRowCount === 0
                || $lecturerPlanHistoryRowCount === 0
            ) {
                $this->addFlash('danger', 'Dėmesio, vienoje iš kopijų nėra įrašų! Patikrinkite, jei jų turi būti pabandykite Išsaugoti planą dar kartą!');
            }
        } catch (Exception $exception) {
            $this->addFlash('danger', 'Kopijos išsaugojimas NEPAVYKO! Bandykite dar kartą! Klaida:' . $exception->getMessage());
        }

        return $this->redirect($this->generateUrl('academicgroupplan_index'));
    }

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

        $academicGroupPlan = new Academicgroupplan();
        $form = $this->createForm('App\Form\AcademicGroupPlanType', $academicGroupPlan);
        $form->handleRequest($request);

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

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

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

    /**
     * Add stream academicGroupPlan entity.
     *
     * @Method({"POST"})
     */
    #[Route(path: '/addStream', name: 'academicgroupplan_new_stream')]
    public function newStreamAction(Request $request)
    {
        if ($this->onlyView) {
            throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
        }

        $existingAgpId = $request->request->get('agp_id');
        $addAgpId = $request->request->get('academic_group_plan_id');
        $addLecturerId = $request->request->get('lecturer_id');




        $lecturerPlan = $this->em->getRepository(LecturerPlan::class)->find($addLecturerId);
        $academicGroupPlan = $this->em->getRepository(AcademicGroupPlan::class)->find($addAgpId);

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

        return $this->redirect($this->generateUrl('lecturerplan_edit', array('id' => $addLecturerId, 'a_id' => $existingAgpId)));
    }

    /**
     * Finds and displays a academicGroupPlan entity.
     *
     * @Method("GET")
     */
    #[Route(path: '/show/{id}', name: 'academicgroupplan_show')]
    public function showAction(AcademicGroupPlan $academicGroupPlan)
    {



//        $academicGroupPlans = $this->em->getRepository(AcademicGroupPlan::class)->getAllPlansById();
//        $academicGroupPlans = $this->em->getRepository(AcademicGroupPlan::class)->getOtherGroupsPlansById($academicGroupPlan->getId());
        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($academicGroupPlan);
        $yearInformation = $this->em->getRepository(YearInformation::class)->findActual();

        return $this->render(
            'academicgroupplan/show.html.twig',
            [
                'academicGroupPlan' => $academicGroupPlan,
                'yearInformation' => $yearInformation,
                'isOnlyView' => $this->onlyView,
                'delete_form' => $deleteForm->createView(),
            //               'isOnlyView' => false,
            //               'academicGroupPlans' => $academicGroupPlans,
            ]
        );
    }

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

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

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

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

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

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

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

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->remove($academicGroupPlan);
            try {
                $this->em->flush();
            } catch (\Doctrine\DBAL\Exception $ex) {
                $this->addFlash('warning', "Ištrinti įrašo nepavyko! Jis gali turėti susijusių įrašų." . $ex->getMessage());
            }
        }

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

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