Passed
Push — master ( e8769f...b1fb6a )
by Aimeos
04:02
created

Standard::countries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 33
rs 10
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-2024
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Checkout\Standard\Address;
13
14
15
// Strings for translation
16
sprintf( 'address' );
17
18
19
/**
20
 * Default implementation of checkout address HTML client.
21
 *
22
 * @package Client
23
 * @subpackage Html
24
 */
25
class Standard
26
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
27
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
28
{
29
	/** client/html/checkout/standard/address/subparts
30
	 * List of HTML sub-clients rendered within the checkout standard address section
31
	 *
32
	 * The output of the frontend is composed of the code generated by the HTML
33
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
34
	 * that are responsible for rendering certain sub-parts of the output. The
35
	 * sub-clients can contain HTML clients themselves and therefore a
36
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
37
	 * the output that is placed inside the container of its parent.
38
	 *
39
	 * At first, always the HTML code generated by the parent is printed, then
40
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
41
	 * determines the order of the output of these sub-clients inside the parent
42
	 * container. If the configured list of clients is
43
	 *
44
	 *  array( "subclient1", "subclient2" )
45
	 *
46
	 * you can easily change the order of the output by reordering the subparts:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
49
	 *
50
	 * You can also remove one or more parts if they shouldn't be rendered:
51
	 *
52
	 *  client/html/<clients>/subparts = array( "subclient1" )
53
	 *
54
	 * As the clients only generates structural HTML, the layout defined via CSS
55
	 * should support adding, removing or reordering content by a fluid like
56
	 * design.
57
	 *
58
	 * @param array List of sub-client names
59
	 * @since 2014.03
60
	 */
61
	private string $subPartPath = 'client/html/checkout/standard/address/subparts';
62
63
	/** client/html/checkout/standard/address/payment/name
64
	 * Name of the payment part used by the checkout standard address client implementation
65
	 *
66
	 * Use "Myname" if your class is named "\Aimeos\Client\Checkout\Standard\Address\Billing\Myname".
67
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
68
	 *
69
	 * @param string Last part of the client class name
70
	 * @since 2014.03
71
	 */
72
73
	/** client/html/checkout/standard/address/delivery/name
74
	 * Name of the delivery part used by the checkout standard address client implementation
75
	 *
76
	 * Use "Myname" if your class is named "\Aimeos\Client\Checkout\Standard\Address\Delivery\Myname".
77
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
78
	 *
79
	 * @param string Last part of the client class name
80
	 * @since 2014.03
81
	 */
82
	private array $subPartNames = ['payment', 'delivery'];
83
84
85
	/**
86
	 * Returns the HTML code for insertion into the body.
87
	 *
88
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
89
	 * @return string HTML code
90
	 */
91
	public function body( string $uid = '' ) : string
92
	{
93
		$view = $this->view();
94
		$step = $view->get( 'standardStepActive', 'address' );
95
		$onepage = $view->config( 'client/html/checkout/standard/onepage', [] );
96
97
		if( $step != 'address' && !( in_array( 'address', $onepage ) && in_array( $step, $onepage ) ) ) {
98
			return '';
99
		}
100
101
		return parent::body( $uid );
102
	}
103
104
105
	/**
106
	 * Returns the sub-client given by its name.
107
	 *
108
	 * @param string $type Name of the client type
109
	 * @param string|null $name Name of the sub-client (Default if null)
110
	 * @return \Aimeos\Client\Html\Iface Sub-client object
111
	 */
112
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
113
	{
114
		/** client/html/checkout/standard/address/decorators/excludes
115
		 * Excludes decorators added by the "common" option from the checkout standard address html client
116
		 *
117
		 * Decorators extend the functionality of a class by adding new aspects
118
		 * (e.g. log what is currently done), executing the methods of the underlying
119
		 * class only in certain conditions (e.g. only for logged in users) or
120
		 * modify what is returned to the caller.
121
		 *
122
		 * This option allows you to remove a decorator added via
123
		 * "client/html/common/decorators/default" before they are wrapped
124
		 * around the html client.
125
		 *
126
		 *  client/html/checkout/standard/address/decorators/excludes = array( 'decorator1' )
127
		 *
128
		 * This would remove the decorator named "decorator1" from the list of
129
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
130
		 * "client/html/common/decorators/default" to the html client.
131
		 *
132
		 * @param array List of decorator names
133
		 * @since 2015.08
134
		 * @see client/html/common/decorators/default
135
		 * @see client/html/checkout/standard/address/decorators/global
136
		 * @see client/html/checkout/standard/address/decorators/local
137
		 */
138
139
		/** client/html/checkout/standard/address/decorators/global
140
		 * Adds a list of globally available decorators only to the checkout standard address html client
141
		 *
142
		 * Decorators extend the functionality of a class by adding new aspects
143
		 * (e.g. log what is currently done), executing the methods of the underlying
144
		 * class only in certain conditions (e.g. only for logged in users) or
145
		 * modify what is returned to the caller.
146
		 *
147
		 * This option allows you to wrap global decorators
148
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
149
		 *
150
		 *  client/html/checkout/standard/address/decorators/global = array( 'decorator1' )
151
		 *
152
		 * This would add the decorator named "decorator1" defined by
153
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
154
		 *
155
		 * @param array List of decorator names
156
		 * @since 2015.08
157
		 * @see client/html/common/decorators/default
158
		 * @see client/html/checkout/standard/address/decorators/excludes
159
		 * @see client/html/checkout/standard/address/decorators/local
160
		 */
161
162
		/** client/html/checkout/standard/address/decorators/local
163
		 * Adds a list of local decorators only to the checkout standard address html client
164
		 *
165
		 * Decorators extend the functionality of a class by adding new aspects
166
		 * (e.g. log what is currently done), executing the methods of the underlying
167
		 * class only in certain conditions (e.g. only for logged in users) or
168
		 * modify what is returned to the caller.
169
		 *
170
		 * This option allows you to wrap local decorators
171
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
172
		 *
173
		 *  client/html/checkout/standard/address/decorators/local = array( 'decorator2' )
174
		 *
175
		 * This would add the decorator named "decorator2" defined by
176
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
177
		 *
178
		 * @param array List of decorator names
179
		 * @since 2015.08
180
		 * @see client/html/common/decorators/default
181
		 * @see client/html/checkout/standard/address/decorators/excludes
182
		 * @see client/html/checkout/standard/address/decorators/global
183
		 */
184
185
		return $this->createSubClient( 'checkout/standard/address/' . $type, $name );
186
	}
187
188
189
	/**
190
	 * Processes the input, e.g. store given values.
191
	 *
192
	 * A view must be available and this method doesn't generate any output
193
	 * besides setting view variables.
194
	 */
195
	public function init()
196
	{
197
		$view = $this->view();
198
		$context = $this->context();
199
200
		try
201
		{
202
			parent::init();
203
204
			if( ( $param = $view->param( 'ca_extra' ) ) !== null ) {
205
				$context->session()->set( 'client/html/checkout/standard/address/extra', (array) $param );
206
			}
207
208
			if( !isset( $view->standardStepActive )
209
				&& !$this->call( 'isAvailable', \Aimeos\Controller\Frontend::create( $context, 'basket' )->get() )
210
			) {
211
				$view->standardStepActive = 'address';
212
			}
213
		}
214
		catch( \Exception $e )
215
		{
216
			$view->standardStepActive = 'address';
217
			throw $e;
218
		}
219
	}
220
221
222
	/**
223
	 * Returns the list of sub-client names configured for the client.
224
	 *
225
	 * @return array List of HTML client names
226
	 */
227
	protected function getSubClientNames() : array
228
	{
229
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
230
	}
231
232
233
	/**
234
	 * Sets the necessary parameter values in the view.
235
	 *
236
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
237
	 * @param array &$tags Result array for the list of tags that are associated to the output
238
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
239
	 * @return \Aimeos\Base\View\Iface Modified view object
240
	 */
241
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
242
	{
243
		$context = $this->context();
244
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
245
		$controller = \Aimeos\Controller\Frontend::create( $context, 'customer' );
246
		$orderAddressManager = \Aimeos\MShop::create( $context, 'order/address' );
247
248
		$deliveryAddressItems = [];
249
		$item = $controller->uses( ['customer/address'] )->get();
250
		$paymentAddressItem = $orderAddressManager->create()->copyFrom( $item->getPaymentAddress() );
251
252
		foreach( $item->getAddressItems() as $pos => $addrItem ) {
253
			$deliveryAddressItems[$pos] = $orderAddressManager->create()->copyFrom( $addrItem );
254
		}
255
256
		$view->addressCustomerItem = $item;
257
		$view->addressPaymentItem = $paymentAddressItem;
258
		$view->addressDeliveryItems = $deliveryAddressItems;
259
		$view->addressSalutations = $this->salutations();
260
		$view->addressCountries = $this->countries();
261
		$view->addressStates = $this->states();
262
		$view->addressLanguages = $localeManager->search( $localeManager->filter( true ) )
263
			->col( 'locale.languageid', 'locale.languageid' );
264
265
		return parent::data( $view, $tags, $expire );
266
	}
267
268
269
	/**
270
	 * Returns the list of available countries
271
	 *
272
	 * @return array Associative list of two letter ISO country codes as keys and country names as values
273
	 */
274
	protected function countries() : array
275
	{
276
		$context = $this->context();
277
278
		/** common/countries
279
		 * List of available country codes for frontend and backend
280
		 *
281
		 * This configration option is used whenever a list of countries is
282
		 * shown in the frontend or backend. It's used e.g.
283
		 * if the customer should select the country he is living in the
284
		 * checkout process. In case that the list is empty, no country
285
		 * selection is shown.
286
		 *
287
		 * Each list entry must be a two letter ISO country code that is then
288
		 * translated into its name. The codes have to be upper case
289
		 * characters like "DE" for Germany or "GB" for Great Britain, e.g.
290
		 *
291
		 *  array( 'DE', 'GB', ... )
292
		 *
293
		 * @param array List of two letter ISO country codes
294
		 * @since 2023.04
295
		 */
296
		$countries = map( $context->config()->get( 'common/countries', [] ) );
297
		$map = $countries->flip()->map( function( $v, $key ) use ( $context ) {
298
			return $context->translate( 'country', $key );
299
		} );
300
301
		// Don't destroy custom order of countries, only sort if order is strictly alphabetical
302
		if( $countries->is( $countries->clone()->sort(), true ) ) {
303
			$map->asort();
304
		}
305
306
		return $countries->all();
307
	}
308
309
310
	/**
311
	 * Tests if an item is available and the step can be skipped
312
	 *
313
	 * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object
314
	 * @return bool TRUE if step can be skipped, FALSE if not
315
	 */
316
	protected function isAvailable( \Aimeos\MShop\Order\Item\Iface $basket ) : bool
317
	{
318
		return !empty( $basket->getAddress( 'payment' ) );
319
	}
320
321
322
	/**
323
	 * Returns the list of configured salutation codes
324
	 *
325
	 * @return array List of salutation codes
326
	 */
327
	protected function salutations() : array
328
	{
329
		/** client/html/common/address/salutations
330
		 * List of salutions the customer can select from
331
		 *
332
		 * The following salutations are available:
333
		 *
334
		 * * empty string for "unknown"
335
		 * * company
336
		 * * mr
337
		 * * ms
338
		 *
339
		 * You can modify the list of salutation codes and remove the ones
340
		 * which shouldn't be used or add new ones.
341
		 *
342
		 * @param array List of available salutation codes
343
		 * @since 2024.04
344
		 * @see common/countries
345
		 * @see common/states
346
		 */
347
		return $this->context()->config()->get( 'client/html/common/address/salutations', [] );
348
	}
349
350
351
	/**
352
	 * Returns the states map for the configured countries
353
	 *
354
	 * @return array Associative list of two letter ISO country codes as keys and map of state codes and state names as values
355
	 */
356
	protected function states() : array
357
	{
358
		/** common/states
359
		 * List of available states for frontend and backend
360
		 *
361
		 * This configration option is used whenever a list of states is
362
		 * shown in the frontend or bakcend. It's used e.g.
363
		 * if the customer should select the state he is living in the
364
		 * checkout process. In case that the list is empty, no state
365
		 * selection is shown.
366
		 *
367
		 * A two letter ISO country code must be the key for the list of
368
		 * states that belong to this country. The list of states must then
369
		 * contain the state code as key and its name as values, e.g.
370
		 *
371
		 *  array(
372
		 *      'US' => array(
373
		 *          'CA' => 'California',
374
		 *          'NY' => 'New York',
375
		 *          ...
376
		 *      ),
377
		 *      ...
378
		 *  );
379
		 *
380
		 * The codes have to be upper case characters like "US" for the
381
		 * United States or "DE" for Germany. The order of the country and
382
		 * state codes determine the order of the states in the frontend and
383
		 * the state codes are later used for per state tax calculation.
384
		 *
385
		 * @param array Multi-dimensional list ISO country codes and state codes/names
386
		 * @since 2023.04
387
		 */
388
		return $this->context()->config()->get( 'common/states', [] );
389
	}
390
391
392
	/** client/html/checkout/standard/address/template-body
393
	 * Relative path to the HTML body template of the checkout standard address client.
394
	 *
395
	 * The template file contains the HTML code and processing instructions
396
	 * to generate the result shown in the body of the frontend. The
397
	 * configuration string is the path to the template file relative
398
	 * to the templates directory (usually in templates/client/html).
399
	 *
400
	 * You can overwrite the template file configuration in extensions and
401
	 * provide alternative templates. These alternative templates should be
402
	 * named like the default one but suffixed by
403
	 * an unique name. You may use the name of your project for this. If
404
	 * you've implemented an alternative client class as well, it
405
	 * should be suffixed by the name of the new class.
406
	 *
407
	 * @param string Relative path to the template creating code for the HTML page body
408
	 * @since 2014.03
409
	 * @see client/html/checkout/standard/address/template-header
410
	 */
411
}
412