|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class EmailReminder_NotificationSchedule extends DataObject |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @var int |
|
8
|
|
|
*/ |
|
9
|
|
|
private static $grace_days = 3; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $default_data_object = 'Member'; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $default_date_field = ''; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $default_email_field = ''; |
|
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $replaceable_record_fields = array('FirstName', 'Surname', 'Email'); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $mail_out_class = 'EmailReminder_DailyMailOut'; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
private static $singular_name = 'Email Reminder Schedule'; |
|
|
|
|
|
|
36
|
|
|
public function i18n_singular_name() |
|
37
|
|
|
{ |
|
38
|
|
|
return self::$singular_name; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private static $plural_name = 'Email Reminder Schedules'; |
|
|
|
|
|
|
42
|
|
|
public function i18n_plural_name() |
|
43
|
|
|
{ |
|
44
|
|
|
return self::$plural_name; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private static $db = array( |
|
|
|
|
|
|
48
|
|
|
'DataObject' => 'Varchar(100)', |
|
49
|
|
|
'EmailField' => 'Varchar(100)', |
|
50
|
|
|
'DateField' => 'Varchar(100)', |
|
51
|
|
|
'Days' => 'Int', |
|
52
|
|
|
'RepeatDays' => 'Int', |
|
53
|
|
|
'BeforeAfter' => "Enum('before,after,immediately','before')", |
|
54
|
|
|
'EmailFrom' => 'Varchar(100)', |
|
55
|
|
|
'EmailSubject' => 'Varchar(100)', |
|
56
|
|
|
'Content' => 'HTMLText', |
|
57
|
|
|
'Disable' => 'Boolean', |
|
58
|
|
|
'SendTestTo' => 'Text' |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
63
|
|
|
'EmailsSent' => 'EmailReminder_EmailRecord' |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
67
|
|
|
'EmailSubject', |
|
68
|
|
|
'Days' |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
private static $field_labels = array( |
|
|
|
|
|
|
72
|
|
|
'DataObject' => 'Class/Table', |
|
73
|
|
|
'Days' => 'Days from Expiry', |
|
74
|
|
|
'RepeatDays' => 'Repeat cycle days' |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
public function populateDefaults() |
|
78
|
|
|
{ |
|
79
|
|
|
parent::populateDefaults(); |
|
80
|
|
|
$this->DataObject = $this->Config()->get('default_data_object'); |
|
|
|
|
|
|
81
|
|
|
$this->EmailField = $this->Config()->get('default_email_field'); |
|
|
|
|
|
|
82
|
|
|
$this->DateField = $this->Config()->get('default_date_field'); |
|
|
|
|
|
|
83
|
|
|
$this->Days = 7; |
|
|
|
|
|
|
84
|
|
|
$this->RepeatDays = 300; |
|
|
|
|
|
|
85
|
|
|
$this->BeforeAfter = 'before'; |
|
|
|
|
|
|
86
|
|
|
$this->EmailFrom = Config::inst()->get('Email', 'admin_email'); |
|
|
|
|
|
|
87
|
|
|
$this->EmailSubject = 'Your memberships expires in [days] days'; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getCMSFields() |
|
91
|
|
|
{ |
|
92
|
|
|
$fields = parent::getCMSFields(); |
|
93
|
|
|
|
|
94
|
|
|
$emailsSentField = $fields->dataFieldByName('EmailsSent'); |
|
95
|
|
|
$fields->removeFieldFromTab('Root', 'EmailsSent'); |
|
96
|
|
|
|
|
97
|
|
|
$fields->addFieldToTab( |
|
98
|
|
|
'Root.Main', |
|
99
|
|
|
CheckboxField::create('Disable') |
|
100
|
|
|
); |
|
101
|
|
|
$fields->addFieldToTab( |
|
102
|
|
|
'Root.Main', |
|
103
|
|
|
$dataObjecField = DropdownField::create( |
|
104
|
|
|
'DataObject', |
|
105
|
|
|
'Table/Class Name', |
|
106
|
|
|
$this->dataObjectOptions() |
|
107
|
|
|
) |
|
108
|
|
|
->setRightTitle('Type a valid table/class name') |
|
109
|
|
|
); |
|
110
|
|
|
if ($this->Config()->get('default_data_object')) { |
|
111
|
|
|
$fields->replaceField('DataObject', $dataObjecField->performReadonlyTransformation()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$fields->addFieldToTab( |
|
117
|
|
|
'Root.Main', |
|
118
|
|
|
$emailFieldField = DropdownField::create( |
|
119
|
|
|
'EmailField', |
|
120
|
|
|
'Email Field', |
|
121
|
|
|
$this->emailFieldOptions() |
|
122
|
|
|
) |
|
123
|
|
|
->setRightTitle('Select the field that will contain a valid email address') |
|
124
|
|
|
->setEmptyString('[ Please select ]') |
|
125
|
|
|
); |
|
126
|
|
|
if ($this->Config()->get('default_email_field')) { |
|
127
|
|
|
|
|
128
|
|
|
$fields->replaceField('EmailField', $emailFieldField->performReadonlyTransformation()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$fields->addFieldToTab( |
|
132
|
|
|
'Root.Main', |
|
133
|
|
|
$dateFieldField = DropdownField::create( |
|
134
|
|
|
'DateField', |
|
135
|
|
|
'Date Field', |
|
136
|
|
|
$this->dateFieldOptions() |
|
137
|
|
|
) |
|
138
|
|
|
->setRightTitle('Select a valid Date field to calculate when reminders should be sent') |
|
139
|
|
|
->setEmptyString('[ Please select ]') |
|
140
|
|
|
); |
|
141
|
|
|
if ($this->Config()->get('default_date_field')) { |
|
142
|
|
|
$fields->replaceField('DateField', $dateFieldField->performReadonlyTransformation()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$fields->removeFieldsFromTab( |
|
146
|
|
|
'Root.Main', |
|
147
|
|
|
array('Days', 'BeforeAfter', 'RepeatDays') |
|
148
|
|
|
); |
|
149
|
|
|
$fields->addFieldsToTab( |
|
150
|
|
|
'Root.Main', |
|
151
|
|
|
array( |
|
152
|
|
|
DropdownField::create('BeforeAfter', 'Before / After Expiration', array('before' => 'before', 'after' => 'after', 'immediately' => 'immediately')) |
|
153
|
|
|
->setRightTitle('Are the days listed above before or after the actual expiration date.'), |
|
154
|
|
|
NumericField::create('Days', 'Days') |
|
155
|
|
|
->setRightTitle('How many days in advance (before) or in arrears (after) of the expiration date should this email be sent? </br>This field is ignored if set to send immediately.'), |
|
156
|
|
|
NumericField::create('RepeatDays', 'Repeat Cycle Days') |
|
157
|
|
|
->setRightTitle(' |
|
158
|
|
|
Number of days after which the same reminder can be sent to the same email address. |
|
159
|
|
|
<br />We allow an e-mail to be sent to one specific email address for one specific reminder only once. |
|
160
|
|
|
<br />In this field you can indicate for how long we will apply this rule.' |
|
161
|
|
|
) |
|
162
|
|
|
) |
|
163
|
|
|
); |
|
164
|
|
|
$fields->addFieldsToTab( |
|
165
|
|
|
'Root.EmailContent', |
|
166
|
|
|
array( |
|
167
|
|
|
TextField::create('EmailFrom', 'Email From Address') |
|
168
|
|
|
->setRightTitle('The email from address, eg: "My Company <[email protected]>"'), |
|
169
|
|
|
$subjectField = TextField::create('EmailSubject', 'Email Subject Line') |
|
170
|
|
|
->setRightTitle('The subject of the email'), |
|
171
|
|
|
$contentField = HTMLEditorField::create('Content', 'Email Content') |
|
172
|
|
|
->SetRows(20) |
|
173
|
|
|
) |
|
174
|
|
|
); |
|
175
|
|
|
if ($obj = $this->getReplacerObject()) { |
|
|
|
|
|
|
176
|
|
|
$html = $obj->replaceHelpList($asHTML = true); |
|
177
|
|
|
$otherFieldsThatCanBeUsed = $this->getFieldsFromDataObject(array('*')); |
|
178
|
|
|
$replaceableFields = $this->Config()->get('replaceable_record_fields'); |
|
179
|
|
|
if (count($otherFieldsThatCanBeUsed)) { |
|
180
|
|
|
$html .= '<h3>You can also use the record fields (not replaced in tests):</h3><ul>'; |
|
181
|
|
|
foreach ($otherFieldsThatCanBeUsed as $key => $value) { |
|
182
|
|
|
if (in_array($key, $replaceableFields)) { |
|
183
|
|
|
$html .= '<li><strong>$'.$key.'</strong> <span>'.$value.'</span></li>'; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
$html .= '</ul>'; |
|
188
|
|
|
$subjectField->setRightTitle('for replacement options, please see below ...'); |
|
189
|
|
|
$contentField->setRightTitle($html); |
|
190
|
|
|
} |
|
191
|
|
|
$fields->addFieldsToTab( |
|
192
|
|
|
'Root.Sent', |
|
193
|
|
|
array( |
|
194
|
|
|
TextareaField::create('SendTestTo', 'Send test email to ...') |
|
195
|
|
|
->setRightTitle(' |
|
196
|
|
|
Separate emails by commas, a test email will be sent every time you save this Email Reminder, if you do not want test emails to be sent make sure this field is empty |
|
197
|
|
|
' |
|
198
|
|
|
) |
|
199
|
|
|
->SetRows(3) |
|
200
|
|
|
) |
|
201
|
|
|
); |
|
202
|
|
|
if ($emailsSentField) { |
|
203
|
|
|
$config = $emailsSentField->getConfig(); |
|
|
|
|
|
|
204
|
|
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
205
|
|
|
$fields->addFieldToTab( |
|
206
|
|
|
'Root.Sent', |
|
207
|
|
|
$emailsSentField |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
$records = $this->CurrentRecords(); |
|
211
|
|
|
if ($records) { |
|
212
|
|
|
$fields->addFieldsToTab( |
|
213
|
|
|
'Root.Review', |
|
214
|
|
|
array( |
|
215
|
|
|
GridField::create( |
|
216
|
|
|
'CurrentRecords', |
|
217
|
|
|
'Today we are sending to ...', |
|
218
|
|
|
$records |
|
219
|
|
|
), |
|
220
|
|
|
LiteralField::create( |
|
221
|
|
|
'SampleSelectStatement', |
|
222
|
|
|
'<h3>Here is a sample statement used to select records:</h3> |
|
223
|
|
|
<pre>'.$this->whereStatementForDays().'</pre>' |
|
224
|
|
|
), |
|
225
|
|
|
LiteralField::create( |
|
226
|
|
|
'SampleFieldDataForRecords', |
|
227
|
|
|
'<h3>sample of '.$this->DateField.' field values:</h3> |
|
|
|
|
|
|
228
|
|
|
<li>'.implode('</li><li>', $this->SampleFieldDataForRecords()).'</li>' |
|
229
|
|
|
) |
|
230
|
|
|
) |
|
231
|
|
|
); |
|
232
|
|
|
} |
|
233
|
|
|
return $fields; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return array |
|
|
|
|
|
|
238
|
|
|
*/ |
|
239
|
|
|
protected function dataObjectOptions() |
|
240
|
|
|
{ |
|
241
|
|
|
return ClassInfo::subclassesFor("DataObject"); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function emailFieldOptions() |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->getFieldsFromDataObject(array('Varchar', 'Email')); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function dateFieldOptions() |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->getFieldsFromDataObject(array('Date')); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* list of database fields available |
|
263
|
|
|
* @param array $fieldTypeMatchArray - strpos filter |
|
264
|
|
|
* @return array |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function getFieldsFromDataObject($fieldTypeMatchArray = array()) |
|
267
|
|
|
{ |
|
268
|
|
|
$array = array(); |
|
269
|
|
|
if ($this->hasValidDataObject()) { |
|
270
|
|
|
$object = Injector::inst()->get($this->DataObject); |
|
|
|
|
|
|
271
|
|
|
if ($object) { |
|
272
|
|
|
$allOptions = $object->stat('db'); |
|
273
|
|
|
$fieldLabels = $object->fieldLabels(); |
|
274
|
|
|
foreach ($allOptions as $fieldName => $fieldType) { |
|
275
|
|
|
foreach ($fieldTypeMatchArray as $matchString) { |
|
276
|
|
|
if ((strpos($fieldType, $matchString) !== false) || $matchString == '*') { |
|
277
|
|
|
if (isset($fieldLabels[$fieldName])) { |
|
278
|
|
|
$label = $fieldLabels[$fieldName]; |
|
279
|
|
|
} else { |
|
280
|
|
|
$label = $fieldName; |
|
281
|
|
|
} |
|
282
|
|
|
$array[$fieldName] = $label; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
return $array; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Test if valid classname has been set |
|
294
|
|
|
* @param null |
|
295
|
|
|
* @return Boolean |
|
296
|
|
|
*/ |
|
297
|
|
|
public function hasValidDataObject() |
|
298
|
|
|
{ |
|
299
|
|
|
return (! $this->DataObject) || ClassInfo::exists($this->DataObject) ? true : false; |
|
|
|
|
|
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Test if valid fields have been set |
|
304
|
|
|
* @param null |
|
305
|
|
|
* @return Boolean |
|
306
|
|
|
*/ |
|
307
|
|
|
public function hasValidDataObjectFields() |
|
308
|
|
|
{ |
|
309
|
|
|
if (!$this->hasValidDataObject()) { |
|
310
|
|
|
return false; |
|
311
|
|
|
} |
|
312
|
|
|
$emailFieldOptions = $this->emailFieldOptions(); |
|
313
|
|
|
if (!isset($emailFieldOptions[$this->EmailField])) { |
|
|
|
|
|
|
314
|
|
|
return false; |
|
315
|
|
|
} |
|
316
|
|
|
$dateFieldOptions = $this->dateFieldOptions(); |
|
317
|
|
|
if (!isset($dateFieldOptions[$this->DateField])) { |
|
|
|
|
|
|
318
|
|
|
return false; |
|
319
|
|
|
} |
|
320
|
|
|
return true; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @return string |
|
325
|
|
|
*/ |
|
326
|
|
|
public function getTitle() |
|
327
|
|
|
{ |
|
328
|
|
|
return ( |
|
329
|
|
|
$this->hasValidDataObjectFields()) ? |
|
330
|
|
|
'[' . $this->EmailSubject . '] // send ' . $this->Days . ' days '.$this->BeforeAfter.' Expiration Date' |
|
|
|
|
|
|
331
|
|
|
: |
|
332
|
|
|
'uncompleted'; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @return boolean |
|
337
|
|
|
*/ |
|
338
|
|
|
public function hasValidFields() |
|
339
|
|
|
{ |
|
340
|
|
|
if (!$this->hasValidDataObject()) { |
|
341
|
|
|
return false; |
|
342
|
|
|
} |
|
343
|
|
|
if (!$this->hasValidDataObjectFields()) { |
|
344
|
|
|
return false; |
|
345
|
|
|
} |
|
346
|
|
|
if ($this->EmailFrom && $this->EmailSubject && $this->Content) { |
|
|
|
|
|
|
347
|
|
|
return true; |
|
348
|
|
|
} |
|
349
|
|
|
return false; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* @return boolean |
|
|
|
|
|
|
354
|
|
|
*/ |
|
355
|
|
|
public function validate() |
|
356
|
|
|
{ |
|
357
|
|
|
$valid = parent::validate(); |
|
358
|
|
|
if ($this->exists()) { |
|
359
|
|
|
if (! $this->hasValidDataObject()) { |
|
360
|
|
|
$valid->error('Please enter valid Tabe/Class name ("' . htmlspecialchars($this->DataObject) .'" does not exist)'); |
|
|
|
|
|
|
361
|
|
|
} elseif (! $this->hasValidDataObjectFields()) { |
|
362
|
|
|
$valid->error('Please select valid fields for both Email & Date'); |
|
363
|
|
|
} elseif (! $this->hasValidFields()) { |
|
364
|
|
|
$valid->error('Please fill in all fields. Make sure not to forget the email details (from who, subject, content)'); |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
return $valid; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
public function onBeforeWrite() |
|
371
|
|
|
{ |
|
372
|
|
|
parent::onBeforeWrite(); |
|
373
|
|
|
if ($this->RepeatDays < ($this->Days * 3)) { |
|
|
|
|
|
|
374
|
|
|
$this->RepeatDays = ($this->Days * 3); |
|
|
|
|
|
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
public function onAfterWrite() |
|
379
|
|
|
{ |
|
380
|
|
|
parent::onAfterWrite(); |
|
381
|
|
|
if ($this->SendTestTo) { |
|
|
|
|
|
|
382
|
|
|
if ($mailOutObject = $this->getMailOutObject()) { |
|
383
|
|
|
$mailOutObject->setTestOnly(true); |
|
384
|
|
|
$mailOutObject->setVerbose(true); |
|
385
|
|
|
$mailOutObject->run(null); |
|
|
|
|
|
|
386
|
|
|
} |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* |
|
392
|
|
|
* @return null | EmailReminder_ReplacerClassInterface |
|
393
|
|
|
*/ |
|
394
|
|
|
public function getReplacerObject() |
|
395
|
|
|
{ |
|
396
|
|
|
if ($mailOutObject = $this->getMailOutObject()) { |
|
397
|
|
|
return $mailOutObject->getReplacerObject(); |
|
398
|
|
|
} |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* |
|
403
|
|
|
* @return null | ScheduledTask |
|
|
|
|
|
|
404
|
|
|
*/ |
|
405
|
|
|
public function getMailOutObject() |
|
406
|
|
|
{ |
|
407
|
|
|
$mailOutClass = $this->Config()->get('mail_out_class'); |
|
408
|
|
|
if (class_exists($mailOutClass)) { |
|
409
|
|
|
$obj = Injector::inst()->get($mailOutClass); |
|
410
|
|
|
if ($obj instanceof BuildTask) { |
|
411
|
|
|
return $obj; |
|
412
|
|
|
} else { |
|
413
|
|
|
user_error($mailOutClass.' needs to be an instance of a Scheduled Task'); |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* @param int $limit |
|
420
|
|
|
* @return array |
|
|
|
|
|
|
421
|
|
|
*/ |
|
422
|
|
|
public function SampleFieldDataForRecords($limit = 200) |
|
423
|
|
|
{ |
|
424
|
|
|
if ($this->hasValidFields()) { |
|
425
|
|
|
$className = $this->DataObject; |
|
|
|
|
|
|
426
|
|
|
$objects = $className::get()->sort('RAND()') |
|
427
|
|
|
->where('"'.$this->DateField.'" IS NOT NULL AND "'.$this->DateField.'" <> \'\' AND "'.$this->DateField.'" <> 0') |
|
|
|
|
|
|
428
|
|
|
->limit($limit); |
|
429
|
|
|
if ($objects->count()) { |
|
430
|
|
|
return array_unique($objects->column($this->DateField)); |
|
|
|
|
|
|
431
|
|
|
} else { |
|
432
|
|
|
return array(); |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* @return DataList | null |
|
440
|
|
|
*/ |
|
441
|
|
|
public function CurrentRecords() |
|
442
|
|
|
{ |
|
443
|
|
|
if ($this->hasValidFields()) { |
|
444
|
|
|
$className = $this->DataObject; |
|
|
|
|
|
|
445
|
|
|
|
|
446
|
|
|
// Use StartsWith to match Date and DateTime fields |
|
447
|
|
|
$records = $className::get()->where($this->whereStatementForDays()); |
|
448
|
|
|
return $records; |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* BeforeAfter = 'after' |
|
454
|
|
|
* Days = 3 |
|
455
|
|
|
* GraceDays = 2 |
|
456
|
|
|
* -> minDays = -5 days start of day |
|
457
|
|
|
* -> maxDays = -3 days end of day |
|
458
|
|
|
* |
|
459
|
|
|
* @return string |
|
460
|
|
|
*/ |
|
461
|
|
|
protected function whereStatementForDays() |
|
462
|
|
|
{ |
|
463
|
|
|
if ($this->hasValidFields()) { |
|
464
|
|
|
$sign = $this->BeforeAfter == 'before' ? '+' : '-'; |
|
|
|
|
|
|
465
|
|
|
$graceDays = 10; //Config::inst()->get('EmailReminder_NotificationSchedule', 'grace_days'); |
|
|
|
|
|
|
466
|
|
|
|
|
467
|
|
|
if ($sign == '+') { |
|
468
|
|
|
$minDays = $sign . ($this->Days - $graceDays) . ' days'; |
|
|
|
|
|
|
469
|
|
|
$maxDays = $sign . $this->Days . ' days'; |
|
|
|
|
|
|
470
|
|
|
$minDate = date('Y-m-d', strtotime($minDays)).' 00:00:00'; |
|
471
|
|
|
$maxDate = date('Y-m-d', strtotime($maxDays)).' 23:59:59'; |
|
472
|
|
|
} else { |
|
473
|
|
|
$minDays = $sign . ($this->Days ) . ' days'; |
|
|
|
|
|
|
474
|
|
|
$maxDays = $sign . $this->Days - $graceDays . ' days'; |
|
|
|
|
|
|
475
|
|
|
//we purposely change these days around here ... |
|
476
|
|
|
$minDate = date('Y-m-d', strtotime($maxDays)).' 00:00:00'; |
|
477
|
|
|
$maxDate = date('Y-m-d', strtotime($minDays)).' 23:59:59'; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
return '("'. $this->DateField.'" BETWEEN \''.$minDate.'\' AND \''.$maxDate.'\')'; |
|
|
|
|
|
|
481
|
|
|
} |
|
482
|
|
|
return '1 == 2'; |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
public function sendEmailNow($recordOrEmail) |
|
486
|
|
|
{ |
|
487
|
|
View Code Duplication |
if (is_object($recordOrEmail)) { |
|
|
|
|
|
|
488
|
|
|
$email_field = $this->EmailField; |
|
|
|
|
|
|
489
|
|
|
$email = $recordOrEmail->$email_field; |
|
490
|
|
|
$record = $recordOrEmail; |
|
|
|
|
|
|
491
|
|
|
} else { |
|
492
|
|
|
$email = strtolower(trim($recordOrEmail)); |
|
493
|
|
|
$record = Injector::inst()->get($reminder->DataObject); |
|
|
|
|
|
|
494
|
|
|
} |
|
495
|
|
|
if (Email::validEmailAddress($email)) { |
|
|
|
|
|
|
496
|
|
|
$send = true; |
|
|
|
|
|
|
497
|
|
|
$filter = array( |
|
498
|
|
|
'EmailTo' => $email, |
|
499
|
|
|
'EmailReminder_NotificationScheduleID' => $this->ID |
|
500
|
|
|
); |
|
501
|
|
|
$logs = EmailReminder_EmailRecord::get()->filter($filter); |
|
502
|
|
|
$send = true; |
|
503
|
|
|
foreach ($logs as $log) { |
|
504
|
|
|
if (! $log->canSendAgain()) { |
|
505
|
|
|
$send = false; |
|
506
|
|
|
break; |
|
507
|
|
|
} |
|
508
|
|
|
} |
|
509
|
|
|
if ($send) { |
|
510
|
|
|
$log = EmailReminder_EmailRecord::create($filter); |
|
511
|
|
|
|
|
512
|
|
|
$subject = $this->EmailSubject; |
|
|
|
|
|
|
513
|
|
|
$email_content = $this->Content; |
|
|
|
|
|
|
514
|
|
|
|
|
515
|
|
|
/* Parse HTML like a template, and translate any internal links */ |
|
516
|
|
|
$data = ArrayData::create(array( |
|
517
|
|
|
'Content' => $email_content |
|
518
|
|
|
)); |
|
519
|
|
|
|
|
520
|
|
|
// $email_body = $record->renderWith(SSViewer::fromString($reminder->Content)); |
|
|
|
|
|
|
521
|
|
|
// echo $record->renderWith('Email_Reminder_Standard_Template');//$email_body; |
|
|
|
|
|
|
522
|
|
|
$email = new Email( |
|
523
|
|
|
$this->EmailFrom, |
|
|
|
|
|
|
524
|
|
|
$email, |
|
525
|
|
|
$subject |
|
526
|
|
|
); |
|
527
|
|
|
|
|
528
|
|
|
$email->setTemplate('Email_Reminder_Standard_Template'); |
|
529
|
|
|
|
|
530
|
|
|
$email->populateTemplate($data); |
|
531
|
|
|
|
|
532
|
|
|
$log->IsTestOnly = $isTestOnly; |
|
|
|
|
|
|
533
|
|
|
$log->Result = $email->send(); |
|
|
|
|
|
|
534
|
|
|
$log->EmailReminder_NotificationScheduleID = $this->ID; |
|
|
|
|
|
|
535
|
|
|
$log->write(); |
|
536
|
|
|
} |
|
537
|
|
|
} |
|
538
|
|
|
return false; |
|
539
|
|
|
} |
|
540
|
|
|
} |
|
541
|
|
|
|
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.