/
freeform_ft.subscribe.php
234 lines (185 loc) · 7.26 KB
/
freeform_ft.subscribe.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
require_once('config.php');
class Subscribe_freeform_ft extends Freeform_base_ft
{
public $info = [
'name' => SUBSCRIBE_MOD_NAME,
'version' => SUBSCRIBE_VERSION,
'description' => SUBSCRIBE_DESCRIPTION,
];
public $show_label = false;
// our fields to match emma fields
protected $fields = [
'name_first' => 'first_name',
'name_last' => 'last_name',
];
protected $text = 'Join our mailing list';
public function __construct()
{
parent::__construct();
if (REQ == 'CP') {
ee()->load->model('subscribe_model');
}
}
public function display_settings($data = [])
{
ee()->load->model('subscribe_model');
if (!ee()->subscribe_model->check()) {
ee()->table->add_row(
'<h3>Error</h3>',
'<div class="subtext ss_notice">Your configuration is not working, please visit the settings page and update your username/password.</div>'
);
return;
}
$list = isset($data['list']) ? $data['list'] : false;
$type = isset($data['type']) ? $data['type'] : false;
$text = isset($data['text']) ? $data['text'] : false;
$auto = isset($data['auto']) ? $data['auto'] : false;
$field = isset($data['field']) ? $data['field'] : false;
$groups = ee()->subscribe_model->lists();
$options = [];
foreach ($groups->all() as $id => $group) {
$options[$group['id']] = $group['name'];
}
ee()->table->add_row(
'List <div class="subtext">Selet the list users will sign up to.</div>',
form_dropdown('subscribe_list', $options, $list)
);
ee()->table->add_row(
'Type of Signup <div class="subtext">
<strong>Always</strong> - user will automatically be added to the list<br>
<strong>Opt-In</strong> - user will need to Opt-In to be added to the list
</div>',
'<label style="padding:0 5px">Always</label>'.
form_radio('subscribe_type', 'always', $type == 'always').
'<label style="padding:0 5px">Opt-In</label>'.
form_radio('subscribe_type', 'opt-in', $type != 'always')
);
ee()->table->add_row(
'Opt-In Autocheck <div class="subtext">Autocheck Checkbox if Opt-In is selected</div>',
'<label style="padding:0 5px">Enabled</label>'.
form_checkbox('subscribe_auto', 'true', $auto == 'true')
);
ee()->table->add_row(
'Opt-In Text <div class="subtext">Displayed with Checkbox if Opt-In is selected</div>',
form_input('subscribe_opt-in_text', $text)
);
ee()->table->add_row(
'Email Field<div class="subtext">If the input field on the page is not "email" please entrer the name. (exp. the marketo form used "work_email")</div>',
form_input('subscribe_field', $field)
);
}
public function save_settings()
{
$list = ee()->input->post('subscribe_list');
$type = ee()->input->post('subscribe_type');
$text = ee()->input->post('subscribe_opt-in_text');
$auto = ee()->input->post('subscribe_auto');
$field = ee()->input->post('subscribe_field');
return [
'list' => $list,
'type' => $type,
'text' => $text,
'auto' => $auto,
'field' => $field,
];
}
public function display_composer_field($data = null)
{
$s = $this->settings;
$type = isset($s['type']) ? $s['type'] : 'opt-in';
if ($type == 'always') {
return 'This field is set to "Always" add user to the list, It will not display any code on the form.';
}
}
public function save($data)
{
ee()->load->model('subscribe_model');
// Does not fire on opt-in if checkbox is not selected
$fields = ee()->subscribe_model->getFields();
$settings = $this->settings;
$groups[] = env('REALMAGNET_GROUP', $settings['list']);
$input = $settings['field'] ?: 'email';
$email = ee()->input->post($input);
$user = [];
foreach ($fields->all() as $key => $field) {
$v = strtolower($key);
$user[$v] = ee()->input->post($v);
if (isset($this->fields[$v])) {
$n = $this->fields[$v];
$user[$v] = ee()->input->post($n);
}
}
if ($email) {
$user['email'] = $email;
}
$add = true;
// is user in the system ???
$return = 'Always: ';
if ($settings['type'] == 'opt-in') {
$return = 'Opt-in: ';
$opt = ee()->input->post($this->field_name.'_opt-in');
if ($opt !== 'y') {
// Does not fire on opt-in if checkbox is not selected
$return .= 'False';
$add = false;
}
}
if ($add && $settings['entry_id'] == 0) {
// new user
$response = ee()->subscribe_model->signup($user, $groups);
if ($response->isSuccessful()) {
$return .= 'Success ('.$response->data->get('id').')';
} else {
$return .= 'Failed';
}
}
return $return;
}
public function display_entry_cp($data)
{
return $data;
}
public function install()
{
ee()->load->model('subscribe_model');
if (!ee()->subscribe_model->check()) {
return ee()->_mcp_reference->actions->full_stop(lang('subscribe_not_installed'));
}
}
public function display_field($data = '', $params = [], $attr = [])
{
if (ee()->input->get('module') == 'freeform' && ee()->input->get('method') == 'edit_entry') {
$pattern = '/Added \((\d+)\)/';
if (preg_match($pattern, $data, $matches)) {
$id = $matches[1];
$link = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=subscribe'.AMP.'method=subscribe_add_edit_user_form'.AMP.'group_id=0'.AMP.'id='.$id;
return '<a href="'.$link.'" target="_blank">'.$data.'</a>';
}
return $data;
}
$s = $this->settings;
$type = (isset($s['type']) && $s['type']) ? $s['type'] : 'opt-in';
$text = (isset($s['text']) && $s['text']) ? $s['text'] : $this->text;
$auto = isset($s['auto']) && $s['auto'] == 'true';
//echo "<pre>".__FILE__.'<br>'.__METHOD__.' : '.__LINE__."<br><br>"; var_dump( $auto ); exit;
if ($type == 'always') {
return form_hidden($this->field_name, 'always');
}
$id = 'freeform_field_'.$this->field_id;
$checkboxOpts = [
'name' => $this->field_name.'_opt-in',
'id' => $id,
'value' => 'y',
'class' => 'form__check',
];
if ($auto) {
$checkboxOpts['checked'] = 'checked';
}
return form_hidden($this->field_name, 'opt-in').
form_checkbox($checkboxOpts).
form_label($text, $id, [
'class' => 'form__label form__label--check',
]);
}
}