<?php namespace App\Admin; use App\Entity\AccountAdherent; use App\Entity\Adherent; use App\Entity\Geoloc; use App\Entity\Groupe; use App\Entity\User; use App\Entity\Usergroup; use App\Enum\CurrencyEnum; use App\Events\MLCEvents; use App\Form\Type\GeolocFormType; use App\Form\Type\UserFormType; use FOS\UserBundle\Event\UserEvent; use Knp\Menu\ItemInterface as MenuItemInterface; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Admin\AdminInterface; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; use Sonata\AdminBundle\Form\FormMapper; use Sonata\AdminBundle\Route\RouteCollection; use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter; use Sonata\UserBundle\Model\UserManagerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Security; /** * Administration des adhérents. * * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire * * @author Julien Jorry <julien.jorry@gmail.com> */ class AdherentAdmin extends AbstractAdmin { protected $baseRouteName = 'adherent'; protected $baseRoutePattern = 'adherent'; protected $security; protected $datagridValues = [ // reverse order (default = 'ASC') '_sort_order' => 'DESC', // name of the ordered field (default = the model's id field, if any) '_sort_by' => 'updatedAt', // '_page' => 1, // '_per_page' => 32 ]; public function setSecurity(Security $security) { $this->security = $security; } public function configure() { parent::configure(); } protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface { $user = $this->security->getUser(); $query = parent::configureQuery($query); $query ->innerJoin($query->getRootAliases()[0] . '.user', 'u') ->addSelect('u') ; if ($this->hasRequest()) { if (empty($this->getRequest()->getSession()->get('_groupegere'))) { if ($this->security->isGranted('ROLE_GESTION_GROUPE') || $this->security->isGranted('ROLE_CONTACT')) { $query->andWhere('false = true'); } } else { $query ->andWhere($query->getRootAliases()[0] . '.groupe = :groupe') ->setParameter('groupe', $this->getRequest()->getSession()->get('_groupegere')) ; } } return $query; } protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null) { if (!$childAdmin && !in_array($action, ['edit', 'show'])) { return; } $admin = $this->isChild() ? $this->getParent() : $this; $id = $admin->getRequest()->get('id'); $user = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(User::class)->findOneBy(['adherent' => $id]); if ($this->isGranted('EDIT') && null != $user) { $menu->addChild("Modifier l'utilisateur", [ 'uri' => $this->getConfigurationPool()->getContainer()->get('router')->generate('admin_app_user_edit', ['id' => $user->getId()], UrlGeneratorInterface::ABSOLUTE_URL), ]); } $menu->addChild('Ajouter une cotisation', [ 'uri' => $this->getConfigurationPool()->getContainer()->get('router')->generate('cotisation_adherent_create', ['expediteur' => $id], UrlGeneratorInterface::ABSOLUTE_URL), ]); $menu->addChild('Voir les cotisations', [ 'uri' => $this->getConfigurationPool()->getContainer()->get('router')->generate('cotisation_adherent_list', ['filter' => ['expediteur' => ['value' => $id]]], UrlGeneratorInterface::ABSOLUTE_URL), ]); } /** * {@inheritdoc} */ protected function configureFormFields(FormMapper $formMapper): void { // Initialize adherent $adherent = $this->getSubject(); $now = new \DateTime(); if ($this->isCurrentRoute('create')) { $user = $this->userManager->createUser(); $groupe = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Usergroup::class)->findOneByName('Adherent'); $user->setEnabled(true); $user->addPossiblegroup($groupe); $user->setGroups([$groupe]); $adherent->setEcompte(0); $user->setAdherent($adherent); $adherent->setUser($user); } if (null == $adherent->getGeoloc()) { $adherent->setGeoloc(new Geoloc()); } $formMapper ->tab('General') ->with('Identité', ['class' => 'col-md-7']) ->add('user', UserFormType::class, [ 'label' => false, ]) ->end() ->with('Adresse', ['class' => 'col-md-5']) ->add('geoloc', GeolocFormType::class, [ 'label' => false, 'required' => true, 'with_geoloc' => false, 'with_latlon' => false, ]) ->end() ->with('Groupe', ['class' => 'col-md-5']) ->add('groupe', ChoiceType::class, [ 'required' => true, 'label' => 'Groupe local :', 'choices' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Groupe::class)->findAll(), 'choice_label' => 'name', 'placeholder' => 'Choisir un groupe', ]) ->end() ->end() ; $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager(); $formMapper->getFormBuilder()->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($em) { $adherent = $event->getData(); $user = $adherent->getUser(); if (!$user || null === $user->getId()) { $repo = $em->getRepository(User::class); $emailExist = $repo->findBy(['email' => $user->getEmail()]); if (count($emailExist) > 0) { $event->getForm()->get('user')->get('email')->addError(new FormError('Courriel déjà utilisé !')); } else { $user->setUsername($user->getEmail()); } } }); parent::configureFormFields($formMapper); } public function preUpdate($adherent) { $this->updateAdherent($adherent); } public function prePersist($adherent) { $this->updateAdherent($adherent); $this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_ADHERENT, new UserEvent($adherent->getUser(), $this->getRequest())); } private function updateAdherent($adherent) { $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager(); if (empty($adherent->getUser()->getUsername())) { $adherent->getUser()->setUsername($adherent->getUser()->getEmail()); } if (empty($adherent->getUser()->getPassword())) { // @TODO : generate password with tokengenerator // $tokenGenerator = $this->getConfigurationPool()->getContainer()->get('fos_user.util.token_generator'); // $password = substr($tokenGenerator->generateToken(), 0, 12); $bytes = random_bytes(64); $password = rtrim(strtr(base64_encode($bytes), '+/', '-_'), '='); $adherent->getUser()->setPassword($password); } $this->userManager->updateUser($adherent->getUser()); $adherent->getUser()->createEmailToken(); $account = $em->getRepository(AccountAdherent::class)->findOneBy(['adherent' => $adherent, 'currency' => CurrencyEnum::CURRENCY_EMLC]); if (null == $account) { $account = new AccountAdherent(); $account ->setCurrency(CurrencyEnum::CURRENCY_EMLC) ; $adherent->addAccount($account); $em->persist($account); } $em->persist($adherent->getUser()); $em->persist($adherent); $em->flush(); } /** * {@inheritdoc} */ protected function configureDatagridFilters(DatagridMapper $datagridMapper): void { $datagridMapper ->add('full_text', CallbackFilter::class, [ 'callback' => [$this, 'getFullTextFilter'], 'field_type' => TextType::class, 'label' => 'Recherche par Nom / Prenom / Courriel', 'show_filter' => true, 'advanced_filter' => false, ]) ->add('groupe', null, [ 'label' => 'Groupe', 'show_filter' => true, 'advanced_filter' => false, ]) ; } public function getFullTextFilter($queryBuilder, $alias, $field, $value) { if (!$value['value']) { return; } // Use `andWhere` instead of `where` to prevent overriding existing `where` conditions $queryBuilder->andWhere($queryBuilder->expr()->orX( $queryBuilder->expr()->like('u.username', $queryBuilder->expr()->literal('%' . $value['value'] . '%')), $queryBuilder->expr()->like('u.email', $queryBuilder->expr()->literal('%' . $value['value'] . '%')), $queryBuilder->expr()->like('u.firstname', $queryBuilder->expr()->literal('%' . $value['value'] . '%')), $queryBuilder->expr()->like('u.lastname', $queryBuilder->expr()->literal('%' . $value['value'] . '%')) )); return true; } /** * @param EventDispatcherInterface $userManager */ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void { $this->eventDispatcher = $eventDispatcher; } /** * @return EventDispatcherInterface */ public function getEventDispatcher() { return $this->eventDispatcher; } /** * @param UserManagerInterface $userManager */ public function setUserManager(UserManagerInterface $userManager): void { $this->userManager = $userManager; } /** * @return UserManagerInterface */ public function getUserManager() { return $this->userManager; } protected function configureListFields(ListMapper $listMapper): void { unset($this->listModes['mosaic']); $listMapper ->addIdentifier('user.lastname', null, ['label' => 'Nom']) ->addIdentifier('user.firstname', null, ['label' => 'Prénom']) ->addIdentifier('user.email', null, ['label' => 'Email']) ->add( 'cotisation', null, [ 'label' => 'Cotisation à jour', 'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/list_user_cotisation.html.twig', ] ) ->addIdentifier('groupe', null, [ 'label' => 'Groupe', 'sortable' => true, 'sort_field_mapping' => ['fieldName' => 'name'], 'sort_parent_association_mappings' => [['fieldName' => 'groupe']], ]) ->add('user.enabled', null, [ 'label' => 'Activé', 'editable' => true, ]) ->addIdentifier('user.updatedAt', null, ['label' => 'Mis à jour']) ; } protected function configureRoutes(RouteCollection $collection) { parent::configureRoutes($collection); $collection->remove('delete'); } public function getBatchActions() { $actions = parent::getBatchActions(); unset($actions['delete']); return $actions; } public function getExportFields() { return [ 'Id' => 'id', 'Nom' => 'user.lastname', 'Prénom' => 'user.firstname', 'username' => 'user.username', 'Email' => 'user.email', 'Groupe' => 'groupe.name', 'Téléphone' => 'user.phone', 'Mobile' => 'user.mobile', 'E-compte' => 'ecompte', ]; } }