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
|
|
|
return $fields; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function initStep(Order $order) |
96
|
|
|
{ |
97
|
|
|
if ($this->SendFeedbackEmail) { |
|
|
|
|
98
|
|
|
Config::inst()->update("OrderStep", "number_of_days_to_send_update_email", $this->MaxDays); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function doStep(Order $order) |
104
|
|
|
{ |
105
|
|
|
//ignore altogether? |
106
|
|
|
if ($this->SendFeedbackEmail) { |
|
|
|
|
107
|
|
|
// too late to send |
108
|
|
|
if ($this->isExpiredFeedbackStep($order)) { |
109
|
|
|
if ($this->Config()->get("verbose")) { |
110
|
|
|
DB::alteration_message(" - Time to send feedback is expired"); |
111
|
|
|
} |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
//is now the right time to send? |
115
|
|
|
elseif ($this->isReadyToGo($order)) { |
116
|
|
|
$subject = $this->EmailSubject; |
|
|
|
|
117
|
|
|
$message = $this->CustomerMessage; |
|
|
|
|
118
|
|
|
if ($this->hasBeenSent($order, false)) { |
119
|
|
|
if ($this->Config()->get("verbose")) { |
120
|
|
|
DB::alteration_message(" - already sent!"); |
121
|
|
|
} |
122
|
|
|
return true; //do nothing |
123
|
|
|
} else { |
124
|
|
|
if ($this->Config()->get("verbose")) { |
125
|
|
|
DB::alteration_message(" - Sending it now!"); |
126
|
|
|
} |
127
|
|
|
return $order->sendEmail( |
128
|
|
|
$this->getEmailClassName(), |
129
|
|
|
$subject, |
130
|
|
|
$message, |
131
|
|
|
$resend = false, |
132
|
|
|
$adminOnlyOrToEmail = false |
|
|
|
|
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
//wait until later.... |
137
|
|
|
else { |
138
|
|
|
if ($this->Config()->get("verbose")) { |
139
|
|
|
DB::alteration_message(" - We need to wait until minimum number of days."); |
140
|
|
|
} |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} else { |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* can continue if emails has been sent or if there is no need to send a receipt. |
150
|
|
|
* @param DataObject $order Order |
151
|
|
|
* @return DataObject | Null - DataObject = next OrderStep |
|
|
|
|
152
|
|
|
**/ |
153
|
|
|
public function nextStep(Order $order) |
154
|
|
|
{ |
155
|
|
|
if ( |
156
|
|
|
! $this->SendFeedbackEmail || |
|
|
|
|
157
|
|
|
$this->hasBeenSent($order, false) || |
158
|
|
|
$this->isExpiredFeedbackStep($order) |
159
|
|
|
) { |
160
|
|
|
if ($this->Config()->get("verbose")) { |
161
|
|
|
DB::alteration_message(" - Moving to next step"); |
162
|
|
|
} |
163
|
|
|
return parent::nextStep($order); |
164
|
|
|
} |
165
|
|
|
if ($this->Config()->get("verbose")) { |
166
|
|
|
DB::alteration_message(" - no next step: has not been sent"); |
167
|
|
|
} |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* For some ordersteps this returns true... |
173
|
|
|
* @return Boolean |
174
|
|
|
**/ |
175
|
|
|
protected function hasCustomerMessage() |
176
|
|
|
{ |
177
|
|
|
return true; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Explains the current order step. |
182
|
|
|
* @return String |
183
|
|
|
*/ |
184
|
|
|
protected function myDescription() |
185
|
|
|
{ |
186
|
|
|
return "The customer is sent a feedback request email."; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* returns true if the Minimum number of days is met.... |
191
|
|
|
* @param Order |
192
|
|
|
* @return Boolean |
193
|
|
|
*/ |
194
|
|
|
protected function isReadyToGo(Order $order) |
195
|
|
|
{ |
196
|
|
|
if ($this->MinDays) { |
|
|
|
|
197
|
|
|
$log = $order->SubmissionLog(); |
198
|
|
|
if ($log) { |
199
|
|
|
$createdTS = strtotime($log->Created); |
200
|
|
|
$nowTS = strtotime('now'); |
201
|
|
|
$startSendingTS = strtotime("+{$this->MinDays} days", $createdTS); |
|
|
|
|
202
|
|
|
//current TS = 10 |
203
|
|
|
//order TS = 8 |
204
|
|
|
//add 4 days: 12 |
205
|
|
|
//thus if 12 <= now then go for it (start point in time has passed) |
206
|
|
|
if ($this->Config()->get("verbose")) { |
207
|
|
|
DB::alteration_message("Time comparison: Start Sending TS: ".$startSendingTS." current TS: ".$nowTS.". If SSTS > NowTS then Go for it."); |
208
|
|
|
} |
209
|
|
|
return ($startSendingTS <= $nowTS) ? true : false; |
210
|
|
|
} else { |
211
|
|
|
user_error("can not find order log for ".$order->ID); |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
} else { |
215
|
|
|
//send immediately |
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* returns true if it is too late to send the feedback step |
222
|
|
|
* @param Order |
223
|
|
|
* @return Boolean |
224
|
|
|
*/ |
225
|
|
|
protected function isExpiredFeedbackStep(Order $order) |
226
|
|
|
{ |
227
|
|
|
if ($this->MaxDays) { |
|
|
|
|
228
|
|
|
$log = $order->SubmissionLog(); |
229
|
|
|
if ($log) { |
230
|
|
|
$createdTS = strtotime($log->Created); |
231
|
|
|
$nowTS = strtotime('now'); |
232
|
|
|
$stopSendingTS = strtotime("+{$this->MaxDays} days", $createdTS); |
|
|
|
|
233
|
|
|
return ($stopSendingTS < $nowTS) ? true : false; |
234
|
|
|
} else { |
235
|
|
|
user_error("can not find order log for ".$order->ID); |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
} else { |
239
|
|
|
return true; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function hasBeenSent(Order $order, $checkDateOfOrder = true) |
244
|
|
|
{ |
245
|
|
|
return OrderEmailRecord::get()->filter( |
246
|
|
|
array( |
247
|
|
|
"OrderEmailRecord.OrderID" => $order->ID, |
248
|
|
|
"OrderEmailRecord.OrderStepID" => $this->ID, |
249
|
|
|
"OrderEmailRecord.Result" => 1 |
250
|
|
|
) |
251
|
|
|
)->count() ? true : false; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
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.