From 67429cfa94d30feb2282f884f215eb2be7a4934b Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 29 Jul 2013 13:55:42 +0200 Subject: [PATCH] Fixed validation of poll option count --- .../templates/__messageFormPoll.tpl | 2 +- wcfsetup/install/files/js/WCF.Poll.js | 29 ++++++++++++++++++- .../lib/system/poll/PollManager.class.php | 5 ++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/com.woltlab.wcf/templates/__messageFormPoll.tpl b/com.woltlab.wcf/templates/__messageFormPoll.tpl index 8eef1cf9b6..5204f5ce6f 100644 --- a/com.woltlab.wcf/templates/__messageFormPoll.tpl +++ b/com.woltlab.wcf/templates/__messageFormPoll.tpl @@ -8,7 +8,7 @@ 'wcf.poll.button.removeOption': '{lang}wcf.poll.button.removeOption{/lang}' }); - new WCF.Poll.Management('pollOptionContainer', [ {implode from=$pollOptions item=pollOption}{ optionID: {@$pollOption[optionID]}, optionValue: '{$pollOption[optionValue]}' }{/implode} ]); + new WCF.Poll.Management('pollOptionContainer', [ {implode from=$pollOptions item=pollOption}{ optionID: {@$pollOption[optionID]}, optionValue: '{$pollOption[optionValue]}' }{/implode} ], {@POLL_MAX_OPTIONS}); }); //]]> diff --git a/wcfsetup/install/files/js/WCF.Poll.js b/wcfsetup/install/files/js/WCF.Poll.js index f085a054e5..14d2983ef3 100644 --- a/wcfsetup/install/files/js/WCF.Poll.js +++ b/wcfsetup/install/files/js/WCF.Poll.js @@ -20,19 +20,34 @@ WCF.Poll.Management = Class.extend({ */ _container: null, + /** + * number of options + * @var integer + */ + _count: 0, + /** * width for input-elements * @var integer */ _inputSize: 0, + /** + * maximum allowed number of options + * @var integer + */ + _maxOptions: 0, + /** * Initializes the WCF.Poll.Management class. * * @param string containerID * @param array optionList + * @param integer maxOptions */ - init: function(containerID, optionList) { + init: function(containerID, optionList, maxOptions) { + this._count = 0; + this._maxOptions = maxOptions || -1; this._container = $('#' + containerID).children('ol:eq(0)'); if (!this._container.length) { console.debug("[WCF.Poll.Management] Invalid container id given, aborting."); @@ -111,6 +126,11 @@ WCF.Poll.Management = Class.extend({ } WCF.DOMNodeInsertedHandler.execute(); + + this._count++; + if (this._count === this._maxOptions) { + this._container.find('span.jsAddOption').removeClass('pointer').addClass('disabled'); + } }, /** @@ -138,6 +158,10 @@ WCF.Poll.Management = Class.extend({ * @param object event */ _addOption: function(event) { + if (this._count === this._maxOptions) { + return false; + } + var $listItem = $(event.currentTarget).parents('li'); this._createOption(undefined, undefined, $listItem); @@ -151,6 +175,9 @@ WCF.Poll.Management = Class.extend({ _removeOption: function(event) { $(event.currentTarget).parents('li').remove(); + this._count--; + this._container.find('span.jsAddOption').addClass('pointer').removeClass('disabled'); + if (this._container.children('li').length == 0) { this._createOption(); } diff --git a/wcfsetup/install/files/lib/system/poll/PollManager.class.php b/wcfsetup/install/files/lib/system/poll/PollManager.class.php index dae3db0699..93a690a219 100644 --- a/wcfsetup/install/files/lib/system/poll/PollManager.class.php +++ b/wcfsetup/install/files/lib/system/poll/PollManager.class.php @@ -215,6 +215,11 @@ class PollManager extends SingletonFactory { throw new UserInputException('pollOptions'); } + // too many options provided, discard superfluous options + if ($count > POLL_MAX_OPTIONS) { + $this->pollOptions = array_slice($this->pollOptions, 0, POLL_MAX_OPTIONS); + } + // less options available than allowed if ($count < $this->pollData['maxVotes']) { throw new UserInputException('pollMaxVotes', 'notValid'); -- 2.20.1