Hi, I have a strange problem. I've looked through the issues but couldn't find anything similar. If this has been answered already, would appreciate it very much if you could point me to the right direction.

I inherited a site with 2 MC lists (not groups). The setup is that one list should show up in certain content areas and another in other content areas. So say, I subscribe to list A, I get subscribed properly. Now, if I go to a page with a subscription block for list B and I subscribe to that one too, what happens is, I get unsubscribed from list A and subscribed to list B (and vice versa). Now, if I'm subscribed to both lists and I unsubscribe from one of them using their specific block, I get unsubscribed from both lists. :o

BUT if I go to the subscription page which lists all my MC newsletters, I can un/subscribe to both with no problems -- one at a time or all at a time.

Afaik, we're using the MC module out of the box and no alterations have been made. I don't mind making changes to the code (to make it work, as we have a deadline for this project) but I was wondering if this is something that has been fixed already or if it is something peculiar to my setup alone.

Any help would be appreciated. Thank you!

Comments

tanjerine’s picture

Hunting down the problem some...

Under the function mailchimp_subscribe_auth_form_submit, there's a line inside a foreach ($lists as $list) loop:

        // unsubscribe a subscribed user who unchecked the box when not registering
        if ($is_subscribed && !$selected && $form['#id'] != 'user-register') {
          $ret = _mailchimp_unsubscribe_user($list, $account->mail, TRUE, $q);
        }

So for list A and B, if you were subscribing to list A using the block created by the module (a block with a single list item), the module loops over the entire array of lists that are available for your particular account and for whichever lists aren't selected (in this case, list B), the account gets unsubscribed from assuming you were subscribed to them in the first place.

So my question (which I'm trying to figure out) is -- if you're on a block for a single list you should be able to subscribe/unsubscribe to that list _alone_ without affecting your subscriptions to other lists. So what should we check for?

Hope this makes sense.

tanjerine’s picture

Okay, here's how I fixed my problem. This could be the wrong way to go about it, though. But it seems to work for me. Here's my updated code for the mailchimp.module

/**
 * Submit handler to add users to lists when editing/creating a user
 */
function mailchimp_subscribe_auth_form_submit($form, &$form_state) {
  if ($q = _mailchimp_get_api_object()) {
    $account = new stdClass();
    if (!empty($form_state['values']['uid'])) {
      $account = user_load(array('uid' => $form_state['values']['uid']));
    }
    else if(isset($form_state['user'])) {
      $account = $form_state['user'];
    }
    else {
      global $user;
      $account = $user;
    }

    $lists = _mailchimp_get_available_lists($account);
    
    foreach ($lists as $list) {
    // ignore required lists, they are handled via hook_user
      if ($list->listtype !== MAILCHIMP_LISTTYPE_REQUIRED) {
        $is_subscribed = _mailchimp_is_subscribed($list->id, $account->mail, $q);
        $ret = TRUE;
        $selected = @$form_state['values']['mailchimp_list_'. $list->id];

       /**
       * [bug: http://drupal.org/node/972856] what if you're on a block for a single list? you should be able to subscribe/unsubscribe to that
        * list _alone_ without affecting your subscriptions to other lists. - Sarah
        *
        * FIX: check the wrapper. Blocks would normally have only 1 wrapper for the single block that it supports. If the list has a wrapper, process it. 
        * Otherwise, forget about it and don't unsubscribe it. In a block/form with more than 1 wrapper, this will still work.
        */
 
        if (!empty($form['wrapper'.$list->id])) { // check the wrapper

          // unsubscribe a subscribed user who unchecked the box when not registering
          if ($is_subscribed && !$selected && $form['#id'] != 'user-register') {
            $ret = _mailchimp_unsubscribe_user($list, $account->mail, TRUE, $q);
          }
          else if ($selected) {
           // subscribe the user if they are not previously subscribed or update existing subscriptions
           $merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list->id, $q->listMergeVars($list->id));

            // include updated email address if already subscribed
            if (!empty($form_state['values']['mail']) && $is_subscribed){
              $merge_vars['EMAIL'] = $form_state['values']['mail'];
            }

            // include interest groups
            if (is_array($form_state['values']['interest_groups_' . $list->id])) {
              foreach($form_state['values']['interest_groups_' . $list->id] as $key => $group) {
                $merge_vars['GROUPINGS'][] = array('id' => $key, 'groups' => is_array($group) ? implode(', ', array_filter($group)) : $group);
              }
            }

            $ret = _mailchimp_subscribe_user($list, $account->mail, $merge_vars, TRUE, $q);
          }

          if (!$ret) {
            watchdog('mailchimp', $q->errorMessage, NULL, WATCHDOG_ERROR);
          }
        }
      }
    }
  }
}
levelos’s picture

Status: Active » Fixed

This was just fixed in http://drupal.org/cvs?commit=452678. Thanks for the help, I ended up taking a different approach which is more robust.

tanjerine’s picture

Thank you so much! :)

Status: Fixed » Closed (fixed)
Issue tags: -subscription

Automatically closed -- issue fixed for 2 weeks with no activity.