Standard::size()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
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
	/** client/html/basket/related/name
25
	 * Class name of the used basket related client implementation
26
	 *
27
	 * Each default HTML client can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the client factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Client\Html\Basket\Related\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Client\Html\Basket\Related\Mybasket
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  client/html/basket/related/name = Mybasket
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyBasket"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 */
56
57
58
	/**
59
	 * Sets the necessary parameter values in the view.
60
	 *
61
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
62
	 * @param array &$tags Result array for the list of tags that are associated to the output
63
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
64
	 * @return \Aimeos\Base\View\Iface Modified view object
65
	 */
66
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], ?string &$expire = null ) : \Aimeos\Base\View\Iface
67
	{
68
		$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'product' );
69
70
		$view->boughtItems = $cntl->uses( $this->domains() )
71
			->product( $this->productIds()->all() )
72
			->search()
73
			->getListItems( 'product', 'bought-together' )
74
			->flat( 1 )
75
			->usort( function( $a, $b ) {
76
				return $a->getPosition() <=> $b->getPosition();
77
			} )
78
			->getRefItem()
79
			->filter()
80
			->slice( 0, $this->size() )
81
			->col( null, 'product.id' );
82
83
		return parent::data( $view, $tags, $expire );
84
	}
85
86
87
	/**
88
	 * Returns the data domains fetched along with the products
89
	 *
90
	 * @return array List of domain names
91
	 */
92
	protected function domains() : array
93
	{
94
		$config = $this->context()->config();
95
96
		/** client/html/basket/related/bought/domains
97
		 * The list of domain names whose items should be available in the template for the products
98
		 *
99
		 * The templates rendering product details usually add the images,
100
		 * prices and texts, etc. associated to the product
101
		 * item. If you want to display additional or less content, you can
102
		 * configure your own list of domains (attribute, media, price, product,
103
		 * text, etc. are domains) whose items are fetched from the storage.
104
		 * Please keep in mind that the more domains you add to the configuration,
105
		 * the more time is required for fetching the content!
106
		 *
107
		 * @param array List of domain names
108
		 * @since 2014.09
109
		 */
110
		$domains = ['catalog', 'media', 'media/property', 'price', 'supplier', 'text'];
111
		$domains = $config->get( 'client/html/basket/related/bought/domains', $domains );
112
		$domains['product'] = ['bought-together'];
113
114
		/** client/html/basket/related/basket-add
115
		 * Display the "add to basket" button for each product item
116
		 *
117
		 * Enables the button for adding products to the basket for the related products
118
		 * in the basket. This works for all type of products, even for selection products
119
		 * with product variants and product bundles. By default, also optional attributes
120
		 * are displayed if they have been associated to a product.
121
		 *
122
		 * @param boolean True to display the button, false to hide it
123
		 * @since 2020.10
124
		 * @see client/html/catalog/home/basket-add
125
		 * @see client/html/catalog/lists/basket-add
126
		 * @see client/html/catalog/detail/basket-add
127
		 * @see client/html/catalog/product/basket-add
128
		 */
129
		if( $config->get( 'client/html/basket/related/basket-add', false ) ) {
130
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute' => ['variant', 'custom', 'config']] );
131
		}
132
133
		return $domains;
134
	}
135
136
137
	/**
138
	 * Returns the IDs of the products in the basket
139
	 *
140
	 * @return \Aimeos\Map List of product IDs
141
	 */
142
	protected function productIds() : \Aimeos\Map
143
	{
144
		$basket = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' )->get();
145
146
		return $basket->getProducts()
147
			->concat( $basket->getProducts()->getProducts() )
148
			->col( 'order.product.parentproductid' )
149
			->unique();
150
	}
151
152
153
	/**
154
	 * Returns the number of products shown in the list
155
	 *
156
	 * @return int Number of products
157
	 */
158
	protected function size() : int
159
	{
160
		/** client/html/basket/related/bought/limit
161
		 * Number of items in the list of bought together products
162
		 *
163
		 * This option limits the number of suggested products in the
164
		 * list of bought together products. The suggested items are
165
		 * calculated using the products that are in the current basket
166
		 * of the customer.
167
		 *
168
		 * Note: You need to start the job controller for calculating
169
		 * the bought together products regularly to get up to date
170
		 * product suggestions.
171
		 *
172
		 * @param integer Number of products
173
		 * @since 2014.09
174
		 */
175
		return $this->context()->config()->get( 'client/html/basket/related/bought/limit', 6 );
176
	}
177
178
179
	/** client/html/basket/related/template-body
180
	 * Relative path to the HTML body template of the basket related client.
181
	 *
182
	 * The template file contains the HTML code and processing instructions
183
	 * to generate the result shown in the body of the frontend. The
184
	 * configuration string is the path to the template file relative
185
	 * to the templates directory (usually in templates/client/html).
186
	 *
187
	 * You can overwrite the template file configuration in extensions and
188
	 * provide alternative templates. These alternative templates should be
189
	 * named like the default one but suffixed by
190
	 * an unique name. You may use the name of your project for this. If
191
	 * you've implemented an alternative client class as well, it
192
	 * should be suffixed by the name of the new class.
193
	 *
194
	 * @param string Relative path to the template creating code for the HTML page body
195
	 * @since 2014.03
196
	 * @see client/html/basket/related/template-header
197
	 */
198
199
	/** client/html/basket/related/template-header
200
	 * Relative path to the HTML header template of the basket related client.
201
	 *
202
	 * The template file contains the HTML code and processing instructions
203
	 * to generate the HTML code that is inserted into the HTML page header
204
	 * of the rendered page in the frontend. The configuration string is the
205
	 * path to the template file relative to the templates directory (usually
206
	 * in templates/client/html).
207
	 *
208
	 * You can overwrite the template file configuration in extensions and
209
	 * provide alternative templates. These alternative templates should be
210
	 * named like the default one but suffixed by
211
	 * an unique name. You may use the name of your project for this. If
212
	 * you've implemented an alternative client class as well, it
213
	 * should be suffixed by the name of the new class.
214
	 *
215
	 * @param string Relative path to the template creating code for the HTML page head
216
	 * @since 2014.03
217
	 * @see client/html/basket/related/template-body
218
	 */
219
220
	/** client/html/basket/related/decorators/excludes
221
	 * Excludes decorators added by the "common" option from the basket related html client
222
	 *
223
	 * Decorators extend the functionality of a class by adding new aspects
224
	 * (e.g. log what is currently done), executing the methods of the underlying
225
	 * class only in certain conditions (e.g. only for logged in users) or
226
	 * modify what is returned to the caller.
227
	 *
228
	 * This option allows you to remove a decorator added via
229
	 * "client/html/common/decorators/default" before they are wrapped
230
	 * around the html client.
231
	 *
232
	 *  client/html/basket/related/decorators/excludes = array( 'decorator1' )
233
	 *
234
	 * This would remove the decorator named "decorator1" from the list of
235
	 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
236
	 * "client/html/common/decorators/default" to the html client.
237
	 *
238
	 * @param array List of decorator names
239
	 * @since 2014.05
240
	 * @see client/html/common/decorators/default
241
	 * @see client/html/basket/related/decorators/global
242
	 * @see client/html/basket/related/decorators/local
243
	 */
244
245
	/** client/html/basket/related/decorators/global
246
	 * Adds a list of globally available decorators only to the basket related html client
247
	 *
248
	 * Decorators extend the functionality of a class by adding new aspects
249
	 * (e.g. log what is currently done), executing the methods of the underlying
250
	 * class only in certain conditions (e.g. only for logged in users) or
251
	 * modify what is returned to the caller.
252
	 *
253
	 * This option allows you to wrap global decorators
254
	 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
255
	 *
256
	 *  client/html/basket/related/decorators/global = array( 'decorator1' )
257
	 *
258
	 * This would add the decorator named "decorator1" defined by
259
	 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
260
	 *
261
	 * @param array List of decorator names
262
	 * @since 2014.05
263
	 * @see client/html/common/decorators/default
264
	 * @see client/html/basket/related/decorators/excludes
265
	 * @see client/html/basket/related/decorators/local
266
	 */
267
268
	/** client/html/basket/related/decorators/local
269
	 * Adds a list of local decorators only to the basket related html client
270
	 *
271
	 * Decorators extend the functionality of a class by adding new aspects
272
	 * (e.g. log what is currently done), executing the methods of the underlying
273
	 * class only in certain conditions (e.g. only for logged in users) or
274
	 * modify what is returned to the caller.
275
	 *
276
	 * This option allows you to wrap local decorators
277
	 * ("\Aimeos\Client\Html\Basket\Decorator\*") around the html client.
278
	 *
279
	 *  client/html/basket/related/decorators/local = array( 'decorator2' )
280
	 *
281
	 * This would add the decorator named "decorator2" defined by
282
	 * "\Aimeos\Client\Html\Basket\Decorator\Decorator2" only to the html client.
283
	 *
284
	 * @param array List of decorator names
285
	 * @since 2014.05
286
	 * @see client/html/common/decorators/default
287
	 * @see client/html/basket/related/decorators/excludes
288
	 * @see client/html/basket/related/decorators/global
289
	 */
290
}
291