Passed
Push — master ( 9e12c4...8666ac )
by Aimeos
02:37
created

Standard   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 470
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 127
c 3
b 0
f 0
dl 0
loc 470
rs 10
wmc 29

10 Methods

Rating   Name   Duplication   Size   Complexity  
A header() 0 45 3
A body() 0 67 3
A init() 0 9 1
A modify() 0 4 1
A seen() 0 48 2
A data() 0 40 4
A domains() 0 40 1
B navigator() 0 53 9
A product() 0 37 3
A stockUrl() 0 28 2
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\Base\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\Base\View\Iface Modified view object
198
	 */
199
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
200
	{
201
		$productItem = $this->product( $view );
202
203
		$propItems = $productItem->getPropertyItems();
204
		$supItems = $productItem->getRefItems( 'supplier', null, 'default' );
205
		$attrItems = $productItem->getRefItems( 'attribute', null, 'default' );
206
		$mediaItems = $productItem->getRefItems( 'media', 'default', 'default' );
207
208
		$this->addMetaItems( $productItem, $expire, $tags );
209
		$this->addMetaItems( $supItems, $expire, $tags );
0 ignored issues
show
Bug introduced by
$supItems of type Aimeos\Map is incompatible with the type Aimeos\MShop\Common\Item\Iface|array expected by parameter $items of Aimeos\Client\Html\Base::addMetaItems(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
		$this->addMetaItems( /** @scrutinizer ignore-type */ $supItems, $expire, $tags );
Loading history...
210
211
		if( in_array( $productItem->getType(), ['bundle', 'select'] ) )
212
		{
213
			\Aimeos\Map::method( 'attrparent', function( $subProdId ) {
214
				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...
215
					$item->parent = array_merge( $item->parent ?? [], [$subProdId] );
216
				}
217
				return $this;
218
			} );
219
220
			foreach( $productItem->getRefItems( 'product', null, 'default' ) as $subProdId => $subProduct )
221
			{
222
				$propItems->merge( $subProduct->getPropertyItems()->assign( ['parent' => $subProdId] ) );
223
				$mediaItems->merge( $subProduct->getRefItems( 'media', 'default', 'default' ) );
224
				$attrItems->replace(
225
					$subProduct->getRefItems( 'attribute', null, ['default', 'variant'] )->attrparent( $subProdId )
226
				);
227
			}
228
		}
229
230
		$view->detailMediaItems = $mediaItems;
231
		$view->detailProductItem = $productItem;
232
		$view->detailAttributeMap = $attrItems->groupBy( 'attribute.type' )->ksort();
233
		$view->detailPropertyMap = $propItems->groupBy( 'product.property.type' )->ksort();
234
		$view->detailStockUrl = $this->stockUrl( $productItem );
235
236
		$this->call( 'seen', $productItem );
237
238
		return parent::data( $view, $tags, $expire );
239
	}
240
241
242
	/**
243
	 * Returns the data domains fetched along with the products
244
	 *
245
	 * @return array List of domain names
246
	 */
247
	protected function domains() : array
248
	{
249
		$context = $this->context();
250
		$config = $context->config();
251
252
		$domains = [
253
			'attribute', 'attribute/property', 'catalog', 'media', 'media/property', 'price',
254
			'product', 'product/property', 'supplier', 'supplier/address', 'text'
255
		];
256
257
		/** client/html/catalog/domains
258
		 * A list of domain names whose items should be available in the catalog view templates
259
		 *
260
		 * @see client/html/catalog/detail/domains
261
		 */
262
		$domains = $config->get( 'client/html/catalog/domains', $domains );
263
264
		/** client/html/catalog/detail/domains
265
		 * A list of domain names whose items should be available in the product detail view template
266
		 *
267
		 * The templates rendering product details usually add the images,
268
		 * prices, texts, attributes, products, etc. associated to the product
269
		 * item. If you want to display additional or less content, you can
270
		 * configure your own list of domains (attribute, media, price, product,
271
		 * text, etc. are domains) whose items are fetched from the storage.
272
		 * Please keep in mind that the more domains you add to the configuration,
273
		 * the more time is required for fetching the content!
274
		 *
275
		 * Since version 2014.05 this configuration option overwrites the
276
		 * "client/html/catalog/domains" option that allows to configure the
277
		 * domain names of the items fetched for all catalog related data.
278
		 *
279
		 * @param array List of domain names
280
		 * @since 2014.03
281
		 * @see client/html/catalog/domains
282
		 * @see client/html/catalog/lists/domains
283
		 */
284
		$domains = $config->get( 'client/html/catalog/detail/domains', $domains );
285
286
		return $domains;
287
	}
288
289
290
	/**
291
	 * Sets the necessary parameter values in the view.
292
	 *
293
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
294
	 * @param array &$tags Result array for the list of tags that are associated to the output
295
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
296
	 * @return \Aimeos\Base\View\Iface Modified view object
297
	 */
298
	public function navigator() : string
299
	{
300
		$view = $this->view();
301
		$context = $this->context();
302
303
		if( is_numeric( $pos = $view->param( 'd_pos' ) ) )
304
		{
305
			if( $pos < 1 ) {
306
				$pos = $start = 0; $size = 2;
307
			} else {
308
				$start = $pos - 1; $size = 3;
309
			}
310
311
			$site = $context->locale()->getSiteItem()->getCode();
312
			$params = $context->session()->get( 'aimeos/catalog/lists/params/last/' . $site, [] );
313
			$level = $view->config( 'client/html/catalog/lists/levels', \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
314
315
			$catids = $view->value( $params, 'f_catid', $view->config( 'client/html/catalog/lists/catid-default' ) );
316
			$sort = $view->value( $params, 'f_sort', $view->config( 'client/html/catalog/lists/sort', 'relevance' ) );
317
318
			$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
319
				->sort( $sort ) // prioritize user sorting over the sorting through relevance and category
320
				->allOf( $view->value( $params, 'f_attrid', [] ) )
321
				->oneOf( $view->value( $params, 'f_optid', [] ) )
322
				->oneOf( $view->value( $params, 'f_oneid', [] ) )
323
				->text( $view->value( $params, 'f_search' ) )
324
				->category( $catids, 'default', $level )
325
				->slice( $start, $size )
326
				->uses( ['text'] )
327
				->search();
328
329
			if( ( $count = count( $products ) ) > 1 )
330
			{
331
				if( $pos > 0 && ( $product = $products->first() ) !== null )
332
				{
333
					$param = ['d_name' => $product->getName( 'url ' ), 'd_prodid' => $product->getId(), 'd_pos' => $pos - 1];
334
					$view->navigationPrev = $view->link( 'client/html/catalog/detail/url', $param );
335
				}
336
337
				if( ( $pos === 0 || $count === 3 ) && ( $product = $products->last() ) !== null )
338
				{
339
					$param = ['d_name' => $product->getName( 'url ' ), 'd_prodid' => $product->getId(), 'd_pos' => $pos + 1];
340
					$view->navigationNext = $view->link( 'client/html/catalog/detail/url', $param );
341
				}
342
			}
343
344
			$config = $context->config();
345
			$template = $config->get( 'client/html/catalog/detail/template-navigator', 'catalog/detail/navigator' );
346
347
			return $view->render( $template );
348
		}
349
350
		return '';
351
	}
352
353
354
	/**
355
	 * Returns the product item
356
	 *
357
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
358
	 * @return \Aimeos\MShop\Product\Item\Iface Product item
359
	 */
360
	protected function product( \Aimeos\Base\View\Iface $view ) : \Aimeos\MShop\Product\Item\Iface
361
	{
362
		$context = $this->context();
363
		$config = $context->config();
364
365
		/** client/html/catalog/detail/prodid-default
366
		 * The default product ID used if none is given as parameter
367
		 *
368
		 * To display a product detail view or a part of it for a specific
369
		 * product, you can configure its ID using this setting. This is
370
		 * most useful in a CMS where the product ID can be configured
371
		 * separately for each content node.
372
		 *
373
		 * @param string Product ID
374
		 * @since 2016.01
375
		 * @see client/html/catalog/detail/prodid-default
376
		 * @see client/html/catalog/lists/catid-default
377
		 */
378
		$id = $view->param( 'd_prodid', $config->get( 'client/html/catalog/detail/prodid-default' ) );
379
380
		/** client/html/catalog/detail/prodcode-default
381
		 * The default product code used if none is given as parameter
382
		 *
383
		 * To display a product detail view or a part of it for a specific
384
		 * product, you can configure its code using this setting. This is
385
		 * most useful in a CMS where the product code can be configured
386
		 * separately for each content node.
387
		 *
388
		 * @param string Product code
389
		 * @since 2019.10
390
		 * @see client/html/catalog/detail/prodid-default
391
		 * @see client/html/catalog/lists/catid-default
392
		 */
393
		$code = $config->get( 'client/html/catalog/detail/prodcode-default' );
394
395
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'product' )->uses( $this->domains() );
396
		return ( $id ? $cntl->get( $id ) : ( $code ? $cntl->find( $code ) : $cntl->resolve( $view->param( 'd_name' ) ) ) );
397
	}
398
399
400
	/**
401
	 * Adds the product to the list of last seen products.
402
	 *
403
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item
404
	 */
405
	protected function seen( \Aimeos\MShop\Product\Item\Iface $product )
406
	{
407
		$id = $product->getId();
408
		$context = $this->context();
409
		$session = $context->session();
410
		$lastSeen = map( $session->get( 'aimeos/catalog/session/seen/list', [] ) );
411
412
		if( !$lastSeen->has( $id ) )
413
		{
414
			$config = $context->config();
415
416
			/** client/html/catalog/detail/partials/seen
417
			 * Relative path to the HTML body template of the catalog detail seen client.
418
			 *
419
			 * The template file contains the HTML code and processing instructions
420
			 * to generate the result shown in the body of the frontend. The
421
			 * configuration string is the path to the template file relative
422
			 * to the templates directory (usually in client/html/templates).
423
			 *
424
			 * You can overwrite the template file configuration in extensions and
425
			 * provide alternative templates. These alternative templates should be
426
			 * named like the default one but suffixed by
427
			 * an unique name. You may use the name of your project for this. If
428
			 * you've implemented an alternative client class as well, it
429
			 * should be suffixed by the name of the new class.
430
			 *
431
			 * @param string Relative path to the template creating the HTML fragment
432
			 * @since 2014.03
433
			 */
434
			$template = $config->get( 'client/html/catalog/detail/partials/seen', 'catalog/detail/seen' );
435
436
			/** client/html/catalog/session/seen/maxitems
437
			 * Maximum number of products displayed in the "last seen" section
438
			 *
439
			 * This option limits the number of products that are shown in the
440
			 * "last seen" section after the user visited their detail pages. It
441
			 * must be a positive integer value greater than 0.
442
			 *
443
			 * @param integer Number of products
444
			 * @since 2014.03
445
			 */
446
			$max = $config->get( 'client/html/catalog/session/seen/maxitems', 6 );
447
448
			$html = $this->view()->set( 'product', $product )->render( $template );
449
			$lastSeen->put( $id, $html )->slice( -$max );
450
		}
451
452
		$session->set( 'aimeos/catalog/session/seen/list', $lastSeen->put( $id, $lastSeen->pull( $id ) )->all() );
453
	}
454
455
456
	/**
457
	 * Returns the URL for fetching stock levels
458
	 *
459
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item
460
	 * @return \Aimeos\Map List of stock URLs
461
	 */
462
	protected function stockUrl( \Aimeos\MShop\Product\Item\Iface $productItem ) : \Aimeos\Map
463
	{
464
		/** client/html/catalog/detail/stock/enable
465
		 * Enables or disables displaying product stock levels in product detail view
466
		 *
467
		 * This configuration option allows shop owners to display product
468
		 * stock levels for each product in the detail views or to disable
469
		 * fetching product stock information.
470
		 *
471
		 * The stock information is fetched via AJAX and inserted via Javascript.
472
		 * This allows to cache product items by leaving out such highly
473
		 * dynamic content like stock levels which changes with each order.
474
		 *
475
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
476
		 * @since 2014.03
477
		 * @see client/html/catalog/lists/stock/enable
478
		 * @see client/html/catalog/stock/url/target
479
		 * @see client/html/catalog/stock/url/controller
480
		 * @see client/html/catalog/stock/url/action
481
		 * @see client/html/catalog/stock/url/config
482
		 */
483
		if( !$this->context()->config()->get( 'client/html/catalog/detail/stock/enable', true ) ) {
484
			return null;
485
		}
486
487
		$products = $productItem->getRefItems( 'product', null, 'default' )->push( $productItem );
488
489
		return $this->getStockUrl( $this->view(), $products );
490
	}
491
}
492