]>
git.wh0rd.org - tt-rss.git/blob - classes/db/prefs.php
4 private static $instance;
7 function __construct() {
8 $this->dbh
= Db
::get();
9 $this->cache
= array();
11 if ($_SESSION["uid"]) $this->cache();
14 private function __clone() {
18 public static function get() {
19 if (self
::$instance == null)
20 self
::$instance = new self();
22 return self
::$instance;
28 $user_id = $_SESSION["uid"];
29 @$profile = $_SESSION["profile"];
32 $profile_qpart = "profile = '$profile' AND";
34 $profile_qpart = "profile IS NULL AND";
37 if (get_schema_version() < 63) $profile_qpart = "";
39 $result = db_query("SELECT
40 value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name
42 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
45 ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND
46 ttrss_prefs_types.id = type_id AND
47 owner_uid = '$user_id' AND
48 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
50 while ($line = db_fetch_assoc($result)) {
51 if ($user_id == $_SESSION["uid"]) {
52 $pref_name = $line["pref_name"];
54 $this->cache
[$pref_name]["type"] = $line["type_name"];
55 $this->cache
[$pref_name]["value"] = $line["value"];
60 function read($pref_name, $user_id = false, $die_on_error = false) {
62 $pref_name = db_escape_string($pref_name);
66 $user_id = $_SESSION["uid"];
67 @$profile = $_SESSION["profile"];
69 $user_id = sprintf("%d", $user_id);
72 if (isset($this->cache
[$pref_name])) {
73 $tuple = $this->cache
[$pref_name];
74 return $this->convert($tuple["value"], $tuple["type"]);
78 $profile_qpart = "profile = '$profile' AND";
80 $profile_qpart = "profile IS NULL AND";
83 if (get_schema_version() < 63) $profile_qpart = "";
85 $result = db_query("SELECT
86 value,ttrss_prefs_types.type_name as type_name
88 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
91 ttrss_user_prefs.pref_name = '$pref_name' AND
92 ttrss_prefs_types.id = type_id AND
93 owner_uid = '$user_id' AND
94 ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
96 if (db_num_rows($result) > 0) {
97 $value = db_fetch_result($result, 0, "value");
98 $type_name = db_fetch_result($result, 0, "type_name");
100 if ($user_id == $_SESSION["uid"]) {
101 $this->cache
[$pref_name]["type"] = $type_name;
102 $this->cache
[$pref_name]["value"] = $value;
105 return $this->convert($value, $type_name);
108 user_error("Fatal error, unknown preferences key: $pref_name", $die_on_error ? E_USER_ERROR
: E_USER_WARNING
);
113 function convert($value, $type_name) {
114 if ($type_name == "bool") {
115 return $value == "true";
116 } else if ($type_name == "integer") {
123 function write($pref_name, $value, $user_id = false, $strip_tags = true) {
124 $pref_name = db_escape_string($pref_name);
125 $value = db_escape_string($value, $strip_tags);
128 $user_id = $_SESSION["uid"];
129 @$profile = $_SESSION["profile"];
131 $user_id = sprintf("%d", $user_id);
132 $prefs_cache = false;
136 $profile_qpart = "AND profile = '$profile'";
138 $profile_qpart = "AND profile IS NULL";
141 if (get_schema_version() < 63) $profile_qpart = "";
146 if (isset($this->cache
[$pref_name])) {
147 $type_name = $this->cache
[$pref_name]["type"];
148 $current_value = $this->cache
[$pref_name]["value"];
152 $result = db_query("SELECT type_name
153 FROM ttrss_prefs,ttrss_prefs_types
154 WHERE pref_name = '$pref_name' AND type_id = ttrss_prefs_types.id");
156 if (db_num_rows($result) > 0)
157 $type_name = db_fetch_result($result, 0, "type_name");
158 } else if ($current_value == $value) {
163 if ($type_name == "bool") {
164 if ($value == "1" ||
$value == "true") {
169 } else if ($type_name == "integer") {
170 $value = sprintf("%d", $value);
173 if ($pref_name == 'USER_TIMEZONE' && $value == '') {
177 db_query("UPDATE ttrss_user_prefs SET
178 value = '$value' WHERE pref_name = '$pref_name'
180 AND owner_uid = " . $_SESSION["uid"]);
182 if ($user_id == $_SESSION["uid"]) {
183 $this->cache
[$pref_name]["type"] = $type_name;
184 $this->cache
[$pref_name]["value"] = $value;