Fixed validation of poll option count
authorAlexander Ebert <ebert@woltlab.com>
Mon, 29 Jul 2013 11:55:42 +0000 (13:55 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 29 Jul 2013 11:55:42 +0000 (13:55 +0200)
com.woltlab.wcf/templates/__messageFormPoll.tpl
wcfsetup/install/files/js/WCF.Poll.js
wcfsetup/install/files/lib/system/poll/PollManager.class.php

index 8eef1cf9b66b55c264ced8134d349c5e0f50b0a7..5204f5ce6f48e795dbe05877c0b6fdae2365529e 100644 (file)
@@ -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});
                });
                //]]>
        </script>
index f085a054e5d118c4213d407b4667e2c4cb3242c6..14d2983ef3aee4d098f253d1046c6a53ecc46012 100644 (file)
@@ -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<object>   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();
                }
index dae3db06996008e1841255cc177c935af321a0aa..93a690a219b3b7890f50333c9db1ab7db50e9a03 100644 (file)
@@ -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');