Passed
Push — master ( 4a159f...7bc452 )
by Aimeos
03:58
created

Standard::modify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
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), 2020-2022
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/subparts
24
	 * List of HTML sub-clients rendered within the catalog home section
25
	 *
26
	 * The output of the frontend is composed of the code generated by the HTML
27
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
28
	 * that are responsible for rendering certain sub-parts of the output. The
29
	 * sub-clients can contain HTML clients themselves and therefore a
30
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
31
	 * the output that is placed inside the container of its parent.
32
	 *
33
	 * At first, always the HTML code generated by the parent is printed, then
34
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
35
	 * determines the order of the output of these sub-clients inside the parent
36
	 * container. If the configured list of clients is
37
	 *
38
	 *  array( "subclient1", "subclient2" )
39
	 *
40
	 * you can easily change the order of the output by reordering the subparts:
41
	 *
42
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
43
	 *
44
	 * You can also remove one or more parts if they shouldn't be rendered:
45
	 *
46
	 *  client/html/<clients>/subparts = array( "subclient1" )
47
	 *
48
	 * As the clients only generates structural HTML, the layout defined via CSS
49
	 * should support adding, removing or reordering content by a fluid like
50
	 * design.
51
	 *
52
	 * @param array List of sub-client names
53
	 * @since 2020.10
54
	 * @category Developer
55
	 */
56
	private $subPartPath = 'client/html/catalog/home/subparts';
57
	private $subPartNames = [];
58
59
	private $tags = [];
60
	private $expire;
61
	private $view;
62
63
64
	/**
65
	 * Returns the HTML code for insertion into the body.
66
	 *
67
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
68
	 * @return string HTML code
69
	 */
70
	public function body( string $uid = '' ) : string
71
	{
72
		$context = $this->context();
73
74
		/** client/html/catalog/home/cache
75
		 * Enables or disables caching only for the catalog home component
76
		 *
77
		 * Disable caching for components can be useful if you would have too much
78
		 * entries to cache or if the component contains non-cacheable parts that
79
		 * can't be replaced using the modify() method.
80
		 *
81
		 * @param boolean True to enable caching, false to disable
82
		 * @category Developer
83
		 * @category User
84
		 * @see client/html/catalog/detail/cache
85
		 * @see client/html/catalog/filter/cache
86
		 * @see client/html/catalog/stage/cache
87
		 * @see client/html/catalog/list/cache
88
		 */
89
90
		/** client/html/catalog/home
91
		 * All parameters defined for the catalog home component and its subparts
92
		 *
93
		 * Please refer to the single settings for details.
94
		 *
95
		 * @param array Associative list of name/value settings
96
		 * @category Developer
97
		 * @see client/html/catalog#home
98
		 */
99
		$confkey = 'client/html/catalog/home';
100
101
		if( ( $html = $this->getCached( 'body', $uid, [], $confkey ) ) === null )
102
		{
103
			$view = $this->view();
104
			$config = $this->context()->config();
105
106
			/** client/html/catalog/home/template-body
107
			 * Relative path to the HTML body template of the catalog home client.
108
			 *
109
			 * The template file contains the HTML code and processing instructions
110
			 * to generate the result shown in the body of the frontend. The
111
			 * configuration string is the path to the template file relative
112
			 * to the templates directory (usually in client/html/templates).
113
			 *
114
			 * You can overwrite the template file configuration in extensions and
115
			 * provide alternative templates. These alternative templates should be
116
			 * named like the default one but with the string "standard" replaced by
117
			 * an unique name. You may use the name of your project for this. If
118
			 * you've implemented an alternative client class as well, "standard"
119
			 * should be replaced by the name of the new class.
120
			 *
121
			 * @param string Relative path to the template creating code for the HTML page body
122
			 * @since 2020.10
123
			 * @category Developer
124
			 * @see client/html/catalog/home/template-header
125
			 */
126
			$tplconf = 'client/html/catalog/home/template-body';
127
			$default = 'catalog/home/body-standard';
128
129
			try
130
			{
131
				$html = '';
132
133
				if( !isset( $this->view ) ) {
134
					$view = $this->view = $this->object()->data( $view, $this->tags, $this->expire );
135
				}
136
137
				foreach( $this->getSubClients() as $subclient ) {
138
					$html .= $subclient->setView( $view )->body( $uid );
139
				}
140
				$view->listBody = $html;
141
142
				$html = $view->render( $config->get( $tplconf, $default ) );
143
				$this->setCached( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
144
145
				return $html;
146
			}
147
			catch( \Aimeos\Client\Html\Exception $e )
148
			{
149
				$error = array( $context->translate( 'client', $e->getMessage() ) );
150
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
151
			}
152
			catch( \Aimeos\Controller\Frontend\Exception $e )
153
			{
154
				$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
155
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
156
			}
157
			catch( \Aimeos\MShop\Exception $e )
158
			{
159
				$error = array( $context->translate( 'mshop', $e->getMessage() ) );
160
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
161
			}
162
			catch( \Exception $e )
163
			{
164
				$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
165
				$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
166
				$this->logException( $e );
167
			}
168
169
			$html = $view->render( $config->get( $tplconf, $default ) );
170
		}
171
		else
172
		{
173
			$html = $this->modify( $html, $uid );
174
		}
175
176
		return $html;
177
	}
178
179
180
	/**
181
	 * Returns the HTML string for insertion into the header.
182
	 *
183
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
184
	 * @return string|null String including HTML tags for the header on error
185
	 */
186
	public function header( string $uid = '' ) : ?string
187
	{
188
		$confkey = 'client/html/catalog/home';
189
190
		if( ( $html = $this->getCached( 'header', $uid, [], $confkey ) ) === null )
191
		{
192
			$view = $this->view();
193
			$config = $this->context()->config();
194
195
			/** client/html/catalog/home/template-header
196
			 * Relative path to the HTML header template of the catalog home client.
197
			 *
198
			 * The template file contains the HTML code and processing instructions
199
			 * to generate the HTML code that is inserted into the HTML page header
200
			 * of the rendered page in the frontend. The configuration string is the
201
			 * path to the template file relative to the templates directory (usually
202
			 * in client/html/templates).
203
			 *
204
			 * You can overwrite the template file configuration in extensions and
205
			 * provide alternative templates. These alternative templates should be
206
			 * named like the default one but with the string "standard" replaced by
207
			 * an unique name. You may use the name of your project for this. If
208
			 * you've implemented an alternative client class as well, "standard"
209
			 * should be replaced by the name of the new class.
210
			 *
211
			 * @param string Relative path to the template creating code for the HTML page head
212
			 * @since 2020.10
213
			 * @category Developer
214
			 * @see client/html/catalog/home/template-body
215
			 */
216
			$tplconf = 'client/html/catalog/home/template-header';
217
			$default = 'catalog/home/header-standard';
218
219
			try
220
			{
221
				$html = '';
222
223
				if( !isset( $this->view ) ) {
224
					$view = $this->view = $this->object()->data( $view, $this->tags, $this->expire );
225
				}
226
227
				foreach( $this->getSubClients() as $subclient ) {
228
					$html .= $subclient->setView( $view )->header( $uid );
229
				}
230
				$view->listHeader = $html;
231
232
				$html = $view->render( $config->get( $tplconf, $default ) );
233
				$this->setCached( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
234
235
				return $html;
236
			}
237
			catch( \Exception $e )
238
			{
239
				$this->logException( $e );
240
			}
241
		}
242
		else
243
		{
244
			$html = $this->modify( $html, $uid );
245
		}
246
247
		return $html;
248
	}
249
250
251
	/**
252
	 * Returns the sub-client given by its name.
253
	 *
254
	 * @param string $type Name of the client type
255
	 * @param string|null $name Name of the sub-client (Default if null)
256
	 * @return \Aimeos\Client\Html\Iface Sub-client object
257
	 */
258
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
259
	{
260
		/** client/html/catalog/home/decorators/excludes
261
		 * Excludes decorators added by the "common" option from the catalog home html client
262
		 *
263
		 * Decorators extend the functionality of a class by adding new aspects
264
		 * (e.g. log what is currently done), executing the methods of the underlying
265
		 * class only in certain conditions (e.g. only for logged in users) or
266
		 * modify what is returned to the caller.
267
		 *
268
		 * This option allows you to remove a decorator added via
269
		 * "client/html/common/decorators/default" before they are wrapped
270
		 * around the html client.
271
		 *
272
		 *  client/html/catalog/home/decorators/excludes = array( 'decorator1' )
273
		 *
274
		 * This would remove the decorator named "decorator1" from the list of
275
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
276
		 * "client/html/common/decorators/default" to the html client.
277
		 *
278
		 * @param array List of decorator names
279
		 * @since 2020.10
280
		 * @category Developer
281
		 * @see client/html/common/decorators/default
282
		 * @see client/html/catalog/home/decorators/global
283
		 * @see client/html/catalog/home/decorators/local
284
		 */
285
286
		/** client/html/catalog/home/decorators/global
287
		 * Adds a list of globally available decorators only to the catalog home html client
288
		 *
289
		 * Decorators extend the functionality of a class by adding new aspects
290
		 * (e.g. log what is currently done), executing the methods of the underlying
291
		 * class only in certain conditions (e.g. only for logged in users) or
292
		 * modify what is returned to the caller.
293
		 *
294
		 * This option allows you to wrap global decorators
295
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
296
		 *
297
		 *  client/html/catalog/home/decorators/global = array( 'decorator1' )
298
		 *
299
		 * This would add the decorator named "decorator1" defined by
300
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
301
		 *
302
		 * @param array List of decorator names
303
		 * @since 2020.10
304
		 * @category Developer
305
		 * @see client/html/common/decorators/default
306
		 * @see client/html/catalog/home/decorators/excludes
307
		 * @see client/html/catalog/home/decorators/local
308
		 */
309
310
		/** client/html/catalog/home/decorators/local
311
		 * Adds a list of local decorators only to the catalog home html client
312
		 *
313
		 * Decorators extend the functionality of a class by adding new aspects
314
		 * (e.g. log what is currently done), executing the methods of the underlying
315
		 * class only in certain conditions (e.g. only for logged in users) or
316
		 * modify what is returned to the caller.
317
		 *
318
		 * This option allows you to wrap local decorators
319
		 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
320
		 *
321
		 *  client/html/catalog/home/decorators/local = array( 'decorator2' )
322
		 *
323
		 * This would add the decorator named "decorator2" defined by
324
		 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
325
		 *
326
		 * @param array List of decorator names
327
		 * @since 2020.10
328
		 * @category Developer
329
		 * @see client/html/common/decorators/default
330
		 * @see client/html/catalog/home/decorators/excludes
331
		 * @see client/html/catalog/home/decorators/global
332
		 */
333
334
		return $this->createSubClient( 'catalog/home/' . $type, $name );
335
	}
336
337
338
	/**
339
	 * Processes the input, e.g. store given values.
340
	 *
341
	 * A view must be available and this method doesn't generate any output
342
	 * besides setting view variables if necessary.
343
	 */
344
	public function init()
345
	{
346
		$context = $this->context();
347
		$view = $this->view();
348
349
		try
350
		{
351
			parent::init();
352
		}
353
		catch( \Aimeos\Client\Html\Exception $e )
354
		{
355
			$error = array( $context->translate( 'client', $e->getMessage() ) );
356
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
357
		}
358
		catch( \Aimeos\Controller\Frontend\Exception $e )
359
		{
360
			$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
361
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
362
		}
363
		catch( \Aimeos\MShop\Exception $e )
364
		{
365
			$error = array( $context->translate( 'mshop', $e->getMessage() ) );
366
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
367
		}
368
		catch( \Exception $e )
369
		{
370
			$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
371
			$view->homeErrorList = array_merge( $view->get( 'homeErrorList', [] ), $error );
372
			$this->logException( $e );
373
		}
374
	}
375
376
377
	/**
378
	 * Returns the list of sub-client names configured for the client.
379
	 *
380
	 * @return array List of HTML client names
381
	 */
382
	protected function getSubClientNames() : array
383
	{
384
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
385
	}
386
387
388
	/**
389
	 * Modifies the cached content to replace content based on sessions or cookies.
390
	 *
391
	 * @param string $content Cached content
392
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
393
	 * @return string Modified content
394
	 */
395
	public function modify( string $content, string $uid ) : string
396
	{
397
		$content = parent::modify( $content, $uid );
398
399
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.lists.items.csrf' );
400
	}
401
402
	/**
403
	 * Sets the necessary parameter values in the view.
404
	 *
405
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
406
	 * @param array &$tags Result array for the list of tags that are associated to the output
407
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
408
	 * @return \Aimeos\MW\View\Iface Modified view object
409
	 */
410
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
411
	{
412
		$context = $this->context();
413
		$config = $context->config();
414
415
		/** client/html/catalog/home/domains
416
		 * A list of domain names whose items should be available in the catalog home view template
417
		 *
418
		 * The templates rendering home lists usually add the images, prices
419
		 * and texts associated to each home item. If you want to display additional
420
		 * content like the home attributes, you can configure your own list of
421
		 * domains (attribute, media, price, home, text, etc. are domains)
422
		 * whose items are fetched from the storage. Please keep in mind that
423
		 * the more domains you add to the configuration, the more time is required
424
		 * for fetching the content!
425
		 *
426
		 * This configuration option overwrites the "client/html/catalog/domains"
427
		 * option that allows to configure the domain names of the items fetched
428
		 * for all catalog related data.
429
		 *
430
		 * @param array List of domain names
431
		 * @since 2020.10
432
		 * @category Developer
433
		 * @see client/html/catalog/domains
434
		 * @see client/html/catalog/detail/domains
435
		 * @see client/html/catalog/stage/domains
436
		 * @see client/html/catalog/lists/domains
437
		 */
438
		$domains = ['media', 'media/property', 'price', 'text', 'product' => ['promotion']];
439
		$domains = $config->get( 'client/html/catalog/domains', $domains );
440
		$domains = $config->get( 'client/html/catalog/home/domains', $domains );
441
442
		/** client/html/catalog/home/basket-add
443
		 * Display the "add to basket" button for each product item in the catalog home component
444
		 *
445
		 * Enables the button for adding products to the basket for the listed products.
446
		 * This works for all type of products, even for selection products with product
447
		 * variants and product bundles. By default, also optional attributes are
448
		 * displayed if they have been associated to a product.
449
		 *
450
		 * @param boolean True to display the button, false to hide it
451
		 * @since 2020.10
452
		 * @category Developer
453
		 * @category User
454
		 * @see client/html/catalog/lists/basket-add
455
		 * @see client/html/catalog/detail/basket-add
456
		 * @see client/html/basket/related/basket-add
457
		 * @see client/html/catalog/product/basket-add
458
		 */
459
		if( $config->get( 'client/html/catalog/home/basket-add', false ) ) {
460
			$domains = array_merge_recursive( $domains, ['attribute' => ['variant', 'custom', 'config']] );
461
		}
462
463
		$tree = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->uses( $domains )
464
			->getTree( \Aimeos\Controller\Frontend\Catalog\Iface::LIST );
465
466
467
		$articles = map();
468
		$products = $tree->getRefItems( 'product', null, 'promotion' );
469
470
		foreach( $tree->getChildren() as $child ) {
471
			$products->union( $child->getRefItems( 'product', null, 'promotion' ) );
472
		}
473
474
		if( $config->get( 'client/html/catalog/home/basket-add', false ) )
475
		{
476
			foreach( $products as $product )
477
			{
478
				if( $product->getType() === 'select' ) {
479
					$articles->union( $product->getRefItems( 'product', 'default', 'default' ) );
480
				}
481
			}
482
		}
483
484
		/** client/html/catalog/home/stock/enable
485
		 * Enables or disables displaying product stock levels in product list views
486
		 *
487
		 * This configuration option allows shop owners to display product
488
		 * stock levels for each product in list views or to disable
489
		 * fetching product stock information.
490
		 *
491
		 * The stock information is fetched via AJAX and inserted via Javascript.
492
		 * This allows to cache product items by leaving out such highly
493
		 * dynamic content like stock levels which changes with each order.
494
		 *
495
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
496
		 * @since 2020.10
497
		 * @category User
498
		 * @category Developer
499
		 * @see client/html/catalog/detail/stock/enable
500
		 * @see client/html/catalog/stock/url/target
501
		 * @see client/html/catalog/stock/url/controller
502
		 * @see client/html/catalog/stock/url/action
503
		 * @see client/html/catalog/stock/url/config
504
		 */
505
		if( !$products->isEmpty() && (bool) $config->get( 'client/html/catalog/home/stock/enable', true ) === true ) {
506
			$view->homeStockUrl = $this->getStockUrl( $view, $products->union( $articles ) );
507
		}
508
509
		// Delete cache when products are added or deleted even when in "tag-all" mode
510
		$this->addMetaItems( $tree, $expire, $tags, ['catalog', 'product'] );
511
512
		$view->homeTree = $tree;
513
514
		return parent::data( $view, $tags, $expire );
515
	}
516
}
517