1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use \DrewM\MailChimp\MailChimp; |
4
|
|
|
|
5
|
|
|
class ChimpifyCampaign extends DataObject |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
private static $api_key; |
|
|
|
|
8
|
|
|
|
9
|
|
|
private static $db = [ |
|
|
|
|
10
|
|
|
'Title' => 'Varchar', |
11
|
|
|
'FromName' => 'Varchar', |
12
|
|
|
'ReplyTo' => 'Varchar', |
13
|
|
|
'TemplateID' => 'Int', |
14
|
|
|
'Intro' => 'Text', |
15
|
|
|
'ItemLimit' => 'Int', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
private static $many_many = [ |
|
|
|
|
19
|
|
|
'ContentSources' => 'Blog', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
private static $defaults = [ |
|
|
|
|
23
|
|
|
'ItemLimit' => 3, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
private static $singular_name = 'MailChimp Campaign'; |
|
|
|
|
27
|
|
|
|
28
|
|
|
private static $plural_name = 'MailChimp Campaigns'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
public function getCMSFields() |
31
|
|
|
{ |
32
|
|
|
if (!$api_key = $this->config()->get('api_key')) { |
33
|
|
|
user_error( |
34
|
|
|
'Add a MailChimp API key to config (ChimpifyCampaign::api_key)', |
35
|
|
|
E_USER_ERROR |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$fields = parent::getCMSFields(); |
40
|
|
|
|
41
|
|
|
$fields->removeByName('ItemLimit'); |
42
|
|
|
$fields->removeByName('ContentSources'); |
43
|
|
|
|
44
|
|
|
$mailChimp = new MailChimp($api_key); |
45
|
|
|
|
46
|
|
|
$fields->addFieldsToTab( |
47
|
|
|
'Root.Main', |
48
|
|
|
[ |
49
|
|
|
TextField::create( |
50
|
|
|
'Title', |
51
|
|
|
_t('Chimpify.FieldLabelSubjectLine', 'Subject line') |
52
|
|
|
), |
53
|
|
|
TextField::create( |
54
|
|
|
'FromName', |
55
|
|
|
_t('Chimpify.FieldLabelFromName', 'From name') |
56
|
|
|
), |
57
|
|
|
EmailField::create( |
58
|
|
|
'ReplyTo', |
59
|
|
|
_t('Chimpify.FieldLabelReplyTo', 'Reply to email address') |
60
|
|
|
), |
61
|
|
|
DropdownField::create( |
62
|
|
|
'TemplateID', |
63
|
|
|
_t('Chimpify.FieldLabelMailChimpTemplate', 'MailChimp template'), |
64
|
|
|
$this->getMailChimpTemplates($mailChimp)->map('id', 'name')) |
65
|
|
|
->setEmptyString( |
66
|
|
|
_t('Chimpify.FieldPlaceholderMailChimpTemplate', 'Select...') |
67
|
|
|
), |
68
|
|
|
TextareaField::create( |
69
|
|
|
'Intro', |
70
|
|
|
_t('Chimpify.FieldLabelIntro', 'Introduction')) |
71
|
|
|
->setDescription(_t( |
72
|
|
|
'Chimpify.FieldDescriptionIntro', |
73
|
|
|
'Dispayled above the list of Blog posts.' |
74
|
|
|
)), |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
if ($this->ID) { |
79
|
|
|
$sourcesConfig = GridFieldConfig_RelationEditor::create(); |
80
|
|
|
$sourcesConfig->removeComponentsByType('GridFieldEditButton'); |
81
|
|
|
$sourcesConfig->removeComponentsByType('GridFieldAddNewButton'); |
82
|
|
|
|
83
|
|
|
$fields->addFieldsToTab( |
84
|
|
|
'Root.Main', |
85
|
|
|
[ |
86
|
|
|
GridField::create( |
87
|
|
|
'ContentSources', |
88
|
|
|
_t('Chimpify.FieldLabelContentSources', 'Content sources'), |
89
|
|
|
$this->ContentSources(), |
|
|
|
|
90
|
|
|
$sourcesConfig |
91
|
|
|
), |
92
|
|
|
NumericField::create( |
93
|
|
|
'ItemLimit', |
94
|
|
|
_t('Chimpify.FieldLabelItemLimit', 'Number of posts')) |
95
|
|
|
->setDescription(_t( |
96
|
|
|
'Chimpify.FieldDescriptionItemLimit', |
97
|
|
|
'The number of posts to display from each content source.' |
98
|
|
|
)), |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->extend('updateCMSFields', $fields); |
104
|
|
|
|
105
|
|
|
return $fields; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getCMSActions() |
109
|
|
|
{ |
110
|
|
|
$actions = parent::getCMSActions(); |
111
|
|
|
|
112
|
|
|
$actions->push( |
113
|
|
|
FormAction::create( |
114
|
|
|
'doGenerateCampaign', |
115
|
|
|
_t('Chimpify.ButtonLabelGenerateCampaign', 'Create in MailChimp') |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$this->extend('updateCMSActions', $actions); |
120
|
|
|
|
121
|
|
|
return $actions; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getCMSValidator() |
125
|
|
|
{ |
126
|
|
|
return new RequiredFields( |
127
|
|
|
'Title', 'FromName', 'ReplyTo', 'TemplateID', 'Intro', 'ItemLimit' |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Fetches a list of email templates from MailChimp. |
133
|
|
|
* |
134
|
|
|
* @param MailChimp $mailChimp |
135
|
|
|
* @return ArrayList |
136
|
|
|
*/ |
137
|
1 |
|
public function getMailChimpTemplates($mailChimp) |
138
|
|
|
{ |
139
|
1 |
|
$templates = ArrayList::create(); |
140
|
1 |
|
$response = $mailChimp->get('templates'); |
141
|
|
|
|
142
|
1 |
View Code Duplication |
if (!$mailChimp->success()) { |
|
|
|
|
143
|
1 |
|
$message = $response && array_key_exists($response['errors']) |
144
|
1 |
|
? $response['errors'][0]['message'] |
145
|
1 |
|
: 'Error connecting to MailChimp API'; |
146
|
|
|
|
147
|
1 |
|
user_error($message, E_USER_ERROR); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
foreach ($response['templates'] as $template) { |
151
|
1 |
|
if ($template['type'] == 'user') { |
152
|
1 |
|
$templates->push(ArrayData::create($template)); |
153
|
1 |
|
} |
154
|
1 |
|
} |
155
|
|
|
|
156
|
1 |
|
$this->extend('updateMailChimpTemplates', $templates); |
157
|
|
|
|
158
|
1 |
|
return $templates; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Generates HTML from ContentSources. |
163
|
|
|
* |
164
|
|
|
* @return String |
165
|
|
|
*/ |
166
|
|
|
public function getCampaignContent() |
167
|
|
|
{ |
168
|
|
|
return $this->renderWith('ChimpifyCampaignContent')->Value; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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.