Passed
Push — master ( 283bfd...df9032 )
by Aimeos
04:29
created

Standard::init()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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