Standard::init()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 19
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-2025
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( !isset( $view->standardStepActive )
205
				&& !$this->call( 'isAvailable', \Aimeos\Controller\Frontend::create( $context, 'basket' )->get() )
206
			) {
207
				$view->standardStepActive = 'address';
208
			}
209
		}
210
		catch( \Exception $e )
211
		{
212
			$view->standardStepActive = 'address';
213
			throw $e;
214
		}
215
	}
216
217
218
	/**
219
	 * Returns the list of sub-client names configured for the client.
220
	 *
221
	 * @return array List of HTML client names
222
	 */
223
	protected function getSubClientNames() : array
224
	{
225
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
226
	}
227
228
229
	/**
230
	 * Sets the necessary parameter values in the view.
231
	 *
232
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
233
	 * @param array &$tags Result array for the list of tags that are associated to the output
234
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
235
	 * @return \Aimeos\Base\View\Iface Modified view object
236
	 */
237
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], ?string &$expire = null ) : \Aimeos\Base\View\Iface
238
	{
239
		$context = $this->context();
240
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
241
		$controller = \Aimeos\Controller\Frontend::create( $context, 'customer' );
242
		$orderManager = \Aimeos\MShop::create( $context, 'order' );
243
244
		$deliveryAddressItems = [];
245
		$item = $controller->uses( ['customer/address'] )->get();
246
		$paymentAddressItem = $orderManager->createAddress()->copyFrom( $item->getPaymentAddress() );
0 ignored issues
show
Bug introduced by
The method createAddress() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
		$paymentAddressItem = $orderManager->/** @scrutinizer ignore-call */ createAddress()->copyFrom( $item->getPaymentAddress() );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
248
		foreach( $item->getAddressItems() as $pos => $addrItem ) {
249
			$deliveryAddressItems[$pos] = $orderManager->createAddress()->copyFrom( $addrItem );
250
		}
251
252
		$view->addressCustomerItem = $item;
253
		$view->addressPaymentItem = $paymentAddressItem;
254
		$view->addressDeliveryItems = $deliveryAddressItems;
255
		$view->addressSalutations = $this->salutations();
256
		$view->addressCountries = $this->countries();
257
		$view->addressStates = $this->states();
258
		$view->addressLanguages = $localeManager->search( $localeManager->filter( true ) )
259
			->col( 'locale.languageid', 'locale.languageid' );
260
261
		return parent::data( $view, $tags, $expire );
262
	}
263
264
265
	/**
266
	 * Returns the list of available countries
267
	 *
268
	 * @return array Associative list of two letter ISO country codes as keys and country names as values
269
	 */
270
	protected function countries() : array
271
	{
272
		$context = $this->context();
273
274
		/** common/countries
275
		 * List of available country codes for frontend and backend
276
		 *
277
		 * This configration option is used whenever a list of countries is
278
		 * shown in the frontend or backend. It's used e.g.
279
		 * if the customer should select the country he is living in the
280
		 * checkout process. In case that the list is empty, no country
281
		 * selection is shown.
282
		 *
283
		 * Each list entry must be a two letter ISO country code that is then
284
		 * translated into its name. The codes have to be upper case
285
		 * characters like "DE" for Germany or "GB" for Great Britain, e.g.
286
		 *
287
		 *  array( 'DE', 'GB', ... )
288
		 *
289
		 * @param array List of two letter ISO country codes
290
		 * @since 2023.04
291
		 */
292
		$countries = map( $context->config()->get( 'common/countries', [] ) );
293
		$map = $countries->flip()->map( function( $v, $key ) use ( $context ) {
294
			return $context->translate( 'country', $key );
295
		} );
296
297
		// Don't destroy custom order of countries, only sort if order is strictly alphabetical
298
		if( $countries->is( $countries->clone()->sort(), true ) ) {
299
			$map->asort();
300
		}
301
302
		return $countries->all();
303
	}
304
305
306
	/**
307
	 * Tests if an item is available and the step can be skipped
308
	 *
309
	 * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object
310
	 * @return bool TRUE if step can be skipped, FALSE if not
311
	 */
312
	protected function isAvailable( \Aimeos\MShop\Order\Item\Iface $basket ) : bool
313
	{
314
		return !empty( $basket->getAddress( 'payment' ) );
315
	}
316
317
318
	/**
319
	 * Returns the list of configured salutation codes
320
	 *
321
	 * @return array List of salutation codes
322
	 */
323
	protected function salutations() : array
324
	{
325
		/** client/html/common/address/salutations
326
		 * List of salutions the customer can select from
327
		 *
328
		 * The following salutations are available:
329
		 *
330
		 * * empty string for "unknown"
331
		 * * company
332
		 * * mr
333
		 * * ms
334
		 *
335
		 * You can modify the list of salutation codes and remove the ones
336
		 * which shouldn't be used or add new ones.
337
		 *
338
		 * @param array List of available salutation codes
339
		 * @since 2024.04
340
		 * @see common/countries
341
		 * @see common/states
342
		 */
343
		return $this->context()->config()->get( 'client/html/common/address/salutations', [] );
344
	}
345
346
347
	/**
348
	 * Returns the states map for the configured countries
349
	 *
350
	 * @return array Associative list of two letter ISO country codes as keys and map of state codes and state names as values
351
	 */
352
	protected function states() : array
353
	{
354
		/** common/states
355
		 * List of available states for frontend and backend
356
		 *
357
		 * This configration option is used whenever a list of states is
358
		 * shown in the frontend or bakcend. It's used e.g.
359
		 * if the customer should select the state he is living in the
360
		 * checkout process. In case that the list is empty, no state
361
		 * selection is shown.
362
		 *
363
		 * A two letter ISO country code must be the key for the list of
364
		 * states that belong to this country. The list of states must then
365
		 * contain the state code as key and its name as values, e.g.
366
		 *
367
		 *  array(
368
		 *      'US' => array(
369
		 *          'CA' => 'California',
370
		 *          'NY' => 'New York',
371
		 *          ...
372
		 *      ),
373
		 *      ...
374
		 *  );
375
		 *
376
		 * The codes have to be upper case characters like "US" for the
377
		 * United States or "DE" for Germany. The order of the country and
378
		 * state codes determine the order of the states in the frontend and
379
		 * the state codes are later used for per state tax calculation.
380
		 *
381
		 * @param array Multi-dimensional list ISO country codes and state codes/names
382
		 * @since 2023.04
383
		 */
384
		return $this->context()->config()->get( 'common/states', [] );
385
	}
386
387
388
	/** client/html/checkout/standard/address/template-body
389
	 * Relative path to the HTML body template of the checkout standard address client.
390
	 *
391
	 * The template file contains the HTML code and processing instructions
392
	 * to generate the result shown in the body of the frontend. The
393
	 * configuration string is the path to the template file relative
394
	 * to the templates directory (usually in templates/client/html).
395
	 *
396
	 * You can overwrite the template file configuration in extensions and
397
	 * provide alternative templates. These alternative templates should be
398
	 * named like the default one but suffixed by
399
	 * an unique name. You may use the name of your project for this. If
400
	 * you've implemented an alternative client class as well, it
401
	 * should be suffixed by the name of the new class.
402
	 *
403
	 * @param string Relative path to the template creating code for the HTML page body
404
	 * @since 2014.03
405
	 * @see client/html/checkout/standard/address/template-header
406
	 */
407
}
408