Passed
Push — master ( a974de...9ab29e )
by Aimeos
18:28 queued 20s
created

Standard   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 295
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubClientNames() 0 3 1
A getBody() 0 34 2
A getSubClient() 0 77 1
A getProductIdsFromBasket() 0 14 3
A addData() 0 82 6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Basket\Related\Bought;
12
13
14
/**
15
 * Default implementation of related basket bought 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/bought/subparts
25
	 * List of HTML sub-clients rendered within the basket related bought section
26
	 *
27
	 * The output of the frontend is composed of the code generated by the HTML
28
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
29
	 * that are responsible for rendering certain sub-parts of the output. The
30
	 * sub-clients can contain HTML clients themselves and therefore a
31
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
32
	 * the output that is placed inside the container of its parent.
33
	 *
34
	 * At first, always the HTML code generated by the parent is printed, then
35
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
36
	 * determines the order of the output of these sub-clients inside the parent
37
	 * container. If the configured list of clients is
38
	 *
39
	 *  array( "subclient1", "subclient2" )
40
	 *
41
	 * you can easily change the order of the output by reordering the subparts:
42
	 *
43
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
44
	 *
45
	 * You can also remove one or more parts if they shouldn't be rendered:
46
	 *
47
	 *  client/html/<clients>/subparts = array( "subclient1" )
48
	 *
49
	 * As the clients only generates structural HTML, the layout defined via CSS
50
	 * should support adding, removing or reordering content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2014.03
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/basket/related/bought/subparts';
58
	private $subPartNames = [];
59
60
61
	/**
62
	 * Returns the HTML code for insertion into the body.
63
	 *
64
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
65
	 * @return string HTML code
66
	 */
67
	public function getBody( string $uid = '' ) : string
68
	{
69
		$view = $this->getView();
70
71
		$html = '';
72
		foreach( $this->getSubClients() as $subclient ) {
73
			$html .= $subclient->setView( $view )->getBody( $uid );
74
		}
75
		$view->boughtBody = $html;
76
77
		/** client/html/basket/related/bought/template-body
78
		 * Relative path to the HTML body template of the basket related bought client.
79
		 *
80
		 * The template file contains the HTML code and processing instructions
81
		 * to generate the result shown in the body of the frontend. The
82
		 * configuration string is the path to the template file relative
83
		 * to the templates directory (usually in client/html/templates).
84
		 *
85
		 * You can overwrite the template file configuration in extensions and
86
		 * provide alternative templates. These alternative templates should be
87
		 * named like the default one but with the string "standard" replaced by
88
		 * an unique name. You may use the name of your project for this. If
89
		 * you've implemented an alternative client class as well, "standard"
90
		 * should be replaced by the name of the new class.
91
		 *
92
		 * @param string Relative path to the template creating code for the HTML page body
93
		 * @since 2014.03
94
		 * @category Developer
95
		 * @see client/html/basket/related/bought/template-header
96
		 */
97
		$tplconf = 'client/html/basket/related/bought/template-body';
98
		$default = 'basket/related/bought-body-standard';
99
100
		return $view->render( $view->config( $tplconf, $default ) );
101
	}
102
103
104
	/**
105
	 * Returns the sub-client given by its name.
106
	 *
107
	 * @param string $type Name of the client type
108
	 * @param string|null $name Name of the sub-client (Default if null)
109
	 * @return \Aimeos\Client\Html\Iface Sub-client object
110
	 */
111
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
112
	{
113
		/** client/html/basket/related/bought/decorators/excludes
114
		 * Excludes decorators added by the "common" option from the basket related bought html client
115
		 *
116
		 * Decorators extend the functionality of a class by adding new aspects
117
		 * (e.g. log what is currently done), executing the methods of the underlying
118
		 * class only in certain conditions (e.g. only for logged in users) or
119
		 * modify what is returned to the caller.
120
		 *
121
		 * This option allows you to remove a decorator added via
122
		 * "client/html/common/decorators/default" before they are wrapped
123
		 * around the html client.
124
		 *
125
		 *  client/html/basket/related/bought/decorators/excludes = array( 'decorator1' )
126
		 *
127
		 * This would remove the decorator named "decorator1" from the list of
128
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
129
		 * "client/html/common/decorators/default" to the html client.
130
		 *
131
		 * @param array List of decorator names
132
		 * @since 2014.05
133
		 * @category Developer
134
		 * @see client/html/common/decorators/default
135
		 * @see client/html/basket/related/bought/decorators/global
136
		 * @see client/html/basket/related/bought/decorators/local
137
		 */
138
139
		/** client/html/basket/related/bought/decorators/global
140
		 * Adds a list of globally available decorators only to the basket related bought html client
141
		 *
142
		 * Decorators extend the functionality of a class by adding new aspects
143
		 * (e.g. log what is currently done), executing the methods of the underlying
144
		 * class only in certain conditions (e.g. only for logged in users) or
145
		 * modify what is returned to the caller.
146
		 *
147
		 * This option allows you to wrap global decorators
148
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
149
		 *
150
		 *  client/html/basket/related/bought/decorators/global = array( 'decorator1' )
151
		 *
152
		 * This would add the decorator named "decorator1" defined by
153
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
154
		 *
155
		 * @param array List of decorator names
156
		 * @since 2014.05
157
		 * @category Developer
158
		 * @see client/html/common/decorators/default
159
		 * @see client/html/basket/related/bought/decorators/excludes
160
		 * @see client/html/basket/related/bought/decorators/local
161
		 */
162
163
		/** client/html/basket/related/bought/decorators/local
164
		 * Adds a list of local decorators only to the basket related bought html client
165
		 *
166
		 * Decorators extend the functionality of a class by adding new aspects
167
		 * (e.g. log what is currently done), executing the methods of the underlying
168
		 * class only in certain conditions (e.g. only for logged in users) or
169
		 * modify what is returned to the caller.
170
		 *
171
		 * This option allows you to wrap local decorators
172
		 * ("\Aimeos\Client\Html\Basket\Decorator\*") around the html client.
173
		 *
174
		 *  client/html/basket/related/bought/decorators/local = array( 'decorator2' )
175
		 *
176
		 * This would add the decorator named "decorator2" defined by
177
		 * "\Aimeos\Client\Html\Basket\Decorator\Decorator2" only to the html client.
178
		 *
179
		 * @param array List of decorator names
180
		 * @since 2014.05
181
		 * @category Developer
182
		 * @see client/html/common/decorators/default
183
		 * @see client/html/basket/related/bought/decorators/excludes
184
		 * @see client/html/basket/related/bought/decorators/global
185
		 */
186
187
		return $this->createSubClient( 'basket/related/bought/' . $type, $name );
188
	}
189
190
191
	/**
192
	 * Returns the list of sub-client names configured for the client.
193
	 *
194
	 * @return array List of HTML client names
195
	 */
196
	protected function getSubClientNames() : array
197
	{
198
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
199
	}
200
201
202
	/**
203
	 * Sets the necessary parameter values in the view.
204
	 *
205
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
206
	 * @param array &$tags Result array for the list of tags that are associated to the output
207
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
208
	 * @return \Aimeos\MW\View\Iface Modified view object
209
	 */
210
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
211
	{
212
		if( isset( $view->relatedBasket ) )
213
		{
214
			$context = $this->getContext();
215
			$config = $context->getConfig();
216
217
			$cntl = \Aimeos\Controller\Frontend::create( $context, 'product' );
218
219
			/** client/html/basket/related/bought/limit
220
			 * Number of items in the list of bought together products
221
			 *
222
			 * This option limits the number of suggested products in the
223
			 * list of bought together products. The suggested items are
224
			 * calculated using the products that are in the current basket
225
			 * of the customer.
226
			 *
227
			 * Note: You need to start the job controller for calculating
228
			 * the bought together products regularly to get up to date
229
			 * product suggestions.
230
			 *
231
			 * @param integer Number of products
232
			 * @since 2014.09
233
			 */
234
			$size = $config->get( 'client/html/basket/related/bought/limit', 6 );
235
236
			/** client/html/basket/related/bought/domains
237
			 * The list of domain names whose items should be available in the template for the products
238
			 *
239
			 * The templates rendering product details usually add the images,
240
			 * prices and texts, etc. associated to the product
241
			 * item. If you want to display additional or less content, you can
242
			 * configure your own list of domains (attribute, media, price, product,
243
			 * text, etc. are domains) whose items are fetched from the storage.
244
			 * Please keep in mind that the more domains you add to the configuration,
245
			 * the more time is required for fetching the content!
246
			 *
247
			 * @param array List of domain names
248
			 * @since 2014.09
249
			 * @category Developer
250
			 */
251
			$domains = $config->get( 'client/html/basket/related/bought/domains', ['text', 'price', 'media'] );
252
			$domains['product'] = ['bought-together'];
253
254
			/** client/html/basket/related/basket-add
255
			 * Display the "add to basket" button for each product item
256
			 *
257
			 * Enables the button for adding products to the basket for the related products
258
			 * in the basket. This works for all type of products, even for selection products
259
			 * with product variants and product bundles. By default, also optional attributes
260
			 * are displayed if they have been associated to a product.
261
			 *
262
			 * @param boolean True to display the button, false to hide it
263
			 * @since 2020.10
264
			 * @see client/html/catalog/home/basket-add
265
			 * @see client/html/catalog/lists/basket-add
266
			 * @see client/html/catalog/detail/basket-add
267
			 * @see client/html/catalog/product/basket-add
268
			 */
269
			if( $view->config( 'client/html/basket/related/basket-add', false ) ) {
270
				$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute'] );
271
			}
272
273
			$items = map();
274
			$prodIds = $this->getProductIdsFromBasket( $view->relatedBasket );
275
276
			foreach( $cntl->uses( $domains )->product( $prodIds )->search() as $prodItem )
277
			{
278
				foreach( $prodItem->getListItems( 'product', 'bought-together' ) as $listItem )
279
				{
280
					if( ( $refItem = $listItem->getRefItem() ) !== null ) {
281
						$items[$refItem->getId()] = $refItem->set( 'position', $listItem->getPosition() );
282
					}
283
				}
284
			}
285
286
			$view->boughtItems = $items->uasort( function( $a, $b ) {
287
				return $a->get( 'position', 0 ) <=> $b->get( 'position', 0 );
288
			} )->slice( 0, $size );
289
		}
290
291
		return parent::addData( $view, $tags, $expire );
292
	}
293
294
295
	/**
296
	 * Returns the IDs of the products in the current basket.
297
	 *
298
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
299
	 * @return string[] List of product IDs
300
	 */
301
	protected function getProductIdsFromBasket( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : array
302
	{
303
		$list = [];
304
305
		foreach( $basket->getProducts() as $orderProduct )
306
		{
307
			$list[$orderProduct->getProductId()] = true;
308
309
			foreach( $orderProduct->getProducts() as $subProduct ) {
310
				$list[$subProduct->getProductId()] = true;
311
			}
312
		}
313
314
		return array_keys( $list );
315
	}
316
}
317