-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathUserOptionEditor.class.php
More file actions
191 lines (166 loc) · 5.01 KB
/
UserOptionEditor.class.php
File metadata and controls
191 lines (166 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace wcf\data\user\option;
use wcf\data\DatabaseObjectEditor;
use wcf\data\IEditableCachedObject;
use wcf\system\cache\builder\UserOptionCacheBuilder;
use wcf\system\WCF;
/**
* Provides functions to edit user options.
*
* @author Alexander Ebert
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @mixin UserOption
* @extends DatabaseObjectEditor<UserOption>
* @implements IEditableCachedObject<UserOption>
*/
class UserOptionEditor extends DatabaseObjectEditor implements IEditableCachedObject
{
/**
* @inheritDoc
*/
protected static $baseClass = UserOption::class;
/**
* @inheritDoc
* @return UserOption
*/
public static function create(array $parameters = [])
{
/** @var UserOption $userOption */
$userOption = parent::create($parameters);
// alter the table "wcf1_user_option_value" with this new option
WCF::getDB()->getEditor()->addColumn(
'wcf1_user_option_value',
'userOption' . $userOption->optionID,
self::getColumnDefinition($parameters['optionType'])
);
// add the default value to this column
if (isset($parameters['defaultValue'])) {
$sql = "UPDATE wcf1_user_option_value
SET userOption" . $userOption->optionID . " = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$parameters['defaultValue']]);
}
return $userOption;
}
/**
* @inheritDoc
*/
public function update(array $parameters = [])
{
parent::update($parameters);
// alter the table "wcf1_user_option_value" with this new option
if (isset($parameters['optionType']) && $parameters['optionType'] != $this->optionType) {
WCF::getDB()->getEditor()->alterColumn(
'wcf1_user_option_value',
'userOption' . $this->optionID,
'userOption' . $this->optionID,
self::getColumnDefinition($parameters['optionType'])
);
}
}
/**
* @inheritDoc
*/
public function delete()
{
$sql = "DELETE FROM wcf1_user_option
WHERE optionID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->optionID]);
WCF::getDB()->getEditor()->dropColumn('wcf1_user_option_value', 'userOption' . $this->optionID);
}
/**
* @inheritDoc
*/
public static function deleteAll(array $objectIDs = [])
{
$returnValue = parent::deleteAll($objectIDs);
foreach ($objectIDs as $objectID) {
WCF::getDB()->getEditor()->dropColumn('wcf1_user_option_value', 'userOption' . $objectID);
}
return $returnValue;
}
/**
* Disables this option.
*
* @return void
*/
public function disable()
{
$this->enable(false);
}
/**
* Enables this option.
*
* @param bool $enable
* @return void
*/
public function enable($enable = true)
{
$value = \intval(!$enable);
$sql = "UPDATE wcf1_user_option
SET isDisabled = ?
WHERE optionID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$value, $this->optionID]);
}
/**
* Determines the needed sql statement to modify column definitions.
*
* @param string $optionType
* @return array{
* autoIncrement: bool,
* key: false,
* notNull: bool,
* type: string,
* default?: int|float|string,
* length?: int,
* } column definition
*/
public static function getColumnDefinition($optionType)
{
$column = [
'autoIncrement' => false,
'key' => false,
'notNull' => false,
'type' => 'text',
];
switch ($optionType) {
case 'boolean':
$column['notNull'] = true;
$column['default'] = 0;
$column['type'] = 'tinyint';
break;
case 'integer':
$column['notNull'] = true;
$column['default'] = 0;
$column['type'] = 'int';
break;
case 'float':
$column['notNull'] = true;
$column['default'] = 0.0;
$column['type'] = 'float';
break;
case 'textarea':
$column['type'] = 'mediumtext';
break;
case 'birthday':
case 'date':
$column['notNull'] = true;
$column['default'] = "'0000-00-00'";
$column['length'] = 10;
$column['type'] = 'char';
break;
}
return $column;
}
/**
* @inheritDoc
*/
public static function resetCache()
{
UserOptionCacheBuilder::getInstance()->reset();
}
}