1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Controller; |
33
|
|
|
|
34
|
|
|
use App\Entity\AttachmentType; |
35
|
|
|
use App\Entity\StructuralDBElement; |
36
|
|
|
use App\Form\BaseEntityAdminForm; |
37
|
|
|
use App\Form\ImportType; |
38
|
|
|
use App\Services\EntityExporter; |
39
|
|
|
use App\Services\EntityImporter; |
40
|
|
|
use App\Services\StructuralElementRecursionHelper; |
41
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
42
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
43
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
44
|
|
|
use Symfony\Component\HttpFoundation\Request; |
45
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
46
|
|
|
|
47
|
|
|
abstract class BaseAdminController extends AbstractController |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
protected $entity_class = ""; |
51
|
|
|
protected $form_class = ""; |
52
|
|
|
protected $twig_template = ""; |
53
|
|
|
protected $route_base = ""; |
54
|
|
|
|
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
if ($this->entity_class === "" || $this->form_class === "" || $this->twig_template === "" || $this->route_base === "") { |
|
|
|
|
58
|
|
|
throw new \InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function _edit(StructuralDBElement $entity, Request $request, EntityManagerInterface $em) |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
$this->denyAccessUnlessGranted('read', $entity); |
66
|
|
|
|
67
|
|
|
$form = $this->createForm($this->form_class, $entity); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$form->handleRequest($request); |
70
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
71
|
|
|
$em->persist($entity); |
72
|
|
|
$em->flush(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->render($this->twig_template, [ |
|
|
|
|
76
|
|
|
'entity' => $entity, |
77
|
|
|
'form' => $form->createView() |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function _new(Request $request, EntityManagerInterface $em, EntityImporter $importer) |
82
|
|
|
{ |
83
|
|
|
/** @var StructuralDBElement $new_entity */ |
84
|
|
|
$new_entity = new $this->entity_class(); |
85
|
|
|
|
86
|
|
|
$this->denyAccessUnlessGranted('read', $new_entity); |
87
|
|
|
|
88
|
|
|
//Basic edit form |
89
|
|
|
$form = $this->createForm($this->form_class, $new_entity); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$form->handleRequest($request); |
92
|
|
|
|
93
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
94
|
|
|
$em->persist($new_entity); |
95
|
|
|
$em->flush(); |
96
|
|
|
//$this->addFlash('success', $translator->trans('part.created_flash')); |
97
|
|
|
|
98
|
|
|
return $this->redirectToRoute($this->route_base . '_edit', ['id' => $new_entity->getID()]); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
//Import form |
102
|
|
|
$import_form = $this->createForm(ImportType::class, ['entity_class' => $this->entity_class]); |
103
|
|
|
$import_form->handleRequest($request); |
104
|
|
|
|
105
|
|
|
if ($import_form->isSubmitted() && $import_form->isValid()) { |
106
|
|
|
/** @var UploadedFile $file */ |
107
|
|
|
$file = $import_form['file']->getData(); |
108
|
|
|
$data = $import_form->getData(); |
109
|
|
|
|
110
|
|
|
$options = array('parent' => $data['parent'], 'preserve_children' => $data['preserve_children'], |
111
|
|
|
'format' => $data['format'], 'csv_separator' => $data['csv_separator']); |
112
|
|
|
|
113
|
|
|
$errors = $importer->fileToDBEntities($file, $this->entity_class, $options); |
|
|
|
|
114
|
|
|
|
115
|
|
|
foreach ($errors as $name => $error) { |
116
|
|
|
/** @var $error ConstraintViolationList */ |
117
|
|
|
$this->addFlash('error', $name . ":" . $error); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->render($this->twig_template, [ |
|
|
|
|
122
|
|
|
'entity' => $new_entity, |
123
|
|
|
'form' => $form->createView(), |
124
|
|
|
'import_form' => $import_form->createView() |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function _delete(Request $request, StructuralDBElement $entity, StructuralElementRecursionHelper $recursionHelper) |
129
|
|
|
{ |
130
|
|
|
$this->denyAccessUnlessGranted('delete', $entity); |
131
|
|
|
|
132
|
|
|
if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) { |
133
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
134
|
|
|
|
135
|
|
|
//Check if we need to remove recursively |
136
|
|
|
if ($request->get('delete_recursive', false)) { |
137
|
|
|
$recursionHelper->delete($entity, false); |
138
|
|
|
} else { |
139
|
|
|
$parent = $entity->getParent(); |
140
|
|
|
|
141
|
|
|
//Move all sub entities to the current parent |
142
|
|
|
foreach ($entity->getSubelements() as $subelement) { |
143
|
|
|
$subelement->setParent($parent); |
144
|
|
|
$entityManager->persist($subelement); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
//Remove current element |
148
|
|
|
$entityManager->remove($entity); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
//Flush changes |
152
|
|
|
$entityManager->flush(); |
153
|
|
|
|
154
|
|
|
$this->addFlash('success', 'attachment_type.deleted'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->redirectToRoute($this->route_base . '_new'); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function _exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request) |
161
|
|
|
{ |
162
|
|
|
$entity = new $this->entity_class(); |
163
|
|
|
|
164
|
|
|
$this->denyAccessUnlessGranted('read', $entity); |
165
|
|
|
|
166
|
|
|
$entities = $em->getRepository($this->entity_class)->findAll(); |
|
|
|
|
167
|
|
|
|
168
|
|
|
return $exporter->exportEntityFromRequest($entities,$request); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function _exportEntity(StructuralDBElement $entity, EntityExporter $exporter, Request $request) |
172
|
|
|
{ |
173
|
|
|
$this->denyAccessUnlessGranted('read', $entity); |
174
|
|
|
|
175
|
|
|
return $exporter->exportEntityFromRequest($entity, $request); |
176
|
|
|
} |
177
|
|
|
} |