GroupeprestataireAdmin.php 8.22 KB
Newer Older
Julien Jorry committed
1 2 3 4
<?php

namespace App\Admin;

Julien Jorry committed
5 6
use App\Entity\Prestataire;
use App\Enum\GroupePrestaEnum;
7
use FOS\CKEditorBundle\Form\Type\CKEditorType;
Julien Jorry committed
8
use Sonata\AdminBundle\Admin\AbstractAdmin;
Julien Jorry committed
9
use Sonata\AdminBundle\Datagrid\DatagridMapper;
Julien Jorry committed
10 11
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
Julien Jorry committed
12
use Sonata\AdminBundle\Route\RouteCollection;
Julien Jorry committed
13
use Sonata\AdminBundle\Show\ShowMapper;
Julien Jorry committed
14 15
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
Julien Jorry committed
16
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Julien Jorry committed
17 18
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
Julien Jorry committed
19
use Symfony\Component\Form\Extension\Core\Type\TextType;
20
use Symfony\Component\Security\Core\Security;
Julien Jorry committed
21

22 23 24 25 26 27
/**
 * Administration des groupe de prestataires (amap, marche)
 *
 * LOCO : Outil de gestion de Monnaie Locale Complémentaire
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
Julien Jorry committed
28 29
class GroupeprestataireAdmin extends AbstractAdmin
{
30
    protected $security;
Julien Jorry committed
31 32 33 34 35 36 37
    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',
    ];

Julien Jorry committed
38 39 40 41 42
    public function configure()
    {
        parent::configure();
    }

43
    public function setSecurity(Security $security)
44
    {
45
        $this->security = $security;
46 47
    }

Julien Jorry committed
48 49 50
    /**
    * {@inheritdoc}
    */
51
    public function createQuery($context = 'list')
Julien Jorry committed
52
    {
53
        $user = $this->security->getUser();
54 55 56 57 58 59
        $query = parent::createQuery($context);
        if ($user->isGranted('ROLE_GESTION_GROUPE') || $user->isGranted('ROLE_CONTACT')) {
            if (empty($user->getGroupesgere())) {
                $query->andWhere('false');
            } else {
                $query
Julien Jorry committed
60 61
                    ->andWhere($query->getRootAliases()[0].'.groupe = :groupe')
                    ->setParameter('groupe', $user->getGroupesgere())
62 63 64
                ;
            }
        }
Julien Jorry committed
65 66
        // dump($query->getQuery());
        // exit();
67
        return $query;
Julien Jorry committed
68 69
    }

70

Julien Jorry committed
71 72 73 74 75
    /**
    * {@inheritdoc}
    */
    protected function configureFormFields(FormMapper $formMapper)
    {
Julien Jorry committed
76
        $user = $this->security->getUser();
77
        $prestataires = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Prestataire::class)->findBy(array('enabled' => true), array('raison'=> 'ASC'));
Julien Jorry committed
78
        $groupepresta = $this->getSubject();
Julien Jorry committed
79 80 81
        if ($this->isCurrentRoute('create') && $user->getGroupesgere() != null) {
            $groupepresta->setGroupe($user->getGroupesgere());
        }
Julien Jorry committed
82
        $formMapper
Julien Jorry committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96
            ->with('Informations', ['class' => 'col-md-7'])
                ->add('type', ChoiceType::class, array(
                    'required' => true,
                    'choices' => GroupePrestaEnum::getAvailableTypes(),
                    'choice_label' => function ($choice) {
                        return GroupePrestaEnum::getTypeName($choice);
                    },
                ))
                ->add('name', TextType::class, array(
                    'label' => 'Nom du groupe :',
                    'required' => true
                ))
        ;
        if (($user->isGranted('ROLE_GESTION_GROUPE') || $user->isGranted('ROLE_CONTACT')) && $this->security->getUser()->getGroupesgere() != null) {
97
            $prestataires = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Prestataire::class)->findBy(array('enabled' => true, 'groupe' => $this->security->getUser()->getGroupesgere()), array('raison'=> 'ASC'));
Julien Jorry committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        }
        if ($user->isGranted('ROLE_SUPER_ADMIN') || $user->isGranted('ROLE_ADMIN_SIEGE')) {
            $formMapper
                ->add('groupe', null, array(
                    'label' => 'Groupe local',
                    'required' => true,
                ))
            ;
        }
        $formMapper
                ->add('horaires', CKEditorType::class, array(
                    'label' => 'Horaires :',
                    'required' => false
                ))
                ->add('content', CKEditorType::class, array(
                    'label' => 'Texte',
                    'required' => false,
                ))
                ->add('enabled', CheckboxType::class, array(
                    'label' => 'Activé ?',
                    'required' => false,
                    'label_attr' => array('class' => 'checkbox-inline')
                ))
            ->end()
            ->with('Image', ['class' => 'col-md-5'])
                ->add('image', MediaType::class, array(
                    'provider' => 'sonata.media.provider.image',
                    'context' => 'groupe',
                    'label' => 'Image'
                ))
            ->end()
            ->with('Prestataires', ['class' => 'col-md-5'])
            ->add('prestataires', CollectionType::class, array(
                'label' => 'Prestataires',
                'entry_type' => EntityType::class,
                'entry_options' => array(
                    'class' => Prestataire::class,
                    'choices' =>  $prestataires,
                    // 'choice_label' => 'name',
                    'placeholder' => 'Choisir un prestataire',
                    'required' => false,
                    'label' => false),
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' => true
Julien Jorry committed
143 144 145 146
            ))
        ;
    }

Julien Jorry committed
147 148 149 150 151
    /**
    * {@inheritdoc}
    */
    protected function configureListFields(ListMapper $listMapper)
    {
Julien Jorry committed
152
        $user = $this->security->getUser();
Julien Jorry committed
153
        unset($this->listModes['mosaic']);
Julien Jorry committed
154 155 156 157 158 159 160
        if ($user->isGranted('ROLE_SUPER_ADMIN') || $user->isGranted('ROLE_ADMIN_SIEGE')) {
            $listMapper
                ->addIdentifier('groupe', null, array(
                    'label' => 'Groupe gestionnaire'
                ))
            ;
        }
Julien Jorry committed
161
        $listMapper
162 163 164
            ->addIdentifier('name', null, array(
                'label' => 'Nom du groupe'
            ))
165 166 167
            ->addIdentifier('type', null, array(
                'label' => 'Type'
            ))
168 169 170 171 172
            ->addIdentifier('horaires', 'html', array(
                'label' => 'Horaires',
                'strip' => true,
                'truncate' => 50
            ))
Julien Jorry committed
173 174 175 176 177 178 179 180 181
            ->add(
                'getPrestatairesCount',
                null,
                [
                    'sortable' => true,
                    'sort_field_mapping' => ['fieldName' => 'id'],
                    'sort_parent_association_mappings' => [],
                ]
            )
182 183 184 185 186
            ->addIdentifier('enabled', null, array(
                'label' => 'Activé',
                'datatype' => 'App.Groupeprestataire',
                'template' => '@SonataAdmin/Boolean/editable_boolean.html.twig'
            ))
Julien Jorry committed
187
        ;
Julien Jorry committed
188
    }
Julien Jorry committed
189

Julien Jorry committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    protected function configureRoutes(RouteCollection $collection)
    {
        parent::configureRoutes($collection);
        $collection->remove('delete');
    }

    public function getBatchActions()
    {
        $actions = parent::getBatchActions();
        unset($actions['delete']);

        return $actions;
    }

    public function getDashboardActions()
    {
        $actions = parent::getDashboardActions();
        unset($actions['list']);
        return $actions;
    }

Julien Jorry committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    // @TODO : NE FONCTIONNE PAS : pour pouvoir classer par nombre de prestataires !
    // voir https://github.com/sonata-project/SonataDoctrineORMAdminBundle/issues/297
    // public function createQuery($context = 'list')
    // {
    //     $query = parent::createQuery($context);
    //     if ('list' === $context) {
    //         $rootAlias = $query->getRootAliases()[0];
    //         $parameters = $this->getFilterParameters();
    //         if ('getPrestatairesCount' === $parameters['_sort_by']) {
    //             $query->select('o', 'DISTINCT o');
    //             $query
    //                 ->leftJoin($rootAlias.'.prestataires', 'gpr')
    //                 // ->leftJoin('groupes_prestataires.prestataires', 'pr')
    //                 ->groupBy($rootAlias.'.id')
    //                 ->orderBy('COUNT(gpr.id)', $parameters['_sort_order'])
    //             ;
    //         }
    //     }
    //     return $query;
    // }
Julien Jorry committed
231
}