1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Frontend |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Frontend\Customer; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default implementation of the customer frontend controller |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Frontend |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Frontend\Base |
|
|
|
|
22
|
|
|
implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Adds and returns a new customer item object |
26
|
|
|
* |
27
|
|
|
* @param array $values Values added to the newly created customer item like "customer.birthday" |
28
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer item |
29
|
|
|
* @since 2017.04 |
30
|
|
|
*/ |
31
|
|
|
public function addItem( array $values ) |
32
|
|
|
{ |
33
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' ); |
34
|
|
|
|
35
|
|
|
$item = $manager->createItem(); |
36
|
|
|
$item->fromArray( $values ); |
37
|
|
|
$manager->saveItem( $item ); |
38
|
|
|
|
39
|
|
|
return $item; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a new customer item object pre-filled with the given values but not yet stored |
45
|
|
|
* |
46
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer item |
47
|
|
|
*/ |
48
|
|
|
public function createItem( array $values = [] ) |
49
|
|
|
{ |
50
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' ); |
51
|
|
|
|
52
|
|
|
$item = $manager->createItem(); |
53
|
|
|
$item->fromArray( $values ); |
54
|
|
|
$item->setStatus( 1 ); |
55
|
|
|
|
56
|
|
|
return $item; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Deletes a customer item that belongs to the current authenticated user |
62
|
|
|
* |
63
|
|
|
* @param string $id Unique customer ID |
64
|
|
|
* @since 2017.04 |
65
|
|
|
*/ |
66
|
|
|
public function deleteItem( $id ) |
67
|
|
|
{ |
68
|
|
|
$this->checkUser( $id ); |
69
|
|
|
|
70
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' ); |
71
|
|
|
$manager->deleteItem( $id ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Updates the customer item identified by its ID |
77
|
|
|
* |
78
|
|
|
* @param string $id Unique customer ID |
79
|
|
|
* @param array $values Values added to the customer item like "customer.birthday" or "customer.city" |
80
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer item |
81
|
|
|
* @since 2017.04 |
82
|
|
|
*/ |
83
|
|
|
public function editItem( $id, array $values ) |
84
|
|
|
{ |
85
|
|
|
$this->checkUser( $id ); |
86
|
|
|
|
87
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' ); |
88
|
|
|
|
89
|
|
|
$item = $manager->getItem( $id, [], true ); |
|
|
|
|
90
|
|
|
$item->fromArray( $values ); |
91
|
|
|
$manager->saveItem( $item ); |
92
|
|
|
|
93
|
|
|
return $item; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the customer item for the given customer ID |
99
|
|
|
* |
100
|
|
|
* @param string|null $id Unique customer ID or null for current customer item |
101
|
|
|
* @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too |
102
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items |
103
|
|
|
* @since 2017.04 |
104
|
|
|
*/ |
105
|
|
|
public function getItem( $id = null, array $domains = [] ) |
106
|
|
|
{ |
107
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' ); |
108
|
|
|
|
109
|
|
|
if( $id === null ) { |
110
|
|
|
return $manager->getItem( $this->getContext()->getUserId(), $domains, true ); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->checkUser( $id ); |
114
|
|
|
|
115
|
|
|
return $manager->getItem( $id, $domains, true ); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the customer item for the given customer code (usually e-mail address) |
121
|
|
|
* |
122
|
|
|
* This method doesn't check if the customer item belongs to the logged in user! |
123
|
|
|
* |
124
|
|
|
* @param string $code Unique customer code |
125
|
|
|
* @param string[] $domains Domain names of items that are associated with the customers and that should be fetched too |
126
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer item including the referenced domains items |
127
|
|
|
* @since 2017.04 |
128
|
|
|
*/ |
129
|
|
|
public function findItem( $code, array $domains = [] ) |
130
|
|
|
{ |
131
|
|
|
return \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->findItem( $code, $domains ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Stores a modified customer item |
137
|
|
|
* |
138
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface Customer item |
139
|
|
|
*/ |
140
|
|
|
public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item ) |
141
|
|
|
{ |
142
|
|
|
\Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer' )->saveItem( $item ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Creates and returns a new item object |
148
|
|
|
* |
149
|
|
|
* @param array $values Values added to the newly created customer item like "customer.birthday" |
150
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer address item |
151
|
|
|
* @since 2017.04 |
152
|
|
|
*/ |
153
|
|
|
public function addAddressItem( array $values ) |
154
|
|
|
{ |
155
|
|
|
$context = $this->getContext(); |
156
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' ); |
157
|
|
|
|
158
|
|
|
$item = $manager->createItem(); |
159
|
|
|
$item->fromArray( $values ); |
160
|
|
|
$item->setParentId( $context->getUserId() ); |
161
|
|
|
$manager->saveItem( $item ); |
162
|
|
|
|
163
|
|
|
return $item; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Creates a new customer address item object pre-filled with the given values but not yet stored |
169
|
|
|
* |
170
|
|
|
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item |
171
|
|
|
*/ |
172
|
|
|
public function createAddressItem( array $values = [] ) |
173
|
|
|
{ |
174
|
|
|
$context = $this->getContext(); |
175
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/address' ); |
176
|
|
|
|
177
|
|
|
$item = $manager->createItem(); |
178
|
|
|
$item->fromArray( $values ); |
179
|
|
|
$item->setParentId( $context->getUserId() ); |
180
|
|
|
|
181
|
|
|
return $item; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Deletes a customer item that belongs to the current authenticated user |
187
|
|
|
* |
188
|
|
|
* @param string $id Unique customer address ID |
189
|
|
|
* @since 2017.04 |
190
|
|
|
*/ |
191
|
|
|
public function deleteAddressItem( $id ) |
192
|
|
|
{ |
193
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' ); |
194
|
|
|
|
195
|
|
|
$this->checkUser( $manager->getItem( $id, [], true )->getParentId() ); |
|
|
|
|
196
|
|
|
|
197
|
|
|
$manager->deleteItem( $id ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Saves a modified customer item object |
203
|
|
|
* |
204
|
|
|
* @param string $id Unique customer address ID |
205
|
|
|
* @param array $values Values added to the customer item like "customer.address.city" |
206
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Customer address item |
207
|
|
|
* @since 2017.04 |
208
|
|
|
*/ |
209
|
|
|
public function editAddressItem( $id, array $values ) |
210
|
|
|
{ |
211
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' ); |
212
|
|
|
|
213
|
|
|
$item = $manager->getItem( $id, [], true ); |
|
|
|
|
214
|
|
|
$this->checkUser( $item->getParentId() ); |
215
|
|
|
|
216
|
|
|
$item->fromArray( $values ); |
217
|
|
|
$manager->saveItem( $item ); |
218
|
|
|
|
219
|
|
|
return $item; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Returns the customer item for the given customer ID |
225
|
|
|
* |
226
|
|
|
* @param string $id Unique customer address ID |
227
|
|
|
* @return \Aimeos\MShop\Customer\Item\Address\Iface Customer address item |
228
|
|
|
* @since 2017.04 |
229
|
|
|
*/ |
230
|
|
|
public function getAddressItem( $id ) |
231
|
|
|
{ |
232
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' ); |
233
|
|
|
$item = $manager->getItem( $id ); |
234
|
|
|
|
235
|
|
|
$this->checkUser( $item->getParentId() ); |
236
|
|
|
|
237
|
|
|
return $item; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Stores a modified customer address item |
243
|
|
|
* |
244
|
|
|
* @param \Aimeos\MShop\Customer\Item\Address\Iface Customer address item |
245
|
|
|
*/ |
246
|
|
|
public function saveAddressItem( \Aimeos\MShop\Customer\Item\Address\Iface $item ) |
247
|
|
|
{ |
248
|
|
|
\Aimeos\MShop\Factory::createManager( $this->getContext(), 'customer/address' )->saveItem( $item ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Checks if the current user is allowed to retrieve the customer data for the given ID |
254
|
|
|
* |
255
|
|
|
* @param string $id Unique customer ID |
256
|
|
|
* @throws \Aimeos\Controller\Frontend\Customer\Exception If access isn't allowed |
257
|
|
|
*/ |
258
|
|
|
protected function checkUser( $id ) |
259
|
|
|
{ |
260
|
|
|
if( $id !== $this->getContext()->getUserId() ) |
261
|
|
|
{ |
262
|
|
|
$msg = sprintf( 'Not allowed to access customer data for ID "%1$s"', $id ); |
263
|
|
|
throw new \Aimeos\Controller\Frontend\Customer\Exception( $msg ); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|