1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class SmartChimpSignupPage extends Page |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public static $icon = "smartchimp/images/treeicons/SmartChimpSignupPage"; |
6
|
|
|
|
7
|
|
|
public static $db = array( |
8
|
|
|
// @todo: provide optional dropdown for entering username/password?? |
9
|
|
|
'MCApiKey' => 'Varchar(50)', // api_key |
10
|
|
|
'MCListKey' => 'Varchar(50)', // list_unique_id |
11
|
|
|
'MCSuccessContent' => 'HTMLText', |
12
|
|
|
"DoubleOptin" => "Boolean", |
13
|
|
|
"SendWelcomeMail" => "Boolean", |
14
|
|
|
"SendGoodbey" => "Boolean", |
15
|
|
|
"SendDeleteNotification" => "Boolean", |
16
|
|
|
"IsDefaultList" => "Boolean", |
17
|
|
|
"FirstFieldRequired" => "Boolean", |
18
|
|
|
"LastFieldRequired" => "Boolean" |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
public static $has_many = array( |
22
|
|
|
"SmartChimpNewsletters" => "SmartChimpNewsletter" |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
public static $defaults = array( |
26
|
|
|
"DoubleOptin" => 1, |
27
|
|
|
"SendWelcomeMail" => 0, |
28
|
|
|
"SendGoodbey" => 0, |
29
|
|
|
"IsDefaultList" => 1 |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
public static $mc_api_version = '1.2.1'; |
33
|
|
|
|
34
|
|
|
protected static $api = null; |
35
|
|
|
|
36
|
|
|
public function getAPI() |
37
|
|
|
{ |
38
|
|
|
if ($this->MCApiKey && $this->MCListKey) { |
39
|
|
|
if (!(self::$api instanceof MCAPI)) { |
|
|
|
|
40
|
|
|
require_once(Director::baseFolder().'/smartchimp/thirdparty/mcapi/'.self::$mc_api_version.'/MCAPI.class.php'); |
41
|
|
|
self::$api = new MCAPI("$this->MCApiKey"); |
42
|
|
|
} |
43
|
|
|
return self::$api; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
public function getCMSFields() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$fields = parent::getCMSFields(); |
51
|
|
|
|
52
|
|
|
$fields->addFieldsToTab('Root.Content.SentNewsletters', array( |
53
|
|
|
new LiteralField('HowToRetrieve', '<p>To retrieve sent newsletters, simply save this page or <a href="'.$this->Link("update").'?flush=1">click here</a>.</p>'), |
54
|
|
|
$this->SmartChimpNewslettersTable() |
55
|
|
|
)); |
56
|
|
|
$fields->addFieldsToTab('Root.Content.MailChimpConfig', array( |
57
|
|
|
new CheckboxField('IsDefaultList', 'This is the default newsletter'), |
|
|
|
|
58
|
|
|
new TextField('MCApiKey', _t('SmartChimp.MCAPIKEY', 'API Key')), |
59
|
|
|
new TextField('MCListKey', _t('SmartChimp.MCLISTKEY', 'Unique ID for List')), |
60
|
|
|
new HTMLEditorField('MCSuccessContent', _t('SmartChimp.MCSuccessContent', 'Signup Success Content')) |
61
|
|
|
)); |
62
|
|
|
$fields->addFieldsToTab('Root.Content.Subscribe', array( |
63
|
|
|
new CheckboxField('DoubleOptin', "Double Opt-In Process (send email to confirm registration)"), |
|
|
|
|
64
|
|
|
new CheckboxField('SendWelcomeMail', "Send Welcome Mail") |
|
|
|
|
65
|
|
|
)); |
66
|
|
|
$fields->addFieldsToTab('Root.Content.Unsubscribe', array( |
67
|
|
|
new CheckboxField('SendGoodbey', "Send Goodbey Email"), |
|
|
|
|
68
|
|
|
new CheckboxField('SendDeleteNotification', "Send notification of unsubscribe") |
|
|
|
|
69
|
|
|
)); |
70
|
|
|
$fields->addFieldsToTab("Root.Content.RequiredFields", array( |
71
|
|
|
new CheckboxField('FirstRequired', "First name is required"), |
|
|
|
|
72
|
|
|
new CheckboxField('LastRequired', "Last name is required") |
|
|
|
|
73
|
|
|
)); |
74
|
|
|
$this->extend('updateSmartChimpCMSFields'); |
75
|
|
|
|
76
|
|
|
return $fields; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function SmartChimpNewslettersTable() |
80
|
|
|
{ |
81
|
|
|
$table = new HasManyComplexTableField( |
82
|
|
|
$controller = $this, |
83
|
|
|
$name = "SmartChimpNewsletters", |
84
|
|
|
$sourceClass = "SmartChimpNewsletter", |
85
|
|
|
$fieldList = null, |
86
|
|
|
$detailFormFields = null, |
87
|
|
|
$sourceFilter = "ParentID = ".$this->ID |
88
|
|
|
); |
89
|
|
|
$table->setPageSize(100); |
90
|
|
|
$table->setPermissions(array('export', 'show', 'edit', 'delete')); |
91
|
|
|
return $table; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function SmartChimpNewslettersShow() |
95
|
|
|
{ |
96
|
|
|
return DataObject::get("SmartChimpNewsletter", "\"ParentID\" = ".$this->ID." AND \"Hide\" <> 1"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function RetrieveCampaigns() |
100
|
|
|
{ |
101
|
|
|
$dos = new DataObjectSet(); |
|
|
|
|
102
|
|
|
SmartChimpNewsletter::clean_up_characters(); |
103
|
|
|
$api = $this->getAPI(); |
104
|
|
|
if ($api && $this->ID) { |
105
|
|
|
$campaignArray = $api->campaigns(array("list_id" => $this->MCListKey)); |
106
|
|
|
if (is_array($campaignArray) && count($campaignArray)) { |
107
|
|
|
foreach ($campaignArray as $key => $campaign) { |
108
|
|
|
if ($campaign["status"] == "sent") { |
109
|
|
|
$obj = DataObject::get_one("SmartChimpNewsletter", "`ParentID` = ".$this->ID." AND `CampaignID` = '".$campaign["id"]."'"); |
110
|
|
|
if ($obj) { |
|
|
|
|
111
|
|
|
//do nothing |
112
|
|
|
} else { |
113
|
|
|
$content = $api->campaignContent($campaign["id"]); |
114
|
|
|
if ($content) { |
115
|
|
|
$obj = new SmartChimpNewsletter(); |
116
|
|
|
$obj->ParentID = $this->ID; |
|
|
|
|
117
|
|
|
$obj->Date = $campaign["send_time"];// |
|
|
|
|
118
|
|
|
$obj->Title = $campaign["title"];// |
|
|
|
|
119
|
|
|
$obj->Subject = $campaign["subject"];// |
|
|
|
|
120
|
|
|
$obj->PermaLink = $campaign["archive_url"];// |
|
|
|
|
121
|
|
|
$obj->CampaignID = $campaign["id"];// |
|
|
|
|
122
|
|
|
$obj->WebID = $campaign["web_id"];// |
|
|
|
|
123
|
|
|
$obj->Status = $campaign["status"];//should be sent! |
|
|
|
|
124
|
|
|
//$obj->TextContent = $content["text"] ;//html |
|
|
|
|
125
|
|
|
//$obj->HTMLContent = $content["html"];//text |
|
|
|
|
126
|
|
|
} else { |
127
|
|
|
user_error(" could not retrieve content for newsletter with subject: ".$campaign["subject"]." AND ID".$campaign["id"], E_USER_NOTICE); |
128
|
|
|
} |
129
|
|
|
$obj->write(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
public function subscribe($email, $firstname, $lastname) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$api = $this->getAPI(); |
141
|
|
|
if ($api) { |
142
|
|
|
$mergeVars = array( |
143
|
|
|
'FNAME' => $firstname, |
144
|
|
|
'LNAME' => $lastname |
145
|
|
|
); |
146
|
|
|
//NOTE: update existing is set to false to not accidentally resubscribe someone. |
147
|
|
|
if (true === $api->listSubscribe($id = $this->MCListKey, $email, $mergeVars, $email_type='html', $this->DoubleOptin, $update_existing=false, $replace_interests=true, $this->SendWelcomeMail)) { |
148
|
|
|
return true; |
149
|
|
|
} else { |
150
|
|
|
return $api->errorMessage; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function unsubscribe($email) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$api = $this->getAPI(); |
158
|
|
|
if ($api) { |
159
|
|
|
if (true === $api->listUnsubscribe($id = $this->MCListKey, $email, $delete_member=false, $this->SendGoodbey, $this->SendDeleteNotification)) { |
160
|
|
|
return true; |
161
|
|
|
} else { |
162
|
|
|
return $api->errorMessage; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function onBeforeWrite() |
168
|
|
|
{ |
169
|
|
|
parent::onBeforeWrite(); |
170
|
|
|
if ($this->IsDefaultList && $this->ID) { |
171
|
|
|
$others = DataObject::get("SmartChimpSignupPage", "`SmartChimpSignupPage`.`ID` <> ".intval($this->ID)." AND `SmartChimpSignupPage`.`IsDefaultList` = 1"); |
172
|
|
|
if ($others) { |
173
|
|
|
foreach ($others as $other) { |
174
|
|
|
$other->IsDefaultList = 0; |
175
|
|
|
$other->writeToStage('Stage'); |
176
|
|
|
$other->publish('Stage', 'Live'); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function onAfterWrite() |
183
|
|
|
{ |
184
|
|
|
parent::onAfterWrite(); |
185
|
|
|
$this->RetrieveCampaigns(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
|
190
|
|
|
public function requireDefaultRecords() |
191
|
|
|
{ |
192
|
|
|
parent::requireDefaultRecords(); |
193
|
|
|
$pages = DataObject::get("SmartChimpSignupPage"); |
194
|
|
|
if ($pages) { |
195
|
|
|
if ($pages->count() == 1) { |
196
|
|
|
foreach ($pages as $page) { |
197
|
|
|
if (!$page->IsDefaultList) { |
198
|
|
|
$page->IsDefaultList = 1; |
199
|
|
|
$page->writeToStage('Stage'); |
200
|
|
|
$page->publish('Stage', 'Live'); |
201
|
|
|
Database::alteration_message($page->ClassName.' created/updated: added IsDefaultList = true setting as there is only one SmartChimpSignupPage', 'edited'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
class SmartChimpSignupPage_Controller extends Page_Controller |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
public static $allowed_actions = array("Form", "update"); |
213
|
|
|
|
214
|
|
|
public function init() |
215
|
|
|
{ |
216
|
|
|
parent::init(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
public function update() |
221
|
|
|
{ |
222
|
|
|
$this->RetrieveCampaigns(); |
223
|
|
|
return array(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function Form() |
227
|
|
|
{ |
228
|
|
|
if (Session::get('SmartChimp.SUCCESS')) { |
229
|
|
|
Session::clear('SmartChimp.SUCCESS'); |
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
$requiredFields = new RequiredFields('email'); |
233
|
|
|
if ($this->FirstFieldRequired) { |
234
|
|
|
$requiredFields->appendRequiredFields(array('fname')); |
235
|
|
|
} |
236
|
|
|
if ($this->LastFieldRequired) { |
237
|
|
|
$requiredFields->appendRequiredFields(array('lname')); |
238
|
|
|
} |
239
|
|
|
$form = new Form($this, 'Form', |
240
|
|
|
new FieldSet( |
241
|
|
|
new TextField('fname', 'First name'), |
242
|
|
|
new TextField('lname', 'Last name'), |
243
|
|
|
new TextField('email', 'Email address') |
244
|
|
|
), |
245
|
|
|
new FieldSet( |
246
|
|
|
new FormAction('SignupAction', 'Sign up') |
|
|
|
|
247
|
|
|
), |
248
|
|
|
$requiredFields |
249
|
|
|
); |
250
|
|
|
$this->extend('updateSmartChimpForm', $form); |
251
|
|
|
return $form; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function ShortForm() |
255
|
|
|
{ |
256
|
|
|
if (Session::get('SmartChimp.SUCCESS')) { |
257
|
|
|
Session::clear('SmartChimp.SUCCESS'); |
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
$form = new Form($this, 'Form', |
261
|
|
|
new FieldSet( |
262
|
|
|
new TextField('email', 'Email Address') |
263
|
|
|
), |
264
|
|
|
new FieldSet( |
265
|
|
|
new FormAction('SignupAction', 'Sign up') |
|
|
|
|
266
|
|
|
), |
267
|
|
|
new RequiredFields('email') |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$this->extend('updateSmartChimpForm', $form); |
271
|
|
|
|
272
|
|
|
return $form; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function mcsuccess() |
276
|
|
|
{ |
277
|
|
|
if (Session::get('SmartChimp.SUCCESS')) { |
278
|
|
|
$this->Content = $this->MCSuccessContent; |
279
|
|
|
} |
280
|
|
|
return array(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function SignupAction($raw_data, $form) |
284
|
|
|
{ |
285
|
|
|
$data = Convert::raw2sql($raw_data); |
286
|
|
|
$outcome = $this->subscribe($data['email'], $data['fname'], $data['lname']); |
287
|
|
|
if (true === $outcome) { |
288
|
|
|
Session::set('SmartChimp.SUCCESS', true); |
|
|
|
|
289
|
|
|
return $this->mcsuccess(); |
290
|
|
|
} else { |
291
|
|
|
$form->sessionMessage($outcome, 'warning'); |
292
|
|
|
Director::redirectBack(); |
|
|
|
|
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
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.