This repository has been archived by the owner on Oct 12, 2019. It is now read-only.
/
NewsletterSubscriptionConfig.php
executable file
·207 lines (183 loc) · 7.64 KB
/
NewsletterSubscriptionConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php namespace ProcessWire;
/**
* Class NewsletterSubscriptionConfig
*/
class NewsletterSubscriptionConfig extends ModuleConfig {
/**
* array Default config values
*/
public function getDefaults() {
return array(
'formfields' => 'email',
'namefields' => '',
'unsubscribe' => false,
'noCompare' => false,
'mailfrom' => 'noreply@server.com',
'periodOfValidity' => 5,
'messageSubscribe' => "Hey %name%\n\nPlease click the following link to confirm your subscription: %link%",
'messageUnsubscribe' => "Hey %name%\n\nPlease click the following link to end your subscription: %link%",
'notifyAdmin' => 0,
'notifyAdminMailto' => 'mailto@server.com',
'notifyAdminSubscribeMailfrom' => 'noreply@server.com',
'notifyAdminUnsubscribeMailfrom' => 'noreply@server.com',
'notifyAdminMessageSubscribe' => "New newsletter subscription",
'notifyAdminMessageUnsubscribe' => "New newsletter unsubscription",
);
}
/**
* Retrieves the list of config input fields
* Implementation of the ConfigurableModule interface
*
* @return InputfieldWrapper
*/
public function getInputfields() {
$formfields = $this->data['formfields'];
$unsubscribe = $this->data['unsubscribe'];
$notifyAdmin = $this->data['notifyAdmin'];
$noCompare = $this->data['noCompare'];
$inputfields = parent::getInputfields();
// select fields
$field = $this->modules->get('InputfieldAsmSelect');
$field->description = __('Add all fields which should be attached to the subscription form.') . PHP_EOL .
__('You have to add all needed fields to the user template first¹.');
$field->addOption('', '');
$field->label = __('Select form fields for subscription');
$field->attr('name', 'formfields');
$field->required = true;
$field->columnWidth = 50;
foreach (wire('templates')->get('user')->fields as $f) {
$field->addOption($f->name, $f->name);
}
$inputfields->add($field);
// select name fields
$field = $this->modules->get('InputfieldAsmSelect');
$field->description = __('Add the fields which should be used as username.') . PHP_EOL .
__('Please save the chosen form fields first to get a list to select from.');
$field->label = __('Select name fields');
$field->attr('name', 'namefields');
$field->columnWidth = 50;
if (is_array($formfields)) {
foreach ($formfields as $formfield) {
$f = wire('fields')->get($formfield);
$field->addOption($f->name, $f->name);
}
}
$inputfields->add($field);
// help - how to add fields to the user template
$help = $this->modules->get('InputfieldMarkup');
$helpLink = 'https://processwire.com/talk/topic/1156-custom-user-fields/?p=10161';
$helpContent = '<p><a target="_blank" href="' . $helpLink . '"><sup>1</sup> How to add fields to the user template?</a></p>';
$help->value = $helpContent;
$inputfields->add($help);
// unsubscribe field
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'unsubscribe';
$field->label = 'Unsubscribe option';
$field->description = __('Should the form contain an unsubscribe option?');
$field->value = 1;
$field->attr('checked', $unsubscribe ? 'checked' : '');
$field->columnWidth = 25;
$inputfields->add($field);
// noCompare field
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'noCompare';
$field->label = 'Do not compare E-Mail-Addresses';
$field->description = __('Do not check whether an E-Mail-Address already exists.');
$field->value = 1;
$field->attr('checked', $noCompare ? 'checked' : '');
$field->columnWidth = 25;
$inputfields->add($field);
// mailserver field
$field = $this->modules->get('InputfieldText');
$field->name = 'mailfrom';
$field->label = __('E-Mail From Address');
$field->description = __('Sender Address');
$field->columnWidth = 25;
$inputfields->add($field);
// periodOfValidity field
$field = $this->modules->get('InputfieldInteger');
$field->name = 'periodOfValidity';
$field->label = __('Period of Validity');
$field->description = __('Number of days confirmation links are valid.');
$field->columnWidth = 25;
$inputfields->add($field);
// new fieldset containing messages
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = $this->_('E-Mail Messages');
$fieldset->collapsed = Inputfield::collapsedYes;
$fieldset->icon = 'send';
$field = $this->modules->get('InputfieldTextarea');
$field->name = 'messageSubscribe';
$field->label = __('Subscribe E-Mail Message');
$field->description = __('Use %fieldName% as placeholder, for example %email%');
$field->rows = 8;
$field->columnWidth = 50;
$fieldset->add($field);
$field = $this->modules->get('InputfieldTextarea');
$field->name = 'messageUnsubscribe';
$field->label = __('Unsubscribe E-Mail Message');
$field->description = __('Use %fieldName% as placeholder, for example %email%');
$field->rows = 8;
$field->columnWidth = 50;
$fieldset->add($field);
$inputfields->add($fieldset);
// new fieldset containing admin notification
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = $this->_('Notify Admin');
$fieldset->collapsed = Inputfield::collapsedYes;
$fieldset->icon = 'comment';
// notify admin field
$field = $this->modules->get('InputfieldCheckbox');
$field->name = 'notifyAdmin';
$field->label = 'Notify Admin';
$field->description = __('Should the admin be notified by email?');
$field->value = 1;
$field->columnWidth = 50;
$field->attr('checked', $notifyAdmin ? 'checked' : '');
$fieldset->add($field);
// notify admin - mailto field
$field = $this->modules->get('InputfieldText');
$field->name = 'notifyAdminMailto';
$field->label = __('Admin Notification E-Mail');
$field->description = __('Mailto Address');
$field->columnWidth = 50;
$field->showIf = "notifyAdmin=1";
$fieldset->add($field);
// notify admin - subscribtion - mailserver field
$field = $this->modules->get('InputfieldText');
$field->name = 'notifyAdminSubscribeMailfrom';
$field->label = __('Subscription - E-Mail From Address');
$field->description = __('Sender Address');
$field->columnWidth = 50;
$field->showIf = "notifyAdmin=1";
$fieldset->add($field);
// notify admin - unsubscribtion - mailserver field
$field = $this->modules->get('InputfieldText');
$field->name = 'notifyAdminUnsubscribeMailfrom';
$field->label = __('Unsubscription - E-Mail From Address');
$field->description = __('Sender Address');
$field->columnWidth = 50;
$field->showIf = "notifyAdmin=1";
$fieldset->add($field);
// notify admin - subscription - message field
$field = $this->modules->get('InputfieldTextarea');
$field->name = 'notifyAdminMessageSubscribe';
$field->label = __('Subscribe E-Mail Message');
$field->description = __('Use %fieldName% as placeholder, for example %email%');
$field->rows = 8;
$field->columnWidth = 50;
$field->showIf = "notifyAdmin=1";
$fieldset->add($field);
// notify admin - unsubscription - message field
$field = $this->modules->get('InputfieldTextarea');
$field->name = 'notifyAdminMessageUnsubscribe';
$field->label = __('Unsubscribe E-Mail Message');
$field->description = __('Use %fieldName% as placeholder, for example %email%');
$field->rows = 8;
$field->columnWidth = 50;
$field->showIf = "notifyAdmin=1";
$fieldset->add($field);
$inputfields->add($fieldset);
return $inputfields;
}
}