Passed
Push — master ( 4a159f...7bc452 )
by Aimeos
03:58
created

Standard::modifyBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Catalog\Lists\Items;
13
14
15
/**
16
 * Default implementation of catalog list item section for HTML clients.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Catalog\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/catalog/lists/items/subparts
26
	 * List of HTML sub-clients rendered within the catalog list items section
27
	 *
28
	 * The output of the frontend is composed of the code generated by the HTML
29
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
30
	 * that are responsible for rendering certain sub-parts of the output. The
31
	 * sub-clients can contain HTML clients themselves and therefore a
32
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
33
	 * the output that is placed inside the container of its parent.
34
	 *
35
	 * At first, always the HTML code generated by the parent is printed, then
36
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
37
	 * determines the order of the output of these sub-clients inside the parent
38
	 * container. If the configured list of clients is
39
	 *
40
	 *  array( "subclient1", "subclient2" )
41
	 *
42
	 * you can easily change the order of the output by reordering the subparts:
43
	 *
44
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
45
	 *
46
	 * You can also remove one or more parts if they shouldn't be rendered:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1" )
49
	 *
50
	 * As the clients only generates structural HTML, the layout defined via CSS
51
	 * should support adding, removing or reordering content by a fluid like
52
	 * design.
53
	 *
54
	 * @param array List of sub-client names
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
	private $subPartPath = 'client/html/catalog/lists/items/subparts';
59
	private $subPartNames = [];
60
61
62
	/**
63
	 * Returns the HTML code for insertion into the body.
64
	 *
65
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
66
	 * @return string HTML code
67
	 */
68
	public function body( string $uid = '' ) : string
69
	{
70
		$view = $this->view();
71
72
		$html = '';
73
		foreach( $this->getSubClients() as $subclient ) {
74
			$html .= $subclient->setView( $view )->body( $uid );
75
		}
76
		$view->itemsBody = $html;
77
78
		/** client/html/catalog/lists/items/template-body
79
		 * Relative path to the HTML body template of the catalog list items client.
80
		 *
81
		 * The template file contains the HTML code and processing instructions
82
		 * to generate the result shown in the body of the frontend. The
83
		 * configuration string is the path to the template file relative
84
		 * to the templates directory (usually in client/html/templates).
85
		 *
86
		 * You can overwrite the template file configuration in extensions and
87
		 * provide alternative templates. These alternative templates should be
88
		 * named like the default one but with the string "standard" replaced by
89
		 * an unique name. You may use the name of your project for this. If
90
		 * you've implemented an alternative client class as well, "standard"
91
		 * should be replaced by the name of the new class.
92
		 *
93
		 * It's also possible to create a specific template for each type, e.g.
94
		 * for the grid, list or whatever view you want to offer your users. In
95
		 * that case, you can configure the template by adding "-<type>" to the
96
		 * configuration key. To configure an alternative list view template for
97
		 * example, use the key
98
		 *
99
		 * client/html/catalog/lists/items/template-body-list = catalog/lists/items-body-list.php
100
		 *
101
		 * The argument is the relative path to the new template file. The type of
102
		 * the view is determined by the "l_type" parameter (allowed characters for
103
		 * the types are a-z and 0-9), which is also stored in the session so users
104
		 * will keep the view during their visit. The catalog list type subpart
105
		 * contains the template for switching between list types.
106
		 *
107
		 * @param string Relative path to the template creating code for the HTML page body
108
		 * @since 2014.03
109
		 * @category Developer
110
		 * @see client/html/catalog/lists/items/template-header
111
		 * @see client/html/catalog/lists/type/template-body
112
		 */
113
		$tplconf = 'client/html/catalog/lists/items/template-body';
114
		$default = 'catalog/lists/items-body-standard';
115
116
		return $view->render( $this->getTemplatePath( $tplconf, $default ) );
117
	}
118
119
120
	/**
121
	 * Returns the HTML string for insertion into the header.
122
	 *
123
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
124
	 * @return string|null String including HTML tags for the header on error
125
	 */
126
	public function header( string $uid = '' ) : ?string
127
	{
128
		$view = $this->view();
129
130
		$html = '';
131
		foreach( $this->getSubClients() as $subclient ) {
132
			$html .= $subclient->setView( $view )->header( $uid );
133
		}
134
		$view->itemsHeader = $html;
135
136
		/** client/html/catalog/lists/items/template-header
137
		 * Relative path to the HTML header template of the catalog list items client.
138
		 *
139
		 * The template file contains the HTML code and processing instructions
140
		 * to generate the HTML code that is inserted into the HTML page header
141
		 * of the rendered page in the frontend. The configuration string is the
142
		 * path to the template file relative to the templates directory (usually
143
		 * in client/html/templates).
144
		 *
145
		 * You can overwrite the template file configuration in extensions and
146
		 * provide alternative templates. These alternative templates should be
147
		 * named like the default one but with the string "standard" replaced by
148
		 * an unique name. You may use the name of your project for this. If
149
		 * you've implemented an alternative client class as well, "standard"
150
		 * should be replaced by the name of the new class.
151
		 *
152
		 * It's also possible to create a specific template for each type, e.g.
153
		 * for the grid, list or whatever view you want to offer your users. In
154
		 * that case, you can configure the template by adding "-<type>" to the
155
		 * configuration key. To configure an alternative list view template for
156
		 * example, use the key
157
		 *
158
		 * client/html/catalog/lists/items/template-header-list = catalog/lists/items-header-list.php
159
		 *
160
		 * The argument is the relative path to the new template file. The type of
161
		 * the view is determined by the "l_type" parameter (allowed characters for
162
		 * the types are a-z and 0-9), which is also stored in the session so users
163
		 * will keep the view during their visit. The catalog list type subpart
164
		 * contains the template for switching between list types.
165
		 *
166
		 * @param string Relative path to the template creating code for the HTML page head
167
		 * @since 2014.03
168
		 * @category Developer
169
		 * @see client/html/catalog/lists/items/template-body
170
		 * @see client/html/catalog/lists/type/template-body
171
		 */
172
		$tplconf = 'client/html/catalog/lists/items/template-header';
173
		$default = 'catalog/lists/items-header-standard';
174
175
		return $view->render( $this->getTemplatePath( $tplconf, $default ) );
176
	}
177
178
179
	/**
180
	 * Returns the sub-client given by its name.
181
	 *
182
	 * @param string $type Name of the client type
183
	 * @param string|null $name Name of the sub-client (Default if null)
184
	 * @return \Aimeos\Client\Html\Iface Sub-client object
185
	 */
186
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
187
	{
188
		/** client/html/catalog/lists/items/decorators/excludes
189
		 * Excludes decorators added by the "common" option from the catalog list items html client
190
		 *
191
		 * Decorators extend the functionality of a class by adding new aspects
192
		 * (e.g. log what is currently done), executing the methods of the underlying
193
		 * class only in certain conditions (e.g. only for logged in users) or
194
		 * modify what is returned to the caller.
195
		 *
196
		 * This option allows you to remove a decorator added via
197
		 * "client/html/common/decorators/default" before they are wrapped
198
		 * around the html client.
199
		 *
200
		 *  client/html/catalog/lists/items/decorators/excludes = array( 'decorator1' )
201
		 *
202
		 * This would remove the decorator named "decorator1" from the list of
203
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
204
		 * "client/html/common/decorators/default" to the html client.
205
		 *
206
		 * @param array List of decorator names
207
		 * @since 2015.08
208
		 * @category Developer
209
		 * @see client/html/common/decorators/default
210
		 * @see client/html/catalog/lists/items/decorators/global
211
		 * @see client/html/catalog/lists/items/decorators/local
212
		 */
213
214
		/** client/html/catalog/lists/items/decorators/global
215
		 * Adds a list of globally available decorators only to the catalog list items html client
216
		 *
217
		 * Decorators extend the functionality of a class by adding new aspects
218
		 * (e.g. log what is currently done), executing the methods of the underlying
219
		 * class only in certain conditions (e.g. only for logged in users) or
220
		 * modify what is returned to the caller.
221
		 *
222
		 * This option allows you to wrap global decorators
223
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
224
		 *
225
		 *  client/html/catalog/lists/items/decorators/global = array( 'decorator1' )
226
		 *
227
		 * This would add the decorator named "decorator1" defined by
228
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
229
		 *
230
		 * @param array List of decorator names
231
		 * @since 2015.08
232
		 * @category Developer
233
		 * @see client/html/common/decorators/default
234
		 * @see client/html/catalog/lists/items/decorators/excludes
235
		 * @see client/html/catalog/lists/items/decorators/local
236
		 */
237
238
		/** client/html/catalog/lists/items/decorators/local
239
		 * Adds a list of local decorators only to the catalog list items html client
240
		 *
241
		 * Decorators extend the functionality of a class by adding new aspects
242
		 * (e.g. log what is currently done), executing the methods of the underlying
243
		 * class only in certain conditions (e.g. only for logged in users) or
244
		 * modify what is returned to the caller.
245
		 *
246
		 * This option allows you to wrap local decorators
247
		 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
248
		 *
249
		 *  client/html/catalog/lists/items/decorators/local = array( 'decorator2' )
250
		 *
251
		 * This would add the decorator named "decorator2" defined by
252
		 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
253
		 *
254
		 * @param array List of decorator names
255
		 * @since 2015.08
256
		 * @category Developer
257
		 * @see client/html/common/decorators/default
258
		 * @see client/html/catalog/lists/items/decorators/excludes
259
		 * @see client/html/catalog/lists/items/decorators/global
260
		 */
261
262
		return $this->createSubClient( 'catalog/lists/items/' . $type, $name );
263
	}
264
265
266
	/**
267
	 * Returns the list of sub-client names configured for the client.
268
	 *
269
	 * @return array List of HTML client names
270
	 */
271
	protected function getSubClientNames() : array
272
	{
273
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
274
	}
275
276
277
	/**
278
	 * Modifies the cached content to replace content based on sessions or cookies.
279
	 *
280
	 * @param string $content Cached content
281
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
282
	 * @return string Modified content
283
	 */
284
	public function modify( string $content, string $uid ) : string
285
	{
286
		$content = parent::modify( $content, $uid );
287
288
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.lists.items.csrf' );
289
	}
290
291
292
	/**
293
	 * Sets the necessary parameter values in the view.
294
	 *
295
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
296
	 * @param array &$tags Result array for the list of tags that are associated to the output
297
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
298
	 * @return \Aimeos\MW\View\Iface Modified view object
299
	 */
300
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
301
	{
302
		$productItems = map();
303
		$context = $this->context();
304
		$config = $context->config();
305
		$products = $view->get( 'listProductItems', [] );
306
307
308
		if( $config->get( 'client/html/catalog/lists/basket-add', false ) )
309
		{
310
			foreach( $products as $product )
311
			{
312
				if( $product->getType() === 'select' ) {
313
					$productItems->union( $product->getRefItems( 'product', 'default', 'default' ) );
314
				}
315
			}
316
317
			$this->addMetaItems( $productItems->toArray(), $expire, $tags );
318
			$view->itemsProductItems = $productItems;
319
		}
320
321
322
		/** client/html/catalog/lists/stock/enable
323
		 * Enables or disables displaying product stock levels in product list views
324
		 *
325
		 * This configuration option allows shop owners to display product
326
		 * stock levels for each product in list views or to disable
327
		 * fetching product stock information.
328
		 *
329
		 * The stock information is fetched via AJAX and inserted via Javascript.
330
		 * This allows to cache product items by leaving out such highly
331
		 * dynamic content like stock levels which changes with each order.
332
		 *
333
		 * @param boolean Value of "1" to display stock levels, "0" to disable displaying them
334
		 * @since 2014.03
335
		 * @category User
336
		 * @category Developer
337
		 * @see client/html/catalog/detail/stock/enable
338
		 * @see client/html/catalog/stock/url/target
339
		 * @see client/html/catalog/stock/url/controller
340
		 * @see client/html/catalog/stock/url/action
341
		 * @see client/html/catalog/stock/url/config
342
		 */
343
344
		if( !$products->isEmpty() && (bool) $config->get( 'client/html/catalog/lists/stock/enable', true ) === true ) {
345
			$view->itemsStockUrl = $this->getStockUrl( $view, $products->copy()->union( $productItems ) );
346
		}
347
348
		if( in_array( 'navigator', $config->get( 'client/html/catalog/stage/subparts', ['navigator'] ) ) )
349
		{
350
			$size = $config->get( 'client/html/catalog/lists/size', 48 );
351
			$size = min( max( $view->param( 'l_size', $size ), 1 ), 100 );
352
			$page = min( max( $view->param( 'l_page', 1 ), 1 ), 100 );
353
354
			$view->itemPosition = ( $page - 1 ) * $size;
355
		}
356
357
		return parent::data( $view, $tags, $expire );
358
	}
359
}
360