Set default captcha type to none
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / option / Option.class.php
CommitLineData
11ade432 1<?php
a9229942 2
11ade432 3namespace wcf\data\option;
a9229942 4
bcb9c1bf 5use wcf\data\DatabaseObject;
c1b907f3
MS
6use wcf\data\TDatabaseObjectOptions;
7use wcf\data\TDatabaseObjectPermissions;
11ade432 8use wcf\system\WCF;
efc04b57 9use wcf\util\ArrayUtil;
11ade432
AE
10use wcf\util\StringUtil;
11
12/**
13 * Represents an option.
e9335ed9 14 *
a9229942
TD
15 * @author Alexander Ebert
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
a9229942
TD
18 *
19 * @property-read int $optionID unique id of the option
20 * @property-read int $packageID id of the package the which delivers the option
21 * @property-read string $optionName name and textual identifier of the option
22 * @property-read string $categoryName name of the option category the option belongs to
23 * @property-read string $optionType textual identifier of the option (corresponds to a class implementing `wcf\system\option\IOptionType`)
24 * @property-read string $optionValue value of the option
25 * @property-read string $validationPattern regular expression used to validate the option's value or empty if no such regular expression exists
26 * @property-read string $selectOptions newline-separated list of selectable options for a selectable option type (line pattern: `{value}:{language item name}`)
27 * @property-read string $enableOptions list of options that are enabled based on the option's value (simple comma-separated list of boolean options, otherwise newline-separated list with line pattern: `{select value}:{comma-separated list}`)
28 * @property-read int $showOrder position of the option in relation to the other option in the option category
29 * @property-read int $hidden is `1` if the option is hidden and thus cannot be explicitly set by in the acp, otherwise `0`
30 * @property-read string $permissions comma separated list of user group permissions of which the active user needs to have at least one to set the option value
31 * @property-read string $options comma separated list of options of which at least one needs to be enabled for the option to be editable
32 * @property-read int $supportI18n is `1` if the option supports different values for all available languages, otherwise `0`
33 * @property-read int $requireI18n is `1` if `$supportI18n = 1` and the option's value has to explicitly set for all values so that the `monolingual` option is not available, otherwise `0`
34 * @property-read array $additionalData array with additional data of the option
11ade432 35 */
a9229942
TD
36class Option extends DatabaseObject
37{
38 use TDatabaseObjectOptions;
39 use TDatabaseObjectPermissions;
40
41 /**
42 * @inheritDoc
43 */
44 public function __get($name)
45 {
4c1ea02c 46 $value = $this->data[$name] ?? null;
a9229942
TD
47
48 // treat additional data as data variables if it is an array
49 if ($value === null) {
50 if (\is_array($this->data['additionalData']) && isset($this->data['additionalData'][$name])) {
51 $value = $this->data['additionalData'][$name];
52 }
53 }
54
55 return $value;
56 }
57
58 /**
59 * @inheritDoc
60 */
61 protected function handleData($data)
62 {
63 parent::handleData($data);
64
65 // unserialize additional data
66 $this->data['additionalData'] = (empty($data['additionalData']) ? [] : @\unserialize($data['additionalData']));
67 }
68
69 /**
70 * Returns a list of options.
71 *
18422cf5 72 * @return static[]
a9229942 73 */
18422cf5 74 public static function getOptions(): array
a9229942
TD
75 {
76 $sql = "SELECT *
18422cf5 77 FROM " . self::getDatabaseTableName();
a9229942
TD
78 $statement = WCF::getDB()->prepareStatement($sql);
79 $statement->execute();
80
81 $options = [];
82 while ($row = $statement->fetchArray()) {
18422cf5 83 $option = new static(null, $row);
a9229942
TD
84 $options[$option->getConstantName()] = $option;
85 }
86
87 return $options;
88 }
89
90 /**
91 * Returns the option with the given name or `null` if no such option exists.
a9229942 92 */
18422cf5 93 public static function getOptionByName(string $optionName): static|null
a9229942
TD
94 {
95 $sql = "SELECT *
18422cf5 96 FROM " . self::getDatabaseTableName() . "
a9229942
TD
97 WHERE optionName = ?";
98 $statement = WCF::getDB()->prepareStatement($sql);
99 $statement->execute([$optionName]);
100
18422cf5 101 return $statement->fetchObject(static::class);
a9229942
TD
102 }
103
104 /**
105 * Parses enableOptions.
106 *
107 * @param string $optionData
108 * @return array
109 */
110 public static function parseEnableOptions($optionData)
111 {
112 $disableOptions = $enableOptions = '';
113
114 if (!empty($optionData)) {
115 $options = ArrayUtil::trim(\explode(',', $optionData));
116
117 foreach ($options as $item) {
118 if ($item[0] == '!') {
119 if (!empty($disableOptions)) {
120 $disableOptions .= ',';
121 }
122 $disableOptions .= "'" . \mb_substr($item, 1) . "' ";
123 } else {
124 if (!empty($enableOptions)) {
125 $enableOptions .= ',';
126 }
127 $enableOptions .= "'" . $item . "' ";
128 }
129 }
130 }
131
132 return [
133 'disableOptions' => $disableOptions,
134 'enableOptions' => $enableOptions,
135 ];
136 }
137
138 /**
139 * Returns a list of the available options.
140 *
141 * @return array
142 */
143 public function parseSelectOptions()
144 {
145 $result = [];
146 $options = \explode("\n", StringUtil::trim(StringUtil::unifyNewlines($this->selectOptions)));
147 foreach ($options as $option) {
148 $key = $value = $option;
77e8e48f 149 if (\str_contains($option, ':')) {
a9229942
TD
150 $optionData = \explode(':', $option);
151 $key = \array_shift($optionData);
152 $value = \implode(':', $optionData);
153 }
154
155 $result[$key] = $value;
156 }
157
158 return $result;
159 }
160
161 /**
162 * Returns a list of the enable options.
163 *
164 * @return array
165 */
166 public function parseMultipleEnableOptions()
167 {
168 $result = [];
169 if (!empty($this->enableOptions)) {
170 $options = \explode("\n", StringUtil::trim(StringUtil::unifyNewlines($this->enableOptions)));
171 $key = -1;
172 foreach ($options as $option) {
77e8e48f 173 if (\str_contains($option, ':')) {
a9229942
TD
174 $optionData = \explode(':', $option);
175 $key = \array_shift($optionData);
176 $value = \implode(':', $optionData);
177 } else {
178 $key++;
179 $value = $option;
180 }
181
182 $result[$key] = $value;
183 }
184 }
185
186 return $result;
187 }
188
189 /**
190 * Returns true if option is visible
191 *
192 * @return bool
193 */
194 public function isVisible()
195 {
196 return !$this->hidden;
197 }
198
199 /**
200 * @inheritDoc
201 */
202 public static function getDatabaseTableAlias()
203 {
204 return 'option_table';
205 }
206
207 /**
208 * Returns the constant name.
209 *
210 * @return string
211 */
212 public function getConstantName()
213 {
214 return \strtoupper($this->optionName);
215 }
216
217 /**
218 * Allows modifications of select options.
219 *
220 * @param string $selectOptions
221 */
222 public function modifySelectOptions($selectOptions)
223 {
224 $this->data['selectOptions'] = $selectOptions;
225 }
226
227 /**
228 * Allows modifications of enable options.
229 *
230 * @param string $enableOptions
231 */
232 public function modifyEnableOptions($enableOptions)
233 {
234 $this->data['enableOptions'] = $enableOptions;
235 }
236
237 /**
238 * Allows modifications of hidden option.
239 *
240 * @param string $hiddenOption
241 */
242 public function modifyHiddenOption($hiddenOption)
243 {
244 $this->data['hidden'] = $hiddenOption;
245 }
11ade432 246}