Passed
Push — master ( 798725...2965aa )
by Aimeos
03:30
created

Standard::addData()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 235
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
cc 10
eloc 44
c 9
b 0
f 0
nc 192
nop 3
dl 0
loc 235
rs 6.9

How to fix   Long Method    Complexity   

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