Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

224 lines
9.2KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\User;
  3. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  10. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  13. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
  14. use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
  15. use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
  16. use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  20. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  21. use Lc\SovBundle\Container\User\UserContainer;
  22. use Lc\SovBundle\Controller\AbstractAdminController;
  23. use Lc\SovBundle\Definition\ActionDefinition;
  24. use Lc\SovBundle\Definition\ApplicationDefinition;
  25. use Lc\SovBundle\Definition\RolesDefinition;
  26. use Lc\SovBundle\Definition\RolesDefinitionInterface;
  27. use Lc\SovBundle\Doctrine\EntityManager;
  28. use Lc\SovBundle\Doctrine\Extension\BlameableInterface;
  29. use Lc\SovBundle\Factory\User\UserFactory;
  30. use Lc\SovBundle\Form\User\ConfirmDeleteUserFormType;
  31. use Lc\SovBundle\Translation\FlashBagTranslator;
  32. use Lc\SovBundle\Translation\TranslatorAdmin;
  33. use Symfony\Component\HttpFoundation\RequestStack;
  34. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  35. abstract class UserAdminController extends AbstractAdminController
  36. {
  37. public function buildIndexActions(Actions $actions): void
  38. {
  39. parent::buildIndexActions($actions); // TODO: Change the autogenerated stub
  40. $actions->add(Crud::PAGE_INDEX, $this->getSwitchUserAction());
  41. }
  42. public function getSwitchUserAction(): Action
  43. {
  44. $switchAction = Action::new(
  45. ActionDefinition::SWITCH_USER,
  46. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER),
  47. 'fa fa-fw fa-user-secret'
  48. )
  49. ->linkToCrudAction(ActionDefinition::SWITCH_USER)
  50. ->setLabel($this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER))
  51. ->setCssClass('in-dropdown text-info action-confirm action_switch');
  52. return $switchAction;
  53. }
  54. public function overrideEntitiesActions(?EntityCollection $entities, string $pageName): void
  55. {
  56. parent::overrideEntitiesActions($entities, $pageName); // TODO: Change the autogenerated stub
  57. foreach ($entities as $entity) {
  58. foreach ($entity->getActions() as $action) {
  59. if ($action->getName() == ActionDefinition::SWITCH_USER) {
  60. $url = $this->generateUrl(
  61. $this->getParameter('lc_sov.homepage_route'),
  62. array('_switch_user' => $entity->getInstance()->getEmail())
  63. );
  64. $action->setLinkUrl($url);
  65. }
  66. }
  67. }
  68. }
  69. public function configureFields(string $pageName): iterable
  70. {
  71. return [
  72. EmailField::new('email'),
  73. TextField::new('lastname'),
  74. TextField::new('firstname'),
  75. ChoiceField::new('roles')
  76. ->allowMultipleChoices()
  77. ->autocomplete()
  78. ->setChoices($this->getUserContainer()->getRoleDefinition()->getRolesList())
  79. ];
  80. }
  81. public function createEntity(string $entityFqcn)
  82. {
  83. return $this->get(UserContainer::class)->getFactory()->create();
  84. }
  85. public function delete(AdminContext $context)
  86. {
  87. $entityManager = $this->getEntityManager();
  88. $eaBeforeCrudActionEventDelete = $this->eaBeforeCrudActionEventDelete($context);
  89. if (!is_null($eaBeforeCrudActionEventDelete)) {
  90. return $eaBeforeCrudActionEventDelete;
  91. }
  92. $entityInstance = $context->getEntity()->getInstance();
  93. $event = new BeforeEntityDeletedEvent($entityInstance);
  94. $this->container->get('event_dispatcher')->dispatch($event);
  95. if ($event->isPropagationStopped()) {
  96. return $event->getResponse();
  97. }
  98. $user = $event->getEntityInstance();
  99. $metas = $entityManager->getMetadataFactory()->getAllMetadata();
  100. foreach ($metas as $meta) {
  101. $entityFqcnList[] = $meta->getName();
  102. }
  103. // Creéer formulaire avec un champ confirm
  104. $confirmDeleteUserForm = $this->createForm(ConfirmDeleteUserFormType::class, null, array(
  105. 'action' => $this->getAdminUrlGenerator()->generateUrl()
  106. ));
  107. $confirmDeleteUserForm->handleRequest($context->getRequest());
  108. $entitiesWarning['reductionCart'] = $this->getReductionCartContainer()->getStore()->getByUserOutOfContext($user);
  109. $entitiesWarning['reductionCatalog'] = $this->getReductionCatalogContainer()->getStore()->getByUserOutOfContext($user);
  110. $entitiesWarning['reductionCredit'] = $this->getReductionCreditContainer()->getStore()->getReductionCreditByUserOutOfContext($user);
  111. $entitiesWarning['reductionGift'] = $this->getReductionCreditContainer()->getStore()->getReductionGiftByUserOutOfContext($user);
  112. //si clqiue suppression
  113. $entitiesToDelete = array();
  114. foreach ($entityFqcnList as $entityFqcn) {
  115. if (!is_null($this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn))) {
  116. $repositoryQuery = $this->container->get(
  117. $this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn)
  118. )->getRepositoryQuery();
  119. $entitiesToDelete[$entityFqcn] = $this->getUserContainer()->getBuilder()->getEntitiesToDeleteForUserDelete($user, $entityFqcn, $repositoryQuery);
  120. }
  121. }
  122. $entityManager->delete($entityInstance);
  123. dump($entityManager->getUnitOfWork()->getScheduledEntityDeletions());
  124. // dump($entityManager->getUnitOfWork()->getEntityChangeSet());
  125. dump($entityManager->getUnitOfWork()->getScheduledEntityUpdates());
  126. dump($entityManager->getUnitOfWork()->getScheduledEntityInsertions());
  127. if ($confirmDeleteUserForm->isSubmitted()) {
  128. $entityManager->flush();
  129. $this->get(FlashBagTranslator::class)->add('success', 'deleted', $this->getTranslationEntityName());
  130. $this->deleteEntity($this->container->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
  131. //Todo supprimer les éléments listés
  132. try {
  133. } catch (ForeignKeyConstraintViolationException $e) {
  134. throw new EntityRemoveException(['entity_name' => $context->getEntity()->getName(), 'message' => $e->getMessage()]);
  135. }
  136. return $this->redirect($this->getAdminUrlGenerator()->setAction(Crud::PAGE_INDEX)->setEntityId(null)->generateUrl());
  137. }else{
  138. $entitiesToDelete = $entityManager->getUnitOfWork()->getScheduledEntityDeletions();
  139. $entitiesToUpdate = $entityManager->getUnitOfWork()->getScheduledEntityUpdates();
  140. }
  141. // foreach ($entitiesToDelete as $entityFqcn) {
  142. // foreach ($entityFqcn as $action => $field) {
  143. // if ($action == 'nullify') {
  144. // foreach ($field as $fieldName => $entityList) {
  145. // $method = 'set' . ucfirst($fieldName);
  146. // $methodGet = 'get' . ucfirst($fieldName);
  147. // foreach ($entityList as $entity) {
  148. // $entity->$method(null);
  149. // $this->getEntityManager()->update($entity);
  150. // }
  151. // }
  152. // }
  153. //
  154. //
  155. //// if ($action == 'delete') {
  156. //// foreach ($field as $entity) {
  157. //// dump($entity);
  158. //// $this->getEntityManager()->delete($entity);
  159. //// }
  160. //// }
  161. //
  162. // }
  163. //
  164. // }
  165. //Détecter les merde
  166. //SELECT * FROM address a LEFT OUTER JOIN user u ON(u.id=a.user_id) WHERE u.id is null
  167. $responseParameters = $this->configureResponseParameters(KeyValueStore::new([
  168. 'pageName' => Crud::PAGE_DETAIL,
  169. 'templatePath' => '@LcSov/adminlte/crud/delete.html.twig',
  170. 'confirm_delete_user_form' => $confirmDeleteUserForm->createView(),
  171. 'global_actions' => array(),
  172. 'batch_actions' => array(),
  173. 'entities_warning' => $entitiesWarning,
  174. 'entities_update' => $entitiesToUpdate,
  175. ]));
  176. $event = new AfterCrudActionEvent($context, $responseParameters);
  177. $this->get('event_dispatcher')->dispatch($event);
  178. if ($event->isPropagationStopped()) {
  179. return $event->getResponse();
  180. }
  181. return $responseParameters;
  182. }
  183. }