Fixed multiple user option issues
authorMarcel Werk <burntime@woltlab.com>
Tue, 23 Apr 2013 20:48:15 +0000 (22:48 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 23 Apr 2013 20:48:15 +0000 (22:48 +0200)
com.woltlab.wcf/template/selectOptionType.tpl
wcfsetup/install/files/acp/templates/selectOptionType.tpl
wcfsetup/install/files/lib/acp/form/UserOptionAddForm.class.php
wcfsetup/install/files/lib/acp/form/UserOptionSetDefaultsForm.class.php
wcfsetup/install/files/lib/data/user/option/UserOptionEditor.class.php
wcfsetup/install/files/lib/system/option/MultiSelectOptionType.class.php

index caa4a50af8907ca407036f3cd6404db5500cc686..25332e39d27cc1c31acc1d4b3740a93bf2c15e8b 100644 (file)
@@ -1,5 +1,6 @@
 <select id="{$option->optionName}" name="values[{$option->optionName}]">
-{foreach from=$selectOptions key=key item=selectOption}
-       <option value="{$key}"{if $value == $key} selected="selected"{/if}>{lang}{@$selectOption}{/lang}</option>
-{/foreach}
+       <option value=""></option>
+       {foreach from=$selectOptions key=key item=selectOption}
+               <option value="{$key}"{if $value == $key} selected="selected"{/if}>{lang}{@$selectOption}{/lang}</option>
+       {/foreach}
 </select>
\ No newline at end of file
index 424e96a1723504819042a41e65049f4f92d54005..25332e39d27cc1c31acc1d4b3740a93bf2c15e8b 100644 (file)
@@ -1,4 +1,5 @@
 <select id="{$option->optionName}" name="values[{$option->optionName}]">
+       <option value=""></option>
        {foreach from=$selectOptions key=key item=selectOption}
                <option value="{$key}"{if $value == $key} selected="selected"{/if}>{lang}{@$selectOption}{/lang}</option>
        {/foreach}
index f164b86e3754d8a4f3ab4691b71990db037c7c4a..815084a707f96ded3c2da10a166fa511525b821e 100644 (file)
@@ -246,7 +246,9 @@ class UserOptionAddForm extends AbstractForm {
        
                // reset values
                $this->optionName = $this->optionDescription = $this->categoryName = $this->optionType = $this->defaultValue = $this->validationPattern = $this->optionType = $this->selectOptions = $this->outputClass = '';
-               $this->required = $this->editable = $this->visible = $this->searchable = $this->showOrder = $this->askDuringRegistration = 0;
+               $this->required = $this->searchable = $this->showOrder = $this->askDuringRegistration = 0;
+               $this->editable = 3;
+               $this->visible = 15;
                
                // show success
                WCF::getTPL()->assign('success', true);
index 4ee00b5343130a9d99d5b1575ce7b81f8afab08e..aef6617a2e87207d4ffb00147dbedd6c0bcf053f 100644 (file)
@@ -89,15 +89,31 @@ class UserOptionSetDefaultsForm extends AbstractForm {
                // apply changes
                if ($this->applyChangesToExistingUsers) {
                        $optionIDs = array_keys($saveOptions);
-                       $sql = "UPDATE  wcf".WCF_N."_user_option_value
-                               SET     userOption".implode(' = ?, userOption', $optionIDs)." = ?";
+                       
+                       // get changed options
+                       $sql = "SELECT  optionID, defaultValue
+                               FROM    wcf".WCF_N."_user_option
+                               WHERE   optionID IN (?".str_repeat(', ?', count($optionIDs) - 1).")"; 
                        $statement = WCF::getDB()->prepareStatement($sql);
-                       $statement->execute(array_merge($saveOptions));
+                       $statement->execute($optionIDs);
+                       $optionIDs = $optionValues = array();
+                       while ($row = $statement->fetchArray()) {
+                               if ($row['defaultValue'] != $saveOptions[$row['optionID']]) {
+                                       $optionIDs[] = $row['optionID'];
+                                       $optionValues[] = $saveOptions[$row['optionID']];
+                               }
+                       }
+                       
+                       if (!empty($optionIDs)) {
+                               $sql = "UPDATE  wcf".WCF_N."_user_option_value
+                                       SET     userOption".implode(' = ?, userOption', $optionIDs)." = ?";
+                               $statement = WCF::getDB()->prepareStatement($sql);
+                               $statement->execute(array_merge($optionValues));
+                       }
                }
                
                // reset cache
                UserOptionCacheBuilder::getInstance()->reset();
-               
                $this->saved();
        
                WCF::getTPL()->assign('success', true);
index f1145b9d4a4f329aca132209e6691cdf30290c03..410919ab2aa03781ea2c500ce3caca7d25b16395 100644 (file)
@@ -67,7 +67,20 @@ class UserOptionEditor extends DatabaseObjectEditor implements IEditableCachedOb
                $statement = WCF::getDB()->prepareStatement($sql);
                $statement->execute(array($this->optionID));
                
-               $sql = WCF::getDB()->getEditor()->dropColumn('wcf'.WCF_N.'_user_option_value', 'userOption'.$this->optionID);
+               WCF::getDB()->getEditor()->dropColumn('wcf'.WCF_N.'_user_option_value', 'userOption'.$this->optionID);
+       }
+       
+       /**
+        * @see wcf\data\IEditableObject::deleteAll()
+        */
+       public static function deleteAll(array $objectIDs = array()) {
+               $returnValue = parent::deleteAll($objectIDs);
+               
+               foreach ($objectIDs as $objectID) {
+                       WCF::getDB()->getEditor()->dropColumn('wcf'.WCF_N.'_user_option_value', 'userOption'.$objectID);
+               }
+               
+               return $returnValue;
        }
        
        /**
index 81d57613acb363f19651366982c5b6cbdebbc8ab..10a1e61ffb4a79df5ea4191076f08f2272a307e0 100644 (file)
@@ -24,7 +24,7 @@ class MultiSelectOptionType extends SelectOptionType {
                WCF::getTPL()->assign(array(
                        'option' => $option,
                        'selectOptions' => $option->parseSelectOptions(),
-                       'value' => explode("\n", $value)
+                       'value' => (!is_array($value) ? explode("\n", $value) : $value)
                ));
                return WCF::getTPL()->fetch('multiSelectOptionType');
        }
@@ -58,7 +58,7 @@ class MultiSelectOptionType extends SelectOptionType {
                $value = ArrayUtil::trim($value);
                if (empty($value)) return false;
                
-               $conditions->add("option_value.userOption".$option->optionID." = ?", array(implode("\n", $value)));
+               $conditions->add("option_value.userOption".$option->optionID." REGEXP '".'(^|\n)'.implode('\n([^\n]*\n)*', array_map('escapeString', $value)).'($|\n)'."'");
                return true;
        }
 }