Passed
Push — master ( 33631f...10f39b )
by Jan
03:09
created

PartController::clone()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 2
nop 4
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * part-db version 0.1
6
 * Copyright (C) 2005 Christoph Lechner
7
 * http://www.cl-projects.de/
8
 *
9
 * part-db version 0.2+
10
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
11
 * http://code.google.com/p/part-db/
12
 *
13
 * Part-DB Version 0.4+
14
 * Copyright (C) 2016 - 2019 Jan Böhmer
15
 * https://github.com/jbtronics
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation; either version 2
20
 * of the License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
30
 *
31
 */
32
33
namespace App\Controller;
34
35
36
use App\Entity\Category;
37
use App\Entity\Part;
38
use App\Form\PartType;
39
use App\Services\AttachmentFilenameService;
40
use App\Services\EntityURLGenerator;
41
use Doctrine\ORM\EntityManager;
42
use Doctrine\ORM\EntityManagerInterface;
43
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
44
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
45
use Symfony\Component\HttpFoundation\Request;
46
use Symfony\Component\Routing\Annotation\Route;
47
use Symfony\Contracts\Translation\TranslatorInterface;
48
49
class PartController extends AbstractController
50
{
51
52
    /**
53
     * @Route("/part/{id}/info", name="part_info")
54
     * @Route("/part/{id}", requirements={"id"="\d+"})
55
     */
56
    public function show(Part $part, AttachmentFilenameService $attachmentFilenameService)
57
    {
58
        $this->denyAccessUnlessGranted('read', $part);
59
60
        $filename = $part->getMasterPictureFilename(true);
61
62
        return $this->render('Parts/show_part_info.html.twig',
63
            [
64
                "part" => $part,
65
                "main_image" => $attachmentFilenameService->attachmentPathToAbsolutePath($filename)
66
            ]
67
            );
68
    }
69
70
    /**
71
     * @Route("/part/{id}/edit", name="part_edit")
72
     *
73
     * @param Part $part
74
     * @return \Symfony\Component\HttpFoundation\Response
75
     */
76
    public function edit(Part $part, Request $request, EntityManagerInterface $em)
77
    {
78
        $this->denyAccessUnlessGranted('edit', $part);
79
80
        $form = $this->createForm(PartType::class, $part);
81
82
        $form->handleRequest($request);
83
        if ($form->isSubmitted() && $form->isValid()) {
84
            $em->persist($part);
85
            $em->flush();
86
            $this->addFlash('info', 'part.edited_flash');
87
        }
88
89
        return $this->render('Parts/edit_part_info.html.twig',
90
            [
91
                "part" => $part,
92
                "form" => $form->createView(),
93
            ]);
94
    }
95
96
    /**
97
     * @Route("/part/new", name="part_new")
98
     *
99
     * @return \Symfony\Component\HttpFoundation\Response
100
     */
101
    public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
102
    {
103
        $new_part = new Part();
104
105
        $this->denyAccessUnlessGranted('create', $new_part);
106
107
        $cid = $request->get('cid', 1);
108
109
        $category = $em->find(Category::class, $cid);
110
        $new_part->setCategory($category);
111
112
        $form = $this->createForm(PartType::class, $new_part);
113
114
        $form->handleRequest($request);
115
116
        if ($form->isSubmitted() && $form->isValid()) {
117
            $em->persist($new_part);
118
            $em->flush();
119
            $this->addFlash('success', $translator->trans('part.created_flash'));
120
            return $this->redirectToRoute('part_edit',['id' => $new_part->getID()]);
121
        }
122
123
124
        return $this->render('Parts/new_part.html.twig',
125
            [
126
                "part" => $new_part,
127
                "form" => $form->createView()
128
            ]);
129
    }
130
131
    /**
132
     * @Route("/part/{id}/clone", name="part_clone")
133
     *
134
     */
135
    public function clone(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
136
    {
137
138
        /** @var Part $new_part */
139
        $new_part = clone($part);
140
141
        $this->denyAccessUnlessGranted('create', $new_part);
142
143
        $form = $this->createForm(PartType::class, $new_part);
144
145
        $form->handleRequest($request);
146
147
        if ($form->isSubmitted() && $form->isValid()) {
148
            $em->persist($new_part);
149
            $em->flush();
150
            $this->addFlash('success', $translator->trans('part.created_flash'));
151
            return $this->redirectToRoute('part_edit',['id' => $new_part->getID()]);
152
        }
153
154
155
        return $this->render('Parts/new_part.html.twig',
156
            [
157
                "part" => $new_part,
158
                "form" => $form->createView()
159
            ]);
160
    }
161
162
}