Passed
Push — master ( ad1a79...28608a )
by Aimeos
03:13
created

Standard::services()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 37
rs 10
c 0
b 0
f 0
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
		$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
330
		$this->call( 'seen', $productItem );
331
332
		return parent::data( $view, $tags, $expire );
333
	}
334
335
336
	/**
337
	 * Sets the necessary parameter values in the view.
338
	 *
339
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
340
	 * @param array &$tags Result array for the list of tags that are associated to the output
341
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
342
	 * @return \Aimeos\Base\View\Iface Modified view object
343
	 */
344
	public function navigator() : string
345
	{
346
		$view = $this->view();
347
		$context = $this->context();
348
349
		if( is_numeric( $pos = $view->param( 'd_pos' ) ) )
350
		{
351
			if( $pos < 1 ) {
352
				$pos = $start = 0; $size = 2;
353
			} else {
354
				$start = $pos - 1; $size = 3;
355
			}
356
357
			$site = $context->locale()->getSiteItem()->getCode();
358
			$params = $context->session()->get( 'aimeos/catalog/lists/params/last/' . $site, [] );
359
			$level = $view->config( 'client/html/catalog/lists/levels', \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
360
361
			$catids = $view->value( $params, 'f_catid', $view->config( 'client/html/catalog/lists/catid-default' ) );
362
			$sort = $view->value( $params, 'f_sort', $view->config( 'client/html/catalog/lists/sort', 'relevance' ) );
363
364
			$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
365
				->sort( $sort ) // prioritize user sorting over the sorting through relevance and category
366
				->allOf( $view->value( $params, 'f_attrid', [] ) )
367
				->oneOf( $view->value( $params, 'f_optid', [] ) )
368
				->oneOf( $view->value( $params, 'f_oneid', [] ) )
369
				->text( $view->value( $params, 'f_search' ) )
370
				->category( $catids, 'default', $level )
371
				->slice( $start, $size )
372
				->uses( ['text'] )
373
				->search();
374
375
			if( ( $count = count( $products ) ) > 1 )
376
			{
377
				if( $pos > 0 && ( $product = $products->first() ) !== null )
378
				{
379
					$param = ['d_name' => $product->getName( 'url ' ), 'd_prodid' => $product->getId(), 'd_pos' => $pos - 1];
380
					$view->navigationPrev = $view->link( 'client/html/catalog/detail/url', $param );
381
				}
382
383
				if( ( $pos === 0 || $count === 3 ) && ( $product = $products->last() ) !== null )
384
				{
385
					$param = ['d_name' => $product->getName( 'url ' ), 'd_prodid' => $product->getId(), 'd_pos' => $pos + 1];
386
					$view->navigationNext = $view->link( 'client/html/catalog/detail/url', $param );
387
				}
388
			}
389
390
			$config = $context->config();
391
			$template = $config->get( 'client/html/catalog/detail/template-navigator', 'catalog/detail/navigator' );
392
393
			return $view->render( $template );
394
		}
395
396
		return '';
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/seen-partial
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/seen-partial', 'catalog/detail/seen-partial' );
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