Standard   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 56
dl 0
loc 262
rs 10
c 3
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A body() 0 37 2
A modify() 0 3 1
A header() 0 15 2
A domains() 0 35 2
A data() 0 46 4
A stockUrl() 0 30 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2025
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Catalog\Product;
12
13
/**
14
 * Implementation of catalog product section HTML clients for a configurable list of products.
15
 *
16
 * @package Client
17
 * @subpackage Html
18
 */
19
class Standard
20
	extends \Aimeos\Client\Html\Catalog\Base
21
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
22
{
23
	/** client/html/catalog/product/name
24
	 * Class name of the used catalog product client implementation
25
	 *
26
	 * Each default HTML client can be replace by an alternative imlementation.
27
	 * To use this implementation, you have to set the last part of the class
28
	 * name as configuration value so the client factory knows which class it
29
	 * has to instantiate.
30
	 *
31
	 * For example, if the name of the default class is
32
	 *
33
	 *  \Aimeos\Client\Html\Catalog\Product\Standard
34
	 *
35
	 * and you want to replace it with your own version named
36
	 *
37
	 *  \Aimeos\Client\Html\Catalog\Product\Myproduct
38
	 *
39
	 * then you have to set the this configuration option:
40
	 *
41
	 *  client/html/catalog/product/name = Myproduct
42
	 *
43
	 * The value is the last part of your own class name and it's case sensitive,
44
	 * so take care that the configuration value is exactly named like the last
45
	 * part of the class name.
46
	 *
47
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
48
	 * characters are possible! You should always start the last part of the class
49
	 * name with an upper case character and continue only with lower case characters
50
	 * or numbers. Avoid chamel case names like "MyProduct"!
51
	 *
52
	 * @param string Last part of the class name
53
	 * @since 2019.06
54
	 */
55
56
57
	private array $tags = [];
58
	private ?string $expire = null;
59
	private ?\Aimeos\Base\View\Iface $view = null;
60
61
62
	/**
63
	 * Returns the HTML code for insertion into the body.
64
	 *
65
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
66
	 * @return string HTML code
67
	 */
68
	public function body( string $uid = '' ) : string
69
	{
70
		/** client/html/catalog/product/cache
71
		 * Enables or disables caching only for the catalog product component
72
		 *
73
		 * Disable caching for components can be useful if you would have too much
74
		 * entries to cache or if the component contains non-cacheable parts that
75
		 * can't be replaced using the modify() method.
76
		 *
77
		 * @param boolean True to enable caching, false to disable
78
		 * @see client/html/catalog/detail/cache
79
		 * @see client/html/catalog/filter/cache
80
		 * @see client/html/catalog/stage/cache
81
		 * @see client/html/catalog/list/cache
82
		 */
83
84
		/** client/html/catalog/product
85
		 * All parameters defined for the catalog product component and its subparts
86
		 *
87
		 * Please refer to the single settings for details.
88
		 *
89
		 * @param array Associative list of name/value settings
90
		 * @see client/html/catalog#product
91
		 */
92
		$confkey = 'client/html/catalog/product';
93
94
		if( $html = $this->cached( 'body', $uid, [], $confkey ) ) {
95
			return $this->modify( $html, $uid );
96
		}
97
98
		$config = $this->context()->config();
99
		$template = $config->get( 'client/html/catalog/product/template-body', 'catalog/product/body' );
100
101
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
102
		$html = $view->render( $template );
103
104
		return $this->cache( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
105
	}
106
107
108
	/**
109
	 * Returns the HTML string for insertion into the header.
110
	 *
111
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
112
	 * @return string|null String including HTML tags for the header on error
113
	 */
114
	public function header( string $uid = '' ) : ?string
115
	{
116
		$confkey = 'client/html/catalog/product';
117
118
		if( $html = $this->cached( 'header', $uid, [], $confkey ) ) {
119
			return $this->modify( $html, $uid );
120
		}
121
122
		$config = $this->context()->config();
123
		$template = $config->get( 'client/html/catalog/product/template-header', 'catalog/product/header' );
124
125
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
126
		$html = $view->render( $template );
127
128
		return $this->cache( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
129
	}
130
131
132
	/**
133
	 * Modifies the cached content to replace content based on sessions or cookies.
134
	 *
135
	 * @param string $content Cached content
136
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
137
	 * @return string Modified content
138
	 */
139
	public function modify( string $content, string $uid ) : string
140
	{
141
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.lists.items.csrf' );
142
	}
143
144
145
	/**
146
	 * Sets the necessary parameter values in the view.
147
	 *
148
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
149
	 * @param array &$tags Result array for the list of tags that are associated to the output
150
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
151
	 * @return \Aimeos\Base\View\Iface Modified view object
152
	 */
153
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], ?string &$expire = null ) : \Aimeos\Base\View\Iface
154
	{
155
		$context = $this->context();
156
		$config = $context->config();
157
158
		/** client/html/catalog/product/product-codes
159
		 * List of codes of products to load for the current list.
160
		 * Should be set dynamically through some integration plugin,
161
		 * to allow a list of products with configurable products.
162
		 *
163
		 * @param string List of codes of products to load for the current list
164
		 * @since 2019.06
165
		 */
166
		$productCodes = $config->get( 'client/html/catalog/product/product-codes', [] );
167
168
		$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
169
			->compare( '==', 'product.code', $productCodes )
170
			->slice( 0, count( $productCodes ) )
171
			->uses( $this->domains() )
172
			->search();
173
174
		// Sort products by the order given in the configuration "client/html/catalog/product/product-codes".
175
		$productCodesOrder = array_flip( $productCodes );
176
		$products->usort( function( $a, $b ) use ( $productCodesOrder ) {
177
			return $productCodesOrder[$a->getCode()] - $productCodesOrder[$b->getCode()];
178
		} );
179
180
		$productItems = $products->copy();
181
182
		if( $config->get( 'client/html/catalog/product/basket-add', false ) )
183
		{
184
			foreach( $products as $product )
185
			{
186
				if( $product->getType() === 'select' ) {
187
					$productItems->union( $product->getRefItems( 'product', 'default', 'default' ) );
188
				}
189
			}
190
		}
191
192
		$this->addMetaItems( $products, $expire, $tags, ['product'] );
193
194
		$view->productItems = $products;
195
		$view->productTotal = count( $products );
196
		$view->itemsStockUrl = $this->stockUrl( $productItems );
197
198
		return parent::data( $view, $tags, $expire );
199
	}
200
201
202
	/**
203
	 * Returns the data domains fetched along with the products
204
	 *
205
	 * @return array List of domain names
206
	 */
207
	protected function domains() : array
208
	{
209
		$config = $this->context()->config();
210
211
		/** client/html/catalog/product/domains
212
		 * A list of domain names whose items should be available in the catalog product view template
213
		 *
214
		 * The templates rendering product lists usually add the images, prices
215
		 * and texts associated to each product item. If you want to display additional
216
		 * content like the product attributes, you can configure your own list of
217
		 * domains (attribute, media, price, product, text, etc. are domains)
218
		 * whose items are fetched from the storage. Please keep in mind that
219
		 * the more domains you add to the configuration, the more time is required
220
		 * for fetching the content!
221
		 *
222
		 * This configuration option overwrites the "client/html/catalog/domains"
223
		 * option that allows to configure the domain names of the items fetched
224
		 * for all catalog related data.
225
		 *
226
		 * @param array List of domain names
227
		 * @since 2019.06
228
		 * @see client/html/catalog/domains
229
		 * @see client/html/catalog/detail/domains
230
		 * @see client/html/catalog/stage/domains
231
		 * @see client/html/catalog/lists/domains
232
		 */
233
		$domains = ['catalog', 'media', 'media/property', 'price', 'supplier', 'text'];
234
		$domains = $config->get( 'client/html/catalog/domains', $domains );
235
		$domains = $config->get( 'client/html/catalog/product/domains', $domains );
236
237
		if( $config->get( 'client/html/catalog/product/basket-add', false ) ) {
238
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute' => ['variant', 'custom', 'config']] );
239
		}
240
241
		return $domains;
242
	}
243
244
245
	/**
246
	 * Returns the list of stock URLs for the given products
247
	 *
248
	 * @param \Aimeos\Map $products List of products
249
	 * @return \Aimeos\Map List of stock URLs
250
	 */
251
	protected function stockUrl( \Aimeos\Map $products ) : \Aimeos\Map
252
	{
253
		$config = $this->context()->config();
254
255
		/** client/html/catalog/product/stock/enable
256
		 * Enables or disables displaying product stock levels in product list views
257
		 *
258
		 * This configuration option allows shop owners to display product
259
		 * stock levels for each product in list views or to disable
260
		 * fetching product stock information.
261
		 *
262
		 * The stock information is fetched via AJAX and inserted via Javascript.
263
		 * This allows to cache product items by leaving out such highly
264
		 * dynamic content like stock levels which changes with each order.
265
		 *
266
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
267
		 * @since 2019.06
268
		 * @see client/html/catalog/detail/stock/enable
269
		 * @see client/html/catalog/stock/url/target
270
		 * @see client/html/catalog/stock/url/controller
271
		 * @see client/html/catalog/stock/url/action
272
		 * @see client/html/catalog/stock/url/config
273
		 */
274
		$enabled = $config->get( 'client/html/catalog/product/stock/enable', true );
275
276
		if( !$enabled || $products->isEmpty() ) {
277
			return map();
278
		}
279
280
		return $this->getStockUrl( $this->view(), $products );
281
	}
282
283
284
	/** client/html/catalog/product/template-body
285
	 * Relative path to the HTML body template of the catalog product client.
286
	 *
287
	 * The template file contains the HTML code and processing instructions
288
	 * to generate the result shown in the body of the frontend. The
289
	 * configuration string is the path to the template file relative
290
	 * to the templates directory (usually in templates/client/html).
291
	 *
292
	 * You can overwrite the template file configuration in extensions and
293
	 * provide alternative templates. These alternative templates should be
294
	 * named like the default one but suffixed by
295
	 * an unique name. You may use the name of your project for this. If
296
	 * you've implemented an alternative client class as well, it
297
	 * should be suffixed by the name of the new class.
298
	 *
299
	 * @param string Relative path to the template creating code for the HTML page body
300
	 * @since 2019.06
301
	 * @see client/html/catalog/product/template-header
302
	 */
303
304
	/** client/html/catalog/product/template-header
305
	 * Relative path to the HTML header template of the catalog product client.
306
	 *
307
	 * The template file contains the HTML code and processing instructions
308
	 * to generate the HTML code that is inserted into the HTML page header
309
	 * of the rendered page in the frontend. The configuration string is the
310
	 * path to the template file relative to the templates directory (usually
311
	 * in templates/client/html).
312
	 *
313
	 * You can overwrite the template file configuration in extensions and
314
	 * provide alternative templates. These alternative templates should be
315
	 * named like the default one but suffixed by
316
	 * an unique name. You may use the name of your project for this. If
317
	 * you've implemented an alternative client class as well, it
318
	 * should be suffixed by the name of the new class.
319
	 *
320
	 * @param string Relative path to the template creating code for the HTML page head
321
	 * @since 2019.06
322
	 * @see client/html/catalog/product/template-body
323
	 */
324
325
	/** client/html/catalog/product/decorators/excludes
326
	 * Excludes decorators added by the "common" option from the catalog product html client
327
	 *
328
	 * Decorators extend the functionality of a class by adding new aspects
329
	 * (e.g. log what is currently done), executing the methods of the underlying
330
	 * class only in certain conditions (e.g. only for logged in users) or
331
	 * modify what is returned to the caller.
332
	 *
333
	 * This option allows you to remove a decorator added via
334
	 * "client/html/common/decorators/default" before they are wrapped
335
	 * around the html client.
336
	 *
337
	 *  client/html/catalog/product/decorators/excludes = array( 'decorator1' )
338
	 *
339
	 * This would remove the decorator named "decorator1" from the list of
340
	 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
341
	 * "client/html/common/decorators/default" to the html client.
342
	 *
343
	 * @param array List of decorator names
344
	 * @see client/html/common/decorators/default
345
	 * @see client/html/catalog/product/decorators/global
346
	 * @see client/html/catalog/product/decorators/local
347
	 */
348
349
	/** client/html/catalog/product/decorators/global
350
	 * Adds a list of globally available decorators only to the catalog product html client
351
	 *
352
	 * Decorators extend the functionality of a class by adding new aspects
353
	 * (e.g. log what is currently done), executing the methods of the underlying
354
	 * class only in certain conditions (e.g. only for logged in users) or
355
	 * modify what is returned to the caller.
356
	 *
357
	 * This option allows you to wrap global decorators
358
	 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
359
	 *
360
	 *  client/html/catalog/product/decorators/global = array( 'decorator1' )
361
	 *
362
	 * This would add the decorator named "decorator1" defined by
363
	 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
364
	 *
365
	 * @param array List of decorator names
366
	 * @see client/html/common/decorators/default
367
	 * @see client/html/catalog/product/decorators/excludes
368
	 * @see client/html/catalog/product/decorators/local
369
	 */
370
371
	/** client/html/catalog/product/decorators/local
372
	 * Adds a list of local decorators only to the catalog product html client
373
	 *
374
	 * Decorators extend the functionality of a class by adding new aspects
375
	 * (e.g. log what is currently done), executing the methods of the underlying
376
	 * class only in certain conditions (e.g. only for logged in users) or
377
	 * modify what is returned to the caller.
378
	 *
379
	 * This option allows you to wrap local decorators
380
	 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
381
	 *
382
	 *  client/html/catalog/product/decorators/local = array( 'decorator2' )
383
	 *
384
	 * This would add the decorator named "decorator2" defined by
385
	 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
386
	 *
387
	 * @param array List of decorator names
388
	 * @see client/html/common/decorators/default
389
	 * @see client/html/catalog/product/decorators/excludes
390
	 * @see client/html/catalog/product/decorators/global
391
	 */
392
}
393