Passed
Push — master ( 6cd0a8...5d904d )
by Aimeos
03:19
created

Standard::body()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 31
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Locale\Select;
12
13
14
/**
15
 * Default implementation of locale select HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/locale/select/subparts
25
	 * List of HTML sub-clients rendered within the locale select section
26
	 *
27
	 * The output of the frontend is composed of the code generated by the HTML
28
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
29
	 * that are responsible for rendering certain sub-parts of the output. The
30
	 * sub-clients can contain HTML clients themselves and therefore a
31
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
32
	 * the output that is placed inside the container of its parent.
33
	 *
34
	 * At first, always the HTML code generated by the parent is printed, then
35
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
36
	 * determines the order of the output of these sub-clients inside the parent
37
	 * container. If the configured list of clients is
38
	 *
39
	 *  array( "subclient1", "subclient2" )
40
	 *
41
	 * you can easily change the order of the output by reordering the subparts:
42
	 *
43
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
44
	 *
45
	 * You can also remove one or more parts if they shouldn't be rendered:
46
	 *
47
	 *  client/html/<clients>/subparts = array( "subclient1" )
48
	 *
49
	 * As the clients only generates structural HTML, the layout defined via CSS
50
	 * should support adding, removing or reordering content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2014.09
55
	 */
56
	private $subPartPath = 'client/html/locale/select/subparts';
57
58
	/** client/html/locale/select/language/name
59
	 * Name of the language part used by the locale selector client implementation
60
	 *
61
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Locale\Select\Language\Myname".
62
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
63
	 *
64
	 * @param string Last part of the client class name
65
	 * @since 2014.09
66
	 */
67
68
	/** client/html/locale/select/currency/name
69
	 * Name of the currency part used by the locale selector client implementation
70
	 *
71
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Locale\Select\Currency\Myname".
72
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
73
	 *
74
	 * @param string Last part of the client class name
75
	 * @since 2014.09
76
	 */
77
	private $subPartNames = array( 'language', 'currency' );
78
79
	private $tags = [];
80
	private $expire;
81
	private $view;
82
83
84
	/**
85
	 * Returns the HTML code for insertion into the body.
86
	 *
87
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
88
	 * @return string HTML code
89
	 */
90
	public function body( string $uid = '' ) : string
91
	{
92
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
93
94
		$html = '';
95
		foreach( $this->getSubClients() as $subclient ) {
96
			$html .= $subclient->setView( $view )->body( $uid );
97
		}
98
99
		/** client/html/locale/select/template-body
100
		 * Relative path to the HTML body template of the locale select client.
101
		 *
102
		 * The template file contains the HTML code and processing instructions
103
		 * to generate the result shown in the body of the frontend. The
104
		 * configuration string is the path to the template file relative
105
		 * to the templates directory (usually in client/html/templates).
106
		 *
107
		 * You can overwrite the template file configuration in extensions and
108
		 * provide alternative templates. These alternative templates should be
109
		 * named like the default one but suffixed by
110
		 * an unique name. You may use the name of your project for this. If
111
		 * you've implemented an alternative client class as well, it
112
		 * should be suffixed by the name of the new class.
113
		 *
114
		 * @param string Relative path to the template creating code for the HTML page body
115
		 * @since 2014.09
116
		 * @see client/html/locale/select/template-header
117
		 */
118
		$template = $this->context()->config()->get( 'client/html/locale/select/template-body', 'locale/select/body' );
119
120
		return $view->set( 'body', $html )->render( $template );
121
	}
122
123
124
	/**
125
	 * Returns the HTML string for insertion into the header.
126
	 *
127
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
128
	 * @return string|null String including HTML tags for the header on error
129
	 */
130
	public function header( string $uid = '' ) : ?string
131
	{
132
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
133
134
		/** client/html/locale/select/template-header
135
		 * Relative path to the HTML header template of the locale select client.
136
		 *
137
		 * The template file contains the HTML code and processing instructions
138
		 * to generate the HTML code that is inserted into the HTML page header
139
		 * of the rendered page in the frontend. The configuration string is the
140
		 * path to the template file relative to the templates directory (usually
141
		 * in client/html/templates).
142
		 *
143
		 * You can overwrite the template file configuration in extensions and
144
		 * provide alternative templates. These alternative templates should be
145
		 * named like the default one but suffixed by
146
		 * an unique name. You may use the name of your project for this. If
147
		 * you've implemented an alternative client class as well, it
148
		 * should be suffixed by the name of the new class.
149
		 *
150
		 * @param string Relative path to the template creating code for the HTML page head
151
		 * @since 2014.09
152
		 * @see client/html/locale/select/template-body
153
		 */
154
		$template = $this->context()->config()->get( 'client/html/locale/select/template-header', 'locale/select/header' );
155
156
		return $view->render( $template );
157
	}
158
159
160
	/**
161
	 * Returns the sub-client given by its name.
162
	 *
163
	 * @param string $type Name of the client type
164
	 * @param string|null $name Name of the sub-client (Default if null)
165
	 * @return \Aimeos\Client\Html\Iface Sub-client object
166
	 */
167
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
168
	{
169
		/** client/html/locale/select/decorators/excludes
170
		 * Excludes decorators added by the "common" option from the locale select html client
171
		 *
172
		 * Decorators extend the functionality of a class by adding new aspects
173
		 * (e.g. log what is currently done), executing the methods of the underlying
174
		 * class only in certain conditions (e.g. only for logged in users) or
175
		 * modify what is returned to the caller.
176
		 *
177
		 * This option allows you to remove a decorator added via
178
		 * "client/html/common/decorators/default" before they are wrapped
179
		 * around the html client.
180
		 *
181
		 *  client/html/locale/select/decorators/excludes = array( 'decorator1' )
182
		 *
183
		 * This would remove the decorator named "decorator1" from the list of
184
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
185
		 * "client/html/common/decorators/default" to the html client.
186
		 *
187
		 * @param array List of decorator names
188
		 * @since 2014.05
189
		 * @see client/html/common/decorators/default
190
		 * @see client/html/locale/select/decorators/global
191
		 * @see client/html/locale/select/decorators/local
192
		 */
193
194
		/** client/html/locale/select/decorators/global
195
		 * Adds a list of globally available decorators only to the locale select html client
196
		 *
197
		 * Decorators extend the functionality of a class by adding new aspects
198
		 * (e.g. log what is currently done), executing the methods of the underlying
199
		 * class only in certain conditions (e.g. only for logged in users) or
200
		 * modify what is returned to the caller.
201
		 *
202
		 * This option allows you to wrap global decorators
203
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
204
		 *
205
		 *  client/html/locale/select/decorators/global = array( 'decorator1' )
206
		 *
207
		 * This would add the decorator named "decorator1" defined by
208
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
209
		 *
210
		 * @param array List of decorator names
211
		 * @since 2014.05
212
		 * @see client/html/common/decorators/default
213
		 * @see client/html/locale/select/decorators/excludes
214
		 * @see client/html/locale/select/decorators/local
215
		 */
216
217
		/** client/html/locale/select/decorators/local
218
		 * Adds a list of local decorators only to the locale select html client
219
		 *
220
		 * Decorators extend the functionality of a class by adding new aspects
221
		 * (e.g. log what is currently done), executing the methods of the underlying
222
		 * class only in certain conditions (e.g. only for logged in users) or
223
		 * modify what is returned to the caller.
224
		 *
225
		 * This option allows you to wrap local decorators
226
		 * ("\Aimeos\Client\Html\Locale\Decorator\*") around the html client.
227
		 *
228
		 *  client/html/locale/select/decorators/local = array( 'decorator2' )
229
		 *
230
		 * This would add the decorator named "decorator2" defined by
231
		 * "\Aimeos\Client\Html\Locale\Decorator\Decorator2" only to the html client.
232
		 *
233
		 * @param array List of decorator names
234
		 * @since 2014.05
235
		 * @see client/html/common/decorators/default
236
		 * @see client/html/locale/select/decorators/excludes
237
		 * @see client/html/locale/select/decorators/global
238
		 */
239
		return $this->createSubClient( 'locale/select/' . $type, $name );
240
	}
241
242
243
	/**
244
	 * Returns the list of sub-client names configured for the client.
245
	 *
246
	 * @return array List of HTML client names
247
	 */
248
	protected function getSubClientNames() : array
249
	{
250
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
251
	}
252
253
254
	/**
255
	 * Sets the necessary parameter values in the view.
256
	 *
257
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
258
	 * @param array &$tags Result array for the list of tags that are associated to the output
259
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
260
	 * @return \Aimeos\MW\View\Iface Modified view object
261
	 */
262
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
263
	{
264
		$map = [];
265
		$context = $this->context();
266
		$config = $context->config();
267
		$locale = $context->locale();
268
269
		/** client/html/locale/select/language/param-name
270
		 * Name of the parameter that contains the language ID value
271
		 *
272
		 * Frameworks and applications normally use its own predefined parameter
273
		 * that contains the current language ID if they are multi-language
274
		 * capable. To adapt the Aimeos parameter name to the already used name,
275
		 * you are able to configure it by using this setting.
276
		 *
277
		 * @param string Parameter name for language ID
278
		 * @since 2015.06
279
		 * @see client/html/locale/select/currency/param-name
280
		 */
281
		$langname = $config->get( 'client/html/locale/select/language/param-name', 'locale' );
282
283
		/** client/html/locale/select/currency/param-name
284
		 * Name of the parameter that contains the currency ID value
285
		 *
286
		 * Frameworks and applications normally use its own predefined parameter
287
		 * that contains the current currency ID if they already support multiple
288
		 * currencies. To adapt the Aimeos parameter name to the already used name,
289
		 * you are able to configure it by using this setting.
290
		 *
291
		 * @param string Parameter name for currency ID
292
		 * @since 2015.06
293
		 * @see client/html/locale/select/language/param-name
294
		 */
295
		$curname = $config->get( 'client/html/locale/select/currency/param-name', 'currency' );
296
297
298
		$items = \Aimeos\Controller\Frontend::create( $context, 'locale' )
299
			->sort( 'position' )->slice( 0, 10000 )->search();
300
301
		foreach( $items as $item )
302
		{
303
			$curId = $item->getCurrencyId();
304
			$langId = $item->getLanguageId();
305
			$map[$langId][$curId] = [$langname => $langId, $curname => $curId];
306
		}
307
308
		$view->selectMap = map( $map );
309
		$view->selectParams = $view->param();
310
		$view->selectLanguageId = $locale->getLanguageId();
311
		$view->selectCurrencyId = $locale->getCurrencyId();
312
313
		return parent::data( $view, $tags, $expire );
314
	}
315
}
316