List duplicate persons

Signed-off-by: Mathieu Jaumotte <mathieu.jaumotte@champs-libres.coop>

Signed-off-by: Mathieu Jaumotte <mathieu.jaumotte@champs-libres.coop>
This commit is contained in:
2021-03-21 13:58:19 +01:00
parent 0cc951b296
commit 728ea73bdf
12 changed files with 469 additions and 22 deletions

View File

@@ -0,0 +1,141 @@
<?php
namespace Chill\PersonBundle\Controller;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\PersonNotDuplicate;
use Chill\PersonBundle\Form\PersonConfimDuplicateType;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Search\SimilarPersonMatcher;
use http\Exception\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\TranslatorInterface;
class PersonDuplicateController extends Controller
{
/**
* @var \Chill\PersonBundle\Search\SimilarPersonMatcher
*/
private $similarPersonMatcher;
/**
* @var \Symfony\Component\Translation\TranslatorInterface
*/
private $translator;
/**
* @var \Chill\PersonBundle\Repository\PersonRepository
*/
private $personRepository;
public function __construct(
SimilarPersonMatcher $similarPersonMatcher,
TranslatorInterface $translator,
PersonRepository $personRepository
) {
$this->similarPersonMatcher = $similarPersonMatcher;
$this->translator = $translator;
$this->personRepository = $personRepository;
}
public function viewAction($person_id)
{
$person = $this->_getPerson($person_id);
if ($person === null) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found on this server");
}
$duplicatePersons = $this->similarPersonMatcher->matchPerson($person, 0.5);
return $this->render('ChillPersonBundle:PersonDuplicate:view.html.twig', [
"person" => $person,
'duplicatePersons' => $duplicatePersons
]);
}
public function confirmAction($person_id, $person2_id, Request $request)
{
if ($person_id === $person2_id) {
throw new InvalidArgumentException('Can not merge same person');
}
if ($person_id > $person2_id) {
$tmpId = $person2_id;
$person2_id = $person_id;
$person_id = $tmpId;
unset($tmpId);
}
$person = $this->_getPerson($person_id);
if ($person === null) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found on this server");
}
$person2 = $this->_getPerson($person2_id);
if ($person2 === null) {
throw $this->createNotFoundException("Person with id $person2_id not"
. " found on this server");
}
$form = $this->createForm(PersonConfimDuplicateType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
dd('todo');
}
return $this->render('ChillPersonBundle:PersonDuplicate:confirm.html.twig', [
'person' => $person,
'person2' => $person2,
'form' => $form->createView(),
]);
}
public function notDuplicateAction($person_id, $person2_id)
{
if ($person_id === $person2_id) {
throw new InvalidArgumentException('Can not merge same person');
}
if ($person_id > $person2_id) {
$tmpId = $person2_id;
$person2_id = $person_id;
$person_id = $tmpId;
unset($tmpId);
}
$person = $this->_getPerson($person_id);
if ($person === null) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found on this server");
}
$person2 = $this->_getPerson($person2_id);
if ($person2 === null) {
throw $this->createNotFoundException("Person with id $person2_id not"
. " found on this server");
}
$personNotDuplicate = new PersonNotDuplicate();
$personNotDuplicate->setPerson1($person);
$personNotDuplicate->setPerson2($person2);
$personNotDuplicate->setUser($this->getUser());
$this->getDoctrine()->getManager()->persist($personNotDuplicate);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('chill_person_duplicate_view', ['person_id' => $person->getId()]);
}
/**
* easy getting a person by his id
*/
private function _getPerson($id): ?Person
{
return $this->personRepository->find($id);
}
}