Passed
Push — master ( dc5862...f6527a )
by Aimeos
04:15 queued 13s
created

Standard::seen()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 50
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 50
rs 9.9
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
		 * @category Developer
57
		 * @category User
58
		 * @see client/html/catalog/filter/cache
59
		 * @see client/html/catalog/lists/cache
60
		 * @see client/html/catalog/stage/cache
61
		 */
62
63
		/** client/html/catalog/detail
64
		 * All parameters defined for the catalog detail component and its subparts
65
		 *
66
		 * This returns all settings related to the detail component.
67
		 * Please refer to the single settings for details.
68
		 *
69
		 * @param array Associative list of name/value settings
70
		 * @category Developer
71
		 * @see client/html/catalog#detail
72
		 */
73
		$confkey = 'client/html/catalog/detail';
74
75
		if( $html = $this->cached( 'body', $uid, $prefixes, $confkey ) ) {
76
			return $this->modify( $html, $uid );
77
		}
78
79
		$template = $config->get( 'client/html/catalog/detail/template-body', 'catalog/detail/body' );
80
		$view = $this->view = $this->view ?? $this->object()->data( $view, $this->tags, $this->expire );
81
		$html = $view->render( $template );
82
83
		return $this->cache( 'body', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
84
	}
85
86
87
	/**
88
	 * Returns the HTML string for insertion into the header.
89
	 *
90
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
91
	 * @return string|null String including HTML tags for the header on error
92
	 */
93
	public function header( string $uid = '' ) : ?string
94
	{
95
		$view = $this->view();
96
		$config = $this->context()->config();
97
		$prefixes = ['d_prodid', 'd_name'];
98
		$confkey = 'client/html/catalog/detail';
99
100
		$code = $config->get( 'client/html/catalog/detail/prodcode-default' );
101
		$id = $config->get( 'client/html/catalog/detail/prodid-default', $code );
102
103
		if( !$view->param( 'd_name', $view->param( 'd_prodid', $id ) ) ) {
104
			return '';
105
		}
106
107
		if( $html = $this->cached( 'header', $uid, $prefixes, $confkey ) ) {
108
			return $this->modify( $html, $uid );
109
		}
110
111
		$template = $config->get( 'client/html/catalog/detail/template-header', 'catalog/detail/header' );
112
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
113
		$html = $view->render( $template );
114
115
		return $this->cache( 'header', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
116
	}
117
118
119
	/**
120
	 * Modifies the cached content to replace content based on sessions or cookies.
121
	 *
122
	 * @param string $content Cached content
123
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
124
	 * @return string Modified content
125
	 */
126
	public function modify( string $content, string $uid ) : string
127
	{
128
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.detail.csrf' );
129
	}
130
131
132
	/**
133
	 * Processes the input, e.g. store given values.
134
	 *
135
	 * A view must be available and this method doesn't generate any output
136
	 * besides setting view variables if necessary.
137
	 */
138
	public function init()
139
	{
140
		$context = $this->context();
141
		$session = $context->session();
142
143
		$site = $context->locale()->getSiteItem()->getCode();
144
		$params = $this->getClientParams( $this->view()->param() );
145
146
		$session->set( 'aimeos/catalog/detail/params/last/' . $site, $params );
147
	}
148
149
150
	/**
151
	 * Sets the necessary parameter values in the view.
152
	 *
153
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
154
	 * @param array &$tags Result array for the list of tags that are associated to the output
155
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
156
	 * @return \Aimeos\MW\View\Iface Modified view object
157
	 */
158
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
159
	{
160
		$context = $this->context();
161
		$config = $context->config();
162
		$session = $context->session();
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
163
		$domains = [
164
			'attribute', 'media', 'media/property', 'price', 'product',
165
			'product/property', 'supplier', 'supplier/address', 'text'
166
		];
167
168
		/** client/html/catalog/domains
169
		 * A list of domain names whose items should be available in the catalog view templates
170
		 *
171
		 * @see client/html/catalog/detail/domains
172
		 */
173
		$domains = $config->get( 'client/html/catalog/domains', $domains );
174
175
		/** client/html/catalog/detail/domains
176
		 * A list of domain names whose items should be available in the product detail view template
177
		 *
178
		 * The templates rendering product details usually add the images,
179
		 * prices, texts, attributes, products, etc. associated to the product
180
		 * item. If you want to display additional or less content, you can
181
		 * configure your own list of domains (attribute, media, price, product,
182
		 * text, etc. are domains) whose items are fetched from the storage.
183
		 * Please keep in mind that the more domains you add to the configuration,
184
		 * the more time is required for fetching the content!
185
		 *
186
		 * Since version 2014.05 this configuration option overwrites the
187
		 * "client/html/catalog/domains" option that allows to configure the
188
		 * domain names of the items fetched for all catalog related data.
189
		 *
190
		 * @param array List of domain names
191
		 * @since 2014.03
192
		 * @category Developer
193
		 * @see client/html/catalog/domains
194
		 * @see client/html/catalog/lists/domains
195
		 */
196
		$domains = $config->get( 'client/html/catalog/detail/domains', $domains );
197
198
		/** client/html/catalog/detail/prodid-default
199
		 * The default product ID used if none is given as parameter
200
		 *
201
		 * To display a product detail view or a part of it for a specific
202
		 * product, you can configure its ID using this setting. This is
203
		 * most useful in a CMS where the product ID can be configured
204
		 * separately for each content node.
205
		 *
206
		 * @param string Product ID
207
		 * @since 2016.01
208
		 * @category User
209
		 * @category Developer
210
		 * @see client/html/catalog/detail/prodid-default
211
		 * @see client/html/catalog/lists/catid-default
212
		 */
213
		$id = $view->param( 'd_prodid', $config->get( 'client/html/catalog/detail/prodid-default' ) );
214
215
		/** client/html/catalog/detail/prodcode-default
216
		 * The default product code used if none is given as parameter
217
		 *
218
		 * To display a product detail view or a part of it for a specific
219
		 * product, you can configure its code using this setting. This is
220
		 * most useful in a CMS where the product code can be configured
221
		 * separately for each content node.
222
		 *
223
		 * @param string Product code
224
		 * @since 2019.10
225
		 * @category User
226
		 * @category Developer
227
		 * @see client/html/catalog/detail/prodid-default
228
		 * @see client/html/catalog/lists/catid-default
229
		 */
230
		$code = $config->get( 'client/html/catalog/detail/prodcode-default' );
231
232
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'product' )->uses( $domains );
233
		$productItem = ( $id ? $cntl->get( $id ) : ( $code ? $cntl->find( $code ) : $cntl->resolve( $view->param( 'd_name' ) ) ) );
234
235
		$propItems = $productItem->getPropertyItems();
236
		$supItems = $productItem->getRefItems( 'supplier', null, 'default' );
237
		$attrItems = $productItem->getRefItems( 'attribute', null, 'default' );
238
		$mediaItems = $productItem->getRefItems( 'media', 'default', 'default' );
239
240
		$this->addMetaItems( $productItem, $expire, $tags );
241
		$this->addMetaItems( $supItems, $expire, $tags );
242
243
		if( in_array( $productItem->getType(), ['bundle', 'select'] ) )
244
		{
245
			\Aimeos\Map::method( 'attrparent', function( $subProdId ) {
246
				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...
247
					$item->parent = array_merge( $item->parent ?? [], [$subProdId] );
248
				}
249
				return $this;
250
			} );
251
252
			foreach( $productItem->getRefItems( 'product', null, 'default' ) as $subProdId => $subProduct )
253
			{
254
				$propItems->merge( $subProduct->getPropertyItems()->assign( ['parent' => $subProdId] ) );
255
				$mediaItems->merge( $subProduct->getRefItems( 'media', 'default', 'default' ) );
256
				$attrItems->replace(
257
					$subProduct->getRefItems( 'attribute', null, ['default', 'variant'] )->attrparent( $subProdId )
258
				);
259
			}
260
		}
261
262
263
		/** client/html/catalog/detail/stock/enable
264
		 * Enables or disables displaying product stock levels in product detail view
265
		 *
266
		 * This configuration option allows shop owners to display product
267
		 * stock levels for each product in the detail views or to disable
268
		 * fetching product stock information.
269
		 *
270
		 * The stock information is fetched via AJAX and inserted via Javascript.
271
		 * This allows to cache product items by leaving out such highly
272
		 * dynamic content like stock levels which changes with each order.
273
		 *
274
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
275
		 * @since 2014.03
276
		 * @category User
277
		 * @category Developer
278
		 * @see client/html/catalog/lists/stock/enable
279
		 * @see client/html/catalog/stock/url/target
280
		 * @see client/html/catalog/stock/url/controller
281
		 * @see client/html/catalog/stock/url/action
282
		 * @see client/html/catalog/stock/url/config
283
		 */
284
285
		if( (bool) $view->config( 'client/html/catalog/detail/stock/enable', true ) === true )
286
		{
287
			$products = $productItem->getRefItems( 'product', null, 'default' )->push( $productItem );
288
			$view->detailStockUrl = $this->getStockUrl( $view, $products );
289
		}
290
291
292
		$view->detailMediaItems = $mediaItems;
293
		$view->detailProductItem = $productItem;
294
		$view->detailAttributeMap = $attrItems->groupBy( 'attribute.type' );
295
		$view->detailPropertyMap = $propItems->groupBy( 'product.property.type' );
296
		$view->serviceItems = $this->call( 'services' );
297
298
		$this->call( 'seen', $productItem );
299
300
		return parent::data( $view, $tags, $expire );
301
	}
302
303
304
	/**
305
	 * Adds the product to the list of last seen products.
306
	 *
307
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item
308
	 */
309
	protected function seen( \Aimeos\MShop\Product\Item\Iface $product )
310
	{
311
		$id = $product->getId();
312
		$context = $this->context();
313
		$session = $context->session();
314
		$lastSeen = map( $session->get( 'aimeos/catalog/session/seen/list', [] ) );
315
316
		if( !$lastSeen->has( $id ) )
317
		{
318
			$config = $context->config();
319
320
			/** client/html/catalog/detail/seen-partial
321
			 * Relative path to the HTML body template of the catalog detail seen client.
322
			 *
323
			 * The template file contains the HTML code and processing instructions
324
			 * to generate the result shown in the body of the frontend. The
325
			 * configuration string is the path to the template file relative
326
			 * to the templates directory (usually in client/html/templates).
327
			 *
328
			 * You can overwrite the template file configuration in extensions and
329
			 * provide alternative templates. These alternative templates should be
330
			 * named like the default one but suffixed by
331
			 * an unique name. You may use the name of your project for this. If
332
			 * you've implemented an alternative client class as well, it
333
			 * should be suffixed by the name of the new class.
334
			 *
335
			 * @param string Relative path to the template creating the HTML fragment
336
			 * @since 2014.03
337
			 */
338
			$template = $config->get( 'client/html/catalog/detail/seen-partial', 'catalog/detail/seen-partial' );
339
340
			/** client/html/catalog/session/seen/maxitems
341
			 * Maximum number of products displayed in the "last seen" section
342
			 *
343
			 * This option limits the number of products that are shown in the
344
			 * "last seen" section after the user visited their detail pages. It
345
			 * must be a positive integer value greater than 0.
346
			 *
347
			 * @param integer Number of products
348
			 * @since 2014.03
349
			 * @category User
350
			 * @category Developer
351
			 */
352
			$max = $config->get( 'client/html/catalog/session/seen/maxitems', 6 );
353
354
			$html = $this->view()->set( 'product', $product )->render( $template );
355
			$lastSeen->put( $id, $html )->slice( -$max );
356
		}
357
358
		$session->set( 'aimeos/catalog/session/seen/list', $lastSeen->put( $id, $lastSeen->pull( $id ) )->all() );
359
	}
360
361
362
	/**
363
	 * Returns the available service options
364
	 *
365
	 * @return \Aimeos\Map List of service items
366
	 */
367
	protected function services() : \Aimeos\Map
368
	{
369
		$context = $this->context();
370
		$config = $context->config();
371
372
		/** client/html/catalog/detail/service/types
373
		 * The service types available in the service template
374
		 *
375
		 * By default, only delivery services will be available in the
376
		 * template but you can extend the list to payment services too.
377
		 *
378
		 * @param array List of type codes
379
		 * @since 2016.05
380
		 * @category Developer
381
		 * @see client/html/catalog/detail/service/domains
382
		 */
383
		$types = $config->get( 'client/html/catalog/detail/service/types', ['delivery'] );
384
385
		/** client/html/catalog/detail/service/domains
386
		 * A list of domain names whose items should be available for the services
387
		 * in the services part of the catalog detail view templates
388
		 *
389
		 * Usually, service prices and texts are available in the templates
390
		 * rendering services related data. If you want to
391
		 * display additional content like the attributes, you can configure
392
		 * your own list of domains (attribute, media, price, text,
393
		 * etc. are domains) whose items are fetched from the storage.
394
		 *
395
		 * @param array List of domain names
396
		 * @since 2016.05
397
		 * @category Developer
398
		 * @see client/html/catalog/detail/service/types
399
		 */
400
		$domains = $config->get( 'client/html/catalog/detail/service/domains', ['text', 'price'] );
401
402
		$items = \Aimeos\Controller\Frontend::create( $context, 'service' )
403
			->uses( $domains )->type( $types )->sort( 'type' )->search();
404
405
		return $this->addMetaItems( $items, $this->expire, $this->tags );
406
	}
407
408
409
	/** client/html/catalog/detail/template-body
410
	 * Relative path to the HTML body template of the catalog detail client.
411
	 *
412
	 * The template file contains the HTML code and processing instructions
413
	 * to generate the result shown in the body of the frontend. The
414
	 * configuration string is the path to the template file relative
415
	 * to the templates directory (usually in client/html/templates).
416
	 *
417
	 * You can overwrite the template file configuration in extensions and
418
	 * provide alternative templates. These alternative templates should be
419
	 * named like the default one but suffixed by
420
	 * an unique name. You may use the name of your project for this. If
421
	 * you've implemented an alternative client class as well, it
422
	 * should be suffixed by the name of the new class.
423
	 *
424
	 * @param string Relative path to the template creating code for the HTML page body
425
	 * @since 2014.03
426
	 * @category Developer
427
	 * @see client/html/catalog/detail/template-header
428
	 * @see client/html/catalog/detail/404
429
	 */
430
431
	/** client/html/catalog/detail/template-header
432
	 * Relative path to the HTML header template of the catalog detail client.
433
	 *
434
	 * The template file contains the HTML code and processing instructions
435
	 * to generate the HTML code that is inserted into the HTML page header
436
	 * of the rendered page in the frontend. The configuration string is the
437
	 * path to the template file relative to the templates directory (usually
438
	 * in client/html/templates).
439
	 *
440
	 * You can overwrite the template file configuration in extensions and
441
	 * provide alternative templates. These alternative templates should be
442
	 * named like the default one but suffixed by
443
	 * an unique name. You may use the name of your project for this. If
444
	 * you've implemented an alternative client class as well, it
445
	 * should be suffixed by the name of the new class.
446
	 *
447
	 * @param string Relative path to the template creating code for the HTML page head
448
	 * @since 2014.03
449
	 * @category Developer
450
	 * @see client/html/catalog/detail/template-body
451
	 * @see client/html/catalog/detail/404
452
	 */
453
}
454