/[drupal]/contributions/modules/aweber/aweber.module
ViewVC logotype

Contents of /contributions/modules/aweber/aweber.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Sep 24 01:36:28 2008 UTC (14 months ago) by coltrane
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +75 -82 lines
File MIME type: text/x-php
Port of AWeber Integration module to Drupal 6. Not yet tested.
1 <?php
2 /* $Id: aweber.module,v 1.4 2008/09/18 23:40:35 coltrane Exp $ */
3
4 /**
5 * @file
6 * Eases management of AWeber email list subscribers and Drupal users.
7 */
8
9 define("AWEBER_SUBMITTED", 1);
10 define("AWEBER_SUBSCRIBED", 2);
11 define("AWEBER_UNSUBSCRIBED", 0);
12 define("AWEBER_CHANGED", 3);
13 define("AWEBER_DELETED", 4);
14
15 /**
16 * Implementation of hook_menu()
17 */
18 function aweber_menu() {
19 $items = array();
20
21 $items['aweberreturnpage'] = array(
22 'title' => 'AWeber return',
23 'page callback' => 'aweber_return_page',
24 'access callback' => TRUE,
25 'type' => MENU_CALLBACK
26 );
27
28 $items['admin/user/aweber'] = array(
29 'title' => 'AWeber',
30 'page callback' => 'aweber_overview',
31 'access arguments' => array('access administration pages'),
32 'type' => MENU_NORMAL_ITEM
33 );
34 $items['admin/user/aweber/list'] = array(
35 'title' => 'Leads',
36 'type' => MENU_DEFAULT_LOCAL_TASK,
37 'weight' => -10
38 );
39 $items['admin/user/aweber/settings'] = array(
40 'title' => 'Settings',
41 'page callback' => 'drupal_get_form',
42 'page arguments' => array('aweber_configure'),
43 'access arguments' => array('access administration pages'),
44 'type' => MENU_LOCAL_TASK
45 );
46 $items['admin/user/aweber/edit/%aweber_lead'] = array(
47 'title' => 'Edit lead status',
48 'page callback' => 'drupal_get_form',
49 'page arguments' => array('aweber_lead_edit_form'),
50 'access arguments' => array('access administration pages'),
51 'type' => MENU_CALLBACK
52 );
53 $items['admin/user/aweber/delete/%aweber_lead'] = array(
54 'title' => 'Delete lead',
55 'page callback' => 'drupal_get_form',
56 'page arguments' => array('aweber_lead_delete_form'),
57 'access arguments' => array('access administration pages'),
58 'type' => MENU_CALLBACK
59 );
60
61 return $items;
62 }
63
64 /**
65 * Configuration settings form
66 */
67 function aweber_configure() {
68 $form = array();
69
70 $form['aweber_account'] = array(
71 '#type' => 'fieldset',
72 '#title' => t('AWeber List'),
73 '#description' => t('Required settings for use of the module.'),
74 '#collapsible' => TRUE,
75 '#collapsed' => FALSE
76 );
77 $form['aweber_account']['aweber_list'] = array(
78 '#type' => 'textfield',
79 '#title' => t('List'),
80 '#description' => t('The list name users will be subscribing to. AWeber calls this the "unit".'),
81 '#default_value' => variable_get('aweber_list', ''),
82 '#required' => TRUE
83 );
84 $form['aweber_account']['aweber_id'] = array(
85 '#type' => 'textfield',
86 '#title' => t('ID'),
87 '#description' => t('The web form ID. AWeber calls this the "meta_web_form_id".'),
88 '#default_value' => variable_get('aweber_id', ''),
89 '#required' => TRUE
90 );
91
92 $form['aweber_user'] = array(
93 '#type' => 'fieldset',
94 '#title' => t('Account Integration'),
95 '#description' => t('Allow users to sign-up during registration and control subscribing and unsubscribing through their account settings.'),
96 '#collapsible' => TRUE,
97 '#collapsed' => FALSE
98 );
99 $form['aweber_user']['aweber_user_form'] = array(
100 '#type' => 'checkbox',
101 '#title' => t('Integrate on account form.'),
102 '#description' => t('Place sign-up option on registration and account edit pages.'),
103 '#default_value' => variable_get('aweber_user_form', 1)
104 );
105 $form['aweber_user']['aweber_registration_default'] = array(
106 '#type' => 'checkbox',
107 '#title' => t('Set checkbox on registration to checked.'),
108 '#description' => t('By default the registration checkbox is not checked. Check this box to sign leads up by default.'),
109 '#default_value' => variable_get('aweber_registration_default', 0)
110 );
111 $form['aweber_user']['aweber_option_text'] = array(
112 '#type' => 'textfield',
113 '#title' => t('Label'),
114 '#description' => t('The text to go alongside the sign-up checkbox.'),
115 '#default_value' => variable_get('aweber_option_text', 'Sign up for our newsletter.')
116 );
117
118 $form['aweber_thanks'] = array(
119 '#type' => 'fieldset',
120 '#title' => t('Thank You Page'),
121 '#description' => t('A Thank You page which records leads is provided if you use the built in block.'),
122 '#collapsible' => TRUE,
123 '#collapsed' => FALSE
124 );
125 $form['aweber_thanks']['aweber_thanks_message'] = array(
126 '#type' => 'textarea',
127 '#title' => t('Message'),
128 '#description' => t('The text to display on the Thank You page.'),
129 '#default_value' => variable_get('aweber_thanks_message', 'Thank you for subscribing! You will receive a confirmation message in your email.')
130 );
131
132 return system_settings_form($form);
133 }
134
135 function aweber_lead_edit_form($aweber_lead) {
136 $form['mail'] = array(
137 '#type' => 'markup',
138 '#value' => '<p>' . check_plain($aweber_lead->mail) . '</p>',
139 );
140 $form['lead'] = array(
141 '#type' => 'value',
142 '#value' => $aweber_lead,
143 );
144 $form['status'] = array(
145 '#type' => 'select',
146 '#title' => t('Status'),
147 '#options' => aweber_format_status(),
148 '#default_value' => $aweber_lead->status,
149 );
150 $form['submit'] = array(
151 '#type' => 'submit',
152 '#value' => t('Save'),
153 );
154 return $form;
155 }
156
157 function aweber_lead_edit_form_submit($form, &$form_state) {
158 aweber_update_lead_status($form_state['values']['status'], $form_state['values']['lead']->uid);
159 $form_state['redirect'] = 'admin/user/aweber';
160 }
161
162 function aweber_lead_delete_form($aweber_lead) {
163 $form['lead'] = array(
164 '#type' => 'hidden',
165 '#value' => $aweber_lead
166 );
167
168 return confirm_form(
169 $form,
170 t('Really delete this lead?'),
171 'admin/user/aweber',
172 t('This cannot be undone. Note that this does not delete the user account.'),
173 t('Delete'),
174 t('Cancel')
175 );
176 }
177
178 function aweber_lead_delete_form_submit($form, &$form_state) {
179 $lead = $form_state['values']['lead'];
180 $account = user_load(array('uid' => $lead->uid));
181 if ($account->uid) {
182 $account->aweber_signup = 0;
183 user_save($account, array('aweber_signup' => 0));
184 }
185
186 db_query("DELETE FROM {aweber} WHERE aid = %d", $aid);
187 drupal_set_message(t('Lead was deleted.'));
188 $form_state['redirect'] = 'admin/user/aweber';
189 }
190
191 function aweber_lead_load($aid) {
192 $result = db_query('SELECT * FROM {aweber} WHERE aid = %d', $aid);
193 return db_fetch_object($result);
194 }
195
196 /**
197 * AWeber web form
198 */
199 function aweber_web_form() {
200 global $base_url;
201 global $user;
202 $form = array();
203
204 $form['aweber']['meta_web_form_id'] = array(
205 '#type' => 'hidden',
206 '#value' => variable_get('aweber_id', '')
207 );
208 $form['aweber']['meta_split_id'] = array(
209 '#type' => 'hidden',
210 '#value' => ''
211 );
212 $form['aweber']['unit'] = array(
213 '#type' => 'hidden',
214 '#value' => variable_get('aweber_list', '')
215 );
216 $form['aweber']['redirect'] = array(
217 '#type' => 'hidden',
218 '#value' => url('aweberreturnpage', array('absolute' => TRUE))
219 );
220 $form['aweber']['meta_redirect_onlist'] = array(
221 '#type' => 'hidden',
222 '#value' => ''
223 );
224 $form['aweber']['meta_adtracking'] = array(
225 '#type' => 'hidden',
226 '#value' => ''
227 );
228 $form['aweber']['meta_message'] = array(
229 '#type' => 'hidden',
230 '#value' => '1'
231 );
232 $form['aweber']['meta_required'] = array(
233 '#type' => 'hidden',
234 '#value' => 'from'
235 );
236 $form['aweber']['meta_forward_vars'] = array(
237 '#type' => 'hidden',
238 '#value' => '1'
239 );
240
241 $form['aweber']['from'] = array(
242 '#type' => 'textfield',
243 '#size' => '20',
244 '#default_value' => $user->uid ? check_plain($user->mail) : ''
245 );
246
247 $form['submit'] = array(
248 '#type' => 'submit',
249 '#value' => 'Submit',
250 '#name' => 'submit'
251 );
252
253 $form['#action'] = 'http://www.aweber.com/scripts/addlead.pl';
254
255 return $form;
256 }
257
258 /**
259 * Implementation of hook_block()
260 */
261 function aweber_block($op = 'list', $delta = 0, $edit = array()) {
262 global $user;
263
264 switch ($op) {
265 case 'list':
266 $blocks['aweber_form'] = array('info' => t('AWeber Sign-up Form'));
267 return $blocks;
268
269 case 'configure':
270 $form['aweber_block_text'] = array(
271 '#type' => 'textarea',
272 '#title' => t('Description'),
273 '#description' => t('Text to display above the form.'),
274 '#default_value' => variable_get('aweber_block_text', '')
275 );
276 return $form;
277
278 case 'save':
279 variable_set('aweber_block_text', $edit['aweber_block_text']);
280 break;
281
282 case 'view':
283 if ($delta == 'aweber_form' && ($user->uid == 0 || $user->aweber_signup == 0)) {
284 $block['subject'] = t('Newsletter');
285 if (variable_get('aweber_block_text', '') != '') {
286 $block['content'] = theme('aweber_block_text', variable_get('aweber_block_text', ''));
287 $block['content'] .= drupal_get_form('aweber_web_form');
288 }
289 else {
290 $block['content'] = drupal_get_form('aweber_web_form');
291 }
292 return $block;
293 }
294 break;
295 }
296 }
297
298 /**
299 * Parse the return variables from AWeber
300 */
301 function _aweber_parse_get() {
302 $data = array();
303
304 if (!empty($_GET['from']) && !empty($_GET['unit'])) {
305 $data['from'] = $_GET['from'];
306 $data['unit'] = urldecode($_GET['unit']);
307 $data['meta_web_form_id'] = urldecode($_GET['meta_web_form_id']);
308 }
309
310 return $data;
311 }
312
313 /**
314 * Callback given to AWeber as the thank you page
315 * does a drupal_goto to the actual thank you page to avoid duplicate _aweber_save_lead()s
316 */
317 function aweber_return_page() {
318 global $user;
319
320 if (variable_get('aweber_thanks_save', 1)) {
321 $data = _aweber_parse_get();
322
323 if (empty($data)) {
324 watchdog('aweber', 'Unable to parse and save variables returned by AWeber sign-up submit.', array(), WATCHDOG_ERROR);
325 }
326 else {
327 _aweber_save_lead($data);
328 if ($user->uid) {
329 $user->aweber_signup = 1; // keep our insert hook from resubmitting
330 user_save($user, array('aweber_signup' => 1));
331 }
332 }
333 }
334
335 if (variable_get('aweber_thank_you_path', '') != '') {
336 drupal_goto('aweberty');
337 }
338 else {
339 return t(variable_get('aweber_thanks_message', 'Thank you for subscribing!'));
340 }
341 }
342
343 /**
344 * Insert a lead
345 */
346 function _aweber_save_lead($data, $uid = NULL) {
347 global $user;
348
349 if ($user->uid || $uid) {
350 $uid = $user->uid ? $user->uid : $uid;
351 db_query("INSERT INTO {aweber} (uid, mail, status, list, id, timestamp) VALUES (%d, '%s', %d, '%s', %d, %d)", $uid, $data['from'], AWEBER_SUBMITTED, $data['unit'], $data['meta_web_form_id'], time());
352 }
353 else {
354 db_query("INSERT INTO {aweber} (mail, status, list, id, timestamp) VALUES ('%s', %d, '%s', %d, %d)", $data['from'], AWEBER_SUBMITTED, $data['unit'], $data['meta_web_form_id'], time());
355 }
356
357 watchdog('aweber', 'New AWeber lead @mail', array('@mail' => $data['from']), WATCHDOG_NOTICE);
358 }
359
360 /**
361 * Overview page of leads
362 */
363 function aweber_overview() {
364 $items[] = t('Submitted: User has begun the signup process. You should check if they followup on the AWeber confirmation by checking your list on aweber.com.');
365 $items[] = t('Subscribed: Mark a lead as subscribed once they have confirmed subscription on aweber.com');
366 $items[] = t('Changed: If a user alters their email address here on the site you should update their subscription on aweber.');
367 $output = theme('item_list', $items, t('Status Legend'));
368
369 $header = array(
370 array('data' => t('Address'), 'field' => 'mail'),
371 array('data' => t('User')),
372 array('data' => t('Status'), 'field' => 'status'),
373 array('data' => t('Added'), 'field' => 'timestamp'),
374 array('data' => t('Operations'), 'colspan' => '1')
375 );
376
377 $sql = 'SELECT * FROM {aweber}';
378 $result = pager_query($sql . tablesort_sql($header), 50);
379 $rows = array();
380 while ($data = db_fetch_object($result)) {
381 $rows[] = array(
382 array('data' => check_plain($data->mail)),
383 array('data' => $data->uid ? theme('username', user_load(array('uid' => $data->uid))) : ''),
384 array('data' => aweber_format_status($data->status)),
385 array('data' => format_date($data->timestamp, 'custom', 'D, M Y g:ia')),
386 array('data' => l(t('edit'), 'admin/user/aweber/edit/'. $data->aid)),
387 array('data' => l(t('delete'), 'admin/user/aweber/delete/'. $data->aid))
388 );
389 }
390
391 if ($rows) {
392 $pager = theme('pager', NULL, 50, 0);
393 $output .= '<h3>'. t('Leads') .'</h3>';
394 $output .= theme('table', $header, $rows);
395 }
396 else {
397 $output .= t('There are no leads.');
398 }
399
400 return $output;
401 }
402
403 function aweber_format_status($status = NULL) {
404 $map = array(
405 AWEBER_SUBMITTED => t('Submitted'),
406 AWEBER_SUBSCRIBED => t('Subscribed'),
407 AWEBER_UNSUBSCRIBED => t('Unsubscribed'),
408 AWEBER_CHANGED => t('Changed'),
409 AWEBER_DELETED => t('Deleted')
410 );
411 return is_null($status) ? $map : $map[$status];
412 }
413
414 /**
415 * Implementation of hook_user()
416 */
417 function aweber_user($op, &$edit, &$account, $category = NULL) {
418 switch ($op) {
419 case 'register':
420 if (variable_get('aweber_user_form', 0) && variable_get('aweber_id', '') != '') {
421 $form['aweber'] = array(
422 '#type' => 'fieldset',
423 '#title' => t('Newsletter'),
424 '#collapsible' => TRUE,
425 '#collapsed' => FALSE
426 );
427 $form['aweber']['aweber_signup'] = array(
428 '#type' => 'checkbox',
429 '#title' => t(variable_get('aweber_option_text', 'Sign up for our newsletter.')),
430 '#default_value' => variable_get('aweber_registration_default', 0),
431 );
432 return $form;
433 }
434 break;
435 case 'form':
436 if (variable_get('aweber_user_form', 0) && variable_get('aweber_id', '') != '' && $category == 'account') {
437 $form['aweber'] = array(
438 '#type' => 'fieldset',
439 '#title' => t('Newsletter'),
440 '#collapsible' => TRUE,
441 '#collapsed' => FALSE
442 );
443 $form['aweber']['aweber_signup'] = array(
444 '#type' => 'checkbox',
445 '#title' => t(variable_get('aweber_option_text', 'Sign up for our newsletter.')),
446 '#default_value' => !empty($account->aweber_signup) ? $account->aweber_signup : 0,
447 );
448 return $form;
449 }
450 break;
451 case 'update':
452 if (isset($edit['mail']) && $edit['mail'] != $account->mail && $account->aweber_signup == 1) {
453 aweber_update_mail($edit['mail'], $account->uid);
454 }
455 // purposefully fall through
456 case 'insert':
457 if (isset($edit['aweber_signup']) && $edit['aweber_signup'] == 1 && $account->aweber_signup == 0) {
458 aweber_form_post($account->mail, $account->uid);
459 }
460 else if (isset($edit['aweber_signup']) && $edit['aweber_signup'] == 0 && $account->aweber_signup == 1) {
461 aweber_update_lead_status(AWEBER_UNSUBSCRIBED, $account->uid);
462 }
463 break;
464 case 'delete':
465 if ($account->aweber_signup == 1) {
466 aweber_update_lead_status(AWEBER_DELETED, $account->uid);
467 }
468 break;
469 }
470 }
471
472 function aweber_update_mail($mail, $uid) {
473 db_query("UPDATE {aweber} SET mail = '%s', status = %d WHERE uid = %d", $mail, AWEBER_CHANGED, $uid);
474 }
475
476 function aweber_update_lead_status($status, $uid) {
477 db_query('UPDATE {aweber} SET status = %d WHERE uid = %d', $status, $uid);
478 }
479
480 /**
481 * Automatic signup post to AWeber
482 */
483 function aweber_form_post($from, $uid) {
484 $data = array(
485 'from' => $from,
486 'meta_web_form_id' => variable_get('aweber_id', ''),
487 'meta_split_id' => '',
488 'unit' => variable_get('aweber_list', ''),
489 'redirect' => 'http://www.aweber.com/form/thankyou_vo.html',
490 'meta_redirect_onlist' => '',
491 'meta_adtracking' => '',
492 'meta_message' => '1',
493 'meta_required' => 'from',
494 'meta_forward_vars' => '0'
495 );
496
497 $url = 'http://www.aweber.com/scripts/addlead.pl';
498 // the magic!
499 drupal_http_request($url, array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'), 'POST', http_build_query($data, '', '&'));
500
501 _aweber_save_lead($data, $uid);
502 }
503
504 /**
505 * Implementation of hook_theme().
506 */
507 function aweber_theme() {
508 return array(
509 'aweber_block_text' => array(
510 'arguments' => array('text' => NULL),
511 ),
512 );
513 }
514
515 /**
516 * Theme text shown above form block
517 */
518 function theme_aweber_block_text($text) {
519 return t('<p>@text</p>', array('@text' => $text));
520 }

  ViewVC Help
Powered by ViewVC 1.1.2