1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2021 |
6
|
|
|
* @package MShop |
7
|
|
|
* @subpackage Service |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MShop\Service\Provider\Delivery; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Email delivery provider implementation |
16
|
|
|
* |
17
|
|
|
* @package MShop |
18
|
|
|
* @subpackage Service |
19
|
|
|
*/ |
20
|
|
|
class Email |
21
|
|
|
extends \Aimeos\MShop\Service\Provider\Delivery\Base |
22
|
|
|
implements \Aimeos\MShop\Service\Provider\Delivery\Iface |
23
|
|
|
{ |
24
|
|
|
private $beConfig = [ |
25
|
|
|
'email.from' => [ |
26
|
|
|
'code' => 'email.from', |
27
|
|
|
'internalcode' => 'email.from', |
28
|
|
|
'label' => 'Sender e-mail address', |
29
|
|
|
'type' => 'string', |
30
|
|
|
'internaltype' => 'string', |
31
|
|
|
'default' => '', |
32
|
|
|
'required' => true, |
33
|
|
|
], |
34
|
|
|
'email.to' => [ |
35
|
|
|
'code' => 'email.to', |
36
|
|
|
'internalcode' => 'email.to', |
37
|
|
|
'label' => 'Recipient e-mail address', |
38
|
|
|
'type' => 'string', |
39
|
|
|
'internaltype' => '', |
40
|
|
|
'default' => '', |
41
|
|
|
'required' => true, |
42
|
|
|
], |
43
|
|
|
'email.subject' => [ |
44
|
|
|
'code' => 'email.subject', |
45
|
|
|
'internalcode' => 'email.subject', |
46
|
|
|
'label' => 'E-mail subject', |
47
|
|
|
'type' => 'string', |
48
|
|
|
'internaltype' => 'string', |
49
|
|
|
'default' => '', |
50
|
|
|
'required' => false, |
51
|
|
|
], |
52
|
|
|
'email.template' => [ |
53
|
|
|
'code' => 'email.template', |
54
|
|
|
'internalcode' => 'email.template', |
55
|
|
|
'label' => 'E-mail template', |
56
|
|
|
'type' => 'string', |
57
|
|
|
'internaltype' => 'string', |
58
|
|
|
'default' => 'service/provider/delivery/email-body-standard', |
59
|
|
|
'required' => true, |
60
|
|
|
], |
61
|
|
|
'email.order-template' => [ |
62
|
|
|
'code' => 'email.order-template', |
63
|
|
|
'internalcode' => 'email.order-template', |
64
|
|
|
'label' => 'Order template', |
65
|
|
|
'type' => 'string', |
66
|
|
|
'internaltype' => 'string', |
67
|
|
|
'default' => 'service/provider/delivery/email-order-standard', |
68
|
|
|
'required' => true, |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Checks the backend configuration attributes for validity |
75
|
|
|
* |
76
|
|
|
* @param array $attributes Attributes added by the shop owner in the administraton interface |
77
|
|
|
* @return array An array with the attribute keys as key and an error message as values for all attributes that are |
78
|
|
|
* known by the provider but aren't valid |
79
|
|
|
*/ |
80
|
|
|
public function checkConfigBE( array $attributes ) : array |
81
|
|
|
{ |
82
|
|
|
$errors = parent::checkConfigBE( $attributes ); |
83
|
|
|
|
84
|
|
|
return array_merge( $errors, $this->checkConfig( $this->beConfig, $attributes ) ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the configuration attribute definitions of the provider to generate a list of available fields and |
90
|
|
|
* rules for the value of each field in the administration interface. |
91
|
|
|
* |
92
|
|
|
* @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface |
93
|
|
|
*/ |
94
|
|
|
public function getConfigBE() : array |
95
|
|
|
{ |
96
|
|
|
return $this->getConfigItems( $this->beConfig ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sends the email and updates the delivery status |
102
|
|
|
* |
103
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface $order Order instance |
104
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface Updated order item |
105
|
|
|
*/ |
106
|
|
|
public function process( \Aimeos\MShop\Order\Item\Iface $order ) : \Aimeos\MShop\Order\Item\Iface |
107
|
|
|
{ |
108
|
|
|
$baseItem = $this->getOrderBase( $order->getBaseId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL ); |
|
|
|
|
109
|
|
|
$this->send( [$order], [$baseItem->getId() => $baseItem] ); |
110
|
|
|
|
111
|
|
|
return $order->setDeliveryStatus( \Aimeos\MShop\Order\Item\Base::STAT_PROGRESS ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sends the email with several orders and updates the delivery status |
117
|
|
|
* |
118
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface[] $orders List of order invoice objects |
119
|
|
|
* @return \Aimeos\MShop\Order\Item\Iface[] Updated order items |
120
|
|
|
*/ |
121
|
|
|
public function processBatch( iterable $orders ) : \Aimeos\Map |
122
|
|
|
{ |
123
|
|
|
$baseItems = $this->getOrderBaseItems( $orders ); |
124
|
|
|
$this->send( $orders, $baseItems ); |
125
|
|
|
|
126
|
|
|
foreach( $orders as $key => $order ) { |
127
|
|
|
$orders[$key] = $order->setDeliveryStatus( \Aimeos\MShop\Order\Item\Base::STAT_PROGRESS ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return map( $orders ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the content for the e-mail body |
136
|
|
|
* |
137
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface[] $orderItems List of order items to export |
138
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface[] $baseItems Associative list of order base items to export |
139
|
|
|
* @param string E-Mail body content |
|
|
|
|
140
|
|
|
*/ |
141
|
|
|
protected function getEmailContent( iterable $orderItems, iterable $baseItems ) |
142
|
|
|
{ |
143
|
|
|
$template = $this->getConfigValue( 'email.template', 'service/provider/delivery/email-body-standard' ); |
144
|
|
|
|
145
|
|
|
return $this->getContext()->getView() |
146
|
|
|
->assign( ['orderItems' => $orderItems, 'baseItems' => $baseItems] ) |
147
|
|
|
->render( $template ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the order content for the e-mail attachment |
153
|
|
|
* |
154
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface[] $orderItems List of order items to export |
155
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface[] $baseItems Associative list of order base items to export |
156
|
|
|
* @param string E-Mail order content |
|
|
|
|
157
|
|
|
*/ |
158
|
|
|
protected function getOrderContent( iterable $orderItems, iterable $baseItems ) |
159
|
|
|
{ |
160
|
|
|
$template = $this->getConfigValue( 'email.template', 'service/provider/delivery/email-order-standard' ); |
161
|
|
|
|
162
|
|
|
return $this->getContext()->getView() |
163
|
|
|
->assign( ['orderItems' => $orderItems, 'baseItems' => $baseItems] ) |
164
|
|
|
->render( $template ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the order base items for the given orders |
170
|
|
|
* |
171
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface[] $orderItems List of order items |
172
|
|
|
* @return \Aimeos\Map List of items implementing \Aimeos\MShop\Order\Item\Base\Iface with IDs as keys |
173
|
|
|
*/ |
174
|
|
|
protected function getOrderBaseItems( iterable $orderItems ) : \Aimeos\Map |
175
|
|
|
{ |
176
|
|
|
$ids = map( $orderItems )->getBaseId(); |
177
|
|
|
$ref = ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service']; |
178
|
|
|
|
179
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'order/base' ); |
180
|
|
|
$search = $manager->filter()->slice( 0, $ids->count() ); |
181
|
|
|
$search->setConditions( $search->compare( '==', 'order.base.id', $ids->toArray() ) ); |
182
|
|
|
|
183
|
|
|
return $manager->search( $search, $ref ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Sends an e-mail for the given orders |
189
|
|
|
* |
190
|
|
|
* @param \Aimeos\MShop\Order\Item\Iface[] $orderItems List of order items to export |
191
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Iface[] $baseItems Associative list of order base items to export |
192
|
|
|
*/ |
193
|
|
|
protected function send( iterable $orderItems, iterable $baseItems ) |
194
|
|
|
{ |
195
|
|
|
$this->getContext()->mail()->createMessage() |
196
|
|
|
->addFrom( $this->getConfigValue( 'email.from' ) ) |
|
|
|
|
197
|
|
|
->addTo( $this->getConfigValue( 'email.to' ) ) |
|
|
|
|
198
|
|
|
->setSubject( $this->getConfigValue( 'email.subject', 'New orders' ) ) |
199
|
|
|
->addAttachment( $this->getOrderContent( $orderItems, $baseItems ), 'text/plain', 'orders.csv' ) |
200
|
|
|
->setBody( $this->getEmailContent( $orderItems, $baseItems ) ) |
201
|
|
|
->send(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|