1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Admin; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
6
|
|
|
use Knp\Menu\ItemInterface as MenuItemInterface; |
7
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
8
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
9
|
|
|
use Sonata\AdminBundle\Route\RouteCollection; |
10
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
11
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
12
|
|
|
|
13
|
|
|
class AdminNode extends BaseAdmin |
14
|
|
|
{ |
15
|
|
|
protected $baseRouteName = 'alpixel_admin_cms_node'; |
16
|
|
|
protected $baseRoutePattern = 'node'; |
17
|
|
|
protected $classnameLabel = 'pages'; |
18
|
|
|
|
19
|
|
|
protected $datagridValues = [ |
20
|
|
|
'_page' => 1, |
21
|
|
|
'_sort_order' => 'DESC', |
22
|
|
|
'_sort_by' => 'dateUpdated', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
protected function configureRoutes(RouteCollection $collection) |
26
|
|
|
{ |
27
|
|
|
$collection->clearExcept(['list', 'batch', 'delete']); |
28
|
|
|
$collection->add('forwardEdit'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
37
|
|
|
{ |
38
|
|
|
$container = $this->getConfigurationPool()->getContainer(); |
39
|
|
|
$entityManager = $container->get('doctrine.orm.default_entity_manager'); |
40
|
|
|
$datagridMapper |
41
|
|
|
->add('locale', 'doctrine_orm_callback', [ |
42
|
|
|
'label' => 'Langue', |
43
|
|
|
'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) { |
44
|
|
|
if (!$value['value']) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
$queryBuilder |
48
|
|
|
->andWhere($alias.'.locale = :locale') |
49
|
|
|
->setParameter('locale', $value['value']); |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
}, |
53
|
|
|
], |
54
|
|
|
'choice', |
55
|
|
|
[ |
56
|
|
|
'choices' => $this->getRealLocales(), |
57
|
|
|
]) |
58
|
|
|
->add('title', null, [ |
59
|
|
|
'label' => 'Page', |
60
|
|
|
]) |
61
|
|
|
->add('published', null, [ |
62
|
|
|
'label' => 'Publié', |
63
|
|
|
]) |
64
|
|
|
->add('node', 'doctrine_orm_callback', [ |
65
|
|
|
'label' => 'Type de contenu', |
66
|
|
|
'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) use ($entityManager) { |
67
|
|
|
if (!$value['value']) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
// We can't query the type from the AlpixelCMSBundle:Node repository (InheritanceType) because of that |
71
|
|
|
// we try to get the repository in AppBundle with the value which is the class name of entity. :pig: |
72
|
|
|
try { |
73
|
|
|
$repository = $entityManager->getRepository(sprintf('AppBundle:%s', ucfirst($value['value']))); |
74
|
|
|
} catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
$data = $repository->findAll(); |
78
|
|
|
if (empty($data)) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
$queryBuilder |
82
|
|
|
->andWhere($alias.'.id IN (:ids)') |
83
|
|
|
->setParameter('ids', $data); |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
}, |
87
|
|
|
], |
88
|
|
|
'choice', |
89
|
|
|
[ |
90
|
|
|
'choices' => $this->getCMSEntityTypes(), |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function configureListFields(ListMapper $listMapper) |
101
|
|
|
{ |
102
|
|
|
$listMapper |
103
|
|
|
->add('id') |
104
|
|
|
->add('locale', null, [ |
105
|
|
|
'label' => 'Langue', |
106
|
|
|
]) |
107
|
|
|
->add('title', null, [ |
108
|
|
|
'label' => 'Page', |
109
|
|
|
]) |
110
|
|
|
->add('type', null, [ |
111
|
|
|
'label' => 'Type', |
112
|
|
|
'template' => 'AlpixelCMSBundle:admin:fields/list__field_type.html.twig', |
113
|
|
|
]) |
114
|
|
|
->add('dateCreated', null, [ |
115
|
|
|
'label' => 'Date de création', |
116
|
|
|
]) |
117
|
|
|
->add('dateUpdated', null, [ |
118
|
|
|
'label' => 'Date d\'édition', |
119
|
|
|
]) |
120
|
|
|
->add('published', null, [ |
121
|
|
|
'label' => 'Publié', |
122
|
|
|
]) |
123
|
|
|
->add('_action', 'actions', [ |
124
|
|
|
'actions' => [ |
125
|
|
|
'see' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_see.html.twig'], |
126
|
|
|
'edit' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_edit.html.twig'], |
127
|
|
|
'delete' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_delete.html.twig'], |
128
|
|
|
], |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function buildBreadcrumbs($action, MenuItemInterface $menu = null) |
133
|
|
|
{ |
134
|
|
|
if (isset($this->breadcrumbs[$action])) { |
135
|
|
|
return $this->breadcrumbs[$action]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$menu = $this->menuFactory->createItem('root'); |
139
|
|
|
|
140
|
|
|
$menu = $menu->addChild('Dashboard', |
141
|
|
|
['uri' => $this->routeGenerator->generate('sonata_admin_dashboard')] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$menu = $menu->addChild('Gestion des pages', |
145
|
|
|
['uri' => $this->routeGenerator->generate('alpixel_admin_cms_node_list')] |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
return $this->breadcrumbs[$action] = $menu; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function createQuery($context = 'list') |
152
|
|
|
{ |
153
|
|
|
$container = $this->getConfigurationPool()->getContainer(); |
154
|
|
|
$entityManager = $container->get('doctrine.orm.entity_manager'); |
155
|
|
|
|
156
|
|
|
$query = parent::createQuery($context); |
157
|
|
|
|
158
|
|
|
if ($this->isGranted('ROLE_SONATA_ADMIN') === false) { |
159
|
|
|
$contentTypes = $this->getCMSTypes(); |
160
|
|
|
|
161
|
|
|
$viewableCMS = []; |
162
|
|
|
foreach ($contentTypes as $key => $contentType) { |
163
|
|
|
try { |
164
|
|
|
if (isset($contentType['admin'])) { |
165
|
|
|
$contentType['admin']->checkAccess('list'); //Throw an exception if doesn' have access |
166
|
|
|
$viewableCMS[$key] = $contentType; |
167
|
|
|
} |
168
|
|
|
} catch (AccessDeniedException $e) { |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$queryBuilder = clone $query; |
173
|
|
|
/* @var QueryBuilder $queryBuilder */ |
174
|
|
|
|
175
|
|
|
$orX = $queryBuilder->expr()->orX(); |
176
|
|
|
$orX->add($queryBuilder->expr()->eq('2', '1')); |
177
|
|
|
|
178
|
|
|
foreach ($viewableCMS as $key => $viewableContent) { |
179
|
|
|
$nodes = $entityManager->getRepository($viewableContent['class'])->findAll(); |
180
|
|
|
$nodesId = []; |
181
|
|
|
|
182
|
|
|
foreach ($nodes as $node) { |
183
|
|
|
$nodesId[] = $node->getId(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (count($nodesId) > 0) { |
187
|
|
|
$orX->add($queryBuilder->expr()->in($queryBuilder->getRootAlias() . '.id', $nodesId)); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$orX->add($queryBuilder->expr()->in($queryBuilder->getRootAlias().'.id', $nodesId)); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
$queryBuilder->andWhere($orX); |
193
|
|
|
|
194
|
|
|
return $queryBuilder; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $query; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.