Total Complexity | 36 |
Total Lines | 496 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
21 | class Standard |
||
22 | extends Base |
||
23 | implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
24 | { |
||
25 | private $baskets = []; |
||
26 | private $domainManager; |
||
27 | private $type = 'default'; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Initializes the frontend controller. |
||
32 | * |
||
33 | * @param \Aimeos\MShop\Context\Item\Iface $context Object storing the required instances for manaing databases |
||
34 | * connections, logger, session, etc. |
||
35 | */ |
||
36 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
37 | { |
||
38 | parent::__construct( $context ); |
||
39 | |||
40 | $this->domainManager = \Aimeos\MShop::create( $context, 'order/base' ); |
||
41 | } |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Empties the basket and removing all products, addresses, services, etc. |
||
46 | * |
||
47 | * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object |
||
48 | */ |
||
49 | public function clear() |
||
50 | { |
||
51 | $this->baskets[$this->type] = $this->domainManager->createItem(); |
||
52 | $this->domainManager->setSession( $this->baskets[$this->type], $this->type ); |
||
1 ignored issue
–
show
|
|||
53 | |||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | |||
58 | /** |
||
59 | * Returns the basket object. |
||
60 | * |
||
61 | * @return \Aimeos\MShop\Order\Item\Base\Iface Basket holding products, addresses and delivery/payment options |
||
62 | */ |
||
63 | public function get() |
||
64 | { |
||
65 | if( !isset( $this->baskets[$this->type] ) ) |
||
66 | { |
||
67 | $this->baskets[$this->type] = $this->domainManager->getSession( $this->type ); |
||
1 ignored issue
–
show
|
|||
68 | $this->checkLocale( $this->baskets[$this->type]->getLocale(), $this->type ); |
||
69 | } |
||
70 | |||
71 | return $this->baskets[$this->type]; |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Explicitely persists the basket content |
||
77 | * |
||
78 | * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object |
||
79 | */ |
||
80 | public function save() |
||
81 | { |
||
82 | if( isset( $this->baskets[$this->type] ) && $this->baskets[$this->type]->isModified() ) { |
||
83 | $this->domainManager->setSession( $this->baskets[$this->type], $this->type ); |
||
84 | } |
||
85 | |||
86 | return $this; |
||
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Sets the new basket type |
||
92 | * |
||
93 | * @param string $type Basket type |
||
94 | * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object |
||
95 | */ |
||
96 | public function setType( $type ) |
||
100 | } |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Creates a new order base object from the current basket |
||
105 | * |
||
106 | * @return \Aimeos\MShop\Order\Item\Base\Iface Order base object including products, addresses and services |
||
107 | */ |
||
108 | public function store() |
||
109 | { |
||
110 | $total = 0; |
||
111 | $context = $this->getContext(); |
||
112 | $config = $context->getConfig(); |
||
113 | |||
114 | /** controller/frontend/basket/limit-count |
||
115 | * Maximum number of orders within the time frame |
||
116 | * |
||
117 | * Creating new orders is limited to avoid abuse and mitigate denial of |
||
118 | * service attacks. The number of orders created within the time frame |
||
119 | * configured by "controller/frontend/basket/limit-seconds" are counted |
||
120 | * before a new order of the same user (either logged in or identified |
||
121 | * by the IP address) is created. If the number of orders is higher than |
||
122 | * the configured value, an error message will be shown to the user |
||
123 | * instead of creating a new order. |
||
124 | * |
||
125 | * @param integer Number of orders allowed within the time frame |
||
126 | * @since 2017.05 |
||
127 | * @category Developer |
||
128 | * @see controller/frontend/basket/limit-seconds |
||
129 | */ |
||
130 | $count = $config->get( 'controller/frontend/basket/limit-count', 5 ); |
||
131 | |||
132 | /** controller/frontend/basket/limit-seconds |
||
133 | * Order limitation time frame in seconds |
||
134 | * |
||
135 | * Creating new orders is limited to avoid abuse and mitigate denial of |
||
136 | * service attacks. Within the configured time frame, only a limited |
||
137 | * number of orders can be created. All orders of the current user |
||
138 | * (either logged in or identified by the IP address) within the last X |
||
139 | * seconds are counted. If the total value is higher then the number |
||
140 | * configured in "controller/frontend/basket/limit-count", an error |
||
141 | * message will be shown to the user instead of creating a new order. |
||
142 | * |
||
143 | * @param integer Number of seconds to check orders within |
||
144 | * @since 2017.05 |
||
145 | * @category Developer |
||
146 | * @see controller/frontend/basket/limit-count |
||
147 | */ |
||
148 | $seconds = $config->get( 'controller/frontend/basket/limit-seconds', 300 ); |
||
149 | |||
150 | $search = $this->domainManager->createSearch(); |
||
151 | $expr = [ |
||
152 | $search->compare( '==', 'order.base.editor', $context->getEditor() ), |
||
153 | $search->compare( '>=', 'order.base.ctime', date( 'Y-m-d H:i:s', time() - $seconds ) ), |
||
154 | ]; |
||
155 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
156 | $search->setSlice( 0, 0 ); |
||
157 | |||
158 | $this->domainManager->searchItems( $search, [], $total ); |
||
159 | |||
160 | if( $total > $count ) |
||
161 | { |
||
162 | $msg = $context->getI18n()->dt( 'controller/frontend', 'Temporary order limit reached' ); |
||
163 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $msg ); |
||
164 | } |
||
165 | |||
166 | |||
167 | $basket = $this->get()->finish(); |
||
168 | $basket->setCustomerId( (string) $context->getUserId() ); |
||
169 | |||
170 | $this->domainManager->begin(); |
||
171 | $this->domainManager->store( $basket ); |
||
1 ignored issue
–
show
|
|||
172 | $this->domainManager->commit(); |
||
173 | |||
174 | $this->save(); // for reusing unpaid orders, might have side effects (!) |
||
175 | $this->createSubscriptions( $basket ); |
||
176 | |||
177 | return $basket; |
||
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Returns the order base object for the given ID |
||
183 | * |
||
184 | * @param string $id Unique ID of the order base object |
||
185 | * @param integer $parts Constants which parts of the order base object should be loaded |
||
186 | * @param boolean $default True to add default criteria (user logged in), false if not |
||
187 | * @return \Aimeos\MShop\Order\Item\Base\Iface Order base object including the given parts |
||
188 | */ |
||
189 | public function load( $id, $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, $default = true ) |
||
190 | { |
||
191 | return $this->domainManager->load( $id, $parts, false, $default ); |
||
1 ignored issue
–
show
|
|||
192 | } |
||
193 | |||
194 | |||
195 | /** |
||
196 | * Adds a categorized product to the basket of the user stored in the session. |
||
197 | * |
||
198 | * @param string $prodid ID of the base product to add |
||
199 | * @param integer $quantity Amount of products that should by added |
||
200 | * @param array $variantAttributeIds List of variant-building attribute IDs that identify a specific product |
||
201 | * in a selection products |
||
202 | * @param array $configAttributeIds List of attribute IDs that doesn't identify a specific product in a |
||
203 | * selection of products but are stored together with the product (e.g. for configurable products) |
||
204 | * @param array $hiddenAttributeIds Deprecated |
||
205 | * @param array $customAttributeValues Associative list of attribute IDs and arbitrary values that should be stored |
||
206 | * along with the product in the order |
||
207 | * @param string $stocktype Unique code of the stock type to deliver the products from |
||
208 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available |
||
209 | */ |
||
210 | public function addProduct( $prodid, $quantity = 1, $stocktype = 'default', array $variantAttributeIds = [], |
||
211 | array $configAttributeIds = [], array $hiddenAttributeIds = [], array $customAttributeValues = [] ) |
||
212 | { |
||
213 | $attributeMap = [ |
||
214 | 'custom' => array_keys( $customAttributeValues ), |
||
215 | 'config' => array_keys( $configAttributeIds ), |
||
216 | ]; |
||
217 | $this->checkListRef( $prodid, 'attribute', $attributeMap ); |
||
218 | |||
219 | |||
220 | $context = $this->getContext(); |
||
221 | $productManager = \Aimeos\MShop::create( $context, 'product' ); |
||
222 | |||
223 | $productItem = $productManager->getItem( $prodid, ['attribute', 'media', 'supplier', 'price', 'product', 'text'], true ); |
||
224 | $prices = $productItem->getRefItems( 'price', 'default', 'default' ); |
||
1 ignored issue
–
show
|
|||
225 | $hidden = $productItem->getRefItems( 'attribute', null, 'hidden' ); |
||
226 | |||
227 | $orderBaseProductItem = \Aimeos\MShop::create( $context, 'order/base/product' )->createItem(); |
||
228 | $orderBaseProductItem->copyFrom( $productItem )->setQuantity( $quantity )->setStockType( $stocktype ); |
||
229 | |||
230 | $custAttr = $this->getOrderProductAttributes( 'custom', array_keys( $customAttributeValues ), $customAttributeValues ); |
||
231 | $confAttr = $this->getOrderProductAttributes( 'config', array_keys( $configAttributeIds ), [], $configAttributeIds ); |
||
232 | $attr = array_merge( $custAttr, $confAttr, $this->getOrderProductAttributes( 'hidden', array_keys( $hidden ) ) ); |
||
233 | |||
234 | $orderBaseProductItem->setAttributeItems( $attr ); |
||
235 | $orderBaseProductItem->setPrice( $this->calcPrice( $orderBaseProductItem, $prices, $quantity ) ); |
||
236 | |||
237 | $this->get()->addProduct( $orderBaseProductItem ); |
||
238 | $this->save(); |
||
239 | } |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Deletes a product item from the basket. |
||
244 | * |
||
245 | * @param integer $position Position number (key) of the order product item |
||
246 | */ |
||
247 | public function deleteProduct( $position ) |
||
248 | { |
||
249 | $product = $this->get()->getProduct( $position ); |
||
250 | |||
251 | if( $product->getFlags() === \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ) |
||
252 | { |
||
253 | $msg = $this->getContext()->getI18n()->dt( 'controller/frontend', 'Basket item at position "%1$d" cannot be deleted manually' ); |
||
254 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $position ) ); |
||
255 | } |
||
256 | |||
257 | $this->get()->deleteProduct( $position ); |
||
258 | $this->save(); |
||
259 | } |
||
260 | |||
261 | |||
262 | /** |
||
263 | * Edits the quantity of a product item in the basket. |
||
264 | * |
||
265 | * @param integer $position Position number (key) of the order product item |
||
266 | * @param integer $quantity New quantiy of the product item |
||
267 | * @param string[] $configAttributeCodes Codes of the product config attributes that should be REMOVED |
||
268 | */ |
||
269 | public function editProduct( $position, $quantity, array $configAttributeCodes = [] ) |
||
270 | { |
||
271 | $product = $this->get()->getProduct( $position ); |
||
272 | |||
273 | if( $product->getFlags() & \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ) |
||
274 | { |
||
275 | $msg = $this->getContext()->getI18n()->dt( 'controller/frontend', 'Basket item at position "%1$d" cannot be changed' ); |
||
276 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $position ) ); |
||
277 | } |
||
278 | |||
279 | $product->setQuantity( $quantity ); |
||
280 | |||
281 | $attributes = $product->getAttributeItems(); |
||
282 | foreach( $attributes as $key => $attribute ) |
||
283 | { |
||
284 | if( in_array( $attribute->getCode(), $configAttributeCodes ) ) { |
||
285 | unset( $attributes[$key] ); |
||
286 | } |
||
287 | } |
||
288 | $product->setAttributeItems( $attributes ); |
||
289 | |||
290 | $manager = \Aimeos\MShop::create( $this->getContext(), 'product' ); |
||
291 | $productItem = $manager->findItem( $product->getProductCode(), array( 'price', 'text' ), true ); |
||
1 ignored issue
–
show
|
|||
292 | $product->setPrice( $this->calcPrice( $product, $productItem->getRefItems( 'price', 'default' ), $quantity ) ); |
||
293 | |||
294 | $this->get()->addProduct( $product, $position ); |
||
295 | |||
296 | $this->save(); |
||
297 | } |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Adds the given coupon code and updates the basket. |
||
302 | * |
||
303 | * @param string $code Coupon code entered by the user |
||
304 | * @throws \Aimeos\Controller\Frontend\Basket\Exception if the coupon code is invalid or not allowed |
||
305 | */ |
||
306 | public function addCoupon( $code ) |
||
307 | { |
||
308 | $context = $this->getContext(); |
||
309 | |||
310 | /** controller/frontend/basket/standard/coupon/allowed |
||
311 | * Number of coupon codes a customer is allowed to enter |
||
312 | * |
||
313 | * This configuration option enables shop owners to limit the number of coupon |
||
314 | * codes that can be added by a customer to his current basket. By default, only |
||
315 | * one coupon code is allowed per order. |
||
316 | * |
||
317 | * Coupon codes are valid until a payed order is placed by the customer. The |
||
318 | * "count" of the codes is decreased afterwards. If codes are not personalized |
||
319 | * the codes can be reused in the next order until their "count" reaches zero. |
||
320 | * |
||
321 | * @param integer Positive number of coupon codes including zero |
||
322 | * @since 2017.08 |
||
323 | * @category User |
||
324 | * @category Developer |
||
325 | */ |
||
326 | $allowed = $context->getConfig()->get( 'controller/frontend/basket/standard/coupon/allowed', 1 ); |
||
327 | |||
328 | if( $allowed <= count( $this->get()->getCoupons() ) ) |
||
329 | { |
||
330 | $msg = $context->getI18n()->dt( 'controller/frontend', 'Number of coupon codes exceeds the limit' ); |
||
331 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $msg ); |
||
332 | } |
||
333 | |||
334 | |||
335 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
336 | $codeManager = \Aimeos\MShop::create( $context, 'coupon/code' ); |
||
337 | |||
338 | $search = $manager->createSearch( true )->setSlice( 0, 1 ); |
||
339 | $expr = [ |
||
340 | $search->compare( '==', 'coupon.code.code', $code ), |
||
341 | $codeManager->createSearch( true )->getConditions(), |
||
342 | $search->getConditions(), |
||
343 | ]; |
||
344 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
345 | |||
346 | $result = $manager->searchItems( $search ); |
||
347 | |||
348 | if( ( $item = reset( $result ) ) === false ) |
||
349 | { |
||
350 | $msg = sprintf( $context->getI18n()->dt( 'controller/frontend', 'Coupon code "%1$s" is invalid or not available any more' ), $code ); |
||
351 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $msg ); |
||
352 | } |
||
353 | |||
354 | |||
355 | $provider = $manager->getProvider( $item, $code ); |
||
1 ignored issue
–
show
|
|||
356 | |||
357 | if( $provider->isAvailable( $this->get() ) !== true ) |
||
358 | { |
||
359 | $msg = sprintf( $context->getI18n()->dt( 'controller/frontend', 'Requirements for coupon code "%1$s" aren\'t met' ), $code ); |
||
360 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $msg ); |
||
361 | } |
||
362 | |||
363 | $provider->addCoupon( $this->get() ); |
||
364 | $this->save(); |
||
365 | } |
||
366 | |||
367 | |||
368 | /** |
||
369 | * Removes the given coupon code and its effects from the basket. |
||
370 | * |
||
371 | * @param string $code Coupon code entered by the user |
||
372 | * @throws \Aimeos\Controller\Frontend\Basket\Exception if the coupon code is invalid |
||
373 | */ |
||
374 | public function deleteCoupon( $code ) |
||
375 | { |
||
376 | $context = $this->getContext(); |
||
377 | $manager = \Aimeos\MShop::create( $context, 'coupon' ); |
||
378 | |||
379 | $search = $manager->createSearch(); |
||
380 | $search->setConditions( $search->compare( '==', 'coupon.code.code', $code ) ); |
||
381 | $search->setSlice( 0, 1 ); |
||
382 | |||
383 | $result = $manager->searchItems( $search ); |
||
384 | |||
385 | if( ( $item = reset( $result ) ) === false ) |
||
386 | { |
||
387 | $msg = $context->getI18n()->dt( 'controller/frontend', 'Coupon code "%1$s" is invalid' ); |
||
388 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $code ) ); |
||
389 | } |
||
390 | |||
391 | $manager->getProvider( $item, $code )->deleteCoupon( $this->get() ); |
||
392 | $this->save(); |
||
393 | } |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Sets the address of the customer in the basket. |
||
398 | * |
||
399 | * @param string $type Address type constant from \Aimeos\MShop\Order\Item\Base\Address\Base |
||
400 | * @param \Aimeos\MShop\Common\Item\Address\Iface|array|null $value Address object or array with key/value pairs of address or null to remove address from basket |
||
401 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the billing or delivery address is not of any required type of |
||
402 | * if one of the keys is invalid when using an array with key/value pairs |
||
403 | */ |
||
404 | public function setAddress( $type, $value ) |
||
405 | { |
||
406 | $context = $this->getContext(); |
||
407 | $address = \Aimeos\MShop::create( $context, 'order/base/address' )->createItem(); |
||
408 | $address->setType( $type ); |
||
409 | |||
410 | if( $value instanceof \Aimeos\MShop\Common\Item\Address\Iface ) |
||
411 | { |
||
412 | $this->get()->setAddress( $address->copyFrom( $value ), $type ); |
||
413 | } |
||
414 | else if( is_array( $value ) ) |
||
415 | { |
||
416 | $this->get()->setAddress( $this->setAddressFromArray( $address, $value ), $type ); |
||
417 | } |
||
418 | else if( $value === null ) |
||
419 | { |
||
420 | $this->get()->deleteAddress( $type ); |
||
421 | } |
||
422 | else |
||
423 | { |
||
424 | $msg = $context->getI18n()->dt( 'controller/frontend', 'Invalid value for address type "%1$s"' ); |
||
425 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $type ) ); |
||
426 | } |
||
427 | |||
428 | $this->save(); |
||
429 | } |
||
430 | |||
431 | |||
432 | /** |
||
433 | * Adds the delivery/payment service item based on the service ID. |
||
434 | * |
||
435 | * @param string $type Service type code like 'payment' or 'delivery' |
||
436 | * @param string $id|null Unique ID of the service item or null to remove it |
||
437 | * @param array $attributes Associative list of key/value pairs containing the attributes selected or |
||
438 | * entered by the customer when choosing one of the delivery or payment options |
||
439 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If there is no price to the service item attached |
||
440 | */ |
||
441 | public function addService( $type, $id, array $attributes = [] ) |
||
442 | { |
||
443 | $context = $this->getContext(); |
||
444 | |||
445 | $serviceManager = \Aimeos\MShop::create( $context, 'service' ); |
||
446 | $serviceItem = $serviceManager->getItem( $id, array( 'media', 'price', 'text' ) ); |
||
447 | |||
448 | $provider = $serviceManager->getProvider( $serviceItem, $serviceItem->getType() ); |
||
1 ignored issue
–
show
|
|||
449 | $result = $provider->checkConfigFE( $attributes ); |
||
450 | $unknown = array_diff_key( $attributes, $result ); |
||
451 | |||
452 | if( count( $unknown ) > 0 ) |
||
453 | { |
||
454 | $msg = $context->getI18n()->dt( 'controller/frontend', 'Unknown attributes "%1$s"' ); |
||
455 | $msg = sprintf( $msg, implode( '","', array_keys( $unknown ) ) ); |
||
456 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $msg ); |
||
457 | } |
||
458 | |||
459 | foreach( $result as $key => $value ) |
||
460 | { |
||
461 | if( $value !== null ) { |
||
462 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $value ); |
||
463 | } |
||
464 | } |
||
465 | |||
466 | $orderBaseServiceManager = \Aimeos\MShop::create( $context, 'order/base/service' ); |
||
467 | $orderServiceItem = $orderBaseServiceManager->createItem(); |
||
468 | $orderServiceItem->copyFrom( $serviceItem ); |
||
469 | |||
470 | // remove service rebate of original price |
||
471 | $price = $provider->calcPrice( $this->get() )->setRebate( '0.00' ); |
||
472 | $orderServiceItem->setPrice( $price ); |
||
473 | |||
474 | $provider->setConfigFE( $orderServiceItem, $attributes ); |
||
475 | |||
476 | $this->get()->addService( $orderServiceItem, $type ); |
||
477 | $this->save(); |
||
478 | } |
||
479 | |||
480 | |||
481 | /** |
||
482 | * Removes the delivery or payment service items from the basket |
||
483 | * |
||
484 | * @param string $type Service type code like 'payment' or 'delivery' |
||
485 | */ |
||
486 | public function deleteService( $type ) |
||
490 | } |
||
491 | |||
492 | |||
493 | /** |
||
494 | * Fills the order address object with the values from the array. |
||
495 | * |
||
496 | * @param \Aimeos\MShop\Order\Item\Base\Address\Iface $address Address item to store the values into |
||
497 | * @param array $map Associative array of key/value pairs. The keys must be the same as when calling toArray() from |
||
498 | * an address item. |
||
499 | * @return \Aimeos\MShop\Order\Item\Base\Address\Iface Updated address item |
||
500 | * @throws \Aimeos\Controller\Frontend\Basket\Exception |
||
501 | */ |
||
502 | protected function setAddressFromArray( \Aimeos\MShop\Order\Item\Base\Address\Iface $address, array $map ) |
||
517 | } |
||
518 | } |
||
519 |