src/Controller/Pim/ReferentialController.php line 199

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Pim;
  3. use App\Enum\AttributeEnum;
  4. use App\Enum\PrivilegeEnum;
  5. use App\Services\Admin\UserService;
  6. use App\Services\Main\ApiService;
  7. use App\Services\Main\HistoricalService;
  8. use App\Services\Pim\AttributeService;
  9. use App\Services\Pim\FamilyService;
  10. use App\Services\Pim\ReferentialService;
  11. use App\Services\Util\JsonService;
  12. use App\Services\Util\UtilService;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. /**
  19.  * Class ReferentialController.
  20.  *
  21.  * @Route("/referential", name="parameters.pim.referential.")
  22.  */
  23. class ReferentialController extends AbstractController
  24. {
  25.     /**
  26.      * @var ReferentialService
  27.      */
  28.     private ReferentialService $referentialService;
  29.     /**
  30.      * @var UserService
  31.      */
  32.     private UserService $userService;
  33.     /**
  34.      * @var ApiService
  35.      */
  36.     private ApiService $apiService;
  37.     /**
  38.      * @var UtilService
  39.      */
  40.     private UtilService $utilService;
  41.     public function __construct(
  42.         ReferentialService $referentialService,
  43.         UserService $userService,
  44.         ApiService $apiService,
  45.         UtilService $utilService
  46.     ) {
  47.         $this->referentialService $referentialService;
  48.         $this->userService $userService;
  49.         $this->apiService $apiService;
  50.         $this->utilService $utilService;
  51.     }
  52.     /**
  53.      * @Route("/list", name="list")
  54.      *
  55.      * @return Response
  56.      */
  57.     public function index(): Response
  58.     {
  59.         if (!$this->userService->hasModule('pim') || !$this->userService->hasPrivilege(PrivilegeEnum::REFERENTIALS_LIST)) {
  60.             $this->utilService->throwCustomException('global.errors.exceptions.pageNotFound');
  61.         }
  62.         return $this->render('pim/referential/list.html.twig', [
  63.             'list' => $this->referentialService->getList(),
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route(
  68.      *     "/delete",
  69.      *     name="delete",
  70.      *     methods={"POST"}
  71.      * )
  72.      *
  73.      * @param Request $request
  74.      *
  75.      * @return mixed
  76.      */
  77.     public function delete(Request $request): mixed
  78.     {
  79.         return $this->json(
  80.             $this->referentialService->delete($request->request->get('referentials-list'))
  81.         );
  82.     }
  83.     /**
  84.      * @Route(
  85.      *     "/edit/{id}",
  86.      *     defaults={"id" = null},
  87.      *     name="edit",
  88.      *     methods={"GET"}
  89.      * )
  90.      */
  91.     public function edit(
  92.         ?string $id,
  93.         Request $request,
  94.         HistoricalService $historicalService,
  95.         AttributeService $attributeService,
  96.         FamilyService $familyService
  97.     ): Response|RedirectResponse {
  98.         if (!$this->userService->hasModule('pim')) {
  99.             $this->utilService->throwCustomException('global.errors.exceptions.pageNotFound');
  100.         }
  101.         $referential $this->apiService->get("pim/referential/id/{$id}", ['defaultResult' => []])->getContent();
  102.         if (null !== $id && empty($referential)) {
  103.             return $this->redirectToRoute('parameters.pim.referential.list');
  104.         }
  105.         $listAttributes $attributeService->getList();
  106.         return $this->render('pim/referential/edit/edit.html.twig', [
  107.             'listAttributes' => $attributeService->serializeListByCode($listAttributes),
  108.             'listFamilies' => $familyService->getFlatList(false),
  109.             'listAttributesNames' => $attributeService->getListAttributesNames($listAttributes$request->getLocale()),
  110.             'listAttributesTypeChoices' => $attributeService->serializeListAttributesChoices(
  111.                 $attributeService->filterListByTypes(
  112.                     $listAttributes,
  113.                     [
  114.                         AttributeEnum::TYPE_SELECT,
  115.                         AttributeEnum::TYPE_MULTIPLE,
  116.                     ]
  117.                 )
  118.             ),
  119.             'referential' => $referential,
  120.             'historicals' => (null !== $id)
  121.                 ? $historicalService->getHistorical([
  122.                     'object' => 'referential',
  123.                     'code' => $referential['code'],
  124.                 ])['historicals']
  125.                 : [],
  126.         ]);
  127.     }
  128.     /**
  129.      * @Route(
  130.      *     "/save",
  131.      *     name="save",
  132.      *     methods={"POST"}
  133.      * )
  134.      *
  135.      * @param Request $request
  136.      * @param JsonService $jsonService
  137.      *
  138.      * @return Response
  139.      */
  140.     public function save(Request $requestJsonService $jsonService): Response
  141.     {
  142.         if (!$this->userService->hasPrivilege(PrivilegeEnum::REFERENTIALS_EDIT)) {
  143.             $this->utilService->throwCustomException('global.errors.exceptions.pageNotFound');
  144.         }
  145.         return $this->json(
  146.             $this->referentialService->save(
  147.                 $jsonService->decode($request->request->get('referential')),
  148.                 $request->request->get('editType')
  149.             )
  150.         );
  151.     }
  152.     /**
  153.      * @Route(
  154.      *     "/import",
  155.      *     name="import",
  156.      *     methods={"POST"}
  157.      * )
  158.      *
  159.      * @param Request $request
  160.      *
  161.      * @return Response
  162.      */
  163.     public function import(Request $request): Response
  164.     {
  165.         return $this->json(
  166.             $this->referentialService->import($request->files->get('import-file'))
  167.         );
  168.     }
  169.     /**
  170.      * @Route(
  171.      *     "/export/{extension}",
  172.      *     name="export",
  173.      *     methods={"GET"}
  174.      * )
  175.      *
  176.      * @param string $extension
  177.      *
  178.      * @return Response
  179.      */
  180.     public function export(string $extension): Response
  181.     {
  182.         return $this->referentialService->export($extension);
  183.     }
  184. }