File: /var/www/dvpis2026/dvpis.kaunokolegija.lt/src/Controller/StudentCountProgramPlanController.php
<?php
namespace App\Controller;
use App\Entity\StudiesProgram;
use DateTime;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityManagerInterface;
use PHPExcel_Shared_Date;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Entity\StudentCountProgramPlan;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Service\FileUploader;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Studentcountprogramplan controller.
*
* @Security("is_granted('ROLE_STUDY_DEPARTMENT')")
*/
#[Route(path: 'studentcount')]
class StudentCountProgramPlanController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em,
private readonly ValidatorInterface $validator,
) {
}
/**
* Lists all studentCountProgramPlan entities.
*
* @Method("GET")
*/
#[Route(path: '/', name: 'studentcount_index')]
public function indexAction()
{
$studentCountProgramPlans = $this->em->getRepository(StudentCountProgramPlan::class)->findAll();
return $this->render('studentcountprogramplan/index.html.twig', array(
'studentCountProgramPlans' => $studentCountProgramPlans,
));
}
/**
* Creates a new studentCountProgramPlan entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/new', name: 'studentcount_new')]
public function newAction(Request $request)
{
$studentCountProgramPlan = new Studentcountprogramplan();
$form = $this->createForm('App\Form\StudentCountProgramPlanType', $studentCountProgramPlan);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($studentCountProgramPlan);
$this->em->flush();
return $this->redirectToRoute('studentcount_index', array('id' => $studentCountProgramPlan->getId()));
}
return $this->render('studentcountprogramplan/new.html.twig', array(
'studentCountProgramPlan' => $studentCountProgramPlan,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing studentCountProgramPlan entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/{id}/edit', name: 'studentcount_edit')]
public function editAction(Request $request, StudentCountProgramPlan $studentCountProgramPlan)
{
$deleteForm = $this->createDeleteForm($studentCountProgramPlan);
$editForm = $this->createForm('App\Form\StudentCountProgramPlanType', $studentCountProgramPlan);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->em->flush();
return $this->redirectToRoute('studentcount_edit', array('id' => $studentCountProgramPlan->getId()));
}
return $this->render('studentcountprogramplan/edit.html.twig', array(
'studentCountProgramPlan' => $studentCountProgramPlan,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a studentCountProgramPlan entity.
*
* @Method("DELETE")
*/
#[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'studentcount_delete')]
public function deleteAction(Request $request, StudentCountProgramPlan $studentCountProgramPlan)
{
$form = $this->createDeleteForm($studentCountProgramPlan);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->remove($studentCountProgramPlan);
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('studentcount_index');
}
/**
* Creates a form to delete a studentCountProgramPlan entity.
*
* @param StudentCountProgramPlan $studentCountProgramPlan The studentCountProgramPlan entity
*
* @return Form The form
*/
private function createDeleteForm(StudentCountProgramPlan $studentCountProgramPlan)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('studentcount_delete', array('id' => $studentCountProgramPlan->getId())))
->setMethod('DELETE')
->getForm()
;
}
/**
* imports studies programs from excel
*
* @Method({"GET","POST"})
*/
#[Route(path: '/import_view', name: 'studentcount_import_view')]
public function showImportAcademicGroupsAction(Request $request, FileUploader $fileUploader)
{
$file = $request->files->get('fileToUpload');
$uploadedFilePath = '';
$fileImportReturn = [];
if (!empty($file)) {
$uploadedFilePath = $fileUploader->getTargetDir() . DIRECTORY_SEPARATOR . $fileUploader->upload($file);
$fileImportReturn = $this->importData($uploadedFilePath);
unlink($uploadedFilePath);
}
return $this->render('studentcountprogramplan/import.html.twig', [
'uploadedFilePath' => $uploadedFilePath,
'importErrors' => $fileImportReturn,
]);
}
private function importData($filePath)
{
$studiesProgram = $this->em->getRepository(StudiesProgram::class)->getAllByShortName();
$studentCountProgramPlans = $this->em->getRepository(StudentCountProgramPlan::class)->getAllByHash();
$objExcel = IOFactory::load($filePath);
$worksheet = $objExcel->setActiveSheetIndex(0);
$errors = [];
foreach ($worksheet->getRowIterator() as $row) {
$rowNumber = $row->getRowIndex();
//insert values
if ($row->getRowIndex() > 1) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
$excelStudiesProgram = $worksheet->getCell('A' . $row->getRowIndex())->getValue();
$excelCourse = $worksheet->getCell('B' . $row->getRowIndex())->getValue();
$excelIndex = md5($excelStudiesProgram . "_" . $excelCourse);
$studentCountProgramPlan = $studentCountProgramPlans[$excelIndex] ?? new StudentCountProgramPlan();
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
$column = $cell->getColumn();
switch ($cell->getColumn()) {
case 'A':
$excelCode = trim($cell->getCalculatedValue());
if (!isset($studiesProgram[$excelCode])) {
// throw new \Exception($excelCode . ": Studijų Programa nerasta!");
$errors[] = $column . $rowNumber . ": Studijų Programa nerasta! (" . $excelCode . ")";
} else {
$studentCountProgramPlan->setStudiesProgram($studiesProgram[$excelCode]);
}
break;
case 'B':
$studentCountProgramPlan->setCourse($cell->getCalculatedValue());
break;
case 'C':
$studentCountProgramPlan->setCountStudentNl($cell->getCalculatedValue());
break;
case 'D':
$studentCountProgramPlan->setCountStudentI($cell->getCalculatedValue());
break;
case 'E':
$studentCountProgramPlan->setCountStudentPart($cell->getCalculatedValue());
break;
case 'F':
$studentCountProgramPlan->setCountListener($cell->getCalculatedValue());
break;
case 'G':
$excelDataDate = $cell->getCalculatedValue();
if (empty($excelDataDate)) {
// throw new \Exception($studentCountProgramPlan->getShortName(). ': Kodas negali būti tuščias');
$excelStateCode = new DateTime();
} else {
$excelDataDate = Date::excelToDateTimeObject($excelDataDate);
}
$studentCountProgramPlan->setDataDate($excelDataDate);
break;
default:
break;
}
}
}
$valid = $this->validator->validate($studentCountProgramPlan);
if (empty($errors)) {
if (empty($valid[0])) {
$this->em->persist($studentCountProgramPlan);
} else {
$errors[] = $column . $rowNumber . ": " . $valid[0]->getMessage();
}
}
}
}
if (empty($errors)) {
try {
$this->em->flush();
} catch (UniqueConstraintViolationException $ex) {
// $this->addFlash("warning", "Faile yra vienodų naujų įrašų. Prašome patikrinti duomenis! Detaliau: ".$ex->getPrevious()->getMessage());
$errors[] = "Faile yra vienodų naujų įrašų. Prašome patikrinti duomenis! Detaliau: " . $ex->getPrevious()->getMessage();
}
}
return $errors;
}
}