Passed
Push — master ( fd781a...b1eaa4 )
by Aimeos
08:26
created

Standard::getSubClientNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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-2022
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Email\Payment;
13
14
15
/**
16
 * Default implementation of payment emails.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/email/payment/subparts
26
	 * List of HTML sub-clients rendered within the email payment section
27
	 *
28
	 * The output of the frontend is composed of the code generated by the HTML
29
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
30
	 * that are responsible for rendering certain sub-parts of the output. The
31
	 * sub-clients can contain HTML clients themselves and therefore a
32
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
33
	 * the output that is placed inside the container of its parent.
34
	 *
35
	 * At first, always the HTML code generated by the parent is printed, then
36
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
37
	 * determines the order of the output of these sub-clients inside the parent
38
	 * container. If the configured list of clients is
39
	 *
40
	 *  array( "subclient1", "subclient2" )
41
	 *
42
	 * you can easily change the order of the output by reordering the subparts:
43
	 *
44
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
45
	 *
46
	 * You can also remove one or more parts if they shouldn't be rendered:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1" )
49
	 *
50
	 * As the clients only generates structural HTML, the layout defined via CSS
51
	 * should support adding, removing or reordering content by a fluid like
52
	 * design.
53
	 *
54
	 * @param array List of sub-client names
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
	private $subPartPath = 'client/html/email/payment/subparts';
59
60
	/** client/html/email/payment/text/name
61
	 * Name of the text part used by the email payment client implementation
62
	 *
63
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Payment\Text\Myname".
64
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
65
	 *
66
	 * @param string Last part of the client class name
67
	 * @since 2014.03
68
	 * @category Developer
69
	 */
70
71
	/** client/html/email/payment/html/name
72
	 * Name of the html part used by the email payment client implementation
73
	 *
74
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Payment\Html\Myname".
75
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
76
	 *
77
	 * @param string Last part of the client class name
78
	 * @since 2014.03
79
	 * @category Developer
80
	 */
81
82
	/** client/html/email/payment/pdf/name
83
	 * Name of the PDF part used by the email payment client implementation
84
	 *
85
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Payment\Pdf\Myname".
86
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
87
	 *
88
	 * @param string Last part of the client class name
89
	 * @since 2020.07
90
	 * @category Developer
91
	 */
92
	private $subPartNames = array( 'text', 'html', 'pdf' );
93
94
95
	/**
96
	 * Returns the HTML code for insertion into the body.
97
	 *
98
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
99
	 * @return string HTML code
100
	 */
101
	public function body( string $uid = '' ) : string
102
	{
103
		$view = $this->object()->data( $this->view() );
104
105
		$content = '';
106
		foreach( $this->getSubClients() as $subclient ) {
107
			$content .= $subclient->setView( $view )->body( $uid );
108
		}
109
		$view->paymentBody = $content;
110
111
112
		/** client/html/email/payment/attachments
113
		 * List of file paths whose content should be attached to all payment e-mails
114
		 *
115
		 * This configuration option allows you to add files to the e-mails that are
116
		 * sent to the customer when the payment status changes, e.g. for the order
117
		 * confirmation e-mail. These files can't be customer specific.
118
		 *
119
		 * @param array List of absolute file paths
120
		 * @since 2016.10
121
		 * @category Developer
122
		 * @category User
123
		 * @see client/html/email/delivery/attachments
124
		 */
125
		$files = $view->config( 'client/html/email/payment/attachments', [] );
126
127
		$this->attachs( $view->mail(), $files );
128
129
130
		/** client/html/email/payment/template-body
131
		 * Relative path to the HTML body template of the email payment client.
132
		 *
133
		 * The template file contains the HTML code and processing instructions
134
		 * to generate the result shown in the body of the frontend. The
135
		 * configuration string is the path to the template file relative
136
		 * to the templates directory (usually in client/html/templates).
137
		 *
138
		 * You can overwrite the template file configuration in extensions and
139
		 * provide alternative templates. These alternative templates should be
140
		 * named like the default one but suffixed by
141
		 * an unique name. You may use the name of your project for this. If
142
		 * you've implemented an alternative client class as well, it
143
		 * should be suffixed by the name of the new class.
144
		 *
145
		 * The email payment HTML client allows to use a different template for
146
		 * each payment status value. You can create a template for each payment
147
		 * status and store it in the "email/payment/<status number>/" directory
148
		 * below the "templates" directory (usually in client/html/templates). If no
149
		 * specific layout template is found, the common template in the
150
		 * "email/payment/" directory is used.
151
		 *
152
		 * @param string Relative path to the template creating code for the HTML page body
153
		 * @since 2014.03
154
		 * @category Developer
155
		 * @see client/html/email/payment/template-header
156
		 */
157
		$tplconf = 'client/html/email/payment/template-body';
158
159
		$status = $view->extOrderItem->getStatusPayment();
160
		$default = array( 'email/payment/' . $status . '/body', 'email/payment/body' );
161
162
		return $view->render( $view->config( $tplconf, $default ) );
163
	}
164
165
166
	/**
167
	 * Returns the HTML string for insertion into the header.
168
	 *
169
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
170
	 * @return string|null String including HTML tags for the header on error
171
	 */
172
	public function header( string $uid = '' ) : ?string
173
	{
174
		$config = $this->context()->config();
175
		$view = $this->object()->data( $this->view() );
176
177
178
		$addr = $view->extAddressItem;
179
180
		$msg = $view->mail();
181
		$msg->header( 'X-MailGenerator', 'Aimeos' );
182
		$msg->to( $addr->getEMail(), $addr->getFirstName() . ' ' . $addr->getLastName() );
183
184
185
		$fromName = $config->get( 'resource/email/from-name' );
186
187
		/** client/html/email/from-name
188
		 * @see client/html/email/payment/from-email
189
		 */
190
		$fromName = $config->get( 'client/html/email/from-name', $fromName );
191
192
		/** client/html/email/payment/from-name
193
		 * Name used when sending payment e-mails
194
		 *
195
		 * The name of the person or e-mail account that is used for sending all
196
		 * shop related payment e-mails to customers. This configuration option
197
		 * overwrite the name set in "client/html/email/from-name".
198
		 *
199
		 * @param string Name shown in the e-mail
200
		 * @since 2014.03
201
		 * @category User
202
		 * @see client/html/email/from-name
203
		 * @see client/html/email/from-email
204
		 * @see client/html/email/reply-email
205
		 * @see client/html/email/bcc-email
206
		 */
207
		$fromNamePayment = $config->get( 'client/html/email/payment/from-name', $fromName );
208
209
		$fromEmail = $config->get( 'resource/email/from-email' );
210
211
		/** client/html/email/from-email
212
		 * @see client/html/email/payment/from-email
213
		 */
214
		$fromEmail = $config->get( 'client/html/email/from-email', $fromEmail );
215
216
		/** client/html/email/payment/from-email
217
		 * E-Mail address used when sending payment e-mails
218
		 *
219
		 * The e-mail address of the person or account that is used for sending
220
		 * all shop related payment emails to customers. This configuration option
221
		 * overwrites the e-mail address set via "client/html/email/from-email".
222
		 *
223
		 * @param string E-mail address
224
		 * @since 2014.03
225
		 * @category User
226
		 * @see client/html/email/payment/from-name
227
		 * @see client/html/email/from-email
228
		 * @see client/html/email/reply-email
229
		 * @see client/html/email/bcc-email
230
		 */
231
		if( ( $fromEmailPayment = $config->get( 'client/html/email/payment/from-email', $fromEmail ) ) != null ) {
232
			$msg->from( $fromEmailPayment, $fromNamePayment );
233
		}
234
235
236
		/** client/html/email/reply-name
237
		 * @see client/html/email/payment/reply-email
238
		 */
239
		$replyName = $config->get( 'client/html/email/reply-name', $fromName );
240
241
		/** client/html/email/payment/reply-name
242
		 * Recipient name displayed when the customer replies to payment e-mails
243
		 *
244
		 * The name of the person or e-mail account the customer should
245
		 * reply to in case of payment related questions or problems. This
246
		 * configuration option overwrites the name set via
247
		 * "client/html/email/reply-name".
248
		 *
249
		 * @param string Name shown in the e-mail
250
		 * @since 2014.03
251
		 * @category User
252
		 * @see client/html/email/payment/reply-email
253
		 * @see client/html/email/reply-name
254
		 * @see client/html/email/reply-email
255
		 * @see client/html/email/from-email
256
		 * @see client/html/email/bcc-email
257
		 */
258
		$replyNamePayment = $config->get( 'client/html/email/payment/reply-name', $replyName );
259
260
		/** client/html/email/reply-email
261
		 * @see client/html/email/payment/reply-email
262
		 */
263
		$replyEmail = $config->get( 'client/html/email/reply-email', $fromEmail );
264
265
		/** client/html/email/payment/reply-email
266
		 * E-Mail address used by the customer when replying to payment e-mails
267
		 *
268
		 * The e-mail address of the person or e-mail account the customer
269
		 * should reply to in case of payment related questions or problems.
270
		 * This configuration option overwrites the e-mail address set via
271
		 * "client/html/email/reply-email".
272
		 *
273
		 * @param string E-mail address
274
		 * @since 2014.03
275
		 * @category User
276
		 * @see client/html/email/payment/reply-name
277
		 * @see client/html/email/reply-email
278
		 * @see client/html/email/from-email
279
		 * @see client/html/email/bcc-email
280
		 */
281
		if( ( $replyEmailPayment = $config->get( 'client/html/email/payment/reply-email', $replyEmail ) ) != null ) {
282
			$msg->replyTo( $replyEmailPayment, $replyNamePayment );
283
		}
284
285
286
		/** client/html/email/bcc-email
287
		 * @see client/html/email/payment/bcc-email
288
		 */
289
		$bccEmail = $config->get( 'client/html/email/bcc-email' );
290
291
		/** client/html/email/payment/bcc-email
292
		 * E-Mail address all payment e-mails should be also sent to
293
		 *
294
		 * Using this option you can send a copy of all payment related e-mails
295
		 * to a second e-mail account. This can be handy for testing and checking
296
		 * the e-mails sent to customers.
297
		 *
298
		 * It also allows shop owners with a very small volume of orders to be
299
		 * notified about payment changes. Be aware that this isn't useful if the
300
		 * order volumne is high or has peeks!
301
		 *
302
		 * This configuration option overwrites the e-mail address set via
303
		 * "client/html/email/bcc-email".
304
		 *
305
		 * @param string|array E-mail address or list of e-mail addresses
306
		 * @since 2014.03
307
		 * @category User
308
		 * @category Developer
309
		 * @see client/html/email/bcc-email
310
		 * @see client/html/email/reply-email
311
		 * @see client/html/email/from-email
312
		 */
313
		if( ( $bccEmailPayment = $config->get( 'client/html/email/payment/bcc-email', $bccEmail ) ) != null )
314
		{
315
			foreach( (array) $bccEmailPayment as $emailAddr ) {
316
				$msg->Bcc( $emailAddr );
317
			}
318
		}
319
320
321
		/** client/html/email/payment/template-header
322
		 * Relative path to the HTML header template of the email payment client.
323
		 *
324
		 * The template file contains the HTML code and processing instructions
325
		 * to generate the HTML code that is inserted into the HTML page header
326
		 * of the rendered page in the frontend. The configuration string is the
327
		 * path to the template file relative to the templates directory (usually
328
		 * in client/html/templates).
329
		 *
330
		 * You can overwrite the template file configuration in extensions and
331
		 * provide alternative templates. These alternative templates should be
332
		 * named like the default one but suffixed by
333
		 * an unique name. You may use the name of your project for this. If
334
		 * you've implemented an alternative client class as well, it
335
		 * should be suffixed by the name of the new class.
336
		 *
337
		 * The email payment HTML client allows to use a different template for
338
		 * each payment status value. You can create a template for each payment
339
		 * status and store it in the "email/payment/<status number>/" directory
340
		 * below the "templates" directory (usually in client/html/templates). If no
341
		 * specific layout template is found, the common template in the
342
		 * "email/payment/" directory is used.
343
		 *
344
		 * @param string Relative path to the template creating code for the HTML page head
345
		 * @since 2014.03
346
		 * @category Developer
347
		 * @see client/html/email/payment/template-body
348
		 */
349
		$tplconf = 'client/html/email/payment/template-header';
350
351
		$status = $view->extOrderItem->getStatusPayment();
352
		$default = array( 'email/payment/' . $status . '/header', 'email/payment/header' );
353
354
		return $view->render( $view->config( $tplconf, $default ) ); ;
355
	}
356
357
358
	/**
359
	 * Returns the sub-client given by its name.
360
	 *
361
	 * @param string $type Name of the client type
362
	 * @param string|null $name Name of the sub-client (Default if null)
363
	 * @return \Aimeos\Client\Html\Iface Sub-client object
364
	 */
365
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
366
	{
367
		/** client/html/email/payment/decorators/excludes
368
		 * Excludes decorators added by the "common" option from the email payment html client
369
		 *
370
		 * Decorators extend the functionality of a class by adding new aspects
371
		 * (e.g. log what is currently done), executing the methods of the underlying
372
		 * class only in certain conditions (e.g. only for logged in users) or
373
		 * modify what is returned to the caller.
374
		 *
375
		 * This option allows you to remove a decorator added via
376
		 * "client/html/common/decorators/default" before they are wrapped
377
		 * around the html client.
378
		 *
379
		 *  client/html/email/payment/decorators/excludes = array( 'decorator1' )
380
		 *
381
		 * This would remove the decorator named "decorator1" from the list of
382
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
383
		 * "client/html/common/decorators/default" to the html client.
384
		 *
385
		 * @param array List of decorator names
386
		 * @since 2014.05
387
		 * @category Developer
388
		 * @see client/html/common/decorators/default
389
		 * @see client/html/email/payment/decorators/global
390
		 * @see client/html/email/payment/decorators/local
391
		 */
392
393
		/** client/html/email/payment/decorators/global
394
		 * Adds a list of globally available decorators only to the email payment html client
395
		 *
396
		 * Decorators extend the functionality of a class by adding new aspects
397
		 * (e.g. log what is currently done), executing the methods of the underlying
398
		 * class only in certain conditions (e.g. only for logged in users) or
399
		 * modify what is returned to the caller.
400
		 *
401
		 * This option allows you to wrap global decorators
402
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
403
		 *
404
		 *  client/html/email/payment/decorators/global = array( 'decorator1' )
405
		 *
406
		 * This would add the decorator named "decorator1" defined by
407
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
408
		 *
409
		 * @param array List of decorator names
410
		 * @since 2014.05
411
		 * @category Developer
412
		 * @see client/html/common/decorators/default
413
		 * @see client/html/email/payment/decorators/excludes
414
		 * @see client/html/email/payment/decorators/local
415
		 */
416
417
		/** client/html/email/payment/decorators/local
418
		 * Adds a list of local decorators only to the email payment html client
419
		 *
420
		 * Decorators extend the functionality of a class by adding new aspects
421
		 * (e.g. log what is currently done), executing the methods of the underlying
422
		 * class only in certain conditions (e.g. only for logged in users) or
423
		 * modify what is returned to the caller.
424
		 *
425
		 * This option allows you to wrap local decorators
426
		 * ("\Aimeos\Client\Html\Email\Decorator\*") around the html client.
427
		 *
428
		 *  client/html/email/payment/decorators/local = array( 'decorator2' )
429
		 *
430
		 * This would add the decorator named "decorator2" defined by
431
		 * "\Aimeos\Client\Html\Email\Decorator\Decorator2" only to the html client.
432
		 *
433
		 * @param array List of decorator names
434
		 * @since 2014.05
435
		 * @category Developer
436
		 * @see client/html/common/decorators/default
437
		 * @see client/html/email/payment/decorators/excludes
438
		 * @see client/html/email/payment/decorators/global
439
		 */
440
441
		return $this->createSubClient( 'email/payment/' . $type, $name );
442
	}
443
444
445
	/**
446
	 * Adds the given list of files as attachments to the mail message object
447
	 *
448
	 * @param \Aimeos\MW\Mail\Message\Iface $msg Mail message
449
	 * @param array $files List of absolute file paths
450
	 */
451
	protected function attachs( \Aimeos\MW\Mail\Message\Iface $msg, array $files )
452
	{
453
		foreach( $files as $filename )
454
		{
455
			if( ( $content = @file_get_contents( $filename ) ) === false ) {
456
				throw new \Aimeos\Client\Html\Exception( sprintf( 'File "%1$s" doesn\'t exist', $filename ) );
457
			}
458
459
			if( class_exists( 'finfo' ) )
460
			{
461
				try
462
				{
463
					$finfo = new \finfo( FILEINFO_MIME_TYPE );
464
					$mimetype = $finfo->file( $filename );
465
				}
466
				catch( \Exception $e )
467
				{
468
					throw new \Aimeos\Client\Html\Exception( $e->getMessage() );
469
				}
470
			}
471
			else if( function_exists( 'mime_content_type' ) )
472
			{
473
				$mimetype = mime_content_type( $filename );
474
			}
475
			else
476
			{
477
				$mimetype = 'application/binary';
478
			}
479
480
			$msg->attach( $content, $mimetype, basename( $filename ) );
481
		}
482
	}
483
484
485
	/**
486
	 * Returns the list of sub-client names configured for the client.
487
	 *
488
	 * @return array List of HTML client names
489
	 */
490
	protected function getSubClientNames() : array
491
	{
492
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
493
	}
494
495
496
	/**
497
	 * Sets the necessary parameter values in the view.
498
	 *
499
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
500
	 * @param array &$tags Result array for the list of tags that are associated to the output
501
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
502
	 * @return \Aimeos\MW\View\Iface Modified view object
503
	 */
504
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
505
	{
506
		$addr = $view->get( 'extAddressItem' );
507
		$list = [
508
			/// E-mail intro with first name (%1$s) and last name (%2$s)
509
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_UNKNOWN => $view->translate( 'client', 'Dear %1$s %2$s' ),
510
			/// E-mail intro with first name (%1$s) and last name (%2$s)
511
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR => $view->translate( 'client', 'Dear Mr %1$s %2$s' ),
512
			/// E-mail intro with first name (%1$s) and last name (%2$s)
513
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MS => $view->translate( 'client', 'Dear Ms %1$s %2$s' ),
514
		];
515
516
		if( $addr && isset( $list[$addr->getSalutation()] ) ) {
517
			$view->emailIntro = sprintf( $list[$addr->getSalutation()], $addr->getFirstName(), $addr->getLastName() );
518
		} else {
519
			$view->emailIntro = $view->translate( 'client', 'Dear customer' );
520
		}
521
522
523
		$key = 'pay:' . $view->extOrderItem->getStatusPayment();
524
		$status = $view->translate( 'mshop/code', $key );
525
526
		/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
527
		$msg = $view->translate( 'client', 'Thank you for your order %1$s from %2$s.' );
528
529
		switch( $view->extOrderItem->getStatusPayment() )
530
		{
531
			case 3:
532
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
533
				$msg = $view->translate( 'client', 'The payment for your order %1$s from %2$s has been refunded.' );
534
				break;
535
			case 4:
536
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
537
				$msg .= "\n" . $view->translate( 'client', 'The order is pending until we receive the final payment. If you\'ve chosen to pay in advance, please transfer the money to our bank account with the order ID %1$s as reference.' );
538
				break;
539
			case 6:
540
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
541
				$msg .= "\n" . $view->translate( 'client', 'We have received your payment, and will take care of your order immediately.' );
542
				break;
543
		}
544
545
		$date = date_create( $view->extOrderItem->getTimeCreated() )->format( $view->translate( 'client', 'Y-m-d' ) );
546
		$view->message = sprintf( $msg, $view->extOrderItem->getOrderNumber(), $date, $status );
547
548
		$pricefmt = $view->translate( 'client/code', 'price:default' );
549
		/// Price format with price value (%1$s) and currency (%2$s)
550
		$view->priceFormat = $pricefmt !== 'price:default' ? $pricefmt : $view->translate( 'client', '%1$s %2$s' );
551
552
553
		return parent::data( $view, $tags, $expire );
554
	}
555
}
556