function savePreference($uri, $key, $value, $username = null) { // First let's find out the username of the user who is currently logged // in because we may want to do some clever cacheing/clearing of caches // if we are setting the preferences for the currently logged in user. $loggedInUsername = null; if (class_exists('Dataface_AuthenticationTool')) { $auth =& Dataface_AuthenticationTool::getInstance(); if ($auth->isLoggedIn()) { $loggedInUsername = $auth->getLoggedInUsername(); } } // If no user was specified, we will set the preferences for the // currently logged in user. if (!isset($username)) { $username = $loggedInUsername; } // If we are setting preferences for the currently logged in user, // then we will update the caches as well. // We also do this for users who aren't logged in. if ($username == $loggedInUsername or !isset($username)) { //$prefs =& $this->getPreferences($uri); //$prefs[$key] = $value; $this->cachedPrefs[$uri][$key] = $value; $this->prefs[$uri][$key] = $value; } $parts = df_parse_uri($uri); if ($username == '*') { // If we are making changes to all users, we should clear our // own preference caches for this table. unset($this->cachedPrefs[$uri]); unset($this->prefs[$parts['table']]); unset($this->prefs['*']); } if ($uri == '*' and isset($username)) { // If we are updating preferences on ALL records, then we should // need to clear all caches. $this->prefs = array(); $this->cachedPrefs = array(); $this->refreshTimes = array(); } if (isset($username)) { // First we have to delete conflicts. // If we are setting a global value (ie a value for all tables) // we will clear out all previous values. $sql = "delete from `dataface__preferences` where `key` = '" . addslashes($key) . "' "; if ($uri != '*') { if ($parts['table'] != $uri) { $sql .= " and `record_id` = '" . addslashes($uri) . "'"; } else { $sql .= " and `table` = '" . addslashes($parts['table']) . "'"; } } if ($username != '*') { $sql .= " and `username` = '" . addslashes($username) . "'"; } $res = mysql_query($sql, df_db()); if (!$res) { $this->_createPreferencesTable(); $res = mysql_query($sql, df_db()); if (!$res) { trigger_error(mysql_error(df_db()), E_USER_ERROR); } } $sql = "insert into `dataface__preferences` \n\t\t\t\t(`table`,`record_id`,`username`,`key`,`value`) values\n\t\t\t\t('" . addslashes($parts['table']) . "','" . addslashes($uri) . "','" . addslashes($username) . "','" . addslashes($key) . "','" . addslashes($value) . "')"; $res = mysql_query($sql, df_db()); if (!$res) { $this->createPreferencesTable(); $res = mysql_query($sql, df_db()); if (!$res) { trigger_error(mysql_error(df_db()), E_USER_ERROR); } } } }
/** * Returns a record or record value given it's unique URI. * @param string $uri The URI of the data we wish to retrieve. * The URI must be of one of the following forms: * tablename?key1=val1&keyn=valn#fieldname * tablename?key1=val1&keyn=valn * tablename/relationshipname?key1=val1&keyn=valn&relationshipname::relatedkey=relatedval#fieldname * tablename/relationshipname?key1=val1&keyn=valn&relationshipname::relatedkey=relatedval * * Where url encoding is used as in normal HTTP urls. If a field is specified (after the '#') * * @param string $filter The name of a filter to pass the data through. This * is only applicable when a field name is specified. Possible filters * include: * strval - Returns the string value of the field. (aka stringValue, getValueAsString) * display - Returns the display value of the field. (This substitutes valuelist values) * htmlValue - Returns the html value of the field. * preview - Returns the preview value of the field (usually this limits * the length of the output and strips any HTML. * * @returns mixed Either a Dataface_Record object, a Dataface_RelatedRecord object * of a value as stored in the object. The output depends on * the input. If it receives invalid input, it will return a PEAR_Error * object. * * Example usage: * * <code> * // Get record from Users table with UserID=10 * $user =& Dataface_IO::getByID('Users?UserID=10'); * // Dataface_Record object * * // get birthdate of user with UserID=10 * $birthdate =& Dataface_IO::getByID('Users?UserID=10#birthdate'); * // array('year'=>'1978','month'=>'12','day'=>'27', ...) * * // get related record from jobs relationship of user with UserID=10 * // where the jobtitle is "cook" * $job =& Dataface_IO::getByID('Users?UserID=10&jobs::jobtitle=cook"); * // Dataface_RelatedRecord object * * // Get the employers name of the cook job * $employername = Dataface_IO::getByID('Users?UserID=10&jobs::jobtitle=cook#employername'); * // String * * // Add filter, so we get the HTML value of the bio field rather than just * // the raw value. * $bio = Dataface_IO::getByID('Users?UserID=10#bio', 'htmlValue'); * * </code> */ static function &getByID($uri, $filter = null) { if (strpos($uri, '?') === false) { return PEAR::raiseError("Invalid record id: " . $uri); } $uri_parts = df_parse_uri($uri); if (PEAR::isError($uri_parts)) { return $uri_parts; } if (!isset($uri_parts['relationship'])) { // This is just requesting a normal record. // Check to see if this is to be a new record or an existing record if (@$uri_parts['action'] and $uri_parts['action'] == 'new') { $record = new Dataface_Record($uri_parts['table'], array()); $record->setValues($uri_parts['query']); return $record; } foreach ($uri_parts['query'] as $ukey => $uval) { if ($uval and $uval[0] != '=') { $uval = '=' . $uval; } $uri_parts['query'][$ukey] = $uval; } // At this point we are sure that this is requesting an existing record $record =& df_get_record($uri_parts['table'], $uri_parts['query']); if (isset($uri_parts['field'])) { if (isset($filter) and method_exists($record, $filter)) { $val =& $record->{$filter}($uri_parts['field']); return $val; } else { $val =& $record->val($uri_parts['field']); return $val; } } else { return $record; } } else { // This is requesting a related record. $record =& df_get_record($uri_parts['table'], $uri_parts['query']); if (!$record) { return PEAR::raiseError("Could not find any records matching the query"); } // Check to see if we are creating a new record if (@$uri_parts['action'] and $uri_parts['action'] == 'new') { $related_record = new Dataface_RelatedRecord($record, $uri_parts['relationship']); $related_record->setValues($uri_parts['query']); return $related_record; } // At this point we can be sure that we are requesting an existing record. $related_records =& $record->getRelatedRecordObjects($uri_parts['relationship'], 0, 1, $uri_parts['related_where']); if (count($related_records) == 0) { return PEAR::raiseError("Could not find any related records matching the query: " . $uri_parts['related_where']); } if (isset($uri_parts['field'])) { if (isset($filter) and method_exists($related_records[0], $filter)) { $val =& $related_records[0]->{$filter}($uri_parts['field']); return $val; } else { $val =& $related_records[0]->val($uri_parts['field']); return $val; } } else { return $related_records[0]; } } }