1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Database; |
4
|
|
|
|
5
|
|
|
use DatabaseException; |
6
|
|
|
use Elgg\Database; |
7
|
|
|
use ElggCache; |
8
|
|
|
use ElggEntity; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Private settings for entities |
12
|
|
|
* |
13
|
|
|
* Private settings provide metadata like storage of settings for plugins |
14
|
|
|
* and users. |
15
|
|
|
* |
16
|
|
|
* WARNING: API IN FLUX. DO NOT USE DIRECTLY. |
17
|
|
|
* |
18
|
|
|
* @access private |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
class PrivateSettingsTable { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Database |
25
|
|
|
*/ |
26
|
|
|
protected $db; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EntityTable |
30
|
|
|
*/ |
31
|
|
|
protected $entities; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ElggCache |
35
|
|
|
*/ |
36
|
|
|
protected $cache; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param Database $db The database |
42
|
|
|
* @param EntityTable $entities Entities table |
43
|
|
|
* @param ElggCache $cache Settings cache |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Database $db, EntityTable $entities, ElggCache $cache) { |
46
|
|
|
$this->db = $db; |
47
|
|
|
$this->entities = $entities; |
48
|
|
|
$this->cache = $cache; |
49
|
27 |
|
} |
50
|
27 |
|
|
51
|
27 |
|
/** |
52
|
27 |
|
* Gets a private setting for an entity |
53
|
27 |
|
* |
54
|
27 |
|
* Plugin authors can set private data on entities. By default private |
55
|
|
|
* data will not be searched or exported. |
56
|
|
|
* |
57
|
|
|
* @param ElggEntity $entity The entity GUID |
58
|
|
|
* @param string $name The name of the setting |
59
|
|
|
* |
60
|
|
|
* @return mixed The setting value, or null if does not exist |
61
|
|
|
* @throws DatabaseException |
62
|
|
|
*/ |
63
|
|
|
public function get(ElggEntity $entity, $name) { |
64
|
|
|
$values = $this->cache->load($entity->guid); |
65
|
|
|
|
66
|
|
|
if (isset($values[$name])) { |
67
|
|
|
return $values[$name]; |
68
|
38 |
|
} |
69
|
38 |
|
|
70
|
|
|
$qb = Select::fromTable('private_settings'); |
71
|
38 |
|
$qb->select('name') |
72
|
|
|
->addSelect('value') |
73
|
|
|
->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING)) |
74
|
|
|
->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER)); |
75
|
38 |
|
|
76
|
|
|
$setting = $this->db->getDataRow($qb); |
77
|
|
|
if ($setting) { |
|
|
|
|
78
|
|
|
return $setting->value; |
79
|
38 |
|
} |
80
|
38 |
|
|
81
|
38 |
|
return null; |
82
|
38 |
|
} |
83
|
38 |
|
|
84
|
|
|
/** |
85
|
38 |
|
* Return an array of all private settings |
86
|
38 |
|
* |
87
|
18 |
|
* @param ElggEntity $entity Entity |
88
|
|
|
* |
89
|
|
|
* @return string[] empty array if no settings |
90
|
30 |
|
* @throws DatabaseException |
91
|
|
|
*/ |
92
|
|
|
public function getAllForEntity(ElggEntity $entity) { |
93
|
|
|
$values = $this->cache->load($entity->guid); |
94
|
|
|
if (isset($values)) { |
95
|
|
|
return $values; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$qb = Select::fromTable('private_settings'); |
99
|
|
|
$qb->select('name') |
100
|
|
|
->addSelect('value') |
101
|
|
|
->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER)); |
102
|
|
|
|
103
|
|
|
$result = $this->db->getData($qb); |
104
|
|
|
|
105
|
|
|
$return = []; |
106
|
|
|
|
107
|
|
|
if ($result) { |
|
|
|
|
108
|
|
|
foreach ($result as $r) { |
109
|
|
|
$return[$r->name] = $r->value; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->cache->save($entity->guid, $return); |
114
|
|
|
|
115
|
|
|
return $return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets a private setting for an entity. |
120
|
|
|
* |
121
|
|
|
* @param ElggEntity $entity Entity |
122
|
|
|
* @param string $name The name of the setting |
123
|
|
|
* @param string $value The value of the setting |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
* @throws DatabaseException |
127
|
|
|
*/ |
128
|
|
|
public function set(ElggEntity $entity, $name, $value) { |
129
|
|
|
$entity->invalidateCache(); |
130
|
|
|
|
131
|
|
|
$value_type = is_int($value) ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING; |
132
|
|
|
|
133
|
|
|
$qb = Select::fromTable('private_settings'); |
134
|
|
|
$qb->select('id') |
135
|
|
|
->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING)) |
136
|
|
|
->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER)); |
137
|
|
|
|
138
|
|
|
$row = $this->db->getDataRow($qb); |
139
|
|
|
|
140
|
|
|
if ($row) { |
|
|
|
|
141
|
124 |
|
$qb = Update::table('private_settings'); |
142
|
124 |
|
$qb->set('value', $qb->param($value, $value_type)) |
143
|
|
|
->where($qb->compare('id', '=', $row->id, ELGG_VALUE_INTEGER)); |
144
|
|
|
|
145
|
|
|
$result = $this->db->updateData($qb); |
146
|
124 |
|
} else { |
147
|
|
|
$qb = Insert::intoTable('private_settings'); |
148
|
124 |
|
$qb->values([ |
149
|
124 |
|
'entity_guid' => $qb->param($entity->guid, ELGG_VALUE_INTEGER), |
150
|
124 |
|
'name' => $qb->param($name, ELGG_VALUE_STRING), |
151
|
124 |
|
'value' => $qb->param($value, $value_type), |
152
|
|
|
]); |
153
|
124 |
|
|
154
|
|
|
$result = $this->db->insertData($qb); |
155
|
124 |
|
} |
156
|
1 |
|
|
157
|
1 |
|
return $result !== false; |
158
|
1 |
|
} |
159
|
|
|
|
160
|
1 |
|
/** |
161
|
|
|
* Deletes a private setting for an entity. |
162
|
124 |
|
* |
163
|
124 |
|
* @param ElggEntity $entity Entity |
164
|
124 |
|
* @param string $name The name of the setting |
165
|
124 |
|
* |
166
|
124 |
|
* @return bool |
167
|
|
|
* @throws DatabaseException |
168
|
|
|
*/ |
169
|
124 |
|
public function remove(ElggEntity $entity, $name) { |
170
|
|
|
$entity->invalidateCache(); |
171
|
|
|
|
172
|
124 |
|
$qb = Delete::fromTable('private_settings'); |
173
|
|
|
$qb->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING)) |
174
|
|
|
->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER)); |
175
|
|
|
|
176
|
|
|
return $this->db->deleteData($qb); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Deletes all private settings for an entity |
181
|
|
|
* |
182
|
|
|
* @param ElggEntity $entity Entity |
183
|
|
|
* |
184
|
3 |
|
* @return bool |
185
|
3 |
|
* @throws DatabaseException |
186
|
|
|
*/ |
187
|
3 |
|
public function removeAllForEntity(ElggEntity $entity) { |
188
|
3 |
|
$entity->invalidateCache(); |
189
|
3 |
|
|
190
|
|
|
$qb = Delete::fromTable('private_settings'); |
191
|
3 |
|
$qb->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER)); |
192
|
|
|
|
193
|
|
|
return $this->db->deleteData($qb); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.