|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Starkerxp\CampagneBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Starkerxp\CampagneBundle\Entity\Template; |
|
6
|
|
|
use Starkerxp\CampagneBundle\Form\Type\TemplateType; |
|
7
|
|
|
use Starkerxp\StructureBundle\Controller\StructureController; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TemplateController extends StructureController |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
public function cgetAction(Request $request) |
|
16
|
|
|
{ |
|
17
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
18
|
|
|
try { |
|
19
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
20
|
|
|
$orderBy = $this->getOrderBy($options['sort']); |
|
21
|
|
|
$resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']); |
|
22
|
|
|
} catch (\Exception $e) { |
|
23
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
24
|
|
|
} |
|
25
|
|
|
if (empty($resultSets)) { |
|
26
|
|
|
return new JsonResponse([]); |
|
27
|
|
|
} |
|
28
|
|
|
$retour = array_map( |
|
29
|
|
|
function ($element) use ($manager, $options) { |
|
30
|
|
|
return $manager->toArray($element, $this->getFields($options['fields'])); |
|
31
|
|
|
}, |
|
32
|
|
|
$resultSets |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
return new JsonResponse($retour); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getAction(Request $request) |
|
39
|
|
|
{ |
|
40
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
41
|
|
|
try { |
|
42
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
43
|
|
|
$template = $manager->findOneBy(['id' => $request->get('id')]); |
|
44
|
|
|
} catch (\Exception $e) { |
|
45
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
46
|
|
|
} |
|
47
|
|
|
if (!$template instanceof Template) { |
|
48
|
|
|
return new JsonResponse(["payload" => "Le template n'existe pas."], 404); |
|
49
|
|
|
} |
|
50
|
|
|
$retour = $manager->toArray($template, $this->getFields($options['fields'])); |
|
51
|
|
|
|
|
52
|
|
|
return new JsonResponse($retour); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function postAction(Request $request) |
|
56
|
|
|
{ |
|
57
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
58
|
|
|
|
|
59
|
|
|
$template = new Template(); |
|
60
|
|
|
$form = $this->createForm(TemplateType::class, $template, ['method' => 'POST']); |
|
61
|
|
|
$data = json_decode($request->getContent(), true); |
|
62
|
|
|
if (empty($data)) { |
|
63
|
|
|
$data = $request->request->all(); |
|
64
|
|
|
} |
|
65
|
|
|
$form->submit($data); |
|
66
|
|
|
if ($form->isValid()) { |
|
67
|
|
|
$template = $form->getData(); |
|
68
|
|
|
$template->setUuid($this->getUuid()); |
|
69
|
|
|
$manager->insert($template); |
|
70
|
|
|
|
|
71
|
|
|
return new JsonResponse([], 201); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function putAction(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
80
|
|
|
$template = $manager->find($request->get('id')); |
|
81
|
|
|
if (!$template instanceof Template) { |
|
82
|
|
|
return new JsonResponse(["payload" => "Le template n'existe pas."], 404); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$form = $this->createForm(TemplateType::class, $template, ['method' => 'PUT']); |
|
86
|
|
|
$data = json_decode($request->getContent(), true); |
|
87
|
|
|
if (empty($data)) { |
|
88
|
|
|
$data = $request->request->all(); |
|
89
|
|
|
} |
|
90
|
|
|
$form->submit($data); |
|
91
|
|
|
if ($form->isValid()) { |
|
92
|
|
|
$template = $form->getData(); |
|
93
|
|
|
$manager->update($template); |
|
94
|
|
|
|
|
95
|
|
|
return new JsonResponse(["payload" => "Le template a bien été mis à jours."], 204); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function deleteAction(Request $request) |
|
102
|
|
|
{ |
|
103
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
104
|
|
|
$template = $manager->find($request->get('id')); |
|
105
|
|
|
if (!$template instanceof Template) { |
|
106
|
|
|
return new JsonResponse(["payload" => "Le template n'existe pas."], 404); |
|
107
|
|
|
} |
|
108
|
|
|
$manager->delete($template); |
|
109
|
|
|
|
|
110
|
|
|
return new JsonResponse(["payload" => "Le template a bien été supprimé."], 204); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
} |
|
|
|
|
|
|
114
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.