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

Contents of /contributions/modules/constant_contact/constant_contact.module

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


Revision 1.6 - (show annotations) (download) (as text)
Sun Feb 17 10:28:48 2008 UTC (21 months, 1 week ago) by konsumer
Branch: MAIN
CVS Tags: DRUPAL-5--2-0, DRUPAL-5--1-0
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.5: +10 -3 lines
File MIME type: text/x-php
applied brenda003's patch for theming support
1 <?php
2 // $Id
3
4 /**
5 * Implementation of hook_perm().
6 */
7 function constant_contact_perm() {
8 return array('admininister constant_contact');
9 }
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function constant_contact_menu($may_cache) {
15 $items = array();
16
17 $items[] = array(
18 'path' => 'admin/settings/constant_contact',
19 'title' => t('Constant Contact Settings'),
20 'description' => t('Configure settings for Constant Contact'),
21 'callback' => 'drupal_get_form',
22 'callback arguments' => array('constant_contact_admin_settings'),
23 'access' => user_access('admininister constant_contact'),
24 'type' => MENU_NORMAL_ITEM);
25
26 $items[] = array(
27 'path' => 'admin/settings/constant_contact_import',
28 'title' => t('Constant Contact Import Users'),
29 'description' => t('Import existing Drupal users to Constant Contact'),
30 'callback' => 'drupal_get_form',
31 'callback arguments' => array('constant_contact_import'),
32 'access' => user_access('admininister constant_contact'),
33 'type' => MENU_NORMAL_ITEM);
34
35 $items[] = array(
36 'path' => 'constant_contact/unsubscribe',
37 'title' => t('List Unsubscribe'),
38 'description' => t('Unsubscribe from a list'),
39 'callback' => 'drupal_get_form',
40 'callback arguments' => array('constant_contact_unsubscribe_confirm'),
41 'type' => MENU_CALLBACK,
42 'access' => TRUE,
43 );
44
45 return $items;
46 }
47
48 /**
49 * Implementation of hook_help().
50 */
51 function constant_contact_help($section) {
52 switch ($section) {
53 case 'admin/help/constant_contact':
54 case 'admin/help#constant_contact':
55 // Return a line-break version of the module README
56 return filter_filter('process', 2, NULL, file_get_contents( dirname(__FILE__)."/README.txt") );
57 }
58 }
59
60 /**
61 * Implementation of hook_block().
62 */
63 function constant_contact_block($op = 'list', $delta = 0) {
64 switch ($op) {
65 case 'list':
66 $blocks=array();
67 $lists=constant_contact_get_lists();
68 foreach ($lists as $i => $list) {
69 $blocks[]=array('info' => t('Constant Contact: '. $list));
70 }
71 return $blocks;
72 break;
73 case 'view':
74 global $user;
75
76 if (empty($user->uid) && !variable_get('constant_contact_allowanon', TRUE)) {
77 return;
78 }
79 $lists=constant_contact_get_lists();
80 $lists_arr=array_values($lists);
81 $keyname=constant_contact_escape_listname($lists_arr[$delta]);
82 $list=$lists[$keyname];
83
84 if (constant_contact_check_user($list)) {
85 return;
86 }
87 $block['content'] = drupal_get_form('constant_contact_block_form', $delta);
88 return $block;
89 break;
90 }
91
92
93 }
94
95
96 /**
97 * Implementation of hook_form()
98 */
99 function constant_contact_admin_settings() {
100 $form = array();
101
102 $form['constant_contact_server'] = array(
103 '#type' => 'fieldset',
104 '#title' => t('Server Settings'),
105 '#description' => t('Put your Constant Contact login info in here'),
106 '#collapsible' => true,
107 );
108
109 $form['constant_contact_server']['constant_contact_username'] = array(
110 '#type' => 'textfield',
111 '#title' => t('Username'),
112 '#default_value' => variable_get('constant_contact_username', ''),
113 '#size' => 30,
114 '#description' => t('The username you use to login to Constant Contact'),
115 );
116 $form['constant_contact_server']['constant_contact_password'] = array(
117 '#type' => 'textfield',
118 '#title' => t('Password'),
119 '#default_value' => variable_get('constant_contact_password', ''),
120 '#size' => 30,
121 '#description' => t('The password you use to login to Constant Contact'),
122 );
123 $form['constant_contact_server']['advanced'] = array(
124 '#type' => 'fieldset',
125 '#title' => t('Advanced'),
126 '#collapsible' => true,
127 '#collapsed' => true,
128 );
129
130 $form['constant_contact_server']['advanced']['constant_contact_addurl']=array(
131 '#type' => 'textfield',
132 '#title' => t('Add Url'),
133 '#default_value' => variable_get('constant_contact_addurl', 'http://ui.constantcontact.com/roving/wdk/API_AddSiteVisitor.jsp'),
134 '#description' => t('The url of the add server'),
135 );
136
137 $form['constant_contact_server']['advanced']['constant_contact_delurl']=array(
138 '#type' => 'textfield',
139 '#title' => t('Remove Url'),
140 '#default_value' => variable_get('constant_contact_delurl', 'http://ui.constantcontact.com/roving/wdk/API_UnsubscribeSiteVisitor.jsp'),
141 '#description' => t('The url of the remove server'),
142 );
143
144 $form['email']=array(
145 '#type' => 'fieldset',
146 '#title' => t('Confirmation Email'),
147 '#description' => t('Customize the e-mail that is sent when the user registers. Available variables are: !list_name, !uri_list_unsubscribe, !username, !site, !password, !uri, !uri_brief, !mailto, !date, !login_uri, !login_url.'),
148 '#collapsible' => true,
149 );
150 $form['email']['constant_contact_sendemail']=array(
151 '#type' => 'checkbox',
152 '#title' => t('Send Email'),
153 '#default_value' => variable_get('constant_contact_sendemail', TRUE),
154 );
155
156 $form['email']['constant_contact_email_subject']=array(
157 '#type' => 'textfield',
158 '#title' => t('Subject'),
159 '#default_value' => variable_get('constant_contact_email_subject', 'Welcome to !list_name Mailing List!'),
160 );
161 $form['email']['constant_contact_email_body'] = array(
162 '#type' => 'textarea',
163 '#title' => t('Body'),
164 '#default_value' => variable_get('constant_contact_email_body', "Dear !username,\nThanks for joining the !list_name mailing list at !uri. If you would like to unsubscribe, use the following URL: !uri_list_unsubscribe\nThanks,\nThe !site Team"),
165 );
166
167 $form['constant_contact_lists'] = array(
168 '#type' => 'fieldset',
169 '#title' => t('List Settings'),
170 '#description' => t('Settings for particular mailing lists'),
171 '#collapsible' => true,
172 );
173
174 $form['constant_contact_lists']['constant_contact_allowanon']=array(
175 '#type' => 'checkbox',
176 '#title' => t('Allow anonymous Users'),
177 '#default_value' => variable_get('constant_contact_allowanon', TRUE),
178 '#description' => t('Can non-logged-in users add themselves to lists? If so, a form will be displayed that will allow them to enter their email address.'),
179 );
180
181 $form['constant_contact_lists']['constant_contact_lists'] = array(
182 '#type' => 'textarea',
183 '#title' => t('Lists'),
184 '#default_value' => variable_get('constant_contact_lists', ''),
185 '#size' => 30,
186 '#description' => t('Put all your Constant Contact mailing lists that you want integration for in here, one per line. I recommend using names shorter then 21 characters, so the keys will be distinct.'),
187 );
188
189 $form['constant_contact_lists']['blocktext']=array(
190 '#type' => 'fieldset',
191 '#title' => t('Block Text'),
192 '#description' => t('The text that appears in the signup block'),
193 '#collapsible' => true,
194 '#collapsed' => true,
195 );
196
197 $lists=constant_contact_get_lists();
198 foreach ($lists as $keyname => $list) {
199 $form['constant_contact_lists']['blocktext'][$keyname] = array(
200 '#type' => 'textfield',
201 '#title' => $list . t(' text'),
202 '#default_value' => variable_get($keyname, "Join $list Mailing List"),
203 '#size' => 30,
204 );
205 }
206
207 return system_settings_form($form);
208 }
209
210 /**
211 * Implementation of hook_form()
212 * creates the join mailing list block
213 */
214 function constant_contact_block_form($delta = 0, $op = 'list', $edit = array()) {
215 global $user;
216
217 $lists=constant_contact_get_lists();
218 $lists_arr=array_values($lists);
219 $keyname=constant_contact_escape_listname($lists_arr[$delta]);
220 $list=$lists[$keyname];
221
222 if (constant_contact_check_user($list)) {
223 return;
224 }
225
226 $form=array();
227
228 $form[] = array(
229 '#value' => '<div id="'. $keyname .'" class="constant_contact_join">'. variable_get($keyname, "Join $list Mailing List") .'</div>',
230 );
231
232 // give anonymous users an email field
233 if (empty($user->uid)) {
234 $form['email'] = array(
235 '#type' => 'textfield',
236 '#title' => t('Email Address'),
237 '#size' => 10,
238 );
239 }
240
241 $form['list'] = array(
242 '#type' => 'hidden',
243 '#value' => $list,
244 );
245
246 $form[] = array(
247 '#type' => 'submit',
248 '#value' => t('Join'),
249 );
250
251 return $form;
252 }
253
254
255 /**
256 * Implementation of hook_form_validate()
257 * validate the join mailing list block
258 */
259 function constant_contact_block_form_validate($form_id, $values) {
260 global $user;
261 $ok=((empty($user->uid) && !empty($values['email'])) || !empty($user->uid));
262
263 if (!$ok){
264 drupal_set_message(t('You need to set your email address.'), 'error');
265 }
266
267 return $ok;
268 }
269
270 /**
271 * Implementation of hook_form_submit()
272 * process the join mailing list block
273 */
274 function constant_contact_block_form_submit($form_id, $values) {
275 global $user;
276 $data=array();
277 if ($user->uid) {
278 $email=$user->mail;
279 $data['Custom_field_1']=$user->name;
280 $data['Custom_field_2']=$user->uid;
281 $data['Custom_field_3']=date('r');
282 }
283 else{
284 $email=$values['email'];
285 }
286
287 require_once('ConstantContact.php');
288 $constant_contact = new ConstantContact();
289 $constant_contact->setUsername(variable_get('constant_contact_username', ''));
290 $constant_contact->setPassword(variable_get('constant_contact_password', ''));
291 $constant_contact->setCategory($values['list']);
292
293 if ($constant_contact->add($email, $data)) {
294 $type='status';
295 $message='You have been added to our list.';
296 if (variable_get('constant_contact_sendemail', TRUE)) {
297 constant_contact_confirmation_email($email, $values['list']);
298 }
299 constant_contact_save_user($user, $values['list']);
300
301 }
302 else{
303 $type='error';
304 $message='There was a problem adding you to the list';
305 }
306 drupal_set_message(t($message), $type);
307 }
308
309 /**
310 * Implementation of hook_form()
311 * confirm unsubscribe
312 */
313 function constant_contact_unsubscribe_confirm($list=NULL) {
314 $lists=constant_contact_get_lists();
315 $list=urldecode($list);
316
317 if (empty($list) || !array_search($list, $lists)) {
318 drupal_set_message(t('Invalid list name.'), 'error');
319 return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
320 }
321
322 $form['list'] = array(
323 '#type' => 'hidden',
324 '#value' => $list,
325 );
326
327 $form = confirm_form($form,
328 t('Are you sure you want to unsubscribe from %list?', array('%list' => $list)),
329 $_GET['destination'] ? $_GET['destination'] : '<front>',
330 t('You must be logged in and subscribed under your account email, for this to work.'),
331 t('Unsubscribe'), t('Cancel')
332 );
333 return $form;
334 }
335
336 /**
337 * Implementation of hook_form_submit()
338 * process confirm unsubscribe
339 */
340 function constant_contact_unsubscribe_confirm_submit($form_id, $values) {
341 global $user;
342
343 if (!constant_contact_check_user($values['list'])) {
344 if (empty($user->uid)) {
345 drupal_set_message(t('You must be logged in to unsubscribe.'), 'error');
346 return drupal_goto('user/user', 'destination=constant_contact/unsubscribe/'. urlencode($values['list']));
347 }
348 else{
349 drupal_set_message(t('You are logged in, but your email does not appear to be subscribed to that list.'), 'error');
350 return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
351 }
352 }
353
354 $lists=constant_contact_get_lists();
355
356 if (empty($values['list']) || !array_search($values['list'], $lists)) {
357 drupal_set_message(t('Invalid list name.'), 'error');
358 return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
359 }
360
361 require_once('ConstantContact.php');
362 $constant_contact = new ConstantContact();
363 $constant_contact->setUsername(variable_get('constant_contact_username', ''));
364 $constant_contact->setPassword(variable_get('constant_contact_password', ''));
365 $constant_contact->setCategory($values['list']);
366
367 if ($constant_contact->remove($user->mail)) {
368 $type='status';
369 $message=t('You have been removed from %list mailing list.', array('%list' => $values['list']));
370 constant_contact_save_user($user, $values['list'], FALSE);
371 }
372 else{
373 $type='error';
374 $message=t('You could not be removed from %list mailing list.', array('%list' => $values['list']));
375 $_GET['destination']='constant_contact/unsubscribe/'. urlencode($values['list']);
376 }
377
378 drupal_set_message($message, $type);
379 return drupal_goto($_GET['destination'] ? $_GET['destination'] : '<front>');
380 }
381
382 /**
383 * Implementation of hook_form()
384 * import users
385 */
386 function constant_contact_import() {
387 $form = array(array(
388 '#value' => '<p>'. t("This will import all your drupal users into your Constant Contact account. It doesn't send confirmation emails, so if you want that, you will need to send a mailing-list email, on the Constant Contact site.") .'</p>',
389 ));
390
391 $lists = constant_contact_get_lists();
392
393 $form['list'] = array(
394 '#type' => 'select',
395 '#title' => t('List'),
396 '#options' => array_combine(array_values($lists), array_values($lists)),
397 '#description' => t('Choose the mailing list to import them to.'),
398 );
399
400 $form[] = array(
401 '#type' => 'submit',
402 '#value' => t('Import'),
403 );
404
405 return $form;
406 }
407
408 /**
409 * Implementation of hook_form_submit()
410 * process import users
411 */
412 function constant_contact_import_submit($form_id, $values) {
413 $lists = constant_contact_get_lists();
414 $rs = db_query("SELECT * FROM {users}");
415 $fail_count = $success_count=0;
416
417 require_once('ConstantContact.php');
418 $constant_contact = new ConstantContact();
419 $constant_contact->setUsername(variable_get('constant_contact_username', ''));
420 $constant_contact->setPassword(variable_get('constant_contact_password', ''));
421 $constant_contact->setCategory($values['list']);
422
423 while ($user = db_fetch_object($rs)) {
424 if (empty($user->uid)) {
425 continue;
426 }
427
428 $data=array(
429 'Custom_field_1' => $user->name,
430 'Custom_field_2' => $user->uid,
431 'Custom_field_3' => date('r'),
432 );
433
434 if ($constant_contact->add($user->mail, $data)) {
435 constant_contact_save_user($user, $values['list']);
436 $success_count++;
437 }
438 else{
439 $fail_count++;
440 }
441 }
442
443 if ($success_count) {
444 drupal_set_message(t('!sc users sucessfully imported.', array('!sc' => $success_count)));
445 }
446 if ($fail_count) {
447 drupal_set_message(t('!sc users failed to be imported.', array('!sc' => $fail_count)), 'error');
448 }
449 }
450
451 /**
452 * Store list in user-data, or session for anon users
453 * @param string $list is the friendly name of the list
454 * @param boolean $subscribe indicates whether we are adding or removing
455 * @return boolean success status
456 */
457 function constant_contact_save_user(&$user, $list, $subscribe=TRUE) {
458
459 // save in session for anon users, and when the user logs out
460 $_SESSION['constant_contact_lists'] = (isset($user->constant_contact_lists)) ? $user->constant_contact_lists : array();
461 $_SESSION['constant_contact_lists'][$list] = $subscribe;
462
463 if (empty($user->uid)) {
464 return TRUE;
465 }
466
467 $data = array();
468 $data['constant_contact_lists']=(isset($user->constant_contact_lists)) ? $user->constant_contact_lists : array();
469 $data['constant_contact_lists'][$list] = $subscribe;
470
471 $u=user_save($user, $data);
472 if (!empty($u)) {
473 $user=$u;
474 return TRUE;
475 }
476 else{
477 drupal_set_message(t('Error storing subscribtion information.'), 'error');
478 return FALSE;
479 }
480
481 }
482
483 /**
484 * returns boolean of whether or not the user (or anon subscriber) has signed up
485 * @param string $list the friendly name of the list to check
486 * @return boolean whether or not the user (or anon subscriber) has signed up
487 */
488 function constant_contact_check_user($list) {
489 global $user;
490
491 return (
492 isset($_SESSION['constant_contact_lists']) &&
493 isset($_SESSION['constant_contact_lists'][$list]) &&
494 $_SESSION['constant_contact_lists'][$list]
495 ) || (
496 isset($user->constant_contact_lists) &&
497 isset($user->constant_contact_lists[$list]) &&
498 $user->constant_contact_lists[$list]
499 );
500 }
501
502 /**
503 * Send a confirmation email
504 * @param string $address email to send to
505 * @param string $list_name friedly name of list that is being subscribed to
506 * @return boolean success status
507 */
508 function constant_contact_confirmation_email($address, $list_name) {
509 global $user, $base_url;
510
511 $variables = array(
512 '!list_name' => $list_name,
513 '!username' => $user->name,
514 '!site' => variable_get('site_name', 'Drupal'),
515 '!login_url' => user_pass_reset_url($account),
516 '!uri' => $base_url,
517 '!uri_brief' => substr($base_url, strlen('http://')),
518 '!mailto' => $address,
519 '!date' => format_date(time()),
520 '!login_uri' => url('user', NULL, NULL, TRUE), // version 6 is url('user', array('absolute' => TRUE)),
521 '!uri_list_unsubscribe' => url('constant_contact/unsubscribe/'. urlencode($list_name), NULL, NULL, TRUE), // version 6 is url('user', array('absolute' => TRUE)),
522 );
523
524 return drupal_mail('constant_contact-confirmation', $address, t(variable_get('constant_contact_email_subject', ''), $variables), t(variable_get('constant_contact_email_body', ''), $variables), variable_get('site_mail', ini_get('sendmail_from')));
525 }
526
527
528 /**
529 * returns an array of available lists
530 * @return array list of available lists, based on constant_contact_lists variable
531 */
532 function constant_contact_get_lists() {
533 $lists=explode("\n", variable_get('constant_contact_lists', ''));
534 $newlists=array();
535 foreach ($lists as $i => $list) {
536 $list=trim($list);
537 if (empty($list)) {
538 continue;
539 }
540 $newlists[constant_contact_escape_listname($list)]=$list;
541 }
542 return $newlists;
543 }
544
545
546 /**
547 * makes the list name key for a mailing-list
548 * @param string $string a "friendly" name of a list
549 * @return string a key name of a list
550 */
551 function constant_contact_escape_listname($string) {
552 return substr("constant_contact_blocktext_". form_clean_id($string), 0, 48);
553 }
554
555 /**
556 * Theme the mailing list block.
557 */
558 function theme_constant_contact_block_form($form) {
559 return drupal_render($form);
560 }
561
562 /**
563 * PHP4 compatability functions
564 */
565 if (!function_exists('array_combine')) {
566 function array_combine($arr1, $arr2) {
567 $out = array();
568 foreach ($arr1 as $key1 => $value1) {
569 $out[$value1] = $arr2[$key1];
570 }
571 return $out;
572 }
573 }

  ViewVC Help
Powered by ViewVC 1.1.2