Commit | Line | Data |
---|---|---|
158bd3ca | 1 | <?php |
a9229942 | 2 | |
158bd3ca | 3 | namespace wcf\system\option; |
a9229942 | 4 | |
158bd3ca | 5 | use wcf\data\option\Option; |
87d3a054 MS |
6 | use wcf\data\user\User; |
7 | use wcf\data\user\UserList; | |
158bd3ca | 8 | use wcf\system\database\util\PreparedStatementConditionBuilder; |
6286572b | 9 | use wcf\system\exception\UserInputException; |
158bd3ca | 10 | use wcf\system\WCF; |
7f77ebf1 | 11 | use wcf\util\ArrayUtil; |
158bd3ca TD |
12 | use wcf\util\StringUtil; |
13 | ||
14 | /** | |
a17de04e | 15 | * Option type implementation for radio buttons. |
a9229942 TD |
16 | * |
17 | * @author Marcel Werk | |
18 | * @copyright 2001-2019 WoltLab GmbH | |
19 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
158bd3ca | 20 | */ |
a9229942 TD |
21 | class RadioButtonOptionType extends AbstractOptionType implements |
22 | ISearchableConditionUserOption, | |
23 | ISelectOptionOptionType | |
24 | { | |
25 | /** | |
26 | * name of the template that contains the form element of this option type | |
27 | * @var string | |
28 | */ | |
29 | public $templateName = 'radioButtonOptionType'; | |
30 | ||
31 | /** | |
32 | * if `true`, the option is considered as being searched when generating the form element | |
33 | * @var bool | |
34 | */ | |
35 | public $forceSearchOption = false; | |
36 | ||
37 | /** | |
38 | * @inheritDoc | |
39 | */ | |
40 | public function getFormElement(Option $option, $value) | |
41 | { | |
42 | $availableOptions = $option->parseMultipleEnableOptions(); | |
43 | $options = [ | |
44 | 'disableOptions' => [], | |
45 | 'enableOptions' => [], | |
46 | ]; | |
47 | ||
48 | foreach ($availableOptions as $key => $enableOptions) { | |
49 | $optionData = Option::parseEnableOptions($enableOptions); | |
50 | ||
51 | $options['disableOptions'][$key] = $optionData['disableOptions']; | |
52 | $options['enableOptions'][$key] = $optionData['enableOptions']; | |
53 | } | |
54 | ||
55 | // Check, if the current value is invalid and use a valid default value as current selection. | |
56 | if (!isset($this->getSelectOptions($option)[$value])) { | |
57 | $keys = \array_keys($this->getSelectOptions($option)); | |
58 | $value = \reset($keys); | |
59 | } | |
60 | ||
61 | WCF::getTPL()->assign([ | |
62 | 'disableOptions' => $options['disableOptions'], | |
63 | 'enableOptions' => $options['enableOptions'], | |
64 | 'option' => $option, | |
65 | 'selectOptions' => $this->getSelectOptions($option), | |
66 | 'value' => $value, | |
67 | ]); | |
68 | ||
69 | return WCF::getTPL()->fetch($this->templateName); | |
70 | } | |
71 | ||
72 | /** | |
73 | * @inheritDoc | |
74 | */ | |
75 | public function validate(Option $option, $newValue) | |
76 | { | |
77 | if (!empty($newValue)) { | |
78 | $options = $this->getSelectOptions($option); | |
79 | if (!isset($options[$newValue])) { | |
80 | throw new UserInputException($option->optionName, 'validationFailed'); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
86 | * @inheritDoc | |
87 | */ | |
88 | public function getSearchFormElement(Option $option, $value) | |
89 | { | |
b54fdcc0 | 90 | $this->templateName = 'shared_radioButtonSearchableOptionType'; |
a9229942 TD |
91 | WCF::getTPL()->assign( |
92 | 'searchOption', | |
93 | $this->forceSearchOption || ($value !== null && $value !== $option->defaultValue) || isset($_POST['searchOptions'][$option->optionName]) | |
94 | ); | |
95 | ||
96 | return $this->getFormElement($option, $value); | |
97 | } | |
98 | ||
99 | /** | |
100 | * @inheritDoc | |
101 | */ | |
102 | public function getCondition(PreparedStatementConditionBuilder &$conditions, Option $option, $value) | |
103 | { | |
104 | if (!isset($_POST['searchOptions'][$option->optionName])) { | |
105 | return false; | |
106 | } | |
107 | ||
2ef0c53c | 108 | $conditions->add("option_value.userOption" . $option->optionID . " = ?", [StringUtil::trim($value ?: '')]); |
a9229942 TD |
109 | |
110 | return true; | |
111 | } | |
112 | ||
113 | /** | |
114 | * @inheritDoc | |
115 | */ | |
116 | public function addCondition(UserList $userList, Option $option, $value) | |
117 | { | |
118 | $userList->getConditionBuilder()->add( | |
119 | 'user_option_value.userOption' . $option->optionID . ' = ?', | |
120 | [StringUtil::trim($value)] | |
121 | ); | |
122 | } | |
123 | ||
124 | /** | |
125 | * @inheritDoc | |
126 | */ | |
127 | public function checkUser(User $user, Option $option, $value) | |
128 | { | |
129 | return \mb_strtolower($user->getUserOption($option->optionName)) == \mb_strtolower(StringUtil::trim($value)); | |
130 | } | |
131 | ||
132 | /** | |
133 | * @inheritDoc | |
134 | */ | |
135 | public function getConditionData(Option $option, $newValue) | |
136 | { | |
137 | return $newValue; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Returns the select options for the given option. | |
142 | * | |
143 | * @param Option $option | |
144 | * @return string[] | |
145 | */ | |
146 | protected function getSelectOptions(Option $option) | |
147 | { | |
148 | return $option->parseSelectOptions(); | |
149 | } | |
150 | ||
151 | /** | |
152 | * @inheritDoc | |
153 | */ | |
154 | public function getCSSClassName() | |
155 | { | |
156 | return 'checkboxList'; | |
157 | } | |
158 | ||
159 | /** | |
160 | * @inheritDoc | |
161 | */ | |
162 | public function getDisabledOptionNames($value, $enableOptions) | |
163 | { | |
164 | $valueToOptions = \explode("\n", StringUtil::trim(StringUtil::unifyNewlines($enableOptions))); | |
165 | ||
166 | $i = 0; | |
167 | foreach ($valueToOptions as $valueToOption) { | |
77e8e48f | 168 | if (\str_contains($valueToOption, ':')) { |
a9229942 TD |
169 | $optionData = \explode(':', $valueToOption); |
170 | $key = \array_shift($optionData); | |
171 | $enableOptionValues = \implode(':', $optionData); | |
172 | } else { | |
173 | $key = $i; | |
174 | $enableOptionValues = $valueToOption; | |
175 | } | |
176 | ||
177 | if ($key == $value) { | |
178 | $options = ArrayUtil::trim(\explode(',', $enableOptionValues)); | |
179 | $result = []; | |
180 | ||
181 | foreach ($options as $item) { | |
182 | if ($item[0] == '!') { | |
183 | $result[] = $item; | |
184 | } else { | |
185 | $result[] = $item; | |
186 | } | |
187 | } | |
188 | ||
189 | return $result; | |
190 | } | |
191 | ||
192 | $i++; | |
193 | } | |
194 | ||
195 | return []; | |
196 | } | |
dcb3a44c | 197 | } |