3 namespace Friendica\Model\Config;
7 * The Config model backend for users, which is using the general DB-model backend for user-configs
9 class PConfig extends DbaConfig
12 * Loads all configuration values and returns the loaded category as an array.
14 * @param int $uid The id of the user to load
15 * @param string|null $cat The category of the configuration values to load
17 * @return array The config array
19 * @throws \Exception In case DB calls are invalid
21 public function load(int $uid, string $cat = null)
26 $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
28 $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
31 while ($config = $this->dba->fetch($configs)) {
33 $value = $this->toConfigValue($config['v']);
35 // just save it in case it is set
37 $return[$config['cat']][$key] = $value;
40 $this->dba->close($configs);
46 * Get a particular user config variable out of the DB with the
47 * given category name ($cat) and a key ($key).
49 * Note: Boolean variables are defined as 0/1 in the database
51 * @param int $uid The id of the user to load
52 * @param string $cat The category of the configuration value
53 * @param string $key The configuration key to query
55 * @return array|string|null Stored value or null if it does not exist
57 * @throws \Exception In case DB calls are invalid
59 public function get(int $uid, string $cat, string $key)
61 if (!$this->isConnected()) {
65 $config = $this->dba->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
66 if ($this->dba->isResult($config)) {
67 $value = $this->toConfigValue($config['v']);
69 // just return it in case it is set
79 * Stores a config value ($value) in the category ($cat) under the key ($key) for a
82 * Note: Please do not store booleans - convert to 0/1 integer values!
84 * @param int $uid The id of the user to load
85 * @param string $cat The category of the configuration value
86 * @param string $key The configuration key to set
87 * @param mixed $value The value to store
89 * @return bool Operation success
91 * @throws \Exception In case DB calls are invalid
93 public function set(int $uid, string $cat, string $key, $value)
95 if (!$this->isConnected()) {
99 // We store our setting values in a string variable.
100 // So we have to do the conversion here so that the compare below works.
101 // The exception are array values.
102 $compare_value = (!is_array($value) ? (string)$value : $value);
103 $stored_value = $this->get($uid, $cat, $key);
105 if (isset($stored_value) && ($stored_value === $compare_value)) {
109 $dbvalue = $this->toDbValue($value);
111 $result = $this->dba->update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
117 * Removes the configured value of the given user.
119 * @param int $uid The id of the user to load
120 * @param string $cat The category of the configuration value
121 * @param string $key The configuration key to delete
123 * @return bool Operation success
125 * @throws \Exception In case DB calls are invalid
127 public function delete(int $uid, string $cat, string $key)
129 if (!$this->isConnected()) {
133 return $this->dba->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);