]> git.wh0rd.org - tt-rss.git/blame - classes/db/prefs.php
move db-prefs to OO
[tt-rss.git] / classes / db / prefs.php
CommitLineData
52d88392
AD
1<?php
2class Db_Prefs {
3 private $dbh;
4 private static $instance;
5 private $cache;
6
7 function __construct() {
8 $this->dbh = Db::get();
9 $this->cache = array();
10
11 if ($_SESSION["uid"]) $this->cache();
12 }
13
14 private function __clone() {
15 //
16 }
17
18 public static function get() {
19 if (self::$instance == null)
20 self::$instance = new self();
21
22 return self::$instance;
23 }
24
25 function cache() {
26 $profile = false;
27
28 $user_id = $_SESSION["uid"];
29 @$profile = $_SESSION["profile"];
30
31 if ($profile) {
32 $profile_qpart = "profile = '$profile' AND";
33 } else {
34 $profile_qpart = "profile IS NULL AND";
35 }
36
37 if (get_schema_version() < 63) $profile_qpart = "";
38
39 $result = db_query("SELECT
40 value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name
41 FROM
42 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
43 WHERE
44 $profile_qpart
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");
49
50 while ($line = db_fetch_assoc($result)) {
51 if ($user_id == $_SESSION["uid"]) {
52 $pref_name = $line["pref_name"];
53
54 $this->cache[$pref_name]["type"] = $line["type_name"];
55 $this->cache[$pref_name]["value"] = $line["value"];
56 }
57 }
58 }
59
60 function read($pref_name, $user_id = false, $die_on_error = false) {
61
62 $pref_name = db_escape_string($pref_name);
63 $profile = false;
64
65 if (!$user_id) {
66 $user_id = $_SESSION["uid"];
67 @$profile = $_SESSION["profile"];
68 } else {
69 $user_id = sprintf("%d", $user_id);
70 }
71
72 if (isset($this->cache[$pref_name])) {
73 $tuple = $this->cache[$pref_name];
74 return $this->convert($tuple["value"], $tuple["type"]);
75 }
76
77 if ($profile) {
78 $profile_qpart = "profile = '$profile' AND";
79 } else {
80 $profile_qpart = "profile IS NULL AND";
81 }
82
83 if (get_schema_version() < 63) $profile_qpart = "";
84
85 $result = db_query("SELECT
86 value,ttrss_prefs_types.type_name as type_name
87 FROM
88 ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types
89 WHERE
90 $profile_qpart
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");
95
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");
99
100 if ($user_id == $_SESSION["uid"]) {
101 $this->cache[$pref_name]["type"] = $type_name;
102 $this->cache[$pref_name]["value"] = $value;
103 }
104
105 return $this->convert($value, $type_name);
106
107 } else {
108 user_error("Fatal error, unknown preferences key: $pref_name", $die_on_error ? E_USER_ERROR : E_USER_WARNING);
109 return null;
110 }
111 }
112
113 function convert($value, $type_name) {
114 if ($type_name == "bool") {
115 return $value == "true";
116 } else if ($type_name == "integer") {
117 return (int)$value;
118 } else {
119 return $value;
120 }
121 }
122
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);
126
127 if (!$user_id) {
128 $user_id = $_SESSION["uid"];
129 @$profile = $_SESSION["profile"];
130 } else {
131 $user_id = sprintf("%d", $user_id);
132 $prefs_cache = false;
133 }
134
135 if ($profile) {
136 $profile_qpart = "AND profile = '$profile'";
137 } else {
138 $profile_qpart = "AND profile IS NULL";
139 }
140
141 if (get_schema_version() < 63) $profile_qpart = "";
142
143 $type_name = "";
144 $current_value = "";
145
146 if (isset($this->cache[$pref_name])) {
147 $type_name = $this->cache[$pref_name]["type"];
148 $current_value = $this->cache[$pref_name]["value"];
149 }
150
151 if (!$type_name) {
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");
155
156 if (db_num_rows($result) > 0)
157 $type_name = db_fetch_result($result, 0, "type_name");
158 } else if ($current_value == $value) {
159 return;
160 }
161
162 if ($type_name) {
163 if ($type_name == "bool") {
164 if ($value == "1" || $value == "true") {
165 $value = "true";
166 } else {
167 $value = "false";
168 }
169 } else if ($type_name == "integer") {
170 $value = sprintf("%d", $value);
171 }
172
173 if ($pref_name == 'USER_TIMEZONE' && $value == '') {
174 $value = 'UTC';
175 }
176
177 db_query("UPDATE ttrss_user_prefs SET
178 value = '$value' WHERE pref_name = '$pref_name'
179 $profile_qpart
180 AND owner_uid = " . $_SESSION["uid"]);
181
182 if ($user_id == $_SESSION["uid"]) {
183 $this->cache[$pref_name]["type"] = $type_name;
184 $this->cache[$pref_name]["value"] = $value;
185 }
186 }
187 }
188
189}
190?>