Passed
Push — master ( 44c482...cc1bad )
by Jan
03:25
created

PartController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 12
rs 10
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 Doctrine\ORM\EntityManager;
41
use Doctrine\ORM\EntityManagerInterface;
42
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
43
use Symfony\Component\HttpFoundation\Request;
44
use Symfony\Component\Routing\Annotation\Route;
45
46
class PartController extends AbstractController
47
{
48
49
    /**
50
     * @Route("/part/{id}/info", name="part_info")
51
     * @Route("/part/{id}")
52
     */
53
    public function show(Part $part, AttachmentFilenameService $attachmentFilenameService)
54
    {
55
        $filename = $part->getMasterPictureFilename(true);
56
        dump($filename);
57
58
        return $this->render('show_part_info.html.twig',
59
            [
60
                "part" => $part,
61
                "main_image" => $attachmentFilenameService->attachmentPathToAbsolutePath($filename)
62
            ]
63
            );
64
    }
65
66
    /**
67
     * @Route("/part/{id}/edit", name="part_edit", requirements={"id"="\d+"})
68
     *
69
     * @param Part $part
70
     * @return \Symfony\Component\HttpFoundation\Response
71
     */
72
    public function edit(Part $part, Request $request)
73
    {
74
        $form = $this->createForm(PartType::class, $part);
75
76
77
        $form->handleRequest($request);
78
79
80
        return $this->render('edit_part_info.html.twig',
81
            [
82
                "part" => $part,
83
                "form" => $form->createView()
84
            ]);
85
    }
86
87
    /**
88
     * @Route("/parts/new", name="part_new")
89
     *
90
     * @return \Symfony\Component\HttpFoundation\Response
91
     */
92
    public function new(Request $request, EntityManagerInterface $em)
93
    {
94
        $new_part = new Part();
95
        $category = $em->find(Category::class, 1);
96
        $new_part->setCategory($category);
97
98
        $this->addFlash('success', 'Article Created!');
99
100
        $form = $this->createForm(PartType::class, $new_part);
101
102
        $form->handleRequest($request);
103
104
        if ($form->isSubmitted() && $form->isValid()) {
105
            /** @var Article $article */
106
            //$part = $form->getData();
107
            $em->persist($new_part);
108
            $em->flush();
109
            $this->addFlash('success', 'Article Created! Knowledge is power!');
110
            return $this->redirectToRoute('part_edit',['id' => $new_part->getID()]);
111
        }
112
113
114
        return $this->render('edit_part_info.html.twig',
115
            [
116
                "part" => $new_part,
117
                "form" => $form->createView()
118
            ]);
119
    }
120
121
}