ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 private $has_stored_entry = false;
37
44 public function __construct($a_id, $a_user_id = null)
45 {
46 global $DIC;
47
48 $ilDB = $DIC['ilDB'];
49 $ilUser = $DIC['ilUser'];
50
51 $this->user_id = (int) $a_user_id;
52 $this->id = (string) $a_id;
53 $this->db = $ilDB;
54
55 if (!$this->user_id) {
56 $this->user_id = $ilUser->getId();
57 }
58
59 $this->read();
60 }
61
66 public function hasStoredEntry()
67 {
69 }
70
76 public function set($a_data)
77 {
78 $this->settings = $a_data;
79 }
80
84 public function reset()
85 {
86 $this->settings = array();
87 }
88
95 public function enabled($a_option)
96 {
97 return (bool) $this->getValue($a_option);
98 }
99
106 public function getValue($a_option)
107 {
108 if ($this->valueExists($a_option)) {
109 return $this->settings[$a_option];
110 }
111 }
112
119 public function setValue($a_option, $a_value)
120 {
121 $this->settings[$a_option] = $a_value;
122 }
123
129 public function deleteValue($a_option)
130 {
131 if ($this->valueExists($a_option)) {
132 unset($this->settings[$a_option]);
133 }
134 }
135
142 public function valueExists($a_option)
143 {
144 return array_key_exists($a_option, (array) $this->settings);
145 }
146
150 public function store()
151 {
152 $this->delete(false);
153
154 $query = "INSERT INTO usr_form_settings (user_id,id,settings) " .
155 "VALUES( " .
156 $this->db->quote($this->user_id, 'integer') . ", " .
157 $this->db->quote($this->id, 'text') . ", " .
158 $this->db->quote(serialize($this->settings), 'text') . " " .
159 ")";
160 $this->db->manipulate($query);
161 }
162
170 protected function read()
171 {
172 $query = "SELECT * FROM usr_form_settings" .
173 " WHERE user_id = " . $this->db->quote($this->user_id, 'integer') .
174 " AND id = " . $this->db->quote($this->id, 'text');
175 $res = $this->db->query($query);
176
177 if ($res->numRows()) {
178 $this->has_stored_entry = true;
179 }
180
181 $this->reset();
182 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
183 $this->settings = unserialize($row->settings);
184 }
185 return true;
186 }
187
193 public function delete($a_reset = true)
194 {
195 $query = "DELETE FROM usr_form_settings" .
196 " WHERE user_id = " . $this->db->quote($this->user_id, 'integer') .
197 " AND id = " . $this->db->quote($this->id, 'text');
198 $this->db->manipulate($query);
199
200 if ($a_reset) {
201 $this->reset();
202 }
203 }
204
208 public static function deleteAllForUser($a_user_id)
209 {
210 global $DIC;
211
212 $ilDB = $DIC['ilDB'];
213 $query = "DELETE FROM usr_form_settings" .
214 " WHERE user_id = " . $ilDB->quote($a_user_id, 'integer');
215 $ilDB->manipulate($query);
216 }
217
222 public static function deleteAllForId($a_id)
223 {
224 $query = "DELETE FROM usr_form_settings" .
225 " WHERE id = " . $GLOBALS['DIC']['ilDB']->quote($a_id, 'text');
226 $GLOBALS['DIC']['ilDB']->manipulate($query);
227 }
228
233 public static function deleteAllForPrefix($a_prefix)
234 {
235 $query = "DELETE FROM usr_form_settings " .
236 'WHERE ' . $GLOBALS['DIC']['ilDB']->like('id', 'text', $a_prefix . '%');
237
238 $GLOBALS['DIC']['ilDB']->manipulate($query);
239 }
240
246 public function importFromForm(ilPropertyFormGUI $a_form)
247 {
248 $this->reset();
249
250 foreach ($a_form->getItems() as $item) {
251 if (method_exists($item, "getPostVar")) {
252 $field = $item->getPostVar();
253
254 if (method_exists($item, "getDate")) {
255 $value = $item->getDate();
256 if ($value && !$value->isNull()) {
257 $value = $value->get(IL_CAL_DATETIME);
258 }
259 } elseif (method_exists($item, "getChecked")) {
260 $value = $item->getChecked();
261 } elseif (method_exists($item, "getMulti") && $item->getMulti()) {
262 $value = $item->getMultiValues();
263 } elseif (method_exists($item, "getValue")) {
264 $value = $item->getValue();
265 }
266
267 $this->setValue($field, $value);
268 }
269 }
270 }
271
277 public function exportToForm(ilPropertyFormGUI $a_form, $a_set_post = false)
278 {
279 foreach ($a_form->getItems() as $item) {
280 if (method_exists($item, "getPostVar")) {
281 $field = $item->getPostVar();
282
283 if ($this->valueExists($field)) {
284 $value = $this->getValue($field);
285 if ($a_set_post) {
286 $_POST[$item->getPostVar()] = $value;
287 }
288
289 if (method_exists($item, "setDate")) {
290 $date = new ilDateTime($value, IL_CAL_DATETIME);
291 $item->setDate($date);
292 } elseif (method_exists($item, "setChecked")) {
293 $item->setChecked((bool) $value);
294 } elseif (method_exists($item, "setValue")) {
295 $item->setValue($value);
296 }
297 }
298 }
299 }
300 }
301}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
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.
static deleteAllForId($a_id)
Delete for id.
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?
exportToForm(ilPropertyFormGUI $a_form, $a_set_post=false)
Export settings from form.
hasStoredEntry()
Check if entry exist.
static deleteAllForPrefix($a_prefix)
Delete all entries for prefix.
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.
$query
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2
global $ilDB
$ilUser
Definition: imgupload.php:18
$DIC
Definition: xapitoken.php:46