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

Standard::domains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 35
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2022
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Catalog\Product;
12
13
/**
14
 * Implementation of catalog product section HTML clients for a configurable list of products.
15
 *
16
 * @package Client
17
 * @subpackage Html
18
 */
19
class Standard
20
	extends \Aimeos\Client\Html\Catalog\Base
21
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
22
{
23
	private $tags = [];
24
	private $expire;
25
	private $view;
26
27
28
	/**
29
	 * Returns the HTML code for insertion into the body.
30
	 *
31
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
32
	 * @return string HTML code
33
	 */
34
	public function body( string $uid = '' ) : string
35
	{
36
		/** client/html/catalog/product/cache
37
		 * Enables or disables caching only for the catalog product component
38
		 *
39
		 * Disable caching for components can be useful if you would have too much
40
		 * entries to cache or if the component contains non-cacheable parts that
41
		 * can't be replaced using the modify() method.
42
		 *
43
		 * @param boolean True to enable caching, false to disable
44
		 * @see client/html/catalog/detail/cache
45
		 * @see client/html/catalog/filter/cache
46
		 * @see client/html/catalog/stage/cache
47
		 * @see client/html/catalog/list/cache
48
		 */
49
50
		/** client/html/catalog/product
51
		 * All parameters defined for the catalog product component and its subparts
52
		 *
53
		 * Please refer to the single settings for details.
54
		 *
55
		 * @param array Associative list of name/value settings
56
		 * @see client/html/catalog#product
57
		 */
58
		$confkey = 'client/html/catalog/product';
59
60
		if( $html = $this->cached( 'body', $uid, [], $confkey ) ) {
61
			return $this->modify( $html, $uid );
62
		}
63
64
		$config = $this->context()->config();
65
		$template = $config->get( 'client/html/catalog/product/template-body', 'catalog/product/body' );
66
67
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
68
		$html = $view->render( $template );
69
70
		return $this->cache( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
71
	}
72
73
74
	/**
75
	 * Returns the HTML string for insertion into the header.
76
	 *
77
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
78
	 * @return string|null String including HTML tags for the header on error
79
	 */
80
	public function header( string $uid = '' ) : ?string
81
	{
82
		$confkey = 'client/html/catalog/product';
83
84
		if( $html = $this->cached( 'header', $uid, [], $confkey ) ) {
85
			return $this->modify( $html, $uid );
86
		}
87
88
		$config = $this->context()->config();
89
		$template = $config->get( 'client/html/catalog/product/template-header', 'catalog/product/header' );
90
91
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
92
		$html = $view->render( $template );
93
94
		return $this->cache( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
95
	}
96
97
98
	/**
99
	 * Modifies the cached content to replace content based on sessions or cookies.
100
	 *
101
	 * @param string $content Cached content
102
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
103
	 * @return string Modified content
104
	 */
105
	public function modify( string $content, string $uid ) : string
106
	{
107
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.lists.items.csrf' );
108
	}
109
110
111
	/**
112
	 * Sets the necessary parameter values in the view.
113
	 *
114
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
115
	 * @param array &$tags Result array for the list of tags that are associated to the output
116
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
117
	 * @return \Aimeos\Base\View\Iface Modified view object
118
	 */
119
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
120
	{
121
		$productItems = map();
0 ignored issues
show
Unused Code introduced by
The assignment to $productItems is dead and can be removed.
Loading history...
122
		$context = $this->context();
123
		$config = $context->config();
124
125
		/** client/html/catalog/product/product-codes
126
		 * List of codes of products to load for the current list.
127
		 * Should be set dynamically through some integration plugin,
128
		 * to allow a list of products with configurable products.
129
		 *
130
		 * @param string List of codes of products to load for the current list
131
		 * @since 2019.06
132
		 */
133
		$productCodes = $config->get( 'client/html/catalog/product/product-codes', [] );
134
135
		$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
136
			->compare( '==', 'product.code', $productCodes )
137
			->slice( 0, count( $productCodes ) )
138
			->uses( $this->domains() )
139
			->search();
140
141
		// Sort products by the order given in the configuration "client/html/catalog/product/product-codes".
142
		$productCodesOrder = array_flip( $productCodes );
143
		$products->usort( function( $a, $b ) use ( $productCodesOrder ) {
144
			return $productCodesOrder[$a->getCode()] - $productCodesOrder[$b->getCode()];
145
		} );
146
147
		$productItems = $products->copy();
148
149
		if( $config->get( 'client/html/catalog/product/basket-add', false ) )
150
		{
151
			foreach( $products as $product )
152
			{
153
				if( $product->getType() === 'select' ) {
154
					$productItems->union( $product->getRefItems( 'product', 'default', 'default' ) );
155
				}
156
			}
157
		}
158
159
		// Delete cache when products are added or deleted even when in "tag-all" mode
160
		$this->addMetaItems( $productItems, $expire, $tags, ['product'] );
161
162
		$view->productItems = $products;
163
		$view->productTotal = count( $products );
164
		$view->itemsStockUrl = $this->stockUrl( $productItems );
165
166
		return parent::data( $view, $tags, $expire );
167
	}
168
169
170
	/**
171
	 * Returns the data domains fetched along with the products
172
	 *
173
	 * @return array List of domain names
174
	 */
175
	protected function domains() : array
176
	{
177
		$config = $this->context()->config();
178
179
		/** client/html/catalog/product/domains
180
		 * A list of domain names whose items should be available in the catalog product view template
181
		 *
182
		 * The templates rendering product lists usually add the images, prices
183
		 * and texts associated to each product item. If you want to display additional
184
		 * content like the product attributes, you can configure your own list of
185
		 * domains (attribute, media, price, product, text, etc. are domains)
186
		 * whose items are fetched from the storage. Please keep in mind that
187
		 * the more domains you add to the configuration, the more time is required
188
		 * for fetching the content!
189
		 *
190
		 * This configuration option overwrites the "client/html/catalog/domains"
191
		 * option that allows to configure the domain names of the items fetched
192
		 * for all catalog related data.
193
		 *
194
		 * @param array List of domain names
195
		 * @since 2019.06
196
		 * @see client/html/catalog/domains
197
		 * @see client/html/catalog/detail/domains
198
		 * @see client/html/catalog/stage/domains
199
		 * @see client/html/catalog/lists/domains
200
		 */
201
		$domains = ['catalog', 'media', 'media/property', 'price', 'supplier', 'text'];
202
		$domains = $config->get( 'client/html/catalog/domains', $domains );
203
		$domains = $config->get( 'client/html/catalog/product/domains', $domains );
204
205
		if( $config->get( 'client/html/catalog/product/basket-add', false ) ) {
206
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute' => ['variant', 'custom', 'config']] );
207
		}
208
209
		return $domains;
210
	}
211
212
213
	/**
214
	 * Returns the list of stock URLs for the given products
215
	 *
216
	 * @param \Aimeos\Map $products List of products
217
	 * @return \Aimeos\Map List of stock URLs
218
	 */
219
	protected function stockUrl( \Aimeos\Map $products ) : \Aimeos\Map
220
	{
221
		$config = $this->context()->config();
222
223
		/** client/html/catalog/product/stock/enable
224
		 * Enables or disables displaying product stock levels in product list views
225
		 *
226
		 * This configuration option allows shop owners to display product
227
		 * stock levels for each product in list views or to disable
228
		 * fetching product stock information.
229
		 *
230
		 * The stock information is fetched via AJAX and inserted via Javascript.
231
		 * This allows to cache product items by leaving out such highly
232
		 * dynamic content like stock levels which changes with each order.
233
		 *
234
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
235
		 * @since 2019.06
236
		 * @see client/html/catalog/detail/stock/enable
237
		 * @see client/html/catalog/stock/url/target
238
		 * @see client/html/catalog/stock/url/controller
239
		 * @see client/html/catalog/stock/url/action
240
		 * @see client/html/catalog/stock/url/config
241
		 */
242
		$enabled = $config->get( 'client/html/catalog/product/stock/enable', true );
243
244
		if( !$enabled || $products->isEmpty() ) {
245
			return map();
246
		}
247
248
		return $this->getStockUrl( $this->view(), $products );
249
	}
250
251
252
	/** client/html/catalog/product/template-body
253
	 * Relative path to the HTML body template of the catalog product client.
254
	 *
255
	 * The template file contains the HTML code and processing instructions
256
	 * to generate the result shown in the body of the frontend. The
257
	 * configuration string is the path to the template file relative
258
	 * to the templates directory (usually in client/html/templates).
259
	 *
260
	 * You can overwrite the template file configuration in extensions and
261
	 * provide alternative templates. These alternative templates should be
262
	 * named like the default one but suffixed by
263
	 * an unique name. You may use the name of your project for this. If
264
	 * you've implemented an alternative client class as well, it
265
	 * should be suffixed by the name of the new class.
266
	 *
267
	 * @param string Relative path to the template creating code for the HTML page body
268
	 * @since 2019.06
269
	 * @see client/html/catalog/product/template-header
270
	 */
271
272
	/** client/html/catalog/product/template-header
273
	 * Relative path to the HTML header template of the catalog product client.
274
	 *
275
	 * The template file contains the HTML code and processing instructions
276
	 * to generate the HTML code that is inserted into the HTML page header
277
	 * of the rendered page in the frontend. The configuration string is the
278
	 * path to the template file relative to the templates directory (usually
279
	 * in client/html/templates).
280
	 *
281
	 * You can overwrite the template file configuration in extensions and
282
	 * provide alternative templates. These alternative templates should be
283
	 * named like the default one but suffixed by
284
	 * an unique name. You may use the name of your project for this. If
285
	 * you've implemented an alternative client class as well, it
286
	 * should be suffixed by the name of the new class.
287
	 *
288
	 * @param string Relative path to the template creating code for the HTML page head
289
	 * @since 2019.06
290
	 * @see client/html/catalog/product/template-body
291
	 */
292
}
293