Passed
Push — master ( d07637...3f0995 )
by Aimeos
04:09
created

Standard::getHeader()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 79
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 22
nop 1
dl 0
loc 79
rs 8.6186
c 0
b 0
f 0

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 Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2020
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Catalog\Lists;
13
14
15
/**
16
 * Default implementation of catalog list section HTML clients.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Catalog\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/catalog/lists/standard/subparts
26
	 * List of HTML sub-clients rendered within the catalog list section
27
	 *
28
	 * The output of the frontend is composed of the code generated by the HTML
29
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
30
	 * that are responsible for rendering certain sub-parts of the output. The
31
	 * sub-clients can contain HTML clients themselves and therefore a
32
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
33
	 * the output that is placed inside the container of its parent.
34
	 *
35
	 * At first, always the HTML code generated by the parent is printed, then
36
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
37
	 * determines the order of the output of these sub-clients inside the parent
38
	 * container. If the configured list of clients is
39
	 *
40
	 *  array( "subclient1", "subclient2" )
41
	 *
42
	 * you can easily change the order of the output by reordering the subparts:
43
	 *
44
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
45
	 *
46
	 * You can also remove one or more parts if they shouldn't be rendered:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1" )
49
	 *
50
	 * As the clients only generates structural HTML, the layout defined via CSS
51
	 * should support adding, removing or reordering content by a fluid like
52
	 * design.
53
	 *
54
	 * @param array List of sub-client names
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
	private $subPartPath = 'client/html/catalog/lists/standard/subparts';
59
60
	/** client/html/catalog/lists/promo/name
61
	 * Name of the promotion part used by the catalog list client implementation
62
	 *
63
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Catalog\Lists\Promo\Myname".
64
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
65
	 *
66
	 * @param string Last part of the client class name
67
	 * @since 2014.03
68
	 * @category Developer
69
	 */
70
71
	/** client/html/catalog/lists/items/name
72
	 * Name of the items part used by the catalog list client implementation
73
	 *
74
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Catalog\Lists\Items\Myname".
75
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
76
	 *
77
	 * @param string Last part of the client class name
78
	 * @since 2014.03
79
	 * @category Developer
80
	 */
81
	private $subPartNames = array( 'items' );
82
83
	private $tags = [];
84
	private $expire;
85
	private $view;
86
87
88
	/**
89
	 * Returns the HTML code for insertion into the body.
90
	 *
91
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
92
	 * @return string HTML code
93
	 */
94
	public function getBody( string $uid = '' ) : string
95
	{
96
		$view = $this->getView();
97
		$context = $this->getContext();
98
		$prefixes = ['f_catid', 'f_supid', 'f_sort', 'l_page', 'l_type'];
99
100
		/** client/html/catalog/lists/cache
101
		 * Enables or disables caching only for the catalog lists component
102
		 *
103
		 * Disable caching for components can be useful if you would have too much
104
		 * entries to cache or if the component contains non-cacheable parts that
105
		 * can't be replaced using the modifyBody() and modifyHeader() methods.
106
		 *
107
		 * @param boolean True to enable caching, false to disable
108
		 * @category Developer
109
		 * @category User
110
		 * @see client/html/catalog/detail/cache
111
		 * @see client/html/catalog/filter/cache
112
		 * @see client/html/catalog/stage/cache
113
		 */
114
115
		/** client/html/catalog/lists
116
		 * All parameters defined for the catalog list component and its subparts
117
		 *
118
		 * This returns all settings related to the filter component.
119
		 * Please refer to the single settings for details.
120
		 *
121
		 * @param array Associative list of name/value settings
122
		 * @category Developer
123
		 * @see client/html/catalog#list
124
		 */
125
		$confkey = 'client/html/catalog/lists';
126
127
		$args = map( $view->param() )->except( $prefixes )->filter( function( $val, $key ) {
128
			return !strncmp( $key, 'f_', 2 ) || !strncmp( $key, 'l_', 2 );
129
		} );
130
131
		if( !$args->isEmpty() || ( $html = $this->getCached( 'body', $uid, $prefixes, $confkey ) ) === null )
132
		{
133
			/** client/html/catalog/lists/standard/template-body
134
			 * Relative path to the HTML body template of the catalog list client.
135
			 *
136
			 * The template file contains the HTML code and processing instructions
137
			 * to generate the result shown in the body of the frontend. The
138
			 * configuration string is the path to the template file relative
139
			 * to the templates directory (usually in client/html/templates).
140
			 *
141
			 * You can overwrite the template file configuration in extensions and
142
			 * provide alternative templates. These alternative templates should be
143
			 * named like the default one but with the string "standard" replaced by
144
			 * an unique name. You may use the name of your project for this. If
145
			 * you've implemented an alternative client class as well, "standard"
146
			 * should be replaced by the name of the new class.
147
			 *
148
			 * It's also possible to create a specific template for each type, e.g.
149
			 * for the grid, list or whatever view you want to offer your users. In
150
			 * that case, you can configure the template by adding "-<type>" to the
151
			 * configuration key. To configure an alternative list view template for
152
			 * example, use the key
153
			 *
154
			 * client/html/catalog/lists/standard/template-body-list = catalog/lists/body-list.php
155
			 *
156
			 * The argument is the relative path to the new template file. The type of
157
			 * the view is determined by the "l_type" parameter (allowed characters for
158
			 * the types are a-z and 0-9), which is also stored in the session so users
159
			 * will keep the view during their visit. The catalog list type subpart
160
			 * contains the template for switching between list types.
161
			 *
162
			 * @param string Relative path to the template creating code for the HTML page body
163
			 * @since 2014.03
164
			 * @category Developer
165
			 * @see client/html/catalog/lists/standard/template-header
166
			 * @see client/html/catalog/lists/type/standard/template-body
167
			 */
168
			$tplconf = 'client/html/catalog/lists/standard/template-body';
169
			$default = 'catalog/lists/body-standard';
170
171
			try
172
			{
173
				if( !isset( $this->view ) ) {
174
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
175
				}
176
177
				$html = '';
178
				foreach( $this->getSubClients() as $subclient ) {
179
					$html .= $subclient->setView( $view )->getBody( $uid );
180
				}
181
				$view->listBody = $html;
182
183
				$html = $view->render( $this->getTemplatePath( $tplconf, $default ) );
184
				$this->setCached( 'body', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
185
186
				return $html;
187
			}
188
			catch( \Aimeos\Client\Html\Exception $e )
189
			{
190
				$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
191
				$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
192
			}
193
			catch( \Aimeos\Controller\Frontend\Exception $e )
194
			{
195
				$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
196
				$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
197
			}
198
			catch( \Aimeos\MShop\Exception $e )
199
			{
200
				$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
201
				$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
202
			}
203
			catch( \Exception $e )
204
			{
205
				$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
206
				$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
207
				$this->logException( $e );
208
			}
209
210
			$html = $view->render( $this->getTemplatePath( $tplconf, $default ) );
211
		}
212
		else
213
		{
214
			$html = $this->modifyBody( $html, $uid );
215
		}
216
217
		return $html;
218
	}
219
220
221
	/**
222
	 * Returns the HTML string for insertion into the header.
223
	 *
224
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
225
	 * @return string|null String including HTML tags for the header on error
226
	 */
227
	public function getHeader( string $uid = '' ) : ?string
228
	{
229
		$view = $this->getView();
230
		$confkey = 'client/html/catalog/lists';
231
		$prefixes = ['f_catid', 'f_supid', 'f_sort', 'l_page', 'l_type'];
232
233
		$args = map( $view->param() )->except( $prefixes )->filter( function( $val, $key ) {
234
			return !strncmp( $key, 'f_', 2 ) || !strncmp( $key, 'l_', 2 );
235
		} );
236
237
		if( !$args->isEmpty() || ( $html = $this->getCached( 'header', $uid, $prefixes, $confkey ) ) === null )
238
		{
239
			/** client/html/catalog/lists/standard/template-header
240
			 * Relative path to the HTML header template of the catalog list client.
241
			 *
242
			 * The template file contains the HTML code and processing instructions
243
			 * to generate the HTML code that is inserted into the HTML page header
244
			 * of the rendered page in the frontend. The configuration string is the
245
			 * path to the template file relative to the templates directory (usually
246
			 * in client/html/templates).
247
			 *
248
			 * You can overwrite the template file configuration in extensions and
249
			 * provide alternative templates. These alternative templates should be
250
			 * named like the default one but with the string "standard" replaced by
251
			 * an unique name. You may use the name of your project for this. If
252
			 * you've implemented an alternative client class as well, "standard"
253
			 * should be replaced by the name of the new class.
254
			 *
255
			 * It's also possible to create a specific template for each type, e.g.
256
			 * for the grid, list or whatever view you want to offer your users. In
257
			 * that case, you can configure the template by adding "-<type>" to the
258
			 * configuration key. To configure an alternative list view template for
259
			 * example, use the key
260
			 *
261
			 * client/html/catalog/lists/standard/template-header-list = catalog/lists/header-list.php
262
			 *
263
			 * The argument is the relative path to the new template file. The type of
264
			 * the view is determined by the "l_type" parameter (allowed characters for
265
			 * the types are a-z and 0-9), which is also stored in the session so users
266
			 * will keep the view during their visit. The catalog list type subpart
267
			 * contains the template for switching between list types.
268
			 *
269
			 * @param string Relative path to the template creating code for the HTML page head
270
			 * @since 2014.03
271
			 * @category Developer
272
			 * @see client/html/catalog/lists/standard/template-body
273
			 * @see client/html/catalog/lists/type/standard/template-body
274
			 */
275
			$tplconf = 'client/html/catalog/lists/standard/template-header';
276
			$default = 'catalog/lists/header-standard';
277
278
			try
279
			{
280
				if( !isset( $this->view ) ) {
281
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
282
				}
283
284
				$html = '';
285
				foreach( $this->getSubClients() as $subclient ) {
286
					$html .= $subclient->setView( $view )->getHeader( $uid );
287
				}
288
				$view->listHeader = $html;
289
290
				$html = $view->render( $this->getTemplatePath( $tplconf, $default ) );
291
				$this->setCached( 'header', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
292
293
				return $html;
294
			}
295
			catch( \Exception $e )
296
			{
297
				$this->logException( $e );
298
			}
299
		}
300
		else
301
		{
302
			$html = $this->modifyHeader( $html, $uid );
303
		}
304
305
		return $html;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $html does not seem to be defined for all execution paths leading up to this point.
Loading history...
306
	}
307
308
309
	/**
310
	 * Returns the sub-client given by its name.
311
	 *
312
	 * @param string $type Name of the client type
313
	 * @param string|null $name Name of the sub-client (Default if null)
314
	 * @return \Aimeos\Client\Html\Iface Sub-client object
315
	 */
316
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
317
	{
318
		/** client/html/catalog/lists/decorators/excludes
319
		 * Excludes decorators added by the "common" option from the catalog list html client
320
		 *
321
		 * Decorators extend the functionality of a class by adding new aspects
322
		 * (e.g. log what is currently done), executing the methods of the underlying
323
		 * class only in certain conditions (e.g. only for logged in users) or
324
		 * modify what is returned to the caller.
325
		 *
326
		 * This option allows you to remove a decorator added via
327
		 * "client/html/common/decorators/default" before they are wrapped
328
		 * around the html client.
329
		 *
330
		 *  client/html/catalog/lists/decorators/excludes = array( 'decorator1' )
331
		 *
332
		 * This would remove the decorator named "decorator1" from the list of
333
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
334
		 * "client/html/common/decorators/default" to the html client.
335
		 *
336
		 * @param array List of decorator names
337
		 * @since 2014.05
338
		 * @category Developer
339
		 * @see client/html/common/decorators/default
340
		 * @see client/html/catalog/lists/decorators/global
341
		 * @see client/html/catalog/lists/decorators/local
342
		 */
343
344
		/** client/html/catalog/lists/decorators/global
345
		 * Adds a list of globally available decorators only to the catalog list html client
346
		 *
347
		 * Decorators extend the functionality of a class by adding new aspects
348
		 * (e.g. log what is currently done), executing the methods of the underlying
349
		 * class only in certain conditions (e.g. only for logged in users) or
350
		 * modify what is returned to the caller.
351
		 *
352
		 * This option allows you to wrap global decorators
353
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
354
		 *
355
		 *  client/html/catalog/lists/decorators/global = array( 'decorator1' )
356
		 *
357
		 * This would add the decorator named "decorator1" defined by
358
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
359
		 *
360
		 * @param array List of decorator names
361
		 * @since 2014.05
362
		 * @category Developer
363
		 * @see client/html/common/decorators/default
364
		 * @see client/html/catalog/lists/decorators/excludes
365
		 * @see client/html/catalog/lists/decorators/local
366
		 */
367
368
		/** client/html/catalog/lists/decorators/local
369
		 * Adds a list of local decorators only to the catalog list html client
370
		 *
371
		 * Decorators extend the functionality of a class by adding new aspects
372
		 * (e.g. log what is currently done), executing the methods of the underlying
373
		 * class only in certain conditions (e.g. only for logged in users) or
374
		 * modify what is returned to the caller.
375
		 *
376
		 * This option allows you to wrap local decorators
377
		 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
378
		 *
379
		 *  client/html/catalog/lists/decorators/local = array( 'decorator2' )
380
		 *
381
		 * This would add the decorator named "decorator2" defined by
382
		 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
383
		 *
384
		 * @param array List of decorator names
385
		 * @since 2014.05
386
		 * @category Developer
387
		 * @see client/html/common/decorators/default
388
		 * @see client/html/catalog/lists/decorators/excludes
389
		 * @see client/html/catalog/lists/decorators/global
390
		 */
391
392
		return $this->createSubClient( 'catalog/lists/' . $type, $name );
393
	}
394
395
396
	/**
397
	 * Processes the input, e.g. store given values.
398
	 *
399
	 * A view must be available and this method doesn't generate any output
400
	 * besides setting view variables if necessary.
401
	 */
402
	public function process()
403
	{
404
		$context = $this->getContext();
405
		$view = $this->getView();
406
407
		try
408
		{
409
			$site = $context->getLocale()->getSiteItem()->getCode();
410
			$params = $this->getClientParams( $view->param() );
411
412
			$catId = $context->getConfig()->get( 'client/html/catalog/lists/catid-default' );
413
414
			if( ( $catId = $view->param( 'f_catid', $catId ) ) )
415
			{
416
				$params['f_name'] = $view->param( 'f_name' );
417
				$params['f_catid'] = $catId;
418
			}
419
420
			$context->getSession()->set( 'aimeos/catalog/lists/params/last/' . $site, $params );
421
422
			parent::process();
423
		}
424
		catch( \Aimeos\Client\Html\Exception $e )
425
		{
426
			$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
427
			$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
428
		}
429
		catch( \Aimeos\Controller\Frontend\Exception $e )
430
		{
431
			$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
432
			$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
433
		}
434
		catch( \Aimeos\MShop\Exception $e )
435
		{
436
			$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
437
			$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
438
		}
439
		catch( \Exception $e )
440
		{
441
			$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
442
			$view->listErrorList = array_merge( $view->get( 'listErrorList', [] ), $error );
443
			$this->logException( $e );
444
		}
445
	}
446
447
448
	/**
449
	 * Returns the list of sub-client names configured for the client.
450
	 *
451
	 * @return array List of HTML client names
452
	 */
453
	protected function getSubClientNames() : array
454
	{
455
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
456
	}
457
458
459
	/**
460
	 * Sets the necessary parameter values in the view.
461
	 *
462
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
463
	 * @param array &$tags Result array for the list of tags that are associated to the output
464
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
465
	 * @return \Aimeos\MW\View\Iface Modified view object
466
	 */
467
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
468
	{
469
		$total = 0;
470
		$context = $this->getContext();
471
		$config = $context->getConfig();
472
473
474
		/** client/html/catalog/domains
475
		 * A list of domain names whose items should be available in the catalog view templates
476
		 *
477
		 * The templates rendering catalog related data usually add the images and
478
		 * texts associated to each item. If you want to display additional
479
		 * content like the attributes, you can configure your own list of
480
		 * domains (attribute, media, price, product, text, etc. are domains)
481
		 * whose items are fetched from the storage. Please keep in mind that
482
		 * the more domains you add to the configuration, the more time is required
483
		 * for fetching the content!
484
		 *
485
		 * This configuration option can be overwritten by the "client/html/catalog/lists/domains"
486
		 * configuration option that allows to configure the domain names of the
487
		 * items fetched specifically for all types of product listings.
488
		 *
489
		 * @param array List of domain names
490
		 * @since 2014.03
491
		 * @category Developer
492
		 * @see client/html/catalog/lists/domains
493
		 * @see client/html/catalog/lists/catid-default
494
		 * @see client/html/catalog/lists/size
495
		 * @see client/html/catalog/lists/levels
496
		 * @see client/html/catalog/lists/sort
497
		 * @see client/html/catalog/lists/pages
498
		 */
499
		$domains = $config->get( 'client/html/catalog/domains', ['media', 'price', 'text'] );
500
501
		/** client/html/catalog/lists/domains
502
		 * A list of domain names whose items should be available in the product list view template
503
		 *
504
		 * The templates rendering product lists usually add the images, prices
505
		 * and texts associated to each product item. If you want to display additional
506
		 * content like the product attributes, you can configure your own list of
507
		 * domains (attribute, media, price, product, text, etc. are domains)
508
		 * whose items are fetched from the storage. Please keep in mind that
509
		 * the more domains you add to the configuration, the more time is required
510
		 * for fetching the content!
511
		 *
512
		 * This configuration option overwrites the "client/html/catalog/domains"
513
		 * option that allows to configure the domain names of the items fetched
514
		 * for all catalog related data.
515
		 *
516
		 * @param array List of domain names
517
		 * @since 2014.03
518
		 * @category Developer
519
		 * @see client/html/catalog/domains
520
		 * @see client/html/catalog/detail/domains
521
		 * @see client/html/catalog/stage/domains
522
		 * @see client/html/catalog/lists/catid-default
523
		 * @see client/html/catalog/lists/size
524
		 * @see client/html/catalog/lists/levels
525
		 * @see client/html/catalog/lists/sort
526
		 * @see client/html/catalog/lists/pages
527
		 */
528
		$domains = $config->get( 'client/html/catalog/lists/domains', $domains );
529
530
		if( $view->config( 'client/html/catalog/lists/basket-add', false ) ) {
531
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute'] );
532
		}
533
534
		/** client/html/catalog/lists/pages
535
		 * Maximum number of product pages shown in pagination
536
		 *
537
		 * Limits the number of product pages that are shown in the navigation.
538
		 * The user is able to move to the next page (or previous one if it's not
539
		 * the first) to display the next (or previous) products.
540
		 *
541
		 * The value must be a positive integer number. Negative values are not
542
		 * allowed. The value can't be overwritten per request.
543
		 *
544
		 * @param integer Number of pages
545
		 * @since 2019.04
546
		 * @category User
547
		 * @category Developer
548
		 * @see client/html/catalog/lists/catid-default
549
		 * @see client/html/catalog/lists/domains
550
		 * @see client/html/catalog/lists/levels
551
		 * @see client/html/catalog/lists/sort
552
		 * @see client/html/catalog/lists/size
553
		 */
554
		$pages = $config->get( 'client/html/catalog/lists/pages', 100 );
555
556
		/** client/html/catalog/lists/size
557
		 * The number of products shown in a list page
558
		 *
559
		 * Limits the number of products that are shown in the list pages to the
560
		 * given value. If more products are available, the products are split
561
		 * into bunches which will be shown on their own list page. The user is
562
		 * able to move to the next page (or previous one if it's not the first)
563
		 * to display the next (or previous) products.
564
		 *
565
		 * The value must be an integer number from 1 to 100. Negative values as
566
		 * well as values above 100 are not allowed. The value can be overwritten
567
		 * per request if the "l_size" parameter is part of the URL.
568
		 *
569
		 * @param integer Number of products
570
		 * @since 2014.03
571
		 * @category User
572
		 * @category Developer
573
		 * @see client/html/catalog/lists/catid-default
574
		 * @see client/html/catalog/lists/domains
575
		 * @see client/html/catalog/lists/levels
576
		 * @see client/html/catalog/lists/sort
577
		 * @see client/html/catalog/lists/pages
578
		 */
579
		$size = $config->get( 'client/html/catalog/lists/size', 48 );
580
581
		/** client/html/catalog/lists/levels
582
		 * Include products of sub-categories in the product list of the current category
583
		 *
584
		 * Sometimes it may be useful to show products of sub-categories in the
585
		 * current category product list, e.g. if the current category contains
586
		 * no products at all or if there are only a few products in all categories.
587
		 *
588
		 * Possible constant values for this setting are:
589
		 * * 1 : Only products from the current category
590
		 * * 2 : Products from the current category and the direct child categories
591
		 * * 3 : Products from the current category and the whole category sub-tree
592
		 *
593
		 * Caution: Please keep in mind that displaying products of sub-categories
594
		 * can slow down your shop, especially if it contains more than a few
595
		 * products! You have no real control over the positions of the products
596
		 * in the result list too because all products from different categories
597
		 * with the same position value are placed randomly.
598
		 *
599
		 * Usually, a better way is to associate products to all categories they
600
		 * should be listed in. This can be done manually if there are only a few
601
		 * ones or during the product import automatically.
602
		 *
603
		 * @param integer Tree level constant
604
		 * @since 2015.11
605
		 * @category Developer
606
		 * @see client/html/catalog/lists/catid-default
607
		 * @see client/html/catalog/lists/domains
608
		 * @see client/html/catalog/lists/size
609
		 * @see client/html/catalog/lists/sort
610
		 * @see client/html/catalog/lists/pages
611
		 */
612
		$level = $config->get( 'client/html/catalog/lists/levels', \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
613
614
615
		/** client/html/catalog/lists/catid-default
616
		 * The default category ID used if none is given as parameter
617
		 *
618
		 * If users view a product list page without a category ID in the
619
		 * parameter list, the first found products are displayed with a
620
		 * random order. You can circumvent this by configuring a default
621
		 * category ID that should be used in this case (the ID of the root
622
		 * category is best for this). In most cases you can set this value
623
		 * via the administration interface of the shop application.
624
		 *
625
		 * @param string Category ID
626
		 * @since 2014.03
627
		 * @category User
628
		 * @category Developer
629
		 * @see client/html/catalog/lists/sort
630
		 * @see client/html/catalog/lists/size
631
		 * @see client/html/catalog/lists/domains
632
		 * @see client/html/catalog/lists/levels
633
		 * @see client/html/catalog/detail/prodid-default
634
		 */
635
		$catids = $view->param( 'f_catid', $config->get( 'client/html/catalog/lists/catid-default' ) );
636
		$catids = $catids != null && is_scalar( $catids ) ? explode( ',', $catids ) : $catids; // workaround for TYPO3
637
638
		/** client/html/catalog/lists/sort
639
		 * Default sorting of product list if no other sorting is given by parameter
640
		 *
641
		 * Configures the standard sorting of products in list views. This sorting is used
642
		 * as long as it's not overwritten by an URL parameter. Except "relevance", all
643
		 * other sort codes can be prefixed by a "-" (minus) sign to sort the products in
644
		 * a descending order. By default, the sorting is ascending.
645
		 *
646
		 * @param string Sort code "relevance", "name", "-name", "price", "-price", "ctime" or "-ctime"
647
		 * @since 2018.07
648
		 * @category User
649
		 * @category Developer
650
		 * @see client/html/catalog/lists/catid-default
651
		 * @see client/html/catalog/lists/domains
652
		 * @see client/html/catalog/lists/levels
653
		 * @see client/html/catalog/lists/size
654
		 */
655
		$sort = $view->param( 'f_sort', $config->get( 'client/html/catalog/lists/sort', 'relevance' ) );
656
		$size = min( max( $view->param( 'l_size', $size ), 1 ), 100 );
657
		$page = min( max( $view->param( 'l_page', 1 ), 1 ), $pages );
658
659
		$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
660
			->sort( $sort ) // prioritize user sorting over the sorting through relevance and category
661
			->text( $view->param( 'f_search' ) )
662
			->price( $view->param( 'f_price' ) )
663
			->category( $catids, 'default', $level )
664
			->supplier( $view->param( 'f_supid', [] ) )
665
			->allOf( $view->param( 'f_attrid', [] ) )
666
			->oneOf( $view->param( 'f_optid', [] ) )
667
			->oneOf( $view->param( 'f_oneid', [] ) )
668
			->slice( ( $page - 1 ) * $size, $size )
669
			->uses( $domains )
670
			->search( $total );
671
672
		if( $catids != null )
673
		{
674
			$controller = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->uses( $domains );
675
			$listCatPath = $controller->getPath( is_array( $catids ) ? reset( $catids ) : $catids );
676
677
			if( ( $categoryItem = $listCatPath->last() ) !== null ) {
678
				$view->listCurrentCatItem = $categoryItem;
679
			}
680
681
			$view->listCatPath = $listCatPath;
682
			$this->addMetaItems( $listCatPath, $expire, $tags );
683
		}
684
685
686
		// Delete cache when products are added or deleted even when in "tag-all" mode
687
		$this->addMetaItems( $products, $expire, $tags, ['product'] );
688
689
690
		$view->listProductItems = $products;
691
		$view->listProductSort = $sort;
692
		$view->listProductTotal = $total;
693
694
		$view->listPageSize = $size;
695
		$view->listPageCurr = $page;
696
		$view->listPagePrev = ( $page > 1 ? $page - 1 : 1 );
697
		$view->listPageLast = ( $total != 0 ? ceil( $total / $size ) : 1 );
0 ignored issues
show
introduced by
The condition $total != 0 is always false.
Loading history...
698
		$view->listPageNext = ( $page < $view->listPageLast ? $page + 1 : $view->listPageLast );
699
700
		$view->listParams = $this->getClientParams( map( $view->param() )->toArray() );
701
702
		return parent::addData( $view, $tags, $expire );
703
	}
704
}
705