src/Controller/Pim/CategoryController.php line 228

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Pim;
  3. use App\Enum\HistoricalEnum;
  4. use App\Services\Pim\CategoryService;
  5. use App\Services\Util\JsonService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. /**
  11.  * Class CategoryController.
  12.  *
  13.  * @Route("/category", name="parameters.pim.category.")
  14.  */
  15. class CategoryController extends AbstractController
  16. {
  17.     /**
  18.      * @var CategoryService
  19.      */
  20.     private CategoryService $categoryService;
  21.     public function __construct(CategoryService $categoryService)
  22.     {
  23.         $this->categoryService $categoryService;
  24.     }
  25.     /**
  26.      * @Route(
  27.      *     "/get-children",
  28.      *     name="getChildren"
  29.      * )
  30.      *
  31.      * @param Request $request
  32.      *
  33.      * @return Response
  34.      */
  35.     public function getChildren(Request $request): Response
  36.     {
  37.         $listParameters json_decode($request->get('parameters'), true);
  38.         $serializeOptions = [
  39.             'countProducts' => (isset($listParameters['countProducts'])
  40.                 && $listParameters['countProducts']),
  41.             'includeChildrenCategories' => (isset($listParameters['includeChildrenCategories'])
  42.                 && $listParameters['includeChildrenCategories']),
  43.             'listCheckedNodes' => $request->get('listCheckedNodes') ?? null,
  44.             'listUncheckedNodes' => $request->get('listUncheckedNodes') ?? null,
  45.             'listOpenedNodes' => $request->get('listOpenedNodes') ?? null,
  46.             'hideEmptyCategories' => (isset($listParameters['hideEmptyCategories'])
  47.                 && $listParameters['hideEmptyCategories']),
  48.             'getProductsList' => (isset($listParameters['getProductsList'])
  49.                 && $listParameters['getProductsList']),
  50.             'listFilters' => $listParameters,
  51.             'recursive' => (isset($listParameters['recursive'])
  52.                 && $listParameters['recursive']),
  53.             'openCategories' => $listParameters['openCategories'] ?? true,
  54.             'prefix' => $listParameters['prefix'] ?? '',
  55.             'defaultNode' => $listParameters['defaultNode'] ?? null,
  56.             'showDetails' => $listParameters['showDetails'] ?? false,
  57.         ];
  58.         if ($serializeOptions['countProducts'] || $serializeOptions['hideEmptyCategories']) {
  59.             $serializeOptions['listFilters']['listAttributeFilters'] = $listParameters['listAttributes'] ?? [];
  60.         }
  61.         $id = ('null' === $request->request->get('id'))
  62.             ? null
  63.             $request->request->get('id');
  64.         if (!empty($serializeOptions['prefix'])) {
  65.             $id str_replace($serializeOptions['prefix'], ''$id);
  66.         }
  67.         return $this->json(
  68.             $this->categoryService->getChildren(
  69.                 $id,
  70.                 $request->getLocale(),
  71.                 $serializeOptions
  72.             )
  73.         );
  74.     }
  75.     /**
  76.      * @Route(
  77.      *     "/save",
  78.      *     name="save",
  79.      *     methods={"POST"}
  80.      * )
  81.      *
  82.      * @param Request $request
  83.      * @param JsonService $jsonService
  84.      *
  85.      * @return Response
  86.      */
  87.     public function save(Request $requestJsonService $jsonService): Response
  88.     {
  89.         return $this->json(
  90.             $this->categoryService->save(
  91.                 $jsonService->decode($request->request->get('category')),
  92.                 $request->request->get('editType') ?? HistoricalEnum::TYPE_EDIT
  93.             )
  94.         );
  95.     }
  96.     /**
  97.      * @Route(
  98.      *     "/delete",
  99.      *     name="delete",
  100.      *     methods={"POST"}
  101.      * )
  102.      *
  103.      * @param Request $request
  104.      *
  105.      * @return Response
  106.      */
  107.     public function delete(Request $request): Response
  108.     {
  109.         return $this->json(
  110.             $this->categoryService->delete($request->request->get('id'))
  111.         );
  112.     }
  113.     /**
  114.      * @Route(
  115.      *     "/clear-products",
  116.      *     name="clearProducts",
  117.      *     methods={"POST"},
  118.      *     options={"expose" = true}
  119.      * )
  120.      *
  121.      * @param Request $request
  122.      *
  123.      * @return Response
  124.      */
  125.     public function clearProducts(Request $request): Response
  126.     {
  127.         return $this->json(
  128.             $this->categoryService->clearProducts(
  129.                 $request->request->get('id'),
  130.                 'true' === $request->request->get('recursive')
  131.             )
  132.         );
  133.     }
  134.     /**
  135.      * @Route(
  136.      *     "/save-list",
  137.      *     name="saveList",
  138.      *     methods={"POST"},
  139.      *     options={"expose" = true}
  140.      * )
  141.      *
  142.      * @param Request $request
  143.      *
  144.      * @return Response
  145.      */
  146.     public function saveList(Request $request): Response
  147.     {
  148.         return $this->json(
  149.             $this->categoryService->saveList($request->request->get('list'))
  150.         );
  151.     }
  152.     /**
  153.      * @Route(
  154.      *     "/import",
  155.      *     name="import",
  156.      *     methods={"POST"},
  157.      *     options={"expose" = true}
  158.      * )
  159.      */
  160.     public function import(Request $request): Response
  161.     {
  162.         $channelCategoryId $request->request->get('channelCategoryId');
  163.         return $this->json(
  164.             $this->categoryService->import($request->files->get('import-file-categories'), $channelCategoryId)
  165.         );
  166.     }
  167.     /**
  168.      * @Route(
  169.      *     "/export/{idParent}/{extension}",
  170.      *     name="export",
  171.      *     methods={"GET"}
  172.      * )
  173.      */
  174.     public function export(string $extensionstring $idParent): Response
  175.     {
  176.         return $this->categoryService->export($extension$idParent);
  177.     }
  178.     /**
  179.      * @Route(
  180.      *     "/breadcrumbs-products",
  181.      *     name="getListBreadcrumbsByListProducts",
  182.      *     methods={"POST"}
  183.      * )
  184.      *
  185.      * @param Request $request
  186.      * @param JsonService $jsonService
  187.      *
  188.      * @return Response
  189.      */
  190.     public function getListBreadcrumbsByListProducts(Request $requestJsonService $jsonService): Response
  191.     {
  192.         return $this->json(
  193.             $this->categoryService->getListBreadcrumbsByListProducts(
  194.                 $request->request->get('channelId'),
  195.                 $jsonService->decode($request->request->get('listProductsUniqId'))
  196.             )
  197.         );
  198.     }
  199.     /**
  200.      * @Route(
  201.      *     "/get-list-breadcrumb",
  202.      *     name="getBreadcrumbByListCategories",
  203.      *     methods={"GET"},
  204.      *     options={"expose" = true}
  205.      * )
  206.      *
  207.      * @param Request $request
  208.      *
  209.      * @return Response
  210.      */
  211.     public function getListBreadcrumbs(Request $request): Response
  212.     {
  213.         $listCategories $request->query->get('listCategories');
  214.         $languageCode $request->getLocale();
  215.         return $this->json(
  216.             $this->categoryService->getListBreadcrumbs($listCategories$languageCode)
  217.         );
  218.     }
  219. }