1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* 1 July bough |
6
|
|
|
* +10 days start sending |
7
|
|
|
* +20 days stop sending |
8
|
|
|
* SO |
9
|
|
|
* on 11 July |
10
|
|
|
* 1 July + 10 < Now |
11
|
|
|
* 1 July + 20 > Now |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* 1 July bought |
19
|
|
|
* +10 days start sending |
20
|
|
|
* +20 days stop sending |
21
|
|
|
* SO |
22
|
|
|
* on 11 July |
23
|
|
|
* 1 July + 10 < Now |
24
|
|
|
* 1 July + 20 > Now |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class OrderStepFeedback extends OrderStep |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
private static $verbose = false; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var String |
35
|
|
|
*/ |
36
|
|
|
protected $emailClassName = "OrderStepFeedback_Email"; |
37
|
|
|
|
38
|
|
|
private static $db = array( |
|
|
|
|
39
|
|
|
'SendFeedbackEmail' => 'Boolean', |
40
|
|
|
'MinDays' => 'Int', |
41
|
|
|
'MaxDays' => 'Int', |
42
|
|
|
'MessageAfterProductsList' => 'HTMLText', |
43
|
|
|
'LinkText' => 'Varchar' |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
private static $defaults = array( |
|
|
|
|
47
|
|
|
'CustomerCanEdit' => 0, |
48
|
|
|
'CustomerCanCancel' => 0, |
49
|
|
|
'CustomerCanPay' => 0, |
50
|
|
|
'Name' => 'Get Feedback', |
51
|
|
|
'Code' => 'FEEDBACK', |
52
|
|
|
"ShowAsInProcessOrder" => true, |
53
|
|
|
"HideStepFromCustomer" => true, |
54
|
|
|
'SendFeedbackEmail' => true, |
55
|
|
|
'MinDays' => 10, |
56
|
|
|
'MaxDays' => 20 |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function getCMSFields() |
61
|
|
|
{ |
62
|
|
|
$fields = parent::getCMSFields(); |
63
|
|
|
$fields->addFieldsToTab( |
64
|
|
|
'Root.CustomerMessage', |
65
|
|
|
array( |
66
|
|
|
CheckboxField::create('SendFeedbackEmail', 'Send feedback email to customer?'), |
67
|
|
|
$minDaysField = NumericField::create('MinDays', "<strong>Min Days</strong> before sending"), |
68
|
|
|
$maxDaysField = NumericField::create('MaxDays', "<strong>Max Days</strong> before sending") |
69
|
|
|
), |
70
|
|
|
"EmailSubject" |
71
|
|
|
); |
72
|
|
|
$minDaysField->setRightTitle('What is the <strong>mininum number of days to wait after completing an order</strong> before this email should be sent?'); |
73
|
|
|
$maxDaysField->setRightTitle( |
74
|
|
|
' |
75
|
|
|
What is the <strong>maxinum number of days to wait after completing an order</strong> before this email should be sent?<br> |
76
|
|
|
<strong>If set to zero, this step will be ignored.</strong>' |
77
|
|
|
); |
78
|
|
|
$fields->addFieldsToTab( |
79
|
|
|
'Root.CustomerMessage', |
80
|
|
|
array( |
81
|
|
|
HTMLEditorField::create( |
82
|
|
|
'MessageAfterProductsList', |
83
|
|
|
_t('OrderStepFeedback.MESSAGEAFTERPRODUCTSLIST', 'Message After Products List') |
84
|
|
|
)->setRightTitle( |
85
|
|
|
'Optional message displayed after the list of products' |
86
|
|
|
)->setRows(3), |
87
|
|
|
TextField::create( |
88
|
|
|
'LinkText', |
89
|
|
|
_t('OrderStepFeedback.BUTTONTEXT', 'Link Text') |
90
|
|
|
)->setRightTitle('This is the text displayed on the "order again" link/button') |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
if ($this->MinDays) { |
|
|
|
|
94
|
|
|
$fields->replaceField( |
95
|
|
|
'DeferTimeInSeconds', |
96
|
|
|
$fields->dataFieldByName('DeferTimeInSeconds')->performReadonlyTransformation() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
return $fields; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function initStep(Order $order) |
103
|
|
|
{ |
104
|
|
|
if ($this->SendFeedbackEmail) { |
|
|
|
|
105
|
|
|
Config::inst()->update("OrderStep", "number_of_days_to_send_update_email", $this->MaxDays); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function doStep(Order $order) |
111
|
|
|
{ |
112
|
|
|
//ignore altogether? |
113
|
|
|
if ($this->SendFeedbackEmail) { |
|
|
|
|
114
|
|
|
// too late to send |
115
|
|
|
if ($this->isExpiredFeedbackStep($order)) { |
116
|
|
|
if ($this->Config()->get("verbose")) { |
117
|
|
|
DB::alteration_message(" - Time to send feedback is expired"); |
118
|
|
|
} |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
//is now the right time to send? |
122
|
|
|
elseif ($this->isReadyToGo($order)) { |
123
|
|
|
$subject = $this->EmailSubject; |
|
|
|
|
124
|
|
|
$message = $this->CustomerMessage; |
|
|
|
|
125
|
|
|
if ($this->hasBeenSent($order, false)) { |
126
|
|
|
if ($this->Config()->get("verbose")) { |
127
|
|
|
DB::alteration_message(" - already sent!"); |
128
|
|
|
} |
129
|
|
|
return true; //do nothing |
130
|
|
|
} else { |
131
|
|
|
if ($this->Config()->get("verbose")) { |
132
|
|
|
DB::alteration_message(" - Sending it now!"); |
133
|
|
|
} |
134
|
|
|
return $order->sendEmail( |
135
|
|
|
$this->getEmailClassName(), |
136
|
|
|
$subject, |
137
|
|
|
$message, |
138
|
|
|
$resend = false, |
139
|
|
|
$adminOnlyOrToEmail = false |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
//wait until later.... |
144
|
|
|
else { |
145
|
|
|
if ($this->Config()->get("verbose")) { |
146
|
|
|
DB::alteration_message(" - We need to wait until minimum number of days."); |
147
|
|
|
} |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
} else { |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* can continue if emails has been sent or if there is no need to send a receipt. |
157
|
|
|
* @param DataObject $order Order |
158
|
|
|
* @return DataObject | Null - DataObject = next OrderStep |
|
|
|
|
159
|
|
|
**/ |
160
|
|
|
public function nextStep(Order $order) |
161
|
|
|
{ |
162
|
|
|
if ( |
163
|
|
|
! $this->SendFeedbackEmail || |
|
|
|
|
164
|
|
|
$this->hasBeenSent($order, false) || |
165
|
|
|
$this->isExpiredFeedbackStep($order) |
166
|
|
|
) { |
167
|
|
|
if ($this->Config()->get("verbose")) { |
168
|
|
|
DB::alteration_message(" - Moving to next step"); |
169
|
|
|
} |
170
|
|
|
return parent::nextStep($order); |
171
|
|
|
} |
172
|
|
|
if ($this->Config()->get("verbose")) { |
173
|
|
|
DB::alteration_message(" - no next step: has not been sent"); |
174
|
|
|
} |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* For some ordersteps this returns true... |
180
|
|
|
* @return Boolean |
181
|
|
|
**/ |
182
|
|
|
protected function hasCustomerMessage() |
183
|
|
|
{ |
184
|
|
|
return true; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Explains the current order step. |
189
|
|
|
* @return String |
190
|
|
|
*/ |
191
|
|
|
protected function myDescription() |
192
|
|
|
{ |
193
|
|
|
return "The customer is sent a feedback request email."; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* returns true if the Minimum number of days is met.... |
198
|
|
|
* @param Order |
199
|
|
|
* @return Boolean |
200
|
|
|
*/ |
201
|
|
|
protected function isReadyToGo(Order $order) |
202
|
|
|
{ |
203
|
|
|
if ($this->MinDays) { |
|
|
|
|
204
|
|
|
$log = $order->SubmissionLog(); |
205
|
|
|
if ($log) { |
206
|
|
|
$createdTS = strtotime($log->Created); |
207
|
|
|
$nowTS = strtotime('now'); |
208
|
|
|
$startSendingTS = strtotime("+{$this->MinDays} days", $createdTS); |
|
|
|
|
209
|
|
|
//current TS = 10 |
210
|
|
|
//order TS = 8 |
211
|
|
|
//add 4 days: 12 |
212
|
|
|
//thus if 12 <= now then go for it (start point in time has passed) |
213
|
|
|
if ($this->Config()->get("verbose")) { |
214
|
|
|
DB::alteration_message("Time comparison: Start Sending TS: ".$startSendingTS." current TS: ".$nowTS.". If SSTS > NowTS then Go for it."); |
215
|
|
|
} |
216
|
|
|
return ($startSendingTS <= $nowTS) ? true : false; |
217
|
|
|
} else { |
218
|
|
|
user_error("can not find order log for ".$order->ID); |
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
} else { |
222
|
|
|
//send immediately |
223
|
|
|
return true; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* returns true if it is too late to send the feedback step |
229
|
|
|
* @param Order |
230
|
|
|
* @return Boolean |
231
|
|
|
*/ |
232
|
|
|
protected function isExpiredFeedbackStep(Order $order) |
233
|
|
|
{ |
234
|
|
|
if ($this->MaxDays) { |
|
|
|
|
235
|
|
|
$log = $order->SubmissionLog(); |
236
|
|
|
if ($log) { |
237
|
|
|
$createdTS = strtotime($log->Created); |
238
|
|
|
$nowTS = strtotime('now'); |
239
|
|
|
$stopSendingTS = strtotime("+{$this->MaxDays} days", $createdTS); |
|
|
|
|
240
|
|
|
return ($stopSendingTS < $nowTS) ? true : false; |
241
|
|
|
} else { |
242
|
|
|
user_error("can not find order log for ".$order->ID); |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
} else { |
246
|
|
|
return true; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
251
|
|
|
{ |
252
|
|
|
return OrderEmailRecord::get()->filter( |
253
|
|
|
array( |
254
|
|
|
"OrderEmailRecord.OrderID" => $order->ID, |
255
|
|
|
"OrderEmailRecord.OrderStepID" => $this->ID, |
256
|
|
|
"OrderEmailRecord.Result" => 1 |
257
|
|
|
) |
258
|
|
|
)->count() ? true : false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Event handler called before writing to the database. |
264
|
|
|
*/ |
265
|
|
|
public function onBeforeWrite() |
266
|
|
|
{ |
267
|
|
|
parent::onBeforeWrite(); |
268
|
|
|
$deferTime = $this->MinDays * 86400; |
|
|
|
|
269
|
|
|
if ($this->DeferTimeInSeconds < $deferTime) { |
|
|
|
|
270
|
|
|
$this->DeferTimeInSeconds = $deferTime; |
|
|
|
|
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.