Completed
Push — master ( 0ff9e3...b14edf )
by Jan
04:14
created

PartController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 17
rs 10
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
namespace App\Controller;
32
33
use App\Entity\Base\NamedDBElement;
34
use App\Entity\Base\StructuralDBElement;
35
use App\Entity\Parts\Category;
36
use App\Entity\Parts\Part;
37
use App\Form\AttachmentFormType;
38
use App\Form\Part\PartBaseType;
39
use App\Services\AttachmentHelper;
40
use App\Services\PricedetailHelper;
41
use App\Services\StructuralElementRecursionHelper;
42
use Doctrine\ORM\EntityManagerInterface;
43
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
44
use Symfony\Component\Form\FormInterface;
45
use Symfony\Component\HttpFoundation\Request;
46
use Symfony\Component\Routing\Annotation\Route;
47
use Symfony\Contracts\Translation\TranslatorInterface;
48
49
/**
50
 * @Route("/part")
51
 * @package App\Controller
52
 */
53
class PartController extends AbstractController
54
{
55
    /**
56
     * @Route("/{id}/info", name="part_info")
57
     * @Route("/{id}", requirements={"id"="\d+"})
58
     * @param Part $part
59
     * @param AttachmentHelper $attachmentHelper
60
     * @return \Symfony\Component\HttpFoundation\Response
61
     */
62
    public function show(Part $part, AttachmentHelper $attachmentHelper, PricedetailHelper $pricedetailHelper)
63
    {
64
        $this->denyAccessUnlessGranted('read', $part);
65
66
        return $this->render('Parts/info/show_part_info.html.twig',
67
            [
68
                'part' => $part,
69
                'attachment_helper' => $attachmentHelper,
70
                'pricedetail_helper' => $pricedetailHelper
71
            ]
72
            );
73
    }
74
75
    /**
76
     * @Route("/{id}/edit", name="part_edit")
77
     *
78
     * @param Part $part
79
     *
80
     * @param Request $request
81
     * @param EntityManagerInterface $em
82
     * @return \Symfony\Component\HttpFoundation\Response
83
     */
84
    public function edit(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator,
85
            AttachmentHelper $attachmentHelper)
86
    {
87
        $this->denyAccessUnlessGranted('edit', $part);
88
89
        $form = $this->createForm(PartBaseType::class, $part);
90
91
        $form->handleRequest($request);
92
        if ($form->isSubmitted() && $form->isValid()) {
93
            //Upload passed files
94
            $attachments = $form['attachments'];
95
            foreach ($attachments as $attachment) {
96
                /** @var $attachment FormInterface */
97
                $attachmentHelper->upload( $attachment->getData(), $attachment['file']->getData());
98
            }
99
100
101
            $em->persist($part);
102
            $em->flush();
103
            $this->addFlash('info', $translator->trans('part.edited_flash'));
104
            //Reload form, so the SIUnitType entries use the new part unit
105
            $form = $this->createForm(PartBaseType::class, $part);
106
        } elseif ($form->isSubmitted() && ! $form->isValid()) {
107
            $this->addFlash('error', $translator->trans('part.edited_flash.invalid'));
108
        }
109
110
        return $this->render('Parts/edit/edit_part_info.html.twig',
111
            [
112
                'part' => $part,
113
                'form' => $form->createView(),
114
                'attachment_helper' => $attachmentHelper,
115
            ]);
116
    }
117
118
    /**
119
     * @Route("/{id}/delete", name="part_delete", methods={"DELETE"})
120
     * @param Request $request
121
     * @param Part $part
122
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
123
     */
124
    public function delete(Request $request, Part $part)
125
    {
126
        $this->denyAccessUnlessGranted('delete', $part);
127
128
        if ($this->isCsrfTokenValid('delete' . $part->getId(), $request->request->get('_token'))) {
129
            $entityManager = $this->getDoctrine()->getManager();
130
131
            //Remove part
132
            $entityManager->remove($part);
133
134
            //Flush changes
135
            $entityManager->flush();
136
137
            $this->addFlash('success', 'part.deleted');
138
        }
139
140
        return $this->redirectToRoute('homepage');
141
    }
142
143
    /**
144
     * @Route("/new", name="part_new")
145
     *
146
     * @param Request $request
147
     * @param EntityManagerInterface $em
148
     * @param TranslatorInterface $translator
149
     * @return \Symfony\Component\HttpFoundation\Response
150
     */
151
    public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator,
152
                        AttachmentHelper $attachmentHelper)
153
    {
154
        $new_part = new Part();
155
156
        $this->denyAccessUnlessGranted('create', $new_part);
157
158
        $cid = $request->get('cid', 1);
159
160
        $category = $em->find(Category::class, $cid);
161
        $new_part->setCategory($category);
162
163
        $form = $this->createForm(PartBaseType::class, $new_part);
164
165
        $form->handleRequest($request);
166
167
        if ($form->isSubmitted() && $form->isValid()) {
168
            //Upload passed files
169
            $attachments = $form['attachments'];
170
            foreach ($attachments as $attachment) {
171
                /** @var $attachment FormInterface */
172
                $attachmentHelper->upload( $attachment->getData(), $attachment['file']->getData());
173
            }
174
175
            $em->persist($new_part);
176
            $em->flush();
177
            $this->addFlash('success', $translator->trans('part.created_flash'));
178
179
            return $this->redirectToRoute('part_edit', ['id' => $new_part->getID()]);
180
        } elseif ($form->isSubmitted() && ! $form->isValid()) {
181
            $this->addFlash('error', $translator->trans('part.created_flash.invalid'));
182
        }
183
184
        return $this->render('Parts/edit/new_part.html.twig',
185
            [
186
                'part' => $new_part,
187
                'form' => $form->createView(),
188
                'attachment_helper' => $attachmentHelper
189
            ]);
190
    }
191
192
    /**
193
     * @Route("/{id}/clone", name="part_clone")
194
     * @param Part $part
195
     * @param Request $request
196
     * @param EntityManagerInterface $em
197
     * @param TranslatorInterface $translator
198
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
199
     */
200
    public function clone(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
201
    {
202
        /** @var Part $new_part */
203
        $new_part = clone $part;
204
205
        $this->denyAccessUnlessGranted('create', $new_part);
206
207
        $form = $this->createForm(PartBaseType::class, $new_part);
208
209
        $form->handleRequest($request);
210
211
        if ($form->isSubmitted() && $form->isValid()) {
212
            $em->persist($new_part);
213
            $em->flush();
214
            $this->addFlash('success', $translator->trans('part.created_flash'));
215
216
            return $this->redirectToRoute('part_edit', ['id' => $new_part->getID()]);
217
        }
218
219
        return $this->render('Parts/edit/new_part.html.twig',
220
            [
221
                'part' => $new_part,
222
                'form' => $form->createView(),
223
            ]);
224
    }
225
}
226