Support non-selectable options in nested selection form fields
authorMatthias Schmidt <gravatronics@live.com>
Sat, 6 Jul 2019 16:19:05 +0000 (18:19 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 6 Jul 2019 16:19:05 +0000 (18:19 +0200)
Close  #2973

com.woltlab.wcf/templates/__multipleSelectionFormField.tpl
com.woltlab.wcf/templates/__singleSelectionFormField.tpl
wcfsetup/install/files/acp/templates/__multipleSelectionFormField.tpl
wcfsetup/install/files/acp/templates/__singleSelectionFormField.tpl
wcfsetup/install/files/lib/system/form/builder/field/TSelectionFormField.class.php

index e9bf9fef66868f660606eba44083dd77e1743906..f6afdf6be3873b63e3cace60ed15adeab4716c85 100644 (file)
@@ -24,8 +24,8 @@
                                                *}type="checkbox" {*
                                                *}name="{@$field->getPrefixedId()}[]" {*
                                                *}value="{$__fieldNestedOption[value]}"{*
-                                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue()} checked{/if}{*
-                                               *}{if $field->isImmutable()} disabled{/if}{*
+                                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue() && $__fieldNestedOption[isSelectable]} checked{/if}{*
+                                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                                        *}> {@$__fieldNestedOption[label]}</label>
                        </li>
                {/foreach}
@@ -36,8 +36,8 @@
                        <input type="checkbox" {*
                                *}name="{@$field->getPrefixedId()}[]" {*
                                *}value="{$__fieldNestedOption[value]}"{*
-                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue()} checked{/if}{*
-                               *}{if $field->isImmutable()} disabled{/if}{*
+                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue() && $__fieldNestedOption[isSelectable]} checked{/if}{*
+                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                        *}> {@$__fieldNestedOption[label]}
                </label>
        {/foreach}
index f772ab7b0309e5132b024b21e0499337fe9623c3..12be157cc99addb63cbc3589830634d5008e54ac 100644 (file)
@@ -24,8 +24,8 @@
                                                *}type="radio" {*
                                                *}name="{@$field->getPrefixedId()}" {*
                                                *}value="{$__fieldNestedOption[value]}"{*
-                                               *}{if $field->getValue() == $__fieldNestedOption[value]} checked{/if}{*
-                                               *}{if $field->isImmutable()} disabled{/if}{*
+                                               *}{if $field->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]} checked{/if}{*
+                                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                                        *}> {@$__fieldNestedOption[label]}</label>
                        </li>
                {/foreach}
@@ -36,8 +36,8 @@
                        <option {*
                                *}name="{@$field->getPrefixedId()}" {*
                                *}value="{$__fieldNestedOption[value]}"{*
-                               *}{if $field->getValue() == $__fieldNestedOption[value]} selected{/if}{*
-                               *}{if $field->isImmutable()} disabled{/if}{*
+                               *}{if $field->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]} selected{/if}{*
+                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                        *}>{@'&nbsp;'|str_repeat:$__fieldNestedOption[depth] * 4}{@$__fieldNestedOption[label]}</option>
                {/foreach}
        </select>
index e9bf9fef66868f660606eba44083dd77e1743906..f6afdf6be3873b63e3cace60ed15adeab4716c85 100644 (file)
@@ -24,8 +24,8 @@
                                                *}type="checkbox" {*
                                                *}name="{@$field->getPrefixedId()}[]" {*
                                                *}value="{$__fieldNestedOption[value]}"{*
-                                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue()} checked{/if}{*
-                                               *}{if $field->isImmutable()} disabled{/if}{*
+                                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue() && $__fieldNestedOption[isSelectable]} checked{/if}{*
+                                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                                        *}> {@$__fieldNestedOption[label]}</label>
                        </li>
                {/foreach}
@@ -36,8 +36,8 @@
                        <input type="checkbox" {*
                                *}name="{@$field->getPrefixedId()}[]" {*
                                *}value="{$__fieldNestedOption[value]}"{*
-                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue()} checked{/if}{*
-                               *}{if $field->isImmutable()} disabled{/if}{*
+                               *}{if $field->getValue() !== null && $__fieldNestedOption[value]|in_array:$field->getValue() && $__fieldNestedOption[isSelectable]} checked{/if}{*
+                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                        *}> {@$__fieldNestedOption[label]}
                </label>
        {/foreach}
index f772ab7b0309e5132b024b21e0499337fe9623c3..12be157cc99addb63cbc3589830634d5008e54ac 100644 (file)
@@ -24,8 +24,8 @@
                                                *}type="radio" {*
                                                *}name="{@$field->getPrefixedId()}" {*
                                                *}value="{$__fieldNestedOption[value]}"{*
-                                               *}{if $field->getValue() == $__fieldNestedOption[value]} checked{/if}{*
-                                               *}{if $field->isImmutable()} disabled{/if}{*
+                                               *}{if $field->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]} checked{/if}{*
+                                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                                        *}> {@$__fieldNestedOption[label]}</label>
                        </li>
                {/foreach}
@@ -36,8 +36,8 @@
                        <option {*
                                *}name="{@$field->getPrefixedId()}" {*
                                *}value="{$__fieldNestedOption[value]}"{*
-                               *}{if $field->getValue() == $__fieldNestedOption[value]} selected{/if}{*
-                               *}{if $field->isImmutable()} disabled{/if}{*
+                               *}{if $field->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]} selected{/if}{*
+                               *}{if $field->isImmutable() || !$__fieldNestedOption[isSelectable]} disabled{/if}{*
                        *}>{@'&nbsp;'|str_repeat:$__fieldNestedOption[depth] * 4}{@$__fieldNestedOption[label]}</option>
                {/foreach}
        </select>
index 78975c002b13c4d8eef16444de79840187a62515..b67b8eebefdfd1a34d5bced33dbcfec9bc718138 100644 (file)
@@ -45,7 +45,7 @@ trait TSelectionFormField {
        }
        
        /**
-        * Returns the possible options of this field.
+        * Returns the selectable options of this field.
         * 
         * @return      array
         * 
@@ -138,6 +138,7 @@ trait TSelectionFormField {
                                        
                                        $collectedOptions[] = [
                                                'depth' => $object->getDepth() - 1,
+                                               'isSelectable' => true,
                                                'label' => $object,
                                                'value' => $object->getObjectID()
                                        ];
@@ -156,9 +157,6 @@ trait TSelectionFormField {
                                if (!is_array($option)) {
                                        throw new \InvalidArgumentException("Nested option with key '{$key}' has is no array.");
                                }
-                               if (count($option) !== 3) {
-                                       throw new \InvalidArgumentException("Nested option with key '{$key}' does not contain three elements.");
-                               }
                                
                                // check if all required elements exist
                                foreach (['label', 'value', 'depth'] as $entry) {
@@ -196,9 +194,6 @@ trait TSelectionFormField {
                                        throw new \InvalidArgumentException("Options values must be unique, but '{$option['value']}' appears at least twice as value.");
                                }
                                
-                               // save value
-                               $this->options[$option['value']] = $option['label'];
-                               
                                // validate depth
                                if (!is_int($option['depth'])) {
                                        throw new \InvalidArgumentException("Depth of nested option with key '{$key}' is no integer, " . gettype($options) . " given.");
@@ -206,6 +201,14 @@ trait TSelectionFormField {
                                if ($option['depth'] < 0) {
                                        throw new \InvalidArgumentException("Depth of nested option with key '{$key}' is negative.");
                                }
+                               
+                               // set default value of `isSelectable`
+                               $option['isSelectable'] = $option['isSelectable'] ?? true;
+                               
+                               // save value
+                               if ($option['isSelectable']) {
+                                       $this->options[$option['value']] = $option['label'];
+                               }
                        }
                        unset($option);
                        
@@ -252,6 +255,7 @@ trait TSelectionFormField {
                                foreach ($this->options as $value => $label) {
                                        $this->nestedOptions[] = [
                                                'depth' => 0,
+                                               'isSelectable' => true,
                                                'label' => $label,
                                                'value' => $value
                                        ];