ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilUserFormSettings.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
31{
32 protected $db;
33 protected $user_id;
34 protected $id;
35 protected $settings = array();
36
43 public function __construct($a_id,$a_user_id = null)
44 {
45 global $ilDB, $ilUser;
46
47 $this->user_id = (int)$a_user_id;
48 $this->id = (string)$a_id;
49 $this->db = $ilDB;
50
51 if(!$this->user_id)
52 {
53 $this->user_id = $ilUser->getId();
54 }
55
56 $this->read();
57 }
58
64 public function set($a_data)
65 {
66 $this->settings = $a_data;
67 }
68
72 public function reset()
73 {
74 $this->settings = array();
75 }
76
83 public function enabled($a_option)
84 {
85 return (bool)$this->getValue($a_option);
86 }
87
94 public function getValue($a_option)
95 {
96 if($this->valueExists($a_option))
97 {
98 return $this->settings[$a_option];
99 }
100 }
101
108 public function setValue($a_option,$a_value)
109 {
110 $this->settings[$a_option] = $a_value;
111 }
112
118 public function deleteValue($a_option)
119 {
120 if($this->valueExists($a_option))
121 {
122 unset($this->settings[$a_option]);
123 }
124 }
125
132 public function valueExists($a_option)
133 {
134 return array_key_exists($a_option,(array)$this->settings);
135 }
136
140 public function store()
141 {
142 $this->delete(false);
143
144 $query = "INSERT INTO usr_form_settings (user_id,id,settings) ".
145 "VALUES( ".
146 $this->db->quote($this->user_id,'integer').", ".
147 $this->db->quote($this->id,'text').", ".
148 $this->db->quote(serialize($this->settings),'text')." ".
149 ")";
150 $this->db->manipulate($query);
151 }
152
160 protected function read()
161 {
162 $query = "SELECT * FROM usr_form_settings".
163 " WHERE user_id = ".$this->db->quote($this->user_id,'integer').
164 " AND id = ".$this->db->quote($this->id,'text');
165 $res = $this->db->query($query);
166
167 $this->reset();
168 if($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
169 {
170 $this->settings = unserialize($row->settings);
171 }
172 return true;
173 }
174
180 public function delete($a_reset = true)
181 {
182 $query = "DELETE FROM usr_form_settings".
183 " WHERE user_id = ".$this->db->quote($this->user_id,'integer').
184 " AND id = ".$this->db->quote($this->id,'text');
185 $this->db->manipulate($query);
186
187 if($a_reset)
188 {
189 $this->reset();
190 }
191 }
192
196 public static function deleteAllForUser($a_user_id)
197 {
198 $query = "DELETE FROM usr_form_settings".
199 " WHERE user_id = ".$this->db->quote($a_user_id,'integer');
200 $this->db->manipulate($query);
201 }
202
208 public function importFromForm(ilPropertyFormGUI $a_form)
209 {
210 $this->reset();
211
212 foreach($a_form->getItems() as $item)
213 {
214 if(method_exists($item, "getPostVar"))
215 {
216 $field = $item->getPostVar();
217
218 if(method_exists($item, "getDate"))
219 {
220 $value = $item->getDate();
221 if($value && !$value->isNull())
222 {
223 $value = $value->get(IL_CAL_DATETIME);
224 }
225 }
226 else if(method_exists($item, "getChecked"))
227 {
228 $value = $item->getChecked();
229 }
230 else if(method_exists($item, "getMulti") && $item->getMulti())
231 {
232 $value = $item->getMultiValues();
233 }
234 else if(method_exists($item, "getValue"))
235 {
236 $value = $item->getValue();
237 }
238
239 $this->setValue($field, $value);
240 }
241 }
242 }
243
249 public function exportToForm(ilPropertyFormGUI $a_form)
250 {
251 foreach($a_form->getItems() as $item)
252 {
253 if(method_exists($item, "getPostVar"))
254 {
255 $field = $item->getPostVar();
256
257 if($this->valueExists($field))
258 {
259 $value = $this->getValue($field);
260
261 if(method_exists($item, "setDate"))
262 {
263 $date = new ilDateTime($value, IL_CAL_DATETIME);
264 $item->setDate($date);
265 }
266 else if(method_exists($item, "setChecked"))
267 {
268 $item->setChecked((bool)$value);
269 }
270 else if(method_exists($item, "setValue"))
271 {
272 $item->setValue($value);
273 }
274 }
275 }
276 }
277 }
278}
279
280?>
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
const IL_CAL_DATETIME
@classDescription Date and time handling
This class represents a property form user interface.
getValue($a_option)
Get value.
__construct($a_id, $a_user_id=null)
Constructor.
enabled($a_option)
Check if a specific option is enabled.
store()
Store settings in DB.
reset()
Remove all settings (internally)
valueExists($a_option)
Does value exist in settings?
setValue($a_option, $a_value)
Set value.
deleteValue($a_option)
Delete value.
importFromForm(ilPropertyFormGUI $a_form)
Import settings from form.
read()
Read store settings.
static deleteAllForUser($a_user_id)
Delete all settings for user id.
exportToForm(ilPropertyFormGUI $a_form)
Export settings from form.
global $ilDB
global $ilUser
Definition: imgupload.php:15