ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $ilDB, $ilUser;
47 
48  $this->user_id = (int) $a_user_id;
49  $this->id = (string) $a_id;
50  $this->db = $ilDB;
51 
52  if (!$this->user_id) {
53  $this->user_id = $ilUser->getId();
54  }
55 
56  $this->read();
57  }
58 
63  public function hasStoredEntry()
64  {
66  }
67 
73  public function set($a_data)
74  {
75  $this->settings = $a_data;
76  }
77 
81  public function reset()
82  {
83  $this->settings = array();
84  }
85 
92  public function enabled($a_option)
93  {
94  return (bool) $this->getValue($a_option);
95  }
96 
103  public function getValue($a_option)
104  {
105  if ($this->valueExists($a_option)) {
106  return $this->settings[$a_option];
107  }
108  }
109 
116  public function setValue($a_option, $a_value)
117  {
118  $this->settings[$a_option] = $a_value;
119  }
120 
126  public function deleteValue($a_option)
127  {
128  if ($this->valueExists($a_option)) {
129  unset($this->settings[$a_option]);
130  }
131  }
132 
139  public function valueExists($a_option)
140  {
141  return array_key_exists($a_option, (array) $this->settings);
142  }
143 
147  public function store()
148  {
149  $this->delete(false);
150 
151  $query = "INSERT INTO usr_form_settings (user_id,id,settings) " .
152  "VALUES( " .
153  $this->db->quote($this->user_id, 'integer') . ", " .
154  $this->db->quote($this->id, 'text') . ", " .
155  $this->db->quote(serialize($this->settings), 'text') . " " .
156  ")";
157  $this->db->manipulate($query);
158  }
159 
167  protected function read()
168  {
169  $query = "SELECT * FROM usr_form_settings" .
170  " WHERE user_id = " . $this->db->quote($this->user_id, 'integer') .
171  " AND id = " . $this->db->quote($this->id, 'text');
172  $res = $this->db->query($query);
173 
174  if ($res->numRows()) {
175  $this->has_stored_entry = true;
176  }
177 
178  $this->reset();
179  if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
180  $this->settings = unserialize($row->settings);
181  }
182  return true;
183  }
184 
190  public function delete($a_reset = true)
191  {
192  $query = "DELETE FROM usr_form_settings" .
193  " WHERE user_id = " . $this->db->quote($this->user_id, 'integer') .
194  " AND id = " . $this->db->quote($this->id, 'text');
195  $this->db->manipulate($query);
196 
197  if ($a_reset) {
198  $this->reset();
199  }
200  }
201 
205  public static function deleteAllForUser($a_user_id)
206  {
207  global $ilDB;
208  $query = "DELETE FROM usr_form_settings" .
209  " WHERE user_id = " . $ilDB->quote($a_user_id, 'integer');
210  $ilDB->manipulate($query);
211  }
212 
217  public static function deleteAllForId($a_id)
218  {
219  $query = "DELETE FROM usr_form_settings" .
220  " WHERE id = " . $GLOBALS['ilDB']->quote($a_id, 'text');
221  $GLOBALS['ilDB']->manipulate($query);
222  }
223 
228  public static function deleteAllForPrefix($a_prefix)
229  {
230  $query = "DELETE FROM usr_form_settings " .
231  'WHERE ' . $GLOBALS['ilDB']->like('id', 'text', $a_prefix . '%');
232 
233  $GLOBALS['ilDB']->manipulate($query);
234  }
235 
241  public function importFromForm(ilPropertyFormGUI $a_form)
242  {
243  $this->reset();
244 
245  foreach ($a_form->getItems() as $item) {
246  if (method_exists($item, "getPostVar")) {
247  $field = $item->getPostVar();
248 
249  if (method_exists($item, "getDate")) {
250  $value = $item->getDate();
251  if ($value && !$value->isNull()) {
252  $value = $value->get(IL_CAL_DATETIME);
253  }
254  } elseif (method_exists($item, "getChecked")) {
255  $value = $item->getChecked();
256  } elseif (method_exists($item, "getMulti") && $item->getMulti()) {
257  $value = $item->getMultiValues();
258  } elseif (method_exists($item, "getValue")) {
259  $value = $item->getValue();
260  }
261 
262  $this->setValue($field, $value);
263  }
264  }
265  }
266 
272  public function exportToForm(ilPropertyFormGUI $a_form, $a_set_post = false)
273  {
274  foreach ($a_form->getItems() as $item) {
275  if (method_exists($item, "getPostVar")) {
276  $field = $item->getPostVar();
277 
278  if ($this->valueExists($field)) {
279  $value = $this->getValue($field);
280  if ($a_set_post) {
281  $_POST[$item->getPostVar()] = $value;
282  }
283 
284  if (method_exists($item, "setDate")) {
285  $date = new ilDateTime($value, IL_CAL_DATETIME);
286  $item->setDate($date);
287  } elseif (method_exists($item, "setChecked")) {
288  $item->setChecked((bool) $value);
289  } elseif (method_exists($item, "setValue")) {
290  $item->setValue($value);
291  }
292  }
293  }
294  }
295  }
296 }
hasStoredEntry()
Check if entry exist.
Add rich text string
valueExists($a_option)
Does value exist in settings?
const IL_CAL_DATETIME
deleteValue($a_option)
Delete value.
This class represents a property form user interface.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
setValue($a_option, $a_value)
Set value.
exportToForm(ilPropertyFormGUI $a_form, $a_set_post=false)
Export settings from form.
static deleteAllForPrefix($a_prefix)
Delete all entries for prefix.
importFromForm(ilPropertyFormGUI $a_form)
Import settings from form.
enabled($a_option)
Check if a specific option is enabled.
store()
Store settings in DB.
foreach($_POST as $key=> $value) $res
getValue($a_option)
Get value.
reset()
Remove all settings (internally)
Date and time handling
$ilUser
Definition: imgupload.php:18
$query
Create styles array
The data for the language used.
settings()
Definition: settings.php:2
static deleteAllForId($a_id)
Delete for id.
static deleteAllForUser($a_user_id)
Delete all settings for user id.
global $ilDB
read()
Read store settings.
$_POST["username"]
__construct($a_id, $a_user_id=null)
Constructor.