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