Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
PreferencesTool.php
Go to the documentation of this file.
1 <?php
3 
4  var $refreshLifetime = 3600;
5  var $prefs = array();
6  var $refreshTimes = array();
7 
8  var $cachedPrefs = array();
9  var $_transientCache = array();
10 
11  public static function &getInstance(){
12  static $instance = -1;
13  if ( $instance == -1 ){
14  if ( isset($_SESSION['dataface__preferences']) ){
15  $instance = @unserialize($_SESSION['dataface__preferences']);
16  unset($instance->cachedPrefs);
17  }
18  if ( !is_a($instance, 'Dataface_PreferencesTool') ){
19  $instance = new Dataface_PreferencesTool();
20  }
21  register_shutdown_function(array(&$instance, 'save'));
22  }
23  return $instance;
24  }
25 
27  import(dirname(__FILE__).'/PreferencesTool/_createPreferencesTable.php');
29  }
30 
31  function loadPreferences($table=null){
32  if ( !isset($table) ){
34  $query =& $app->getQuery();
35  $table = $query['-table'];
36  }
37  $this->prefs[$table] = array();
38  if ( class_exists('Dataface_AuthenticationTool') ){
40  $username = $auth->getLoggedInUsername();
41  } else {
42  $username = '*';
43  }
44  $sql = "select * from `dataface__preferences` where `username` in ('*','".addslashes($username)."') and `table` in ('*','".addslashes($table)."')";
45 
46  $res = mysql_query($sql, df_db());
47  if ( !$res ){
48  $this->_createPreferencesTable();
49  $res = mysql_query($sql, df_db());
50  if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
51  }
52 
53  while ($row = mysql_fetch_assoc($res) ){
54  if ( $row['table'] == '*' ){
55  $this->prefs['*'][ $row['key'] ] = $row['value'];
56  } else {
57  $this->prefs[$row['table']][$row['record_id']][$row['key']] = $row['value'];
58  }
59  }
60 
61  @mysql_free_result($res);
62 
63  $this->refreshTimes[ $table ] = time();
64 
65 
66  }
67 
68 
69  function &getPreferences($uri){
70  if ( !isset($this->_transientCache[ $uri ]) ){
71  if ( !isset( $this->cachedPrefs[ $uri ] ) ){
72  $parts = df_parse_uri($uri);
73  if ( !isset( $this->prefs[$parts['table']] ) or ( (time() - $this->refreshLifeTime) > @$this->refreshTimes[ $parts['table'] ])){
74  $this->loadPreferences($parts['table']);
75  }
76 
77 
78  $this->cachedPrefs[ $uri ] = array();
79  if ( isset( $this->prefs['*'] ) ) $this->cachedPrefs[$uri] = array_merge($this->cachedPrefs[$uri], $this->prefs['*']);
80  if ( isset( $this->prefs[ $parts['table'] ]['*'] ) ){
81  $this->prefs[ $uri ] = array_merge( $this->cachedPrefs[$uri], $this->prefs[$parts['table']]['*']);
82  }
83  if ( isset($this->prefs[ $parts['table'] ][ $uri ]) ){
84  $this->prefs[$uri] = array_merge( $this->cachedPrefs[$uri], $this->prefs[$parts['table']][$uri]);
85 
86  }
87 
88  }
89  if ( isset( $this->cachedPrefs['*'] ) ){
90  $this->_transientCache[$uri] = $this->cachedPrefs['*'];
91  } else {
92  $this->_transientCache[$uri] = array();
93  }
94 
95  $this->_transientCache[ $uri ] = array_merge( $this->_transientCache[ $uri ], $this->cachedPrefs[ $uri ]);
96 
97  }
98  return $this->_transientCache[$uri];
99  }
100 
101  function savePreference( $uri, $key, $value, $username=null ){
102 
103  // First let's find out the username of the user who is currently logged
104  // in because we may want to do some clever cacheing/clearing of caches
105  // if we are setting the preferences for the currently logged in user.
106  $loggedInUsername = null;
107  if ( class_exists('Dataface_AuthenticationTool') ){
109  if ( $auth->isLoggedIn() ){
110  $loggedInUsername = $auth->getLoggedInUsername();
111  }
112  }
113 
114 
115  // If no user was specified, we will set the preferences for the
116  // currently logged in user.
117  if ( !isset($username) ){
118  $username = $loggedInUsername;
119  }
120 
121  // If we are setting preferences for the currently logged in user,
122  // then we will update the caches as well.
123  // We also do this for users who aren't logged in.
124  if ( ($username == $loggedInUsername) or !isset($username) ){
125  //$prefs =& $this->getPreferences($uri);
126  //$prefs[$key] = $value;
127  $this->cachedPrefs[$uri][$key] = $value;
128  $this->prefs[$uri][$key] = $value;
129  }
130 
131  $parts = df_parse_uri($uri);
132 
133  if ( $username == '*' ) {
134  // If we are making changes to all users, we should clear our
135  // own preference caches for this table.
136 
137  unset($this->cachedPrefs[$uri]);
138 
139  unset($this->prefs[$parts['table']]);
140  unset($this->prefs['*']);
141 
142  }
143 
144  if ( $uri == '*' and isset($username) ){
145  // If we are updating preferences on ALL records, then we should
146  // need to clear all caches.
147  $this->prefs = array();
148  $this->cachedPrefs = array();
149  $this->refreshTimes = array();
150  }
151 
152  if ( isset($username) ){
153 
154 
155  // First we have to delete conflicts.
156  // If we are setting a global value (ie a value for all tables)
157  // we will clear out all previous values.
158  $sql = "delete from `dataface__preferences` where `key` = '".addslashes($key)."' ";
159  if ( $uri != '*' ){
160  if ( $parts['table'] != $uri ) $sql .= " and `record_id` = '".addslashes($uri)."'";
161  else $sql .= " and `table` = '".addslashes($parts['table'])."'";
162  }
163 
164  if ( $username != '*' ){
165  $sql .= " and `username` = '".addslashes($username)."'";
166  }
167 
168  $res = mysql_query($sql, df_db());
169  if ( !$res ){
170  $this->_createPreferencesTable();
171  $res = mysql_query($sql, df_db());
172  if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
173  }
174 
175  $sql = "insert into `dataface__preferences`
176  (`table`,`record_id`,`username`,`key`,`value`) values
177  ('".addslashes($parts['table'])."','".addslashes($uri)."','".addslashes($username)."','".addslashes($key)."','".addslashes($value)."')";
178 
179  $res = mysql_query($sql, df_db());
180  if ( !$res ){
181  $this->createPreferencesTable();
182  $res = mysql_query($sql, df_db());
183  if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
184  }
185  }
186 
187 
188 
189 
190 
191  }
192 
193  function save(){
194  unset($this->_transientCache);
195  $_SESSION['dataface__preferences'] = serialize($this);
196  }
197 
198  function __wakeup(){
199  $this->_transientCache = array();
200  }
201 
202 
203 }