Passed
Branch master (4f99e3)
by Aimeos
04:48
created

Standard::addAttachments()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 7
nop 2
dl 0
loc 30
rs 9.2222
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, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Email\Delivery;
13
14
15
/**
16
 * Default implementation of delivery 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/delivery/standard/subparts
26
	 * List of HTML sub-clients rendered within the email delivery 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/delivery/standard/subparts';
59
60
	/** client/html/email/delivery/text/name
61
	 * Name of the text part used by the email delivery client implementation
62
	 *
63
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Delivery\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/delivery/html/name
72
	 * Name of the html part used by the email delivery client implementation
73
	 *
74
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Delivery\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
	private $subPartNames = array( 'text', 'html' );
82
83
84
	/**
85
	 * Returns the HTML code for insertion into the body.
86
	 *
87
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
88
	 * @return string HTML code
89
	 */
90
	public function getBody( $uid = '' )
91
	{
92
		$view = $this->getObject()->addData( $this->getView() );
93
94
		$content = '';
95
		foreach( $this->getSubClients() as $subclient ) {
96
			$content .= $subclient->setView( $view )->getBody( $uid );
97
		}
98
		$view->deliveryBody = $content;
99
100
101
		/** client/html/email/delivery/attachments
102
		 * List of file paths whose content should be attached to all delivery e-mails
103
		 *
104
		 * This configuration option allows you to add files to the e-mails that are
105
		 * sent to the customer when the delivery status changes. These files can't be
106
		 * customer specific.
107
		 *
108
		 * @param array List of absolute file paths
109
		 * @since 2016.10
110
		 * @category Developer
111
		 * @category User
112
		 * @see client/html/email/payment/attachments
113
		 */
114
		$files = $view->config( 'client/html/email/delivery/attachments', [] );
115
116
		$this->addAttachments( $view->mail(), $files );
117
118
119
		/** client/html/email/delivery/standard/template-body
120
		 * Relative path to the text body template of the email delivery client.
121
		 *
122
		 * The template file contains the text and processing instructions
123
		 * to generate the result shown in the e-mail. The
124
		 * configuration string is the path to the template file relative
125
		 * to the templates directory (usually in client/html/templates).
126
		 *
127
		 * You can overwrite the template file configuration in extensions and
128
		 * provide alternative templates. These alternative templates should be
129
		 * named like the default one but with the string "standard" replaced by
130
		 * an unique name. You may use the name of your project for this. If
131
		 * you've implemented an alternative client class as well, "standard"
132
		 * should be replaced by the name of the new class.
133
		 *
134
		 * The email delivery text client allows to use a different template for
135
		 * each delivery status value. You can create a template for each delivery
136
		 * status and store it in the "email/delivery/<status number>/" directory
137
		 * below the "templates" directory (usually in client/html/templates). If no
138
		 * specific layout template is found, the common template in the
139
		 * "email/delivery/" directory is used.
140
		 *
141
		 * @param string Relative path to the template creating code for the e-mail body
142
		 * @since 2014.03
143
		 * @category Developer
144
		 * @see client/html/email/delivery/standard/template-header
145
		 */
146
		$tplconf = 'client/html/email/delivery/standard/template-body';
147
148
		$status = $view->extOrderItem->getDeliveryStatus();
149
		$default = array( 'email/delivery/' . $status . '/body-standard', 'email/delivery/body-standard' );
150
151
		return $view->render( $view->config( $tplconf, $default ) );
152
	}
153
154
155
	/**
156
	 * Returns the HTML string for insertion into the header.
157
	 *
158
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
159
	 * @return string|null String including HTML tags for the header on error
160
	 */
161
	public function getHeader( $uid = '' )
162
	{
163
		$view = $this->getObject()->addData( $this->getView() );
164
165
		$content = '';
166
		foreach( $this->getSubClients() as $subclient ) {
167
			$content .= $subclient->setView( $view )->getHeader( $uid );
168
		}
169
		$view->deliveryHeader = $content;
170
171
172
		$addr = $view->extAddressItem;
173
		$billAddr = $view->extOrderBaseItem->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
174
175
		$msg = $view->mail();
176
		$msg->addHeader( 'X-MailGenerator', 'Aimeos' );
177
		$msg->addTo( $addr->getEMail(), $addr->getFirstName() . ' ' . $addr->getLastName() );
178
179
		if( $billAddr->getEMail() != $addr->getEmail() ) {
180
			$msg->addCc( $billAddr->getEMail(), $billAddr->getFirstName() . ' ' . $billAddr->getLastName() );
181
		}
182
183
		/** client/html/email/from-name
184
		 * Name used when sending e-mails
185
		 *
186
		 * The name of the person or e-mail account that is used for sending all
187
		 * shop related emails to customers.
188
		 *
189
		 * @param string Name shown in the e-mail
190
		 * @since 2014.03
191
		 * @category User
192
		 * @see client/html/email/delivery/from-name
193
		 * @see client/html/email/from-email
194
		 * @see client/html/email/reply-email
195
		 * @see client/html/email/bcc-email
196
		 */
197
		$fromName = $view->config( 'client/html/email/from-name' );
198
199
		/** client/html/email/delivery/from-name
200
		 * Name used when sending delivery e-mails
201
		 *
202
		 * The name of the person or e-mail account that is used for sending all
203
		 * shop related delivery e-mails to customers. This configuration option
204
		 * overwrites the name set in "client/html/email/from-name".
205
		 *
206
		 * @param string Name shown in the e-mail
207
		 * @since 2014.03
208
		 * @category User
209
		 * @see client/html/email/from-name
210
		 * @see client/html/email/from-email
211
		 * @see client/html/email/reply-email
212
		 * @see client/html/email/bcc-email
213
		 */
214
		$fromNameDelivery = $view->config( 'client/html/email/delivery/from-name', $fromName );
215
216
		/** client/html/email/from-email
217
		 * E-Mail address used when sending e-mails
218
		 *
219
		 * The e-mail address of the person or account that is used for sending
220
		 * all shop related emails to customers.
221
		 *
222
		 * @param string E-mail address
223
		 * @since 2014.03
224
		 * @category User
225
		 * @see client/html/email/from-name
226
		 * @see client/html/email/delivery/from-email
227
		 * @see client/html/email/reply-email
228
		 * @see client/html/email/bcc-email
229
		 */
230
		$fromEmail = $view->config( 'client/html/email/from-email' );
231
232
		/** client/html/email/delivery/from-email
233
		 * E-Mail address used when sending delivery e-mails
234
		 *
235
		 * The e-mail address of the person or account that is used for sending
236
		 * all shop related delivery emails to customers. This configuration option
237
		 * overwrites the e-mail address set via "client/html/email/from-email".
238
		 *
239
		 * @param string E-mail address
240
		 * @since 2014.03
241
		 * @category User
242
		 * @see client/html/email/delivery/from-name
243
		 * @see client/html/email/from-email
244
		 * @see client/html/email/reply-email
245
		 * @see client/html/email/bcc-email
246
		 */
247
		if( ( $fromEmailDelivery = $view->config( 'client/html/email/delivery/from-email', $fromEmail ) ) != null ) {
248
			$msg->addFrom( $fromEmailDelivery, $fromNameDelivery );
249
		}
250
251
252
		/** client/html/email/reply-name
253
		 * Recipient name displayed when the customer replies to e-mails
254
		 *
255
		 * The name of the person or e-mail account the customer should
256
		 * reply to in case of questions or problems. If no reply name is
257
		 * configured, the name person or e-mail account set via
258
		 * "client/html/email/from-name" is used.
259
		 *
260
		 * @param string Name shown in the e-mail
261
		 * @since 2014.03
262
		 * @category User
263
		 * @see client/html/email/reply-email
264
		 * @see client/html/email/delivery/reply-email
265
		 * @see client/html/email/from-email
266
		 * @see client/html/email/from-name
267
		 * @see client/html/email/bcc-email
268
		 */
269
		$replyName = $view->config( 'client/html/email/reply-name', $fromName );
270
271
		/** client/html/email/delivery/reply-name
272
		 * Recipient name displayed when the customer replies to delivery e-mails
273
		 *
274
		 * The name of the person or e-mail account the customer should
275
		 * reply to in case of questions or problems. This configuration option
276
		 * overwrites the name set via "client/html/email/reply-name".
277
		 *
278
		 * @param string Name shown in the e-mail
279
		 * @since 2014.03
280
		 * @category User
281
		 * @see client/html/email/delivery/reply-email
282
		 * @see client/html/email/reply-name
283
		 * @see client/html/email/reply-email
284
		 * @see client/html/email/from-email
285
		 * @see client/html/email/bcc-email
286
		 */
287
		$replyNameDelivery = $view->config( 'client/html/email/delivery/reply-name', $replyName );
288
289
		/** client/html/email/reply-email
290
		 * E-Mail address used by the customer when replying to e-mails
291
		 *
292
		 * The e-mail address of the person or e-mail account the customer
293
		 * should reply to in case of questions or problems.
294
		 *
295
		 * @param string E-mail address
296
		 * @since 2014.03
297
		 * @category User
298
		 * @see client/html/email/reply-name
299
		 * @see client/html/email/delivery/reply-email
300
		 * @see client/html/email/from-email
301
		 * @see client/html/email/bcc-email
302
		 */
303
		$replyEmail = $view->config( 'client/html/email/reply-email', $fromEmail );
304
305
		/** client/html/email/delivery/reply-email
306
		 * E-Mail address used by the customer when replying to delivery e-mails
307
		 *
308
		 * The e-mail address of the person or e-mail account the customer
309
		 * should reply to in case of questions or problems. This configuration
310
		 * option overwrites the e-mail address set via "client/html/email/reply-email".
311
		 *
312
		 * @param string E-mail address
313
		 * @since 2014.03
314
		 * @category User
315
		 * @see client/html/email/delivery/reply-name
316
		 * @see client/html/email/reply-email
317
		 * @see client/html/email/from-email
318
		 * @see client/html/email/bcc-email
319
		 */
320
		if( ( $replyEmailDelivery = $view->config( 'client/html/email/delivery/reply-email', $replyEmail ) ) != null ) {
321
			$msg->addReplyTo( $replyEmailDelivery, $replyNameDelivery );
322
		}
323
324
325
		/** client/html/email/bcc-email
326
		 * E-Mail address all e-mails should be also sent to
327
		 *
328
		 * Using this option you can send a copy of all shop related e-mails to
329
		 * a second e-mail account. This can be handy for testing and checking
330
		 * the e-mails sent to customers.
331
		 *
332
		 * It also allows shop owners with a very small volume of orders to be
333
		 * notified about new orders. Be aware that this isn't useful if the
334
		 * order volumne is high or has peeks!
335
		 *
336
		 * @param string E-mail address
337
		 * @since 2014.03
338
		 * @category User
339
		 * @category Developer
340
		 * @see client/html/email/delivery/bcc-email
341
		 * @see client/html/email/reply-email
342
		 * @see client/html/email/from-email
343
		 */
344
		$bccEmail = $view->config( 'client/html/email/bcc-email' );
345
346
		/** client/html/email/delivery/bcc-email
347
		 * E-Mail address all delivery e-mails should be also sent to
348
		 *
349
		 * Using this option you can send a copy of all delivery related e-mails
350
		 * to a second e-mail account. This can be handy for testing and checking
351
		 * the e-mails sent to customers.
352
		 *
353
		 * It also allows shop owners with a very small volume of orders to be
354
		 * notified about new orders. Be aware that this isn't useful if the
355
		 * order volumne is high or has peeks!
356
		 *
357
		 * This configuration option overwrites the e-mail address set via
358
		 * "client/html/email/bcc-email".
359
		 *
360
		 * @param string|array E-mail address or list of e-mail addresses
361
		 * @since 2014.03
362
		 * @category User
363
		 * @category Developer
364
		 * @see client/html/email/bcc-email
365
		 * @see client/html/email/reply-email
366
		 * @see client/html/email/from-email
367
		 */
368
		if( ( $bccEmailDelivery = $view->config( 'client/html/email/delivery/bcc-email', $bccEmail ) ) != null )
369
		{
370
			foreach( (array) $bccEmailDelivery as $emailAddr ) {
371
				$msg->addBcc( $emailAddr );
372
			}
373
		}
374
375
376
		/** client/html/email/delivery/standard/template-header
377
		 * Relative path to the text header template of the email delivery client.
378
		 *
379
		 * The template file contains the text and processing instructions
380
		 * to generate the text that is inserted into the header
381
		 * of the e-mail. The configuration string is the
382
		 * path to the template file relative to the templates directory (usually
383
		 * in client/html/templates).
384
		 *
385
		 * You can overwrite the template file configuration in extensions and
386
		 * provide alternative templates. These alternative templates should be
387
		 * named like the default one but with the string "standard" replaced by
388
		 * an unique name. You may use the name of your project for this. If
389
		 * you've implemented an alternative client class as well, "standard"
390
		 * should be replaced by the name of the new class.
391
		 *
392
		 * The email payment text client allows to use a different template for
393
		 * each payment status value. You can create a template for each payment
394
		 * status and store it in the "email/payment/<status number>/" directory
395
		 * below the "templates" directory (usually in client/html/templates). If no
396
		 * specific layout template is found, the common template in the
397
		 * "email/payment/" directory is used.
398
		 *
399
		 * @param string Relative path to the template creating code for the e-mail header
400
		 * @since 2014.03
401
		 * @category Developer
402
		 * @see client/html/email/delivery/standard/template-body
403
		 */
404
		$tplconf = 'client/html/email/delivery/standard/template-header';
405
406
		$status = $view->extOrderItem->getDeliveryStatus();
407
		$default = array( 'email/delivery/' . $status . '/header-standard', 'email/delivery/header-standard' );
408
409
		return $view->render( $view->config( $tplconf, $default ) ); ;
410
	}
411
412
413
	/**
414
	 * Returns the sub-client given by its name.
415
	 *
416
	 * @param string $type Name of the client type
417
	 * @param string|null $name Name of the sub-client (Default if null)
418
	 * @return \Aimeos\Client\Html\Iface Sub-client object
419
	 */
420
	public function getSubClient( $type, $name = null )
421
	{
422
		/** client/html/email/delivery/decorators/excludes
423
		 * Excludes decorators added by the "common" option from the email delivery html client
424
		 *
425
		 * Decorators extend the functionality of a class by adding new aspects
426
		 * (e.g. log what is currently done), executing the methods of the underlying
427
		 * class only in certain conditions (e.g. only for logged in users) or
428
		 * modify what is returned to the caller.
429
		 *
430
		 * This option allows you to remove a decorator added via
431
		 * "client/html/common/decorators/default" before they are wrapped
432
		 * around the html client.
433
		 *
434
		 *  client/html/email/delivery/decorators/excludes = array( 'decorator1' )
435
		 *
436
		 * This would remove the decorator named "decorator1" from the list of
437
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
438
		 * "client/html/common/decorators/default" to the html client.
439
		 *
440
		 * @param array List of decorator names
441
		 * @since 2014.05
442
		 * @category Developer
443
		 * @see client/html/common/decorators/default
444
		 * @see client/html/email/delivery/decorators/global
445
		 * @see client/html/email/delivery/decorators/local
446
		 */
447
448
		/** client/html/email/delivery/decorators/global
449
		 * Adds a list of globally available decorators only to the email delivery html client
450
		 *
451
		 * Decorators extend the functionality of a class by adding new aspects
452
		 * (e.g. log what is currently done), executing the methods of the underlying
453
		 * class only in certain conditions (e.g. only for logged in users) or
454
		 * modify what is returned to the caller.
455
		 *
456
		 * This option allows you to wrap global decorators
457
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
458
		 *
459
		 *  client/html/email/delivery/decorators/global = array( 'decorator1' )
460
		 *
461
		 * This would add the decorator named "decorator1" defined by
462
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
463
		 *
464
		 * @param array List of decorator names
465
		 * @since 2014.05
466
		 * @category Developer
467
		 * @see client/html/common/decorators/default
468
		 * @see client/html/email/delivery/decorators/excludes
469
		 * @see client/html/email/delivery/decorators/local
470
		 */
471
472
		/** client/html/email/delivery/decorators/local
473
		 * Adds a list of local decorators only to the email delivery html client
474
		 *
475
		 * Decorators extend the functionality of a class by adding new aspects
476
		 * (e.g. log what is currently done), executing the methods of the underlying
477
		 * class only in certain conditions (e.g. only for logged in users) or
478
		 * modify what is returned to the caller.
479
		 *
480
		 * This option allows you to wrap local decorators
481
		 * ("\Aimeos\Client\Html\Email\Decorator\*") around the html client.
482
		 *
483
		 *  client/html/email/delivery/decorators/local = array( 'decorator2' )
484
		 *
485
		 * This would add the decorator named "decorator2" defined by
486
		 * "\Aimeos\Client\Html\Email\Decorator\Decorator2" only to the html client.
487
		 *
488
		 * @param array List of decorator names
489
		 * @since 2014.05
490
		 * @category Developer
491
		 * @see client/html/common/decorators/default
492
		 * @see client/html/email/delivery/decorators/excludes
493
		 * @see client/html/email/delivery/decorators/global
494
		 */
495
496
		return $this->createSubClient( 'email/delivery/' . $type, $name );
497
	}
498
499
500
	/**
501
	 * Adds the given list of files as attachments to the mail message object
502
	 *
503
	 * @param \Aimeos\MW\Mail\Message\Iface $msg Mail message
504
	 * @param array $files List of absolute file paths
505
	 */
506
	protected function addAttachments( \Aimeos\MW\Mail\Message\Iface $msg, array $files )
507
	{
508
		foreach( $files as $filename )
509
		{
510
			if( ( $content = @file_get_contents( $filename ) ) === false ) {
511
				throw new \Aimeos\Client\Html\Exception( sprintf( 'File "%1$s" doesn\'t exist', $filename ) );
512
			}
513
514
			if( class_exists( 'finfo' ) )
515
			{
516
				try
517
				{
518
					$finfo = new \finfo( FILEINFO_MIME_TYPE );
519
					$mimetype = $finfo->file( $filename );
520
				}
521
				catch( \Exception $e )
522
				{
523
					throw new \Aimeos\Client\Html\Exception( $e->getMessage() );
524
				}
525
			}
526
			else if( function_exists( 'mime_content_type' ) )
527
			{
528
				$mimetype = mime_content_type( $filename );
529
			}
530
			else
531
			{
532
				$mimetype = 'application/binary';
533
			}
534
535
			$msg->addAttachment( $content, $mimetype, basename( $filename ) );
536
		}
537
	}
538
539
540
	/**
541
	 * Returns the list of sub-client names configured for the client.
542
	 *
543
	 * @return array List of HTML client names
544
	 */
545
	protected function getSubClientNames()
546
	{
547
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
548
	}
549
550
551
	/**
552
	 * Sets the necessary parameter values in the view.
553
	 *
554
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
555
	 * @param array &$tags Result array for the list of tags that are associated to the output
556
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
557
	 * @return \Aimeos\MW\View\Iface Modified view object
558
	 */
559
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], &$expire = null )
560
	{
561
		$salutations = array(
562
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR,
563
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MRS,
564
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MISS,
565
		);
566
567
		try
568
		{
569
			$salutation = '';
570
			$addr = $view->extAddressItem;
571
572
			if( in_array( $addr->getSalutation(), $salutations ) ) {
573
				$salutation = $view->translate( 'mshop/code', $addr->getSalutation() );
574
			}
575
576
			/// E-mail intro with salutation (%1$s), first name (%2$s) and last name (%3$s)
577
			$view->emailIntro = sprintf( $view->translate( 'client', 'Dear %1$s %2$s %3$s' ),
578
				$salutation, $addr->getFirstName(), $addr->getLastName()
579
			);
580
		}
581
		catch( \Exception $e )
582
		{
583
			$view->emailIntro = $view->translate( 'client/html/email', 'Dear Sir or Madam' );
584
		}
585
586
		return parent::addData( $view, $tags, $expire );
587
	}
588
}