]> git.mxchange.org Git - friendica.git/blob - src/Model/Config/PConfig.php
Merge pull request #7540 from vinzv/patch-1
[friendica.git] / src / Model / Config / PConfig.php
1 <?php
2
3 namespace Friendica\Model\Config;
4
5
6 /**
7  * The Config model backend for users, which is using the general DB-model backend for user-configs
8  */
9 class PConfig extends DbaConfig
10 {
11         /**
12          * Loads all configuration values and returns the loaded category as an array.
13          *
14          * @param int         $uid The id of the user to load
15          * @param string|null $cat The category of the configuration values to load
16          *
17          * @return array The config array
18          *
19          * @throws \Exception In case DB calls are invalid
20          */
21         public function load(int $uid, string $cat = null)
22         {
23                 $return = [];
24
25                 if (empty($cat)) {
26                         $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
27                 } else {
28                         $configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
29                 }
30
31                 while ($config = $this->dba->fetch($configs)) {
32                         $key   = $config['k'];
33                         $value = $this->toConfigValue($config['v']);
34
35                         // just save it in case it is set
36                         if (isset($value)) {
37                                 $return[$config['cat']][$key] = $value;
38                         }
39                 }
40                 $this->dba->close($configs);
41
42                 return $return;
43         }
44
45         /**
46          * Get a particular user config variable out of the DB with the
47          * given category name ($cat) and a key ($key).
48          *
49          * Note: Boolean variables are defined as 0/1 in the database
50          *
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
54          *
55          * @return array|string|null Stored value or null if it does not exist
56          *
57          * @throws \Exception In case DB calls are invalid
58          */
59         public function get(int $uid, string $cat, string $key)
60         {
61                 if (!$this->isConnected()) {
62                         return null;
63                 }
64
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']);
68
69                         // just return it in case it is set
70                         if (isset($value)) {
71                                 return $value;
72                         }
73                 }
74
75                 return null;
76         }
77
78         /**
79          * Stores a config value ($value) in the category ($cat) under the key ($key) for a
80          * given user ($uid).
81          *
82          * Note: Please do not store booleans - convert to 0/1 integer values!
83          *
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
88          *
89          * @return bool Operation success
90          *
91          * @throws \Exception In case DB calls are invalid
92          */
93         public function set(int $uid, string $cat, string $key, $value)
94         {
95                 if (!$this->isConnected()) {
96                         return false;
97                 }
98
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);
104
105                 if (isset($stored_value) && ($stored_value === $compare_value)) {
106                         return true;
107                 }
108
109                 $dbvalue = $this->toDbValue($value);
110
111                 $result = $this->dba->update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
112
113                 return $result;
114         }
115
116         /**
117          * Removes the configured value of the given user.
118          *
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
122          *
123          * @return bool Operation success
124          *
125          * @throws \Exception In case DB calls are invalid
126          */
127         public function delete(int $uid, string $cat, string $key)
128         {
129                 if (!$this->isConnected()) {
130                         return false;
131                 }
132
133                 return $this->dba->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
134         }
135 }