Passed
Push — master ( e00f2d...85cd17 )
by Aimeos
02:59
created

Standard::domains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Basket\Standard;
12
13
14
/**
15
 * Default implementation of standard basket HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Basket\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/basket/standard/name
25
	 * Class name of the used basket standard client implementation
26
	 *
27
	 * Each default HTML client can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the client factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Client\Html\Basket\Standard\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Client\Html\Basket\Standard\Mybasket
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  client/html/basket/standard/name = Mybasket
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyBasket"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 */
56
57
58
	/**
59
	 * Sets the necessary parameter values in the view.
60
	 *
61
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
62
	 * @param array &$tags Result array for the list of tags that are associated to the output
63
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
64
	 * @return \Aimeos\Base\View\Iface Modified view object
65
	 */
66
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
67
	{
68
		$context = $this->context();
69
		$site = $context->locale()->getSiteItem()->getCode();
70
71
		$view->standardBackUrl = $context->session()->get( 'aimeos/catalog/last/' . $site );
72
		$view->standardBasket = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' )->get();
73
74
		return parent::data( $view, $tags, $expire );
75
	}
76
77
78
	/**
79
	 * Sets the necessary parameter values in the view.
80
	 *
81
	 * A view must be available and this method doesn't generate any output
82
	 * besides setting view variables if necessary.
83
	 */
84
	public function init()
85
	{
86
		$view = $this->view();
87
		$context = $this->context();
88
		$controller = \Aimeos\Controller\Frontend::create( $context, 'basket' );
89
90
		try
91
		{
92
			switch( $view->param( 'b_action' ) )
93
			{
94
				case 'add':
95
					$this->addProducts( $view );
96
					break;
97
				case 'coupon-delete':
98
					$this->deleteCoupon( $view );
99
					break;
100
				case 'delete':
101
					$this->deleteProducts( $view );
102
					break;
103
				case 'save':
104
					$this->saveBasket( $view );
105
					break;
106
				default:
107
					$this->updateProducts( $view );
108
					$this->addCoupon( $view );
109
			}
110
111
			/** client/html/basket/standard/check
112
			 * Alters the behavior of the product checks before continuing with the checkout
113
			 *
114
			 * By default, the product related checks are performed every time the basket
115
			 * is shown. They test if there are any products in the basket and execute all
116
			 * basket plugins that have been registered for the "check.before" and "check.after"
117
			 * events.
118
			 *
119
			 * Using this configuration setting, you can either disable all checks completely
120
			 * (0) or display a "Check" button instead of the "Checkout" button (2). In the
121
			 * later case, customers have to click on the "Check" button first to perform
122
			 * the checks and if everything is OK, the "Checkout" button will be displayed
123
			 * that allows the customers to continue the checkout process. If one of the
124
			 * checks fails, the customers have to fix the related basket item and must click
125
			 * on the "Check" button again before they can continue.
126
			 *
127
			 * Available values are:
128
			 *  0 = no product related checks
129
			 *  1 = checks are performed every time when the basket is displayed
130
			 *  2 = checks are performed only when clicking on the "check" button
131
			 *
132
			 * @param integer One of the allowed values (0, 1 or 2)
133
			 * @since 2016.08
134
			 */
135
			$check = (int) $view->config( 'client/html/basket/standard/check', 1 );
136
137
			switch( $check )
138
			{
139
				case 2: if( $view->param( 'b_check', 0 ) == 0 ) { break; }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
140
				case 1: $controller->get()->check( ['order/product'] );
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
141
				default: $view->standardCheckout = true;
142
			}
143
		}
144
		catch( \Exception $e )
145
		{
146
			$controller->save();
147
			throw $e;
148
		}
149
150
		$controller->save();
151
	}
152
153
154
	/**
155
	 * Adds the coupon specified by the view parameters from the basket.
156
	 *
157
	 * @param \Aimeos\Base\View\Iface $view View object
158
	 */
159
	protected function addCoupon( \Aimeos\Base\View\Iface $view )
160
	{
161
		if( ( $coupon = $view->param( 'b_coupon' ) ) != '' )
162
		{
163
			$context = $this->context();
164
			$cntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
165
			$code = $cntl->get()->getCoupons()->keys()->first();
166
167
			/** client/html/basket/standard/coupon/overwrite
168
			 * Replace previous coupon codes each time the user enters a new one
169
			 *
170
			 * If you want to allow only one coupon code per order and replace a
171
			 * previously entered one automatically, this configuration option
172
			 * should be set to true.
173
			 *
174
			 * @param boolean True to overwrite a previous coupon, false to keep them
175
			 * @since 2020.04
176
			 */
177
			if( $code && $context->config()->get( 'client/html/basket/standard/coupon/overwrite', false ) ) {
178
				$cntl->deleteCoupon( $code );
179
			}
180
181
			$cntl->addCoupon( $coupon );
182
			$this->clearCached();
183
		}
184
	}
185
186
187
	/**
188
	 * Adds the products specified by the view parameters to the basket.
189
	 *
190
	 * @param \Aimeos\Base\View\Iface $view View object
191
	 */
192
	protected function addProducts( \Aimeos\Base\View\Iface $view )
193
	{
194
		$context = $this->context();
195
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
196
		$productCntl = \Aimeos\Controller\Frontend::create( $context, 'product' )->uses( $this->call( 'domains' ) );
197
198
		if( ( $prodid = $view->param( 'b_prodid', '' ) ) !== '' && $view->param( 'b_quantity', 0 ) > 0 )
199
		{
200
			$basketCntl->addProduct(
201
				$productCntl->get( $prodid ),
202
				(float) $view->param( 'b_quantity', 0 ),
203
				(array) $view->param( 'b_attrvarid', [] ),
204
				$this->getAttributeMap( $view->param( 'b_attrconfid', [] ) ),
205
				array_filter( (array) $view->param( 'b_attrcustid', [] ) ),
206
				(string) $view->param( 'b_stocktype', 'default' ),
207
				$view->param( 'b_siteid' )
208
			);
209
		}
210
		else
211
		{
212
			foreach( (array) $view->param( 'b_prod', [] ) as $values )
213
			{
214
				if( ( $values['prodid'] ?? null ) && ( $values['quantity'] ?? 0 ) > 0 )
215
				{
216
					$basketCntl->addProduct( $productCntl->get( $values['prodid'] ),
217
						(float) ( $values['quantity'] ?? 0 ),
218
						array_filter( (array) ( $values['attrvarid'] ?? [] ) ),
219
						$this->getAttributeMap( (array) ( $values['attrconfid'] ?? [] ) ),
220
						array_filter( (array) ( $values['attrcustid'] ?? [] ) ),
221
						(string) ( $values['stocktype'] ?? 'default' ),
222
						$values['siteid'] ?? null
223
					);
224
				}
225
			}
226
		}
227
228
		$this->clearCached();
229
	}
230
231
232
	/**
233
	 * Removes the coupon specified by the view parameters from the basket.
234
	 *
235
	 * @param \Aimeos\Base\View\Iface $view View object
236
	 */
237
	protected function deleteCoupon( \Aimeos\Base\View\Iface $view )
238
	{
239
		if( ( $coupon = $view->param( 'b_coupon' ) ) != '' )
240
		{
241
			\Aimeos\Controller\Frontend::create( $this->context(), 'basket' )->deleteCoupon( $coupon );
242
			$this->clearCached();
243
		}
244
	}
245
246
247
	/**
248
	 * Removes the products specified by the view parameters from the basket.
249
	 *
250
	 * @param \Aimeos\Base\View\Iface $view View object
251
	 */
252
	protected function deleteProducts( \Aimeos\Base\View\Iface $view )
253
	{
254
		$controller = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' );
255
		$products = (array) $view->param( 'b_position', [] );
256
257
		foreach( $products as $position ) {
258
			$controller->deleteProduct( $position );
259
		}
260
261
		$this->clearCached();
262
	}
263
264
265
	/**
266
	 * Returns the name of the domains that should be fetched together with the product
267
	 *
268
	 * @return array Domain names
269
	 */
270
	protected function domains() : array
271
	{
272
		return ['attribute', 'catalog', 'media', 'price', 'product', 'text', 'locale/site'];
273
	}
274
275
276
	/**
277
	 * Returns the configurable attribute values as ID/quantity pairs
278
	 *
279
	 * @param array $values Associative list which "id" and "qty" keys
280
	 * @return array Pairs of config attribute ID/quantity pairs
281
	 */
282
	protected function getAttributeMap( array $values )
283
	{
284
		$list = [];
285
		$confIds = ( isset( $values['id'] ) ? array_filter( (array) $values['id'] ) : [] );
286
		$confQty = ( isset( $values['qty'] ) ? array_filter( (array) $values['qty'] ) : [] );
287
288
		foreach( $confIds as $idx => $id )
289
		{
290
			if( isset( $confQty[$idx] ) && $confQty[$idx] > 0 ) {
291
				$list[$id] = $confQty[$idx];
292
			}
293
		}
294
295
		return $list;
296
	}
297
298
299
	/**
300
	 * Saves the basket of the user permanently
301
	 *
302
	 * @param \Aimeos\Base\View\Iface $view View object
303
	 */
304
	protected function saveBasket( \Aimeos\Base\View\Iface $view )
305
	{
306
		$context = $this->context();
307
308
		if( ( $userId = $context->user() ) === null )
309
		{
310
			$msg = $view->translate( 'client', 'You must log in first' );
311
			$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] );
312
313
			return;
314
		}
315
316
		$manager = \Aimeos\MShop::create( $context, 'order/basket' );
317
318
		$item = $manager->create()->setId( md5( microtime( true ) . getmypid() . rand() ) )
319
			->setCustomerId( $userId )->setName( $view->param( 'b_name', date( 'Y-m-d H:i:s' ) ) )
320
			->setItem( \Aimeos\Controller\Frontend::create( $context, 'basket' )->get() );
321
322
		$manager->save( $item );
323
324
		$msg = $view->translate( 'client', 'Basket saved sucessfully' );
325
		$view->infos = array_merge( $view->get( 'infos', [] ), [$msg] );
326
	}
327
328
329
	/**
330
	 * Edits the products specified by the view parameters to the basket.
331
	 *
332
	 * @param \Aimeos\Base\View\Iface $view View object
333
	 */
334
	protected function updateProducts( \Aimeos\Base\View\Iface $view )
335
	{
336
		$controller = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' );
337
		$products = (array) $view->param( 'b_prod', [] );
338
339
		if( ( $position = $view->param( 'b_position', '' ) ) !== '' )
340
		{
341
			$products[] = array(
342
				'position' => $position,
343
				'quantity' => $view->param( 'b_quantity', 1 )
344
			);
345
		}
346
347
		foreach( $products as $values )
348
		{
349
			$controller->updateProduct(
350
				( isset( $values['position'] ) ? (int) $values['position'] : 0 ),
351
				( isset( $values['quantity'] ) ? (float) $values['quantity'] : 1 )
352
			);
353
		}
354
355
		$this->clearCached();
356
	}
357
358
	/** client/html/basket/standard/template-body
359
	 * Relative path to the HTML body template of the basket standard client.
360
	 *
361
	 * The template file contains the HTML code and processing instructions
362
	 * to generate the result shown in the body of the frontend. The
363
	 * configuration string is the path to the template file relative
364
	 * to the templates directory (usually in templates/client/html).
365
	 *
366
	 * You can overwrite the template file configuration in extensions and
367
	 * provide alternative templates. These alternative templates should be
368
	 * named like the default one but suffixed by
369
	 * an unique name. You may use the name of your project for this. If
370
	 * you've implemented an alternative client class as well, it
371
	 * should be suffixed by the name of the new class.
372
	 *
373
	 * @param string Relative path to the template creating code for the HTML page body
374
	 * @since 2014.03
375
	 * @see client/html/basket/standard/template-header
376
	 */
377
378
	/** client/html/basket/standard/template-header
379
	 * Relative path to the HTML header template of the basket standard client.
380
	 *
381
	 * The template file contains the HTML code and processing instructions
382
	 * to generate the HTML code that is inserted into the HTML page header
383
	 * of the rendered page in the frontend. The configuration string is the
384
	 * path to the template file relative to the templates directory (usually
385
	 * in templates/client/html).
386
	 *
387
	 * You can overwrite the template file configuration in extensions and
388
	 * provide alternative templates. These alternative templates should be
389
	 * named like the default one but suffixed by
390
	 * an unique name. You may use the name of your project for this. If
391
	 * you've implemented an alternative client class as well, it
392
	 * should be suffixed by the name of the new class.
393
	 *
394
	 * @param string Relative path to the template creating code for the HTML page head
395
	 * @since 2014.03
396
	 * @see client/html/basket/standard/template-body
397
	 */
398
399
	/** client/html/basket/standard/decorators/excludes
400
	 * Excludes decorators added by the "common" option from the basket standard html client
401
	 *
402
	 * Decorators extend the functionality of a class by adding new aspects
403
	 * (e.g. log what is currently done), executing the methods of the underlying
404
	 * class only in certain conditions (e.g. only for logged in users) or
405
	 * modify what is returned to the caller.
406
	 *
407
	 * This option allows you to remove a decorator added via
408
	 * "client/html/common/decorators/default" before they are wrapped
409
	 * around the html client.
410
	 *
411
	 *  client/html/basket/standard/decorators/excludes = array( 'decorator1' )
412
	 *
413
	 * This would remove the decorator named "decorator1" from the list of
414
	 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
415
	 * "client/html/common/decorators/default" to the html client.
416
	 *
417
	 * @param array List of decorator names
418
	 * @since 2014.05
419
	 * @see client/html/common/decorators/default
420
	 * @see client/html/basket/standard/decorators/global
421
	 * @see client/html/basket/standard/decorators/local
422
	 */
423
424
	/** client/html/basket/standard/decorators/global
425
	 * Adds a list of globally available decorators only to the basket standard html client
426
	 *
427
	 * Decorators extend the functionality of a class by adding new aspects
428
	 * (e.g. log what is currently done), executing the methods of the underlying
429
	 * class only in certain conditions (e.g. only for logged in users) or
430
	 * modify what is returned to the caller.
431
	 *
432
	 * This option allows you to wrap global decorators
433
	 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
434
	 *
435
	 *  client/html/basket/standard/decorators/global = array( 'decorator1' )
436
	 *
437
	 * This would add the decorator named "decorator1" defined by
438
	 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
439
	 *
440
	 * @param array List of decorator names
441
	 * @since 2014.05
442
	 * @see client/html/common/decorators/default
443
	 * @see client/html/basket/standard/decorators/excludes
444
	 * @see client/html/basket/standard/decorators/local
445
	 */
446
447
	/** client/html/basket/standard/decorators/local
448
	 * Adds a list of local decorators only to the basket standard html client
449
	 *
450
	 * Decorators extend the functionality of a class by adding new aspects
451
	 * (e.g. log what is currently done), executing the methods of the underlying
452
	 * class only in certain conditions (e.g. only for logged in users) or
453
	 * modify what is returned to the caller.
454
	 *
455
	 * This option allows you to wrap local decorators
456
	 * ("\Aimeos\Client\Html\Basket\Decorator\*") around the html client.
457
	 *
458
	 *  client/html/basket/standard/decorators/local = array( 'decorator2' )
459
	 *
460
	 * This would add the decorator named "decorator2" defined by
461
	 * "\Aimeos\Client\Html\Basket\Decorator\Decorator2" only to the html client.
462
	 *
463
	 * @param array List of decorator names
464
	 * @since 2014.05
465
	 * @see client/html/common/decorators/default
466
	 * @see client/html/basket/standard/decorators/excludes
467
	 * @see client/html/basket/standard/decorators/global
468
	 */
469
}
470