* @return void
*/
public static function load($uid, $family) {
- global $a;
+ $a = get_app();
$r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
dbesc($family),
intval($uid)
);
- if (count($r)) {
+ if (dbm::is_result($r)) {
foreach ($r as $rr) {
$k = $rr['k'];
$a->config[$uid][$family][$k] = $rr['v'];
*/
public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
- global $a;
+ $a = get_app();
if (!$refresh) {
// Looking if the whole family isn't set
* The value to store
* @return mixed Stored $value or false
*/
- public static function set($uid,$family,$key,$value) {
+ public static function set($uid, $family, $key, $value) {
- global $a;
+ $a = get_app();
+
+ $stored = self::get($uid, $family, $key);
+
+ if ($stored == $value) {
+ return true;
+ }
// manage array value
$dbvalue = (is_array($value) ? serialize($value):$value);
$a->config[$uid][$family][$key] = $value;
- // The "INSERT" command is very cost intense. It saves performance to do it this way.
- $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
- intval($uid),
- dbesc($family),
- dbesc($key)
- );
-
- // It would be better to use the dbm class.
- // My lacking knowdledge in autoloaders prohibits this.
- // if (!dbm::is_result($ret))
- if (!$ret)
+ if (is_null($stored)) {
$ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
intval($uid),
dbesc($family),
dbesc($dbvalue),
dbesc($dbvalue)
);
- elseif ($ret[0]['v'] != $dbvalue)
+ } else {
$ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
dbesc($dbvalue),
intval($uid),
dbesc($family),
dbesc($key)
);
+ }
- if ($ret)
+ if ($ret) {
return $value;
-
+ }
return $ret;
}
*/
public static function delete($uid,$family,$key) {
- global $a;
- if (x($a->config[$uid][$family], $key))
+ $a = get_app();
+
+ if (x($a->config[$uid][$family], $key)) {
unset($a->config[$uid][$family][$key]);
+ }
+
$ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
intval($uid),
dbesc($family),
dbesc($key)
);
+
return $ret;
}
}