Passed
Push — master ( 75274e...ab5b20 )
by Aimeos
03:49
created

Standard::navigator()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
c 0
b 0
f 0
nc 11
nop 0
dl 0
loc 53
rs 8.0555

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Catalog\Detail;
12
13
14
/**
15
 * Default implementation of catalog detail section HTML clients.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Catalog\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	private $tags = [];
25
	private $expire;
26
	private $view;
27
28
29
	/**
30
	 * Returns the HTML code for insertion into the body.
31
	 *
32
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
33
	 * @return string HTML code
34
	 */
35
	public function body( string $uid = '' ) : string
36
	{
37
		$view = $this->view();
38
		$config = $this->context()->config();
39
		$prefixes = ['d_prodid', 'd_name'];
40
41
		$code = $config->get( 'client/html/catalog/detail/prodcode-default' );
42
		$id = $config->get( 'client/html/catalog/detail/prodid-default', $code );
43
44
		if( !$view->param( 'd_name', $view->param( 'd_prodid', $id ) ) ) {
45
			return '';
46
		}
47
48
		/** client/html/catalog/detail/cache
49
		 * Enables or disables caching only for the catalog detail component
50
		 *
51
		 * Disable caching for components can be useful if you would have too much
52
		 * entries to cache or if the component contains non-cacheable parts that
53
		 * can't be replaced using the modify() method.
54
		 *
55
		 * @param boolean True to enable caching, false to disable
56
		 * @see client/html/catalog/filter/cache
57
		 * @see client/html/catalog/lists/cache
58
		 * @see client/html/catalog/stage/cache
59
		 */
60
61
		/** client/html/catalog/detail
62
		 * All parameters defined for the catalog detail component and its subparts
63
		 *
64
		 * This returns all settings related to the detail component.
65
		 * Please refer to the single settings for details.
66
		 *
67
		 * @param array Associative list of name/value settings
68
		 * @see client/html/catalog#detail
69
		 */
70
		$confkey = 'client/html/catalog/detail';
71
72
		if( $html = $this->cached( 'body', $uid, $prefixes, $confkey ) ) {
73
			return $this->modify( $html, $uid );
74
		}
75
76
		/** client/html/catalog/detail/template-body
77
		 * Relative path to the HTML body template of the catalog detail client.
78
		 *
79
		 * The template file contains the HTML code and processing instructions
80
		 * to generate the result shown in the body of the frontend. The
81
		 * configuration string is the path to the template file relative
82
		 * to the templates directory (usually in client/html/templates).
83
		 *
84
		 * You can overwrite the template file configuration in extensions and
85
		 * provide alternative templates. These alternative templates should be
86
		 * named like the default one but suffixed by
87
		 * an unique name. You may use the name of your project for this. If
88
		 * you've implemented an alternative client class as well, it
89
		 * should be suffixed by the name of the new class.
90
		 *
91
		 * @param string Relative path to the template creating code for the HTML page body
92
		 * @since 2014.03
93
		 * @see client/html/catalog/detail/template-header
94
		 * @see client/html/catalog/detail/404
95
		 */
96
		$template = $config->get( 'client/html/catalog/detail/template-body', 'catalog/detail/body' );
97
98
		$view = $this->view = $this->view ?? $this->object()->data( $view, $this->tags, $this->expire );
99
		$html = $this->modify( $view->render( $template ), $uid );
100
101
		return $this->cache( 'body', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
102
	}
103
104
105
	/**
106
	 * Returns the HTML string for insertion into the header.
107
	 *
108
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
109
	 * @return string|null String including HTML tags for the header on error
110
	 */
111
	public function header( string $uid = '' ) : ?string
112
	{
113
		$view = $this->view();
114
		$config = $this->context()->config();
115
		$prefixes = ['d_prodid', 'd_name'];
116
		$confkey = 'client/html/catalog/detail';
117
118
		$code = $config->get( 'client/html/catalog/detail/prodcode-default' );
119
		$id = $config->get( 'client/html/catalog/detail/prodid-default', $code );
120
121
		if( !$view->param( 'd_name', $view->param( 'd_prodid', $id ) ) ) {
122
			return '';
123
		}
124
125
		if( $html = $this->cached( 'header', $uid, $prefixes, $confkey ) ) {
126
			return $this->modify( $html, $uid );
127
		}
128
129
		/** client/html/catalog/detail/template-header
130
		 * Relative path to the HTML header template of the catalog detail client.
131
		 *
132
		 * The template file contains the HTML code and processing instructions
133
		 * to generate the HTML code that is inserted into the HTML page header
134
		 * of the rendered page in the frontend. The configuration string is the
135
		 * path to the template file relative to the templates directory (usually
136
		 * in client/html/templates).
137
		 *
138
		 * You can overwrite the template file configuration in extensions and
139
		 * provide alternative templates. These alternative templates should be
140
		 * named like the default one but suffixed by
141
		 * an unique name. You may use the name of your project for this. If
142
		 * you've implemented an alternative client class as well, it
143
		 * should be suffixed by the name of the new class.
144
		 *
145
		 * @param string Relative path to the template creating code for the HTML page head
146
		 * @since 2014.03
147
		 * @see client/html/catalog/detail/template-body
148
		 * @see client/html/catalog/detail/404
149
		 */
150
		$template = $config->get( 'client/html/catalog/detail/template-header', 'catalog/detail/header' );
151
152
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
153
		$html = $view->render( $template );
154
155
		return $this->cache( 'header', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
156
	}
157
158
159
	/**
160
	 * Modifies the cached content to replace content based on sessions or cookies.
161
	 *
162
	 * @param string $content Cached content
163
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
164
	 * @return string Modified content
165
	 */
166
	public function modify( string $content, string $uid ) : string
167
	{
168
		$content = $this->replaceSection( $content, $this->navigator(), 'catalog.detail.navigator' );
169
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.detail.csrf' );
170
	}
171
172
173
	/**
174
	 * Processes the input, e.g. store given values.
175
	 *
176
	 * A view must be available and this method doesn't generate any output
177
	 * besides setting view variables if necessary.
178
	 */
179
	public function init()
180
	{
181
		$context = $this->context();
182
		$session = $context->session();
183
184
		$site = $context->locale()->getSiteItem()->getCode();
185
		$params = $this->getClientParams( $this->view()->param() );
186
187
		$session->set( 'aimeos/catalog/detail/params/last/' . $site, $params );
188
	}
189
190
191
	/**
192
	 * Sets the necessary parameter values in the view.
193
	 *
194
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
195
	 * @param array &$tags Result array for the list of tags that are associated to the output
196
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
197
	 * @return \Aimeos\MW\View\Iface Modified view object
198
	 */
199
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
200
	{
201
		$context = $this->context();
202
		$config = $context->config();
203
		$domains = [
204
			'attribute', 'media', 'media/property', 'price', 'product',
205
			'product/property', 'supplier', 'supplier/address', 'text'
206
		];
207
208
		/** client/html/catalog/domains
209
		 * A list of domain names whose items should be available in the catalog view templates
210
		 *
211
		 * @see client/html/catalog/detail/domains
212
		 */
213
		$domains = $config->get( 'client/html/catalog/domains', $domains );
214
215
		/** client/html/catalog/detail/domains
216
		 * A list of domain names whose items should be available in the product detail view template
217
		 *
218
		 * The templates rendering product details usually add the images,
219
		 * prices, texts, attributes, products, etc. associated to the product
220
		 * item. If you want to display additional or less content, you can
221
		 * configure your own list of domains (attribute, media, price, product,
222
		 * text, etc. are domains) whose items are fetched from the storage.
223
		 * Please keep in mind that the more domains you add to the configuration,
224
		 * the more time is required for fetching the content!
225
		 *
226
		 * Since version 2014.05 this configuration option overwrites the
227
		 * "client/html/catalog/domains" option that allows to configure the
228
		 * domain names of the items fetched for all catalog related data.
229
		 *
230
		 * @param array List of domain names
231
		 * @since 2014.03
232
		 * @see client/html/catalog/domains
233
		 * @see client/html/catalog/lists/domains
234
		 */
235
		$domains = $config->get( 'client/html/catalog/detail/domains', $domains );
236
237
		/** client/html/catalog/detail/prodid-default
238
		 * The default product ID used if none is given as parameter
239
		 *
240
		 * To display a product detail view or a part of it for a specific
241
		 * product, you can configure its ID using this setting. This is
242
		 * most useful in a CMS where the product ID can be configured
243
		 * separately for each content node.
244
		 *
245
		 * @param string Product ID
246
		 * @since 2016.01
247
		 * @see client/html/catalog/detail/prodid-default
248
		 * @see client/html/catalog/lists/catid-default
249
		 */
250
		$id = $view->param( 'd_prodid', $config->get( 'client/html/catalog/detail/prodid-default' ) );
251
252
		/** client/html/catalog/detail/prodcode-default
253
		 * The default product code used if none is given as parameter
254
		 *
255
		 * To display a product detail view or a part of it for a specific
256
		 * product, you can configure its code using this setting. This is
257
		 * most useful in a CMS where the product code can be configured
258
		 * separately for each content node.
259
		 *
260
		 * @param string Product code
261
		 * @since 2019.10
262
		 * @see client/html/catalog/detail/prodid-default
263
		 * @see client/html/catalog/lists/catid-default
264
		 */
265
		$code = $config->get( 'client/html/catalog/detail/prodcode-default' );
266
267
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'product' )->uses( $domains );
268
		$productItem = ( $id ? $cntl->get( $id ) : ( $code ? $cntl->find( $code ) : $cntl->resolve( $view->param( 'd_name' ) ) ) );
269
270
		$propItems = $productItem->getPropertyItems();
271
		$supItems = $productItem->getRefItems( 'supplier', null, 'default' );
272
		$attrItems = $productItem->getRefItems( 'attribute', null, 'default' );
273
		$mediaItems = $productItem->getRefItems( 'media', 'default', 'default' );
274
275
		$this->addMetaItems( $productItem, $expire, $tags );
276
		$this->addMetaItems( $supItems, $expire, $tags );
277
278
		if( in_array( $productItem->getType(), ['bundle', 'select'] ) )
279
		{
280
			\Aimeos\Map::method( 'attrparent', function( $subProdId ) {
281
				foreach( $this->list as $item ) {
0 ignored issues
show
Bug Best Practice introduced by
The property list does not exist on Aimeos\Client\Html\Catalog\Detail\Standard. Did you maybe forget to declare it?
Loading history...
282
					$item->parent = array_merge( $item->parent ?? [], [$subProdId] );
283
				}
284
				return $this;
285
			} );
286
287
			foreach( $productItem->getRefItems( 'product', null, 'default' ) as $subProdId => $subProduct )
288
			{
289
				$propItems->merge( $subProduct->getPropertyItems()->assign( ['parent' => $subProdId] ) );
290
				$mediaItems->merge( $subProduct->getRefItems( 'media', 'default', 'default' ) );
291
				$attrItems->replace(
292
					$subProduct->getRefItems( 'attribute', null, ['default', 'variant'] )->attrparent( $subProdId )
293
				);
294
			}
295
		}
296
297
298
		/** client/html/catalog/detail/stock/enable
299
		 * Enables or disables displaying product stock levels in product detail view
300
		 *
301
		 * This configuration option allows shop owners to display product
302
		 * stock levels for each product in the detail views or to disable
303
		 * fetching product stock information.
304
		 *
305
		 * The stock information is fetched via AJAX and inserted via Javascript.
306
		 * This allows to cache product items by leaving out such highly
307
		 * dynamic content like stock levels which changes with each order.
308
		 *
309
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
310
		 * @since 2014.03
311
		 * @see client/html/catalog/lists/stock/enable
312
		 * @see client/html/catalog/stock/url/target
313
		 * @see client/html/catalog/stock/url/controller
314
		 * @see client/html/catalog/stock/url/action
315
		 * @see client/html/catalog/stock/url/config
316
		 */
317
318
		if( (bool) $view->config( 'client/html/catalog/detail/stock/enable', true ) === true )
319
		{
320
			$products = $productItem->getRefItems( 'product', null, 'default' )->push( $productItem );
321
			$view->detailStockUrl = $this->getStockUrl( $view, $products );
322
		}
323
324
325
		$view->detailMediaItems = $mediaItems;
326
		$view->detailProductItem = $productItem;
327
		$view->detailAttributeMap = $attrItems->groupBy( 'attribute.type' );
328
		$view->detailPropertyMap = $propItems->groupBy( 'product.property.type' );
329
		$view->serviceItems = $this->call( 'services' );
330
331
		$this->call( 'seen', $productItem );
332
333
		return parent::data( $view, $tags, $expire );
334
	}
335
336
337
	/**
338
	 * Sets the necessary parameter values in the view.
339
	 *
340
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
341
	 * @param array &$tags Result array for the list of tags that are associated to the output
342
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
343
	 * @return \Aimeos\MW\View\Iface Modified view object
344
	 */
345
	public function navigator() : string
346
	{
347
		$view = $this->view();
348
		$context = $this->context();
349
350
		if( is_numeric( $pos = $view->param( 'd_pos' ) ) )
351
		{
352
			if( $pos < 1 ) {
353
				$pos = $start = 0; $size = 2;
354
			} else {
355
				$start = $pos - 1; $size = 3;
356
			}
357
358
			$site = $context->locale()->getSiteItem()->getCode();
359
			$params = $context->session()->get( 'aimeos/catalog/lists/params/last/' . $site, [] );
360
			$level = $view->config( 'client/html/catalog/lists/levels', \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
361
362
			$catids = $view->value( $params, 'f_catid', $view->config( 'client/html/catalog/lists/catid-default' ) );
363
			$sort = $view->value( $params, 'f_sort', $view->config( 'client/html/catalog/lists/sort', 'relevance' ) );
364
365
			$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
366
				->sort( $sort ) // prioritize user sorting over the sorting through relevance and category
367
				->allOf( $view->value( $params, 'f_attrid', [] ) )
368
				->oneOf( $view->value( $params, 'f_optid', [] ) )
369
				->oneOf( $view->value( $params, 'f_oneid', [] ) )
370
				->text( $view->value( $params, 'f_search' ) )
371
				->category( $catids, 'default', $level )
372
				->slice( $start, $size )
373
				->uses( ['text'] )
374
				->search();
375
376
			if( ( $count = count( $products ) ) > 1 )
377
			{
378
				if( $pos > 0 && ( $product = $products->first() ) !== null )
379
				{
380
					$param = ['d_pos' => $pos - 1, 'd_name' => $product->getName( 'url ' )];
381
					$view->navigationPrev = $view->link( 'client/html/catalog/detail/url', $param );
382
				}
383
384
				if( ( $pos === 0 || $count === 3 ) && ( $product = $products->last() ) !== null )
385
				{
386
					$param = ['d_pos' => $pos + 1, 'd_name' => $product->getName( 'url ' )];
387
					$view->navigationNext = $view->link( 'client/html/catalog/detail/url', $param );
388
				}
389
			}
390
391
			$config = $context->config();
392
			$template = $config->get( 'client/html/catalog/detail/template-navigator', 'catalog/detail/navigator' );
393
394
			return $view->render( $template );
395
		}
396
397
		return '';
398
	}
399
400
401
	/**
402
	 * Adds the product to the list of last seen products.
403
	 *
404
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item
405
	 */
406
	protected function seen( \Aimeos\MShop\Product\Item\Iface $product )
407
	{
408
		$id = $product->getId();
409
		$context = $this->context();
410
		$session = $context->session();
411
		$lastSeen = map( $session->get( 'aimeos/catalog/session/seen/list', [] ) );
412
413
		if( !$lastSeen->has( $id ) )
414
		{
415
			$config = $context->config();
416
417
			/** client/html/catalog/detail/seen-partial
418
			 * Relative path to the HTML body template of the catalog detail seen client.
419
			 *
420
			 * The template file contains the HTML code and processing instructions
421
			 * to generate the result shown in the body of the frontend. The
422
			 * configuration string is the path to the template file relative
423
			 * to the templates directory (usually in client/html/templates).
424
			 *
425
			 * You can overwrite the template file configuration in extensions and
426
			 * provide alternative templates. These alternative templates should be
427
			 * named like the default one but suffixed by
428
			 * an unique name. You may use the name of your project for this. If
429
			 * you've implemented an alternative client class as well, it
430
			 * should be suffixed by the name of the new class.
431
			 *
432
			 * @param string Relative path to the template creating the HTML fragment
433
			 * @since 2014.03
434
			 */
435
			$template = $config->get( 'client/html/catalog/detail/seen-partial', 'catalog/detail/seen-partial' );
436
437
			/** client/html/catalog/session/seen/maxitems
438
			 * Maximum number of products displayed in the "last seen" section
439
			 *
440
			 * This option limits the number of products that are shown in the
441
			 * "last seen" section after the user visited their detail pages. It
442
			 * must be a positive integer value greater than 0.
443
			 *
444
			 * @param integer Number of products
445
			 * @since 2014.03
446
			 */
447
			$max = $config->get( 'client/html/catalog/session/seen/maxitems', 6 );
448
449
			$html = $this->view()->set( 'product', $product )->render( $template );
450
			$lastSeen->put( $id, $html )->slice( -$max );
451
		}
452
453
		$session->set( 'aimeos/catalog/session/seen/list', $lastSeen->put( $id, $lastSeen->pull( $id ) )->all() );
454
	}
455
456
457
	/**
458
	 * Returns the available service options
459
	 *
460
	 * @return \Aimeos\Map List of service items
461
	 */
462
	protected function services() : \Aimeos\Map
463
	{
464
		$context = $this->context();
465
		$config = $context->config();
466
467
		/** client/html/catalog/detail/service/types
468
		 * The service types available in the service template
469
		 *
470
		 * By default, only delivery services will be available in the
471
		 * template but you can extend the list to payment services too.
472
		 *
473
		 * @param array List of type codes
474
		 * @since 2016.05
475
		 * @see client/html/catalog/detail/service/domains
476
		 */
477
		$types = $config->get( 'client/html/catalog/detail/service/types', ['delivery'] );
478
479
		/** client/html/catalog/detail/service/domains
480
		 * A list of domain names whose items should be available for the services
481
		 * in the services part of the catalog detail view templates
482
		 *
483
		 * Usually, service prices and texts are available in the templates
484
		 * rendering services related data. If you want to
485
		 * display additional content like the attributes, you can configure
486
		 * your own list of domains (attribute, media, price, text,
487
		 * etc. are domains) whose items are fetched from the storage.
488
		 *
489
		 * @param array List of domain names
490
		 * @since 2016.05
491
		 * @see client/html/catalog/detail/service/types
492
		 */
493
		$domains = $config->get( 'client/html/catalog/detail/service/domains', ['text', 'price'] );
494
495
		$items = \Aimeos\Controller\Frontend::create( $context, 'service' )
496
			->uses( $domains )->type( $types )->sort( 'type' )->search();
497
498
		return $this->addMetaItems( $items, $this->expire, $this->tags );
499
	}
500
}
501