Passed
Push — master ( 25bd95...9e12c4 )
by Aimeos
02:39
created

Standard::body()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 36
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2022
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Catalog\Home;
12
13
/**
14
 * Implementation of catalog home section HTML clients for a configurable list of homes.
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/home/cache
37
		 * Enables or disables caching only for the catalog home 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/home
51
		 * All parameters defined for the catalog home 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#home
57
		 */
58
		$confkey = 'client/html/catalog/home';
59
		$config = $this->context()->config();
60
61
		if( $html = $this->cached( 'body', $uid, [], $confkey ) ) {
62
			return $this->modify( $html, $uid );
63
		}
64
65
		$template = $config->get( 'client/html/catalog/home/template-body', 'catalog/home/body' );
66
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
67
		$html = $view->render( $template );
68
69
		return $this->cache( 'body', $uid, [], $confkey, $html, $this->tags, $this->expire );
70
	}
71
72
73
	/**
74
	 * Returns the HTML string for insertion into the header.
75
	 *
76
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
77
	 * @return string|null String including HTML tags for the header on error
78
	 */
79
	public function header( string $uid = '' ) : ?string
80
	{
81
		$confkey = 'client/html/catalog/home';
82
		$config = $this->context()->config();
83
84
		if( $html = $this->cached( 'header', $uid, [], $confkey ) ) {
85
			return $this->modify( $html, $uid );
86
		}
87
88
		$template = $config->get( 'client/html/catalog/home/template-header', 'catalog/home/header' );
89
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
90
		$html = $view->render( $template );
91
92
		return $this->cache( 'header', $uid, [], $confkey, $html, $this->tags, $this->expire );
93
	}
94
95
96
	/**
97
	 * Sets the necessary parameter values in the view.
98
	 *
99
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
100
	 * @param array &$tags Result array for the list of tags that are associated to the output
101
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
102
	 * @return \Aimeos\Base\View\Iface Modified view object
103
	 */
104
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
105
	{
106
		$context = $this->context();
107
		$config = $context->config();
108
109
		$tree = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->uses( $this->domains() )
110
			->getTree( \Aimeos\Controller\Frontend\Catalog\Iface::LIST );
111
112
113
		$articles = map();
114
		$products = $tree->getRefItems( 'product', null, 'promotion' );
115
116
		foreach( $tree->getChildren() as $child ) {
117
			$products->union( $child->getRefItems( 'product', null, 'promotion' ) );
118
		}
119
120
		if( $config->get( 'client/html/catalog/home/basket-add', false ) )
121
		{
122
			foreach( $products as $product )
123
			{
124
				if( $product->getType() === 'select' ) {
125
					$articles->union( $product->getRefItems( 'product', 'default', 'default' ) );
126
				}
127
			}
128
		}
129
130
		/** client/html/catalog/home/stock/enable
131
		 * Enables or disables displaying product stock levels in product list views
132
		 *
133
		 * This configuration option allows shop owners to display product
134
		 * stock levels for each product in list views or to disable
135
		 * fetching product stock information.
136
		 *
137
		 * The stock information is fetched via AJAX and inserted via Javascript.
138
		 * This allows to cache product items by leaving out such highly
139
		 * dynamic content like stock levels which changes with each order.
140
		 *
141
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
142
		 * @since 2020.10
143
		 * @see client/html/catalog/detail/stock/enable
144
		 * @see client/html/catalog/stock/url/target
145
		 * @see client/html/catalog/stock/url/controller
146
		 * @see client/html/catalog/stock/url/action
147
		 * @see client/html/catalog/stock/url/config
148
		 */
149
		if( !$products->isEmpty() && (bool) $config->get( 'client/html/catalog/home/stock/enable', true ) === true ) {
150
			$view->homeStockUrl = $this->getStockUrl( $view, $products->union( $articles ) );
151
		}
152
153
		// Delete cache when products are added or deleted even when in "tag-all" mode
154
		$this->addMetaItems( $tree, $expire, $tags, ['catalog', 'product'] );
155
156
		$view->homeTree = $tree;
157
158
		return parent::data( $view, $tags, $expire );
159
	}
160
161
162
	/**
163
	 * Returns the data domains fetched along with the products
164
	 *
165
	 * @return array List of domain names
166
	 */
167
	protected function domains() : array
168
	{
169
		$context = $this->context();
170
		$config = $context->config();
171
172
		/** client/html/catalog/home/domains
173
		 * A list of domain names whose items should be available in the catalog home view template
174
		 *
175
		 * The templates rendering home lists usually add the images, prices
176
		 * and texts associated to each home item. If you want to display additional
177
		 * content like the home attributes, you can configure your own list of
178
		 * domains (attribute, media, price, home, text, etc. are domains)
179
		 * whose items are fetched from the storage. Please keep in mind that
180
		 * the more domains you add to the configuration, the more time is required
181
		 * for fetching the content!
182
		 *
183
		 * This configuration option overwrites the "client/html/catalog/domains"
184
		 * option that allows to configure the domain names of the items fetched
185
		 * for all catalog related data.
186
		 *
187
		 * @param array List of domain names
188
		 * @since 2020.10
189
		 * @see client/html/catalog/domains
190
		 * @see client/html/catalog/detail/domains
191
		 * @see client/html/catalog/stage/domains
192
		 * @see client/html/catalog/lists/domains
193
		 */
194
		$domains = ['catalog', 'media', 'media/property', 'price', 'supplier', 'text', 'product' => ['promotion']];
195
		$domains = $config->get( 'client/html/catalog/domains', $domains );
196
		$domains = $config->get( 'client/html/catalog/home/domains', $domains );
197
198
		/** client/html/catalog/home/basket-add
199
		 * Display the "add to basket" button for each product item in the catalog home component
200
		 *
201
		 * Enables the button for adding products to the basket for the listed products.
202
		 * This works for all type of products, even for selection products with product
203
		 * variants and product bundles. By default, also optional attributes are
204
		 * displayed if they have been associated to a product.
205
		 *
206
		 * @param boolean True to display the button, false to hide it
207
		 * @since 2020.10
208
		 * @see client/html/catalog/lists/basket-add
209
		 * @see client/html/catalog/detail/basket-add
210
		 * @see client/html/basket/related/basket-add
211
		 * @see client/html/catalog/product/basket-add
212
		 */
213
		if( $config->get( 'client/html/catalog/home/basket-add', false ) ) {
214
			$domains = array_merge_recursive( $domains, ['attribute' => ['variant', 'custom', 'config']] );
215
		}
216
217
		return $domains;
218
	}
219
220
221
	/** client/html/catalog/home/template-body
222
	 * Relative path to the HTML body template of the catalog home client.
223
	 *
224
	 * The template file contains the HTML code and processing instructions
225
	 * to generate the result shown in the body of the frontend. The
226
	 * configuration string is the path to the template file relative
227
	 * to the templates directory (usually in client/html/templates).
228
	 *
229
	 * You can overwrite the template file configuration in extensions and
230
	 * provide alternative templates. These alternative templates should be
231
	 * named like the default one but suffixed by
232
	 * an unique name. You may use the name of your project for this. If
233
	 * you've implemented an alternative client class as well, it
234
	 * should be suffixed by the name of the new class.
235
	 *
236
	 * @param string Relative path to the template creating code for the HTML page body
237
	 * @since 2020.10
238
	 * @see client/html/catalog/home/template-header
239
	 */
240
241
	/** client/html/catalog/home/template-header
242
	 * Relative path to the HTML header template of the catalog home client.
243
	 *
244
	 * The template file contains the HTML code and processing instructions
245
	 * to generate the HTML code that is inserted into the HTML page header
246
	 * of the rendered page in the frontend. The configuration string is the
247
	 * path to the template file relative to the templates directory (usually
248
	 * in client/html/templates).
249
	 *
250
	 * You can overwrite the template file configuration in extensions and
251
	 * provide alternative templates. These alternative templates should be
252
	 * named like the default one but suffixed by
253
	 * an unique name. You may use the name of your project for this. If
254
	 * you've implemented an alternative client class as well, it
255
	 * should be suffixed by the name of the new class.
256
	 *
257
	 * @param string Relative path to the template creating code for the HTML page head
258
	 * @since 2020.10
259
	 * @see client/html/catalog/home/template-body
260
	 */
261
}
262