3 namespace wcf\data\option
;
5 use wcf\data\DatabaseObject
;
6 use wcf\data\TDatabaseObjectOptions
;
7 use wcf\data\TDatabaseObjectPermissions
;
9 use wcf\util\ArrayUtil
;
10 use wcf\util\StringUtil
;
13 * Represents an option.
15 * @author Alexander Ebert
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
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
36 class Option
extends DatabaseObject
38 use TDatabaseObjectOptions
;
39 use TDatabaseObjectPermissions
;
44 public function __get($name)
46 $value = $this->data
[$name] ??
null;
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];
61 protected function handleData($data)
63 parent
::handleData($data);
65 // unserialize additional data
66 $this->data
['additionalData'] = (empty($data['additionalData']) ?
[] : @\
unserialize($data['additionalData']));
70 * Returns a list of options.
74 public static function getOptions(): array
77 FROM " . self
::getDatabaseTableName();
78 $statement = WCF
::getDB()->prepareStatement($sql);
79 $statement->execute();
82 while ($row = $statement->fetchArray()) {
83 $option = new static(null, $row);
84 $options[$option->getConstantName()] = $option;
91 * Returns the option with the given name or `null` if no such option exists.
93 public static function getOptionByName(string $optionName): static|
null
96 FROM " . self
::getDatabaseTableName() . "
97 WHERE optionName = ?";
98 $statement = WCF
::getDB()->prepareStatement($sql);
99 $statement->execute([$optionName]);
101 return $statement->fetchObject(static::class);
105 * Parses enableOptions.
107 * @param string $optionData
110 public static function parseEnableOptions($optionData)
112 $disableOptions = $enableOptions = '';
114 if (!empty($optionData)) {
115 $options = ArrayUtil
::trim(\
explode(',', $optionData));
117 foreach ($options as $item) {
118 if ($item[0] == '!') {
119 if (!empty($disableOptions)) {
120 $disableOptions .= ',';
122 $disableOptions .= "'" . \
mb_substr($item, 1) . "' ";
124 if (!empty($enableOptions)) {
125 $enableOptions .= ',';
127 $enableOptions .= "'" . $item . "' ";
133 'disableOptions' => $disableOptions,
134 'enableOptions' => $enableOptions,
139 * Returns a list of the available options.
143 public function parseSelectOptions()
146 $options = \
explode("\n", StringUtil
::trim(StringUtil
::unifyNewlines($this->selectOptions
)));
147 foreach ($options as $option) {
148 $key = $value = $option;
149 if (\
str_contains($option, ':')) {
150 $optionData = \
explode(':', $option);
151 $key = \array_shift
($optionData);
152 $value = \
implode(':', $optionData);
155 $result[$key] = $value;
162 * Returns a list of the enable options.
166 public function parseMultipleEnableOptions()
169 if (!empty($this->enableOptions
)) {
170 $options = \
explode("\n", StringUtil
::trim(StringUtil
::unifyNewlines($this->enableOptions
)));
172 foreach ($options as $option) {
173 if (\
str_contains($option, ':')) {
174 $optionData = \
explode(':', $option);
175 $key = \array_shift
($optionData);
176 $value = \
implode(':', $optionData);
182 $result[$key] = $value;
190 * Returns true if option is visible
194 public function isVisible()
196 return !$this->hidden
;
202 public static function getDatabaseTableAlias()
204 return 'option_table';
208 * Returns the constant name.
212 public function getConstantName()
214 return \
strtoupper($this->optionName
);
218 * Allows modifications of select options.
220 * @param string $selectOptions
222 public function modifySelectOptions($selectOptions)
224 $this->data
['selectOptions'] = $selectOptions;
228 * Allows modifications of enable options.
230 * @param string $enableOptions
232 public function modifyEnableOptions($enableOptions)
234 $this->data
['enableOptions'] = $enableOptions;
238 * Allows modifications of hidden option.
240 * @param string $hiddenOption
242 public function modifyHiddenOption($hiddenOption)
244 $this->data
['hidden'] = $hiddenOption;