--- original/mailbuild/mailbuild.module	2008-05-16 08:18:58.000000000 +1000
+++ new/mailbuild/mailbuild.module	2008-09-04 16:56:11.000000000 +1000
@@ -68,11 +68,26 @@ function mailbuild_menu($may_cache) {
 /**
  * Implementation of hook_block()
  */
-function mailbuild_block($op = 'list', $delta = 0, $edit = array()) {
+function mailbuild_block($op = 'list', $delta = 0, $edit = array()) {
+  if ($op == 'list') {
+    return mailbuild_block_list();
+  }
+  if ($delta == 'mblist') {
+    switch ($op) {
+      case 'configure':
+        return mailbuild_mblist_block_settings('form', $edit);
+      case 'save':
+        mailbuild_mblist_block_settings('save', $edit);
+        break;
+      default:
+      case 'view':
+        return array(
+          'subject' => t('Mailbuild list'),
+          'content' => drupal_get_form('mailbuild_block_view_form', 'mblist'),
+        );
+    }
+  }
   switch ($op) {
-    case 'list':
-      return mailbuild_block_list();
-      
     case 'configure':
     case 'save':
       // no additional configuration options
@@ -99,7 +114,8 @@ function mailbuild_block($op = 'list', $
  * A block is returned for each Mailbuild list defined in admin/settings/mailbuild
  */
 function mailbuild_block_list() {
-  $lists = variable_get('mailbuild_lists', array());
+  $lists = variable_get('mailbuild_lists', array());
+  $blocks = array('mblist' => array('info' => t('Mailbuild list')));
   foreach ($lists as $list_id => $list) {
     $blocks[$list_id]['info'] = check_plain($list['name']);
   }
@@ -137,7 +153,68 @@ function mailbuild_block_view_form($list
   $form['subscribe'] = array(
     '#type' => 'submit',
     '#value' => 'Subscribe',
-  );
+  );
+
+  if ($list_id == 'mblist') {
+    $options = array();
+    // List each list ...
+    foreach (variable_get('mailbuild_lists', array()) as $id => $list) {
+      $options[$id] = $list['name'];
+    }
+    $fields = variable_get('mailbuild_lists_fields', array());
+    
+    // Each list becomes the mblists
+    if (!empty($options)) {
+      $form['mblists'] = array(
+        '#tree' => TRUE,
+        '#type' => 'item',
+        '#title' => variable_get('mailbuild_lists_title', 'Interests'),
+        '#prefix' => '<div id="edit-mblists">',
+        '#suffix' => '</div>',
+      );
+      foreach ($options as $id => $name) {
+        $form['mblists'][$id]['radio'] = array(
+          '#type' => 'radio',
+          '#title' => $name,
+          '#return_value' => $id,
+          // Set #parents so we get $form_values['mblists'][$id] in post
+          '#parents' => array('mb_mblists'),
+        );
+        $ops = array();
+        foreach (array_map('trim', explode("\n", $fields[$id]['fields'])) as $value) {
+          list($key, $val) = explode('|', $value, 2);
+          $ops[$key] = $val;
+        }
+        $form['mblists'][$id]['fields'] = array(
+        	'#type' => $fields[$id]['type'],
+          '#options' => array_filter($ops),
+          // Set #parents so we get $form_values['fields'][$id] in post
+          '#parents' => array('mb_fields', $id),
+          '#prefix' => '<div class="form-field">',
+          '#suffix' => '</div>',
+        );
+        $form['mblists'][$id]['custom'] = array(
+          '#type' => 'value',
+          '#value' => $fields[$id]['custom'],
+          // Set #parents so we get $form_values['custom'][$id] in post
+          '#parents' => array('mb_custom', $id),
+        );
+        $form[$fields[$id]['custom']] = array('#type' => 'value');
+      }
+      
+      // Select first as default mblists
+      $default = (array_shift(array_keys($options)));
+      $form['mblists'][$default]['radio']['#default_value'] = $default;
+      
+      $form['subscribe']['#weight'] = 1;
+      
+      // Add custom fields item
+      $form['custom_fields'] = array('#type' => 'value');
+      
+      drupal_add_js(drupal_get_path('module', 'mailbuild') .'/mailbuild.js');
+    }
+  }
+
   return $form;
 }
 
@@ -150,7 +227,22 @@ function mailbuild_block_view_form_valid
   }
   if (!strlen($form_values['name'])) {
     form_set_error('name', 'Please specify a name');
-  }
+  }
+  
+  if ($form['list_id'] == 'mblist') {
+    if (!empty($form_values['mb_mblists'])) {
+      // Here is the magic
+      $id = $form_values['mb_mblists'];
+      form_set_value($form['list_id'], $id);
+  	  form_set_value($form[$form_values['mb_custom'][$id]], $form_values['mb_fields'][$id]);
+      form_set_value($form['custom_fields'], $form_values['mb_custom'][$id]);
+    }
+    else {
+      // If they somehow deselect throw error
+      form_set_error('mblists', t('Please choose an option.'));
+    }
+  }
+  
 }
 
 /**
@@ -164,8 +256,29 @@ function mailbuild_block_view_form_submi
     'Email' => $form_values['email'],
     'Name' => $form_values['name'],
   );
-  $result = mailbuild_soap_call('AddSubscriber', $params);
-  if ($result['Subscriber.AddResult']['Code'] === '0') {
+  $type = 'AddSubscriber';
+  $return = 'Subscriber.AddResult';
+  if (!empty($form_values['custom_fields'])) {
+    $custom = explode(',', $form_values['custom_fields']);
+    $array = array();
+    foreach ($custom as $key) {
+      if (is_array($form_values[$key])) {
+        foreach (array_filter($form_values[$key]) as $value) {
+          $array[] = array('Key' => $key, 'Value' => $value);
+        }
+      }
+      elseif (!empty($form_values[$key])) {
+        $array[] = array('Key' => $key, 'Value' => $form_values[$key]);
+      }
+    }
+    if (!empty($array)) {
+      $type = 'AddSubscriberWithCustomFields';
+      $return = 'Subscriber.AddWithCustomFieldsResult';
+      $params['CustomFields']['SubscriberCustomField'] = $array;
+    }
+  }
+  $result = mailbuild_soap_call($type, $params);
+  if ($result[$return]['Code'] === '0') {
     $msg = 'Thank you for subscribing to our list.';
     if ($list['optin'] == 'double') {
       $msg .= ' A confirmation link has been sent to your email address.';
@@ -173,7 +286,7 @@ function mailbuild_block_view_form_submi
     drupal_set_message($msg);
   }
   else {
-    $error = $result['Subscriber.AddResult']['Message'];
+    $error = $result[$return]['Message'];
     if (!strlen($error)) {
       $error = 'NuSOAP library error';
     }
@@ -210,6 +323,50 @@ function mailbuild_get_list($id) {
  */
 function mailbuild_nusoap_path() {
   return variable_get('mailbuild_nusoap_path', drupal_get_path('module', 'mailbuild') .'/nusoap/lib/nusoap.php');
+}
+
+function mailbuild_mblist_block_settings($op, $edit) {
+  if ($op == 'form') {
+    $lists = variable_get('mailbuild_lists', array());
+    $fields = variable_get('mailbuild_lists_fields', array());
+    $form['fields_title'] = array(
+	  '#title' => t('Choice title'),
+      '#type' => 'textfield',
+      '#default_value' => variable_get('mailbuild_lists_title', 'Interests'),
+    );
+    $form['lists']['#tree'] = TRUE;
+    foreach ($lists as $id => $list) {
+      $form['lists'][$id] = array(
+        '#type' => 'fieldset',
+        '#title' => $list['name'],
+        '#collapsible' => TRUE,
+        '#collapsed' => TRUE,
+      );
+      $form['lists'][$id]['fields'] = array(
+        '#type' => 'textarea',
+        '#title' => t('Custom field values'),
+        '#default_value' => $fields[$id]['fields'],
+        '#description' => t('Enter values seperated by newlines in format $key|$value'),
+      );
+      $form['lists'][$id]['type'] = array(
+        '#type' => 'radios',
+        '#title' => t('Type'),
+        '#options' => array('radios' => t('Radios'), 'checkboxes' => t('Checkboxes'), 'select' => t('Select list')),
+        '#default_value' => isset($fields[$id]['type'])? $fields[$id]['type'] : 'radios',
+      );
+      $form['lists'][$id]['custom'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Custom field key'),
+        '#default_value' => isset($fields[$id]['custom'])? $fields[$id]['custom'] : 'Custom',
+        '#description' => t('As per corresponding field key in mailbuild system (First entry under "Personalization Tag")'),
+      );
+    }
+    return $form;
+  }
+  elseif ($op == 'save') {
+    variable_set('mailbuild_lists_fields', (array) $edit['lists']);
+    variable_set('mailbuild_lists_title', $edit['fields_title']);
+  }
 }
 
 /**
@@ -307,12 +464,12 @@ function mailbuild_admin_settings_form()
       '#description' => t("Single opt-in means new subscribers are added to this list as soon as they complete the subscribe form. Double opt-in means a verification email with a confirmation link will be sent to the subscriber that they must click to validate their address before they're added to this list. Select the opt-in type that matches your list settings on Mailbuild."),
       '#options' => mailbuild_optin_options(),
     );
-  }
-
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => 'Save',
-  );
+  }
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Save',
+  );
   return $form;
 }
 
