Completed
Push — master ( b9f1e5...5ff555 )
by Aimeos
11:02
created

Standard   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 315
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 0
loc 315
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 0 50 2
A getHeader() 0 51 2
B getSubClient() 0 78 1
A getSubClientNames() 0 4 1
B addData() 0 50 5
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Catalog\Lists\Promo;
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/promo/subparts
26
	 * List of HTML sub-clients rendered within the catalog list promo 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/promo/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 getBody( string $uid = '' ) : string
69
	{
70
		$view = $this->getView();
71
72
		$html = '';
73
		foreach( $this->getSubClients() as $subclient ) {
74
			$html .= $subclient->setView( $view )->getBody( $uid );
75
		}
76
		$view->promoBody = $html;
77
78
		/** client/html/catalog/lists/promo/template-body
79
		 * Relative path to the HTML body template of the catalog list promotion 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/promo/template-body-list = catalog/lists/promo-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/promo/template-header
111
		 * @see client/html/catalog/lists/type/template-body
112
		 */
113
		$tplconf = 'client/html/catalog/lists/promo/template-body';
114
		$default = 'catalog/lists/promo-body-standard';
115
116
		return $view->render( $this->getTemplatePath( $tplconf, $default, 'aimeos/catalog/lists/type' ) );
0 ignored issues
show
Unused Code introduced by
The call to Standard::getTemplatePath() has too many arguments starting with 'aimeos/catalog/lists/type'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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 getHeader( string $uid = '' ) : ?string
127
	{
128
		$view = $this->getView();
129
130
		$html = '';
131
		foreach( $this->getSubClients() as $subclient ) {
132
			$html .= $subclient->setView( $view )->getHeader( $uid );
133
		}
134
		$view->promoHeader = $html;
135
136
		/** client/html/catalog/lists/promo/template-header
137
		 * Relative path to the HTML header template of the catalog list promotion 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/promo/template-header-list = catalog/lists/promo-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/promo/template-body
170
		 * @see client/html/catalog/lists/type/template-body
171
		 */
172
		$tplconf = 'client/html/catalog/lists/promo/template-header';
173
		$default = 'catalog/lists/promo-header-standard';
174
175
		return $view->render( $this->getTemplatePath( $tplconf, $default, 'aimeos/catalog/lists/type' ) );
0 ignored issues
show
Unused Code introduced by
The call to Standard::getTemplatePath() has too many arguments starting with 'aimeos/catalog/lists/type'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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/promo/decorators/excludes
189
		 * Excludes decorators added by the "common" option from the catalog list promo 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/promo/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/promo/decorators/global
211
		 * @see client/html/catalog/lists/promo/decorators/local
212
		 */
213
214
		/** client/html/catalog/lists/promo/decorators/global
215
		 * Adds a list of globally available decorators only to the catalog list promo 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/promo/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/promo/decorators/excludes
235
		 * @see client/html/catalog/lists/promo/decorators/local
236
		 */
237
238
		/** client/html/catalog/lists/promo/decorators/local
239
		 * Adds a list of local decorators only to the catalog list promo 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/promo/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/promo/decorators/excludes
259
		 * @see client/html/catalog/lists/promo/decorators/global
260
		 */
261
262
		return $this->createSubClient( 'catalog/lists/promo/' . $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->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
274
	}
275
276
277
	/**
278
	 * Sets the necessary parameter values in the view.
279
	 *
280
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
281
	 * @param array &$tags Result array for the list of tags that are associated to the output
282
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
283
	 * @return \Aimeos\MW\View\Iface Modified view object
284
	 */
285
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
286
	{
287
		$context = $this->getContext();
288
		$config = $context->getConfig();
289
290
		if( isset( $view->listCurrentCatItem ) ) {
291
			$catId = $view->listCurrentCatItem->getId();
292
		} else {
293
			$catId = $config->get( 'client/html/catalog/lists/catid-default', '' );
294
		}
295
296
		if( $catId )
297
		{
298
			/** client/html/catalog/lists/promo/size
299
			 * The maximum number of products that should be shown in the promotion section
300
			 *
301
			 * Each product list can render a list of promoted products on
302
			 * top if there are any products associated to that category whose
303
			 * list type is "promotion". This option limits the maximum number
304
			 * of products that are displayed. It takes only effect if more
305
			 * promotional products are added to this category than the set
306
			 * value.
307
			 *
308
			 * @param integer Number of promotion products
309
			 * @since 2014.03
310
			 * @category User
311
			 * @category Developer
312
			 */
313
			$size = $config->get( 'client/html/catalog/lists/promo/size', 6 );
314
			$domains = $config->get( 'client/html/catalog/lists/domains', ['media', 'price', 'text'] );
315
			$level = $config->get( 'client/html/catalog/lists/levels', \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
316
317
			$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
318
				->category( $catId, 'promotion', $level )
319
				->sort( 'relevance' )->slice( 0, $size )
320
				->uses( $domains )
321
				->search();
322
323
			$this->addMetaItems( $products, $expire, $tags );
324
325
326
			if( !empty( $products ) && (bool) $config->get( 'client/html/catalog/lists/stock/enable', true ) === true ) {
327
				$view->promoStockUrl = $this->getStockUrl( $view, $products );
328
			}
329
330
			$view->promoItems = $products;
331
		}
332
333
		return parent::addData( $view, $tags, $expire );
334
	}
335
}
336