diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index fb7e6b9b4..bb5d9bdac 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -94,7 +94,6 @@ class PersonMove foreach ($metadata->getAssociationMappings() as $field => $mapping) { if ($mapping['targetEntity'] === Person::class) { - if (\in_array($metadata->getName(), $toDelete)) { $sql = $this->createDeleteSQL($metadata, $from, $field); $event = new ActionEvent($from->getId(), $metadata->getName(), $sql, diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php index 4438bde8d..a7b9a6523 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php @@ -2,13 +2,17 @@ namespace Chill\PersonBundle\Controller; +use Chill\PersonBundle\Actions\Remove\PersonMove; +use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\PersonNotDuplicate; use Chill\PersonBundle\Form\PersonConfimDuplicateType; +use Chill\PersonBundle\Privacy\PrivacyEvent; use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Search\SimilarPersonMatcher; use http\Exception\InvalidArgumentException; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Translation\TranslatorInterface; @@ -29,14 +33,28 @@ class PersonDuplicateController extends Controller */ private $personRepository; + /** + * @var \Chill\PersonBundle\Actions\Remove\PersonMove + */ + private $personMove; + + /** + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + private $eventDispatcher; + public function __construct( SimilarPersonMatcher $similarPersonMatcher, TranslatorInterface $translator, - PersonRepository $personRepository + PersonRepository $personRepository, + PersonMove $personMove, + EventDispatcherInterface $eventDispatcher ) { $this->similarPersonMatcher = $similarPersonMatcher; $this->translator = $translator; $this->personRepository = $personRepository; + $this->personMove = $personMove; + $this->eventDispatcher = $eventDispatcher; } public function viewAction($person_id) @@ -47,88 +65,88 @@ class PersonDuplicateController extends Controller . " found on this server"); } - $duplicatePersons = $this->similarPersonMatcher->matchPerson($person, 0.5); + $duplicatePersons = $this->similarPersonMatcher-> + matchPerson($person, 0.5, SimilarPersonMatcher::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL); + + $notDuplicatePersons = $this->getDoctrine()->getRepository(PersonNotDuplicate::class) + ->findNotDuplicatePerson($person); return $this->render('ChillPersonBundle:PersonDuplicate:view.html.twig', [ - "person" => $person, - 'duplicatePersons' => $duplicatePersons + 'person' => $person, + 'duplicatePersons' => $duplicatePersons, + 'notDuplicatePersons' => $notDuplicatePersons, ]); } - public function confirmAction($person_id, $person2_id, Request $request) + public function confirmAction($person1_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"); - } + [$person1, $person2] = $this->_getPersonsByPriority($person1_id, $person2_id); $form = $this->createForm(PersonConfimDuplicateType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - dd('todo'); + $event = new PrivacyEvent($person1, array( + 'element_class' => Person::class, + 'action' => 'move' + )); + $event->addPerson($person2); + $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); + + $sqls = $this->personMove->getSQL($person2, $person1); + + $connection = $this->getDoctrine()->getConnection(); + + $connection->beginTransaction(); + foreach($sqls as $sql) { + $connection->executeQuery($sql); + } + $connection->commit(); + + return $this->redirectToRoute('chill_person_duplicate_view', ['person_id' => $person1->getId()]); } return $this->render('ChillPersonBundle:PersonDuplicate:confirm.html.twig', [ - 'person' => $person, + 'person' => $person1, 'person2' => $person2, 'form' => $form->createView(), ]); } - public function notDuplicateAction($person_id, $person2_id) + public function notDuplicateAction($person1_id, $person2_id) { - if ($person_id === $person2_id) { - throw new InvalidArgumentException('Can not merge same person'); + [$person1, $person2] = $this->_getPersonsByPriority($person1_id, $person2_id); + + $personNotDuplicate = $this->getDoctrine()->getRepository(PersonNotDuplicate::class) + ->findOneBy(['person1' => $person1, 'person2' => $person2]); + + if (!$personNotDuplicate instanceof PersonNotDuplicate) { + $personNotDuplicate = new PersonNotDuplicate(); + $personNotDuplicate->setPerson1($person1); + $personNotDuplicate->setPerson2($person2); + $personNotDuplicate->setUser($this->getUser()); + + $this->getDoctrine()->getManager()->persist($personNotDuplicate); + $this->getDoctrine()->getManager()->flush(); } - if ($person_id > $person2_id) { - $tmpId = $person2_id; - $person2_id = $person_id; - $person_id = $tmpId; - unset($tmpId); + return $this->redirectToRoute('chill_person_duplicate_view', ['person_id' => $person1->getId()]); + } + + public function removeNotDuplicateAction($person1_id, $person2_id) + { + [$person1, $person2] = $this->_getPersonsByPriority($person1_id, $person2_id); + + $personNotDuplicate = $this->getDoctrine()->getRepository(PersonNotDuplicate::class) + ->findOneBy(['person1' => $person1, 'person2' => $person2]); + + if ($personNotDuplicate instanceof PersonNotDuplicate) { + $this->getDoctrine()->getManager()->remove($personNotDuplicate); + $this->getDoctrine()->getManager()->flush(); } - $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()]); + return $this->redirectToRoute('chill_person_duplicate_view', ['person_id' => $person1->getId()]); } /** @@ -138,4 +156,31 @@ class PersonDuplicateController extends Controller { return $this->personRepository->find($id); } + + private function _getPersonsByPriority($person1_id, $person2_id) + { + if ($person1_id === $person2_id) { + throw new InvalidArgumentException('Can not merge same person'); + } + + if ($person1_id > $person2_id) { + $person1 = $this->_getPerson($person2_id); + $person2 = $this->_getPerson($person1_id); + } else { + $person1 = $this->_getPerson($person1_id); + $person2 = $this->_getPerson($person2_id); + } + + if ($person1 === null) { + throw $this->createNotFoundException("Person with id $person1_id not" + . " found on this server"); + } + + if ($person2 === null) { + throw $this->createNotFoundException("Person with id $person2_id not" + . " found on this server"); + } + + return [$person1, $person2]; + } } diff --git a/src/Bundle/ChillPersonBundle/Entity/PersonNotDuplicate.php b/src/Bundle/ChillPersonBundle/Entity/PersonNotDuplicate.php index e8b79698f..ce2a1c26f 100644 --- a/src/Bundle/ChillPersonBundle/Entity/PersonNotDuplicate.php +++ b/src/Bundle/ChillPersonBundle/Entity/PersonNotDuplicate.php @@ -9,7 +9,7 @@ use Chill\MainBundle\Entity\User; * PersonNotDuplicate * * @ORM\Table(name="chill_person_not_duplicate") - * @ORM\Entity() + * @ORM\Entity(repositoryClass="Chill\PersonBundle\Repository\PersonNotDuplicateRepository") */ class PersonNotDuplicate { diff --git a/src/Bundle/ChillPersonBundle/Form/PersonConfimDuplicateType.php b/src/Bundle/ChillPersonBundle/Form/PersonConfimDuplicateType.php index acc385f43..fd7fa3e50 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonConfimDuplicateType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonConfimDuplicateType.php @@ -16,7 +16,7 @@ class PersonConfimDuplicateType extends AbstractType { $builder ->add('confirm', CheckboxType::class, [ - 'label' => 'Je confirme la fusion de ces 2 personnes', + 'label' => 'I confirm the merger of these 2 people', 'mapped' => false, ]); } diff --git a/src/Bundle/ChillPersonBundle/Repository/PersonNotDuplicateRepository.php b/src/Bundle/ChillPersonBundle/Repository/PersonNotDuplicateRepository.php new file mode 100644 index 000000000..8ab711b15 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Repository/PersonNotDuplicateRepository.php @@ -0,0 +1,41 @@ +createQueryBuilder('pnd'); + $qb->select('pnd') + ->where('pnd.person1 = :person OR pnd.person2 = :person') + ; + $qb->setParameter('person', $person); + $result = $qb->getQuery()->getResult(); + + $persons = []; + foreach ($result as $row) { + if ($row->getPerson1() === $person) { + $persons[] = $row->getPerson2(); + } elseif ($row->getPerson2() === $person) { + $persons[] = $row->getPerson1(); + } + } + + return $persons; + } +} diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig index fcd314a62..f6155eead 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig @@ -6,12 +6,12 @@ ' ' ~ person.lastName }}{% endblock %} {% block personcontent %} -