Passed
Push — master ( 863c7f...5d3493 )
by Aimeos
03:44
created

Standard.php$0 ➔ type()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
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 Aimeos (aimeos.org), 2015-2024
6
 * @package Controller
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Order\Email\Payment;
12
13
14
/**
15
 * Order payment e-mail job controller.
16
 *
17
 * @package Controller
18
 * @subpackage Order
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/** controller/jobs/order/email/payment/name
25
	 * Class name of the used order email payment scheduler controller implementation
26
	 *
27
	 * Each default job controller 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 controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Jobs\Order\Email\Payment\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Order\Email\Payment\Mypayment
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/order/email/payment/name = Mypayment
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 "MyPayment"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 */
56
57
	/** controller/jobs/order/email/payment/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the order email payment controllers
59
	 *
60
	 * Decorators extend the functionality of a class by adding new aspects
61
	 * (e.g. log what is currently done), executing the methods of the underlying
62
	 * class only in certain conditions (e.g. only for logged in users) or
63
	 * modify what is returned to the caller.
64
	 *
65
	 * This option allows you to remove a decorator added via
66
	 * "controller/jobs/common/decorators/default" before they are wrapped
67
	 * around the job controller.
68
	 *
69
	 *  controller/jobs/order/email/payment/decorators/excludes = array( 'decorator1' )
70
	 *
71
	 * This would remove the decorator named "decorator1" from the list of
72
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
73
	 * "controller/jobs/common/decorators/default" to this job controller.
74
	 *
75
	 * @param array List of decorator names
76
	 * @since 2015.09
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/order/email/payment/decorators/global
79
	 * @see controller/jobs/order/email/payment/decorators/local
80
	 */
81
82
	/** controller/jobs/order/email/payment/decorators/global
83
	 * Adds a list of globally available decorators only to the order email payment controllers
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
92
	 *
93
	 *  controller/jobs/order/email/payment/decorators/global = array( 'decorator1' )
94
	 *
95
	 * This would add the decorator named "decorator1" defined by
96
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to this job controller.
97
	 *
98
	 * @param array List of decorator names
99
	 * @since 2015.09
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/order/email/payment/decorators/excludes
102
	 * @see controller/jobs/order/email/payment/decorators/local
103
	 */
104
105
	/** controller/jobs/order/email/payment/decorators/local
106
	 * Adds a list of local decorators only to the order email payment controllers
107
	 *
108
	 * Decorators extend the functionality of a class by adding new aspects
109
	 * (e.g. log what is currently done), executing the methods of the underlying
110
	 * class only in certain conditions (e.g. only for logged in users) or
111
	 * modify what is returned to the caller.
112
	 *
113
	 * This option allows you to wrap local decorators
114
	 * ("\Aimeos\Controller\Jobs\Order\Email\Payment\Decorator\*") around this job controller.
115
	 *
116
	 *  controller/jobs/order/email/payment/decorators/local = array( 'decorator2' )
117
	 *
118
	 * This would add the decorator named "decorator2" defined by
119
	 * "\Aimeos\Controller\Jobs\Order\Email\Payment\Decorator\Decorator2" only to this job
120
	 * controller.
121
	 *
122
	 * @param array List of decorator names
123
	 * @since 2015.09
124
	 * @see controller/jobs/common/decorators/default
125
	 * @see controller/jobs/order/email/payment/decorators/excludes
126
	 * @see controller/jobs/order/email/payment/decorators/global
127
	 */
128
129
130
	use \Aimeos\Controller\Jobs\Mail;
131
132
133
	/**
134
	 * Returns the localized name of the job.
135
	 *
136
	 * @return string Name of the job
137
	 */
138
	public function getName() : string
139
	{
140
		return $this->context()->translate( 'controller/jobs', 'Order payment related e-mails' );
141
	}
142
143
144
	/**
145
	 * Returns the localized description of the job.
146
	 *
147
	 * @return string Description of the job
148
	 */
149
	public function getDescription() : string
150
	{
151
		return $this->context()->translate( 'controller/jobs', 'Sends order confirmation or payment status update e-mails' );
152
	}
153
154
155
	/**
156
	 * Executes the job.
157
	 *
158
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
159
	 */
160
	public function run()
161
	{
162
		$context = $this->context();
163
		$manager = \Aimeos\MShop::create( $context, 'order' );
164
165
		foreach( $this->status() as $status )
166
		{
167
			$ref = ['order'] + $context->config()->get( 'mshop/order/manager/subdomains', [] );
168
			$filter = $this->filter( $manager->filter(), $status );
169
			$cursor = $manager->cursor( $filter );
170
171
			while( $items = $manager->iterate( $cursor, $ref ) ) {
172
				$this->notify( $items, $status );
173
			}
174
		}
175
	}
176
177
178
	/**
179
	 * Returns the address item from the order
180
	 *
181
	 * @param \Aimeos\MShop\Order\Item\Iface $basket Order including address items
182
	 * @return \Aimeos\MShop\Common\Item\Address\Iface Address item
183
	 * @throws \Aimeos\Controller\Jobs\Exception If no suitable address item is available
184
	 */
185
	protected function address( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Common\Item\Address\Iface
186
	{
187
		if( ( $addr = current( $basket->getAddress( 'payment' ) ) ) !== false && $addr->getEmail() ) {
188
			return $addr;
189
		};
190
191
		$msg = sprintf( 'No address with e-mail found in order base with ID "%1$s"', $basket->getId() );
192
		throw new \Aimeos\Controller\Jobs\Exception( $msg );
193
	}
194
195
196
	/**
197
	 * Adds the given list of files as attachments to the mail message object
198
	 *
199
	 * @param \Aimeos\Base\Mail\Message\Iface $msg Mail message
200
	 * @param array $files List of absolute file paths
201
	 */
202
	protected function attachments( \Aimeos\Base\Mail\Message\Iface $msg ) : \Aimeos\Base\Mail\Message\Iface
203
	{
204
		$context = $this->context();
205
		$fs = $context->fs();
206
207
		/** controller/jobs/order/email/payment/attachments
208
		 * List of file paths whose content should be attached to all payment e-mails
209
		 *
210
		 * This configuration option allows you to add files to the e-mails that are
211
		 * sent to the customer when the payment status changes, e.g. for the order
212
		 * confirmation e-mail. These files can't be customer specific.
213
		 *
214
		 * @param array List of absolute file paths
215
		 * @since 2016.10
216
		 * @see controller/jobs/order/email/delivery/attachments
217
		 */
218
		$files = $context->config()->get( 'controller/jobs/order/email/payment/attachments', [] );
219
220
		foreach( $files as $filepath )
221
		{
222
			if( $fs->has( $filepath ) ) {
223
				$msg->attach( $fs->read( $filepath ), basename( $filepath ) );
224
			}
225
		}
226
227
		return $msg;
228
	}
229
230
231
	/**
232
	 * Returns the PDF file name
233
	 *
234
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order item
235
	 * @return string PDF file name
236
	 */
237
	protected function filename( \Aimeos\MShop\Order\Item\Iface $order ) : string
238
	{
239
		return $this->context()->translate( 'controller/jobs', 'Invoice' ) . '-' . $order->getInvoiceNumber() . '.pdf';
240
	}
241
242
243
	/**
244
	 * Returns the filter for searching the appropriate orders
245
	 *
246
	 * @param \Aimeos\Base\Criteria\Iface $filter Order filter object
247
	 * @param int $status Delivery status value	to search for
248
	 * @return \Aimeos\Base\Criteria\Iface Filter object with conditions set
249
	 */
250
	protected function filter( \Aimeos\Base\Criteria\Iface $filter, int $status ) : \Aimeos\Base\Criteria\Iface
251
	{
252
		$limitDate = date( 'Y-m-d H:i:s', time() - $this->limit() * 86400 );
253
		$param = [$this->type(), (string) $status];
254
255
		$filter->add( $filter->and( [
256
			$filter->compare( '>=', 'order.mtime', $limitDate ),
257
			$filter->compare( '==', 'order.statuspayment', $status ),
258
			$filter->compare( '==', $filter->make( 'order:status', $param ), 0 ),
259
		] ) );
260
261
		return $filter;
262
	}
263
264
265
	/**
266
	 * Returns the number of days after no e-mail will be sent anymore
267
	 *
268
	 * @return int Number of days
269
	 */
270
	protected function limit() : int
271
	{
272
		/** controller/jobs/order/email/payment/limit-days
273
		 * Only send payment e-mails of orders that were created in the past within the configured number of days
274
		 *
275
		 * The payment e-mails are normally send immediately after the payment
276
		 * status has changed. This option prevents e-mails for old order from
277
		 * being send in case anything went wrong or an update failed to avoid
278
		 * confusion of customers.
279
		 *
280
		 * @param integer Number of days
281
		 * @since 2014.03
282
		 * @see controller/jobs/order/email/delivery/limit-days
283
		 * @see controller/jobs/service/delivery/process/limit-days
284
		 */
285
		return (int) $this->context()->config()->get( 'controller/jobs/order/email/payment/limit-days', 30 );
286
	}
287
288
289
	/**
290
	 * Sends the payment e-mail for the given orders
291
	 *
292
	 * @param \Aimeos\Map $items List of order items implementing \Aimeos\MShop\Order\Item\Iface with their IDs as keys
293
	 * @param int $status Delivery status value
294
	 */
295
	protected function notify( \Aimeos\Map $items, int $status )
296
	{
297
		$context = $this->context();
298
		$sites = $this->sites( $items->getSiteId()->unique() );
299
300
		foreach( $items as $id => $item )
301
		{
302
			try
303
			{
304
				$list = $sites->get( $item->getSiteId(), map() );
305
306
				$this->send( $item, $list->getTheme()->filter()->last(), $list->getLogo()->filter()->last() );
307
				$this->update( $id, $status );
308
309
				$str = sprintf( 'Sent order payment e-mail for order "%1$s" and status "%2$s"', $item->getId(), $status );
310
				$context->logger()->info( $str, 'email/order/payment' );
311
			}
312
			catch( \Exception $e )
313
			{
314
				$str = 'Error while trying to send payment e-mail for order ID "%1$s" and status "%2$s": %3$s';
315
				$msg = sprintf( $str, $item->getId(), $item->getStatusPayment(), $e->getMessage() );
316
				$context->logger()->error( $msg . PHP_EOL . $e->getTraceAsString(), 'email/order/payment' );
317
			}
318
		}
319
	}
320
321
322
	/**
323
	 * Returns the generated PDF file for the order
324
	 *
325
	 * @param \Aimeos\Base\View\Iface $view View object with address and order item assigned
326
	 * @return string|null PDF content or NULL for no PDF file
327
	 */
328
	protected function pdf( \Aimeos\Base\View\Iface $view ) : ?string
329
	{
330
		$config = $this->context()->config();
331
332
		/** controller/jobs/order/email/payment/pdf
333
		 * Enables attaching the order confirmation PDF to the payment e-mail
334
		 *
335
		 * The order confirmation PDF contains the same information like the
336
		 * HTML e-mail and can be also used as invoice if possible.
337
		 *
338
		 * @param bool TRUE to enable attaching the PDF, FALSE to skip the PDF
339
		 * @since 2022.04
340
		 */
341
		if( !$config->get( 'controller/jobs/order/email/payment/pdf', true ) ) {
342
			return null;
343
		}
344
345
		$pdf = new class( PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false ) extends \TCPDF {
346
			private ?\Closure $headerFcn = null;
347
			private ?\Closure $footerFcn = null;
348
349
			public function Footer() { return ( $fcn = $this->footerFcn ) ? $fcn( $this ) : null; }
350
			public function Header() { return ( $fcn = $this->headerFcn ) ? $fcn( $this ) : null; }
351
			public function setFooterFunction( \Closure $fcn ) { $this->footerFcn = $fcn; }
352
			public function setHeaderFunction( \Closure $fcn ) { $this->headerFcn = $fcn; }
353
		};
354
		$pdf->setCreator( PDF_CREATOR );
355
		$pdf->setAuthor( 'Aimeos' );
356
357
		/** controller/jobs/order/email/payment/template-pdf
358
		 * Relative path to the template for the PDF part of the payment emails.
359
		 *
360
		 * The template file contains the text and processing instructions
361
		 * to generate the result shown in the body of the frontend. The
362
		 * configuration string is the path to the template file relative
363
		 * to the templates directory (usually in templates/controller/jobs).
364
		 * You can overwrite the template file configuration in extensions and
365
		 * provide alternative templates.
366
		 *
367
		 * @param string Relative path to the template
368
		 * @since 2022.10
369
		 * @see controller/jobs/order/email/payment/template-html
370
		 * @see controller/jobs/order/email/payment/template-text
371
		 */
372
		$template = $config->get( 'controller/jobs/order/email/payment/template-pdf', 'order/email/payment/pdf' );
373
374
		// Generate HTML before creating first PDF page to include header added in template
375
		$content = $view->set( 'pdf', $pdf )->render( $template );
376
377
		$pdf->addPage();
378
		$pdf->writeHtml( $content );
379
		$pdf->lastPage();
380
381
		return $pdf->output( '', 'S' );
382
	}
383
384
385
	/**
386
	 * Sends the payment related e-mail for a single order
387
	 *
388
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order item
389
	 * @param string|null $theme Theme name or NULL for default theme
390
	 * @param string|null $logoPath Relative path to the logo in the fs-media file system
391
	 */
392
	protected function send( \Aimeos\MShop\Order\Item\Iface $order, string $theme = null, string $logoPath = null )
393
	{
394
		/** controller/jobs/order/email/payment/template-html
395
		 * Relative path to the template for the HTML part of the payment emails.
396
		 *
397
		 * The template file contains the HTML code and processing instructions
398
		 * to generate the result shown in the body of the frontend. The
399
		 * configuration string is the path to the template file relative
400
		 * to the templates directory (usually in templates/controller/jobs).
401
		 * You can overwrite the template file configuration in extensions and
402
		 * provide alternative templates.
403
		 *
404
		 * @param string Relative path to the template
405
		 * @since 2022.04
406
		 * @see controller/jobs/order/email/payment/template-text
407
		 */
408
409
		/** controller/jobs/order/email/payment/template-text
410
		 * Relative path to the template for the text part of the payment emails.
411
		 *
412
		 * The template file contains the text and processing instructions
413
		 * to generate the result shown in the body of the frontend. The
414
		 * configuration string is the path to the template file relative
415
		 * to the templates directory (usually in templates/controller/jobs).
416
		 * You can overwrite the template file configuration in extensions and
417
		 * provide alternative templates.
418
		 *
419
		 * @param string Relative path to the template
420
		 * @since 2022.04
421
		 * @see controller/jobs/order/email/payment/template-html
422
		 */
423
424
		$address = $this->address( $order );
425
426
		$context = $this->context();
427
		$context->locale()->setLanguageId( $address->getLanguageId() );
428
429
		$msg = $this->call( 'mailTo', $address );
430
		$msg = $this->attachments( $msg );
431
432
		$view = $this->view( $order, $theme );
433
		$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) );
434
		$view->summaryBasket = $order;
435
		$view->addressItem = $address;
436
		$view->orderItem = $order;
437
438
		$config = $context->config();
439
440
		/** controller/jobs/order/email/payment/cc-email
441
		 * E-Mail address all payment e-mails should be also sent to
442
		 *
443
		 * Using this option you can send a copy of all payment related e-mails
444
		 * to a second e-mail account. This can be handy for testing and checking
445
		 * the e-mails sent to customers.
446
		 *
447
		 * It also allows shop owners with a very small volume of orders to be
448
		 * notified about payment changes. Be aware that this isn't useful if the
449
		 * order volumne is high or has peeks!
450
		 *
451
		 * @param string E-mail address or list of e-mail addresses
452
		 * @since 2023.10
453
		 */
454
		$msg->cc( $config->get( 'controller/jobs/order/email/payment/cc-email', '' ) );
455
456
		/** controller/jobs/order/email/payment/bcc-email
457
		 * Hidden e-mail address all payment e-mails should be also sent to
458
		 *
459
		 * Using this option you can send a copy of all payment related e-mails
460
		 * to a second e-mail account. This can be handy for testing and checking
461
		 * the e-mails sent to customers.
462
		 *
463
		 * It also allows shop owners with a very small volume of orders to be
464
		 * notified about payment changes. Be aware that this isn't useful if the
465
		 * order volumne is high or has peeks!
466
		 *
467
		 * @param string|array E-mail address or list of e-mail addresses
468
		 * @since 2014.03
469
		 */
470
		$msg->bcc( $config->get( 'controller/jobs/order/email/payment/bcc-email', [] ) );
471
472
		$msg->subject( sprintf( $context->translate( 'controller/jobs', 'Your order %1$s' ), $order->getInvoiceNumber() ) )
473
			->html( $view->render( $config->get( 'controller/jobs/order/email/payment/template-html', 'order/email/payment/html' ) ) )
474
			->text( $view->render( $config->get( 'controller/jobs/order/email/payment/template-text', 'order/email/payment/text' ) ) )
475
			->attach( $this->pdf( $view ), $this->call( 'filename', $order ), 'application/pdf' )
476
			->send();
477
	}
478
479
480
	/**
481
	 * Returns the site items for the given site codes
482
	 *
483
	 * @param iterable $siteIds List of site IDs
484
	 * @return \Aimeos\Map Site items with codes as keys
485
	 */
486
	protected function sites( iterable $siteIds ) : \Aimeos\Map
487
	{
488
		$map = [];
489
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
490
491
		foreach( $siteIds as $siteId )
492
		{
493
			$list = explode( '.', trim( $siteId, '.' ) );
494
			$map[$siteId] = $manager->getPath( end( $list ) );
495
		}
496
497
		return map( $map );
498
	}
499
500
501
	/**
502
	 * Returns the payment status values for which an e-mail should be sent
503
	 *
504
	 * @return array List of payment status values
505
	 */
506
	protected function status() : array
507
	{
508
		$default = [
509
			\Aimeos\MShop\Order\Item\Base::PAY_REFUND,
510
			\Aimeos\MShop\Order\Item\Base::PAY_PENDING,
511
			\Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED,
512
			\Aimeos\MShop\Order\Item\Base::PAY_RECEIVED,
513
		];
514
515
		/** controller/jobs/order/email/payment/status
516
		 * Only send order payment notification e-mails for these payment status values
517
		 *
518
		 * Notification e-mail about payment status changes can be sent for these
519
		 * status values:
520
		 *
521
		 * * 0: deleted
522
		 * * 1: canceled
523
		 * * 2: refused
524
		 * * 3: refund
525
		 * * 4: pending
526
		 * * 5: authorized
527
		 * * 6: received
528
		 * * 7: transferred
529
		 *
530
		 * User-defined status values are possible but should be in the private
531
		 * block of values between 30000 and 32767.
532
		 *
533
		 * @param integer Payment status constant
534
		 * @since 2014.03
535
		 * @see controller/jobs/order/email/delivery/status
536
		 * @see controller/jobs/order/email/payment/limit-days
537
		 */
538
		return (array) $this->context()->config()->get( 'controller/jobs/order/email/payment/status', $default );
539
	}
540
541
542
	/**
543
	 * Returns the status type for filtering the orders
544
	 *
545
	 * @return string Status type
546
	 */
547
	protected function type() : string
548
	{
549
		return \Aimeos\MShop\Order\Item\Status\Base::EMAIL_PAYMENT;
550
	}
551
552
553
	/**
554
	 * Adds the status of the delivered e-mail for the given order ID
555
	 *
556
	 * @param string $orderId Unique order ID
557
	 * @param int $value Status value
558
	 */
559
	protected function update( string $orderId, int $value )
560
	{
561
		$manager = \Aimeos\MShop::create( $this->context(), 'order/status' );
562
563
		$item = $manager->create()
564
			->setParentId( $orderId )
565
			->setType( $this->type() )
566
			->setValue( $value );
567
568
		$manager->save( $item );
569
	}
570
571
572
	/**
573
	 * Returns the view populated with common data
574
	 *
575
	 * @param \Aimeos\MShop\Order\Item\Iface $base Basket including addresses
576
	 * @param string|null $theme Theme name
577
	 * @return \Aimeos\Base\View\Iface View object
578
	 */
579
	protected function view( \Aimeos\MShop\Order\Item\Iface $base, string $theme = null ) : \Aimeos\Base\View\Iface
580
	{
581
		$address = $this->address( $base );
582
		$langId = $address->getLanguageId() ?: $base->locale()->getLanguageId();
583
584
		$view = $this->call( 'mailView', $langId );
585
		$view->intro = $this->call( 'mailIntro', $address );
586
		$view->css = $this->call( 'mailCss', $theme );
587
		$view->address = $address;
588
		$view->urlparams = [
589
			'currency' => $base->getPrice()->getCurrencyId(),
590
			'site' => $base->getSiteCode(),
591
			'locale' => $langId,
592
		];
593
594
		return $view;
595
	}
596
}
597