File: /var/www/dvpis2025/dvpis.kaunokolegija.lt/src/Controller/MeovLecturerController.php
<?php
namespace App\Controller;
use Doctrine\DBAL\Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Entity\MeovLecturer;
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: 'meovlecturer')]
class MeovLecturerController 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 meovLecturer entities.
*
* @Method("GET")
*/
#[Route(path: '/', name: 'meovlecturer_index')]
public function indexAction()
{
$meovLecturers = $this->em->getRepository(MeovLecturer::class)->findAll();
return $this->render('meovlecturer/index.html.twig', [
'meovLecturers' => $meovLecturers,
]);
}
/**
* Creates a new meovLecturer entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/new', name: 'meovlecturer_new')]
public function newAction(Request $request)
{
if (!$this->dateValidator->isActiveFact()) {
throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
}
if ($this->dbDataFilter->getUserInfo()->getRole() === "ROLE_DEPARTMENT_ADMINISTRATOR") {
$this->dbDataFilter->enableOnlyLecturerFilter();
}
$meovLecturer = new Meovlecturer();
$form = $this->createForm(
'App\Form\MeovLecturerNewType',
$meovLecturer,
['isEnabledDepartmentHeadComment' => $this->isGranted('ROLE_DEPARTMENT_HEAD'),]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($meovLecturer);
$this->em->flush();
return $this->redirectToRoute('meovlecturer_index', ['id' => $meovLecturer->getId()]);
}
return $this->render('meovlecturer/new.html.twig', [
'meovLecturer' => $meovLecturer,
'form' => $form->createView(),
]);
}
/**
* Displays a form to edit an existing meovLecturer entity.
*
* @Method({"GET", "POST"})
*/
#[Route(path: '/{id}/edit', name: 'meovlecturer_edit')]
public function editAction(Request $request, MeovLecturer $meovLecturer)
{
if (!$this->dateValidator->isActiveFact()) {
throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
}
if (empty($meovLecturer->getDescriptionFact())) {
$meovLecturer->setDescriptionFact($meovLecturer->getDescription());
}
$deleteForm = $this->createDeleteForm($meovLecturer);
$editForm = $this->createForm(
'App\Form\MeovLecturerEditType',
$meovLecturer,
['isEnabledDepartmentHeadComment' => $this->isGranted('ROLE_DEPARTMENT_HEAD'),]
);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->em->flush();
return $this->redirectToRoute('meovlecturer_edit', ['id' => $meovLecturer->getId()]);
}
return $this->render('meovlecturer/edit.html.twig', [
'meovLecturer' => $meovLecturer,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
]);
}
/**
* Deletes a meovLecturer entity.
*
* @Method("DELETE")
*/
#[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'meovlecturer_delete')]
public function deleteAction(Request $request, MeovLecturer $meovLecturer)
{
if (!$this->dateValidator->isActiveFact()) {
throw new AccessDeniedHttpException("Jūs negalite pasiekti šio turinio");
}
$form = $this->createDeleteForm($meovLecturer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->remove($meovLecturer);
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('meovlecturer_index');
}
/**
* Creates a form to delete a meovLecturer entity.
*
* @param MeovLecturer $meovLecturer The meovLecturer entity
*
* @return Form The form
*/
private function createDeleteForm(MeovLecturer $meovLecturer)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('meovlecturer_delete', ['id' => $meovLecturer->getId()]))
->setMethod('DELETE')
->getForm();
}
}