Completed
Push — master ( 27a001...9385d2 )
by Jan
06:11
created

PartController::edit()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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