diff -ruN original/mailchimp.module mod/mailchimp.module --- original/mailchimp.module 2011-04-28 01:13:47.000000000 +0700 +++ mod/mailchimp.module 2011-06-18 17:33:38.000000000 +0700 @@ -55,7 +55,7 @@ // include interest groups if (isset($edit['interest_groups_' . $list->id]) && is_array($edit['interest_groups_' . $list->id])) { foreach($edit['interest_groups_' . $list->id] as $key => $group) { - $merge_vars['GROUPINGS'][] = array('id' => $key, 'groups' => is_array($group) ? implode(', ', array_filter($group)) : $group); + $merge_vars['GROUPINGS'][] = array('id' => $key, 'groups' => $q->implodeInterestGroups($group)); } } @@ -296,7 +296,7 @@ // include interest groups if (isset($form_state['values']['interest_groups_' . $list->id]) && 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); + $merge_vars['GROUPINGS'][] = array('id' => $key, 'groups' => $q->implodeInterestGroups($group)); } } @@ -520,28 +520,30 @@ * @param $q - MC API object */ function mailchimp_subscribe_anon_form_submit($form, &$form_state) { - $lists = $form_state['values']['mailchimp_lists']; - foreach ($lists as $list) { - if (!empty($list['EMAIL'])) { - $success = FALSE; + if ($q = _mailchimp_get_api_object()) { + $lists = $form_state['values']['mailchimp_lists']; + foreach ($lists as $list) { + if (!empty($list['EMAIL'])) { + $success = FALSE; - // include interest groups - if (isset($list['interest_groups_' . $list['list']->id]) && is_array($list['interest_groups_' . $list['list']->id])) { - foreach($list['interest_groups_' . $list['list']->id] as $key => $group) { - $list['GROUPINGS'][] = array('id' => $key, 'groups' => is_array($group) ? implode(', ', array_filter($group)) : $group); + // include interest groups + if (isset($list['interest_groups_' . $list['list']->id]) && is_array($list['interest_groups_' . $list['list']->id])) { + foreach($list['interest_groups_' . $list['list']->id] as $key => $group) { + $list['GROUPINGS'][] = array('id' => $key, 'groups' => $q->implodeInterestGroups($group)); + } } - } - $success = _mailchimp_subscribe_user($list['list'], $list['EMAIL'], $list, TRUE); + $success = _mailchimp_subscribe_user($list['list'], $list['EMAIL'], $list, TRUE, $q); - if ($success) { - $msg = t(variable_get('mailchimp_subscription_success_message', 'Thank you, you have been successfully subscribed.')); - } - else { - $msg = t(variable_get('mailchimp_subscription_failure_message', 'We were unable to subscribe you at this time. Please try again later.')); - } + if ($success) { + $msg = t(variable_get('mailchimp_subscription_success_message', 'Thank you, you have been successfully subscribed.')); + } + else { + $msg = t(variable_get('mailchimp_subscription_failure_message', 'We were unable to subscribe you at this time. Please try again later.')); + } - drupal_set_message($msg); + drupal_set_message($msg); + } } } } @@ -666,7 +668,7 @@ if (!empty($groupings)) { foreach($groupings as $grouping) { if ($grouping['id'] == $group['id']) { - $default = explode(",", str_replace(', ', ',', $grouping['groups'])); + $default = $q->explodeInterestGroups($grouping['groups']); } } } diff -ruN original/MCAPI.class.php mod/MCAPI.class.php --- original/MCAPI.class.php 2011-04-28 01:13:47.000000000 +0700 +++ mod/MCAPI.class.php 2011-06-18 17:25:18.000000000 +0700 @@ -59,6 +59,43 @@ } /** + * Escape commas in interest groups and implode if there are multiple + * values. + * + * @param mixed $groups string with single group or array with groups + * @return string + */ + function implodeInterestGroups($groups) { + if (!is_array($groups)) { + $groups = array($groups); + } + else { + $groups = array_filter($groups); + } + foreach ($groups as $key => $group) { + $groups[$key] = str_replace(',', '\,', $group); + } + $groups = implode(',', $groups); + return $groups; + } + + /** + * Parses interest groups into array and un-escapes commas in group names. + * + * @param string $groups + * @return array + */ + function explodeInterestGroups($groups) { + // Split by comma that is not preceded by backslash and followed by + // zero to one space + $groups = preg_split('/(? $group) { + $groups[$key] = str_replace('\,', ',', $group); + } + return $groups; + } + + /** * Unschedule a campaign that is scheduled to be sent in the future * * @section Campaign Related