1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class EmailReminder_DailyMailOut extends BuildTask |
|
|
|
|
5
|
|
|
implements EmailReminder_MailOutInterface |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var int |
11
|
|
|
*/ |
12
|
|
|
private static $limit = 20; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private static $replacer_class = 'EmailReminder_ReplacerClassBase'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
protected $verbose = false; |
21
|
|
|
|
22
|
|
|
protected $testOnly = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The object that replaces tags in the subject and content. |
26
|
|
|
* @var EmailReinder_ReplacerClassInterface |
27
|
|
|
*/ |
28
|
|
|
protected $replacerObject = null; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function setVerbose($b) |
32
|
|
|
{ |
33
|
|
|
$this->verbose = $b; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setTestOnly($b) |
37
|
|
|
{ |
38
|
|
|
$this->testOnly = $b; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* @todo: https://docs.silverstripe.org/en/3.1/developer_guides/extending/injector/ implement |
44
|
|
|
* for email class to be used... |
45
|
|
|
* |
46
|
|
|
* expire date = 08-09 |
47
|
|
|
* days before 7 |
48
|
|
|
* min: current date + 7 - grace days |
49
|
|
|
* min: current date + 7 |
50
|
|
|
* |
51
|
|
|
* expire date = 08-09 |
52
|
|
|
* days after 7 |
53
|
|
|
* min: current date - 7 |
54
|
|
|
* max current date - 7 - grace days |
55
|
|
|
* |
56
|
|
|
* @param SS_Request $request |
57
|
|
|
*/ |
58
|
|
|
public function run($request) |
59
|
|
|
{ |
60
|
|
|
$this->startSending(); |
61
|
|
|
$this->runAll(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function startSending() |
65
|
|
|
{ |
66
|
|
|
//CRUCIAL ! |
67
|
|
|
// |
68
|
|
|
Email::set_mailer(new EmailReminder_Mailer()); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @param EmailReminder_NotificationSchedule $reminder |
75
|
|
|
* @param string|DataObject $recordOrEmail |
76
|
|
|
* @param bool $isTestOnly |
77
|
|
|
*/ |
78
|
|
|
public function runOne($reminder, $recordOrEmail, $isTestOnly = false) |
79
|
|
|
{ |
80
|
|
|
$this->startSending(); |
81
|
|
|
$this->sendEmail($reminder, $recordOrEmail, $isTestOnly); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function runAll() |
85
|
|
|
{ |
86
|
|
|
$reminders = EmailReminder_NotificationSchedule::get(); |
87
|
|
|
foreach ($reminders as $reminder) { |
88
|
|
|
if (! $reminder->hasValidFields()) { |
89
|
|
|
continue; // skip if task is not valid |
90
|
|
|
} |
91
|
|
|
if ($reminder->Disabled) { |
92
|
|
|
continue; // skip if task is disable |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Use StartsWith to match Date and DateTime fields |
96
|
|
|
if ($this->testOnly) { |
97
|
|
|
if ($reminder->SendTestTo) { |
98
|
|
|
$emails = explode(',', $reminder->SendTestTo); |
99
|
|
|
foreach ($emails as $key => $email) { |
100
|
|
|
$this->sendEmail($reminder, $email, $isTestOnly = true); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
$limit = Config::inst()->get('EmailReminder_DailyMailOut', 'daily_limit'); |
105
|
|
|
$records = $reminder->CurrentRecords(); |
106
|
|
|
$records = $records->limit($limit); |
107
|
|
|
if ($records) { |
108
|
|
|
foreach ($records as $record) { |
109
|
|
|
$this->sendEmail($reminder, $record, $isTestOnly = false); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
protected function sendEmail($reminder, $recordOrEmail, $isTestOnly) |
118
|
|
|
{ |
119
|
|
|
$filter = array( |
120
|
|
|
'EmailReminder_NotificationScheduleID' => $reminder->ID, |
121
|
|
|
); |
122
|
|
|
if ($recordOrEmail instanceof DataObject) { |
123
|
|
|
$email_field = $reminder->EmailField; |
124
|
|
|
$email = $recordOrEmail->$email_field; |
125
|
|
|
$record = $recordOrEmail; |
126
|
|
|
$filter['ExternalRecordClassName'] = $recordOrEmail->ClassName; |
127
|
|
|
$filter['ExternalRecordID'] = $recordOrEmail->ID; |
128
|
|
|
} else { |
129
|
|
|
$email = strtolower(trim($recordOrEmail)); |
130
|
|
|
$record = Injector::inst()->get($reminder->DataObject); |
131
|
|
|
} |
132
|
|
|
$filter['EmailTo'] = $email; |
133
|
|
|
if (Email::validEmailAddress($email)) { |
|
|
|
|
134
|
|
|
$send = true; |
|
|
|
|
135
|
|
|
$logs = EmailReminder_EmailRecord::get()->filter($filter); |
136
|
|
|
$send = true; |
137
|
|
|
foreach ($logs as $log) { |
138
|
|
|
if (! $log->canSendAgain()) { |
139
|
|
|
$send = false; |
140
|
|
|
break; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
if ($send) { |
144
|
|
|
$log = EmailReminder_EmailRecord::create($filter); |
145
|
|
|
|
146
|
|
|
$subject = $reminder->EmailSubject; |
147
|
|
|
$email_content = $reminder->Content; |
148
|
|
|
if ($replacerObject = $this->getReplacerObject()) { |
149
|
|
|
$email_content = $replacerObject->replace($reminder, $record, $email_content); |
150
|
|
|
$subject = $replacerObject->replace($reminder, $record, $subject); |
151
|
|
|
} |
152
|
|
|
$email_content = $this->getParsedContent($record, $email_content); |
153
|
|
|
|
154
|
|
|
/* Parse HTML like a template, and translate any internal links */ |
155
|
|
|
$data = ArrayData::create(array( |
156
|
|
|
'Content' => $email_content |
157
|
|
|
)); |
158
|
|
|
|
159
|
|
|
// $email_body = $record->renderWith(SSViewer::fromString($reminder->Content)); |
|
|
|
|
160
|
|
|
// echo $record->renderWith('Email_Reminder_Standard_Template');//$email_body; |
|
|
|
|
161
|
|
|
$email = new Email( |
162
|
|
|
$reminder->EmailFrom, |
163
|
|
|
$email, |
164
|
|
|
$subject |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$email->setTemplate('Email_Reminder_Standard_Template'); |
168
|
|
|
|
169
|
|
|
$email->populateTemplate($data); |
170
|
|
|
|
171
|
|
|
// $email->send(); |
|
|
|
|
172
|
|
|
$log->IsTestOnly = $isTestOnly; |
|
|
|
|
173
|
|
|
$log->Result = $email->send(); |
|
|
|
|
174
|
|
|
$log->EmailReminder_NotificationScheduleID = $reminder->ID; |
|
|
|
|
175
|
|
|
$log->EmailContent = $email->body; |
|
|
|
|
176
|
|
|
$log->write(); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return EmailReminder_ReplacerClassInterface | null |
185
|
|
|
*/ |
186
|
|
|
public function getReplacerObject() |
187
|
|
|
{ |
188
|
|
|
if (! $this->replacerObject) { |
189
|
|
|
$replacerClass = Config::inst()->get("EmailReminder_DailyMailOut", "replacer_class"); |
190
|
|
|
if ($replacerClass && class_exists($replacerClass)) { |
191
|
|
|
$interfaces = class_implements($replacerClass); |
192
|
|
|
if ($interfaces && in_array('EmailReminder_ReplacerClassInterface', $interfaces)) { |
|
|
|
|
193
|
|
|
$this->replacerObject = Injector::inst()->get($replacerClass); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
return $this->replacerObject; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function getParsedContent($record, $content) |
206
|
|
|
{ |
207
|
|
|
return ShortcodeParser::get_active() |
208
|
|
|
->parse( |
209
|
|
|
$record->renderWith( |
210
|
|
|
SSViewer::fromString($content) |
211
|
|
|
) |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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.