1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handles Settings in the system |
4
|
|
|
* |
5
|
|
|
* Tabel fields: id, intranet_id, user_id, setting, value, sub_id |
6
|
|
|
* Settings levels: System, Intranet, User |
7
|
|
|
* |
8
|
|
|
* @author Sune Jensen <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
require_once 'Intraface/functions.php'; |
11
|
|
|
|
12
|
|
|
class Intraface_Setting |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var object |
16
|
|
|
*/ |
17
|
|
|
private $db; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $system; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var integer |
26
|
|
|
*/ |
27
|
|
|
private $user_id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var integer |
31
|
|
|
*/ |
32
|
|
|
private $intranet_id; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $settings = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var boolean |
41
|
|
|
*/ |
42
|
|
|
protected $is_loaded = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Checks whether setting is in system |
46
|
|
|
* |
47
|
|
|
* @param integer $intranet_id |
48
|
|
|
* @param integer $user_id |
49
|
|
|
* |
50
|
|
|
* @return void |
|
|
|
|
51
|
|
|
*/ |
52
|
14 |
|
function __construct($intranet_id, $user_id = 0) |
53
|
|
|
{ |
54
|
14 |
|
global $_setting; |
|
|
|
|
55
|
|
|
|
56
|
14 |
|
require_once dirname(__FILE__) . '/config/setting_kernel.php'; |
57
|
|
|
|
58
|
14 |
|
$this->db = new DB_Sql; |
59
|
14 |
|
$this->system = &$_setting; // don't remove the & - otherwise it will not work |
60
|
14 |
|
$this->user_id = (int)$user_id; |
61
|
14 |
|
$this->intranet_id = (int)$intranet_id; |
62
|
14 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Checks whether setting is in system |
66
|
|
|
* |
67
|
|
|
* @param string $setting to test |
68
|
|
|
* |
69
|
|
|
* @return boolean or throws exception |
70
|
|
|
*/ |
71
|
13 |
|
private function checkSystem($setting) |
72
|
|
|
{ |
73
|
13 |
|
if (!empty($setting) && is_array($this->system) && isset($this->system[$setting])) { |
74
|
12 |
|
return true; |
75
|
|
|
} else { |
76
|
1 |
|
throw new Exception('Setting "'.$setting.'" is not defined'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Checks whether type is a valid type |
82
|
|
|
* |
83
|
|
|
* @param string $type to test |
84
|
|
|
* |
85
|
|
|
* @return boolean or throws exception |
86
|
|
|
*/ |
87
|
12 |
|
private function checkType($type) |
88
|
|
|
{ |
89
|
12 |
|
if ($type == 'system' || $type == 'intranet' || $type == 'user') { |
90
|
12 |
|
return true; |
91
|
|
|
} else { |
92
|
|
|
throw new Exception('Ugyldig type setting "'.$type.'"'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks whether the user is logged in |
98
|
|
|
* |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
12 |
|
private function checkLogin() |
102
|
|
|
{ |
103
|
12 |
|
if ($this->user_id != 0) { |
104
|
12 |
|
return true; |
105
|
|
|
} else { |
106
|
|
|
throw new Exception('This action cannot be performed when no user has logged in'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets a certain setting |
112
|
|
|
* |
113
|
|
|
* @access protected, however to be tested we kept it public |
114
|
|
|
* |
115
|
|
|
* @param string $type Can be either system, intranet, user |
116
|
|
|
* @param string $setting The actual setting |
117
|
|
|
* @param integer $sub_id @todo What is this exactly |
118
|
|
|
* |
119
|
|
|
* @return boolean |
120
|
|
|
*/ |
121
|
12 |
|
function set($type, $setting, $value, $sub_id = 0) |
122
|
|
|
{ |
123
|
|
|
|
124
|
12 |
|
if ($this->checkSystem($setting) && $this->checkType($type) && $this->checkLogin()) { |
125
|
|
|
switch ($type) { |
126
|
11 |
|
case 'system': |
127
|
1 |
|
throw new Exception('Du kan ikke ændre på systemsetting'); |
128
|
|
|
break; |
|
|
|
|
129
|
10 |
|
case 'intranet': |
130
|
4 |
|
$this->db->query("SELECT id FROM setting WHERE setting = ".$this->db->quote($setting, 'text')." AND intranet_id = ".$this->intranet_id." AND user_id = 0 AND sub_id = ".intval($sub_id)); |
131
|
4 |
View Code Duplication |
if ($this->db->nextRecord()) { |
132
|
1 |
|
$this->db->query("UPDATE setting SET value = ".$this->db->quote($value, 'text')." WHERE id = ".$this->db->quote($this->db->f("id"), 'integer')); |
133
|
1 |
|
} else { |
134
|
4 |
|
$this->db->query("INSERT INTO setting SET value = ".$this->db->quote($value, 'text').", setting = ".$this->db->quote($setting, 'text').", intranet_id = ".$this->db->quote($this->intranet_id, 'integer').", user_id = 0, sub_id = ".intval($sub_id)); |
135
|
|
|
} |
136
|
4 |
|
$this->settings[$this->intranet_id][0][$setting][$sub_id] = $value; |
137
|
4 |
|
break; |
138
|
9 |
|
case 'user': |
139
|
9 |
|
if ($this->checkSystem($setting)) { |
140
|
9 |
|
$this->db->query("SELECT id FROM setting WHERE setting = ".$this->db->quote($setting, 'text')." AND intranet_id = ".$this->db->quote($this->intranet_id, 'integer')." AND user_id = ".$this->db->quote($this->user_id, 'integer')." AND sub_id = ".intval($sub_id)); |
141
|
9 |
View Code Duplication |
if ($this->db->nextRecord()) { |
142
|
1 |
|
$this->db->query("UPDATE setting SET value = ".$this->db->quote($value, 'text')." WHERE id = ".$this->db->quote($this->db->f("id"), 'integer')); |
143
|
1 |
|
} else { |
144
|
9 |
|
$this->db->query("INSERT INTO setting SET value = ".$this->db->quote($value, 'text').", setting = ".$this->db->quote($setting, 'text').", intranet_id = ".$this->intranet_id.", user_id = ".$this->db->quote($this->user_id, 'integer').", sub_id = ".intval($sub_id)); |
145
|
|
|
} |
146
|
9 |
|
} |
147
|
9 |
|
$this->settings[$this->intranet_id][$this->user_id][$setting][$sub_id] = $value; |
148
|
9 |
|
break; |
149
|
|
|
} |
150
|
10 |
|
return true; |
151
|
|
|
} |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Checks whether a setting is set |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
1 |
|
public function getSettings() |
161
|
|
|
{ |
162
|
1 |
|
return $this->settings; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Loads settings |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
7 |
|
private function loadSettings() |
171
|
|
|
{ |
172
|
7 |
|
$this->settings = array(); |
173
|
7 |
|
$this->db->query("SELECT setting, value, sub_id, user_id FROM setting WHERE intranet_id = " . $this->db->quote($this->intranet_id, 'integer')." AND (user_id = ".$this->db->quote($this->user_id, 'integer')." OR user_id = 0)"); |
174
|
7 |
|
while ($this->db->nextRecord()) { |
175
|
6 |
|
$this->settings[$this->intranet_id][$this->db->f('user_id')][$this->db->f('setting')][$this->db->f('sub_id')] = $this->db->f('value'); |
176
|
6 |
|
} |
177
|
7 |
|
$this->is_loaded = true; |
178
|
7 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns whether the settings has already been loaded |
182
|
|
|
* |
183
|
|
|
* @return boolean |
184
|
|
|
*/ |
185
|
7 |
|
private function isLoaded() |
186
|
|
|
{ |
187
|
7 |
|
return $this->is_loaded; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets a certain setting |
192
|
|
|
* |
193
|
|
|
* @access protected, however to be tested we kept it public |
194
|
|
|
* |
195
|
|
|
* @param string $type Can be either system, intranet, user |
196
|
|
|
* @param string $setting The actual setting |
197
|
|
|
* @param integer $sub_id @todo What is this exactly |
198
|
|
|
* |
199
|
|
|
* @return boolean |
200
|
|
|
*/ |
201
|
7 |
|
public function get($type, $setting, $sub_id = 0) |
202
|
|
|
{ |
203
|
7 |
|
if (!$this->isLoaded()) { |
204
|
6 |
|
$this->loadSettings(); |
205
|
6 |
|
} |
206
|
|
|
|
207
|
7 |
|
if ($this->checkSystem($setting) && $this->checkType($type)) { |
208
|
|
|
switch ($type) { |
209
|
7 |
|
case 'user': |
210
|
7 |
|
if ($this->checkLogin()) { |
211
|
|
|
// hvis der ikke er nogen intranet-indstillinger på posten vil den stadig |
212
|
|
|
// blive ved med at lave opslaget. Hvordan undgør vi lige det på en god og sikker måde? |
213
|
|
|
/* |
|
|
|
|
214
|
|
|
if (!isset($this->settings['user'])) { |
215
|
|
|
$this->settings['user'] = array(); |
216
|
|
|
$this->db->query("SELECT setting, value, sub_id FROM setting WHERE intranet_id = ".$this->intranet_id." AND user_id = ".$this->user_id); |
217
|
|
|
while ($this->db->nextRecord()) { |
218
|
|
|
$this->settings['user'][$this->db->f('setting')][$this->db->f('sub_id')] = $this->db->f('value'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
*/ |
223
|
7 |
|
if (isset($this->settings[$this->intranet_id][$this->user_id][$setting][intval($sub_id)])) { |
224
|
5 |
|
return $this->settings[$this->intranet_id][$this->user_id][$setting][intval($sub_id)]; |
225
|
|
|
} |
226
|
2 |
|
} |
227
|
|
|
// no break because it has to fall through if user is not set |
228
|
5 |
|
case 'intranet': |
229
|
|
|
// hvis der ikke er nogen intranet-indstillinger p� posten vil den stadig |
230
|
|
|
// blive ved med at lave opslaget. Hvordan undg�r vi lige det. |
231
|
|
|
/* |
|
|
|
|
232
|
|
|
if (!isset($this->settings['intranet'])) { |
233
|
|
|
$this->settings['intranet'] = array(); |
234
|
|
|
$this->db->query("SELECT setting, value, sub_id FROM setting WHERE intranet_id = ".$this->intranet_id." AND user_id = 0"); |
235
|
|
|
|
236
|
|
|
while ($this->db->nextRecord()) { |
237
|
|
|
$this->settings['intranet'][$this->db->f('setting')][intval($this->db->f('sub_id'))] = $this->db->f('value'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
*/ |
242
|
5 |
|
if (isset($this->settings[$this->intranet_id][0][$setting][intval($sub_id)])) { |
243
|
4 |
|
return $this->settings[$this->intranet_id][0][$setting][intval($sub_id)]; |
244
|
|
|
} |
245
|
|
|
// no break because it has to fall through if intranet is not set |
246
|
3 |
|
default: |
247
|
3 |
|
return $this->system[$setting]; |
248
|
|
|
break; |
|
|
|
|
249
|
2 |
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Checks whether a setting is set |
255
|
|
|
* |
256
|
|
|
* @access protected, however to be tested we kept it public |
257
|
|
|
* |
258
|
|
|
* @param string $type Can be either system, intranet, user |
259
|
|
|
* @param string $setting The actual setting |
260
|
|
|
* @param integer $sub_id @todo What is this exactly |
261
|
|
|
* |
262
|
|
|
* @return boolean |
263
|
|
|
*/ |
264
|
3 |
|
function isSettingSet($type, $setting, $sub_id = 0) |
265
|
|
|
{ |
266
|
3 |
|
if ($this->checkSystem($setting) && $this->checkType($type)) { |
267
|
|
|
switch ($type) { |
268
|
3 |
|
case 'user': |
269
|
3 |
|
if ($this->checkLogin()) { |
270
|
3 |
|
$this->db->query("SELECT value FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." AND user_id = ".$this->user_id." AND sub_id = ".intval($sub_id)); |
271
|
3 |
|
if ($this->db->nextRecord()) { |
272
|
1 |
|
return true; |
273
|
|
|
} |
274
|
2 |
|
} |
275
|
2 |
|
break; |
276
|
|
|
|
277
|
|
|
case 'intranet': |
278
|
|
|
$this->db->query("SELECT value FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." AND user_id = 0 AND sub_id = ".intval($sub_id)); |
279
|
|
|
if ($this->db->nextRecord()) { |
280
|
|
|
return true; |
281
|
|
|
} |
282
|
|
|
break; |
283
|
|
|
|
284
|
|
|
default: |
285
|
|
|
return true; |
286
|
|
|
break; |
|
|
|
|
287
|
|
|
} |
288
|
2 |
|
} |
289
|
2 |
|
return false; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Deletes a setting |
294
|
|
|
* |
295
|
|
|
* @access protected, however to be tested we kept it public |
296
|
|
|
* |
297
|
|
|
* @param string $type Can be either system, intranet, user |
298
|
|
|
* @param string $setting The actual setting |
299
|
|
|
* @param integer $sub_id @todo What is this exactly |
300
|
|
|
* |
301
|
|
|
* @return boolean |
302
|
|
|
*/ |
303
|
1 |
|
function delete($type, $setting, $sub_id = 0) |
304
|
|
|
{ |
305
|
|
|
|
306
|
1 |
|
if ($this->checkSystem($setting) && $this->checkType($type) && $this->checkLogin()) { |
307
|
1 |
|
if ($sub_id == 'ALL') { |
308
|
1 |
|
$sql_sub = ''; |
309
|
1 |
|
} else { |
310
|
|
|
$sql_sub = "AND sub_id = ".intval($sub_id); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
switch ($type) { |
314
|
1 |
View Code Duplication |
case 'user': |
315
|
1 |
|
$this->db->query("DELETE FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." AND user_id = ".$this->user_id." ".$sql_sub); |
316
|
1 |
|
$return = true; |
317
|
1 |
|
break; |
318
|
|
|
|
319
|
|
View Code Duplication |
case 'intranet': |
320
|
|
|
$this->db->query("DELETE FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." ".$sql_sub); |
321
|
|
|
$return = true; |
322
|
|
|
break; |
323
|
|
|
|
324
|
|
|
default: |
325
|
|
|
throw new Exception('Du kan ikke slette en system setting'); |
326
|
|
|
return false; |
|
|
|
|
327
|
|
|
} |
328
|
1 |
|
} |
329
|
|
|
|
330
|
1 |
|
$this->loadSettings(); |
331
|
|
|
|
332
|
1 |
|
return $return; |
|
|
|
|
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.