src/Controller/Administration/CspController.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Administration;
  4. use Symfony\Component\HttpFoundation\{
  5.     Request,
  6.     Response
  7. };
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use App\Common\Controllers\CrmCrudAbstractController;
  12. use App\Form\Type\CspType;
  13. use App\Entity\Csp;
  14. use App\Services\Administration\CspService;
  15. class CspController extends CrmCrudAbstractController
  16. {
  17.     private const TEMPLATE_FOLDER 'administration/csp/';
  18.     /**
  19.      * @see CrmAbstractController
  20.      */
  21.     protected function initModuleDetails(): array
  22.     {
  23.         return [
  24.             'entityClassName' => Csp::class,
  25.             'formClassName' => CspType::class,
  26.             'moduleService' => CspService::class,
  27.             'modulePrefix' => 'administration_csp',
  28.             'moduleTemplate' => self::TEMPLATE_FOLDER,
  29.             'hasPagin' => true,
  30.             'defaultSort' => ['field' => 'c.name''op' => 'ASC'],
  31.             'useIndexMode' => true,
  32.         ];
  33.     }
  34.     #[Route('/administration/csp'name'administration_csp_management_index'options: ['expose' => true])]
  35.     public function index(Request $requestCspService $cspTypeService): Response
  36.     {
  37.         return parent::getList($requestparent::MODE_INDEX$cspTypeService);
  38.     }
  39.     #[Route('/administration/csp/listing'name'administration_csp_management_listing'options: ['expose' => true])]
  40.     public function listing(Request $requestCspService $cspTypeService): Response
  41.     {
  42.         return parent::getList($requestparent::MODE_LISTING$cspTypeServicetrue);
  43.     }
  44.     #[Security("is_granted('ROLE_PM') or is_granted('ROLE_EDM') or is_granted('ROLE_EPA')")]
  45.     #[Route('/administration/csp/add'name'administration_csp_management_add'options: ['expose' => true])]
  46.     #[Route('/administration/csp/edit/{id}'name'administration_csp_management_edit'options: ['expose' => true])]
  47.     public function add(Request $requestCspService $cspService, ?Csp $csp): Response
  48.     {
  49.         try {
  50.             $response parent::addEntity(
  51.                 $request,
  52.                 $csp,
  53.                 ['csp' => $csp,],
  54.                 [],
  55.                 false
  56.             );
  57.             if ($request->isMethod('POST')) {
  58.                 $cspService->saveCsp(
  59.                     $response['entity'],
  60.                     $_POST['csp']['selectedCsptype'] ?? [],
  61.                     $_POST['csp']['selectedCompanytype'] ?? [],
  62.                     $_POST['csp']['selectedCountry'] ?? []
  63.                 );
  64.                 $this->addFlash('success''Done successfully !');
  65.                 return $this->redirectToRoute('administration_csp_management_index');
  66.             }
  67.         } catch (\Exception $exception) {
  68.             $this->addFlash('error'$exception->getMessage());
  69.             
  70.             if ($request->isMethod('POST')) {
  71.                 $route = ($csp)
  72.                     ? $this->generateUrl('administration_csp_management_edit', ['id' => $csp->getId(),])
  73.                     : $this->generateUrl('administration_csp_management_add')
  74.                 ;
  75.                 return $this->redirect($route);
  76.             }
  77.             return $this->redirectToRoute('administration_csp_management_index');
  78.         }
  79.         return $response;
  80.     }
  81.     #[IsGranted("ROLE_PM")]
  82.     #[Route('/administration/csp/delete/{id}'name'administration_csp_management_delete'options: ['expose' => true])]
  83.     public function delete(CspService $cspServiceCsp $csp): Response
  84.     {
  85.         try {
  86.             $cspService->moveToTrash($csp);
  87.             $this->addFlash('success''Done successfully !');
  88.         } catch (\Exception $exception) {
  89.             $this->addFlash('error'$exception->getMessage());
  90.         }
  91.         return $this->redirectToRoute('administration_csp_management_index');
  92.     }
  93.     #[Security("is_granted('ROLE_PM') or is_granted('ROLE_EDM') or is_granted('ROLE_EPA')")]
  94.     #[Route(
  95.         '/administration/csp/csv-export',
  96.         name'administration_csp_management_csv_export',
  97.         options: ['expose' => true])
  98.     ]
  99.     public function csvExport(Request $requestCspService $cspService): Response
  100.     {
  101.         return parent::csvExportResponse(
  102.             $request,
  103.             $cspService,
  104.             $cspService->getCsvExportFilename()
  105.         );
  106.     }
  107. }