diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index 13a62094d..8363134fd 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -13,7 +13,11 @@ namespace Chill\PersonBundle\Actions\Remove; use Chill\PersonBundle\Actions\ActionEvent; use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; +use Chill\PersonBundle\Entity\Household\HouseholdMember; +use Chill\PersonBundle\Entity\Household\PersonHouseholdAddress; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Entity\Relationships\Relationship; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -42,9 +46,10 @@ class PersonMove protected $eventDispatcher; public function __construct( - EntityManagerInterface $em, + EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher - ) { + ) + { $this->em = $em; $this->eventDispatcher = $eventDispatcher; } @@ -84,8 +89,11 @@ class PersonMove } foreach ($metadata->getAssociationMappings() as $field => $mapping) { - if (Person::class === $mapping['targetEntity']) { - if (in_array($metadata->getName(), $toDelete, true)) { + if (in_array($mapping['sourceEntity'], $this->getIgnoredEntities())) { + continue; + } + if (Person::class === $mapping['targetEntity'] and true === $mapping['isOwningSide']) { + if (in_array($mapping['sourceEntity'], $toDelete, true)) { $sql = $this->createDeleteSQL($metadata, $from, $field); $event = new ActionEvent( $from->getId(), @@ -117,10 +125,11 @@ class PersonMove $from->getId() ); + dump($sqls); return $sqls; } - protected function createDeleteSQL(ClassMetadata $metadata, Person $from, $field): string + private function createDeleteSQL(ClassMetadata $metadata, Person $from, $field): string { $mapping = $metadata->getAssociationMapping($field); @@ -137,26 +146,41 @@ class PersonMove ); } - protected function createMoveSQL(ClassMetadata $metadata, Person $from, Person $to, $field): string + private function createMoveSQL(ClassMetadata $metadata, Person $from, Person $to, $field): string { $mapping = $metadata->getAssociationMapping($field); // Set part of the query, aka in "UPDATE table SET " $sets = []; - - foreach ($mapping['joinColumns'] as $columns) { - $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); - } - $conditions = []; + dump($mapping); - foreach ($mapping['joinColumns'] as $columns) { - $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + if (array_key_exists('joinTable', $mapping)) { + $tableName = (null !== ($mapping['joinTable']['schema'] ?? null) ? $mapping['joinTable']['schema'] . '.' : '') + . $mapping['joinTable']['name']; + + foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { + $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); + } + + foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + } + } elseif (array_key_exists('joinColumns', $mapping)) { + $tableName = $this->getTableName($metadata); + foreach ($mapping['joinColumns'] as $columns) { + $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); + } + + + foreach ($mapping['joinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + } } return sprintf( 'UPDATE %s SET %s WHERE %s', - $this->getTableName($metadata), + $tableName, implode(' ', $sets), implode(' AND ', $conditions) ); @@ -166,10 +190,23 @@ class PersonMove * return an array of classes where entities should be deleted * instead of moved. */ - protected function getDeleteEntities(): array + private function getDeleteEntities(): array { return [ - AccompanyingPeriod::class, + Person\PersonCenterHistory::class, + HouseholdMember::class, + AccompanyingPeriodParticipation::class, + AccompanyingPeriod\AccompanyingPeriodWork::class, + Relationship::class + ]; + } + + private function getIgnoredEntities(): array + { + return [ + Person\PersonCurrentAddress::class, + PersonHouseholdAddress::class, + Person\PersonCenterCurrent::class, ]; } diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php index 928112d2e..ab353c83e 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php @@ -247,7 +247,7 @@ class PersonDuplicateController extends Controller ); $duplicatePersons = $this->similarPersonMatcher-> - matchPerson($person, $personNotDuplicateRepository, 0.5, SimilarPersonMatcher::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL); + matchPerson($person, 0.5, SimilarPersonMatcher::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL, false, $personNotDuplicateRepository); $notDuplicatePersons = $personNotDuplicateRepository->findNotDuplicatePerson($person); @@ -264,14 +264,14 @@ class PersonDuplicateController extends Controller $nb_activity = $em->getRepository(Activity::class)->findBy(['person' => $id]); $nb_document = $em->getRepository(PersonDocument::class)->findBy(['person' => $id]); - $nb_event = $em->getRepository(Participation::class)->findBy(['person' => $id]); +// $nb_event = $em->getRepository(Participation::class)->findBy(['person' => $id]); $nb_task = $em->getRepository(SingleTask::class)->countByParameters(['person' => $id]); $person = $em->getRepository(Person::class)->findOneBy(['id' => $id]); return [ 'nb_activity' => count($nb_activity), 'nb_document' => count($nb_document), - 'nb_event' => count($nb_event), +// 'nb_event' => count($nb_event), 'nb_task' => $nb_task, 'nb_addresses' => count($person->getAddresses()), ]; diff --git a/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php b/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php index 39ac557fb..b7934c2b2 100644 --- a/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php +++ b/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php @@ -15,6 +15,7 @@ use Chill\MainBundle\Routing\LocalMenuBuilderInterface; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Repository\ResidentialAddressRepository; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; +use Chill\PersonBundle\Security\Authorization\PersonVoter; use Knp\Menu\MenuItem; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Security\Core\Security; @@ -106,17 +107,19 @@ class PersonMenuBuilder implements LocalMenuBuilderInterface ->setExtras([ 'order' => 99999, ]); - /* - $menu->addChild($this->translator->trans('Person duplicate'), [ - 'route' => 'chill_person_duplicate_view', - 'routeParameters' => [ - 'person_id' => $parameters['person']->getId(), - ], - ]) - ->setExtras([ - 'order' => 99999, - ]); - */ + + if ($this->security->isGranted(PersonVoter::DUPLICATE, $parameters['person'])) { + $menu->addChild($this->translator->trans('Person duplicate'), [ + 'route' => 'chill_person_duplicate_view', + 'routeParameters' => [ + 'person_id' => $parameters['person']->getId(), + ], + ]) + ->setExtras([ + 'order' => 99999, + ]); + } + if ( 'visible' === $this->showAccompanyingPeriod && $this->security->isGranted(AccompanyingPeriodVoter::SEE, $parameters['person']) diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig index 660fff7a6..1e770062a 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig @@ -39,7 +39,7 @@
  • {{ person.counters.nb_activity }} {{ (person.counters.nb_activity > 1)? 'échanges' : 'échange' }}
  • {{ person.counters.nb_task }} {{ (person.counters.nb_task > 1)? 'tâches' : 'tâche' }}
  • {{ person.counters.nb_document }} {{ (person.counters.nb_document > 1)? 'documents' : 'document' }}
  • -
  • {{ person.counters.nb_event }} {{ (person.counters.nb_event > 1)? 'événements' : 'événement' }}
  • +{#
  • {{ person.counters.nb_event }} {{ (person.counters.nb_event > 1)? 'événements' : 'événement' }}
  • #}
  • {{ person.counters.nb_addresses }} {{ (person.counters.nb_addresses > 1)? 'adresses' : 'adresse' }}
  • diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig index 304e78eb3..218b89f2a 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/confirm.html.twig @@ -25,7 +25,7 @@

    {{ 'Merge duplicate persons folders'|trans }}

    -
    +

    {{ 'Old person'|trans }}: {{ 'Old person explain'|trans }}

    @@ -43,7 +43,7 @@
    -
    +

    {{ 'New person'|trans }}: {{ 'New person explain'|trans }}

    @@ -63,10 +63,10 @@ {{ form_start(form) }} -
    +
    -
    +
    {{ form_widget(form.confirm) }}