CotisationAdherentAdmin.php 4.64 KB
Newer Older
Julien Jorry committed
1 2 3 4
<?php

namespace App\Admin;

5 6
use App\Entity\Adherent;
use App\Entity\User;
7
use Sonata\AdminBundle\Datagrid\DatagridMapper;
Julien Jorry committed
8 9
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
Julien Jorry committed
10
use Sonata\AdminBundle\Route\RouteCollection;
Julien Jorry committed
11
use Sonata\AdminBundle\Show\ShowMapper;
12 13
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
Julien Jorry committed
14

15 16 17 18 19 20
/**
 * Administration des cotisations des adhérents
 *
 * LOCO : Outil de gestion de Monnaie Locale Complémentaire
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
Julien Jorry committed
21 22
class CotisationAdherentAdmin extends CotisationAdmin
{
Julien Jorry committed
23 24 25
    protected $baseRouteName = 'cotisation_adherent';
    protected $baseRoutePattern = 'cotisation_adherent';

26 27 28 29 30
    public function configure()
    {
        parent::configure();
    }

Julien Jorry committed
31 32 33 34 35
    /**
    * {@inheritdoc}
    */
    public function createQuery($context = 'list')
    {
36
        $user = $this->security->getUser();
Julien Jorry committed
37
        $query = parent::createQuery($context);
38
        $query->leftJoin($query->getRootAliases()[0] . '.operateur', 'u')
39 40
              ->andWhere($query->getRootAliases()[0] .".type='cotisation_adherent'")
              // ->andWhere('u.adherent IS NOT NULL')
41
        ;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        if ($user->isGranted('ROLE_GESTION_GROUPE') || $user->isGranted('ROLE_CONTACT')) {
            if (empty($user->getGroupesgere())) {
                $query->andWhere('false');
            } else {
                $groupe = $user->getGroupesgere();
                $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getEntityManager();
                $connection = $em->getConnection();
                $statement = $connection->prepare('SELECT f.id FROM flux f INNER JOIN adherent a ON (a.id = f.adherent_id OR a.id = f.adherent_dest_id) WHERE a.groupe_id = '.$groupe->getId());
                $statement->execute();
                $ids = $statement->fetchAll();
                $query
                    ->andWhere($query->expr()->in($query->getRootAliases()[0].'.id', ':ids'))
                    ->setParameter('ids', $ids)
                ;
            }
        }
Julien Jorry committed
58 59 60 61 62 63 64 65 66 67 68
        return $query;
    }

    /**
    * {@inheritdoc}
    */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        parent::configureShowFields($showMapper);
    }

69 70 71 72 73 74
    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        parent::configureDatagridFilters($datagridMapper);
75 76 77
        $datagridMapper
            ->add('expediteur', null, array('label' => 'Adhérent'))
        ;
78 79
    }

Julien Jorry committed
80 81 82 83 84
    /**
    * {@inheritdoc}
    */
    protected function configureFormFields(FormMapper $formMapper): void
    {
85 86 87 88 89 90 91 92 93 94 95
        $expediteurInfos = array(
            'label' => 'Expéditeur',
            'class' => Adherent::class,
            'choices' =>  $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Adherent::class)->findOrderByName(),
            'placeholder' => 'Choisir un adhérent',
            'required' => true,
        );
        $exp = $this->getRequest()->get('expediteur');
        if (!empty($exp)) {
            $expediteurInfos['data'] = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Adherent::class)->findOneById($exp);
        }
Julien Jorry committed
96 97
        $formMapper
            ->with('Cotisation', ['class' => 'col-md-8'])
98 99 100 101 102 103
                ->add('reference', HiddenType::class, array(
                    'data' => 'cotisation_adherent'
                ))
                ->add('type', HiddenType::class, array(
                    'data' => 'cotisation_adherent'
                ))
104
                ->add('expediteur', EntityType::class, $expediteurInfos)
105 106 107 108
                ->add('operateur', HiddenType::class, array(
                    'data' => $this->security->getUser()->getId(),
                    'entity_class' => User::class,
                    'em' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager()
109
                ))
Julien Jorry committed
110 111
            ->end()
        ;
Julien Jorry committed
112
        parent::configureFormFields($formMapper);
Julien Jorry committed
113 114 115 116 117 118 119 120
    }

    /**
    * {@inheritdoc}
    */
    protected function configureRoutes(RouteCollection $collection)
    {
        parent::configureRoutes($collection);
Julien Jorry committed
121 122 123 124 125 126 127
    }

    /**
    * {@inheritdoc}
    */
    protected function configureListFields(ListMapper $listMapper): void
    {
128 129
        unset($this->listModes['mosaic']);
        $listMapper
130 131 132 133 134 135 136
            ->addIdentifier('expediteur', null, array(
                'label' => 'Adherent'
            ))
            ->addIdentifier('expediteur.user.email', null, array(
                'label' => 'Email'
            ))
        ;
Julien Jorry committed
137 138 139
        parent::configureListFields($listMapper);
    }
}