Browse Source

! Poll options get mixed up when options are deleted [Bug 4954] + additional error in case of many options (> 255 including the deleted ones)

emanuele 13 years ago
parent
commit
e94ff7a0a1
4 changed files with 25 additions and 9 deletions
  1. 9 1
      Sources/Poll.php
  2. 10 6
      Sources/Post.php
  3. 3 1
      Themes/default/Poll.template.php
  4. 3 1
      Themes/default/Post.template.php

+ 9 - 1
Sources/Poll.php

@@ -451,6 +451,8 @@ function EditPoll()
 			'is_last' => true
 		);
 
+		$context['last_choice_id'] = $last_id;
+
 		if ($context['can_moderate_poll'])
 			$context['poll']['expiration'] = $_POST['poll_expire'];
 
@@ -527,6 +529,7 @@ function EditPoll()
 				'label' => '',
 				'is_last' => true
 			);
+			$context['last_choice_id'] = $last_id;
 		}
 		// New poll?
 		else
@@ -551,6 +554,7 @@ function EditPoll()
 				array('id' => 3, 'number' => 4, 'votes' => -1, 'label' => '', 'is_last' => false),
 				array('id' => 4, 'number' => 5, 'votes' => -1, 'label' => '', 'is_last' => true)
 			);
+			$context['last_choice_id'] = 4;
 		}
 	}
 	$context['page_title'] = $context['is_edit'] ? $txt['poll_edit'] : $txt['add_poll'];
@@ -633,15 +637,19 @@ function EditPoll2()
 		isAllowedTo('poll_add_' . ($user_info['id'] == $bcinfo['id_member_started'] ? 'own' : 'any'));
 
 	$optionCount = 0;
+	$idCount = 0;
 	// Ensure the user is leaving a valid amount of options - there must be at least two.
 	foreach ($_POST['options'] as $k => $option)
 	{
 		if (trim($option) != '')
+		{
 			$optionCount++;
+			$idCount = max($idCount, $k);
+		}
 	}
 	if ($optionCount < 2)
 		$poll_errors[] = 'poll_few';
-	elseif ($optionCount > 256)
+	elseif ($optionCount > 256 || $idCount > 255)
 		$poll_errors[] = 'poll_many';
 
 	// Also - ensure they are not removing the question.

+ 10 - 6
Sources/Post.php

@@ -205,6 +205,7 @@ function Post()
 			array('id' => 3, 'number' => 4, 'label' => '', 'is_last' => false),
 			array('id' => 4, 'number' => 5, 'label' => '', 'is_last' => true)
 		);
+		$context['last_choice_id'] = 4;
 	}
 
 	if ($context['make_event'])
@@ -487,6 +488,14 @@ function Post()
 				);
 			}
 
+			// One empty option for those with js disabled...I know are few... :P
+			$context['choices'][] = array(
+				'id' => $choice_id++,
+				'number' => $choice_id,
+				'label' => '',
+				'is_last' => false
+			);
+
 			if (count($context['choices']) < 2)
 			{
 				$context['choices'][] = array(
@@ -495,13 +504,8 @@ function Post()
 					'label' => '',
 					'is_last' => false
 				);
-				$context['choices'][] = array(
-					'id' => $choice_id++,
-					'number' => $choice_id,
-					'label' => '',
-					'is_last' => false
-				);
 			}
+			$context['last_choice_id'] = $choice_id;
 			$context['choices'][count($context['choices']) - 1]['is_last'] = true;
 		}
 

+ 3 - 1
Themes/default/Poll.template.php

@@ -18,6 +18,7 @@ function template_main()
 	echo '
 		<script type="text/javascript"><!-- // --><![CDATA[
 			var pollOptionNum = 0;
+			var pollOptionId = ', $context['last_choice_id'], ';
 
 			function addPollOption()
 			{
@@ -28,8 +29,9 @@ function template_main()
 							pollOptionNum++;
 				}
 				pollOptionNum++
+				pollOptionId++
 
-				setOuterHTML(document.getElementById("pollMoreOptions"), \'<li><label for="options-\' + pollOptionNum + \'" ', (isset($context['poll_error']['no_question']) ? ' class="error"' : ''), '>', $txt['option'], ' \' + pollOptionNum + \'</label>: <input type="text" name="options[\' + (pollOptionNum - 1) + \']" id="options-\' + (pollOptionNum - 1) + \'" value="" size="80" maxlength="255" class="input_text" /></li><li id="pollMoreOptions"></li\');
+				setOuterHTML(document.getElementById("pollMoreOptions"), \'<li><label for="options-\' + pollOptionId + \'" ', (isset($context['poll_error']['no_question']) ? ' class="error"' : ''), '>', $txt['option'], ' \' + pollOptionNum + \'</label>: <input type="text" name="options[\' + (pollOptionId) + \']" id="options-\' + (pollOptionId) + \'" value="" size="80" maxlength="255" class="input_text" /></li><li id="pollMoreOptions"></li\');
 			}
 		// ]]></script>';
 

+ 3 - 1
Themes/default/Post.template.php

@@ -37,6 +37,7 @@ function template_main()
 	if ($context['make_poll'])
 		echo '
 			var pollOptionNum = 0, pollTabIndex;
+			var pollOptionId = ', $context['last_choice_id'], ';
 			function addPollOption()
 			{
 				if (pollOptionNum == 0)
@@ -49,8 +50,9 @@ function template_main()
 						}
 				}
 				pollOptionNum++
+				pollOptionId++
 
-				setOuterHTML(document.getElementById(\'pollMoreOptions\'), ', JavaScriptEscape('<li><label for="options-'), ' + pollOptionNum + ', JavaScriptEscape('">' . $txt['option'] . ' '), ' + pollOptionNum + ', JavaScriptEscape('</label>: <input type="text" name="options['), ' + pollOptionNum + ', JavaScriptEscape(']" id="options-'), ' + pollOptionNum + ', JavaScriptEscape('" value="" size="80" maxlength="255" tabindex="'), ' + pollTabIndex + ', JavaScriptEscape('" class="input_text" /></li><li id="pollMoreOptions"></li>'), ');
+				setOuterHTML(document.getElementById(\'pollMoreOptions\'), ', JavaScriptEscape('<li><label for="options-'), ' + pollOptionId + ', JavaScriptEscape('">' . $txt['option'] . ' '), ' + pollOptionNum + ', JavaScriptEscape('</label>: <input type="text" name="options['), ' + pollOptionId + ', JavaScriptEscape(']" id="options-'), ' + pollOptionId + ', JavaScriptEscape('" value="" size="80" maxlength="255" tabindex="'), ' + pollTabIndex + ', JavaScriptEscape('" class="input_text" /></li><li id="pollMoreOptions"></li>'), ');
 			}';
 
 	// If we are making a calendar event we want to ensure we show the current days in a month etc... this is done here.