Completed
Push — master ( a46bcd...68aa75 )
by Jan
03:05
created

AttachmentTypeController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 20 3
A exportEntity() 0 3 1
A exportAll() 0 5 1
B new() 0 43 6
A edit() 0 14 3
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
35
use App\Entity\AttachmentType;
36
use App\Entity\NamedDBElement;
37
use App\Entity\StructuralDBElement;
38
use App\Form\BaseEntityAdminForm;
39
use App\Form\ExportType;
0 ignored issues
show
Bug introduced by
The type App\Form\ExportType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
use App\Form\ImportType;
41
use App\Services\EntityExporter;
42
use App\Services\EntityImporter;
43
use Doctrine\ORM\EntityManagerInterface;
44
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
45
use Symfony\Component\HttpFoundation\File\UploadedFile;
46
use Symfony\Component\HttpFoundation\Request;
47
use Symfony\Component\HttpFoundation\Response;
48
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
49
use Symfony\Component\Routing\Annotation\Route;
50
use Symfony\Component\Serializer\SerializerInterface;
51
use Symfony\Component\Validator\ConstraintViolationList;
52
53
/**
54
 * @Route("/attachment_type")
55
 * @package App\Controller
56
 */
57
class AttachmentTypeController extends AbstractController
58
{
59
60
    /**
61
     * @Route("/{id}/edit", requirements={"id"="\d+"}, name="attachment_type_edit")
62
     * @Route("/{id}/", requirements={"id"="\d+"})
63
     */
64
    public function edit(AttachmentType $entity, Request $request, EntityManagerInterface $em)
65
    {
66
67
        $form = $this->createForm(BaseEntityAdminForm::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('AdminPages/AttachmentTypeAdmin.html.twig', [
76
            'entity' => $entity,
77
            'form' => $form->createView()
78
        ]);
79
    }
80
81
    /**
82
     * @Route("/new", name="attachment_type_new")
83
     * @Route("/")
84
     *
85
     * @return \Symfony\Component\HttpFoundation\Response
86
     */
87
    public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer)
88
    {
89
        $new_entity = new AttachmentType();
90
91
        $this->denyAccessUnlessGranted('create', $new_entity);
92
93
        //Basic edit form
94
        $form = $this->createForm(BaseEntityAdminForm::class, $new_entity);
95
96
        $form->handleRequest($request);
97
98
        if ($form->isSubmitted() && $form->isValid()) {
99
            $em->persist($new_entity);
100
            $em->flush();
101
            //$this->addFlash('success', $translator->trans('part.created_flash'));
102
103
            return $this->redirectToRoute('attachment_type_edit', ['id' => $new_entity->getID()]);
104
        }
105
106
        //Import form
107
        $import_form = $this->createForm(ImportType::class, ['entity_class' => AttachmentType::class]);
108
        $import_form->handleRequest($request);
109
110
        if ($import_form->isSubmitted() && $import_form->isValid()) {
111
            /** @var UploadedFile $file */
112
            $file = $import_form['file']->getData();
113
            $data = $import_form->getData();
114
115
            $options = array('parent' => $data['parent'], 'preserve_children' => $data['preserve_children'],
116
                'format' => $data['format'], 'csv_separator' => $data['csv_separator']);
117
118
            $errors = $importer->fileToDBEntities($file, AttachmentType::class, $options);
119
120
            foreach ($errors as $name => $error) {
121
                /** @var $error ConstraintViolationList */
122
                $this->addFlash('error', $name . ":" . $error);
123
            }
124
        }
125
126
        return $this->render('AdminPages/AttachmentTypeAdmin.html.twig', [
127
            'entity' => $new_entity,
128
            'form' => $form->createView(),
129
            'import_form' => $import_form->createView()
130
        ]);
131
    }
132
133
    /**
134
     * @Route("/{id}", name="attachment_type_delete", methods={"DELETE"})
135
     */
136
    public function delete(Request $request, AttachmentType $entity)
137
    {
138
        if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) {
139
            $entityManager = $this->getDoctrine()->getManager();
140
141
            $parent = $entity->getParent();
142
143
            //Move all sub entities to the current parent
144
            foreach ($entity->getSubelements() as $subelement) {
145
                $subelement->setParent($parent);
146
                $entityManager->persist($subelement);
147
            }
148
149
            //Remove current element
150
            $entityManager->remove($entity);
151
            $entityManager->flush();
152
            $this->addFlash('success', 'attachment_type.deleted');
153
        }
154
155
        return $this->redirectToRoute('attachment_type_new');
156
    }
157
158
    /**
159
     * @Route("/export", name="attachment_type_export_all")
160
     * @param Request $request
161
     * @param SerializerInterface $serializer
162
     * @param EntityManagerInterface $em
163
     * @return Response
164
     */
165
    public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request)
166
    {
167
        $entities = $em->getRepository(AttachmentType::class)->findAll();
168
169
        return $exporter->exportEntityFromRequest($entities,$request);
170
    }
171
172
    /**
173
     * @Route("/{id}/export", name="attachment_type_export")
174
     * @param Request $request
175
     * @param AttachmentType $entity
176
     */
177
    public function exportEntity(AttachmentType $entity, EntityExporter $exporter, Request $request)
178
    {
179
        return $exporter->exportEntityFromRequest($entity, $request);
180
    }
181
182
}