1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiouPiou\RibsAdminBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Exception; |
7
|
|
|
use PiouPiou\RibsAdminBundle\Entity\Package; |
8
|
|
|
use PiouPiou\RibsAdminBundle\Service\Version; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
14
|
|
|
|
15
|
|
|
class PackageController extends AbstractController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @Route("/packages/", name="ribsadmin_packages") |
19
|
|
|
* @param EntityManagerInterface $em |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public function index(EntityManagerInterface $em): Response |
23
|
|
|
{ |
24
|
|
|
$packages = $em->getRepository(Package::class)->findAll(); |
25
|
|
|
|
26
|
|
|
return $this->render("@RibsAdmin/packages/list.html.twig", ["packages" => $packages]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @Route("/packages/create/", name="ribsadmin_packages_create") |
31
|
|
|
* @Route("/packages/show/{guid}", name="ribsadmin_packages_show") |
32
|
|
|
* @Route("/packages/edit/{guid}", name="ribsadmin_packages_edit") |
33
|
|
|
* @param Request $request |
34
|
|
|
* @param EntityManagerInterface $em |
35
|
|
|
* @param string|null $guid |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
|
|
public function edit(Request $request, EntityManagerInterface $em, string $guid = null): Response |
39
|
|
|
{ |
40
|
|
|
$disabled_form = strpos($request->get("_route"), "_show") ? true : false; |
41
|
|
|
if (!$guid) { |
42
|
|
|
$package = new Package(); |
43
|
|
|
$text = "created"; |
44
|
|
|
} else { |
45
|
|
|
$package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]); |
46
|
|
|
$text = "edited"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$form = $this->createForm(\PiouPiou\RibsAdminBundle\Form\Package::class, $package, ["disabled" => $disabled_form]); |
50
|
|
|
$form->handleRequest($request); |
51
|
|
|
|
52
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
53
|
|
|
/** @var Package $data */ |
54
|
|
|
$data = $form->getData(); |
55
|
|
|
$em->persist($data); |
56
|
|
|
$em->flush(); |
57
|
|
|
$this->addFlash("success-flash", "Package " . $data->getProjectName() . " was " . $text); |
58
|
|
|
|
59
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->render("@RibsAdmin/packages/edit.html.twig", [ |
63
|
|
|
"disabled_form" => $disabled_form, |
64
|
|
|
"form" => $form->createView(), |
65
|
|
|
"form_errors" => $form->getErrors(), |
66
|
|
|
"package" => $package |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @Route("/packages/delete/{guid}", name="ribsadmin_packages_delete") |
72
|
|
|
* @param EntityManagerInterface $em |
73
|
|
|
* @param string $guid |
74
|
|
|
* @return RedirectResponse |
75
|
|
|
*/ |
76
|
|
|
public function delete(EntityManagerInterface $em, string $guid): RedirectResponse |
77
|
|
|
{ |
78
|
|
|
$package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]); |
79
|
|
|
|
80
|
|
|
if ($package) { |
81
|
|
|
$em->remove($package); |
82
|
|
|
$em->flush(); |
83
|
|
|
$this->addFlash("success-flash", "The project package was deleted"); |
84
|
|
|
} else { |
85
|
|
|
$this->addFlash("error-flash", "The project package was not found"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @Route("/packages/update/{guid}", name="ribsadmin_packages_update") |
93
|
|
|
* @param Version $version |
94
|
|
|
* @param string $guid |
95
|
|
|
* @return RedirectResponse |
96
|
|
|
* @throws Exception |
97
|
|
|
*/ |
98
|
|
|
public function updatePackage(Version $version, string $guid): RedirectResponse |
99
|
|
|
{ |
100
|
|
|
if ($guid) { |
101
|
|
|
$version->save($guid); |
102
|
|
|
|
103
|
|
|
if ($version->getMessages()) { |
104
|
|
|
$message = "<ul>"; |
105
|
|
|
$message .= "<li>The project package was not well updated</li>"; |
106
|
|
|
foreach ($version->getMessages() as $version_message) { |
107
|
|
|
$message .= "<li>".$version_message."</li>"; |
108
|
|
|
} |
109
|
|
|
$message .= "</ul>"; |
110
|
|
|
|
111
|
|
|
$this->addFlash("info-flash", $message); |
112
|
|
|
} else { |
113
|
|
|
$this->addFlash("success-flash", "The project package was updated"); |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
$this->addFlash("error-flash", "The project package was not found"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|