ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
54  $this->user_id = $ilUser->getId();
55  }
56 
57  $this->read();
58  }
59 
64  public function hasStoredEntry()
65  {
67  }
68 
74  public function set($a_data)
75  {
76  $this->settings = $a_data;
77  }
78 
82  public function reset()
83  {
84  $this->settings = array();
85  }
86 
93  public function enabled($a_option)
94  {
95  return (bool)$this->getValue($a_option);
96  }
97 
104  public function getValue($a_option)
105  {
106  if($this->valueExists($a_option))
107  {
108  return $this->settings[$a_option];
109  }
110  }
111 
118  public function setValue($a_option,$a_value)
119  {
120  $this->settings[$a_option] = $a_value;
121  }
122 
128  public function deleteValue($a_option)
129  {
130  if($this->valueExists($a_option))
131  {
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  {
179  $this->has_stored_entry = true;
180  }
181 
182  $this->reset();
183  if($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
184  {
185  $this->settings = unserialize($row->settings);
186  }
187  return true;
188  }
189 
195  public function delete($a_reset = true)
196  {
197  $query = "DELETE FROM usr_form_settings".
198  " WHERE user_id = ".$this->db->quote($this->user_id,'integer').
199  " AND id = ".$this->db->quote($this->id,'text');
200  $this->db->manipulate($query);
201 
202  if($a_reset)
203  {
204  $this->reset();
205  }
206  }
207 
211  public static function deleteAllForUser($a_user_id)
212  {
213  global $ilDB;
214  $query = "DELETE FROM usr_form_settings".
215  " WHERE user_id = ".$ilDB->quote($a_user_id,'integer');
216  $ilDB->manipulate($query);
217  }
218 
223  public static function deleteAllForId($a_id)
224  {
225  $query = "DELETE FROM usr_form_settings".
226  " WHERE id = ".$GLOBALS['ilDB']->quote($a_id,'text');
227  $GLOBALS['ilDB']->manipulate($query);
228 
229  }
230 
235  public static function deleteAllForPrefix($a_prefix)
236  {
237  $query = "DELETE FROM usr_form_settings ".
238  'WHERE '. $GLOBALS['ilDB']->like('id', 'text', $a_prefix.'%');
239 
240  $GLOBALS['ilDB']->manipulate($query);
241 
242  }
243 
249  public function importFromForm(ilPropertyFormGUI $a_form)
250  {
251  $this->reset();
252 
253  foreach($a_form->getItems() as $item)
254  {
255  if(method_exists($item, "getPostVar"))
256  {
257  $field = $item->getPostVar();
258 
259  if(method_exists($item, "getDate"))
260  {
261  $value = $item->getDate();
262  if($value && !$value->isNull())
263  {
264  $value = $value->get(IL_CAL_DATETIME);
265  }
266  }
267  else if(method_exists($item, "getChecked"))
268  {
269  $value = $item->getChecked();
270  }
271  else if(method_exists($item, "getMulti") && $item->getMulti())
272  {
273  $value = $item->getMultiValues();
274  }
275  else if(method_exists($item, "getValue"))
276  {
277  $value = $item->getValue();
278  }
279 
280  $this->setValue($field, $value);
281  }
282  }
283  }
284 
290  public function exportToForm(ilPropertyFormGUI $a_form)
291  {
292  foreach($a_form->getItems() as $item)
293  {
294  if(method_exists($item, "getPostVar"))
295  {
296  $field = $item->getPostVar();
297 
298  if($this->valueExists($field))
299  {
300  $value = $this->getValue($field);
301 
302  if(method_exists($item, "setDate"))
303  {
304  $date = new ilDateTime($value, IL_CAL_DATETIME);
305  $item->setDate($date);
306  }
307  else if(method_exists($item, "setChecked"))
308  {
309  $item->setChecked((bool)$value);
310  }
311  else if(method_exists($item, "setValue"))
312  {
313  $item->setValue($value);
314  }
315  }
316  }
317  }
318  }
319 }
320 
321 ?>
hasStoredEntry()
Check if entry exist.
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.
Add rich text string
The name of the decorator.
setValue($a_option, $a_value)
Set value.
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.
getValue($a_option)
Get value.
reset()
Remove all settings (internally)
Date and time handling
$ilUser
Definition: imgupload.php:18
exportToForm(ilPropertyFormGUI $a_form)
Export settings from form.
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.
__construct($a_id, $a_user_id=null)
Constructor.