<?php
namespace App\Controller\Pim;
use App\Enum\HistoricalEnum;
use App\Services\Pim\CategoryService;
use App\Services\Util\JsonService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class CategoryController.
*
* @Route("/category", name="parameters.pim.category.")
*/
class CategoryController extends AbstractController
{
/**
* @var CategoryService
*/
private CategoryService $categoryService;
public function __construct(CategoryService $categoryService)
{
$this->categoryService = $categoryService;
}
/**
* @Route(
* "/get-children",
* name="getChildren"
* )
*
* @param Request $request
*
* @return Response
*/
public function getChildren(Request $request): Response
{
$listParameters = json_decode($request->get('parameters'), true);
$serializeOptions = [
'countProducts' => (isset($listParameters['countProducts'])
&& $listParameters['countProducts']),
'includeChildrenCategories' => (isset($listParameters['includeChildrenCategories'])
&& $listParameters['includeChildrenCategories']),
'listCheckedNodes' => $request->get('listCheckedNodes') ?? null,
'listUncheckedNodes' => $request->get('listUncheckedNodes') ?? null,
'listOpenedNodes' => $request->get('listOpenedNodes') ?? null,
'hideEmptyCategories' => (isset($listParameters['hideEmptyCategories'])
&& $listParameters['hideEmptyCategories']),
'getProductsList' => (isset($listParameters['getProductsList'])
&& $listParameters['getProductsList']),
'listFilters' => $listParameters,
'recursive' => (isset($listParameters['recursive'])
&& $listParameters['recursive']),
'openCategories' => $listParameters['openCategories'] ?? true,
'prefix' => $listParameters['prefix'] ?? '',
'defaultNode' => $listParameters['defaultNode'] ?? null,
'showDetails' => $listParameters['showDetails'] ?? false,
];
if ($serializeOptions['countProducts'] || $serializeOptions['hideEmptyCategories']) {
$serializeOptions['listFilters']['listAttributeFilters'] = $listParameters['listAttributes'] ?? [];
}
$id = ('null' === $request->request->get('id'))
? null
: $request->request->get('id');
if (!empty($serializeOptions['prefix'])) {
$id = str_replace($serializeOptions['prefix'], '', $id);
}
return $this->json(
$this->categoryService->getChildren(
$id,
$request->getLocale(),
$serializeOptions
)
);
}
/**
* @Route(
* "/save",
* name="save",
* methods={"POST"}
* )
*
* @param Request $request
* @param JsonService $jsonService
*
* @return Response
*/
public function save(Request $request, JsonService $jsonService): Response
{
return $this->json(
$this->categoryService->save(
$jsonService->decode($request->request->get('category')),
$request->request->get('editType') ?? HistoricalEnum::TYPE_EDIT
)
);
}
/**
* @Route(
* "/delete",
* name="delete",
* methods={"POST"}
* )
*
* @param Request $request
*
* @return Response
*/
public function delete(Request $request): Response
{
return $this->json(
$this->categoryService->delete($request->request->get('id'))
);
}
/**
* @Route(
* "/clear-products",
* name="clearProducts",
* methods={"POST"},
* options={"expose" = true}
* )
*
* @param Request $request
*
* @return Response
*/
public function clearProducts(Request $request): Response
{
return $this->json(
$this->categoryService->clearProducts(
$request->request->get('id'),
'true' === $request->request->get('recursive')
)
);
}
/**
* @Route(
* "/save-list",
* name="saveList",
* methods={"POST"},
* options={"expose" = true}
* )
*
* @param Request $request
*
* @return Response
*/
public function saveList(Request $request): Response
{
return $this->json(
$this->categoryService->saveList($request->request->get('list'))
);
}
/**
* @Route(
* "/import",
* name="import",
* methods={"POST"},
* options={"expose" = true}
* )
*/
public function import(Request $request): Response
{
$channelCategoryId = $request->request->get('channelCategoryId');
return $this->json(
$this->categoryService->import($request->files->get('import-file-categories'), $channelCategoryId)
);
}
/**
* @Route(
* "/export/{idParent}/{extension}",
* name="export",
* methods={"GET"}
* )
*/
public function export(string $extension, string $idParent): Response
{
return $this->categoryService->export($extension, $idParent);
}
/**
* @Route(
* "/breadcrumbs-products",
* name="getListBreadcrumbsByListProducts",
* methods={"POST"}
* )
*
* @param Request $request
* @param JsonService $jsonService
*
* @return Response
*/
public function getListBreadcrumbsByListProducts(Request $request, JsonService $jsonService): Response
{
return $this->json(
$this->categoryService->getListBreadcrumbsByListProducts(
$request->request->get('channelId'),
$jsonService->decode($request->request->get('listProductsUniqId'))
)
);
}
}