Passed
Push — master ( 8102ef...4ef140 )
by Aimeos
07:34
created

Standard::body()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 46
rs 9.9332
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-2022
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Basket\Mini;
13
14
15
/**
16
 * Default implementation of mini basket HTML client.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Basket\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/basket/mini/name
26
	 * Class name of the used basket mini client implementation
27
	 *
28
	 * Each default HTML client can be replace by an alternative imlementation.
29
	 * To use this implementation, you have to set the last part of the class
30
	 * name as configuration value so the client factory knows which class it
31
	 * has to instantiate.
32
	 *
33
	 * For example, if the name of the default class is
34
	 *
35
	 *  \Aimeos\Client\Html\Basket\Mini\Standard
36
	 *
37
	 * and you want to replace it with your own version named
38
	 *
39
	 *  \Aimeos\Client\Html\Basket\Mini\Mybasket
40
	 *
41
	 * then you have to set the this configuration option:
42
	 *
43
	 *  client/html/basket/mini/name = Mybasket
44
	 *
45
	 * The value is the last part of your own class name and it's case sensitive,
46
	 * so take care that the configuration value is exactly named like the last
47
	 * part of the class name.
48
	 *
49
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
50
	 * characters are possible! You should always start the last part of the class
51
	 * name with an upper case character and continue only with lower case characters
52
	 * or numbers. Avoid chamel case names like "MyBasket"!
53
	 *
54
	 * @param string Last part of the class name
55
	 * @since 2014.03
56
	 */
57
58
59
	/** client/html/basket/mini/subparts
60
	 * List of HTML sub-clients rendered within the basket mini section
61
	 *
62
	 * The output of the frontend is composed of the code generated by the HTML
63
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
64
	 * that are responsible for rendering certain sub-parts of the output. The
65
	 * sub-clients can contain HTML clients themselves and therefore a
66
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
67
	 * the output that is placed inside the container of its parent.
68
	 *
69
	 * At first, always the HTML code generated by the parent is printed, then
70
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
71
	 * determines the order of the output of these sub-clients inside the parent
72
	 * container. If the configured list of clients is
73
	 *
74
	 *  array( "subclient1", "subclient2" )
75
	 *
76
	 * you can easily change the order of the output by reordering the subparts:
77
	 *
78
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
79
	 *
80
	 * You can also remove one or more parts if they shouldn't be rendered:
81
	 *
82
	 *  client/html/<clients>/subparts = array( "subclient1" )
83
	 *
84
	 * As the clients only generates structural HTML, the layout defined via CSS
85
	 * should support adding, removing or reordering content by a fluid like
86
	 * design.
87
	 *
88
	 * @param array List of sub-client names
89
	 * @since 2014.03
90
	 */
91
	private $subPartPath = 'client/html/basket/mini/subparts';
92
	private $subPartNames = [];
93
	private $view;
94
95
96
	/**
97
	 * Returns the HTML code for insertion into the body.
98
	 *
99
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
100
	 * @return string HTML code
101
	 */
102
	public function body( string $uid = '' ) : string
103
	{
104
		$context = $this->context();
105
		$site = $context->locale()->getSiteId();
106
107
		/** client/html/basket/mini
108
		 * All parameters defined for the small basket component and its subparts
109
		 *
110
		 * This returns all settings related to the small basket component.
111
		 * Please refer to the single settings for details.
112
		 *
113
		 * @param array Associative list of name/value settings
114
		 * @see client/html/basket#mini
115
		 */
116
		$config = $context->config()->get( 'client/html/basket/mini', [] );
117
		$key = $this->getParamHash( [], $uid . $site . ':basket:mini-body', $config );
118
119
		if( $html = $this->getBasketCached( $key ) ) {
120
			return $this->modify( $html, $uid );
121
		}
122
123
		/** client/html/basket/mini/template-body
124
		 * Relative path to the HTML body template of the basket mini client.
125
		 *
126
		 * The template file contains the HTML code and processing instructions
127
		 * to generate the result shown in the body of the frontend. The
128
		 * configuration string is the path to the template file relative
129
		 * to the templates directory (usually in client/html/templates).
130
		 *
131
		 * You can overwrite the template file configuration in extensions and
132
		 * provide alternative templates. These alternative templates should be
133
		 * named like the default one but suffixed by
134
		 * an unique name. You may use the name of your project for this. If
135
		 * you've implemented an alternative client class as well, it
136
		 * should be suffixed by the name of the new class.
137
		 *
138
		 * @param string Relative path to the template creating code for the HTML page body
139
		 * @since 2014.03
140
		 * @see client/html/basket/mini/template-header
141
		 */
142
		$template = $this->context()->config()->get( 'client/html/basket/mini/template-body', 'basket/mini/body' );
143
144
		$view = $this->view = $this->view ?? $this->object()->data( $this->view() );
145
		$html = $this->modify( $view->render( $template ), $uid );
146
147
		return (string) $this->setBasketCached( $key, $html );
148
	}
149
150
151
	/**
152
	 * Returns the HTML string for insertion into the header.
153
	 *
154
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
155
	 * @return string|null String including HTML tags for the header on error
156
	 */
157
	public function header( string $uid = '' ) : ?string
158
	{
159
		$context = $this->context();
160
		$site = $context->locale()->getSiteId();
161
		$view = $this->view();
0 ignored issues
show
Unused Code introduced by
The assignment to $view is dead and can be removed.
Loading history...
162
163
		$config = $context->config()->get( 'client/html/basket/mini', [] );
164
		$key = $this->getParamHash( [], $uid . $site . ':basket:mini-header', $config );
165
166
		if( $html = $this->getBasketCached( $key ) ) {
167
			return $this->modify( $html, $uid );
168
		}
169
170
		/** client/html/basket/mini/template-header
171
		 * Relative path to the HTML header template of the basket mini client.
172
		 *
173
		 * The template file contains the HTML code and processing instructions
174
		 * to generate the HTML code that is inserted into the HTML page header
175
		 * of the rendered page in the frontend. The configuration string is the
176
		 * path to the template file relative to the templates directory (usually
177
		 * in client/html/templates).
178
		 *
179
		 * You can overwrite the template file configuration in extensions and
180
		 * provide alternative templates. These alternative templates should be
181
		 * named like the default one but suffixed by
182
		 * an unique name. You may use the name of your project for this. If
183
		 * you've implemented an alternative client class as well, it
184
		 * should be suffixed by the name of the new class.
185
		 *
186
		 * @param string Relative path to the template creating code for the HTML page head
187
		 * @since 2014.03
188
		 * @see client/html/basket/mini/template-body
189
		 */
190
		$template = $this->context()->config()->get( 'client/html/basket/mini/template-header', 'basket/mini/header' );
191
192
		$view = $this->view = $this->view ?? $this->object()->data( $this->view() );
193
		$html = $this->modify( $view->render( $template ), $uid );
194
195
		return $this->setBasketCached( $key, $html );
196
	}
197
198
199
	/**
200
	 * Returns the sub-client given by its name.
201
	 *
202
	 * @param string $type Name of the client type
203
	 * @param string|null $name Name of the sub-client (Default if null)
204
	 * @return \Aimeos\Client\Html\Iface Sub-client object
205
	 */
206
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
207
	{
208
		/** client/html/basket/mini/decorators/excludes
209
		 * Excludes decorators added by the "common" option from the basket mini html client
210
		 *
211
		 * Decorators extend the functionality of a class by adding new aspects
212
		 * (e.g. log what is currently done), executing the methods of the underlying
213
		 * class only in certain conditions (e.g. only for logged in users) or
214
		 * modify what is returned to the caller.
215
		 *
216
		 * This option allows you to remove a decorator added via
217
		 * "client/html/common/decorators/default" before they are wrapped
218
		 * around the html client.
219
		 *
220
		 *  client/html/basket/mini/decorators/excludes = array( 'decorator1' )
221
		 *
222
		 * This would remove the decorator named "decorator1" from the list of
223
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
224
		 * "client/html/common/decorators/default" to the html client.
225
		 *
226
		 * @param array List of decorator names
227
		 * @since 2014.05
228
		 * @see client/html/common/decorators/default
229
		 * @see client/html/basket/mini/decorators/global
230
		 * @see client/html/basket/mini/decorators/local
231
		 */
232
233
		/** client/html/basket/mini/decorators/global
234
		 * Adds a list of globally available decorators only to the basket mini html client
235
		 *
236
		 * Decorators extend the functionality of a class by adding new aspects
237
		 * (e.g. log what is currently done), executing the methods of the underlying
238
		 * class only in certain conditions (e.g. only for logged in users) or
239
		 * modify what is returned to the caller.
240
		 *
241
		 * This option allows you to wrap global decorators
242
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
243
		 *
244
		 *  client/html/basket/mini/decorators/global = array( 'decorator1' )
245
		 *
246
		 * This would add the decorator named "decorator1" defined by
247
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
248
		 *
249
		 * @param array List of decorator names
250
		 * @since 2014.05
251
		 * @see client/html/common/decorators/default
252
		 * @see client/html/basket/mini/decorators/excludes
253
		 * @see client/html/basket/mini/decorators/local
254
		 */
255
256
		/** client/html/basket/mini/decorators/local
257
		 * Adds a list of local decorators only to the basket mini html client
258
		 *
259
		 * Decorators extend the functionality of a class by adding new aspects
260
		 * (e.g. log what is currently done), executing the methods of the underlying
261
		 * class only in certain conditions (e.g. only for logged in users) or
262
		 * modify what is returned to the caller.
263
		 *
264
		 * This option allows you to wrap local decorators
265
		 * ("\Aimeos\Client\Html\Basket\Decorator\*") around the html client.
266
		 *
267
		 *  client/html/basket/mini/decorators/local = array( 'decorator2' )
268
		 *
269
		 * This would add the decorator named "decorator2" defined by
270
		 * "\Aimeos\Client\Html\Basket\Decorator\Decorator2" only to the html client.
271
		 *
272
		 * @param array List of decorator names
273
		 * @since 2014.05
274
		 * @see client/html/common/decorators/default
275
		 * @see client/html/basket/mini/decorators/excludes
276
		 * @see client/html/basket/mini/decorators/global
277
		 */
278
279
		return $this->createSubClient( 'basket/mini/' . $type, $name );
280
	}
281
282
283
	/**
284
	 * Returns the list of sub-client names configured for the client.
285
	 *
286
	 * @return array List of HTML client names
287
	 */
288
	protected function getSubClientNames() : array
289
	{
290
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
291
	}
292
293
294
	/**
295
	 * Sets the necessary parameter values in the view.
296
	 *
297
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
298
	 * @param array &$tags Result array for the list of tags that are associated to the output
299
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
300
	 * @return \Aimeos\Base\View\Iface Modified view object
301
	 */
302
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
303
	{
304
		$controller = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' );
305
		$view->miniBasket = $controller->get();
306
307
		return parent::data( $view, $tags, $expire );
308
	}
309
}
310