|
|
@@ -30,6 +30,7 @@ use Lc\SovBundle\Doctrine\EntityManager; |
|
|
|
use Lc\SovBundle\Doctrine\Extension\BlameableInterface; |
|
|
|
use Lc\SovBundle\Factory\User\UserFactory; |
|
|
|
use Lc\SovBundle\Form\User\ConfirmDeleteUserFormType; |
|
|
|
use Lc\SovBundle\Model\User\UserInterface; |
|
|
|
use Lc\SovBundle\Translation\FlashBagTranslator; |
|
|
|
use Lc\SovBundle\Translation\TranslatorAdmin; |
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
@@ -96,14 +97,15 @@ abstract class UserAdminController extends AbstractAdminController |
|
|
|
{ |
|
|
|
$entityManager = $this->getEntityManager(); |
|
|
|
|
|
|
|
// Todo envisager un refactor similaire pour toutes les fonctions CRUD |
|
|
|
$eaBeforeCrudActionEventDelete = $this->eaBeforeCrudActionEventDelete($context); |
|
|
|
if (!is_null($eaBeforeCrudActionEventDelete)) { |
|
|
|
return $eaBeforeCrudActionEventDelete; |
|
|
|
} |
|
|
|
|
|
|
|
$entityInstance = $context->getEntity()->getInstance(); |
|
|
|
$user = $context->getEntity()->getInstance(); |
|
|
|
|
|
|
|
$event = new BeforeEntityDeletedEvent($entityInstance); |
|
|
|
$event = new BeforeEntityDeletedEvent($user); |
|
|
|
$this->container->get('event_dispatcher')->dispatch($event); |
|
|
|
if ($event->isPropagationStopped()) { |
|
|
|
return $event->getResponse(); |
|
|
@@ -112,102 +114,42 @@ abstract class UserAdminController extends AbstractAdminController |
|
|
|
$user = $event->getEntityInstance(); |
|
|
|
|
|
|
|
|
|
|
|
$metas = $entityManager->getMetadataFactory()->getAllMetadata(); |
|
|
|
foreach ($metas as $meta) { |
|
|
|
$entityFqcnList[] = $meta->getName(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Creéer formulaire avec un champ confirm |
|
|
|
$confirmDeleteUserForm = $this->createForm(ConfirmDeleteUserFormType::class, null, array( |
|
|
|
'action' => $this->getAdminUrlGenerator()->generateUrl() |
|
|
|
)); |
|
|
|
|
|
|
|
$confirmDeleteUserForm->handleRequest($context->getRequest()); |
|
|
|
|
|
|
|
$entityManager->delete($user); |
|
|
|
|
|
|
|
$entitiesWarning['reductionCart'] = $this->getReductionCartContainer()->getStore()->getByUserOutOfContext($user); |
|
|
|
$entitiesWarning['reductionCatalog'] = $this->getReductionCatalogContainer()->getStore()->getByUserOutOfContext($user); |
|
|
|
$entitiesWarning['reductionCredit'] = $this->getReductionCreditContainer()->getStore()->getReductionCreditByUserOutOfContext($user); |
|
|
|
$entitiesWarning['reductionGift'] = $this->getReductionCreditContainer()->getStore()->getReductionGiftByUserOutOfContext($user); |
|
|
|
|
|
|
|
|
|
|
|
//si clqiue suppression |
|
|
|
$entitiesToDelete = array(); |
|
|
|
foreach ($entityFqcnList as $entityFqcn) { |
|
|
|
|
|
|
|
if (!is_null($this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn))) { |
|
|
|
$repositoryQuery = $this->container->get( |
|
|
|
$this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn) |
|
|
|
)->getRepositoryQuery(); |
|
|
|
|
|
|
|
$entitiesToDelete[$entityFqcn] = $this->getUserContainer()->getBuilder()->getEntitiesToDeleteForUserDelete($user, $entityFqcn, $repositoryQuery); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$entityManager->delete($entityInstance); |
|
|
|
|
|
|
|
dump($entityManager->getUnitOfWork()->getScheduledEntityDeletions()); |
|
|
|
// dump($entityManager->getUnitOfWork()->getEntityChangeSet()); |
|
|
|
dump($entityManager->getUnitOfWork()->getScheduledEntityUpdates()); |
|
|
|
dump($entityManager->getUnitOfWork()->getScheduledEntityInsertions()); |
|
|
|
|
|
|
|
if ($confirmDeleteUserForm->isSubmitted()) { |
|
|
|
|
|
|
|
$entityManager->flush(); |
|
|
|
$this->get(FlashBagTranslator::class)->add('success', 'deleted', $this->getTranslationEntityName()); |
|
|
|
$warningMessages = $this->getDeleteUserWarningMessageList($user); |
|
|
|
|
|
|
|
|
|
|
|
$this->deleteEntity($this->container->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance); |
|
|
|
//Todo supprimer les éléments listés |
|
|
|
//Avant la suppression on s'assure que l'utilisateur à confirmer et qu'il n'y aucun message d'erreur |
|
|
|
if ($confirmDeleteUserForm->isSubmitted() && count($warningMessages['danger']) === 0) { |
|
|
|
//Détecter les tables qui possède des relations avec un champ qui n'existe plus |
|
|
|
//Dans le cas ci-dessous détecter les adresses lié à un utilisateur qui n'existe plus |
|
|
|
//SELECT * FROM address a LEFT OUTER JOIN user u ON(u.id=a.user_id) WHERE u.id is null |
|
|
|
try { |
|
|
|
$entityManager->flush(); |
|
|
|
$this->addFlashTranslator('success', 'deleted'); |
|
|
|
} catch (ForeignKeyConstraintViolationException $e) { |
|
|
|
throw new EntityRemoveException(['entity_name' => $context->getEntity()->getName(), 'message' => $e->getMessage()]); |
|
|
|
} |
|
|
|
return $this->redirect($this->getAdminUrlGenerator()->setAction(Crud::PAGE_INDEX)->setEntityId(null)->generateUrl()); |
|
|
|
|
|
|
|
}else{ |
|
|
|
$entitiesToDelete = $entityManager->getUnitOfWork()->getScheduledEntityDeletions(); |
|
|
|
$entitiesToUpdate = $entityManager->getUnitOfWork()->getScheduledEntityUpdates(); |
|
|
|
if($confirmDeleteUserForm->isSubmitted()){ |
|
|
|
$this->addFlashTranslator('error', 'cannotDelete'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// foreach ($entitiesToDelete as $entityFqcn) { |
|
|
|
// foreach ($entityFqcn as $action => $field) { |
|
|
|
// if ($action == 'nullify') { |
|
|
|
// foreach ($field as $fieldName => $entityList) { |
|
|
|
// $method = 'set' . ucfirst($fieldName); |
|
|
|
// $methodGet = 'get' . ucfirst($fieldName); |
|
|
|
// foreach ($entityList as $entity) { |
|
|
|
// $entity->$method(null); |
|
|
|
// $this->getEntityManager()->update($entity); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// |
|
|
|
//// if ($action == 'delete') { |
|
|
|
//// foreach ($field as $entity) { |
|
|
|
//// dump($entity); |
|
|
|
//// $this->getEntityManager()->delete($entity); |
|
|
|
//// } |
|
|
|
//// } |
|
|
|
// |
|
|
|
// } |
|
|
|
// |
|
|
|
// } |
|
|
|
//Détecter les merde |
|
|
|
//SELECT * FROM address a LEFT OUTER JOIN user u ON(u.id=a.user_id) WHERE u.id is null |
|
|
|
|
|
|
|
$responseParameters = $this->configureResponseParameters(KeyValueStore::new([ |
|
|
|
'pageName' => Crud::PAGE_DETAIL, |
|
|
|
'templatePath' => '@LcSov/adminlte/crud/delete.html.twig', |
|
|
|
'confirm_delete_user_form' => $confirmDeleteUserForm->createView(), |
|
|
|
'global_actions' => array(), |
|
|
|
'batch_actions' => array(), |
|
|
|
'entities_warning' => $entitiesWarning, |
|
|
|
'entities_update' => $entitiesToUpdate, |
|
|
|
'warning_message_list' => $warningMessages |
|
|
|
])); |
|
|
|
|
|
|
|
$event = new AfterCrudActionEvent($context, $responseParameters); |
|
|
@@ -217,7 +159,41 @@ abstract class UserAdminController extends AbstractAdminController |
|
|
|
} |
|
|
|
|
|
|
|
return $responseParameters; |
|
|
|
} |
|
|
|
|
|
|
|
public function getDeleteUserWarningMessageList(UserInterface $user): array |
|
|
|
{ |
|
|
|
$warningMessages = array(); |
|
|
|
$warningMessages['danger'] = []; |
|
|
|
$warningMessages['warning'] = []; |
|
|
|
$warningMessages['info'] = []; |
|
|
|
|
|
|
|
|
|
|
|
$entityManager = $this->getEntityManager(); |
|
|
|
|
|
|
|
$entityToDeleteListCount = array(); |
|
|
|
$entityToDeleteListName = array(); |
|
|
|
foreach ($entityManager->getUnitOfWork()->getScheduledEntityDeletions() as $entityToDelete) { |
|
|
|
if (isset($entityToDeleteListCount[(new \ReflectionClass($entityToDelete))->getShortName()])) { |
|
|
|
$entityToDeleteListCount[(new \ReflectionClass($entityToDelete))->getShortName()]++; |
|
|
|
} else { |
|
|
|
$entityToDeleteListCount[(new \ReflectionClass($entityToDelete))->getShortName()] = 1; |
|
|
|
} |
|
|
|
$entityToDeleteListName[(new \ReflectionClass($entityToDelete))->getShortName()][] = $entityToDelete->getId(); |
|
|
|
} |
|
|
|
|
|
|
|
foreach ($entityToDeleteListCount as $entityName => $entityToDeleteCount) { |
|
|
|
$warningMessages['info'][] = $this->getTranslatorAdmin()->transFlashMessage( |
|
|
|
'error', |
|
|
|
'deleteEntityCascade', |
|
|
|
'User', |
|
|
|
array( |
|
|
|
'%entity%' => $this->getTranslatorAdmin()->trans('entity.'.$entityName.'.label_plurial'), |
|
|
|
'%count%' => $entityToDeleteCount |
|
|
|
) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
return $warningMessages; |
|
|
|
} |
|
|
|
} |