ComptoirAdmin.php 3.64 KB
Newer Older
Julien Jorry committed
1 2 3 4
<?php

namespace App\Admin;

Julien Jorry committed
5
use App\Entity\Geoloc;
6
use App\Form\Type\GeolocFormType;
Julien Jorry committed
7
use Sonata\AdminBundle\Admin\AbstractAdmin;
8
use Sonata\AdminBundle\Datagrid\DatagridMapper;
Julien Jorry committed
9 10
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
Julien Jorry committed
11
use Sonata\AdminBundle\Route\RouteCollection;
Julien Jorry committed
12
use Sonata\AdminBundle\Show\ShowMapper;
Julien Jorry committed
13
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
14
use Symfony\Component\Security\Core\Security;
Julien Jorry committed
15

16 17 18 19 20 21
/**
 * Administration des comptoirs
 *
 * LOCO : Outil de gestion de Monnaie Locale Complémentaire
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
Julien Jorry committed
22 23
class ComptoirAdmin extends AbstractAdmin
{
24
    protected $security;
Julien Jorry committed
25 26 27 28 29
    protected $datagridValues = [
        '_sort_order' => 'ASC',
        '_sort_by' => 'name',
    ];

30
    public function setSecurity(Security $security)
31
    {
32
        $this->security = $security;
33 34
    }

Julien Jorry committed
35 36 37
    /**
    * {@inheritdoc}
    */
38
    public function createQuery($context = 'list')
Julien Jorry committed
39
    {
40
        $query = parent::createQuery($context);
41
        $user = $this->security->getUser();
42 43 44 45 46 47 48 49 50 51 52 53
        if ($user->isGranted('ROLE_GESTION_GROUPE') || $user->isGranted('ROLE_CONTACT')) {
            if (empty($user->getGroupesgere())) {
                $query->andWhere('false');
            } else {
                $groupe = $user->getGroupesgere();
                $query
                    ->andWhere($query->getRootAliases()[0] . '.groupe = :group')
                    ->setParameter('group', $groupe)
                ;
            }
        }
        return $query;
Julien Jorry committed
54 55 56 57 58 59 60 61
    }

    /**
    * {@inheritdoc}
    */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $comptoir = $this->getSubject();
62 63 64 65
        if ($this->isCurrentRoute('create')) {
            $geoloc = new Geoloc();
            $comptoir->setGeoloc($geoloc);
        }
Julien Jorry committed
66 67
        $formMapper
            ->with('Comptoir', ['class' => 'col-md-8'])
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
                ->add('groupe', null, array(
                    'label' => 'Groupe local',
                    'required' => true,
                ))
                ->add('name', null, array(
                    'label' => 'Nom',
                    'required' => true,
                ))
                ->add('email', null, array(
                    'label' => 'Email',
                    'required' => true,
                ))
                ->add('tel', null, array(
                    'label' => 'Téléphone',
                    'required' => true,
                ))
84
                ->add('enabled', null, array('label' => 'Activé ?'))
Julien Jorry committed
85 86
            ->end()
            ->with('Geoloc', ['class' => 'col-md-4'])
87 88 89 90
                ->add('geoloc', GeolocFormType::class, array(
                    'label' => false,
                    'required' => true
                ))
Julien Jorry committed
91 92 93 94
            ->end()
        ;
    }

95 96 97 98 99 100 101 102 103 104
    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('enabled')
        ;
    }

Julien Jorry committed
105 106 107
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('delete');
Julien Jorry committed
108 109 110 111 112 113 114
    }

    /**
    * {@inheritdoc}
    */
    protected function configureListFields(ListMapper $listMapper)
    {
Julien Jorry committed
115 116 117 118
        unset($this->listModes['mosaic']);
        $listMapper
            ->addIdentifier('name')
            ->addIdentifier('email')
119
            ->addIdentifier('compte', null, array('label' => 'Solde'))
120
            ->addIdentifier('geoloc.adresse')
Julien Jorry committed
121
            ->addIdentifier('tel')
122
            ->addIdentifier('enabled', null, array('label' => 'Activé', 'datatype' => 'App.Comptoir', 'template' => '@SonataAdmin/Boolean/editable_boolean.html.twig'))
Julien Jorry committed
123
        ;
Julien Jorry committed
124 125
    }
}