2 namespace wcf\system\form\builder\container
;
3 use wcf\system\form\builder\field\IFormField
;
4 use wcf\system\form\builder\field\IImmutableFormField
;
5 use wcf\system\form\builder\field\ISelectionFormField
;
9 * Represents a form field container for one main field with (optional) support for a suffix selection
12 * Child elements explicitly added to this container are not shown.
14 * @author Matthias Schmidt
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Form\Builder\Container
20 class SuffixFormFieldContainer
extends FormContainer
{
22 * form field to which the suffix selection is added
28 * selection form field containing the suffix options
29 * @var ISelectionFormField
31 protected $suffixField;
34 * Sets the form field to which the suffix selection is added and returns this field.
36 * @param IFormField $formField
39 public function field(IFormField
$formField) {
40 if ($this->field
!== null) {
41 throw new \
BadMethodCallException('Field has already been set.');
44 $this->field
= $formField;
45 $this->appendChild($formField);
51 * Returns the form field to which the suffix selection is added.
55 public function getField() {
56 if ($this->field
=== null) {
57 throw new \
BadMethodCallException('Field has not been set yet.');
66 public function getHtml() {
67 return WCF
::getTPL()->fetch('__suffixFormFieldContainer', 'wcf', [
73 * Returns the initial option of the suffix selection dropdown.
76 * @throws \BadMethodCallException if no suffix field is set or has no options
78 public function getSelectedSuffixOption() {
79 if ($this->getSuffixField() === null) {
80 throw new \
BadMethodCallException('There is no suffix field for which a label could be determined.');
82 if (empty($this->getSuffixField()->getOptions())) {
83 throw new \
BadMethodCallException('The suffix field has no options.');
86 foreach ($this->getSuffixField()->getNestedOptions() as $option) {
87 if ($this->getSuffixField()->getValue() === null) {
88 if ($option['isSelectable']) {
92 else if ($option['value'] == $this->getSuffixField()->getValue()) {
97 // Return the first selectable option if no valid value is selected.
98 foreach ($this->getSuffixField()->getNestedOptions() as $option) {
99 if ($option['isSelectable']) {
104 throw new \
RuntimeException('Cannot determine selected suffix option.');
108 * Returns the selection form field containing the suffix options.
110 * @return ISelectionFormField
112 public function getSuffixField() {
113 return $this->suffixField
;
117 * Returns the label used for the suffix selection if the field has no selectable options
122 public function getSuffixLabel() {
123 if ($this->getSuffixField() === null) {
124 throw new \
BadMethodCallException('There is no suffix field for which a label could be determined.');
127 if (empty($this->getSuffixField()->getOptions())) {
131 if (isset($this->getSuffixField()->getOptions()[$this->getSuffixField()->getValue()])) {
132 return $this->getSuffixField()->getOptions()[$this->getSuffixField()->getValue()];
139 * Sets the selection form field containing the suffix options.
141 * @param ISelectionFormField $formField
143 * @throws \BadMethodCallException if no suffix field is set
145 public function suffixField(ISelectionFormField
$formField) {
146 if ($this->suffixField
!== null) {
147 throw new \
BadMethodCallException('Suffix field has already been set.');
150 $this->suffixField
= $formField;
151 $this->appendChild($formField);
157 * Returns `true` if the suffix selection has any selectable options.
161 public function suffixHasSelectableOptions() {
162 if ($this->getSuffixField() === null) {
166 if ($this->getSuffixField() instanceof IImmutableFormField
&& $this->getSuffixField()->isImmutable()) {
170 foreach ($this->getSuffixField()->getNestedOptions() as $option) {
171 if ($option['isSelectable']) {