Completed
Push — master ( b9f1e5...5ff555 )
by Aimeos
11:02
created

Standard::addAttachments()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
cc 6
nc 7
nop 2
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-2021
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 getBody( string $uid = '' ) : string
102
	{
103
		$view = $this->getObject()->addData( $this->getView() );
104
105
		$content = '';
106
		foreach( $this->getSubClients() as $subclient ) {
107
			$content .= $subclient->setView( $view )->getBody( $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->addAttachments( $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 with the string "standard" replaced 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, "standard"
143
		 * should be replaced 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->getPaymentStatus();
160
		$default = array( 'email/payment/' . $status . '/body-standard', 'email/payment/body-standard' );
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 getHeader( string $uid = '' ) : ?string
173
	{
174
		$view = $this->getObject()->addData( $this->getView() );
175
176
		$content = '';
177
		foreach( $this->getSubClients() as $subclient ) {
178
			$content .= $subclient->setView( $view )->getHeader( $uid );
179
		}
180
		$view->paymentHeader = $content;
181
182
183
		$addr = $view->extAddressItem;
184
185
		$msg = $view->mail();
186
		$msg->addHeader( 'X-MailGenerator', 'Aimeos' );
187
		$msg->addTo( $addr->getEMail(), $addr->getFirstName() . ' ' . $addr->getLastName() );
188
189
190
		/** client/html/email/from-name
191
		 * @see client/html/email/payment/from-email
192
		 */
193
		$fromName = $view->config( 'client/html/email/from-name' );
194
195
		/** client/html/email/payment/from-name
196
		 * Name used when sending payment e-mails
197
		 *
198
		 * The name of the person or e-mail account that is used for sending all
199
		 * shop related payment e-mails to customers. This configuration option
200
		 * overwrite the name set in "client/html/email/from-name".
201
		 *
202
		 * @param string Name shown in the e-mail
203
		 * @since 2014.03
204
		 * @category User
205
		 * @see client/html/email/from-name
206
		 * @see client/html/email/from-email
207
		 * @see client/html/email/reply-email
208
		 * @see client/html/email/bcc-email
209
		 */
210
		$fromNamePayment = $view->config( 'client/html/email/payment/from-name', $fromName );
211
212
		/** client/html/email/from-email
213
		 * @see client/html/email/payment/from-email
214
		 */
215
		$fromEmail = $view->config( 'client/html/email/from-email' );
216
217
		/** client/html/email/payment/from-email
218
		 * E-Mail address used when sending payment e-mails
219
		 *
220
		 * The e-mail address of the person or account that is used for sending
221
		 * all shop related payment emails to customers. This configuration option
222
		 * overwrites the e-mail address set via "client/html/email/from-email".
223
		 *
224
		 * @param string E-mail address
225
		 * @since 2014.03
226
		 * @category User
227
		 * @see client/html/email/payment/from-name
228
		 * @see client/html/email/from-email
229
		 * @see client/html/email/reply-email
230
		 * @see client/html/email/bcc-email
231
		 */
232
		if( ( $fromEmailPayment = $view->config( 'client/html/email/payment/from-email', $fromEmail ) ) != null ) {
233
			$msg->addFrom( $fromEmailPayment, $fromNamePayment );
234
		}
235
236
237
		/** client/html/email/reply-name
238
		 * @see client/html/email/payment/reply-email
239
		 */
240
		$replyName = $view->config( 'client/html/email/reply-name', $fromName );
241
242
		/** client/html/email/payment/reply-name
243
		 * Recipient name displayed when the customer replies to payment e-mails
244
		 *
245
		 * The name of the person or e-mail account the customer should
246
		 * reply to in case of payment related questions or problems. This
247
		 * configuration option overwrites the name set via
248
		 * "client/html/email/reply-name".
249
		 *
250
		 * @param string Name shown in the e-mail
251
		 * @since 2014.03
252
		 * @category User
253
		 * @see client/html/email/payment/reply-email
254
		 * @see client/html/email/reply-name
255
		 * @see client/html/email/reply-email
256
		 * @see client/html/email/from-email
257
		 * @see client/html/email/bcc-email
258
		 */
259
		$replyNamePayment = $view->config( 'client/html/email/payment/reply-name', $replyName );
260
261
		/** client/html/email/reply-email
262
		 * @see client/html/email/payment/reply-email
263
		 */
264
		$replyEmail = $view->config( 'client/html/email/reply-email', $fromEmail );
265
266
		/** client/html/email/payment/reply-email
267
		 * E-Mail address used by the customer when replying to payment e-mails
268
		 *
269
		 * The e-mail address of the person or e-mail account the customer
270
		 * should reply to in case of payment related questions or problems.
271
		 * This configuration option overwrites the e-mail address set via
272
		 * "client/html/email/reply-email".
273
		 *
274
		 * @param string E-mail address
275
		 * @since 2014.03
276
		 * @category User
277
		 * @see client/html/email/payment/reply-name
278
		 * @see client/html/email/reply-email
279
		 * @see client/html/email/from-email
280
		 * @see client/html/email/bcc-email
281
		 */
282
		if( ( $replyEmailPayment = $view->config( 'client/html/email/payment/reply-email', $replyEmail ) ) != null ) {
283
			$msg->addReplyTo( $replyEmailPayment, $replyNamePayment );
284
		}
285
286
287
		/** client/html/email/bcc-email
288
		 * @see client/html/email/payment/bcc-email
289
		 */
290
		$bccEmail = $view->config( 'client/html/email/bcc-email' );
291
292
		/** client/html/email/payment/bcc-email
293
		 * E-Mail address all payment e-mails should be also sent to
294
		 *
295
		 * Using this option you can send a copy of all payment related e-mails
296
		 * to a second e-mail account. This can be handy for testing and checking
297
		 * the e-mails sent to customers.
298
		 *
299
		 * It also allows shop owners with a very small volume of orders to be
300
		 * notified about payment changes. Be aware that this isn't useful if the
301
		 * order volumne is high or has peeks!
302
		 *
303
		 * This configuration option overwrites the e-mail address set via
304
		 * "client/html/email/bcc-email".
305
		 *
306
		 * @param string|array E-mail address or list of e-mail addresses
307
		 * @since 2014.03
308
		 * @category User
309
		 * @category Developer
310
		 * @see client/html/email/bcc-email
311
		 * @see client/html/email/reply-email
312
		 * @see client/html/email/from-email
313
		 */
314
		if( ( $bccEmailPayment = $view->config( 'client/html/email/payment/bcc-email', $bccEmail ) ) != null )
315
		{
316
			foreach( (array) $bccEmailPayment as $emailAddr ) {
317
				$msg->addBcc( $emailAddr );
318
			}
319
		}
320
321
322
		/** client/html/email/payment/template-header
323
		 * Relative path to the HTML header template of the email payment client.
324
		 *
325
		 * The template file contains the HTML code and processing instructions
326
		 * to generate the HTML code that is inserted into the HTML page header
327
		 * of the rendered page in the frontend. The configuration string is the
328
		 * path to the template file relative to the templates directory (usually
329
		 * in client/html/templates).
330
		 *
331
		 * You can overwrite the template file configuration in extensions and
332
		 * provide alternative templates. These alternative templates should be
333
		 * named like the default one but with the string "standard" replaced by
334
		 * an unique name. You may use the name of your project for this. If
335
		 * you've implemented an alternative client class as well, "standard"
336
		 * should be replaced by the name of the new class.
337
		 *
338
		 * The email payment HTML client allows to use a different template for
339
		 * each payment status value. You can create a template for each payment
340
		 * status and store it in the "email/payment/<status number>/" directory
341
		 * below the "templates" directory (usually in client/html/templates). If no
342
		 * specific layout template is found, the common template in the
343
		 * "email/payment/" directory is used.
344
		 *
345
		 * @param string Relative path to the template creating code for the HTML page head
346
		 * @since 2014.03
347
		 * @category Developer
348
		 * @see client/html/email/payment/template-body
349
		 */
350
		$tplconf = 'client/html/email/payment/template-header';
351
352
		$status = $view->extOrderItem->getPaymentStatus();
353
		$default = array( 'email/payment/' . $status . '/header-standard', 'email/payment/header-standard' );
354
355
		return $view->render( $view->config( $tplconf, $default ) ); ;
356
	}
357
358
359
	/**
360
	 * Returns the sub-client given by its name.
361
	 *
362
	 * @param string $type Name of the client type
363
	 * @param string|null $name Name of the sub-client (Default if null)
364
	 * @return \Aimeos\Client\Html\Iface Sub-client object
365
	 */
366
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
367
	{
368
		/** client/html/email/payment/decorators/excludes
369
		 * Excludes decorators added by the "common" option from the email payment html client
370
		 *
371
		 * Decorators extend the functionality of a class by adding new aspects
372
		 * (e.g. log what is currently done), executing the methods of the underlying
373
		 * class only in certain conditions (e.g. only for logged in users) or
374
		 * modify what is returned to the caller.
375
		 *
376
		 * This option allows you to remove a decorator added via
377
		 * "client/html/common/decorators/default" before they are wrapped
378
		 * around the html client.
379
		 *
380
		 *  client/html/email/payment/decorators/excludes = array( 'decorator1' )
381
		 *
382
		 * This would remove the decorator named "decorator1" from the list of
383
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
384
		 * "client/html/common/decorators/default" to the html client.
385
		 *
386
		 * @param array List of decorator names
387
		 * @since 2014.05
388
		 * @category Developer
389
		 * @see client/html/common/decorators/default
390
		 * @see client/html/email/payment/decorators/global
391
		 * @see client/html/email/payment/decorators/local
392
		 */
393
394
		/** client/html/email/payment/decorators/global
395
		 * Adds a list of globally available decorators only to the email payment html client
396
		 *
397
		 * Decorators extend the functionality of a class by adding new aspects
398
		 * (e.g. log what is currently done), executing the methods of the underlying
399
		 * class only in certain conditions (e.g. only for logged in users) or
400
		 * modify what is returned to the caller.
401
		 *
402
		 * This option allows you to wrap global decorators
403
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
404
		 *
405
		 *  client/html/email/payment/decorators/global = array( 'decorator1' )
406
		 *
407
		 * This would add the decorator named "decorator1" defined by
408
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
409
		 *
410
		 * @param array List of decorator names
411
		 * @since 2014.05
412
		 * @category Developer
413
		 * @see client/html/common/decorators/default
414
		 * @see client/html/email/payment/decorators/excludes
415
		 * @see client/html/email/payment/decorators/local
416
		 */
417
418
		/** client/html/email/payment/decorators/local
419
		 * Adds a list of local decorators only to the email payment html client
420
		 *
421
		 * Decorators extend the functionality of a class by adding new aspects
422
		 * (e.g. log what is currently done), executing the methods of the underlying
423
		 * class only in certain conditions (e.g. only for logged in users) or
424
		 * modify what is returned to the caller.
425
		 *
426
		 * This option allows you to wrap local decorators
427
		 * ("\Aimeos\Client\Html\Email\Decorator\*") around the html client.
428
		 *
429
		 *  client/html/email/payment/decorators/local = array( 'decorator2' )
430
		 *
431
		 * This would add the decorator named "decorator2" defined by
432
		 * "\Aimeos\Client\Html\Email\Decorator\Decorator2" only to the html client.
433
		 *
434
		 * @param array List of decorator names
435
		 * @since 2014.05
436
		 * @category Developer
437
		 * @see client/html/common/decorators/default
438
		 * @see client/html/email/payment/decorators/excludes
439
		 * @see client/html/email/payment/decorators/global
440
		 */
441
442
		return $this->createSubClient( 'email/payment/' . $type, $name );
443
	}
444
445
446
	/**
447
	 * Adds the given list of files as attachments to the mail message object
448
	 *
449
	 * @param \Aimeos\MW\Mail\Message\Iface $msg Mail message
450
	 * @param array $files List of absolute file paths
451
	 */
452
	protected function addAttachments( \Aimeos\MW\Mail\Message\Iface $msg, array $files )
453
	{
454
		foreach( $files as $filename )
455
		{
456
			if( ( $content = @file_get_contents( $filename ) ) === false ) {
457
				throw new \Aimeos\Client\Html\Exception( sprintf( 'File "%1$s" doesn\'t exist', $filename ) );
458
			}
459
460
			if( class_exists( 'finfo' ) )
461
			{
462
				try
463
				{
464
					$finfo = new \finfo( FILEINFO_MIME_TYPE );
465
					$mimetype = $finfo->file( $filename );
466
				}
467
				catch( \Exception $e )
468
				{
469
					throw new \Aimeos\Client\Html\Exception( $e->getMessage() );
470
				}
471
			}
472
			else if( function_exists( 'mime_content_type' ) )
473
			{
474
				$mimetype = mime_content_type( $filename );
475
			}
476
			else
477
			{
478
				$mimetype = 'application/binary';
479
			}
480
481
			$msg->addAttachment( $content, $mimetype, basename( $filename ) );
482
		}
483
	}
484
485
486
	/**
487
	 * Returns the list of sub-client names configured for the client.
488
	 *
489
	 * @return array List of HTML client names
490
	 */
491
	protected function getSubClientNames() : array
492
	{
493
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
494
	}
495
496
497
	/**
498
	 * Sets the necessary parameter values in the view.
499
	 *
500
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
501
	 * @param array &$tags Result array for the list of tags that are associated to the output
502
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
503
	 * @return \Aimeos\MW\View\Iface Modified view object
504
	 */
505
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
506
	{
507
		$addr = $view->get( 'extAddressItem' );
508
		$list = array(
509
			/// E-mail intro with first name (%1$s) and last name (%2$s)
510
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR => $view->translate( 'client', 'Dear Mr %1$s %2$s' ),
511
			/// E-mail intro with first name (%1$s) and last name (%2$s)
512
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MS => $view->translate( 'client', 'Dear Ms %1$s %2$s' ),
513
		);
514
515
		if( $addr && isset( $list[$addr->getSalutation()] ) ) {
516
			$view->emailIntro = sprintf( $list[$addr->getSalutation()], $addr->getFirstName(), $addr->getLastName() );
517
		} elseif( $addr ) {
518
			$view->emailIntro = sprintf( 'Dear %1$s %2$s', $addr->getFirstName(), $addr->getLastName() );
519
		} else {
520
			$view->emailIntro = $view->translate( 'client', 'Dear customer' );
521
		}
522
523
524
		$key = 'pay:' . $view->extOrderItem->getPaymentStatus();
525
		$status = $view->translate( 'mshop/code', $key );
526
527
		/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
528
		$msg = $view->translate( 'client', 'Thank you for your order %1$s from %2$s.' );
529
530
		switch( $view->extOrderItem->getPaymentStatus() )
531
		{
532
			case 3:
533
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
534
				$msg = $view->translate( 'client', 'The payment for your order %1$s from %2$s has been refunded.' );
535
				break;
536
			case 4:
537
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
538
				$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.' );
539
				break;
540
			case 6:
541
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
542
				$msg .= "\n" . $view->translate( 'client', 'We have received your payment, and will take care of your order immediately.' );
543
				break;
544
		}
545
546
		$date = date_create( $view->extOrderItem->getTimeCreated() )->format( $view->translate( 'client', 'Y-m-d' ) );
547
		$view->message = sprintf( $msg, $view->extOrderItem->getId(), $date, $status );
548
549
		$pricefmt = $view->translate( 'client/code', 'price:default' );
550
		/// Price format with price value (%1$s) and currency (%2$s)
551
		$view->priceFormat = $pricefmt !== 'price:default' ? $pricefmt : $view->translate( 'client', '%1$s %2$s' );
552
553
554
		return parent::addData( $view, $tags, $expire );
555
	}
556
}
557