Index: advpoll.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advpoll/advpoll.module,v
retrieving revision 1.21.2.71
diff -u -p -r1.21.2.71 advpoll.module
--- advpoll.module	6 Oct 2007 15:28:12 -0000	1.21.2.71
+++ advpoll.module	7 Oct 2007 17:08:00 -0000
@@ -264,16 +264,6 @@ function advpoll_form($node, $form_value
   
   $form['body_filter']['format'] = filter_form($node->format);
 
-  if (isset($form_values)) {
-    $choices = $form_values['choices'];
-    if ($form_values['more_choices']) {
-      $choices *= 2;
-    }
-  }
-  else {
-    $choices = max(2, count($node->choice) ? count($node->choice) : ADVPOLL_INITIAL_CHOICES);
-  }
-
   if (variable_get('advpoll_use_question_'. $type->type, ADVPOLL_USE_QUESTION) || (isset($node->question) && $node->question !== '')) {
     $form['question'] = array(
       '#type' => 'textfield',
@@ -283,67 +273,34 @@ function advpoll_form($node, $form_value
     );
   }
   
-  $form['choices'] = array(
-    '#type' => 'hidden',
-    '#value' => $choices,
-  );
-
   // Advanced Poll choices
-  $form['choice'] = array(
+  $form['choices_fieldset'] = array(
     '#type' => 'fieldset',
     '#title' => t('Poll choices'),
     '#collapsible' => TRUE,
     '#prefix' => '<div class="poll-form">',
     '#suffix' => '</div>',
-    '#tree' => TRUE,
     '#weight' => 1,
   );
   
   if ($editing) {
-    $form['choice']['choice_note'] = array(
+    $form['choices_fieldset']['choice_note'] = array(
       '#value' => '<div id="edit-settings-choice-note" class="description">'. t('Note: adding or removing choices after voting has begun is not recommended.') .'</div>',
     );
   }
-  
-  $form['choice']['more_choices'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Need more choices'),
-    '#value' => 0,
-    '#parents' => array('more_choices'), // Don't pollute $form['choice']
-    '#prefix' => '<div id="more-choices">',
-    '#suffix' => '</div>',
-    '#description' => t("If the number of choices above isn't enough, check this box and click the Preview button below to double the number of choices."),
-    '#weight' => 1,
-  );
 
-  // First, loop through any currently existing choices.
-  $current_choices = 0;
+  $choices = '';
   if (isset($node->choice)) {
-    foreach ($node->choice as $index => $choice) {
-      $form['choice'][$index]['label'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Choice %n', array('%n' => $current_choices + 1)) . ($choice['writein'] ? ' '. t('(write-in)') : ''),
-        '#default_value' => $choice['label'],
-        '#attributes' => array('class' => 'choices'),
-      );
-      $current_choices++;
-      $next_index = $index + 1;
-    }
-  }
-  else {
-    $next_index = 1;
-  }
-
-  // Now add on extra choices if we need to.
-  if ($current_choices < $choices) {
-    for ($index = $next_index; $current_choices < $choices; $index++, $current_choices++) {
-      $form['choice'][$index]['label'] = array(
-        '#type' => 'textfield',
-        '#title' => t('Choice %n', array('%n' => $current_choices + 1)),
-        '#attributes' => array('class' => 'choices'),
-      );
+    foreach ($node->choice as $choice) {
+      $choices .= $choice['label'] . "\n";
     }
   }
+  
+  $form['choices_fieldset']['choices']= array(
+    '#type' => 'textarea',
+    '#default_value' => $choices,
+    '#description' => t('Add one choice per row.'),
+  );
 
   $form['settings'] = array(
     '#type' => 'fieldset',
@@ -353,19 +310,11 @@ function advpoll_form($node, $form_value
     '#weight' => 2,
     '#tree' => TRUE,
   );
-
-  $max_choice_list = array();
-  for ($i = 0; $i <= $choices; $i++) {
-    $max_choice_list[$i] = ($i == 0 ? t('No limit') : $i);
-  }
-
   $form['settings']['max_choices'] = array(
-    '#type' => 'select',
+    '#type' => 'textfield',
     '#title' => t('Maximum choices'),
     '#default_value' => ($node->max_choices ? $node->max_choices : variable_get('advpoll_max_choices_'. $type->type, ADVPOLL_MAX_CHOICES)),
-    '#options' => $max_choice_list,
-    '#DANGEROUS_SKIP_CHECK' => TRUE, // Allow jQuery to add new options
-    '#description' => t('Limits the total number of choices voters may select.')
+    '#description' => t('Limits the total number of choices voters may select. Set to 0 for no limit.')
   );
 
   $voting_algorithms = advpoll_algorithms($mode);
@@ -469,8 +418,6 @@ function advpoll_form($node, $form_value
     }
   }
   
-  $form['#multistep'] = TRUE;
-
   return $form;
 }
 
@@ -605,32 +552,25 @@ function advpoll_validate(&$node) {
     drupal_goto('node/'. $node->nid .'/reset');
   }
   else {
-    // Use form_set_error for any errors.
-    $node->choice = array_values($node->choice);
-
-    // Start keys at 1 rather than 0.
-    array_unshift($node->choice, '');
-    unset($node->choice[0]);
-
-    // Check for at least two choices.
-    $real_choices = 0;
-    foreach ($node->choice as $i => $choice) {
-      if ($choice['label'] != '') {
-        $real_choices++;
+    $choices = explode("\n", $node->choices);
+    // Remove empty choices.
+    foreach ($choices as $key => $label) {
+      if (trim($label) == '') {
+        unset($choices[$key]);
       }
     }
 
-    if ($real_choices < 2) {
-      form_set_error("choice][$real_choices][label", t('You must fill in at least two choices.'));
+    if (count($choices) < 2) {
+      form_set_error('choices', t('You must fill in at least two choices.'));
     }
 
-    // Validate max choices since it has #DANGEROUS_SKIP_CHECK set to true.
-    if ($node->settings['max_choices'] < 0) {
-      form_set_error('settings][max_choices]', t('Maximum choices must be a non-negative integer.'));
+    // Validate max choices.
+    if (!is_numeric($node->settings['max_choices']) || $node->settings['max_choices'] < 0) {
+      form_set_error('settings][max_choices', t('Maximum choices must be a non-negative integer.'));
     }
 
-    if ($node->settings['max_choices'] > count($node->choice)) {
-      form_set_error('settings][max_choices]', t('Maximum choices cannot be larger than the number of choices submitted.'));
+    if ($node->settings['max_choices'] > count($choices)) {
+      form_set_error('settings][max_choices', t('Maximum choices cannot be larger than the number of choices submitted.'));
     }
     
     // Validate dates.
@@ -656,7 +596,7 @@ function advpoll_insert($node) {
   db_query("INSERT INTO {advpoll} (nid, mode, use_list, active, max_choices, algorithm, show_votes, start_date, end_date, writeins, show_writeins, question) VALUES (%d, '%s', %d, %d, %d, '%s', %d, '%s', '%s', %d, %d, '%s')", $node->nid, $mode, $node->settings['use_list'], !$node->settings['close'], $node->settings['max_choices'], $node->settings['algorithm'], $node->settings['show_votes'], $node->settings['start_date'] ? strtotime($node->settings['start_date']) : 0, $node->settings['end_date'] ? strtotime($node->settings['end_date']) : 0, $node->settings['writeins'], $node->settings['show_writeins'], $node->question);
 
   // Insert the choices.
-  _advpoll_insert_choices($node->nid);
+  _advpoll_insert_choices($node->nid, $node->choices);
 }
 
 /**
@@ -667,8 +607,7 @@ function advpoll_insert($node) {
 function advpoll_update($node) {
 
   db_query("UPDATE {advpoll} SET active = %d, max_choices = %d, algorithm = '%s', use_list = %d, show_votes = %d, start_date = '%s', end_date = '%s', writeins = %d, show_writeins = %d, question = '%s' WHERE nid = %d", !$node->settings['close'], $node->settings['max_choices'], $node->settings['algorithm'], $node->settings['use_list'], $node->settings['show_votes'], $node->settings['start_date'] ? strtotime($node->settings['start_date']) : 0, $node->settings['end_date'] ? strtotime($node->settings['end_date']) : 0, $node->settings['writeins'], $node->settings['show_writeins'], $node->question, $node->nid);
-
-  _advpoll_insert_choices($node->nid);
+  _advpoll_insert_choices($node->nid, $node->choices);
   votingapi_recalculate_results('advpoll', $node->nid);
 }
 
@@ -1081,12 +1020,16 @@ function _advpoll_is_active($node, $retu
   return $return_status ? $status : $active; 
 }
 
-function _advpoll_insert_choices($nid) {
-  $node = node_load($nid);
+function _advpoll_insert_choices($nid, $choices) {
   db_query('DELETE FROM {advpoll_choices} WHERE nid = %d', $nid);
-  foreach ($_POST['choice'] as $index => $choice) {
-    if ($choice['label'] != '') {
-      db_query("INSERT INTO {advpoll_choices} (nid, label, vote_offset, writein) VALUES (%d, '%s', %d, %d)", $nid, $choice['label'], $index, isset($node->choice[$index]) ? $node->choice[$index]['writein'] : 0);
+  $choices = explode("\n", $choices);
+  $vote_offset = 1;
+  // Remove empty choices.
+  foreach ($choices as $label) {
+    if (trim($label) !== '') {
+      //TODO: handle write-ins
+      db_query("INSERT INTO {advpoll_choices} (nid, label, vote_offset, writein) VALUES (%d, '%s', %d, %d)", $nid, $label, $vote_offset, isset($node->choice[$index]) ? $node->choice[$index]['writein'] : 0);
+      $vote_offset++;
     }
   }
 }
