Issues (946)

classes/Elgg/Database/PrivateSettingsTable.php (2 issues)

1
<?php
2
3
namespace Elgg\Database;
4
5
use DatabaseException;
6
use Elgg\Database;
7
use ElggEntity;
8
use Elgg\Cache\PrivateSettingsCache;
9
use Elgg\Values;
10
11
/**
12
 * Private settings for entities
13
 *
14
 * Private settings provide metadata like storage of settings for plugins
15
 * and users.
16
 *
17
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
18
 *
19
 * @internal
20
 * @since  2.0.0
21
 */
22
class PrivateSettingsTable {
23
24
	/**
25
	 * @var Database
26
	 */
27
	protected $db;
28
29
	/**
30
	 * @var EntityTable
31
	 */
32
	protected $entities;
33
34
	/**
35
	 * @var PrivateSettingsCache
36
	 */
37
	protected $cache;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param Database             $db       The database
43
	 * @param EntityTable          $entities Entities table
44
	 * @param PrivateSettingsCache $cache    Settings cache
45
	 */
46 54
	public function __construct(Database $db, EntityTable $entities, PrivateSettingsCache $cache) {
47 54
		$this->db = $db;
48 54
		$this->entities = $entities;
49 54
		$this->cache = $cache;
50 54
	}
51
52
	/**
53
	 * Gets a private setting for an entity
54
	 *
55
	 * Plugin authors can set private data on entities. By default private
56
	 * data will not be searched or exported.
57
	 *
58
	 * @param ElggEntity $entity The entity GUID
59
	 * @param string     $name   The name of the setting
60
	 *
61
	 * @return mixed The setting value, or null if does not exist
62
	 * @throws DatabaseException
63
	 */
64 187
	public function get(ElggEntity $entity, $name) {
65 187
		return elgg_extract($name, $this->getAllForEntity($entity));
66
	}
67
68
	/**
69
	 * Return an array of all private settings
70
	 *
71
	 * @param ElggEntity $entity Entity
72
	 *
73
	 * @return string[] empty array if no settings
74
	 * @throws DatabaseException
75
	 */
76 160
	public function getAllForEntity(ElggEntity $entity) {
77 160
		$values = $this->cache->load($entity->guid);
78 160
		if (isset($values)) {
79 19
			return $values;
80
		}
81
82 160
		$qb = Select::fromTable('private_settings');
83 160
		$qb->select('name')
84 160
			->addSelect('value')
85 160
			->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
86
87 160
		$result = $this->db->getData($qb);
88
89 160
		$return = [];
90
91 160
		if (!empty($result)) {
92 51
			foreach ($result as $r) {
93 51
				$return[$r->name] = $r->value;
94
			}
95
		}
96
97 160
		$this->cache->save($entity->guid, $return);
98
99 160
		return $return;
100
	}
101
102
	/**
103
	 * Return an array of all private settings for the requested guids
104
	 *
105
	 * @note this does not use cache as this is a helper function for the privatesettingscache which does the caching
106
	 *
107
	 * @see \Elgg\Cache\PrivateSettingsCache::populateFromEntities()
108
	 *
109
	 * @param int[] $guids GUIDS to fetch the settings for
110
	 *
111
	 * @return string[] array of guids and their settings
112
	 * @throws DatabaseException
113
	 *
114
	 * @internal
115
	 */
116 36
	public function getAllForGUIDs($guids) {
117 36
		$guids = Values::normalizeGuids($guids);
118
		
119 36
		if (!is_array($guids) || empty($guids)) {
120
			return [];
121
		}
122
		
123 36
		$qb = Select::fromTable('private_settings');
124 36
		$qb->select('entity_guid')
125 36
			->addSelect('name')
126 36
			->addSelect('value')
127 36
			->where($qb->compare('entity_guid', 'IN', $guids));
128
129 36
		$result = $this->db->getData($qb);
130 36
		if (empty($result)) {
131
			return [];
132
		}
133
		
134 36
		$return = [];
135
		
136 36
		foreach ($result as $r) {
137 36
			$return[$r->entity_guid][$r->name] = $r->value;
138
		}
139
	
140 36
		return $return;
141
	}
142
143
	/**
144
	 * Sets a private setting for an entity.
145
	 *
146
	 * @param ElggEntity $entity Entity
147
	 * @param string     $name   The name of the setting
148
	 * @param string|int $value  The value of the setting
149
	 *
150
	 * @return bool
151
	 * @throws DatabaseException
152
	 */
153 159
	public function set(ElggEntity $entity, $name, $value) {
154 159
		$entity->invalidateCache();
155
156 159
		$value_type = is_int($value) ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING;
157
158 159
		$qb = Select::fromTable('private_settings');
159 159
		$qb->select('id')
160 159
			->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
161 159
			->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
162
163 159
		$row = $this->db->getDataRow($qb);
164
165 159
		if (!empty($row)) {
166 3
			$qb = Update::table('private_settings');
167 3
			$qb->set('value', $qb->param($value, $value_type))
168 3
				->where($qb->compare('id', '=', $row->id, ELGG_VALUE_INTEGER));
169
170 3
			$result = $this->db->updateData($qb);
171
		} else {
172 159
			$qb = Insert::intoTable('private_settings');
173 159
			$qb->values([
174 159
				'entity_guid' => $qb->param($entity->guid, ELGG_VALUE_INTEGER),
175 159
				'name' => $qb->param($name, ELGG_VALUE_STRING),
176 159
				'value' => $qb->param($value, $value_type),
177
			]);
178
179 159
			$result = $this->db->insertData($qb);
180
		}
181
182 159
		return $result !== false;
183
	}
184
185
	/**
186
	 * Deletes a private setting for an entity.
187
	 *
188
	 * @param ElggEntity $entity Entity
189
	 * @param string     $name   The name of the setting
190
	 *
191
	 * @return bool
192
	 * @throws DatabaseException
193
	 */
194 5
	public function remove(ElggEntity $entity, $name) {
195 5
		$entity->invalidateCache();
196
197 5
		$qb = Delete::fromTable('private_settings');
198 5
		$qb->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
199 5
			->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
200
201 5
		return $this->db->deleteData($qb);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return $this->db->deleteData($qb) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
202
	}
203
204
	/**
205
	 * Deletes all private settings for an entity
206
	 *
207
	 * @param ElggEntity $entity Entity
208
	 *
209
	 * @return bool
210
	 * @throws DatabaseException
211
	 */
212 277
	public function removeAllForEntity(ElggEntity $entity) {
213 277
		$entity->invalidateCache();
214
215 277
		$qb = Delete::fromTable('private_settings');
216 277
		$qb->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
217
218 277
		return $this->db->deleteData($qb);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return $this->db->deleteData($qb) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
219
	}
220
221
}
222