3 namespace wcf\system\option
;
5 use wcf\data\option\Option
;
6 use wcf\data\user\User
;
7 use wcf\data\user\UserList
;
8 use wcf\system\database\util\PreparedStatementConditionBuilder
;
9 use wcf\system\exception\UserInputException
;
11 use wcf\util\ArrayUtil
;
12 use wcf\util\StringUtil
;
15 * Option type implementation for radio buttons.
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 class RadioButtonOptionType
extends AbstractOptionType
implements
22 ISearchableConditionUserOption
,
23 ISelectOptionOptionType
26 * name of the template that contains the form element of this option type
29 public $templateName = 'radioButtonOptionType';
32 * if `true`, the option is considered as being searched when generating the form element
35 public $forceSearchOption = false;
40 public function getFormElement(Option
$option, $value)
42 $availableOptions = $option->parseMultipleEnableOptions();
44 'disableOptions' => [],
45 'enableOptions' => [],
48 foreach ($availableOptions as $key => $enableOptions) {
49 $optionData = Option
::parseEnableOptions($enableOptions);
51 $options['disableOptions'][$key] = $optionData['disableOptions'];
52 $options['enableOptions'][$key] = $optionData['enableOptions'];
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);
61 WCF
::getTPL()->assign([
62 'disableOptions' => $options['disableOptions'],
63 'enableOptions' => $options['enableOptions'],
65 'selectOptions' => $this->getSelectOptions($option),
69 return WCF
::getTPL()->fetch($this->templateName
);
75 public function validate(Option
$option, $newValue)
77 if (!empty($newValue)) {
78 $options = $this->getSelectOptions($option);
79 if (!isset($options[$newValue])) {
80 throw new UserInputException($option->optionName
, 'validationFailed');
88 public function getSearchFormElement(Option
$option, $value)
90 $this->templateName
= 'shared_radioButtonSearchableOptionType';
91 WCF
::getTPL()->assign(
93 $this->forceSearchOption ||
($value !== null && $value !== $option->defaultValue
) ||
isset($_POST['searchOptions'][$option->optionName
])
96 return $this->getFormElement($option, $value);
102 public function getCondition(PreparedStatementConditionBuilder
&$conditions, Option
$option, $value)
104 if (!isset($_POST['searchOptions'][$option->optionName
])) {
108 $conditions->add("option_value.userOption" . $option->optionID
. " = ?", [StringUtil
::trim($value ?
: '')]);
116 public function addCondition(UserList
$userList, Option
$option, $value)
118 $userList->getConditionBuilder()->add(
119 'user_option_value.userOption' . $option->optionID
. ' = ?',
120 [StringUtil
::trim($value)]
127 public function checkUser(User
$user, Option
$option, $value)
129 return \
mb_strtolower($user->getUserOption($option->optionName
)) == \
mb_strtolower(StringUtil
::trim($value));
135 public function getConditionData(Option
$option, $newValue)
141 * Returns the select options for the given option.
143 * @param Option $option
146 protected function getSelectOptions(Option
$option)
148 return $option->parseSelectOptions();
154 public function getCSSClassName()
156 return 'checkboxList';
162 public function getDisabledOptionNames($value, $enableOptions)
164 $valueToOptions = \
explode("\n", StringUtil
::trim(StringUtil
::unifyNewlines($enableOptions)));
167 foreach ($valueToOptions as $valueToOption) {
168 if (\
str_contains($valueToOption, ':')) {
169 $optionData = \
explode(':', $valueToOption);
170 $key = \array_shift
($optionData);
171 $enableOptionValues = \
implode(':', $optionData);
174 $enableOptionValues = $valueToOption;
177 if ($key == $value) {
178 $options = ArrayUtil
::trim(\
explode(',', $enableOptionValues));
181 foreach ($options as $item) {
182 if ($item[0] == '!') {