Completed
Push — 3.0 ( a46cca...861f1c )
by Jeroen
204:32 queued 99:44
created

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 43
	public function __construct(Database $db, EntityTable $entities, PrivateSettingsCache $cache) {
47 43
		$this->db = $db;
48 43
		$this->entities = $entities;
49 43
		$this->cache = $cache;
50 43
	}
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 167
	public function get(ElggEntity $entity, $name) {
65 167
		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 145
	public function getAllForEntity(ElggEntity $entity) {
77 145
		$values = $this->cache->load($entity->guid);
78 145
		if (isset($values)) {
79 18
			return $values;
80
		}
81
82 145
		$qb = Select::fromTable('private_settings');
83 145
		$qb->select('name')
84 145
			->addSelect('value')
85 145
			->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
86
87 145
		$result = $this->db->getData($qb);
88
89 145
		$return = [];
90
91 145
		if (!empty($result)) {
92 50
			foreach ($result as $r) {
93 50
				$return[$r->name] = $r->value;
94
			}
95
		}
96
97 145
		$this->cache->save($entity->guid, $return);
98
99 145
		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 26
	public function getAllForGUIDs($guids) {
117 26
		$guids = Values::normalizeGuids($guids);
118
		
119 26
		if (!is_array($guids) || empty($guids)) {
120
			return [];
121
		}
122
		
123 26
		$qb = Select::fromTable('private_settings');
124 26
		$qb->select('entity_guid')
125 26
			->addSelect('name')
126 26
			->addSelect('value')
127 26
			->where($qb->compare('entity_guid', 'IN', $guids));
128
129 26
		$result = $this->db->getData($qb);
130 26
		if (empty($result)) {
131
			return [];
132
		}
133
		
134 26
		$return = [];
135
		
136 26
		foreach ($result as $r) {
137 26
			$return[$r->entity_guid][$r->name] = $r->value;
138
		}
139
	
140 26
		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 144
	public function set(ElggEntity $entity, $name, $value) {
154 144
		$entity->invalidateCache();
155
156 144
		$value_type = is_int($value) ? ELGG_VALUE_INTEGER : ELGG_VALUE_STRING;
157
158 144
		$qb = Select::fromTable('private_settings');
159 144
		$qb->select('id')
160 144
			->where($qb->compare('name', '=', $name, ELGG_VALUE_STRING))
161 144
			->andWhere($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
162
163 144
		$row = $this->db->getDataRow($qb);
164
165 144
		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 144
			$qb = Insert::intoTable('private_settings');
173 144
			$qb->values([
174 144
				'entity_guid' => $qb->param($entity->guid, ELGG_VALUE_INTEGER),
175 144
				'name' => $qb->param($name, ELGG_VALUE_STRING),
176 144
				'value' => $qb->param($value, $value_type),
177
			]);
178
179 144
			$result = $this->db->insertData($qb);
180
		}
181
182 144
		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
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 250
	public function removeAllForEntity(ElggEntity $entity) {
213 250
		$entity->invalidateCache();
214
215 250
		$qb = Delete::fromTable('private_settings');
216 250
		$qb->where($qb->compare('entity_guid', '=', $entity->guid, ELGG_VALUE_INTEGER));
217
218 250
		return $this->db->deleteData($qb);
0 ignored issues
show
Bug Best Practice introduced by
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