3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model\Config;
26 * The Config model backend for users, which is using the general DB-model backend for user-configs
28 class PConfig extends DbaConfig
31 * Loads all configuration values and returns the loaded category as an array.
33 * @param int $uid The id of the user to load
34 * @param string|null $cat The category of the configuration values to load
36 * @return array The config array
38 * @throws \Exception In case DB calls are invalid
40 public function load(int $uid, string $cat = null)
45 $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
47 $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
50 while ($config = $this->dba->fetch($configs)) {
52 $value = $this->toConfigValue($config['v']);
54 // just save it in case it is set
56 $return[$config['cat']][$key] = $value;
59 $this->dba->close($configs);
65 * Get a particular user config variable out of the DB with the
66 * given category name ($cat) and a key ($key).
68 * Note: Boolean variables are defined as 0/1 in the database
70 * @param int $uid The id of the user to load
71 * @param string $cat The category of the configuration value
72 * @param string $key The configuration key to query
74 * @return array|string|null Stored value or null if it does not exist
76 * @throws \Exception In case DB calls are invalid
78 public function get(int $uid, string $cat, string $key)
80 if (!$this->isConnected()) {
84 $config = $this->dba->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
85 if ($this->dba->isResult($config)) {
86 $value = $this->toConfigValue($config['v']);
88 // just return it in case it is set
98 * Stores a config value ($value) in the category ($cat) under the key ($key) for a
101 * Note: Please do not store booleans - convert to 0/1 integer values!
103 * @param int $uid The id of the user to load
104 * @param string $cat The category of the configuration value
105 * @param string $key The configuration key to set
106 * @param mixed $value The value to store
108 * @return bool Operation success
110 * @throws \Exception In case DB calls are invalid
112 public function set(int $uid, string $cat, string $key, $value)
114 if (!$this->isConnected()) {
118 // We store our setting values in a string variable.
119 // So we have to do the conversion here so that the compare below works.
120 // The exception are array values.
121 $compare_value = (!is_array($value) ? (string)$value : $value);
122 $stored_value = $this->get($uid, $cat, $key);
124 if (isset($stored_value) && ($stored_value === $compare_value)) {
128 $dbvalue = $this->toDbValue($value);
130 $result = $this->dba->update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
136 * Removes the configured value of the given user.
138 * @param int $uid The id of the user to load
139 * @param string $cat The category of the configuration value
140 * @param string $key The configuration key to delete
142 * @return bool Operation success
144 * @throws \Exception In case DB calls are invalid
146 public function delete(int $uid, string $cat, string $key)
148 if (!$this->isConnected()) {
152 return $this->dba->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);