mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
validate thirdparty merge
This commit is contained in:
parent
58291c7402
commit
9979378e78
@ -14,16 +14,16 @@ namespace Chill\ThirdPartyBundle\Controller;
|
|||||||
use Chill\PersonBundle\Form\PersonConfimDuplicateType;
|
use Chill\PersonBundle\Form\PersonConfimDuplicateType;
|
||||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||||
use Chill\ThirdPartyBundle\Form\ThirdpartyFindDuplicateType;
|
use Chill\ThirdPartyBundle\Form\ThirdpartyFindDuplicateType;
|
||||||
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
|
|
||||||
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
|
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||||
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
class ThirdpartyDuplicateController extends AbstractController
|
class ThirdpartyDuplicateController extends AbstractController
|
||||||
{
|
{
|
||||||
public function __construct(private readonly ThirdPartyRepository $thirdPartyRepository, private readonly ThirdpartyMergeService $thirdPartyMergeService) {}
|
public function __construct(private readonly ThirdpartyMergeService $thirdPartyMergeService, private readonly TranslatorInterface $translator) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ParamConverter("thirdparty", options={"id": "thirdparty_id"})
|
* @ParamConverter("thirdparty", options={"id": "thirdparty_id"})
|
||||||
@ -74,25 +74,45 @@ class ThirdpartyDuplicateController extends AbstractController
|
|||||||
#[Route(path: '/{_locale}/3party/{thirdparty1_id}/duplicate/{thirdparty2_id}/confirm', name: 'chill_thirdparty_duplicate_confirm')]
|
#[Route(path: '/{_locale}/3party/{thirdparty1_id}/duplicate/{thirdparty2_id}/confirm', name: 'chill_thirdparty_duplicate_confirm')]
|
||||||
public function confirmAction(ThirdParty $thirdparty1, ThirdParty $thirdparty2, Request $request)
|
public function confirmAction(ThirdParty $thirdparty1, ThirdParty $thirdparty2, Request $request)
|
||||||
{
|
{
|
||||||
if ($thirdparty1 === $thirdparty2) {
|
try {
|
||||||
throw new \InvalidArgumentException('Can not merge same thirdparty');
|
$this->validateThirdpartyMerge($thirdparty1, $thirdparty2);
|
||||||
|
$form = $this->createForm(PersonConfimDuplicateType::class);
|
||||||
|
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
|
|
||||||
|
$this->thirdPartyMergeService->merge($thirdparty1, $thirdparty2);
|
||||||
|
|
||||||
|
return $this->redirectToRoute('chill_crud_3party_3party_view', ['id' => $thirdparty1->getId()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('@ChillThirdParty/ThirdPartyDuplicate/confirm.html.twig', [
|
||||||
|
'thirdparty' => $thirdparty1,
|
||||||
|
'thirdparty2' => $thirdparty2,
|
||||||
|
'form' => $form->createView(),
|
||||||
|
]);
|
||||||
|
} catch (\InvalidArgumentException $e) {
|
||||||
|
$this->addFlash('error', $this->translator->trans($e->getMessage()));
|
||||||
|
|
||||||
|
return $this->redirectToRoute('chill_thirdparty_find_duplicate', [
|
||||||
|
'thirdparty_id' => $thirdparty1->getId(),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$form = $this->createForm(PersonConfimDuplicateType::class);
|
private function validateThirdpartyMerge(Thirdparty $thirdparty1, Thirdparty $thirdparty2): void
|
||||||
|
{
|
||||||
|
$constraints = [
|
||||||
|
[$thirdparty1 === $thirdparty2, 'thirdparty_duplicate.You cannot merge a thirdparty with itself. Please choose a different thirdparty'],
|
||||||
|
[$thirdparty1->getKind() !== $thirdparty2->getKind(), 'thirdparty_duplicate.A thirdparty can only be merged with a thirdparty of the same kind'],
|
||||||
|
[$thirdparty1->getParent() !== $thirdparty2->getParent(), 'thirdparty_duplicate.Two child thirdparties must have the same parent'],
|
||||||
|
];
|
||||||
|
|
||||||
$form->handleRequest($request);
|
foreach ($constraints as [$condition, $message]) {
|
||||||
|
if ($condition) {
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
throw new \InvalidArgumentException($message);
|
||||||
|
}
|
||||||
$this->thirdPartyMergeService->merge($thirdparty1, $thirdparty2);
|
|
||||||
|
|
||||||
return $this->redirectToRoute('chill_crud_3party_3party_view', ['id' => $thirdparty1->getId()]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->render('@ChillThirdParty/ThirdPartyDuplicate/confirm.html.twig', [
|
|
||||||
'thirdparty' => $thirdparty1,
|
|
||||||
'thirdparty2' => $thirdparty2,
|
|
||||||
'form' => $form->createView(),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,6 @@ class ThirdpartyMergeService
|
|||||||
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
|
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
|
||||||
|
|
||||||
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) {
|
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) {
|
||||||
// TODO what with 'address_id' and 'civility_id'? This condition also contains columns like updatedBy_id which we want to ignore...
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,9 +101,7 @@ class ThirdpartyMergeService
|
|||||||
'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
|
'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
|
||||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||||
];
|
];
|
||||||
}
|
} elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) {
|
||||||
|
|
||||||
if ($assoc['type'] === 8 && isset($assoc['joinTable'])) {
|
|
||||||
$joinTable = $assoc['joinTable']['name'];
|
$joinTable = $assoc['joinTable']['name'];
|
||||||
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
|
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
|
||||||
$queries[] = [
|
$queries[] = [
|
||||||
|
@ -141,3 +141,6 @@ thirdparty_duplicate:
|
|||||||
Thirdparty to delete explanation: Ce tiers sera supprimé. Seuls les contacts de ce tiers, énumérés ci-dessous, seront transférés.
|
Thirdparty to delete explanation: Ce tiers sera supprimé. Seuls les contacts de ce tiers, énumérés ci-dessous, seront transférés.
|
||||||
Thirdparty to keep explanation: Ce tiers sera conserver
|
Thirdparty to keep explanation: Ce tiers sera conserver
|
||||||
Data to keep: Données conservées
|
Data to keep: Données conservées
|
||||||
|
You cannot merge a thirdparty with itself. Please choose a different thirdparty: Vous ne pouvez pas fusionner un tiers avec lui-même. Veuillez choisir un autre tiers.
|
||||||
|
A thirdparty can only be merged with a thirdparty of the same kind: Un tiers ne peut être fusionné qu'avec un tiers de même type.
|
||||||
|
Two child thirdparties must have the same parent: Deux tiers de type « contact » doivent avoir le même tiers parent.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user