1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
*@author nicolaas [at] sunnysideup.co.nz |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
**/ |
8
|
|
|
|
9
|
|
|
class CampaignMonitorCampaign extends DataObject |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private static $emogrifier_add_allowed_media_types = array( |
|
|
|
|
17
|
|
|
"screen" |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $emogrifier_remove_allowed_media_types = array(); |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private static $default_template = "CampaignMonitorCampaign"; |
|
|
|
|
31
|
|
|
|
32
|
|
|
private static $db = array( |
|
|
|
|
33
|
|
|
"HasBeenSent" => "Boolean", |
34
|
|
|
"MessageFromNewsletterServer" => "Text", |
35
|
|
|
"CreateAsTemplate" => "Boolean", |
36
|
|
|
"CreateFromWebsite" => "Boolean", |
37
|
|
|
"CreatedFromWebsite" => "Boolean", |
38
|
|
|
"TemplateID" => "Varchar(40)", |
39
|
|
|
"CampaignID" => "Varchar(40)", |
40
|
|
|
"Name" => "Varchar(100)", |
41
|
|
|
"Subject" => "Varchar(100)", |
42
|
|
|
"FromName" => "Varchar(100)", |
43
|
|
|
"FromEmail" => "Varchar(100)", |
44
|
|
|
"ReplyTo" => "Varchar(100)", |
45
|
|
|
"SentDate" => "SS_Datetime", |
46
|
|
|
"WebVersionURL" => "Varchar(255)", |
47
|
|
|
"WebVersionTextURL" => "Varchar(255)", |
48
|
|
|
"Hide" => "Boolean", |
49
|
|
|
"Content" => "HTMLText", |
50
|
|
|
"Hash" => "Varchar(32)" |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
private static $indexes = array( |
|
|
|
|
54
|
|
|
"TemplateID" => true, |
55
|
|
|
"CampaignID" => true, |
56
|
|
|
"Hide" => true, |
57
|
|
|
"Hash" => true |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
private static $field_labels = array( |
|
|
|
|
61
|
|
|
"CreateFromWebsite" => "Create on newsletter server" |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
private static $has_one = array( |
|
|
|
|
65
|
|
|
"CampaignMonitorCampaignStyle" => "CampaignMonitorCampaignStyle" |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
private static $many_many = array( |
|
|
|
|
69
|
|
|
"Pages" => "CampaignMonitorSignupPage" |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
private static $searchable_fields = array( |
|
|
|
|
73
|
|
|
"Subject" => "PartialMatchFilter", |
74
|
|
|
"Content" => "PartialMatchFilter", |
75
|
|
|
"Hide" => "ExactMatch" |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
private static $summary_fields = array( |
|
|
|
|
79
|
|
|
"Subject" => "Subject", |
80
|
|
|
"SentDate" => "Sent Date" |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
private static $singular_name = "Campaign"; |
|
|
|
|
84
|
|
|
|
85
|
|
|
private static $plural_name = "Campaigns"; |
|
|
|
|
86
|
|
|
|
87
|
|
|
private static $default_sort = "Hide ASC, SentDate DESC"; |
|
|
|
|
88
|
|
|
|
89
|
|
|
public function canDelete($member = null) |
90
|
|
|
{ |
91
|
|
|
return $this->HasBeenSentCheck() ? false : parent::canDelete($member); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getCMSFields() |
95
|
|
|
{ |
96
|
|
|
$fields = parent::getCMSFields(); |
97
|
|
|
//readonly |
98
|
|
|
$fields->makeFieldReadonly("MessageFromNewsletterServer"); |
99
|
|
|
$fields->makeFieldReadonly("CampaignID"); |
100
|
|
|
$fields->makeFieldReadonly("TemplateID"); |
101
|
|
|
$fields->makeFieldReadonly("WebVersionURL"); |
102
|
|
|
$fields->makeFieldReadonly("WebVersionTextURL"); |
103
|
|
|
$fields->makeFieldReadonly("SentDate"); |
104
|
|
|
$fields->makeFieldReadonly("HasBeenSent"); |
105
|
|
|
$fields->makeFieldReadonly("Hash"); |
106
|
|
|
//removed |
107
|
|
|
$fields->removeFieldFromTab("Root.Main", "CreatedFromWebsite"); |
108
|
|
|
$fields->removeFieldFromTab("Root.Main", "CreatedTemplateFromWebsite"); |
109
|
|
|
$fields->removeFieldFromTab("Root.Main", "SecurityCode"); |
110
|
|
|
//pages |
111
|
|
|
$pages = CampaignMonitorSignupPage::get()->map("ID", "Title")->toArray(); |
112
|
|
|
$fields->removeFieldFromTab("Root.Main", "Pages"); |
113
|
|
|
$fields->replaceField("CreateAsTemplate", new OptionsetField("CreateAsTemplate", "Type", array(0 => "Create as Campaign", 1 => "Create as Template"))); |
114
|
|
|
if (count($pages)) { |
115
|
|
|
$fields->addFieldToTab("Root.Pages", new CheckboxSetField("Pages", "Shown on the following pages ...", $pages)); |
116
|
|
|
} |
117
|
|
|
if ($this->ExistsOnCampaignMonitorCheck()) { |
118
|
|
|
$fields->removeFieldFromTab("Root.Main", "CreateAsTemplate"); |
119
|
|
|
if (!$this->CreateAsTemplate) { |
|
|
|
|
120
|
|
|
$fields->removeFieldFromTab("Root.Main", "CreateFromWebsite"); |
121
|
|
|
if (!$this->HasBeenSentCheck()) { |
122
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("CreateFromWebsiteRemake", "<h2>To edit this newsletter, please first delete it from your newsletter server</h2>"), "CampaignID"); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
$fields->removeFieldFromTab("Root.Main", "Hash"); |
126
|
|
|
$fields->removeFieldFromTab("Root.Main", "CampaignMonitorCampaignStyleID"); |
127
|
|
|
|
128
|
|
|
$fields->makeFieldReadonly("Name"); |
129
|
|
|
$fields->makeFieldReadonly("Subject"); |
130
|
|
|
$fields->makeFieldReadonly("FromName"); |
131
|
|
|
$fields->makeFieldReadonly("FromEmail"); |
132
|
|
|
$fields->makeFieldReadonly("ReplyTo"); |
133
|
|
|
$fields->makeFieldReadonly("SentDate"); |
134
|
|
|
$fields->makeFieldReadonly("WebVersionURL"); |
135
|
|
|
$fields->makeFieldReadonly("WebVersionTextURL"); |
136
|
|
|
$fields->makeFieldReadonly("Content"); |
137
|
|
|
} else { |
138
|
|
|
$this->CampaignID = null; |
|
|
|
|
139
|
|
|
$this->TemplateID = null; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
if ($this->HasBeenSentCheck()) { |
142
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("Link", "<h2><a target=\"_blank\" href=\"".$this->Link()."\">Link</a></h2>"), "CampaignID"); |
143
|
|
|
} else { |
144
|
|
|
$fields->removeFieldFromTab("Root.Main", "Hide"); |
145
|
|
|
if ($this->exists()) { |
146
|
|
|
if ($this->ExistsOnCampaignMonitorCheck()) { |
147
|
|
|
$fields->removeFieldFromTab("Root.Main", "CreateFromWebsite"); |
148
|
|
|
} else { |
149
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("PreviewLink", "<h2><a target=\"_blank\" href=\"".$this->PreviewLink()."\">Preview Link</a></h2>"), "CampaignID"); |
150
|
|
|
} |
151
|
|
|
} else { |
152
|
|
|
$fields->removeFieldFromTab("Root.Main", "CreateFromWebsite"); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
return $fields; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* returns link to view campaign |
160
|
|
|
* @var return |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function Link($action = "") |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
if ($page = $this->Pages()->First()) { |
|
|
|
|
165
|
|
|
$link = $page->Link("viewcampaign".$action."/".$this->ID."/"); |
166
|
|
|
return Director::absoluteURL($link); |
167
|
|
|
} |
168
|
|
|
return "#"; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* returns link to view preview campaign |
173
|
|
|
* this link is used to create templates / campaigns on Campaign Monitor |
174
|
|
|
* @var return |
175
|
|
|
*/ |
176
|
|
View Code Duplication |
public function PreviewLink($action = "") |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
if ($page = $this->Pages()->First()) { |
|
|
|
|
179
|
|
|
$link = $page->Link("previewcampaign".$action."/".$this->ID."/?hash=".$this->Hash); |
|
|
|
|
180
|
|
|
return Director::absoluteURL($link); |
181
|
|
|
} |
182
|
|
|
return ""; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* html for newsletter to be created |
187
|
|
|
* @var return |
188
|
|
|
*/ |
189
|
|
|
public function getNewsletterContent() |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$extension = $this->extend("updateNewsletterContent", $content); |
192
|
|
|
if (is_array($extension) && count($extension)) { |
193
|
|
|
return $extension[0]; |
194
|
|
|
} |
195
|
|
|
$html = ""; |
196
|
|
|
if (class_exists('\Pelago\Emogrifier')) { |
197
|
|
|
$allCSS = ""; |
198
|
|
|
$cssFileLocations = $this->getCSSFileLocations(); |
199
|
|
|
foreach ($cssFileLocations as $cssFileLocation) { |
200
|
|
|
$cssFileHandler = fopen($cssFileLocation, 'r'); |
201
|
|
|
$allCSS .= fread($cssFileHandler, filesize($cssFileLocation)); |
202
|
|
|
fclose($cssFileHandler); |
203
|
|
|
} |
204
|
|
|
$isThemeEnabled = Config::inst()->get('SSViewer', 'theme_enabled'); |
205
|
|
|
if (!$isThemeEnabled) { |
206
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', true); |
207
|
|
|
} |
208
|
|
|
Requirements::clear(); |
209
|
|
|
$templateName = $this->getRenderWithTemplate(); |
210
|
|
|
$html = $this->renderWith($templateName); |
211
|
|
|
if (!$isThemeEnabled) { |
212
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', false); |
213
|
|
|
} |
214
|
|
|
$emogrifier = new \Pelago\Emogrifier($html, $allCSS); |
215
|
|
|
$addMediaTypes = $this->Config()->get("emogrifier_add_allowed_media_types"); |
216
|
|
|
foreach ($addMediaTypes as $type) { |
|
|
|
|
217
|
|
|
//$emogrifier->addAllowedMediaType($type); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
$removeMediaTypes = $this->Config()->get("emogrifier_remove_allowed_media_types"); |
220
|
|
|
foreach ($removeMediaTypes as $type) { |
221
|
|
|
$emogrifier->removeAllowedMediaType($type); |
222
|
|
|
} |
223
|
|
|
$html = $emogrifier->emogrify(); |
224
|
|
|
} else { |
225
|
|
|
user_error("Please include Emogrifier module"); |
226
|
|
|
} |
227
|
|
|
return $html; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* provide template used for RenderWith |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public function getRenderWithTemplate() |
235
|
|
|
{ |
236
|
|
|
if ($style = $this->CampaignMonitorCampaignStyle()) { |
|
|
|
|
237
|
|
|
if ($style->exists() && $style->TemplateName) { |
238
|
|
|
return $style->TemplateName; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
return $this->Config()->get("default_template"); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
protected function getCSSFileLocations() |
248
|
|
|
{ |
249
|
|
|
if ($style = $this->CampaignMonitorCampaignStyle()) { |
|
|
|
|
250
|
|
|
return $style->getCSSFilesAsArray(); |
251
|
|
|
} |
252
|
|
|
return array(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
public function getHTMLContent() |
259
|
|
|
{ |
260
|
|
|
if ($style = $this->CampaignMonitorCampaignStyle()) { |
|
|
|
|
261
|
|
|
return $style->getHTMLContent($this); |
262
|
|
|
} |
263
|
|
|
return $this->renderWith("CampaignMonitorCampaign"); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
protected $countOfWrites = 0; |
267
|
|
|
|
268
|
|
|
public function onBeforeWrite() |
269
|
|
|
{ |
270
|
|
|
parent::onBeforeWrite(); |
271
|
|
|
if (!$this->Hash) { |
|
|
|
|
272
|
|
|
$this->Hash = substr(hash("md5", uniqid()), 0, 7); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
if (!$this->ExistsOnCampaignMonitorCheck($forceRecheck = true)) { |
275
|
|
|
if ($this->CreateAsTemplate) { |
|
|
|
|
276
|
|
|
$this->TemplateID = null; |
|
|
|
|
277
|
|
|
} else { |
278
|
|
|
$this->CampaignID = null; |
|
|
|
|
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function onAfterWrite() |
284
|
|
|
{ |
285
|
|
|
parent::onAfterWrite(); |
286
|
|
|
if ($this->Pages()->count() == 0) { |
|
|
|
|
287
|
|
|
if ($page = CampaignMonitorSignupPage::get()->first()) { |
288
|
|
|
$this->Pages()->add($page); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
$this->countOfWrites++; |
292
|
|
|
if (!$this->ExistsOnCampaignMonitorCheck($forceRecheck = true) && $this->CreateFromWebsite && $this->countOfWrites < 3) { |
|
|
|
|
293
|
|
|
$api = $this->getAPI(); |
294
|
|
|
if ($this->CreateAsTemplate) { |
|
|
|
|
295
|
|
|
if ($this->TemplateID) { |
|
|
|
|
296
|
|
|
$api->updateTemplate($this, $this->TemplateID); |
|
|
|
|
297
|
|
|
} else { |
298
|
|
|
$api->createTemplate($this); |
299
|
|
|
} |
300
|
|
|
} else { |
301
|
|
|
$api->createCampaign($this); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function onBeforeDelete() |
307
|
|
|
{ |
308
|
|
|
parent::onBeforeDelete(); |
309
|
|
|
if ($this->HasBeenSentCheck()) { |
|
|
|
|
310
|
|
|
//do nothing |
311
|
|
|
} else { |
312
|
|
|
if ($this->ExistsOnCampaignMonitorCheck($forceRecheck = true)) { |
313
|
|
|
$api = $this->getAPI(); |
314
|
|
|
if ($this->CreateAsTemplate) { |
|
|
|
|
315
|
|
|
$api->deleteTemplate($this->TemplateID); |
|
|
|
|
316
|
|
|
} else { |
317
|
|
|
$api->deleteCampaign($this->CampaignID); |
|
|
|
|
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
private static $_api = null; |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* |
327
|
|
|
* @return CampaignMonitorAPIConnector |
328
|
|
|
*/ |
329
|
|
|
protected function getAPI() |
330
|
|
|
{ |
331
|
|
|
if (!self::$_api) { |
332
|
|
|
self::$_api = CampaignMonitorAPIConnector::create(); |
333
|
|
|
self::$_api->init(); |
334
|
|
|
} |
335
|
|
|
return self::$_api; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
private $_hasBeenSent = null; |
339
|
|
|
|
340
|
|
|
public function HasBeenSentCheck() |
|
|
|
|
341
|
|
|
{ |
342
|
|
|
//lazy check |
343
|
|
|
if ($this->HasBeenSent || $this->WebVersionURL) { |
|
|
|
|
344
|
|
|
return true; |
345
|
|
|
} |
346
|
|
|
//real check |
347
|
|
|
if ($this->_hasBeenSent === null) { |
348
|
|
|
if (!$this->CampaignID) { |
|
|
|
|
349
|
|
|
$this->_hasBeenSent = false; |
350
|
|
|
} elseif (!$this->HasBeenSent) { |
|
|
|
|
351
|
|
|
$api = $this->getAPI(); |
|
|
|
|
352
|
|
|
$result = $this->api->getCampaigns(); |
|
|
|
|
353
|
|
|
if (isset($result)) { |
354
|
|
|
foreach ($result as $key => $campaign) { |
355
|
|
|
if ($this->CampaignID == $campaign->CampaignID) { |
|
|
|
|
356
|
|
|
$this->HasBeenSent = true; |
|
|
|
|
357
|
|
|
$this->HasBeenSent->write(); |
|
|
|
|
358
|
|
|
$this->_hasBeenSent = true; |
359
|
|
|
break; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
} else { |
364
|
|
|
$this->_hasBeenSent = $this->HasBeenSent; |
|
|
|
|
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
return $this->_hasBeenSent; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
private $_existsOnCampaignMonitorCheck = null; |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* checks if the template and/or the campaign exists |
374
|
|
|
* @return boolean |
375
|
|
|
*/ |
376
|
|
|
public function ExistsOnCampaignMonitorCheck($forceRecheck = false) |
377
|
|
|
{ |
378
|
|
|
//lazy check |
379
|
|
|
if ($this->HasBeenSent) { |
|
|
|
|
380
|
|
|
return true; |
381
|
|
|
} |
382
|
|
|
//real check |
383
|
|
|
if ($this->_existsOnCampaignMonitorCheck === null || $forceRecheck) { |
384
|
|
|
$this->_existsOnCampaignMonitorCheck = false; |
385
|
|
|
if ($this->CreateAsTemplate) { |
|
|
|
|
386
|
|
|
$field = "TemplateID"; |
387
|
|
|
$apiMethod1 = "getTemplates"; |
388
|
|
|
$apiMethod2 = ""; |
389
|
|
|
} else { |
390
|
|
|
$field = "CampaignID"; |
391
|
|
|
$apiMethod1 = "getDrafts"; |
392
|
|
|
$apiMethod2 = "getCampaigns"; |
393
|
|
|
} |
394
|
|
|
if (!$this->$field) { |
|
|
|
|
395
|
|
|
//do nothing |
396
|
|
|
} else { |
397
|
|
|
$api = $this->getAPI(); |
|
|
|
|
398
|
|
|
//check drafts |
399
|
|
|
$result = $this->api->$apiMethod1(); |
|
|
|
|
400
|
|
|
if (isset($result)) { |
401
|
|
View Code Duplication |
foreach ($result as $key => $campaign) { |
|
|
|
|
402
|
|
|
if ($this->$field == $campaign->$field) { |
403
|
|
|
$this->_existsOnCampaignMonitorCheck = true; |
404
|
|
|
break; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
} elseif ($apiMethod2) { |
408
|
|
|
//check sent ones |
409
|
|
|
$result = $this->api->$apiMethod2(); |
|
|
|
|
410
|
|
|
if (isset($result)) { |
411
|
|
View Code Duplication |
foreach ($result as $key => $campaign) { |
|
|
|
|
412
|
|
|
if ($this->$field == $campaign->$field) { |
413
|
|
|
$this->_existsOnCampaignMonitorCheck = true; |
414
|
|
|
break; |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
return $this->_existsOnCampaignMonitorCheck; |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
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.