Passed
Push — master ( 78ace0...e8769f )
by Aimeos
32:12 queued 18:55
created

Standard::cssPayment()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 8
nop 0
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2024
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Account\Profile;
12
13
14
/**
15
 * Default implementation of account profile 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\Iface
23
{
24
	/** client/html/account/profile/name
25
	 * Class name of the used account profile client implementation
26
	 *
27
	 * Each default HTML client can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the client factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Client\Html\Account\Profile\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Client\Html\Account\Profile\Myprofile
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  client/html/account/profile/name = Myprofile
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyProfile"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2016.10
55
	 */
56
57
58
	/**
59
	 * Sets the necessary parameter values in the view.
60
	 *
61
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
62
	 * @param array &$tags Result array for the list of tags that are associated to the output
63
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
64
	 * @return \Aimeos\Base\View\Iface Modified view object
65
	 */
66
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
67
	{
68
		$context = $this->context();
69
		$config = $context->config();
70
71
		/** client/html/common/address/salutations
72
		 * List of salutions the customers can select from in the HTML frontend
73
		 *
74
		 * The following salutations are available:
75
		 *
76
		 * * empty string for "unknown"
77
		 * * company
78
		 * * mr
79
		 * * ms
80
		 *
81
		 * You can modify the list of salutation codes and remove the ones
82
		 * which shouldn't be used or add new ones.
83
		 *
84
		 * @param array List of available salutation codes
85
		 * @since 2021.04
86
		 * @see client/html/account/profile/address/salutations
87
		 */
88
		$salutations = $config->get( 'client/html/common/address/salutations', ['', 'company', 'mr', 'ms'] );
89
90
		/** client/html/account/profile/domains
91
		 * A list of domain names whose items should be available in the account profile view template
92
		 *
93
		 * The templates rendering customer details can contain additional
94
		 * items. If you want to display additional content, you can configure
95
		 * your own list of domains (attribute, media, price, product, text,
96
		 * etc. are domains) whose items are fetched from the storage.
97
		 *
98
		 * @param array List of domain names
99
		 * @since 2016.10
100
		 */
101
		$domains = $config->get( 'client/html/account/profile/domains', ['customer/address'] );
102
103
		/** common/countries
104
		 * A list of ISO country codes which should be available in the checkout address step.
105
		 *
106
		 * If you want to ship your products to several countries or you need
107
		 * to know from which countries your customers are, you have to enable
108
		 * the country selection in the address page of the checkout process.
109
		 *
110
		 * @param array List of two letter ISO country codes
111
		 * @since 2023.04
112
		 */
113
		$countries = $view->config( 'common/countries', [] );
114
115
		/** common/states
116
		 * A list of ISO country codes which should be available in the checkout address step.
117
		 *
118
		 * For each country you can freely define a list of states or regions
119
		 * that can be used afterwards to calculate the final price for each
120
		 * delivery option.
121
		 *
122
		 * To define states or regions use something like this:
123
		 *
124
		 *  [
125
		 *		'US' => [
126
		 *			'CA' => 'California',
127
		 *			'NY' => 'New York',
128
		 *			// ...
129
		 *		'EU' => [
130
		 *			'W' => 'Western Europe',
131
		 *			'C' => 'Central Europe',
132
		 *			// ...
133
		 *		],
134
		 *	],
135
		 *
136
		 * @param array List of two letter ISO country codes
137
		 * @since 2023.04
138
		 */
139
		$states = $view->config( 'common/states', [] );
140
141
		$item = \Aimeos\Controller\Frontend::create( $context, 'customer' )->uses( $domains )->get();
142
143
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
144
		$languages = $localeManager->search( $localeManager->filter( true ) )
145
			->col( 'locale.languageid', 'locale.languageid' );
146
147
		$deliveries = [];
148
		$addr = $item->getPaymentAddress();
149
150
		if( !$addr->getLanguageId() ) {
151
			$addr->setLanguageId( $context->locale()->getLanguageId() );
152
		}
153
154
		$payment = $addr->toArray();
155
		$payment['string'] = $this->call( 'getAddressString', $view, $addr );
156
157
		foreach( $item->getAddressItems() as $pos => $address )
158
		{
159
			$delivery = $address->toArray();
160
			$delivery['string'] = $this->call( 'getAddressString', $view, $address );
161
			$deliveries[$pos] = $delivery;
162
		}
163
164
		$view->profileItem = $item;
165
		$view->addressPayment = $payment;
166
		$view->addressDelivery = $deliveries;
167
		$view->addressPaymentCss = $this->cssPayment();
168
		$view->addressDeliveryCss = $this->cssDelivery();
169
		$view->addressCountries = $countries;
170
		$view->addressLanguages = $languages;
171
		$view->addressSalutations = $salutations;
172
		$view->addressStates = $states;
173
174
		return parent::data( $view, $tags, $expire );
175
	}
176
177
178
	/**
179
	 * Processes the input, e.g. store given values.
180
	 *
181
	 * A view must be available and this method doesn't generate any output
182
	 * besides setting view variables if necessary.
183
	 */
184
	public function init()
185
	{
186
		$view = $this->view();
187
188
		if( !$view->param( 'address/save' ) && !$view->param( 'address/delete' ) ) {
189
			return;
190
		}
191
192
		$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' );
193
		$addrItems = $cntl->uses( ['customer/address'] )->get()->getAddressItems();
194
		$cntl->add( $view->param( 'address/payment', [] ) );
195
196
		$map = $view->param( 'address/delivery', [] );
197
198
		if( $pos = $view->param( 'address/delete' ) ) {
199
			unset( $map[$pos] );
200
		}
201
202
		foreach( $map as $pos => $data )
203
		{
204
			$addrItem = $addrItems->get( $pos ) ?: $cntl->createAddressItem();
205
			$cntl->addAddressItem( $addrItem->fromArray( $data ), $pos );
206
			$addrItems->remove( $pos );
207
		}
208
209
		foreach( $addrItems as $addrItem ) {
210
			$cntl->deleteAddressItem( $addrItem );
211
		}
212
213
		$cntl->store();
214
	}
215
216
217
	/**
218
	 * Returns the CSS classes for the delivery address fields
219
	 *
220
	 * @return array Associative list of CSS classes for the delivery address fields
221
	 */
222
	protected function cssDelivery() : array
223
	{
224
		$config = $this->context()->config();
225
226
		$mandatory = $config->get( 'client/html/common/address/delivery/mandatory', [] );
227
		$optional = $config->get( 'client/html/common/address/delivery/optional', [] );
228
		$hidden = $config->get( 'client/html/common/address/delivery/hidden', [] );
229
230
		$css = [];
231
232
		foreach( $mandatory as $name ) {
233
			$css[$name][] = 'mandatory';
234
		}
235
236
		foreach( $optional as $name ) {
237
			$css[$name][] = 'optional';
238
		}
239
240
		foreach( $hidden as $name ) {
241
			$css[$name][] = 'hidden';
242
		}
243
244
		return $css;
245
	}
246
247
248
	/**
249
	 * Returns the CSS classes for the payment address fields
250
	 *
251
	 * @return array Associative list of CSS classes for the payment address fields
252
	 */
253
	protected function cssPayment() : array
254
	{
255
		$config = $this->context()->config();
256
257
		$mandatory = $config->get( 'client/html/common/address/payment/mandatory', [] );
258
		$optional = $config->get( 'client/html/common/address/payment/optional', [] );
259
		$hidden = $config->get( 'client/html/common/address/payment/hidden', [] );
260
261
		$css = [];
262
263
		foreach( $mandatory as $name ) {
264
			$css[$name][] = 'mandatory';
265
		}
266
267
		foreach( $optional as $name ) {
268
			$css[$name][] = 'optional';
269
		}
270
271
		foreach( $hidden as $name ) {
272
			$css[$name][] = 'hidden';
273
		}
274
275
		return $css;
276
	}
277
278
279
	/**
280
	 * Returns the address as string
281
	 *
282
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
283
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Order address item
284
	 * @return string Address as string
285
	 */
286
	protected function getAddressString( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $addr )
287
	{
288
		return preg_replace( "/\n+/m", "\n", trim( sprintf(
289
			/// Address format with company (%1$s), salutation (%2$s), title (%3$s), first name (%4$s), last name (%5$s),
290
			/// address part one (%6$s, e.g street), address part two (%7$s, e.g house number), address part three (%8$s, e.g additional information),
291
			/// postal/zip code (%9$s), city (%10$s), state (%11$s), country (%12$s), language (%13$s),
292
			/// e-mail (%14$s), phone (%15$s), facsimile/telefax (%16$s), mobile (%17$s), web site (%18$s), vatid (%19$s)
293
			$view->translate( 'client', '%1$s
294
%2$s %3$s %4$s %5$s
295
%6$s %7$s
296
%8$s
297
%9$s %10$s
298
%11$s
299
%12$s
300
%13$s
301
%14$s
302
%15$s
303
%16$s
304
%17$s
305
%18$s
306
%19$s
307
'
308
			),
309
			$addr->getCompany(),
310
			$view->translate( 'mshop/code', (string) $addr->getSalutation() ),
311
			$addr->getTitle(),
312
			$addr->getFirstName(),
313
			$addr->getLastName(),
314
			$addr->getAddress1(),
315
			$addr->getAddress2(),
316
			$addr->getAddress3(),
317
			$addr->getPostal(),
318
			$addr->getCity(),
319
			$addr->getState(),
320
			$view->translate( 'country', (string) $addr->getCountryId() ),
321
			$view->translate( 'language', (string) $addr->getLanguageId() ),
322
			$addr->getEmail(),
323
			$addr->getTelephone(),
324
			$addr->getTelefax(),
325
			$addr->getMobile(),
326
			$addr->getWebsite(),
327
			$addr->getVatID()
328
		) ) );
329
	}
330
331
332
	/** client/html/account/profile/template-body
333
	 * Relative path to the HTML body template of the account profile client.
334
	 *
335
	 * The template file contains the HTML code and processing instructions
336
	 * to generate the result shown in the body of the frontend. The
337
	 * configuration string is the path to the template file relative
338
	 * to the templates directory (usually in templates/client/html).
339
	 *
340
	 * You can overwrite the template file configuration in extensions and
341
	 * provide alternative templates. These alternative templates should be
342
	 * named like the default one but suffixed by
343
	 * an unique name. You may use the name of your project for this. If
344
	 * you've implemented an alternative client class as well, it
345
	 * should be suffixed by the name of the new class.
346
	 *
347
	 * @param string Relative path to the template creating code for the HTML page body
348
	 * @since 2016.10
349
	 * @see client/html/account/profile/template-header
350
	 */
351
352
	/** client/html/account/profile/template-header
353
	 * Relative path to the HTML header template of the account profile client.
354
	 *
355
	 * The template file contains the HTML code and processing instructions
356
	 * to generate the HTML code that is inserted into the HTML page header
357
	 * of the rendered page in the frontend. The configuration string is the
358
	 * path to the template file relative to the templates directory (usually
359
	 * in templates/client/html).
360
	 *
361
	 * You can overwrite the template file configuration in extensions and
362
	 * provide alternative templates. These alternative templates should be
363
	 * named like the default one but suffixed by
364
	 * an unique name. You may use the name of your project for this. If
365
	 * you've implemented an alternative client class as well, it
366
	 * should be suffixed by the name of the new class.
367
	 *
368
	 * @param string Relative path to the template creating code for the HTML page head
369
	 * @since 2016.10
370
	 * @see client/html/account/profile/template-body
371
	 */
372
373
	/** client/html/account/profile/decorators/excludes
374
	 * Excludes decorators added by the "common" option from the account profile html client
375
	 *
376
	 * Decorators extend the functionality of a class by adding new aspects
377
	 * (e.g. log what is currently done), executing the methods of the underlying
378
	 * class only in certain conditions (e.g. only for logged in users) or
379
	 * modify what is returned to the caller.
380
	 *
381
	 * This option allows you to remove a decorator added via
382
	 * "client/html/common/decorators/default" before they are wrapped
383
	 * around the html client.
384
	 *
385
	 *  client/html/account/profile/decorators/excludes = array( 'decorator1' )
386
	 *
387
	 * This would remove the decorator named "decorator1" from the list of
388
	 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
389
	 * "client/html/common/decorators/default" to the html client.
390
	 *
391
	 * @param array List of decorator names
392
	 * @see client/html/common/decorators/default
393
	 * @see client/html/account/profile/decorators/global
394
	 * @see client/html/account/profile/decorators/local
395
	 */
396
397
	/** client/html/account/profile/decorators/global
398
	 * Adds a list of globally available decorators only to the account profile html client
399
	 *
400
	 * Decorators extend the functionality of a class by adding new aspects
401
	 * (e.g. log what is currently done), executing the methods of the underlying
402
	 * class only in certain conditions (e.g. only for logged in users) or
403
	 * modify what is returned to the caller.
404
	 *
405
	 * This option allows you to wrap global decorators
406
	 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
407
	 *
408
	 *  client/html/account/profile/decorators/global = array( 'decorator1' )
409
	 *
410
	 * This would add the decorator named "decorator1" defined by
411
	 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
412
	 *
413
	 * @param array List of decorator names
414
	 * @see client/html/common/decorators/default
415
	 * @see client/html/account/profile/decorators/excludes
416
	 * @see client/html/account/profile/decorators/local
417
	 */
418
419
	/** client/html/account/profile/decorators/local
420
	 * Adds a list of local decorators only to the account profile html client
421
	 *
422
	 * Decorators extend the functionality of a class by adding new aspects
423
	 * (e.g. log what is currently done), executing the methods of the underlying
424
	 * class only in certain conditions (e.g. only for logged in users) or
425
	 * modify what is returned to the caller.
426
	 *
427
	 * This option allows you to wrap local decorators
428
	 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
429
	 *
430
	 *  client/html/account/profile/decorators/local = array( 'decorator2' )
431
	 *
432
	 * This would add the decorator named "decorator2" defined by
433
	 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
434
	 *
435
	 * @param array List of decorator names
436
	 * @see client/html/common/decorators/default
437
	 * @see client/html/account/profile/decorators/excludes
438
	 * @see client/html/account/profile/decorators/global
439
	 */
440
}
441