|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
4
|
|
|
|
|
5
|
|
|
Copyright (C) 2015-2017: Luis Ramón López López |
|
6
|
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
it under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
(at your option) any later version. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
GNU Affero General Public License for more details. |
|
16
|
|
|
|
|
17
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppBundle\Controller\Organization; |
|
22
|
|
|
|
|
23
|
|
|
use AppBundle\Entity\Element; |
|
24
|
|
|
use AppBundle\Entity\ElementRepository; |
|
25
|
|
|
use AppBundle\Form\Type\ElementType; |
|
26
|
|
|
use AppBundle\Security\OrganizationVoter; |
|
27
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
28
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
|
29
|
|
|
use Pagerfanta\Pagerfanta; |
|
30
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
31
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
32
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @Route("/centro/elementos") |
|
36
|
|
|
*/ |
|
37
|
|
|
class ElementController extends Controller |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @Route("/listar/{page}/{path}", name="organization_element_list", requirements={"page" = "\d+", "path" = ".+"}, defaults={"page" = "1", "path" = null}, methods={"GET"}) |
|
41
|
|
|
*/ |
|
42
|
|
|
public function listAction($page, $path = null, Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
$organization = $this->get('AppBundle\Service\UserExtensionService')->getCurrentOrganization(); |
|
45
|
|
|
$this->denyAccessUnlessGranted(OrganizationVoter::MANAGE, $organization); |
|
46
|
|
|
|
|
47
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
48
|
|
|
|
|
49
|
|
|
$elementRepository = $em->getRepository('AppBundle:Element'); |
|
50
|
|
|
$element = $this->getSelectedElement($path, $elementRepository, $organization); |
|
51
|
|
|
|
|
52
|
|
|
/** @var QueryBuilder $queryBuilder */ |
|
53
|
|
|
$queryBuilder = $elementRepository->getChildrenQueryBuilder($element, true) |
|
54
|
|
|
->addSelect('ref') |
|
55
|
|
|
->leftJoin('node.references', 'ref'); |
|
56
|
|
|
|
|
57
|
|
|
$q = $request->get('q', null); |
|
58
|
|
|
if ($q) { |
|
59
|
|
|
$queryBuilder |
|
60
|
|
|
->andWhere('node.name LIKE :tq') |
|
61
|
|
|
->setParameter('tq', '%'.$q.'%'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$adapter = new DoctrineORMAdapter($queryBuilder, false); |
|
65
|
|
|
$pager = new Pagerfanta($adapter); |
|
66
|
|
|
$pager |
|
67
|
|
|
->setMaxPerPage($this->getParameter('page.size')) |
|
68
|
|
|
->setCurrentPage($page); |
|
69
|
|
|
|
|
70
|
|
|
$breadcrumb = $this->generateBreadcrumb($element); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return $this->render('organization/element/list.html.twig', [ |
|
73
|
|
|
'breadcrumb' => $breadcrumb, |
|
74
|
|
|
'title' => $element->getName(), |
|
75
|
|
|
'elements' => $pager->getIterator(), |
|
76
|
|
|
'pager' => $pager, |
|
77
|
|
|
'current' => $element, |
|
78
|
|
|
'q' => $q, |
|
79
|
|
|
'domain' => 'element' |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @Route("/operar/{path}", name="organization_element_operation", requirements={"path" = ".+"}, defaults={"path" = null}, methods={"POST"}) |
|
85
|
|
|
*/ |
|
86
|
|
|
public function operationAction($path = null, Request $request) |
|
87
|
|
|
{ |
|
88
|
|
|
$organization = $this->get('AppBundle\Service\UserExtensionService')->getCurrentOrganization(); |
|
89
|
|
|
$this->denyAccessUnlessGranted(OrganizationVoter::MANAGE, $organization); |
|
90
|
|
|
|
|
91
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
92
|
|
|
|
|
93
|
|
|
$elementRepository = $em->getRepository('AppBundle:Element'); |
|
94
|
|
|
$element = $this->getSelectedElement($path, $elementRepository, $organization); |
|
95
|
|
|
|
|
96
|
|
|
$ok = false; |
|
97
|
|
View Code Duplication |
if ($request->get('up')) { |
|
|
|
|
|
|
98
|
|
|
$item = $em->getRepository('AppBundle:Element')->find($request->get('up')); |
|
99
|
|
|
if (null === $item || $item->getParent() !== $element) { |
|
100
|
|
|
throw $this->createNotFoundException(); |
|
101
|
|
|
} |
|
102
|
|
|
$em->getRepository('AppBundle:Element')->moveUp($item); |
|
103
|
|
|
$ok = true; |
|
104
|
|
|
} |
|
105
|
|
View Code Duplication |
if ($request->get('down')) { |
|
|
|
|
|
|
106
|
|
|
$item = $em->getRepository('AppBundle:Element')->find($request->get('down')); |
|
107
|
|
|
if (null === $item || $item->getParent() !== $element) { |
|
108
|
|
|
throw $this->createNotFoundException(); |
|
109
|
|
|
} |
|
110
|
|
|
$em->getRepository('AppBundle:Element')->moveDown($item); |
|
111
|
|
|
$ok = true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$items = $request->request->get('elements', []); |
|
115
|
|
|
if ($ok || count($items) === 0) { |
|
116
|
|
|
return $this->redirectToRoute('organization_element_list', ['path' => $path]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$elements = $em->createQueryBuilder() |
|
120
|
|
|
->select('e') |
|
121
|
|
|
->from('AppBundle:Element', 'e') |
|
122
|
|
|
->where('e.id IN (:items)') |
|
123
|
|
|
->andWhere('e.parent = :current') |
|
124
|
|
|
->andWhere('e.code IS NULL') |
|
125
|
|
|
->setParameter('items', $items) |
|
126
|
|
|
->setParameter('current', $element) |
|
127
|
|
|
->orderBy('e.left') |
|
128
|
|
|
->getQuery() |
|
129
|
|
|
->getResult(); |
|
130
|
|
|
|
|
131
|
|
|
if ($request->get('confirm', '') === 'ok') { |
|
132
|
|
|
try { |
|
133
|
|
|
$em->createQueryBuilder() |
|
134
|
|
|
->delete('AppBundle:Element', 'e') |
|
135
|
|
|
->where('e.id IN (:items)') |
|
136
|
|
|
->andWhere('e.parent = :current') |
|
137
|
|
|
->andWhere('e.code IS NULL') |
|
138
|
|
|
->setParameter('items', $items) |
|
139
|
|
|
->setParameter('current', $element) |
|
140
|
|
|
->getQuery() |
|
141
|
|
|
->execute(); |
|
142
|
|
|
|
|
143
|
|
|
$em->flush(); |
|
144
|
|
|
$this->addFlash('success', $this->get('translator')->trans('message.deleted', [], 'element')); |
|
145
|
|
|
} catch (\Exception $e) { |
|
146
|
|
|
$this->addFlash('error', $this->get('translator')->trans('message.delete_error', [], 'element')); |
|
147
|
|
|
} |
|
148
|
|
|
return $this->redirectToRoute('organization_element_list', ['path' => $path]); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$title = $this->get('translator')->trans('title.delete', [], 'element'); |
|
152
|
|
|
$breadcrumb = $this->generateBreadcrumb($element, false); |
|
153
|
|
|
$breadcrumb[] = ['fixed' => $this->get('translator')->trans('title.delete', [], 'element')]; |
|
154
|
|
|
|
|
155
|
|
|
return $this->render('organization/element/delete.html.twig', [ |
|
156
|
|
|
'menu_path' => 'organization_element_list', |
|
157
|
|
|
'breadcrumb' => $breadcrumb, |
|
158
|
|
|
'title' => $title, |
|
159
|
|
|
'elements' => $elements |
|
160
|
|
|
]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @Route("/carpeta/{path}", name="organization_element_folder_new", requirements={"path" = ".+"}, methods={"GET", "POST"}) |
|
165
|
|
|
* @Route("/nuevo/{path}", name="organization_element_new", requirements={"path" = ".+"}, methods={"GET", "POST"}) |
|
166
|
|
|
* @Route("/modificar/{path}", name="organization_element_form", requirements={"path" = ".+"}, methods={"GET", "POST"}) |
|
167
|
|
|
*/ |
|
168
|
|
|
public function formAction($path, Request $request) |
|
169
|
|
|
{ |
|
170
|
|
|
$organization = $this->get('AppBundle\Service\UserExtensionService')->getCurrentOrganization(); |
|
171
|
|
|
$this->denyAccessUnlessGranted(OrganizationVoter::MANAGE, $organization); |
|
172
|
|
|
|
|
173
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
174
|
|
|
|
|
175
|
|
|
/** @var Element|null $element */ |
|
176
|
|
|
if (null === $element = $em->getRepository('AppBundle:Element')->findOneByOrganizationAndPath($organization, $path)) { |
|
177
|
|
|
throw $this->createNotFoundException(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$new = in_array($request->get('_route'), ['organization_element_new', 'organization_element_folder_new'], true); |
|
181
|
|
|
$breadcrumb = $this->generateBreadcrumb($element, !$new); |
|
182
|
|
|
|
|
183
|
|
|
if ($new) { |
|
184
|
|
|
$newElement = new Element(); |
|
185
|
|
|
$newElement |
|
186
|
|
|
->setParent($element) |
|
187
|
|
|
->setOrganization($organization) |
|
188
|
|
|
->setFolder($request->get('_route') === 'organization_element_folder_new'); |
|
189
|
|
|
|
|
190
|
|
|
$em->persist($newElement); |
|
191
|
|
|
|
|
192
|
|
|
$element = $newElement; |
|
193
|
|
|
|
|
194
|
|
|
$title = $this->get('translator')->trans($newElement->isFolder() ? 'title.new_folder' : 'title.new', [], 'element'); |
|
195
|
|
|
$breadcrumb[] = ['fixed' => $title]; |
|
196
|
|
|
} else { |
|
197
|
|
|
$title = $this->get('translator')->trans('title.edit', [], 'element'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$form = $this->createForm(ElementType::class, $element); |
|
201
|
|
|
|
|
202
|
|
|
$form->handleRequest($request); |
|
203
|
|
|
|
|
204
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
205
|
|
|
try { |
|
206
|
|
|
$em->flush(); |
|
207
|
|
|
$this->addFlash('success', $this->get('translator')->trans('message.saved', [], 'element')); |
|
208
|
|
|
return $this->redirectToRoute('organization_element_list', ['page' => 1, 'path' => $element->getParent()->getPath()]); |
|
209
|
|
|
} catch (\Exception $e) { |
|
210
|
|
|
$this->addFlash('error', $this->get('translator')->trans('message.save_error', [], 'element')); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return $this->render('organization/element/form.html.twig', [ |
|
215
|
|
|
'menu_path' => 'organization_element_list', |
|
216
|
|
|
'breadcrumb' => $breadcrumb, |
|
217
|
|
|
'title' => $title, |
|
218
|
|
|
'element' => $element, |
|
219
|
|
|
'form' => $form->createView() |
|
220
|
|
|
]); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Returns breadcrumb that matches the element (ignores root element) |
|
225
|
|
|
* @param Element|null $element |
|
226
|
|
|
* @param bool $ignoreLast |
|
227
|
|
|
* @return array |
|
228
|
|
|
*/ |
|
229
|
|
|
private function generateBreadcrumb(Element $element, $ignoreLast = true) |
|
230
|
|
|
{ |
|
231
|
|
|
$breadcrumb = []; |
|
232
|
|
|
|
|
233
|
|
|
if (null !== $element) { |
|
234
|
|
|
$item = $element; |
|
235
|
|
|
while ($item->getParent()) { |
|
236
|
|
|
$entry = ['fixed' => $item->getName()]; |
|
237
|
|
|
if ($item !== $element || !$ignoreLast) { |
|
238
|
|
|
$entry['routeName'] = 'organization_element_list'; |
|
239
|
|
|
$entry['routeParams'] = ['path' => $item->getPath()]; |
|
240
|
|
|
} |
|
241
|
|
|
array_unshift($breadcrumb, $entry); |
|
242
|
|
|
$item = $item->getParent(); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
return $breadcrumb; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param $path |
|
250
|
|
|
* @param ElementRepository $elementRepository |
|
251
|
|
|
* @param $organization |
|
252
|
|
|
* @return Element |
|
253
|
|
|
*/ |
|
254
|
|
|
private function getSelectedElement($path, ElementRepository $elementRepository, $organization) |
|
255
|
|
|
{ |
|
256
|
|
|
/** @var Element|null $element */ |
|
257
|
|
|
if (null !== $path) { |
|
258
|
|
|
$element = $elementRepository->findOneByOrganizationAndPath($organization, $path); |
|
259
|
|
|
if (null === $element) { |
|
260
|
|
|
throw $this->createNotFoundException(); |
|
261
|
|
|
} |
|
262
|
|
|
} else { |
|
263
|
|
|
$element = $elementRepository->findCurrentOneByOrganization($organization); |
|
264
|
|
|
} |
|
265
|
|
|
return $element; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: