![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 class Dataface_PreferencesTool { 00003 00004 var $refreshLifetime = 3600; 00005 var $prefs = array(); 00006 var $refreshTimes = array(); 00007 00008 var $cachedPrefs = array(); 00009 var $_transientCache = array(); 00010 00011 public static function &getInstance(){ 00012 static $instance = -1; 00013 if ( $instance == -1 ){ 00014 if ( isset($_SESSION['dataface__preferences']) ){ 00015 $instance = @unserialize($_SESSION['dataface__preferences']); 00016 unset($instance->cachedPrefs); 00017 } 00018 if ( !is_a($instance, 'Dataface_PreferencesTool') ){ 00019 $instance = new Dataface_PreferencesTool(); 00020 } 00021 register_shutdown_function(array(&$instance, 'save')); 00022 } 00023 return $instance; 00024 } 00025 00026 function _createPreferencesTable(){ 00027 import(dirname(__FILE__).'/PreferencesTool/_createPreferencesTable.php'); 00028 return Dataface_PreferencesTool__createPreferencesTable(); 00029 } 00030 00031 function loadPreferences($table=null){ 00032 if ( !isset($table) ){ 00033 $app =& Dataface_Application::getInstance(); 00034 $query =& $app->getQuery(); 00035 $table = $query['-table']; 00036 } 00037 $this->prefs[$table] = array(); 00038 if ( class_exists('Dataface_AuthenticationTool') ){ 00039 $auth =& Dataface_AuthenticationTool::getInstance(); 00040 $username = $auth->getLoggedInUsername(); 00041 } else { 00042 $username = '*'; 00043 } 00044 $sql = "select * from `dataface__preferences` where `username` in ('*','".addslashes($username)."') and `table` in ('*','".addslashes($table)."')"; 00045 00046 $res = mysql_query($sql, df_db()); 00047 if ( !$res ){ 00048 $this->_createPreferencesTable(); 00049 $res = mysql_query($sql, df_db()); 00050 if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR); 00051 } 00052 00053 while ($row = mysql_fetch_assoc($res) ){ 00054 if ( $row['table'] == '*' ){ 00055 $this->prefs['*'][ $row['key'] ] = $row['value']; 00056 } else { 00057 $this->prefs[$row['table']][$row['record_id']][$row['key']] = $row['value']; 00058 } 00059 } 00060 00061 @mysql_free_result($res); 00062 00063 $this->refreshTimes[ $table ] = time(); 00064 00065 00066 } 00067 00068 00069 function &getPreferences($uri){ 00070 if ( !isset($this->_transientCache[ $uri ]) ){ 00071 if ( !isset( $this->cachedPrefs[ $uri ] ) ){ 00072 $parts = df_parse_uri($uri); 00073 if ( !isset( $this->prefs[$parts['table']] ) or ( (time() - $this->refreshLifeTime) > @$this->refreshTimes[ $parts['table'] ])){ 00074 $this->loadPreferences($parts['table']); 00075 } 00076 00077 00078 $this->cachedPrefs[ $uri ] = array(); 00079 if ( isset( $this->prefs['*'] ) ) $this->cachedPrefs[$uri] = array_merge($this->cachedPrefs[$uri], $this->prefs['*']); 00080 if ( isset( $this->prefs[ $parts['table'] ]['*'] ) ){ 00081 $this->prefs[ $uri ] = array_merge( $this->cachedPrefs[$uri], $this->prefs[$parts['table']]['*']); 00082 } 00083 if ( isset($this->prefs[ $parts['table'] ][ $uri ]) ){ 00084 $this->prefs[$uri] = array_merge( $this->cachedPrefs[$uri], $this->prefs[$parts['table']][$uri]); 00085 00086 } 00087 00088 } 00089 if ( isset( $this->cachedPrefs['*'] ) ){ 00090 $this->_transientCache[$uri] = $this->cachedPrefs['*']; 00091 } else { 00092 $this->_transientCache[$uri] = array(); 00093 } 00094 00095 $this->_transientCache[ $uri ] = array_merge( $this->_transientCache[ $uri ], $this->cachedPrefs[ $uri ]); 00096 00097 } 00098 return $this->_transientCache[$uri]; 00099 } 00100 00101 function savePreference( $uri, $key, $value, $username=null ){ 00102 00103 // First let's find out the username of the user who is currently logged 00104 // in because we may want to do some clever cacheing/clearing of caches 00105 // if we are setting the preferences for the currently logged in user. 00106 $loggedInUsername = null; 00107 if ( class_exists('Dataface_AuthenticationTool') ){ 00108 $auth =& Dataface_AuthenticationTool::getInstance(); 00109 if ( $auth->isLoggedIn() ){ 00110 $loggedInUsername = $auth->getLoggedInUsername(); 00111 } 00112 } 00113 00114 00115 // If no user was specified, we will set the preferences for the 00116 // currently logged in user. 00117 if ( !isset($username) ){ 00118 $username = $loggedInUsername; 00119 } 00120 00121 // If we are setting preferences for the currently logged in user, 00122 // then we will update the caches as well. 00123 // We also do this for users who aren't logged in. 00124 if ( ($username == $loggedInUsername) or !isset($username) ){ 00125 //$prefs =& $this->getPreferences($uri); 00126 //$prefs[$key] = $value; 00127 $this->cachedPrefs[$uri][$key] = $value; 00128 $this->prefs[$uri][$key] = $value; 00129 } 00130 00131 $parts = df_parse_uri($uri); 00132 00133 if ( $username == '*' ) { 00134 // If we are making changes to all users, we should clear our 00135 // own preference caches for this table. 00136 00137 unset($this->cachedPrefs[$uri]); 00138 00139 unset($this->prefs[$parts['table']]); 00140 unset($this->prefs['*']); 00141 00142 } 00143 00144 if ( $uri == '*' and isset($username) ){ 00145 // If we are updating preferences on ALL records, then we should 00146 // need to clear all caches. 00147 $this->prefs = array(); 00148 $this->cachedPrefs = array(); 00149 $this->refreshTimes = array(); 00150 } 00151 00152 if ( isset($username) ){ 00153 00154 00155 // First we have to delete conflicts. 00156 // If we are setting a global value (ie a value for all tables) 00157 // we will clear out all previous values. 00158 $sql = "delete from `dataface__preferences` where `key` = '".addslashes($key)."' "; 00159 if ( $uri != '*' ){ 00160 if ( $parts['table'] != $uri ) $sql .= " and `record_id` = '".addslashes($uri)."'"; 00161 else $sql .= " and `table` = '".addslashes($parts['table'])."'"; 00162 } 00163 00164 if ( $username != '*' ){ 00165 $sql .= " and `username` = '".addslashes($username)."'"; 00166 } 00167 00168 $res = mysql_query($sql, df_db()); 00169 if ( !$res ){ 00170 $this->_createPreferencesTable(); 00171 $res = mysql_query($sql, df_db()); 00172 if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR); 00173 } 00174 00175 $sql = "insert into `dataface__preferences` 00176 (`table`,`record_id`,`username`,`key`,`value`) values 00177 ('".addslashes($parts['table'])."','".addslashes($uri)."','".addslashes($username)."','".addslashes($key)."','".addslashes($value)."')"; 00178 00179 $res = mysql_query($sql, df_db()); 00180 if ( !$res ){ 00181 $this->createPreferencesTable(); 00182 $res = mysql_query($sql, df_db()); 00183 if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR); 00184 } 00185 } 00186 00187 00188 00189 00190 00191 } 00192 00193 function save(){ 00194 unset($this->_transientCache); 00195 $_SESSION['dataface__preferences'] = serialize($this); 00196 } 00197 00198 function __wakeup(){ 00199 $this->_transientCache = array(); 00200 } 00201 00202 00203 }