1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
4
|
|
|
* |
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Controller; |
12
|
|
|
|
13
|
|
|
use LoginCidadao\CoreBundle\Model\LocationSelectData; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
16
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
17
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
18
|
|
|
use LoginCidadao\CoreBundle\Entity\PersonAddress; |
19
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
20
|
|
|
use Doctrine\Common\Collections\Collection; |
21
|
|
|
use Symfony\Component\Form\FormError; |
22
|
|
|
use LoginCidadao\CoreBundle\Entity\State; |
23
|
|
|
use LoginCidadao\CoreBundle\Entity\City; |
24
|
|
|
|
25
|
|
|
class PersonAddressController extends Controller |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @Route("/person/addresses", name="lc_person_addresses") |
30
|
|
|
* @Template() |
31
|
|
|
*/ |
32
|
|
|
public function listAction() |
33
|
|
|
{ |
34
|
|
|
$deleteForms = $this->getDeleteForms(); |
35
|
|
|
|
36
|
|
|
return compact('deleteForms'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @Route("/person/addresses/new", name="lc_person_addresses_new") |
41
|
|
|
* @Template() |
42
|
|
|
*/ |
43
|
|
|
public function newAddressAction(Request $request) |
44
|
|
|
{ |
45
|
|
|
$address = new PersonAddress(); |
46
|
|
|
$form = $this->createForm('LoginCidadao\CoreBundle\Form\Type\PersonAddressFormType', $address); |
47
|
|
|
|
48
|
|
|
$form->handleRequest($request); |
49
|
|
|
|
50
|
|
|
$em = $this->getDoctrine()->getManager(); |
51
|
|
|
if ($form->isValid()) { |
52
|
|
|
$address->setPerson($this->getUser()); |
53
|
|
|
$em->persist($address); |
54
|
|
|
$em->flush(); |
55
|
|
|
|
56
|
|
|
return $this->redirect($this->generateUrl('lc_person_addresses')); |
57
|
|
|
} |
58
|
|
|
$deleteForms = $this->getDeleteForms(); |
59
|
|
|
|
60
|
|
|
return compact('form', 'deleteForms'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function checkAddressLocation(&$address, $form, &$em) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$form = $form->get('location'); |
66
|
|
|
if (!$address->getCountry()) { |
67
|
|
|
$form->get('country')->addError(new FormError($this->get('translator')->trans('required.field'))); |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
$isPreferred = $em->getRepository('LoginCidadaoCoreBundle:Country')->isPreferred($address->getCountry()); |
72
|
|
|
if (!$address->getState() && !$isPreferred) { |
73
|
|
|
$steppe = ucwords(strtolower(trim($form->get('statesteppe')->getData()))); |
74
|
|
|
if ($steppe) { |
75
|
|
|
$repo = $em->getRepository('LoginCidadaoCoreBundle:State'); |
76
|
|
|
$ent = $repo->findOneBy(array( |
77
|
|
|
'name' => $steppe, |
78
|
|
|
'country' => $address->getCountry(), |
79
|
|
|
)); |
80
|
|
|
if (!$ent) { |
81
|
|
|
$ent = new State(); |
82
|
|
|
$ent->setName($steppe); |
83
|
|
|
$ent->setCountry($address->getCountry()); |
84
|
|
|
$em->persist($ent); |
85
|
|
|
} |
86
|
|
|
$address->setState($ent); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
if (!$address->getState()) { |
90
|
|
|
$form->get('state')->addError(new FormError($this->get('translator')->trans('required.field'))); |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
if (!$address->getCity() && !$isPreferred) { |
95
|
|
|
$steppe = ucwords(strtolower(trim($form->get('citysteppe')->getData()))); |
96
|
|
|
if ($address->getState()) { |
97
|
|
|
$state = $address->getState(); |
98
|
|
|
} elseif (isset($ent)) { |
99
|
|
|
$state = $ent; |
100
|
|
|
} else { |
101
|
|
|
$state = null; |
102
|
|
|
} |
103
|
|
|
if ($state && $steppe) { |
104
|
|
|
$repo = $em->getRepository('LoginCidadaoCoreBundle:City'); |
105
|
|
|
$ent = $repo->findOneBy(array( |
106
|
|
|
'name' => $steppe, |
107
|
|
|
'state' => $state, |
108
|
|
|
)); |
109
|
|
|
if (!$ent) { |
110
|
|
|
$ent = new City(); |
111
|
|
|
$ent->setName($steppe); |
112
|
|
|
$ent->setState($state); |
113
|
|
|
$em->persist($ent); |
114
|
|
|
} |
115
|
|
|
$address->setCity($ent); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
if (!$address->getCity()) { |
119
|
|
|
$form->get('city')->addError(new FormError($this->get('translator')->trans('required.field'))); |
120
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @Route("/person/addresses/{id}/edit", name="lc_person_addresses_edit") |
129
|
|
|
* @Template("LoginCidadaoCoreBundle:PersonAddress:newAddress.html.twig") |
130
|
|
|
*/ |
131
|
|
|
public function editAction(Request $request, $id) |
132
|
|
|
{ |
133
|
|
|
$em = $this->getDoctrine()->getManager(); |
134
|
|
|
$addresses = $em->getRepository('LoginCidadaoCoreBundle:PersonAddress'); |
135
|
|
|
/** @var PersonAddress $address */ |
136
|
|
|
$address = $addresses->find($id); |
137
|
|
|
if ($address->getPerson()->getId() !== $this->getUser()->getId()) { |
138
|
|
|
throw new AccessDeniedException(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$address = $this->prepareAddressForEdition($address); |
142
|
|
|
|
143
|
|
|
$form = $this->createForm('LoginCidadao\CoreBundle\Form\Type\PersonAddressFormType', $address); |
144
|
|
|
$form->handleRequest($request); |
145
|
|
|
|
146
|
|
|
if ($form->isValid()) { |
147
|
|
|
$address->setPerson($this->getUser()); |
148
|
|
|
|
149
|
|
|
$em->flush(); |
150
|
|
|
|
151
|
|
|
return $this->redirect($this->generateUrl('lc_person_addresses')); |
152
|
|
|
} |
153
|
|
|
$deleteForms = $this->getDeleteForms(); |
154
|
|
|
$edit_form = $form->createView(); |
155
|
|
|
|
156
|
|
|
return compact('edit_form', 'deleteForms'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @Route("/person/addresses/{id}/remove", name="lc_person_addresses_delete") |
161
|
|
|
* @Template() |
162
|
|
|
*/ |
163
|
|
|
public function deleteAction(Request $request, $id) |
164
|
|
|
{ |
165
|
|
|
$translator = $this->get('translator'); |
166
|
|
|
$form = $this->createForm('LoginCidadao\CoreBundle\Form\Type\RemovePersonAddressFormType'); |
167
|
|
|
$form->handleRequest($request); |
168
|
|
|
|
169
|
|
|
if ($form->isValid()) { |
170
|
|
|
$person = $this->getUser(); |
171
|
|
|
$em = $this->getDoctrine()->getManager(); |
172
|
|
|
$addresses = $em->getRepository('LoginCidadaoCoreBundle:PersonAddress'); |
173
|
|
|
$address = $addresses->find($id); |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
if ($address->getPerson()->getId() !== $person->getId()) { |
177
|
|
|
throw new AccessDeniedException(); |
178
|
|
|
} |
179
|
|
|
$em->remove($address); |
180
|
|
|
$em->flush(); |
181
|
|
|
} catch (AccessDeniedException $e) { |
182
|
|
|
$this->get('session')->getFlashBag()->add('error', |
183
|
|
|
$translator->trans("Access Denied.")); |
184
|
|
|
} catch (\Exception $e) { |
185
|
|
|
$this->get('session')->getFlashBag()->add('error', |
186
|
|
|
$translator->trans("Wasn't possible to remove this address.")); |
187
|
|
|
$this->get('session')->getFlashBag()->add('error', |
188
|
|
|
$e->getMessage()); |
189
|
|
|
} |
190
|
|
|
} else { |
191
|
|
|
$this->get('session')->getFlashBag()->add('error', |
192
|
|
|
$translator->trans("Wasn't possible to remove this address.")); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $this->redirect($this->generateUrl('lc_person_addresses')); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
protected function getDeleteForms() |
199
|
|
|
{ |
200
|
|
|
$person = $this->getUser(); |
201
|
|
|
$deleteForms = array(); |
202
|
|
|
$addresses = $person->getAddresses(); |
203
|
|
|
|
204
|
|
|
if (is_array($addresses) || $addresses instanceof Collection) { |
205
|
|
|
foreach ($addresses as $address) { |
206
|
|
|
$data = array('address_id' => $address->getId()); |
207
|
|
|
$deleteForms[$address->getId()] = $this->createForm( |
208
|
|
|
'LoginCidadao\CoreBundle\Form\Type\RemovePersonAddressFormType', |
209
|
|
|
$data) |
210
|
|
|
->createView(); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $deleteForms; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function prepareAddressForEdition(PersonAddress $address) |
218
|
|
|
{ |
219
|
|
|
if ($address->getId() > 0) { |
220
|
|
|
$city = $address->getCity(); |
221
|
|
|
if ($city instanceof City) { |
|
|
|
|
222
|
|
|
if (!$address->getLocation() instanceof LocationSelectData) { |
|
|
|
|
223
|
|
|
$address->setLocation(new LocationSelectData()); |
224
|
|
|
} |
225
|
|
|
$state = $city->getState(); |
226
|
|
|
$country = $state->getCountry(); |
227
|
|
|
$address->getLocation()->setCity($city) |
228
|
|
|
->setState($state)->setCountry($country); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $address; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.