Passed
Push — master ( 2196ce...ac647e )
by Aimeos
03:24
created

Standard::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 16
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\Delivery;
12
13
14
/**
15
 * Order delivery 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/delivery/name
25
	 * Class name of the used order email delivery 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\Delivery\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Order\Email\Delivery\Mydelivery
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/order/email/delivery/name = Mydelivery
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 "MyDelivery"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 */
56
57
	/** controller/jobs/order/email/delivery/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the order email delivery 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/delivery/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/delivery/decorators/global
79
	 * @see controller/jobs/order/email/delivery/decorators/local
80
	 */
81
82
	/** controller/jobs/order/email/delivery/decorators/global
83
	 * Adds a list of globally available decorators only to the order email delivery 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/delivery/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/delivery/decorators/excludes
102
	 * @see controller/jobs/order/email/delivery/decorators/local
103
	 */
104
105
	/** controller/jobs/order/email/delivery/decorators/local
106
	 * Adds a list of local decorators only to the order email delivery 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\Delivery\Decorator\*") around this job controller.
115
	 *
116
	 *  controller/jobs/order/email/delivery/decorators/local = array( 'decorator2' )
117
	 *
118
	 * This would add the decorator named "decorator2" defined by
119
	 * "\Aimeos\Controller\Jobs\Order\Email\Delivery\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/delivery/decorators/excludes
126
	 * @see controller/jobs/order/email/delivery/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 delivery 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 delivery 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
		$config = $context->config();
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
164
		$limitDate = date( 'Y-m-d H:i:s', time() - $this->limit() * 86400 );
165
166
		$orderManager = \Aimeos\MShop::create( $context, 'order' );
167
168
		foreach( $this->status() as $status )
169
		{
170
			$param = array( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_DELIVERY, (string) $status );
171
			$filter = $orderManager->filter();
172
			$filter->add( $filter->and( [
173
				$filter->compare( '>=', 'order.mtime', $limitDate ),
174
				$filter->compare( '==', 'order.statusdelivery', $status ),
175
				$filter->compare( '==', $filter->make( 'order:status', $param ), 0 ),
176
			] ) );
177
178
			$start = 0;
179
			$ref = ['order'] + $context->config()->get( 'mshop/order/manager/subdomains', [] );
180
181
			do
182
			{
183
				$items = $orderManager->search( $filter->slice( $start ), $ref );
184
185
				$this->notify( $items, $status );
186
187
				$count = count( $items );
188
				$start += $count;
189
			}
190
			while( $count >= $filter->getLimit() );
191
		}
192
	}
193
194
195
	/**
196
	 * Returns the address item from the order
197
	 *
198
	 * @param \Aimeos\MShop\Order\Item\Iface $basket Order including address items
199
	 * @return \Aimeos\MShop\Common\Item\Address\Iface Address item
200
	 * @throws \Aimeos\Controller\Jobs\Exception If no suitable address item is available
201
	 */
202
	protected function address( \Aimeos\MShop\Order\Item\Iface $basket ) : \Aimeos\MShop\Common\Item\Address\Iface
203
	{
204
		if( ( $addr = current( $basket->getAddress( 'delivery' ) ) ) !== false && $addr->getEmail() ) {
205
			return $addr;
206
		};
207
208
		if( ( $addr = current( $basket->getAddress( 'payment' ) ) ) !== false && $addr->getEmail() ) {
209
			return $addr;
210
		};
211
212
		$msg = sprintf( 'No address with e-mail found in order base with ID "%1$s"', $basket->getId() );
213
		throw new \Aimeos\Controller\Jobs\Exception( $msg );
214
	}
215
216
217
	/**
218
	 * Adds the given list of files as attachments to the mail message object
219
	 *
220
	 * @param \Aimeos\Base\Mail\Message\Iface $msg Mail message
221
	 * @param array $files List of absolute file paths
222
	 */
223
	protected function attachments( \Aimeos\Base\Mail\Message\Iface $msg ) : \Aimeos\Base\Mail\Message\Iface
224
	{
225
		$context = $this->context();
226
		$fs = $context->fs();
227
228
		/** controller/jobs/order/email/delivery/attachments
229
		 * List of file paths whose content should be attached to all delivery e-mails
230
		 *
231
		 * This configuration option allows you to add files to the e-mails that are
232
		 * sent to the customer when the delivery status changes, e.g. for the order
233
		 * confirmation e-mail. These files can't be customer specific.
234
		 *
235
		 * @param array List of absolute file paths
236
		 * @since 2016.10
237
		 * @see controller/jobs/order/email/payment/attachments
238
		 */
239
		$files = $context->config()->get( 'controller/jobs/order/email/delivery/attachments', [] );
240
241
		foreach( $files as $filepath )
242
		{
243
			if( $fs->has( $filepath ) ) {
244
				$msg->attach( $fs->read( $filepath ), basename( $filepath ) );
245
			}
246
		}
247
248
		return $msg;
249
	}
250
251
252
	/**
253
	 * Returns the number of days after no e-mail will be sent anymore
254
	 *
255
	 * @return int Number of days
256
	 */
257
	protected function limit() : int
258
	{
259
		/** controller/jobs/order/email/delivery/limit-days
260
		 * Only send delivery e-mails of orders that were created in the past within the configured number of days
261
		 *
262
		 * The delivery e-mails are normally send immediately after the delivery
263
		 * status has changed. This option prevents e-mails for old order from
264
		 * being send in case anything went wrong or an update failed to avoid
265
		 * confusion of customers.
266
		 *
267
		 * @param integer Number of days
268
		 * @since 2014.03
269
		 * @see controller/jobs/order/email/delivery/limit-days
270
		 * @see controller/jobs/service/delivery/process/limit-days
271
		 */
272
		return (int) $this->context()->config()->get( 'controller/jobs/order/email/delivery/limit-days', 90 );
273
	}
274
275
276
	/**
277
	 * Sends the delivery e-mail for the given orders
278
	 *
279
	 * @param \Aimeos\Map $items List of order items implementing \Aimeos\MShop\Order\Item\Iface with their IDs as keys
280
	 * @param int $status Delivery status value
281
	 */
282
	protected function notify( \Aimeos\Map $items, int $status )
283
	{
284
		$context = $this->context();
285
		$sites = $this->sites( $items->getSiteId()->unique() );
286
287
		foreach( $items as $id => $item )
288
		{
289
			try
290
			{
291
				$list = $sites->get( $item->getSiteId(), map() );
292
293
				$this->send( $item, $list->getTheme()->filter()->last(), $list->getLogo()->filter()->last() );
294
				$this->update( $id, $status );
295
296
				$str = sprintf( 'Sent order delivery e-mail for order "%1$s" and status "%2$s"', $item->getId(), $status );
297
				$context->logger()->info( $str, 'email/order/delivery' );
298
			}
299
			catch( \Exception $e )
300
			{
301
				$str = 'Error while trying to send delivery e-mail for order ID "%1$s" and status "%2$s": %3$s';
302
				$msg = sprintf( $str, $item->getId(), $item->getStatusPayment(), $e->getMessage() );
303
				$context->logger()->error( $msg . PHP_EOL . $e->getTraceAsString(), 'email/order/delivery' );
304
			}
305
		}
306
	}
307
308
309
	/**
310
	 * Sends the delivery related e-mail for a single order
311
	 *
312
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order item
313
	 * @param string|null $theme Theme name or NULL for default theme
314
	 * @param string|null $logoPath Relative path to the logo in the fs-media file system
315
	 */
316
	protected function send( \Aimeos\MShop\Order\Item\Iface $order, string $theme = null, string $logoPath = null )
317
	{
318
		/** controller/jobs/order/email/delivery/template-html
319
		 * Relative path to the template for the HTML part of the delivery emails.
320
		 *
321
		 * The template file contains the HTML code and processing instructions
322
		 * to generate the result shown in the body of the frontend. The
323
		 * configuration string is the path to the template file relative
324
		 * to the templates directory (usually in templates/controller/jobs).
325
		 * You can overwrite the template file configuration in extensions and
326
		 * provide alternative templates.
327
		 *
328
		 * @param string Relative path to the template
329
		 * @since 2022.04
330
		 * @see controller/jobs/order/email/delivery/template-text
331
		 */
332
333
		/** controller/jobs/order/email/delivery/template-text
334
		 * Relative path to the template for the text part of the delivery emails.
335
		 *
336
		 * The template file contains the text and processing instructions
337
		 * to generate the result shown in the body of the frontend. The
338
		 * configuration string is the path to the template file relative
339
		 * to the templates directory (usually in templates/controller/jobs).
340
		 * You can overwrite the template file configuration in extensions and
341
		 * provide alternative templates.
342
		 *
343
		 * @param string Relative path to the template
344
		 * @since 2022.04
345
		 * @see controller/jobs/order/email/delivery/template-html
346
		 */
347
348
		$address = $this->address( $order );
349
350
		$context = $this->context();
351
		$context->locale()->setLanguageId( $address->getLanguageId() );
352
353
		$msg = $this->call( 'mailTo', $address );
354
		$msg = $this->attachments( $msg );
355
356
		$view = $this->view( $order, $theme );
357
		$view->logo = $msg->embed( $this->call( 'mailLogo', $logoPath ), basename( (string) $logoPath ) );
358
		$view->summaryBasket = $order;
359
		$view->addressItem = $address;
360
		$view->orderItem = $order;
361
362
		$config = $context->config();
363
364
		/** controller/jobs/order/email/delivery/cc-email
365
		 * E-Mail address all delivery e-mails should be also sent to
366
		 *
367
		 * Using this option you can send a copy of all delivery related e-mails
368
		 * to a second e-mail account. This can be handy for testing and checking
369
		 * the e-mails sent to customers.
370
		 *
371
		 * It also allows shop owners with a very small volume of orders to be
372
		 * notified about delivery changes. Be aware that this isn't useful if the
373
		 * order volumne is high or has peeks!
374
		 *
375
		 * @param string E-mail address or list of e-mail addresses
376
		 * @since 2023.10
377
		 */
378
		$msg->cc( $config->get( 'controller/jobs/order/email/delivery/cc-email', '' ) );
379
380
		/** controller/jobs/order/email/delivery/bcc-email
381
		 * Hidden e-mail address all delivery e-mails should be also sent to
382
		 *
383
		 * Using this option you can send a copy of all delivery related e-mails
384
		 * to a second e-mail account. This can be handy for testing and checking
385
		 * the e-mails sent to customers.
386
		 *
387
		 * It also allows shop owners with a very small volume of orders to be
388
		 * notified about delivery changes. Be aware that this isn't useful if the
389
		 * order volumne is high or has peeks!
390
		 *
391
		 * @param string|array E-mail address or list of e-mail addresses
392
		 * @since 2014.03
393
		 */
394
		$msg->bcc( $config->get( 'controller/jobs/order/email/delivery/bcc-email', [] ) );
395
396
		$msg->subject( sprintf( $context->translate( 'controller/jobs', 'Your order %1$s' ), $order->getInvoiceNumber() ) )
397
			->html( $view->render( $config->get( 'controller/jobs/order/email/delivery/template-html', 'order/email/delivery/html' ) ) )
398
			->text( $view->render( $config->get( 'controller/jobs/order/email/delivery/template-text', 'order/email/delivery/text' ) ) )
399
			->send();
400
	}
401
402
403
	/**
404
	 * Returns the site items for the given site codes
405
	 *
406
	 * @param iterable $siteIds List of site IDs
407
	 * @return \Aimeos\Map Site items with codes as keys
408
	 */
409
	protected function sites( iterable $siteIds ) : \Aimeos\Map
410
	{
411
		$map = [];
412
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
413
414
		foreach( $siteIds as $siteId )
415
		{
416
			$list = explode( '.', trim( $siteId, '.' ) );
417
			$map[$siteId] = $manager->getPath( end( $list ) );
418
		}
419
420
		return map( $map );
421
	}
422
423
424
	/**
425
	 * Returns the list of delivery status values for which e-mails should be sent
426
	 *
427
	 * @return array List of delivery status values
428
	 */
429
	protected function status() : array
430
	{
431
		$default = [
432
			\Aimeos\MShop\Order\Item\Base::STAT_PROGRESS,
433
			\Aimeos\MShop\Order\Item\Base::STAT_DISPATCHED,
434
			\Aimeos\MShop\Order\Item\Base::STAT_REFUSED,
435
			\Aimeos\MShop\Order\Item\Base::STAT_RETURNED,
436
		];
437
438
		/** controller/jobs/order/email/delivery/status
439
		 * Only send order delivery notification e-mails for these delivery status values
440
		 *
441
		 * Notification e-mail about delivery status changes can be sent for these
442
		 * status values:
443
		 *
444
		 * * 0: deleted
445
		 * * 1: pending
446
		 * * 2: progress
447
		 * * 3: dispatched
448
		 * * 4: delivered
449
		 * * 5: lost
450
		 * * 6: refused
451
		 * * 7: returned
452
		 *
453
		 * User-defined status values are possible but should be in the private
454
		 * block of values between 30000 and 32767.
455
		 *
456
		 * @param integer Delivery status constant
457
		 * @since 2014.03
458
		 * @see controller/jobs/order/email/payment/status
459
		 * @see controller/jobs/order/email/delivery/limit-days
460
		 */
461
		return (array) $this->context()->config()->get( 'controller/jobs/order/email/delivery/status', $default );
462
	}
463
464
465
	/**
466
	 * Adds the status of the delivered e-mail for the given order ID
467
	 *
468
	 * @param string $orderId Unique order ID
469
	 * @param int $value Status value
470
	 */
471
	protected function update( string $orderId, int $value )
472
	{
473
		$manager = \Aimeos\MShop::create( $this->context(), 'order/status' );
474
475
		$item = $manager->create()
476
			->setParentId( $orderId )
477
			->setType( \Aimeos\MShop\Order\Item\Status\Base::EMAIL_DELIVERY )
478
			->setValue( $value );
479
480
		$manager->save( $item );
481
	}
482
483
484
	/**
485
	 * Returns the view populated with common data
486
	 *
487
	 * @param \Aimeos\MShop\Order\Item\Iface $base Basket including addresses
488
	 * @param string|null $theme Theme name
489
	 * @return \Aimeos\Base\View\Iface View object
490
	 */
491
	protected function view( \Aimeos\MShop\Order\Item\Iface $base, string $theme = null ) : \Aimeos\Base\View\Iface
492
	{
493
		$address = $this->address( $base );
494
		$langId = $address->getLanguageId() ?: $base->locale()->getLanguageId();
495
496
		$view = $this->call( 'mailView', $langId );
497
		$view->intro = $this->call( 'mailIntro', $address );
498
		$view->css = $this->call( 'mailCss', $theme );
499
		$view->address = $address;
500
		$view->urlparams = [
501
			'currency' => $base->getPrice()->getCurrencyId(),
502
			'site' => $base->getSiteCode(),
503
			'locale' => $langId,
504
		];
505
506
		return $view;
507
	}
508
}
509