Fixed whitespaces in enable options
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / option / Option.class.php
1 <?php
2 namespace wcf\data\option;
3 use wcf\data\DatabaseObject;
4 use wcf\data\TDatabaseObjectOptions;
5 use wcf\data\TDatabaseObjectPermissions;
6 use wcf\system\WCF;
7 use wcf\util\ArrayUtil;
8 use wcf\util\StringUtil;
9
10 /**
11 * Represents an option.
12 *
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
17 *
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
34 */
35 class Option extends DatabaseObject {
36 use TDatabaseObjectOptions;
37 use TDatabaseObjectPermissions;
38
39 /**
40 * @inheritDoc
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
55 /**
56 * @inheritDoc
57 */
58 protected function handleData($data) {
59 parent::handleData($data);
60
61 // unserialize additional data
62 $this->data['additionalData'] = (empty($data['additionalData']) ? [] : @unserialize($data['additionalData']));
63 }
64
65 /**
66 * Returns a list of options.
67 *
68 * @return Option[]
69 */
70 public static function getOptions() {
71 $sql = "SELECT *
72 FROM wcf".WCF_N."_option";
73 $statement = WCF::getDB()->prepareStatement($sql);
74 $statement->execute();
75
76 $options = [];
77 while ($row = $statement->fetchArray()) {
78 $option = new Option(null, $row);
79 $options[$option->getConstantName()] = $option;
80 }
81
82 return $options;
83 }
84
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
101 /**
102 * Parses enableOptions.
103 *
104 * @param string $optionData
105 * @return array
106 */
107 public static function parseEnableOptions($optionData) {
108 $disableOptions = $enableOptions = '';
109
110 if (!empty($optionData)) {
111 $options = ArrayUtil::trim(explode(',', $optionData));
112
113 foreach ($options as $item) {
114 if ($item{0} == '!') {
115 if (!empty($disableOptions)) $disableOptions .= ',';
116 $disableOptions .= "'".mb_substr($item, 1)."' ";
117 }
118 else {
119 if (!empty($enableOptions)) $enableOptions .= ',';
120 $enableOptions .= "'".$item."' ";
121 }
122 }
123 }
124
125 return [
126 'disableOptions' => $disableOptions,
127 'enableOptions' => $enableOptions
128 ];
129 }
130
131 /**
132 * Returns a list of the available options.
133 *
134 * @return array
135 */
136 public function parseSelectOptions() {
137 $result = [];
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);
145 }
146
147 $result[$key] = $value;
148 }
149
150 return $result;
151 }
152
153 /**
154 * Returns a list of the enable options.
155 *
156 * @return array
157 */
158 public function parseMultipleEnableOptions() {
159 $result = [];
160 if (!empty($this->enableOptions)) {
161 $options = explode("\n", StringUtil::trim(StringUtil::unifyNewlines($this->enableOptions)));
162 $key = -1;
163 foreach ($options as $option) {
164 if (mb_strpos($option, ':') !== false) {
165 $optionData = explode(':', $option);
166 $key = array_shift($optionData);
167 $value = implode(':', $optionData);
168 }
169 else {
170 $key++;
171 $value = $option;
172 }
173
174 $result[$key] = $value;
175 }
176 }
177
178 return $result;
179 }
180
181 /**
182 * Returns true if option is visible
183 *
184 * @return boolean
185 */
186 public function isVisible() {
187 return !$this->hidden;
188 }
189
190 /**
191 * @inheritDoc
192 */
193 public static function getDatabaseTableAlias() {
194 return 'option_table';
195 }
196
197 /**
198 * Returns the constant name.
199 *
200 * @return string
201 */
202 public function getConstantName() {
203 return strtoupper($this->optionName);
204 }
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 }
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 }
223
224 /**
225 * Allows modifications of hidden option.
226 *
227 * @param string $hiddenOption
228 */
229 public function modifyHiddenOption($hiddenOption) {
230 $this->data['hidden'] = $hiddenOption;
231 }
232 }