1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @Description: Email specifically for communicating with customer about order. |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
* @authors: Nicolaas [at] Sunny Side Up .co.nz |
9
|
|
|
* @package: ecommerce |
10
|
|
|
* @sub-package: email |
11
|
|
|
* @inspiration: Silverstripe Ltd, Jeremy |
12
|
|
|
**/ |
13
|
|
|
abstract class Order_Email extends Email |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Order |
17
|
|
|
*/ |
18
|
|
|
protected $order = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $resend = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* turns an html document into a formatted html document |
27
|
|
|
* using the emogrify method. |
28
|
|
|
* |
29
|
|
|
* @param $html |
30
|
|
|
* |
31
|
|
|
* @return string HTML |
32
|
|
|
*/ |
33
|
|
|
public static function emogrify_html($html) |
34
|
|
|
{ |
35
|
|
|
//get required files |
36
|
|
|
$baseFolder = Director::baseFolder(); |
37
|
|
|
if (!class_exists('\Pelago\Emogrifier')) { |
38
|
|
|
require_once $baseFolder.'/ecommerce/thirdparty/Emogrifier.php'; |
39
|
|
|
} |
40
|
|
|
$cssFileLocation = Director::baseFolder().'/'.EcommerceConfig::get('Order_Email', 'css_file_location'); |
41
|
|
|
$cssFileHandler = fopen($cssFileLocation, 'r'); |
42
|
|
|
$css = fread($cssFileHandler, filesize($cssFileLocation)); |
43
|
|
|
fclose($cssFileHandler); |
44
|
|
|
$emogrifier = new \Pelago\Emogrifier($html, $css); |
45
|
|
|
$html = $emogrifier->emogrify(); |
46
|
|
|
//make links absolute! |
47
|
|
|
$html = HTTP::absoluteURLs($html); |
48
|
|
|
|
49
|
|
|
return $html; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* returns the standard from email address (e.g. the shop admin email address). |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public static function get_from_email() |
58
|
|
|
{ |
59
|
|
|
$ecommerceConfig = EcommerceDBConfig::current_ecommerce_db_config(); |
60
|
|
|
if ($ecommerceConfig && $ecommerceConfig->ReceiptEmail) { |
|
|
|
|
61
|
|
|
$email = $ecommerceConfig->ReceiptEmail; |
|
|
|
|
62
|
|
|
} else { |
63
|
|
|
$email = Email::config()->admin_email; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return trim($email); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* returns the subject for the email (doh!). |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public static function get_subject() |
75
|
|
|
{ |
76
|
|
|
$siteConfig = SiteConfig::current_site_config(); |
77
|
|
|
if ($siteConfig && $siteConfig->Title) { |
78
|
|
|
return _t('Order_Email.SALEUPDATE', 'Sale Update for Order #[OrderNumber] from ').$siteConfig->Title; |
79
|
|
|
} else { |
80
|
|
|
return _t('Order_Email.SALEUPDATE', 'Sale Update for Order #[OrderNumber] '); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* set the order associated with the email. |
86
|
|
|
* |
87
|
|
|
* @param Order $order - the order to which the email relates |
88
|
|
|
*/ |
89
|
|
|
public function setOrder(Order $order) |
90
|
|
|
{ |
91
|
|
|
$this->order = $order; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* sets resend to true, which means that the email |
96
|
|
|
* is sent even if it has already been sent. |
97
|
|
|
*/ |
98
|
|
|
public function setResend($resend = true) |
99
|
|
|
{ |
100
|
|
|
$this->resend = $resend; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param null|string $messageID - ID for the message, you can leave this blank |
105
|
|
|
* @param bool $returnBodyOnly - rather than sending the email, only return the HTML BODY |
106
|
|
|
* |
107
|
|
|
* @return bool - TRUE for success and FALSE for failure. |
|
|
|
|
108
|
|
|
*/ |
109
|
|
|
public function send($messageID = null, $returnBodyOnly = false) |
110
|
|
|
{ |
111
|
|
|
if (!$this->order) { |
112
|
|
|
user_error('Must set the order (Order_Email::setOrder()) before the message is sent (Order_Email::send()).', E_USER_NOTICE); |
113
|
|
|
} |
114
|
|
|
if (!$this->subject) { |
115
|
|
|
$this->subject = self::get_subject(); |
116
|
|
|
} |
117
|
|
|
$this->subject = str_replace('[OrderNumber]', $this->order->ID, $this->subject); |
118
|
|
|
if ((!$this->hasBeenSent()) || ($this->resend)) { |
119
|
|
|
if (EcommerceConfig::get('Order_Email', 'copy_to_admin_for_all_emails') && ($this->to !== self::get_from_email())) { |
120
|
|
|
if ($memberEmail = self::get_from_email()) { |
121
|
|
|
$array = [ $memberEmail ]; |
122
|
|
|
if($bcc = $this->Bcc()) { |
123
|
|
|
$array[] = $bcc; |
124
|
|
|
} |
125
|
|
|
$this->setBcc(implode(", ", $array)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
//last chance to adjust |
129
|
|
|
$this->extend('adjustOrderEmailSending', $this, $order); |
130
|
|
|
if ($returnBodyOnly) { |
131
|
|
|
return $this->Body(); |
132
|
|
|
} |
133
|
|
|
$orderEmailRecord = $this->createRecord($result); |
|
|
|
|
134
|
|
|
if (EcommerceConfig::get('Order_Email', 'send_all_emails_plain')) { |
135
|
|
|
$result = parent::sendPlain($messageID); |
|
|
|
|
136
|
|
|
} else { |
137
|
|
|
$result = parent::send($messageID); |
138
|
|
|
} |
139
|
|
|
$orderEmailRecord->Result = $result; |
|
|
|
|
140
|
|
|
$orderEmailRecord->write(); |
141
|
|
|
|
142
|
|
|
return $result; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param bool $result: how did the email go? 1 = sent, 0 = not sent |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return DataObject (OrderEmailRecord) |
150
|
|
|
**/ |
151
|
|
|
protected function createRecord($result) |
152
|
|
|
{ |
153
|
|
|
$orderEmailRecord = OrderEmailRecord::create(); |
154
|
|
|
$orderEmailRecord->From = $this->emailToVarchar($this->from); |
|
|
|
|
155
|
|
|
$orderEmailRecord->To = $this->emailToVarchar($this->to); |
|
|
|
|
156
|
|
|
if ($this->Cc()) { |
|
|
|
|
157
|
|
|
$orderEmailRecord->To .= ', CC: '.$this->emailToVarchar($this->Cc()); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
if ($this->Bcc()) { |
|
|
|
|
160
|
|
|
$orderEmailRecord->To .= ', BCC: '.$this->emailToVarchar($this->Bcc()); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
//always set result to try if |
163
|
|
|
$orderEmailRecord->Subject = $this->subject; |
|
|
|
|
164
|
|
|
if (!$result) { |
165
|
|
|
if (Director::isDev()) { |
166
|
|
|
$result = true; |
167
|
|
|
$orderEmailRecord->Subject .= _t('Order_Email.FAKELY_RECORDED_AS_SENT', ' - FAKELY RECORDED AS SENT '); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
$orderEmailRecord->Content = $this->body; |
|
|
|
|
171
|
|
|
$orderEmailRecord->Result = $result ? 1 : 0; |
|
|
|
|
172
|
|
|
$orderEmailRecord->OrderID = $this->order->ID; |
|
|
|
|
173
|
|
|
$orderEmailRecord->OrderStepID = $this->order->StatusID; |
|
|
|
|
174
|
|
|
if ($sendAllEmailsTo = Config::inst()->get('Email', 'send_all_emails_to')) { |
175
|
|
|
$orderEmailRecord->To .= |
|
|
|
|
176
|
|
|
_t('Order_Email.ACTUALLY_SENT_TO', ' | actually sent to: ') |
177
|
|
|
.$sendAllEmailsTo |
178
|
|
|
._t('Order_Email.CONFIG_EXPLANATION', ' - (Email::send_all_emails_to)'); |
179
|
|
|
} |
180
|
|
|
$orderEmailRecord->write(); |
181
|
|
|
|
182
|
|
|
return $orderEmailRecord; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* converts an Email to A Varchar. |
187
|
|
|
* |
188
|
|
|
* @param string $email - email address |
189
|
|
|
* |
190
|
|
|
* @return string - returns email address without > and < |
191
|
|
|
*/ |
192
|
|
|
public function emailToVarchar($email) |
193
|
|
|
{ |
194
|
|
|
$email = str_replace(array('<', '>', '"', "'"), ' - ', $email); |
195
|
|
|
|
196
|
|
|
return $email; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Checks if an email has been sent for this Order for this status (order step). |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
**/ |
204
|
|
|
public function hasBeenSent() |
205
|
|
|
{ |
206
|
|
|
$orderStep = $this->order->Status(); |
|
|
|
|
207
|
|
|
if (is_a($orderStep, Object::getCustomClass('OrderStep'))) { |
208
|
|
|
return $orderStep->hasBeenSent($this->order); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* moves CSS to inline CSS in email. |
216
|
|
|
* |
217
|
|
|
* @param bool $isPlain - should we send the email as HTML or as TEXT |
218
|
|
|
*/ |
219
|
|
|
protected function parseVariables($isPlain = false) |
220
|
|
|
{ |
221
|
|
|
//start parsing |
222
|
|
|
parent::parseVariables($isPlain); |
223
|
|
|
if (!$isPlain) { |
224
|
|
|
$this->body = self::emogrify_html($this->body); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* returns the instance of EcommerceDBConfig. |
230
|
|
|
* |
231
|
|
|
* @return EcommerceDBConfig |
232
|
|
|
**/ |
233
|
|
|
public function EcomConfig() |
234
|
|
|
{ |
235
|
|
|
return EcommerceDBConfig::current_ecommerce_db_config(); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.