Standard::validateFields()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 20
rs 9.2222
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2025
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
		$data = $view->param( 'address/payment', [] );
193
		$map = $view->param( 'address/delivery', [] );
194
195
		if( !empty( $data ) && ( $view->addressPaymentError = $this->checkFields( $data, 'payment' ) ) !== [] ) {
196
			throw new \Aimeos\Client\Html\Exception( sprintf( 'At least one payment address part is missing or invalid' ) );
197
		}
198
199
		$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' );
200
		$addrItems = $cntl->uses( ['customer/address'] )->get()->getAddressItems();
201
		$cntl->add( $view->param( 'address/payment', [] ) );
202
203
		if( $pos = $view->param( 'address/delete' ) )
204
		{
205
			if( isset( $addrItems[$pos] ) ) {
206
				$cntl->deleteAddressItem( $addrItems[$pos] );
207
			}
208
209
			unset( $map[$pos] );
210
		}
211
212
		foreach( $map as $pos => $data )
213
		{
214
			if( ( $view->addressDeliveryError = $this->checkFields( $data, 'delivery' ) ) !== [] ) {
215
				throw new \Aimeos\Client\Html\Exception( sprintf( 'At least one delivery address part is missing or invalid' ) );
216
			}
217
218
			$addrItem = $addrItems->get( $pos ) ?: $cntl->createAddressItem();
219
			$cntl->addAddressItem( $addrItem->fromArray( $data ), $pos );
220
		}
221
222
		$cntl->store();
223
	}
224
225
226
	/**
227
	 * Checks the address fields for missing data and sanitizes the given parameter list.
228
	 *
229
	 * @param array $params Associative list of address keys (order.address.* or customer.address.*) and their values
230
	 * @return array List of missing field names
231
	 */
232
	protected function checkFields( array $params, string $type ) : array
233
	{
234
		$view = $this->view();
235
		$prefix = $type === 'payment' ? 'customer.' : 'customer.address.';
236
237
		$mandatory = $view->config( 'client/html/common/address/delivery/mandatory', [] );
238
		$optional = $view->config( 'client/html/common/address/delivery/optional', [] );
239
		$hidden = $view->config( 'client/html/common/address/delivery/hidden', [] );
240
241
		$allFields = array_flip( array_merge( $mandatory, $optional, $hidden ) );
242
		$invalid = $this->validateFields( $params, $allFields );
243
		$this->checkSalutation( $params, $mandatory );
244
245
		$msg = match( $type ) {
246
			'delivery' => $view->translate( 'client', 'Delivery address part "%1$s" is invalid' ),
247
			'payment' => $view->translate( 'client', 'Payment address part "%1$s" is invalid' ),
248
			default => $view->translate( 'client', 'Address part "%1$s" is invalid' )
249
		};
250
251
		foreach( $invalid as $key => $name ) {
252
			$invalid[$key] = sprintf( $msg, $name );
253
		}
254
255
		$msg = match( $type ) {
256
			'delivery' => $view->translate( 'client', 'Delivery address part "%1$s" is missing' ),
257
			'payment' => $view->translate( 'client', 'Payment address part "%1$s" is missing' ),
258
			default => $view->translate( 'client', 'Address part "%1$s" is missing' )
259
		};
260
261
		foreach( $mandatory as $key )
262
		{
263
			if( !isset( $params[$prefix . $key] ) || $params[$prefix . $key] == '' ) {
264
				$invalid[$key] = sprintf( $msg, $key );
265
			}
266
		}
267
268
		return $invalid;
269
	}
270
271
272
	/**
273
	 * Additional checks for the salutation
274
	 *
275
	 * @param array $params Associative list of address keys (order.address.*) and their values
276
	 * @param array &$mandatory List of mandatory field names
277
	 */
278
	protected function checkSalutation( array $params, array &$mandatory )
279
	{
280
		if( isset( $params['order.address.salutation'] )
281
				&& $params['order.address.salutation'] === 'company'
282
				&& in_array( 'company', $mandatory ) === false
283
		) {
284
			$mandatory[] = 'company';
285
		}
286
	}
287
288
289
	/**
290
	 * Returns the CSS classes for the delivery address fields
291
	 *
292
	 * @return array Associative list of CSS classes for the delivery address fields
293
	 */
294
	protected function cssDelivery() : array
295
	{
296
		$config = $this->context()->config();
297
298
		$mandatory = $config->get( 'client/html/common/address/delivery/mandatory', [] );
299
		$optional = $config->get( 'client/html/common/address/delivery/optional', [] );
300
		$hidden = $config->get( 'client/html/common/address/delivery/hidden', [] );
301
302
		$css = [];
303
304
		foreach( $mandatory as $name ) {
305
			$css[$name][] = 'mandatory';
306
		}
307
308
		foreach( $optional as $name ) {
309
			$css[$name][] = 'optional';
310
		}
311
312
		foreach( $hidden as $name ) {
313
			$css[$name][] = 'hidden';
314
		}
315
316
		return $css;
317
	}
318
319
320
	/**
321
	 * Returns the CSS classes for the payment address fields
322
	 *
323
	 * @return array Associative list of CSS classes for the payment address fields
324
	 */
325
	protected function cssPayment() : array
326
	{
327
		$config = $this->context()->config();
328
329
		$mandatory = $config->get( 'client/html/common/address/payment/mandatory', [] );
330
		$optional = $config->get( 'client/html/common/address/payment/optional', [] );
331
		$hidden = $config->get( 'client/html/common/address/payment/hidden', [] );
332
333
		$css = [];
334
335
		foreach( $mandatory as $name ) {
336
			$css[$name][] = 'mandatory';
337
		}
338
339
		foreach( $optional as $name ) {
340
			$css[$name][] = 'optional';
341
		}
342
343
		foreach( $hidden as $name ) {
344
			$css[$name][] = 'hidden';
345
		}
346
347
		return $css;
348
	}
349
350
351
	/**
352
	 * Returns the address as string
353
	 *
354
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
355
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Order address item
356
	 * @return string Address as string
357
	 */
358
	protected function getAddressString( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $addr )
359
	{
360
		return preg_replace( "/\n+/m", "\n", trim( sprintf(
361
			/// Address format with company (%1$s), salutation (%2$s), title (%3$s), first name (%4$s), last name (%5$s),
362
			/// 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),
363
			/// postal/zip code (%9$s), city (%10$s), state (%11$s), country (%12$s), language (%13$s),
364
			/// e-mail (%14$s), phone (%15$s), facsimile/telefax (%16$s), mobile (%17$s), web site (%18$s), vatid (%19$s)
365
			$view->translate( 'client', '%1$s
366
%2$s %3$s %4$s %5$s
367
%6$s %7$s
368
%8$s
369
%9$s %10$s
370
%11$s
371
%12$s
372
%13$s
373
%14$s
374
%15$s
375
%16$s
376
%17$s
377
%18$s
378
%19$s
379
'
380
			),
381
			$addr->getCompany(),
382
			$view->translate( 'mshop/code', (string) $addr->getSalutation() ),
383
			$addr->getTitle(),
384
			$addr->getFirstName(),
385
			$addr->getLastName(),
386
			$addr->getAddress1(),
387
			$addr->getAddress2(),
388
			$addr->getAddress3(),
389
			$addr->getPostal(),
390
			$addr->getCity(),
391
			$addr->getState(),
392
			$view->translate( 'country', (string) $addr->getCountryId() ),
393
			$view->translate( 'language', (string) $addr->getLanguageId() ),
394
			$addr->getEmail(),
395
			$addr->getTelephone(),
396
			$addr->getTelefax(),
397
			$addr->getMobile(),
398
			$addr->getWebsite(),
399
			$addr->getVatID()
400
		) ) );
401
	}
402
403
404
	/**
405
	 * Validate the address key/value pairs using regular expressions
406
	 *
407
	 * @param array &$params Associative list of address keys (order.address.*) and their values
408
	 * @param array $fields List of field names to validate
409
	 * @return array List of invalid address keys
410
	 */
411
	protected function validateFields( array $params, array $fields ) : array
412
	{
413
		$invalid = [];
414
		$config = $this->context()->config();
415
416
		foreach( $params as $key => $value )
417
		{
418
			$name = ( $pos = strrpos( $key, '.' ) ) ? substr( $key, $pos + 1 ) : $key;
419
420
			if( isset( $fields[$name] ) )
421
			{
422
				$regex = $config->get( 'client/html/common/address/validate/' . $name );
423
424
				if( $regex && preg_match( '/' . $regex . '/', $value ) !== 1 ) {
425
					$invalid[$name] = $name;
426
				}
427
			}
428
		}
429
430
		return $invalid;
431
	}
432
433
434
	/** client/html/account/profile/template-body
435
	 * Relative path to the HTML body template of the account profile client.
436
	 *
437
	 * The template file contains the HTML code and processing instructions
438
	 * to generate the result shown in the body of the frontend. The
439
	 * configuration string is the path to the template file relative
440
	 * to the templates directory (usually in templates/client/html).
441
	 *
442
	 * You can overwrite the template file configuration in extensions and
443
	 * provide alternative templates. These alternative templates should be
444
	 * named like the default one but suffixed by
445
	 * an unique name. You may use the name of your project for this. If
446
	 * you've implemented an alternative client class as well, it
447
	 * should be suffixed by the name of the new class.
448
	 *
449
	 * @param string Relative path to the template creating code for the HTML page body
450
	 * @since 2016.10
451
	 * @see client/html/account/profile/template-header
452
	 */
453
454
	/** client/html/account/profile/template-header
455
	 * Relative path to the HTML header template of the account profile client.
456
	 *
457
	 * The template file contains the HTML code and processing instructions
458
	 * to generate the HTML code that is inserted into the HTML page header
459
	 * of the rendered page in the frontend. The configuration string is the
460
	 * path to the template file relative to the templates directory (usually
461
	 * in templates/client/html).
462
	 *
463
	 * You can overwrite the template file configuration in extensions and
464
	 * provide alternative templates. These alternative templates should be
465
	 * named like the default one but suffixed by
466
	 * an unique name. You may use the name of your project for this. If
467
	 * you've implemented an alternative client class as well, it
468
	 * should be suffixed by the name of the new class.
469
	 *
470
	 * @param string Relative path to the template creating code for the HTML page head
471
	 * @since 2016.10
472
	 * @see client/html/account/profile/template-body
473
	 */
474
475
	/** client/html/account/profile/decorators/excludes
476
	 * Excludes decorators added by the "common" option from the account profile html client
477
	 *
478
	 * Decorators extend the functionality of a class by adding new aspects
479
	 * (e.g. log what is currently done), executing the methods of the underlying
480
	 * class only in certain conditions (e.g. only for logged in users) or
481
	 * modify what is returned to the caller.
482
	 *
483
	 * This option allows you to remove a decorator added via
484
	 * "client/html/common/decorators/default" before they are wrapped
485
	 * around the html client.
486
	 *
487
	 *  client/html/account/profile/decorators/excludes = array( 'decorator1' )
488
	 *
489
	 * This would remove the decorator named "decorator1" from the list of
490
	 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
491
	 * "client/html/common/decorators/default" to the html client.
492
	 *
493
	 * @param array List of decorator names
494
	 * @see client/html/common/decorators/default
495
	 * @see client/html/account/profile/decorators/global
496
	 * @see client/html/account/profile/decorators/local
497
	 */
498
499
	/** client/html/account/profile/decorators/global
500
	 * Adds a list of globally available decorators only to the account profile html client
501
	 *
502
	 * Decorators extend the functionality of a class by adding new aspects
503
	 * (e.g. log what is currently done), executing the methods of the underlying
504
	 * class only in certain conditions (e.g. only for logged in users) or
505
	 * modify what is returned to the caller.
506
	 *
507
	 * This option allows you to wrap global decorators
508
	 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
509
	 *
510
	 *  client/html/account/profile/decorators/global = array( 'decorator1' )
511
	 *
512
	 * This would add the decorator named "decorator1" defined by
513
	 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
514
	 *
515
	 * @param array List of decorator names
516
	 * @see client/html/common/decorators/default
517
	 * @see client/html/account/profile/decorators/excludes
518
	 * @see client/html/account/profile/decorators/local
519
	 */
520
521
	/** client/html/account/profile/decorators/local
522
	 * Adds a list of local decorators only to the account profile html client
523
	 *
524
	 * Decorators extend the functionality of a class by adding new aspects
525
	 * (e.g. log what is currently done), executing the methods of the underlying
526
	 * class only in certain conditions (e.g. only for logged in users) or
527
	 * modify what is returned to the caller.
528
	 *
529
	 * This option allows you to wrap local decorators
530
	 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
531
	 *
532
	 *  client/html/account/profile/decorators/local = array( 'decorator2' )
533
	 *
534
	 * This would add the decorator named "decorator2" defined by
535
	 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
536
	 *
537
	 * @param array List of decorator names
538
	 * @see client/html/common/decorators/default
539
	 * @see client/html/account/profile/decorators/excludes
540
	 * @see client/html/account/profile/decorators/global
541
	 */
542
}
543