Completed
Push — master ( 3a1193...09a594 )
by Jan
07:54
created

PartController::edit()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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