Standard   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
dl 0
loc 243
rs 10
c 1
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A body() 0 36 2
A header() 0 14 2
A domains() 0 51 2
A data() 0 15 1
A stockUrl() 0 41 6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2025
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Catalog\Home;
12
13
/**
14
 * Implementation of catalog home section HTML clients for a configurable list of homes.
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/home/name
24
	 * Class name of the used catalog home 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\Home\Standard
34
	 *
35
	 * and you want to replace it with your own version named
36
	 *
37
	 *  \Aimeos\Client\Html\Catalog\Home\Myhome
38
	 *
39
	 * then you have to set the this configuration option:
40
	 *
41
	 *  client/html/catalog/home/name = Myhome
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 "MyHome"!
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/home/cache
71
		 * Enables or disables caching only for the catalog home 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/home
85
		 * All parameters defined for the catalog home 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#home
91
		 */
92
		$confkey = 'client/html/catalog/home';
93
		$config = $this->context()->config();
94
95
		if( $html = $this->cached( 'body', $uid, [], $confkey ) ) {
96
			return $this->modify( $html, $uid );
97
		}
98
99
		$template = $config->get( 'client/html/catalog/home/template-body', 'catalog/home/body' );
100
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
101
		$html = $view->render( $template );
102
103
		return $this->cache( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
104
	}
105
106
107
	/**
108
	 * Returns the HTML string for insertion into the header.
109
	 *
110
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
111
	 * @return string|null String including HTML tags for the header on error
112
	 */
113
	public function header( string $uid = '' ) : ?string
114
	{
115
		$confkey = 'client/html/catalog/home';
116
		$config = $this->context()->config();
117
118
		if( $html = $this->cached( 'header', $uid, [], $confkey ) ) {
119
			return $this->modify( $html, $uid );
120
		}
121
122
		$template = $config->get( 'client/html/catalog/home/template-header', 'catalog/home/header' );
123
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
124
		$html = $view->render( $template );
125
126
		return $this->cache( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
127
	}
128
129
130
	/**
131
	 * Sets the necessary parameter values in the view.
132
	 *
133
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
134
	 * @param array &$tags Result array for the list of tags that are associated to the output
135
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
136
	 * @return \Aimeos\Base\View\Iface Modified view object
137
	 */
138
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], ?string &$expire = null ) : \Aimeos\Base\View\Iface
139
	{
140
		$tree = \Aimeos\Controller\Frontend::create( $this->context(), 'catalog' )->uses( $this->domains() )
141
			->getTree( \Aimeos\Controller\Frontend\Catalog\Iface::LIST );
142
143
		$products = \Aimeos\Controller\Frontend::create( $this->context(), 'product' )->uses( $this->domains() )
144
			->category( $tree->getChildren()->getId()->all(), 'promotion' )->search();
145
146
		$this->addMetaItemCatalog( $tree, $expire, $tags );
147
		$this->addMetaItems( $products, $expire, $tags, ['product'] );
148
149
		$view->homeTree = $tree;
150
		$view->homeStockUrl = $this->stockUrl( $products );
151
152
		return parent::data( $view, $tags, $expire );
153
	}
154
155
156
	/**
157
	 * Returns the data domains fetched along with the products
158
	 *
159
	 * @return array List of domain names
160
	 */
161
	protected function domains() : array
162
	{
163
		$context = $this->context();
164
		$config = $context->config();
165
166
		/** client/html/catalog/home/domains
167
		 * A list of domain names whose items should be available in the catalog home view template
168
		 *
169
		 * The templates rendering home lists usually add the images, prices
170
		 * and texts associated to each home item. If you want to display additional
171
		 * content like the home attributes, you can configure your own list of
172
		 * domains (attribute, media, price, home, text, etc. are domains)
173
		 * whose items are fetched from the storage. Please keep in mind that
174
		 * the more domains you add to the configuration, the more time is required
175
		 * for fetching the content!
176
		 *
177
		 * This configuration option overwrites the "client/html/catalog/domains"
178
		 * option that allows to configure the domain names of the items fetched
179
		 * for all catalog related data.
180
		 *
181
		 * @param array List of domain names
182
		 * @since 2020.10
183
		 * @see client/html/catalog/domains
184
		 * @see client/html/catalog/detail/domains
185
		 * @see client/html/catalog/stage/domains
186
		 * @see client/html/catalog/lists/domains
187
		 */
188
		$domains = ['catalog', 'media', 'media/property', 'price', 'supplier', 'text', 'product' => ['promotion']];
189
		$domains = $config->get( 'client/html/catalog/domains', $domains );
190
		$domains = $config->get( 'client/html/catalog/home/domains', $domains );
191
192
		/** client/html/catalog/home/basket-add
193
		 * Display the "add to basket" button for each product item in the catalog home component
194
		 *
195
		 * Enables the button for adding products to the basket for the listed products.
196
		 * This works for all type of products, even for selection products with product
197
		 * variants and product bundles. By default, also optional attributes are
198
		 * displayed if they have been associated to a product.
199
		 *
200
		 * @param boolean True to display the button, false to hide it
201
		 * @since 2020.10
202
		 * @see client/html/catalog/lists/basket-add
203
		 * @see client/html/catalog/detail/basket-add
204
		 * @see client/html/basket/related/basket-add
205
		 * @see client/html/catalog/product/basket-add
206
		 */
207
		if( $config->get( 'client/html/catalog/home/basket-add', false ) ) {
208
			$domains = array_merge_recursive( $domains, ['attribute' => ['variant', 'custom', 'config']] );
209
		}
210
211
		return $domains;
212
	}
213
214
215
	/**
216
	 * Returns the list of stock URLs for the given products
217
	 *
218
	 * @param \Aimeos\Map $products List of products
219
	 * @return \Aimeos\Map List of stock URLs
220
	 */
221
	protected function stockUrl( \Aimeos\Map $products ) : \Aimeos\Map
222
	{
223
		$articles = map();
224
		$config = $this->context()->config();
225
226
		if( $config->get( 'client/html/catalog/home/basket-add', false ) )
227
		{
228
			foreach( $products as $product )
229
			{
230
				if( $product->getType() === 'select' ) {
231
					$articles->union( $product->getRefItems( 'product', 'default', 'default' ) );
232
				}
233
			}
234
		}
235
236
		/** client/html/catalog/home/stock/enable
237
		 * Enables or disables displaying product stock levels in product list views
238
		 *
239
		 * This configuration option allows shop owners to display product
240
		 * stock levels for each product in list views or to disable
241
		 * fetching product stock information.
242
		 *
243
		 * The stock information is fetched via AJAX and inserted via Javascript.
244
		 * This allows to cache product items by leaving out such highly
245
		 * dynamic content like stock levels which changes with each order.
246
		 *
247
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
248
		 * @since 2020.10
249
		 * @see client/html/catalog/detail/stock/enable
250
		 * @see client/html/catalog/stock/url/target
251
		 * @see client/html/catalog/stock/url/controller
252
		 * @see client/html/catalog/stock/url/action
253
		 * @see client/html/catalog/stock/url/config
254
		 */
255
		$enabled = $config->get( 'client/html/catalog/home/stock/enable', true );
256
257
		if( !$enabled || $products->isEmpty() ) {
258
			return map();
259
		}
260
261
		return $this->getStockUrl( $this->view(), $products->union( $articles ) );
262
	}
263
264
265
	/** client/html/catalog/home/template-body
266
	 * Relative path to the HTML body template of the catalog home client.
267
	 *
268
	 * The template file contains the HTML code and processing instructions
269
	 * to generate the result shown in the body of the frontend. The
270
	 * configuration string is the path to the template file relative
271
	 * to the templates directory (usually in templates/client/html).
272
	 *
273
	 * You can overwrite the template file configuration in extensions and
274
	 * provide alternative templates. These alternative templates should be
275
	 * named like the default one but suffixed by
276
	 * an unique name. You may use the name of your project for this. If
277
	 * you've implemented an alternative client class as well, it
278
	 * should be suffixed by the name of the new class.
279
	 *
280
	 * @param string Relative path to the template creating code for the HTML page body
281
	 * @since 2020.10
282
	 * @see client/html/catalog/home/template-header
283
	 */
284
285
	/** client/html/catalog/home/template-header
286
	 * Relative path to the HTML header template of the catalog home client.
287
	 *
288
	 * The template file contains the HTML code and processing instructions
289
	 * to generate the HTML code that is inserted into the HTML page header
290
	 * of the rendered page in the frontend. The configuration string is the
291
	 * path to the template file relative to the templates directory (usually
292
	 * in templates/client/html).
293
	 *
294
	 * You can overwrite the template file configuration in extensions and
295
	 * provide alternative templates. These alternative templates should be
296
	 * named like the default one but suffixed by
297
	 * an unique name. You may use the name of your project for this. If
298
	 * you've implemented an alternative client class as well, it
299
	 * should be suffixed by the name of the new class.
300
	 *
301
	 * @param string Relative path to the template creating code for the HTML page head
302
	 * @since 2020.10
303
	 * @see client/html/catalog/home/template-body
304
	 */
305
306
	/** client/html/catalog/home/decorators/excludes
307
	 * Excludes decorators added by the "common" option from the catalog home html client
308
	 *
309
	 * Decorators extend the functionality of a class by adding new aspects
310
	 * (e.g. log what is currently done), executing the methods of the underlying
311
	 * class only in certain conditions (e.g. only for logged in users) or
312
	 * modify what is returned to the caller.
313
	 *
314
	 * This option allows you to remove a decorator added via
315
	 * "client/html/common/decorators/default" before they are wrapped
316
	 * around the html client.
317
	 *
318
	 *  client/html/catalog/home/decorators/excludes = array( 'decorator1' )
319
	 *
320
	 * This would remove the decorator named "decorator1" from the list of
321
	 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
322
	 * "client/html/common/decorators/default" to the html client.
323
	 *
324
	 * @param array List of decorator names
325
	 * @since 2020.10
326
	 * @see client/html/common/decorators/default
327
	 * @see client/html/catalog/home/decorators/global
328
	 * @see client/html/catalog/home/decorators/local
329
	 */
330
331
	/** client/html/catalog/home/decorators/global
332
	 * Adds a list of globally available decorators only to the catalog home html client
333
	 *
334
	 * Decorators extend the functionality of a class by adding new aspects
335
	 * (e.g. log what is currently done), executing the methods of the underlying
336
	 * class only in certain conditions (e.g. only for logged in users) or
337
	 * modify what is returned to the caller.
338
	 *
339
	 * This option allows you to wrap global decorators
340
	 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
341
	 *
342
	 *  client/html/catalog/home/decorators/global = array( 'decorator1' )
343
	 *
344
	 * This would add the decorator named "decorator1" defined by
345
	 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
346
	 *
347
	 * @param array List of decorator names
348
	 * @since 2020.10
349
	 * @see client/html/common/decorators/default
350
	 * @see client/html/catalog/home/decorators/excludes
351
	 * @see client/html/catalog/home/decorators/local
352
	 */
353
354
	/** client/html/catalog/home/decorators/local
355
	 * Adds a list of local decorators only to the catalog home html client
356
	 *
357
	 * Decorators extend the functionality of a class by adding new aspects
358
	 * (e.g. log what is currently done), executing the methods of the underlying
359
	 * class only in certain conditions (e.g. only for logged in users) or
360
	 * modify what is returned to the caller.
361
	 *
362
	 * This option allows you to wrap local decorators
363
	 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
364
	 *
365
	 *  client/html/catalog/home/decorators/local = array( 'decorator2' )
366
	 *
367
	 * This would add the decorator named "decorator2" defined by
368
	 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
369
	 *
370
	 * @param array List of decorator names
371
	 * @since 2020.10
372
	 * @see client/html/common/decorators/default
373
	 * @see client/html/catalog/home/decorators/excludes
374
	 * @see client/html/catalog/home/decorators/global
375
	 */
376
}
377