Add optgroup support for AbstractSelectCondition
authorMatthias Schmidt <gravatronics@live.com>
Fri, 4 Jul 2014 20:13:00 +0000 (22:13 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 4 Jul 2014 20:13:00 +0000 (22:13 +0200)
wcfsetup/install/files/lib/system/condition/AbstractMultiSelectCondition.class.php
wcfsetup/install/files/lib/system/condition/AbstractSelectCondition.class.php
wcfsetup/install/files/lib/system/condition/MultiPageControllerCondition.class.php

index f5752c7c2d89e96e9d8f7074028c4c50bb985244..f01a2add7eeff47eea3265078468f08a8400a6da 100644 (file)
@@ -39,15 +39,27 @@ abstract class AbstractMultiSelectCondition extends AbstractSelectCondition {
        protected function getFieldElement() {
                $options = $this->getOptions();
                
-               $fieldElement = '<select name="'.$this->fieldName.'[]" id="'.$this->fieldName.'" multiple="multiple" size="'.(count($options) > 10 ? 10 : count($options)).'">';
-               foreach ($options as $value => $label) {
-                       $fieldElement .= '<option value="'.$value.'"'.(in_array($value, $this->fieldValue) ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
+               $fieldElement = '<select name="'.$this->fieldName.'[]" id="'.$this->fieldName.'" multiple="multiple" size="'.(count($options, COUNT_RECURSIVE) > 10 ? 10 : count($options)).'">';
+               foreach ($options as $key => $value) {
+                       if (is_array($value)) {
+                               $fieldElement .= $this->getOptGroupCode($key, $value);
+                       }
+                       else {
+                               $fieldElement .= $this->getOptionCode($key, $value);
+                       }
                }
                $fieldElement .= "</select>";
                
                return $fieldElement;
        }
        
+       /**
+        * @see \wcf\system\condition\AbstractSelectCondition::getOptionCode()
+        */
+       protected function getOptionCode($value, $label) {
+               return '<option value="'.$value.'"'.(in_array($value, $this->fieldValue) ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
+       }
+       
        /**
         * @see \wcf\system\condition\ICondition::readFormParameters()
         */
@@ -69,6 +81,12 @@ abstract class AbstractMultiSelectCondition extends AbstractSelectCondition {
                $options = $this->getOptions();
                foreach ($this->fieldValue as $value) {
                        if (!isset($options[$value])) {
+                               foreach ($options as $optionValue) {
+                                       if (is_array($optionValue) && isset($optionValue[$value])) {
+                                               return;
+                                       }
+                               }
+                               
                                $this->errorMessage = 'wcf.global.form.error.noValidSelection';
                                
                                throw new UserInputException($this->fieldName, 'noValidSelection');
index e236f1b300ccd42c4bf3efd5a0efb5560454de61..320703c04cd5bdd551a738b0014366f120e35dff 100644 (file)
@@ -53,14 +53,47 @@ abstract class AbstractSelectCondition extends AbstractSingleFieldCondition {
                $options = $this->getOptions();
                
                $fieldElement = '<select name="'.$this->fieldName.'">';
-               foreach ($options as $value => $label) {
-                       $fieldElement .= '<option value="'.$value.'"'.($this->fieldValue == $value ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
+               foreach ($options as $key => $value) {
+                       if (is_array($value)) {
+                               $fieldElement .= $this->getOptGroupCode($key, $value);
+                       }
+                       else {
+                               $fieldElement .= $this->getOptionCode($key, $value);
+                       }
                }
                $fieldElement .= "</select>";
                
                return $fieldElement;
        }
        
+       /**
+        * Returns the html code for an opt group.
+        * 
+        * @param       string                  $label
+        * @param       array<string>           $options
+        * @return      string
+        */
+       protected function getOptGroupCode($label, array $options) {
+               $html = '<optgroup label="'.$label.'">';
+               foreach ($options as $key => $value) {
+                       $html .= $this->getOptionCode($key, $value);
+               }
+               $html .= '</optgroup>';
+               
+               return $html;
+       }
+       
+       /**
+        * Returns the html code for an option.
+        * 
+        * @param       string          $value
+        * @param       string          $label
+        * @return      string
+        */
+       protected function getOptionCode($value, $label) {
+               return '<option value="'.$value.'"'.($this->fieldValue == $value ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
+       }
+       
        /**
         * Returns the selectable options.
         */
@@ -95,6 +128,12 @@ abstract class AbstractSelectCondition extends AbstractSingleFieldCondition {
                        $options = $this->getOptions();
                        
                        if (!isset($options[$this->fieldValue])) {
+                               foreach ($options as $key => $value) {
+                                       if (is_array($value) && isset($value[$this->fieldValue])) {
+                                               return;
+                                       }
+                               }
+                               
                                $this->errorMessage = 'wcf.global.form.error.noValidSelection';
                                
                                throw new UserInputException($this->fieldName, 'noValidSelection');
index cd83919574ba087805f78d6e43e4217088b3db09..40cb07f707253b7740b1fc6610586b239215f40e 100644 (file)
@@ -33,18 +33,10 @@ class MultiPageControllerCondition extends AbstractMultiSelectCondition implemen
        protected $label = 'wcf.page.requestedPage';
        
        /**
-        * @see \wcf\system\condition\AbstractSingleFieldCondition::getFieldElement()
+        * @see \wcf\system\condition\AbstractSelectCondition::getOptionCode()
         */
-       protected function getFieldElement() {
-               $options = $this->getOptions();
-               
-               $fieldElement = '<select name="'.$this->fieldName.'[]" id="'.$this->fieldName.'" multiple="multiple" size="'.(count($options) > 10 ? 10 : count($options)).'">';
-               foreach ($options as $value => $label) {
-                       $fieldElement .= '<option value="'.$value.'" data-object-type="'.ObjectTypeCache::getInstance()->getObjectType($value)->objectType.'"'.(in_array($value, $this->fieldValue) ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
-               }
-               $fieldElement .= "</select>";
-               
-               return $fieldElement;
+       protected function getOptionCode($value, $label) {
+               return '<option value="'.$value.'" data-object-type="'.ObjectTypeCache::getInstance()->getObjectType($value)->objectType.'"'.(in_array($value, $this->fieldValue) ? ' selected="selected"' : '').'>'.WCF::getLanguage()->get($label).'</option>';
        }
        
        /**