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