|
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
|
|
|
What is the <strong>maxinum number of days to wait after completing an order</strong> before this email should be sent?<br> |
|
75
|
|
|
<strong>If set to zero, this step will be ignored.</strong>' |
|
76
|
|
|
); |
|
77
|
|
|
$fields->addFieldsToTab( |
|
78
|
|
|
'Root.CustomerMessage', |
|
79
|
|
|
array( |
|
80
|
|
|
HTMLEditorField::create( |
|
81
|
|
|
'MessageAfterProductsList', |
|
82
|
|
|
_t('OrderStepFeedback.MESSAGEAFTERPRODUCTSLIST', 'Message After Products List') |
|
83
|
|
|
)->setRightTitle( |
|
84
|
|
|
'Optional message displayed after the list of products' |
|
85
|
|
|
)->setRows(3), |
|
86
|
|
|
TextField::create( |
|
87
|
|
|
'LinkText', |
|
88
|
|
|
_t('OrderStepFeedback.BUTTONTEXT', 'Link Text') |
|
89
|
|
|
)->setRightTitle('This is the text displayed on the "order again" link/button') |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
if($this->MinDays) { |
|
|
|
|
|
|
93
|
|
|
$fields->replaceField( |
|
94
|
|
|
'DeferTimeInSeconds', |
|
95
|
|
|
$fields->dataFieldByName('DeferTimeInSeconds')->performReadonlyTransformation() |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
return $fields; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function initStep(Order $order) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->SendFeedbackEmail) { |
|
|
|
|
|
|
104
|
|
|
Config::inst()->update("OrderStep", "number_of_days_to_send_update_email", $this->MaxDays); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function doStep(Order $order) |
|
110
|
|
|
{ |
|
111
|
|
|
//ignore altogether? |
|
112
|
|
|
if ($this->SendFeedbackEmail) { |
|
|
|
|
|
|
113
|
|
|
// too late to send |
|
114
|
|
|
if ($this->isExpiredFeedbackStep($order)) { |
|
115
|
|
|
if ($this->Config()->get("verbose")) { |
|
116
|
|
|
DB::alteration_message(" - Time to send feedback is expired"); |
|
117
|
|
|
} |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
//is now the right time to send? |
|
121
|
|
|
elseif ($this->isReadyToGo($order)) { |
|
122
|
|
|
$subject = $this->EmailSubject; |
|
|
|
|
|
|
123
|
|
|
$message = $this->CustomerMessage; |
|
|
|
|
|
|
124
|
|
|
if ($this->hasBeenSent($order, false)) { |
|
125
|
|
|
if ($this->Config()->get("verbose")) { |
|
126
|
|
|
DB::alteration_message(" - already sent!"); |
|
127
|
|
|
} |
|
128
|
|
|
return true; //do nothing |
|
129
|
|
|
} else { |
|
130
|
|
|
if ($this->Config()->get("verbose")) { |
|
131
|
|
|
DB::alteration_message(" - Sending it now!"); |
|
132
|
|
|
} |
|
133
|
|
|
return $order->sendEmail( |
|
134
|
|
|
$this->getEmailClassName(), |
|
135
|
|
|
$subject, |
|
136
|
|
|
$message, |
|
137
|
|
|
$resend = false, |
|
138
|
|
|
$adminOnlyOrToEmail = false |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
//wait until later.... |
|
143
|
|
|
else { |
|
144
|
|
|
if ($this->Config()->get("verbose")) { |
|
145
|
|
|
DB::alteration_message(" - We need to wait until minimum number of days."); |
|
146
|
|
|
} |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* can continue if emails has been sent or if there is no need to send a receipt. |
|
156
|
|
|
* @param DataObject $order Order |
|
157
|
|
|
* @return DataObject | Null - DataObject = next OrderStep |
|
|
|
|
|
|
158
|
|
|
**/ |
|
159
|
|
|
public function nextStep(Order $order) |
|
160
|
|
|
{ |
|
161
|
|
|
if ( |
|
162
|
|
|
! $this->SendFeedbackEmail || |
|
|
|
|
|
|
163
|
|
|
$this->hasBeenSent($order, false) || |
|
164
|
|
|
$this->isExpiredFeedbackStep($order) |
|
165
|
|
|
) { |
|
166
|
|
|
if ($this->Config()->get("verbose")) { |
|
167
|
|
|
DB::alteration_message(" - Moving to next step"); |
|
168
|
|
|
} |
|
169
|
|
|
return parent::nextStep($order); |
|
170
|
|
|
} |
|
171
|
|
|
if ($this->Config()->get("verbose")) { |
|
172
|
|
|
DB::alteration_message(" - no next step: has not been sent"); |
|
173
|
|
|
} |
|
174
|
|
|
return null; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* For some ordersteps this returns true... |
|
179
|
|
|
* @return Boolean |
|
180
|
|
|
**/ |
|
181
|
|
|
protected function hasCustomerMessage() |
|
182
|
|
|
{ |
|
183
|
|
|
return true; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Explains the current order step. |
|
188
|
|
|
* @return String |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function myDescription() |
|
191
|
|
|
{ |
|
192
|
|
|
return "The customer is sent a feedback request email."; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* returns true if the Minimum number of days is met.... |
|
197
|
|
|
* @param Order |
|
198
|
|
|
* @return Boolean |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function isReadyToGo(Order $order) |
|
201
|
|
|
{ |
|
202
|
|
|
if ($this->MinDays) { |
|
|
|
|
|
|
203
|
|
|
$log = $order->SubmissionLog(); |
|
204
|
|
|
if ($log) { |
|
205
|
|
|
$createdTS = strtotime($log->Created); |
|
206
|
|
|
$nowTS = strtotime('now'); |
|
207
|
|
|
$startSendingTS = strtotime("+{$this->MinDays} days", $createdTS); |
|
|
|
|
|
|
208
|
|
|
//current TS = 10 |
|
209
|
|
|
//order TS = 8 |
|
210
|
|
|
//add 4 days: 12 |
|
211
|
|
|
//thus if 12 <= now then go for it (start point in time has passed) |
|
212
|
|
|
if ($this->Config()->get("verbose")) { |
|
213
|
|
|
DB::alteration_message("Time comparison: Start Sending TS: ".$startSendingTS." current TS: ".$nowTS.". If SSTS > NowTS then Go for it."); |
|
214
|
|
|
} |
|
215
|
|
|
return ($startSendingTS <= $nowTS) ? true : false; |
|
216
|
|
|
} else { |
|
217
|
|
|
user_error("can not find order log for ".$order->ID); |
|
218
|
|
|
return false; |
|
219
|
|
|
} |
|
220
|
|
|
} else { |
|
221
|
|
|
//send immediately |
|
222
|
|
|
return true; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* returns true if it is too late to send the feedback step |
|
228
|
|
|
* @param Order |
|
229
|
|
|
* @return Boolean |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function isExpiredFeedbackStep(Order $order) |
|
232
|
|
|
{ |
|
233
|
|
|
if ($this->MaxDays) { |
|
|
|
|
|
|
234
|
|
|
$log = $order->SubmissionLog(); |
|
235
|
|
|
if ($log) { |
|
236
|
|
|
$createdTS = strtotime($log->Created); |
|
237
|
|
|
$nowTS = strtotime('now'); |
|
238
|
|
|
$stopSendingTS = strtotime("+{$this->MaxDays} days", $createdTS); |
|
|
|
|
|
|
239
|
|
|
return ($stopSendingTS < $nowTS) ? true : false; |
|
240
|
|
|
} else { |
|
241
|
|
|
user_error("can not find order log for ".$order->ID); |
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
|
|
} else { |
|
245
|
|
|
return true; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
|
250
|
|
|
{ |
|
251
|
|
|
return OrderEmailRecord::get()->filter( |
|
252
|
|
|
array( |
|
253
|
|
|
"OrderEmailRecord.OrderID" => $order->ID, |
|
254
|
|
|
"OrderEmailRecord.OrderStepID" => $this->ID, |
|
255
|
|
|
"OrderEmailRecord.Result" => 1 |
|
256
|
|
|
) |
|
257
|
|
|
)->count() ? true : false; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Event handler called before writing to the database. |
|
263
|
|
|
*/ |
|
264
|
|
|
public function onBeforeWrite() |
|
265
|
|
|
{ |
|
266
|
|
|
parent::onBeforeWrite(); |
|
267
|
|
|
$deferTime = $this->MinDays * 86400; |
|
|
|
|
|
|
268
|
|
|
if($this->DeferTimeInSeconds < $deferTime) { |
|
|
|
|
|
|
269
|
|
|
$this->DeferTimeInSeconds = $deferTime; |
|
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
|
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.