Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / option / Option.class.php
CommitLineData
11ade432
AE
1<?php
2namespace wcf\data\option;
bcb9c1bf 3use wcf\data\DatabaseObject;
c1b907f3
MS
4use wcf\data\TDatabaseObjectOptions;
5use wcf\data\TDatabaseObjectPermissions;
11ade432 6use wcf\system\WCF;
efc04b57 7use wcf\util\ArrayUtil;
11ade432
AE
8use wcf\util\StringUtil;
9
10/**
11 * Represents an option.
a17de04e 12 *
11ade432 13 * @author Alexander Ebert
c839bd49 14 * @copyright 2001-2018 WoltLab GmbH
11ade432 15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 16 * @package WoltLabSuite\Core\Data\Option
e9335ed9 17 *
ed6a4e42
MS
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`
1615fc2e 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`
ed6a4e42 33 * @property-read array $additionalData array with additional data of the option
11ade432
AE
34 */
35class Option extends DatabaseObject {
c1b907f3
MS
36 use TDatabaseObjectOptions;
37 use TDatabaseObjectPermissions;
38
3ef894e8 39 /**
0fcfe5f6 40 * @inheritDoc
3ef894e8
MS
41 */
42 public function __get($name) {
43 $value = parent::__get($name);
44
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];
49 }
50 }
51
52 return $value;
53 }
54
11ade432 55 /**
0fcfe5f6 56 * @inheritDoc
11ade432
AE
57 */
58 protected function handleData($data) {
59 parent::handleData($data);
60
61 // unserialize additional data
058cbd6a 62 $this->data['additionalData'] = (empty($data['additionalData']) ? [] : @unserialize($data['additionalData']));
11ade432
AE
63 }
64
65 /**
66 * Returns a list of options.
9f959ced 67 *
7a23a706 68 * @return Option[]
11ade432 69 */
f1c1fc65 70 public static function getOptions() {
0eafcb83
AE
71 $sql = "SELECT *
72 FROM wcf".WCF_N."_option";
11ade432 73 $statement = WCF::getDB()->prepareStatement($sql);
f1c1fc65 74 $statement->execute();
0eafcb83 75
058cbd6a 76 $options = [];
11ade432 77 while ($row = $statement->fetchArray()) {
85ccfeb4
MW
78 $option = new Option(null, $row);
79 $options[$option->getConstantName()] = $option;
11ade432
AE
80 }
81
82 return $options;
83 }
84
8b4d542c
MS
85 /**
86 * Returns the option with the given name or `null` if no such option exists.
87 *
88 * @param string $optionName name of the requested option
89 * @return Option|null requested option
90 */
91 public static function getOptionByName($optionName) {
92 $sql = "SELECT *
93 FROM wcf".WCF_N."_option
94 WHERE optionName = ?";
95 $statement = WCF::getDB()->prepareStatement($sql);
96 $statement->execute([$optionName]);
97
98 return $statement->fetchObject(self::class);
99 }
100
11ade432
AE
101 /**
102 * Parses enableOptions.
9f959ced 103 *
11ade432
AE
104 * @param string $optionData
105 * @return array
106 */
107 public static function parseEnableOptions($optionData) {
108 $disableOptions = $enableOptions = '';
109
110 if (!empty($optionData)) {
efc04b57 111 $options = ArrayUtil::trim(explode(',', $optionData));
11ade432
AE
112
113 foreach ($options as $item) {
114 if ($item{0} == '!') {
115 if (!empty($disableOptions)) $disableOptions .= ',';
838e315b 116 $disableOptions .= "'".mb_substr($item, 1)."' ";
11ade432
AE
117 }
118 else {
119 if (!empty($enableOptions)) $enableOptions .= ',';
120 $enableOptions .= "'".$item."' ";
121 }
122 }
123 }
124
058cbd6a 125 return [
11ade432
AE
126 'disableOptions' => $disableOptions,
127 'enableOptions' => $enableOptions
058cbd6a 128 ];
11ade432
AE
129 }
130
131 /**
132 * Returns a list of the available options.
9f959ced 133 *
11ade432
AE
134 * @return array
135 */
136 public function parseSelectOptions() {
058cbd6a 137 $result = [];
11ade432
AE
138 $options = explode("\n", StringUtil::trim(StringUtil::unifyNewlines($this->selectOptions)));
139 foreach ($options as $option) {
140 $key = $value = $option;
838e315b 141 if (mb_strpos($option, ':') !== false) {
11ade432
AE
142 $optionData = explode(':', $option);
143 $key = array_shift($optionData);
144 $value = implode(':', $optionData);
145 }
a17de04e 146
11ade432
AE
147 $result[$key] = $value;
148 }
149
150 return $result;
151 }
152
153 /**
154 * Returns a list of the enable options.
9f959ced 155 *
11ade432
AE
156 * @return array
157 */
158 public function parseMultipleEnableOptions() {
058cbd6a 159 $result = [];
11ade432
AE
160 if (!empty($this->enableOptions)) {
161 $options = explode("\n", StringUtil::trim(StringUtil::unifyNewlines($this->enableOptions)));
162 $key = -1;
163 foreach ($options as $option) {
838e315b 164 if (mb_strpos($option, ':') !== false) {
11ade432
AE
165 $optionData = explode(':', $option);
166 $key = array_shift($optionData);
167 $value = implode(':', $optionData);
168 }
169 else {
170 $key++;
171 $value = $option;
172 }
2d63c13c 173
11ade432
AE
174 $result[$key] = $value;
175 }
176 }
177
178 return $result;
179 }
f76cc27c
AE
180
181 /**
28410a97 182 * Returns true if option is visible
f76cc27c
AE
183 *
184 * @return boolean
185 */
cf251c63 186 public function isVisible() {
f76cc27c
AE
187 return !$this->hidden;
188 }
0af40e84
AE
189
190 /**
0fcfe5f6 191 * @inheritDoc
0af40e84
AE
192 */
193 public static function getDatabaseTableAlias() {
194 return 'option_table';
195 }
85ccfeb4
MW
196
197 /**
198 * Returns the constant name.
199 *
200 * @return string
201 */
202 public function getConstantName() {
0eafcb83 203 return strtoupper($this->optionName);
85ccfeb4 204 }
cc016743
AE
205
206 /**
207 * Allows modifications of select options.
208 *
209 * @param string $selectOptions
210 */
211 public function modifySelectOptions($selectOptions) {
212 $this->data['selectOptions'] = $selectOptions;
213 }
07ade600
S
214
215 /**
216 * Allows modifications of enable options.
217 *
218 * @param string $enableOptions
219 */
220 public function modifyEnableOptions($enableOptions) {
221 $this->data['enableOptions'] = $enableOptions;
222 }
8e2bc2bd
AE
223
224 /**
225 * Allows modifications of hidden option.
1a6e8c52 226 *
8e2bc2bd
AE
227 * @param string $hiddenOption
228 */
229 public function modifyHiddenOption($hiddenOption) {
230 $this->data['hidden'] = $hiddenOption;
231 }
11ade432 232}