*** mailchimp.module	2009-10-09 16:44:02.000000000 +0100
--- mailchimp-new.module	2009-10-09 16:43:44.000000000 +0100
***************
*** 535,599 ****
   */
  function mailchimp_subscribe_form() {
    global $user;
  
    if ($q = _mailchimp_get_api_object()) {
      $lists =_mailchimp_get_available_lists($user);
      foreach ($lists as $list) {
!       $form['list_'. $list->id] = array(
!           '#type'   => 'fieldset',
!           '#title'  => $list->name,
!           '#collapsible' => TRUE,
!           '#tree'   => TRUE,
!           '#attributes'   => array('class' => 'mailchimp-newsletter mailchimp-newsletter-'. $list->id)
!       );
! 
!       $form['list_'. $list->id]['list'] = array(
!           '#type'   => 'value',
!           '#value'  => $list
!       );
! 
!       $checked = ($list->listtype == "optout") ? 1 : 0;
!       $form['list_'. $list->id][$list->id] = array(
!           '#type'           => 'checkbox',
!           '#title'          => t('Subscribe to the @newsletter newsletter', array('@newsletter' => $list->name)),
!           '#default_value'  => $checked,
!           '#description'    => $list->description,
!       );
! 
!       foreach ((array)$q->listMergeVars($list->id) as $mergevar) {
!         $form['list_'. $list->id][$mergevar['tag']] = _mailchimp_insert_drupal_form_tag($mergevar);
!       }
! 
!       // present interest groups
!       if ($intgroup = $q->listInterestGroups($list->id)) {
!         switch ($intgroup['form_field']) {
!           case 'radio':
!             $field_type = 'radios';
!             break;
!           case 'checkbox':
!             $field_type = 'checkboxes';
!             break;
!           default:
!             $field_type = $intgroup['form_field'];
!         }
!         foreach ((array)$intgroup['groups'] as $group) {
!           $options[$group] = $group;
!         }
!         $form['list_'. $list->id]['INTERESTS'] = array(
!             '#type'           => $field_type,
!             '#title'          => $intgroup['name'],
!             '#options'        => $options
          );
        }
      }
! 
!     $form['submit'] = array(
!         '#type' => 'submit',
!         '#value' => t('Sign Up!'),
!     );
!   }
!   return $form;
  }
  
  /**
   * Convert mailchimp form elements to Drupal Form API
--- 535,617 ----
   */
  function mailchimp_subscribe_form() {
    global $user;
+   $args = func_get_args();
+   $list_id = $args[1];
  
    if ($q = _mailchimp_get_api_object()) {
      $lists =_mailchimp_get_available_lists($user);
+     $list_exists = false; // check if at least one list exists
      foreach ($lists as $list) {
!     	if(empty($list_id) || $list->id == $list_id) {
! 	      $form['list_'. $list->id] = array(
! 	          '#type'   => 'fieldset',
! 	          '#title'  => $list->name,
! 	          '#collapsible' => TRUE,
! 	          '#tree'   => TRUE,
! 	          '#attributes'   => array('class' => 'mailchimp-newsletter mailchimp-newsletter-'. $list->id)
! 	      );
! 
! 	      $form['list_'. $list->id]['list'] = array(
! 	          '#type'   => 'value',
! 	          '#value'  => $list
! 	      );
! 	
! 	      $checked = ($list->listtype == "optout") ? 1 : 0;
! 	      $form['list_'. $list->id][$list->id] = array(
! 	          '#type'           => 'checkbox',
! 	          '#title'          => t('Subscribe to the @newsletter newsletter', array('@newsletter' => $list->name)),
! 	          '#default_value'  => $checked,
! 	          '#description'    => $list->description,
! 	      );
! 	
! 	      foreach ((array)$q->listMergeVars($list->id) as $mergevar) {
! 	        $form['list_'. $list->id][$mergevar['tag']] = _mailchimp_insert_drupal_form_tag($mergevar);
! 	      }
! 	
! 	      // present interest groups
! 	      if ($intgroup = $q->listInterestGroups($list->id)) {
! 	        switch ($intgroup['form_field']) {
! 	          case 'radio':
! 	            $field_type = 'radios';
! 	            break;
! 	          case 'checkbox':
! 	            $field_type = 'checkboxes';
! 	            break;
! 	          default:
! 	            $field_type = $intgroup['form_field'];
! 	        }
! 	        foreach ((array)$intgroup['groups'] as $group) {
! 	          $options[$group] = $group;
! 	        }
! 	        $form['list_'. $list->id]['INTERESTS'] = array(
! 	            '#type'           => $field_type,
! 	            '#title'          => $intgroup['name'],
! 	            '#options'        => $options
! 	        );
! 	      }
!     	}
! 	    if($list->id == $list_id) {
! 	      $form['submit'] = array(
!             '#type' => 'submit',
!             '#value' => t('Sign Up!'),
!           );
! 	      break;
! 	    }
! 	    else {
! 	      $list_exists = true;
! 	    }
!       }
!       if($list_exists) {
!         $form['submit'] = array(
!           '#type' => 'submit',
!           '#value' => t('Sign Up!'),
          );
        }
+         
      }
!     return $form;
  }
+   
  
  /**
   * Convert mailchimp form elements to Drupal Form API
***************
*** 989,1002 ****
  
  /**
   * implementation of hook_block
-  * @todo: provide a block for each individual list
   */
  function mailchimp_block($op='list', $delta=0) {
    if ($op == "list") {
!     $block[0]["info"] = t("Mailchimp Subscription Form");
    }
    else if ($op == 'view') {
!     $block['content'] = drupal_get_form('mailchimp_subscribe_form');
    }
    return $block;
  }
\ No newline at end of file
--- 1007,1030 ----
  
  /**
   * implementation of hook_block
   */
  function mailchimp_block($op='list', $delta=0) {
    if ($op == "list") {
!     global $user;
! 
!     if ($q = _mailchimp_get_api_object()) {
!       $lists = variable_get('mailchimp_lists', array());
!       if (!empty($lists)) {
!         $lists = unserialize($lists);
!       }
!       foreach ($lists as $list) {
!       	$block[$list->id]["info"] = t('Mailchimp Subscription Form: ' . $list->name);
! 		$count++;
!       }
!     }
    }
    else if ($op == 'view') {
!     $block['content'] = drupal_get_form('mailchimp_subscribe_form', $delta);
    }
    return $block;
  }
\ No newline at end of file
