2 namespace wcf\data\option
;
3 use wcf\data\DatabaseObject
;
4 use wcf\data\TDatabaseObjectOptions
;
5 use wcf\data\TDatabaseObjectPermissions
;
7 use wcf\util\ArrayUtil
;
8 use wcf\util\StringUtil
;
11 * Represents an option.
13 * @author Alexander Ebert
14 * @copyright 2001-2018 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\Data\Option
18 * @property-read integer $optionID unique id of the option
19 * @property-read integer $packageID id of the package the which delivers the option
20 * @property-read string $optionName name and textual identifier of the option
21 * @property-read string $categoryName name of the option category the option belongs to
22 * @property-read string $optionType textual identifier of the option (corresponds to a class implementing `wcf\system\option\IOptionType`)
23 * @property-read string $optionValue value of the option
24 * @property-read string $validationPattern regular expression used to validate the option's value or empty if no such regular expression exists
25 * @property-read string $selectOptions newline-separated list of selectable options for a selectable option type (line pattern: `{value}:{language item name}`)
26 * @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}`)
27 * @property-read integer $showOrder position of the option in relation to the other option in the option category
28 * @property-read integer $hidden is `1` if the option is hidden and thus cannot be explicitly set by in the acp, otherwise `0`
29 * @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
30 * @property-read string $options comma separated list of options of which at least one needs to be enabled for the option to be editable
31 * @property-read integer $supportI18n is `1` if the option supports different values for all available languages, otherwise `0`
32 * @property-read integer $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`
33 * @property-read array $additionalData array with additional data of the option
35 class Option
extends DatabaseObject
{
36 use TDatabaseObjectOptions
;
37 use TDatabaseObjectPermissions
;
42 public function __get($name) {
43 $value = parent
::__get($name);
45 // treat additional data as data variables if it is an array
46 if ($value === null) {
47 if (is_array($this->data
['additionalData']) && isset($this->data
['additionalData'][$name])) {
48 $value = $this->data
['additionalData'][$name];
58 protected function handleData($data) {
59 parent
::handleData($data);
61 // unserialize additional data
62 $this->data
['additionalData'] = (empty($data['additionalData']) ?
[] : @unserialize
($data['additionalData']));
66 * Returns a list of options.
70 public static function getOptions() {
72 FROM wcf".WCF_N
."_option";
73 $statement = WCF
::getDB()->prepareStatement($sql);
74 $statement->execute();
77 while ($row = $statement->fetchArray()) {
78 $option = new Option(null, $row);
79 $options[$option->getConstantName()] = $option;
86 * Returns the option with the given name or `null` if no such option exists.
88 * @param string $optionName name of the requested option
89 * @return Option|null requested option
91 public static function getOptionByName($optionName) {
93 FROM wcf".WCF_N
."_option
94 WHERE optionName = ?";
95 $statement = WCF
::getDB()->prepareStatement($sql);
96 $statement->execute([$optionName]);
98 return $statement->fetchObject(self
::class);
102 * Parses enableOptions.
104 * @param string $optionData
107 public static function parseEnableOptions($optionData) {
108 $disableOptions = $enableOptions = '';
110 if (!empty($optionData)) {
111 $options = ArrayUtil
::trim(explode(',', $optionData));
113 foreach ($options as $item) {
114 if ($item{0} == '!') {
115 if (!empty($disableOptions)) $disableOptions .= ',';
116 $disableOptions .= "'".mb_substr($item, 1)."' ";
119 if (!empty($enableOptions)) $enableOptions .= ',';
120 $enableOptions .= "'".$item."' ";
126 'disableOptions' => $disableOptions,
127 'enableOptions' => $enableOptions
132 * Returns a list of the available options.
136 public function parseSelectOptions() {
138 $options = explode("\n", StringUtil
::trim(StringUtil
::unifyNewlines($this->selectOptions
)));
139 foreach ($options as $option) {
140 $key = $value = $option;
141 if (mb_strpos($option, ':') !== false) {
142 $optionData = explode(':', $option);
143 $key = array_shift($optionData);
144 $value = implode(':', $optionData);
147 $result[$key] = $value;
154 * Returns a list of the enable options.
158 public function parseMultipleEnableOptions() {
160 if (!empty($this->enableOptions
)) {
161 $options = explode("\n", StringUtil
::trim(StringUtil
::unifyNewlines($this->enableOptions
)));
163 foreach ($options as $option) {
164 if (mb_strpos($option, ':') !== false) {
165 $optionData = explode(':', $option);
166 $key = array_shift($optionData);
167 $value = implode(':', $optionData);
174 $result[$key] = $value;
182 * Returns true if option is visible
186 public function isVisible() {
187 return !$this->hidden
;
193 public static function getDatabaseTableAlias() {
194 return 'option_table';
198 * Returns the constant name.
202 public function getConstantName() {
203 return strtoupper($this->optionName
);
207 * Allows modifications of select options.
209 * @param string $selectOptions
211 public function modifySelectOptions($selectOptions) {
212 $this->data
['selectOptions'] = $selectOptions;
216 * Allows modifications of enable options.
218 * @param string $enableOptions
220 public function modifyEnableOptions($enableOptions) {
221 $this->data
['enableOptions'] = $enableOptions;
225 * Allows modifications of hidden option.
227 * @param string $hiddenOption
229 public function modifyHiddenOption($hiddenOption) {
230 $this->data
['hidden'] = $hiddenOption;