Passed
Push — master ( c66389...2cab20 )
by Aimeos
02:51
created

Standard   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 122
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A domains() 0 42 2
A size() 0 18 1
A productIds() 0 8 1
A data() 0 18 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Basket\Related;
12
13
14
/**
15
 * Default implementation of related basket HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Basket\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/**
25
	 * Sets the necessary parameter values in the view.
26
	 *
27
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
28
	 * @param array &$tags Result array for the list of tags that are associated to the output
29
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
30
	 * @return \Aimeos\Base\View\Iface Modified view object
31
	 */
32
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
33
	{
34
		$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'product' );
35
36
		$view->boughtItems = $cntl->uses( $this->domains() )
37
			->product( $this->productIds()->all() )
38
			->search()
39
			->getListItems( 'product', 'bought-together' )
40
			->flat( 1 )
41
			->usort( function( $a, $b ) {
42
				return $a->getPosition() <=> $b->getPosition();
43
			} )
44
			->getRefItem()
45
			->filter()
46
			->slice( 0, $this->size() )
47
			->col( null, 'product.id' );
48
49
		return parent::data( $view, $tags, $expire );
50
	}
51
52
53
	/**
54
	 * Returns the data domains fetched along with the products
55
	 *
56
	 * @return array List of domain names
57
	 */
58
	protected function domains() : array
59
	{
60
		$config = $this->context()->config();
61
62
		/** client/html/basket/related/bought/domains
63
		 * The list of domain names whose items should be available in the template for the products
64
		 *
65
		 * The templates rendering product details usually add the images,
66
		 * prices and texts, etc. associated to the product
67
		 * item. If you want to display additional or less content, you can
68
		 * configure your own list of domains (attribute, media, price, product,
69
		 * text, etc. are domains) whose items are fetched from the storage.
70
		 * Please keep in mind that the more domains you add to the configuration,
71
		 * the more time is required for fetching the content!
72
		 *
73
		 * @param array List of domain names
74
		 * @since 2014.09
75
		 */
76
		$domains = ['catalog', 'media', 'media/property', 'price', 'supplier', 'text'];
77
		$domains = $config->get( 'client/html/basket/related/bought/domains', $domains );
78
		$domains['product'] = ['bought-together'];
79
80
		/** client/html/basket/related/basket-add
81
		 * Display the "add to basket" button for each product item
82
		 *
83
		 * Enables the button for adding products to the basket for the related products
84
		 * in the basket. This works for all type of products, even for selection products
85
		 * with product variants and product bundles. By default, also optional attributes
86
		 * are displayed if they have been associated to a product.
87
		 *
88
		 * @param boolean True to display the button, false to hide it
89
		 * @since 2020.10
90
		 * @see client/html/catalog/home/basket-add
91
		 * @see client/html/catalog/lists/basket-add
92
		 * @see client/html/catalog/detail/basket-add
93
		 * @see client/html/catalog/product/basket-add
94
		 */
95
		if( $config->get( 'client/html/basket/related/basket-add', false ) ) {
96
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute' => ['variant', 'custom', 'config']] );
97
		}
98
99
		return $domains;
100
	}
101
102
103
	/**
104
	 * Returns the IDs of the products in the basket
105
	 *
106
	 * @return \Aimeos\Map List of product IDs
107
	 */
108
	protected function productIds() : \Aimeos\Map
109
	{
110
		$basket = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' )->get();
111
112
		return $basket->getProducts()
113
			->concat( $basket->getProducts()->getProducts() )
114
			->col( 'order.base.product.parentproductid' )
115
			->unique();
116
	}
117
118
119
	/**
120
	 * Returns the number of products shown in the list
121
	 *
122
	 * @return int Number of products
123
	 */
124
	protected function size() : int
125
	{
126
		/** client/html/basket/related/bought/limit
127
		 * Number of items in the list of bought together products
128
		 *
129
		 * This option limits the number of suggested products in the
130
		 * list of bought together products. The suggested items are
131
		 * calculated using the products that are in the current basket
132
		 * of the customer.
133
		 *
134
		 * Note: You need to start the job controller for calculating
135
		 * the bought together products regularly to get up to date
136
		 * product suggestions.
137
		 *
138
		 * @param integer Number of products
139
		 * @since 2014.09
140
		 */
141
		return $this->context()->config()->get( 'client/html/basket/related/bought/limit', 6 );
142
	}
143
144
145
	/** client/html/basket/related/template-body
146
	 * Relative path to the HTML body template of the basket related client.
147
	 *
148
	 * The template file contains the HTML code and processing instructions
149
	 * to generate the result shown in the body of the frontend. The
150
	 * configuration string is the path to the template file relative
151
	 * to the templates directory (usually in client/html/templates).
152
	 *
153
	 * You can overwrite the template file configuration in extensions and
154
	 * provide alternative templates. These alternative templates should be
155
	 * named like the default one but suffixed by
156
	 * an unique name. You may use the name of your project for this. If
157
	 * you've implemented an alternative client class as well, it
158
	 * should be suffixed by the name of the new class.
159
	 *
160
	 * @param string Relative path to the template creating code for the HTML page body
161
	 * @since 2014.03
162
	 * @see client/html/basket/related/template-header
163
	 */
164
165
	/** client/html/basket/related/template-header
166
	 * Relative path to the HTML header template of the basket related client.
167
	 *
168
	 * The template file contains the HTML code and processing instructions
169
	 * to generate the HTML code that is inserted into the HTML page header
170
	 * of the rendered page in the frontend. The configuration string is the
171
	 * path to the template file relative to the templates directory (usually
172
	 * in client/html/templates).
173
	 *
174
	 * You can overwrite the template file configuration in extensions and
175
	 * provide alternative templates. These alternative templates should be
176
	 * named like the default one but suffixed by
177
	 * an unique name. You may use the name of your project for this. If
178
	 * you've implemented an alternative client class as well, it
179
	 * should be suffixed by the name of the new class.
180
	 *
181
	 * @param string Relative path to the template creating code for the HTML page head
182
	 * @since 2014.03
183
	 * @see client/html/basket/related/template-body
184
	 */
185
}
186