|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Starkerxp\LeadBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
6
|
|
|
use Starkerxp\LeadBundle\Entity\Lead; |
|
7
|
|
|
use Starkerxp\LeadBundle\Events; |
|
8
|
|
|
use Starkerxp\LeadBundle\Form\Type\LeadType; |
|
9
|
|
|
use Starkerxp\StructureBundle\Controller\StructureController; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class LeadController extends StructureController |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @ApiDoc( |
|
19
|
|
|
* resource=true, |
|
20
|
|
|
* description="Liste les leads.", |
|
21
|
|
|
* section="Lead", |
|
22
|
|
|
* parameters={ |
|
23
|
|
|
* { |
|
24
|
|
|
* "name"="offset", |
|
25
|
|
|
* "dataType"="integer", |
|
26
|
|
|
* "requirement"="\d+", |
|
27
|
|
|
* "description"="starkerxp_structure.doc.offset.result", |
|
28
|
|
|
* "required"="false" |
|
29
|
|
|
* }, |
|
30
|
|
|
* { |
|
31
|
|
|
* "name"="limit", |
|
32
|
|
|
* "dataType"="integer", |
|
33
|
|
|
* "requirement"="\d+", |
|
34
|
|
|
* "description"="starkerxp_structure.doc.limit.result", |
|
35
|
|
|
* "required"="false" |
|
36
|
|
|
* }, |
|
37
|
|
|
* { |
|
38
|
|
|
* "name"="fields", |
|
39
|
|
|
* "dataType"="string", |
|
40
|
|
|
* "requirement"="\w+", |
|
41
|
|
|
* "description"="starkerxp_structure.doc.list_field.entity", |
|
42
|
|
|
* "required"="false" |
|
43
|
|
|
* }, |
|
44
|
|
|
* { |
|
45
|
|
|
* "name"="sort", |
|
46
|
|
|
* "dataType"="string", |
|
47
|
|
|
* "requirement"="\w+", |
|
48
|
|
|
* "description"="starkerxp_structure.doc.sort.result", |
|
49
|
|
|
* "required"="false" |
|
50
|
|
|
* } |
|
51
|
|
|
* }, |
|
52
|
|
|
* views = { "default" } |
|
53
|
|
|
* ) |
|
54
|
|
|
* |
|
55
|
|
|
*/ |
|
56
|
|
|
public function cgetAction(Request $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
|
59
|
|
|
try { |
|
60
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
61
|
|
|
$orderBy = $this->getOrderBy($options['sort']); |
|
62
|
|
|
$resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']); |
|
63
|
|
|
} catch (\Exception $e) { |
|
64
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
65
|
|
|
} |
|
66
|
|
|
if (empty($resultSets)) { |
|
67
|
|
|
return new JsonResponse([]); |
|
68
|
|
|
} |
|
69
|
|
|
$retour = array_map( |
|
70
|
|
|
function ($element) use ($manager, $options) { |
|
71
|
|
|
return $manager->toArray($element, $this->getFields($options['fields'])); |
|
72
|
|
|
}, |
|
73
|
|
|
$resultSets |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
return new JsonResponse($retour); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @ApiDoc( |
|
82
|
|
|
* resource=true, |
|
83
|
|
|
* description="Affiche un lead.", |
|
84
|
|
|
* section="Lead", |
|
85
|
|
|
* requirements={ |
|
86
|
|
|
* { |
|
87
|
|
|
* "name"="lead_id", |
|
88
|
|
|
* "dataType"="integer", |
|
89
|
|
|
* "requirement"="\d+", |
|
90
|
|
|
* "description"="Show an element" |
|
91
|
|
|
* } |
|
92
|
|
|
* }, |
|
93
|
|
|
* parameters={ |
|
94
|
|
|
* { |
|
95
|
|
|
* "name"="fields", |
|
96
|
|
|
* "dataType"="string", |
|
97
|
|
|
* "requirement"="\w+", |
|
98
|
|
|
* "description"="starkerxp_structure.doc.list_field.entity", |
|
99
|
|
|
* "required"="false" |
|
100
|
|
|
* } |
|
101
|
|
|
* }, |
|
102
|
|
|
* views = { "default" } |
|
103
|
|
|
* ) |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getAction(Request $request) |
|
106
|
|
|
{ |
|
107
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
|
108
|
|
|
try { |
|
109
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
110
|
|
|
/** @var Lead $entite */ |
|
111
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('lead_id')])) { |
|
112
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404); |
|
113
|
|
|
} |
|
114
|
|
|
} catch (\Exception $e) { |
|
115
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$retour = $manager->toArray($entite, $this->getFields($options['fields'])); |
|
119
|
|
|
|
|
120
|
|
|
return new JsonResponse($retour); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @ApiDoc( |
|
125
|
|
|
* resource=true, |
|
126
|
|
|
* description="Ajoute un lead.", |
|
127
|
|
|
* section="Lead", |
|
128
|
|
|
* views = { "default" } |
|
129
|
|
|
* ) |
|
130
|
|
|
*/ |
|
131
|
|
|
public function postAction(Request $request) |
|
132
|
|
|
{ |
|
133
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
|
134
|
|
|
try { |
|
135
|
|
|
$entite = new Lead(); |
|
136
|
|
|
$form = $this->createForm(LeadType::class, $entite, ['method' => 'POST']); |
|
137
|
|
|
$form->submit($this->getRequestData($request)); |
|
138
|
|
|
if ($form->isValid()) { |
|
139
|
|
|
$lead = $form->getData(); |
|
140
|
|
|
$manager->insert($lead); |
|
141
|
|
|
$this->dispatch(Events::LEAD_CREATED, new GenericEvent($lead)); |
|
142
|
|
|
|
|
143
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.created", "lead")], 201); |
|
144
|
|
|
} |
|
145
|
|
|
} catch (\Exception $e) { |
|
146
|
|
|
$manager->rollback(); |
|
147
|
|
|
|
|
148
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @ApiDoc( |
|
156
|
|
|
* resource=true, |
|
157
|
|
|
* description="Edit lead.", |
|
158
|
|
|
* section="Lead", |
|
159
|
|
|
* requirements={ |
|
160
|
|
|
* { |
|
161
|
|
|
* "name"="lead_id", |
|
162
|
|
|
* "dataType"="integer", |
|
163
|
|
|
* "requirement"="\d+", |
|
164
|
|
|
* "description"="Edit an element." |
|
165
|
|
|
* } |
|
166
|
|
|
* }, |
|
167
|
|
|
* views = { "default" } |
|
168
|
|
|
* ) |
|
169
|
|
|
*/ |
|
170
|
|
|
public function putAction(Request $request) |
|
171
|
|
|
{ |
|
172
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
|
173
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('lead_id')])) { |
|
174
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404); |
|
175
|
|
|
} |
|
176
|
|
|
$manager->beginTransaction(); |
|
177
|
|
|
try { |
|
178
|
|
|
$form = $this->createForm(LeadType::class, $entite, ['method' => 'PUT']); |
|
179
|
|
|
$form->submit($this->getRequestData($request), false); |
|
180
|
|
|
if ($form->isValid()) { |
|
181
|
|
|
$entite = $form->getData(); |
|
182
|
|
|
$manager->update($entite); |
|
183
|
|
|
$this->dispatch(Events::LEAD_UPDATED, new GenericEvent($entite)); |
|
184
|
|
|
|
|
185
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.updated", "campaign")], 204); |
|
186
|
|
|
} |
|
187
|
|
|
} catch (\Exception $e) { |
|
188
|
|
|
$manager->rollback(); |
|
189
|
|
|
|
|
190
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @ApiDoc( |
|
198
|
|
|
* resource=true, |
|
199
|
|
|
* description="Edit lead.", |
|
200
|
|
|
* section="Lead", |
|
201
|
|
|
* views = { "default" } |
|
202
|
|
|
* ) |
|
203
|
|
|
*/ |
|
204
|
|
|
public function putWithoutIdAction(Request $request) |
|
205
|
|
|
{ |
|
206
|
|
|
$data = $this->getRequestData($request); |
|
207
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
|
208
|
|
|
if (!$entite = $manager->findOneBy(['origin' => $data['origin'], 'externalReference' => $data['external_reference']])) { |
|
209
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404); |
|
210
|
|
|
} |
|
211
|
|
|
$response = $this->forward( |
|
212
|
|
|
'StarkerxpLeadBundle:Lead:put', |
|
213
|
|
|
[ |
|
214
|
|
|
'lead_id' => $entite->getId(), |
|
215
|
|
|
] |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
|
|
return $response; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
|
|
|
|
|
221
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.