<?php
declare(strict_types=1);
namespace App\Controller\Administration;
use Symfony\Component\HttpFoundation\{
Request,
Response
};
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use App\Common\Controllers\CrmCrudAbstractController;
use App\Form\Type\CspTypeType;
use App\Entity\{Csptype, Csp};
use App\Services\Administration\CspTypeService;
use Doctrine\DBAL\DBALException;
class CspTypeController extends CrmCrudAbstractController
{
private const TEMPLATE_FOLDER = 'administration/csp_type/';
/**
* @see CrmAbstractController
*/
protected function initModuleDetails(): array
{
return [
'entityClassName' => Csptype::class,
'formClassName' => CspTypeType::class,
'moduleService' => CspTypeService::class,
'modulePrefix' => 'administration_csptype',
'moduleTemplate' => self::TEMPLATE_FOLDER,
'hasPagin' => false,
'defaultSort' => ['field' => 'ct.name', 'op' => 'ASC'],
'hasFilter' => false,
];
}
#[IsGranted("ROLE_ADMIN")]
#[Route('/administration/csp-type', name: 'administration_csptype_management_index', options: ['expose' => true])]
public function index(Request $request, CspTypeService $cspTypeService): Response
{
return parent::getList($request, parent::MODE_INDEX, $cspTypeService);
}
#[IsGranted("ROLE_ADMIN")]
#[Route('/administration/csp-type/add', name: 'administration_csptype_management_add', options: ['expose' => true])]
#[Route('/administration/csp-type/edit/{id}', name: 'administration_csptype_management_edit', options: ['expose' => true])]
public function add(Request $request, CspTypeService $cspTypeService, ?Csptype $csptype): Response
{
try {
$response = parent::addEntity(
$request,
$csptype
);
if ($request->isMethod('POST')) {
$this->addFlash('success', 'Done successfully !');
return $this->redirectToRoute('administration_csptype_management_index');
}
} catch (\Exception $exception) {
$this->addFlash('error', $exception->getMessage());
if ($request->isMethod('POST')) {
$route = ($csptype)
? $this->generateUrl('administration_csptype_management_edit', ['id' => $csptype->getId(),])
: $this->generateUrl('administration_csptype_management_add')
;
return $this->redirect($route);
}
return $this->redirectToRoute('administration_csptype_management_index');
}
return $response;
}
#[IsGranted("ROLE_ADMIN")]
#[Route('/administration/csp-type/delete/{id}', name: 'administration_csptype_management_delete', options: ['expose' => true])]
public function delete(CspTypeService $cspTypeService, Csptype $csptype): Response
{
try {
$cspTypeService->removeCspTypeFromDatasetCriteria($csptype);
parent::deleteEntity($csptype);
$this->addFlash('success', 'Done successfully !');
} catch (DBALException $exception) {
$this->addFlash('error', $exception->getMessage());
}
return $this->redirectToRoute('administration_csptype_management_index');
}
#[Route('/administration/csp/{id}/csptpes/ddl', name: 'administration_csp_csptypes_ddl', options: ['expose' => true])]
public function cspCountriesDdl(Csp $csp): Response
{
return $this->render(
'administration/csp/csp_csptypes_ddl.html.twig', [
'csptypes' => $csp->getCsptype()->toArray(),
]
);
}
}