forked from sonata-project/ecommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerController.php
220 lines (184 loc) · 6.34 KB
/
CustomerController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\CustomerBundle\Controller;
use Sonata\Component\Customer\AddressInterface;
use Sonata\Component\Customer\AddressManagerInterface;
use Sonata\Component\Customer\CustomerManagerInterface;
use Sonata\CustomerBundle\Entity\BaseAddress;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class CustomerController
*
* @package Sonata\CustomerBundle\Controller
*
* @author Hugo Briand <briand@ekino.com>
*/
class CustomerController extends Controller
{
/**
* Lists customer's addresses
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addressesAction()
{
$customer = $this->getCustomer();
$addresses = array();
if (null === $customer) {
// Customer not yet created, the user didn't order yet
$customer = $this->getCustomerManager()->create();
$customer->setUser($this->getUser());
$this->getCustomerManager()->save($customer);
} else {
$custAddresses = $this->getAddressManager()->findBy(array('customer' => $customer));
$typeCodes = BaseAddress::getTypesList();
// This allows to specify the display order
$addresses = array(
$typeCodes[AddressInterface::TYPE_DELIVERY] => array(),
$typeCodes[AddressInterface::TYPE_BILLING] => array(),
$typeCodes[AddressInterface::TYPE_CONTACT] => array(),
);
foreach ($custAddresses as $address) {
$addresses[$address->getTypeCode()][] = $address;
}
}
return $this->render('SonataCustomerBundle:Addresses:list.html.twig', array(
'addresses' => $addresses,
'customer' => $customer,
'breadcrumb_context' => 'customer_address',
));
}
/**
* Adds an address to current customer
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addAddressAction()
{
return $this->updateAddress();
}
/**
* Controller action to edit address $id
*
* @param $id
*
* @return RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAddressAction($id)
{
return $this->updateAddress($id);
}
/**
* Deletes address $id
*
* @param $id The address to delete
*
* @return RedirectResponse
*/
public function deleteAddressAction($id)
{
if ($this->getRequest()->getMethod() !== 'POST') {
throw new MethodNotAllowedHttpException(array('POST'));
}
$address = $this->getAddressManager()->findOneBy(array('id' => $id));
$this->checkAddress($address);
$this->getAddressManager()->delete($address);
$this->get('session')->getFlashBag()->add('sonata_customer_success', 'customer_address_delete');
return new RedirectResponse($this->generateUrl('sonata_customer_addresses'));
}
/**
* Sets address $id to current
*
* @param $id
*
* @return RedirectResponse
*/
public function setCurrentAddressAction($id)
{
$address = $this->getAddressManager()->findOneBy(array('id' => $id));
$this->checkAddress($address);
$this->getAddressManager()->setCurrent($address);
return new RedirectResponse($this->generateUrl('sonata_customer_addresses'));
}
/**
* Updates or create an address
*
* @param int $id Address id
*
* @return RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
protected function updateAddress($id = null)
{
$customer = $this->getCustomer();
// Show address creation/edition form
if (null === $id) {
$form = $this->createForm('sonata_customer_address');
} else {
$address = $this->getAddressManager()->findOneBy(array('id' => $id));
$this->checkAddress($address);
$form = $this->createForm('sonata_customer_address', $address);
}
$template = 'SonataCustomerBundle:Addresses:new.html.twig';
if ($this->get('request')->getMethod() == 'POST') {
$form->bind($this->get('request'));
if ($form->isValid()) {
$address = $form->getData();
$customer->addAddress($address);
$this->getCustomerManager()->save($customer);
$this->get('session')->getFlashBag()->add('sonata_customer_success', $id ? 'address_edit_success' : 'address_add_success');
$url = $this->get('session')->get('sonata_address_redirect', $this->generateUrl('sonata_customer_addresses'));
return new RedirectResponse($url);
}
}
return $this->render($template, array(
'form' => $form->createView(),
'breadcrumb_context' => 'customer_address',
));
}
/**
* Checks if $address is valid
*
* @param AddressInterface $address
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
protected function checkAddress(AddressInterface $address = null)
{
if (null === $address
|| $address->getCustomer()->getId() !== $this->getCustomer()->getId()) {
throw new NotFoundHttpException;
}
}
/**
* @return \Sonata\Component\Customer\CustomerInterface
*/
protected function getCustomer()
{
$user = $this->getUser();
return $this->getCustomerManager()->findOneBy(array('user' => $user));
}
/**
* @return AddressManagerInterface
*/
protected function getAddressManager()
{
return $this->get('sonata.address.manager');
}
/**
* @return CustomerManagerInterface
*/
protected function getCustomerManager()
{
return $this->get('sonata.customer.manager');
}
}