]> git.mxchange.org Git - friendica.git/blob - src/Model/Config/PConfig.php
Refactor PConfiguration
[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('üconfig', ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
29                 }
30
31                 while ($config = $this->dba->fetch($configs)) {
32
33                         $key   = $config['k'];
34                         $value = $this->toConfigValue($config['v']);
35
36                         // just save it in case it is set
37                         if (isset($value)) {
38                                 $return[$config['cat']][$key] = $value;
39                         }
40                 }
41                 $this->dba->close($configs);
42
43                 return $return;
44         }
45
46         /**
47          * Get a particular user config variable out of the DB with the
48          * given category name ($cat) and a key ($key).
49          *
50          * Note: Boolean variables are defined as 0/1 in the database
51          *
52          * @param int         $uid The id of the user to load
53          * @param string $cat The category of the configuration value
54          * @param string $key The configuration key to query
55          *
56          * @return array|string|null Stored value or null if it does not exist
57          *
58          * @throws \Exception In case DB calls are invalid
59          */
60         public function get(int $uid, string $cat, string $key)
61         {
62                 if (!$this->isConnected()) {
63                         return null;
64                 }
65
66                 $config = $this->dba->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
67                 if ($this->dba->isResult($config)) {
68                         $value = $this->toConfigValue($config['v']);
69
70                         // just return it in case it is set
71                         if (isset($value)) {
72                                 return $value;
73                         }
74                 }
75
76                 return null;
77         }
78
79         /**
80          * Stores a config value ($value) in the category ($cat) under the key ($key) for a
81          * given user ($uid).
82          *
83          * Note: Please do not store booleans - convert to 0/1 integer values!
84          *
85          * @param int    $uid   The id of the user to load
86          * @param string $cat   The category of the configuration value
87          * @param string $key   The configuration key to set
88          * @param mixed  $value The value to store
89          *
90          * @return bool Operation success
91          *
92          * @throws \Exception In case DB calls are invalid
93          */
94         public function set(int $uid, string $cat, string $key, $value)
95         {
96                 if (!$this->isConnected()) {
97                         return false;
98                 }
99
100                 // We store our setting values in a string variable.
101                 // So we have to do the conversion here so that the compare below works.
102                 // The exception are array values.
103                 $compare_value = (!is_array($value) ? (string)$value : $value);
104                 $stored_value  = $this->get($uid, $cat, $key);
105
106                 if (isset($stored_value) && ($stored_value === $compare_value)) {
107                         return true;
108                 }
109
110                 $dbvalue = $this->toDbValue($value);
111
112                 $result = $this->dba->update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
113
114                 return $result;
115         }
116
117         /**
118          * Removes the configured value of the given user.
119          *
120          * @param int    $uid The id of the user to load
121          * @param string $cat The category of the configuration value
122          * @param string $key The configuration key to delete
123          *
124          * @return bool Operation success
125          *
126          * @throws \Exception In case DB calls are invalid
127          */
128         public function delete(int $uid, string $cat, string $key)
129         {
130                 if (!$this->isConnected()) {
131                         return false;
132                 }
133
134                 return $this->dba->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
135         }
136 }