Passed
Push — master ( 25bd95...9e12c4 )
by Aimeos
02:39
created

Standard::body()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 67
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 67
rs 9.7998

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