12 static $instance = -1;
13 if ( $instance == -1 ){
14 if ( isset($_SESSION[
'dataface__preferences']) ){
15 $instance = @unserialize($_SESSION[
'dataface__preferences']);
16 unset($instance->cachedPrefs);
18 if ( !is_a($instance,
'Dataface_PreferencesTool') ){
21 register_shutdown_function(array(&$instance,
'save'));
27 import(dirname(__FILE__).
'/PreferencesTool/_createPreferencesTable.php');
34 $query =&
$app->getQuery();
37 $this->prefs[
$table] = array();
38 if ( class_exists(
'Dataface_AuthenticationTool') ){
40 $username = $auth->getLoggedInUsername();
44 $sql =
"select * from `dataface__preferences` where `username` in ('*','".addslashes($username).
"') and `table` in ('*','".addslashes(
$table).
"')";
46 $res = mysql_query($sql, df_db());
49 $res = mysql_query($sql, df_db());
50 if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
53 while ($row = mysql_fetch_assoc($res) ){
54 if ( $row[
'table'] ==
'*' ){
55 $this->prefs[
'*'][ $row[
'key'] ] = $row[
'value'];
57 $this->prefs[$row[
'table']][$row[
'record_id']][$row[
'key']] = $row[
'value'];
61 @mysql_free_result($res);
63 $this->refreshTimes[
$table ] = time();
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'] ])){
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']][
'*']);
83 if ( isset($this->prefs[ $parts[
'table'] ][ $uri ]) ){
84 $this->prefs[$uri] = array_merge( $this->cachedPrefs[$uri], $this->prefs[$parts[
'table']][$uri]);
89 if ( isset( $this->cachedPrefs[
'*'] ) ){
90 $this->_transientCache[$uri] = $this->cachedPrefs[
'*'];
92 $this->_transientCache[$uri] = array();
95 $this->_transientCache[ $uri ] = array_merge( $this->_transientCache[ $uri ], $this->cachedPrefs[ $uri ]);
98 return $this->_transientCache[$uri];
106 $loggedInUsername = null;
107 if ( class_exists(
'Dataface_AuthenticationTool') ){
109 if ( $auth->isLoggedIn() ){
110 $loggedInUsername = $auth->getLoggedInUsername();
117 if ( !isset($username) ){
118 $username = $loggedInUsername;
124 if ( ($username == $loggedInUsername) or !isset($username) ){
127 $this->cachedPrefs[$uri][$key] = $value;
128 $this->prefs[$uri][$key] = $value;
131 $parts = df_parse_uri($uri);
133 if ( $username ==
'*' ) {
137 unset($this->cachedPrefs[$uri]);
139 unset($this->prefs[$parts[
'table']]);
140 unset($this->prefs[
'*']);
144 if ( $uri ==
'*' and isset($username) ){
147 $this->prefs = array();
148 $this->cachedPrefs = array();
149 $this->refreshTimes = array();
152 if ( isset($username) ){
158 $sql =
"delete from `dataface__preferences` where `key` = '".addslashes($key).
"' ";
160 if ( $parts[
'table'] != $uri ) $sql .=
" and `record_id` = '".addslashes($uri).
"'";
161 else $sql .=
" and `table` = '".addslashes($parts[
'table']).
"'";
164 if ( $username !=
'*' ){
165 $sql .=
" and `username` = '".addslashes($username).
"'";
168 $res = mysql_query($sql, df_db());
171 $res = mysql_query($sql, df_db());
172 if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
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).
"')";
179 $res = mysql_query($sql, df_db());
181 $this->createPreferencesTable();
182 $res = mysql_query($sql, df_db());
183 if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
194 unset($this->_transientCache);
195 $_SESSION[
'dataface__preferences'] = serialize($this);
199 $this->_transientCache = array();