Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / form / builder / container / SuffixFormFieldContainer.class.php
1 <?php
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;
6 use wcf\system\WCF;
7
8 /**
9 * Represents a form field container for one main field with (optional) support for a suffix selection
10 * form field.
11 *
12 * Child elements explicitly added to this container are not shown.
13 *
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
18 * @since 5.2
19 */
20 class SuffixFormFieldContainer extends FormContainer {
21 /**
22 * form field to which the suffix selection is added
23 * @var IFormField
24 */
25 protected $field;
26
27 /**
28 * selection form field containing the suffix options
29 * @var ISelectionFormField
30 */
31 protected $suffixField;
32
33 /**
34 * Sets the form field to which the suffix selection is added and returns this field.
35 *
36 * @param IFormField $formField
37 * @return $this
38 */
39 public function field(IFormField $formField) {
40 if ($this->field !== null) {
41 throw new \BadMethodCallException('Field has already been set.');
42 }
43
44 $this->field = $formField;
45 $this->appendChild($formField);
46
47 return $this;
48 }
49
50 /**
51 * Returns the form field to which the suffix selection is added.
52 *
53 * @return IFormField
54 */
55 public function getField() {
56 if ($this->field === null) {
57 throw new \BadMethodCallException('Field has not been set yet.');
58 }
59
60 return $this->field;
61 }
62
63 /**
64 * @inheritDoc
65 */
66 public function getHtml() {
67 return WCF::getTPL()->fetch('__suffixFormFieldContainer', 'wcf', [
68 'element' => $this
69 ]);
70 }
71
72 /**
73 * Returns the initial option of the suffix selection dropdown.
74 *
75 * @return array
76 * @throws \BadMethodCallException if no suffix field is set or has no options
77 */
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.');
81 }
82 if (empty($this->getSuffixField()->getOptions())) {
83 throw new \BadMethodCallException('The suffix field has no options.');
84 }
85
86 foreach ($this->getSuffixField()->getNestedOptions() as $option) {
87 if ($this->getSuffixField()->getValue() === null) {
88 if ($option['isSelectable']) {
89 return $option;
90 }
91 }
92 else if ($option['value'] == $this->getSuffixField()->getValue()) {
93 return $option;
94 }
95 }
96
97 // Return the first selectable option if no valid value is selected.
98 foreach ($this->getSuffixField()->getNestedOptions() as $option) {
99 if ($option['isSelectable']) {
100 return $option;
101 }
102 }
103
104 throw new \RuntimeException('Cannot determine selected suffix option.');
105 }
106
107 /**
108 * Returns the selection form field containing the suffix options.
109 *
110 * @return ISelectionFormField
111 */
112 public function getSuffixField() {
113 return $this->suffixField;
114 }
115
116 /**
117 * Returns the label used for the suffix selection if the field has no selectable options
118 * or is immutable.
119 *
120 * @return string
121 */
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.');
125 }
126
127 if (empty($this->getSuffixField()->getOptions())) {
128 return '';
129 }
130
131 if (isset($this->getSuffixField()->getOptions()[$this->getSuffixField()->getValue()])) {
132 return $this->getSuffixField()->getOptions()[$this->getSuffixField()->getValue()];
133 }
134
135 return '';
136 }
137
138 /**
139 * Sets the selection form field containing the suffix options.
140 *
141 * @param ISelectionFormField $formField
142 * @return $this
143 * @throws \BadMethodCallException if no suffix field is set
144 */
145 public function suffixField(ISelectionFormField $formField) {
146 if ($this->suffixField !== null) {
147 throw new \BadMethodCallException('Suffix field has already been set.');
148 }
149
150 $this->suffixField = $formField;
151 $this->appendChild($formField);
152
153 return $this;
154 }
155
156 /**
157 * Returns `true` if the suffix selection has any selectable options.
158 *
159 * @return bool
160 */
161 public function suffixHasSelectableOptions() {
162 if ($this->getSuffixField() === null) {
163 return false;
164 }
165
166 if ($this->getSuffixField() instanceof IImmutableFormField && $this->getSuffixField()->isImmutable()) {
167 return false;
168 }
169
170 foreach ($this->getSuffixField()->getNestedOptions() as $option) {
171 if ($option['isSelectable']) {
172 return true;
173 }
174 }
175
176 return false;
177 }
178 }