ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSetting.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 protected ilDBInterface $db;
29
35 private static array $settings_cache = array();
36
42 private static ?string $value_type = null;
43 public array $setting = array();
44 public string $module = "";
45 protected bool $cache_disabled = false;
46
47 public function __construct(
48 string $a_module = "common",
49 bool $a_disabled_cache = false
50 ) {
51 global $DIC;
52
53 $this->db = $DIC->database();
54 $ilDB = $DIC->database();
55
56 $this->cache_disabled = $a_disabled_cache;
57 $this->module = $a_module;
58 // check whether ini file object exists
59 if (!is_object($ilDB)) {
60 die("Fatal Error: ilSettings object instantiated without DB initialisation.");
61 }
62 $this->read();
63 }
64
65 public function getModule(): string
66 {
67 return $this->module;
68 }
69
70 public function read(): void
71 {
73
74 // get the settings from the cache if they exist.
75 // The setting array of the class is a reference to the cache.
76 // So changing settings in one instance will change them in all.
77 // This is the same behaviour as if the are read from the DB.
78 if (!$this->cache_disabled) {
79 if (isset(self::$settings_cache[$this->module])) {
80 $this->setting = &self::$settings_cache[$this->module];
81 return;
82 } else {
83 $this->setting = array();
84 self::$settings_cache[$this->module] = &$this->setting;
85 }
86 }
87
88 $query = "SELECT * FROM settings WHERE module=" . $ilDB->quote($this->module, "text");
89 $res = $ilDB->query($query);
90
91 while ($row = $ilDB->fetchAssoc($res)) {
92 $this->setting[$row["keyword"]] = $row["value"];
93 }
94 }
95
99 public function get(
100 string $a_keyword,
101 ?string $a_default_value = null
102 ): ?string {
103 return $this->setting[$a_keyword] ?? $a_default_value;
104 }
105
106 public function deleteAll(): void
107 {
109
110 $query = "DELETE FROM settings WHERE module = " . $ilDB->quote($this->module, "text");
111 $ilDB->manipulate($query);
112
113 $this->setting = array();
114 }
115
116 public function deleteLike(string $a_like): void
117 {
119
120 $query = "SELECT keyword FROM settings" .
121 " WHERE module = " . $ilDB->quote($this->module, "text") .
122 " AND " . $ilDB->like("keyword", "text", $a_like);
123 $res = $ilDB->query($query);
124 while ($row = $ilDB->fetchAssoc($res)) {
125 $this->delete($row["keyword"]);
126 }
127 }
128
129 public function delete(string $a_keyword): void
130 {
132
133 $ilDB->manipulate("DELETE FROM settings WHERE keyword = " .
134 $ilDB->quote($a_keyword, "text") . " AND module = " .
135 $ilDB->quote($this->module, "text"));
136
137 unset($this->setting[$a_keyword]);
138 }
139
140 public function getAll(): array
141 {
142 return $this->setting;
143 }
144
145 public function set(string $a_key, string $a_val): void
146 {
148
149 $this->delete($a_key);
150
151 if (!isset(self::$value_type)) {
152 self::$value_type = self::_getValueType();
153 }
154
155 if (self::$value_type === 'text' && strlen($a_val) >= 4000) {
156 global $DIC;
157 $DIC->ui()->mainTemplate()->setOnScreenMessage('failure', $DIC->language()->txt('setting_value_truncated'), true);
158 $a_val = substr($a_val, 0, 4000);
159 }
160
161 $ilDB->insert("settings", array(
162 "module" => array("text", $this->module),
163 "keyword" => array("text", $a_key),
164 "value" => array(self::$value_type, $a_val)));
165
166 $this->setting[$a_key] = $a_val;
167 }
168
173 public function setScormDebug(string $a_key, string $a_val): void
174 {
176 if ($a_val !== "1") {
177 $ilDB->query("UPDATE sahs_lm SET debug = 'n'");
178 }
179 $this->set($a_key, $a_val);
180 }
181
182 public static function _lookupValue(
183 string $a_module,
184 string $a_keyword
185 ): ?string {
186 global $DIC;
187
188 $ilDB = $DIC->database();
189
190 $query = "SELECT value FROM settings WHERE module = %s AND keyword = %s";
191 $res = $ilDB->queryF($query, array('text', 'text'), array($a_module, $a_keyword));
192 $data = $ilDB->fetchAssoc($res);
193 return $data['value'] ?? null;
194 }
195
200 public static function _getValueType(): string
201 {
202 $analyzer = new ilDBAnalyzer();
203 $info = $analyzer->getFieldInformation('settings');
204
205 if ($info['value']['type'] === 'clob') {
206 return 'clob';
207 } else {
208 return 'text';
209 }
210 }
211
218 public static function _changeValueType(
219 string $a_new_type = 'text'
220 ): bool {
221 global $DIC;
222
223 $ilDB = $DIC->database();
224
225 $old_type = self::_getValueType();
226
227 if ($a_new_type === $old_type) {
228 return false;
229 }
230 if ($a_new_type === 'clob') {
231 $ilDB->addTableColumn(
232 'settings',
233 'value2',
234 array( "type" => "clob",
235 "notnull" => false,
236 "default" => null)
237 );
238
239 $ilDB->query("UPDATE settings SET value2 = value");
240 $ilDB->dropTableColumn('settings', 'value');
241 $ilDB->renameTableColumn('settings', 'value2', 'value');
242
243 return true;
244 }
245 if ($a_new_type === 'text') {
246 $ilDB->addTableColumn(
247 'settings',
248 'value2',
249 array( "type" => "text",
250 "length" => 4000,
251 "notnull" => false,
252 "default" => null)
253 );
254
255 $ilDB->query("UPDATE settings SET value2 = value");
256 $ilDB->dropTableColumn('settings', 'value');
257 $ilDB->renameTableColumn('settings', 'value2', 'value');
258
259 return true;
260 }
261 return false;
262 }
263
264
268 public static function _getLongerSettings(
269 int $a_limit = 4000
270 ): array {
271 global $DIC;
272
273 $ilDB = $DIC->database();
274
275 $settings = array();
276
277 $query = "SELECT * FROM settings WHERE LENGTH(value) > "
278 . $ilDB->quote($a_limit, 'integer');
279
280 $result = $ilDB->query($query);
281
282 while ($row = $ilDB->fetchAssoc($result)) {
283 $settings[] = $row;
284 }
285
286 return $settings;
287 }
288}
This class gives all kind of DB information using the database manager and reverse module.
ILIAS Setting Class.
static _changeValueType(string $a_new_type='text')
change the type of the value column in the database
static array $settings_cache
cache for the read settings ilSetting is instantiated more than once per request for some modules The...
static _getLongerSettings(int $a_limit=4000)
get a list of setting records with values loger than a limit
static _getValueType()
Get the type of the value column in the database.
string $module
bool $cache_disabled
setScormDebug(string $a_key, string $a_val)
static string $value_type
the type of settings value field in database This is determined in the set method to get a correct DB...
ilDBInterface $db
deleteLike(string $a_like)
static _lookupValue(string $a_module, string $a_keyword)
__construct(string $a_module="common", bool $a_disabled_cache=false)
$info
Definition: entry_point.php:21
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26