1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
143
144
<?php
namespace App\Admin;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Security\Core\Security;
class GroupeprestataireAdmin extends AbstractAdmin
{
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',
];
public function setSecurity(Security $security)
{
$this->security = $security;
}
/**
* {@inheritdoc}
*/
public function createQuery($context = 'list')
{
$user = $this->security->getUser();
$query = parent::createQuery($context);
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.prestataire_id OR a.id = f.prestataire_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)
;
}
}
return $query;
}
/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $formMapper)
{
$groupepresta = $this->getSubject();
$formMapper
->add('type', TextType::class, array(
'label' => 'Type (marche/amap) :',
'required' => true
))
->add('name', TextType::class, array(
'label' => 'Nom du groupe :',
'required' => true
))
->add('content', CKEditorType::class, array(
'label' => 'Texte',
'required' => false,
))
->add('horaires', TextareaType::class, array(
'label' => 'Horaires :',
'required' => false
))
->add('enabled', CheckboxType::class, array(
'label' => 'Activé ?',
'required' => false,
'label_attr' => array('class' => 'checkbox-inline')
))
;
}
/**
* {@inheritdoc}
*/
protected function configureListFields(ListMapper $listMapper)
{
unset($this->listModes['mosaic']);
$listMapper
->addIdentifier('name', null, array(
'label' => 'Nom du groupe'
))
->addIdentifier('type', null, array(
'label' => 'Type'
))
->addIdentifier('horaires', 'html', array(
'label' => 'Horaires',
'strip' => true,
'truncate' => 50
))
->add(
'getPrestatairesCount',
null,
[
'sortable' => true,
'sort_field_mapping' => ['fieldName' => 'id'],
'sort_parent_association_mappings' => [],
]
)
->addIdentifier('enabled', null, array(
'label' => 'Activé',
'datatype' => 'App.Groupeprestataire',
'template' => '@SonataAdmin/Boolean/editable_boolean.html.twig'
))
;
}
// @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;
// }
}