<?php
namespace App\Controller\Pim;
use App\Enum\AttributeEnum;
use App\Enum\PrivilegeEnum;
use App\Services\Admin\UserService;
use App\Services\Main\ApiService;
use App\Services\Main\HistoricalService;
use App\Services\Pim\AttributeService;
use App\Services\Pim\FamilyService;
use App\Services\Pim\ReferentialService;
use App\Services\Util\JsonService;
use App\Services\Util\UtilService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class ReferentialController.
*
* @Route("/referential", name="parameters.pim.referential.")
*/
class ReferentialController extends AbstractController
{
/**
* @var ReferentialService
*/
private ReferentialService $referentialService;
/**
* @var UserService
*/
private UserService $userService;
/**
* @var ApiService
*/
private ApiService $apiService;
/**
* @var UtilService
*/
private UtilService $utilService;
public function __construct(
ReferentialService $referentialService,
UserService $userService,
ApiService $apiService,
UtilService $utilService
) {
$this->referentialService = $referentialService;
$this->userService = $userService;
$this->apiService = $apiService;
$this->utilService = $utilService;
}
/**
* @Route("/list", name="list")
*
* @return Response
*/
public function index(): Response
{
if (!$this->userService->hasModule('pim') || !$this->userService->hasPrivilege(PrivilegeEnum::REFERENTIALS_LIST)) {
$this->utilService->throwCustomException('global.errors.exceptions.pageNotFound');
}
return $this->render('pim/referential/list.html.twig', [
'list' => $this->referentialService->getList(),
]);
}
/**
* @Route(
* "/delete",
* name="delete",
* methods={"POST"}
* )
*
* @param Request $request
*
* @return mixed
*/
public function delete(Request $request): mixed
{
return $this->json(
$this->referentialService->delete($request->request->get('referentials-list'))
);
}
/**
* @Route(
* "/edit/{id}",
* defaults={"id" = null},
* name="edit",
* methods={"GET"}
* )
*/
public function edit(
?string $id,
Request $request,
HistoricalService $historicalService,
AttributeService $attributeService,
FamilyService $familyService
): Response|RedirectResponse {
if (!$this->userService->hasModule('pim')) {
$this->utilService->throwCustomException('global.errors.exceptions.pageNotFound');
}
$referential = $this->apiService->get("pim/referential/id/{$id}", ['defaultResult' => []])->getContent();
if (null !== $id && empty($referential)) {
return $this->redirectToRoute('parameters.pim.referential.list');
}
$listAttributes = $attributeService->getList();
return $this->render('pim/referential/edit/edit.html.twig', [
'listAttributes' => $attributeService->serializeListByCode($listAttributes),
'listFamilies' => $familyService->getFlatList(false),
'listAttributesNames' => $attributeService->getListAttributesNames($listAttributes, $request->getLocale()),
'listAttributesTypeChoices' => $attributeService->serializeListAttributesChoices(
$attributeService->filterListByTypes(
$listAttributes,
[
AttributeEnum::TYPE_SELECT,
AttributeEnum::TYPE_MULTIPLE,
]
)
),
'referential' => $referential,
'historicals' => (null !== $id)
? $historicalService->getHistorical([
'object' => 'referential',
'code' => $referential['code'],
])['historicals']
: [],
]);
}
/**
* @Route(
* "/save",
* name="save",
* methods={"POST"}
* )
*
* @param Request $request
* @param JsonService $jsonService
*
* @return Response
*/
public function save(Request $request, JsonService $jsonService): Response
{
if (!$this->userService->hasPrivilege(PrivilegeEnum::REFERENTIALS_EDIT)) {
$this->utilService->throwCustomException('global.errors.exceptions.pageNotFound');
}
return $this->json(
$this->referentialService->save(
$jsonService->decode($request->request->get('referential')),
$request->request->get('editType')
)
);
}
/**
* @Route(
* "/import",
* name="import",
* methods={"POST"}
* )
*
* @param Request $request
*
* @return Response
*/
public function import(Request $request): Response
{
return $this->json(
$this->referentialService->import($request->files->get('import-file'))
);
}
/**
* @Route(
* "/export/{extension}",
* name="export",
* methods={"GET"}
* )
*
* @param string $extension
*
* @return Response
*/
public function export(string $extension): Response
{
return $this->referentialService->export($extension);
}
}