Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
PrivateSettingsTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
4 use Elgg\Database;
7 
20 
22  private $db;
23 
25  private $entities;
26 
28  private $table;
29 
31  private $cache;
32 
40  public function __construct(Database $db, EntityTable $entities, PluginSettingsCache $cache) {
41  $this->db = $db;
42  $this->entities = $entities;
43  $this->cache = $cache;
44  $this->table = $this->db->getTablePrefix() . 'private_settings';
45  }
46 
80  public function getEntities(array $options = array()) {
81  $defaults = array(
82  'private_setting_names' => ELGG_ENTITIES_ANY_VALUE,
83  'private_setting_values' => ELGG_ENTITIES_ANY_VALUE,
84  'private_setting_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
85  'private_setting_name_value_pairs_operator' => 'AND',
86  'private_setting_name_prefix' => '',
87  );
88 
89  $options = array_merge($defaults, $options);
90 
91  $singulars = array(
92  'private_setting_name',
93  'private_setting_value',
94  'private_setting_name_value_pair',
95  );
96 
98 
99  $clauses = $this->getWhereSql('e',
100  $options['private_setting_names'],
101  $options['private_setting_values'],
102  $options['private_setting_name_value_pairs'],
103  $options['private_setting_name_value_pairs_operator'],
104  $options['private_setting_name_prefix']);
105 
106  if ($clauses) {
107  // merge wheres to pass to get_entities()
108  if (isset($options['wheres']) && !is_array($options['wheres'])) {
109  $options['wheres'] = array($options['wheres']);
110  } elseif (!isset($options['wheres'])) {
111  $options['wheres'] = array();
112  }
113 
114  $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']);
115 
116  // merge joins to pass to get_entities()
117  if (isset($options['joins']) && !is_array($options['joins'])) {
118  $options['joins'] = array($options['joins']);
119  } elseif (!isset($options['joins'])) {
120  $options['joins'] = array();
121  }
122 
123  $options['joins'] = array_merge($options['joins'], $clauses['joins']);
124  }
125 
126  return $this->entities->getEntities($options);
127  }
128 
140  private function getWhereSql($table, $names = null, $values = null,
141  $pairs = null, $pair_operator = 'AND', $name_prefix = '') {
142 
143  // @todo short circuit test
144 
145  $return = array (
146  'joins' => array (),
147  'wheres' => array(),
148  );
149 
150  $return['joins'][] = "JOIN {$this->table} ps on
151  {$table}.guid = ps.entity_guid";
152 
153  $wheres = array();
154 
155  // get names wheres
156  $names_where = '';
157  if ($names !== null) {
158  if (!is_array($names)) {
159  $names = array($names);
160  }
161 
162  $sanitised_names = array();
163  foreach ($names as $name) {
164  $name = $name_prefix . $name;
165  $sanitised_names[] = '\'' . $this->db->sanitizeString($name) . '\'';
166  }
167 
168  $names_str = implode(',', $sanitised_names);
169  if ($names_str) {
170  $names_where = "(ps.name IN ($names_str))";
171  }
172  }
173 
174  // get values wheres
175  $values_where = '';
176  if ($values !== null) {
177  if (!is_array($values)) {
178  $values = array($values);
179  }
180 
181  $sanitised_values = array();
182  foreach ($values as $value) {
183  // normalize to 0
184  if (!$value) {
185  $value = 0;
186  }
187  $sanitised_values[] = '\'' . $this->db->sanitizeString($value) . '\'';
188  }
189 
190  $values_str = implode(',', $sanitised_values);
191  if ($values_str) {
192  $values_where = "(ps.value IN ($values_str))";
193  }
194  }
195 
196  if ($names_where && $values_where) {
197  $wheres[] = "($names_where AND $values_where)";
198  } elseif ($names_where) {
199  $wheres[] = "($names_where)";
200  } elseif ($values_where) {
201  $wheres[] = "($values_where)";
202  }
203 
204  // add pairs which must be in arrays.
205  if (is_array($pairs)) {
206  // join counter for incremental joins in pairs
207  $i = 1;
208 
209  // check if this is an array of pairs or just a single pair.
210  if (isset($pairs['name']) || isset($pairs['value'])) {
211  $pairs = array($pairs);
212  }
213 
214  $pair_wheres = array();
215 
216  foreach ($pairs as $index => $pair) {
217  // @todo move this elsewhere?
218  // support shortcut 'n' => 'v' method.
219  if (!is_array($pair)) {
220  $pair = array(
221  'name' => $index,
222  'value' => $pair
223  );
224  }
225 
226  // must have at least a name and value
227  if (!isset($pair['name']) || !isset($pair['value'])) {
228  // @todo should probably return false.
229  continue;
230  }
231 
232  if (isset($pair['operand'])) {
233  $operand = $this->db->sanitizeString($pair['operand']);
234  } else {
235  $operand = ' = ';
236  }
237 
238  // for comparing
239  $trimmed_operand = trim(strtolower($operand));
240 
241  // if the value is an int, don't quote it because str '15' < str '5'
242  // if the operand is IN don't quote it because quoting should be done already.
243  if (is_numeric($pair['value'])) {
244  $value = $this->db->sanitizeString($pair['value']);
245  } else if (is_array($pair['value'])) {
246  $values_array = array();
247 
248  foreach ($pair['value'] as $pair_value) {
249  if (is_numeric($pair_value)) {
250  $values_array[] = $this->db->sanitizeString($pair_value);
251  } else {
252  $values_array[] = "'" . $this->db->sanitizeString($pair_value) . "'";
253  }
254  }
255 
256  if ($values_array) {
257  $value = '(' . implode(', ', $values_array) . ')';
258  }
259 
260  // @todo allow support for non IN operands with array of values.
261  // will have to do more silly joins.
262  $operand = 'IN';
263  } else if ($trimmed_operand == 'in') {
264  $value = "({$pair['value']})";
265  } else {
266  $value = "'" . $this->db->sanitizeString($pair['value']) . "'";
267  }
268 
269  $name = $this->db->sanitizeString($name_prefix . $pair['name']);
270 
271  // @todo The multiple joins are only needed when the operator is AND
272  $return['joins'][] = "JOIN {$this->table} ps{$i}
273  on {$table}.guid = ps{$i}.entity_guid";
274 
275  $pair_wheres[] = "(ps{$i}.name = '$name' AND ps{$i}.value
276  $operand $value)";
277 
278  $i++;
279  }
280 
281  $where = implode(" $pair_operator ", $pair_wheres);
282  if ($where) {
283  $wheres[] = "($where)";
284  }
285  }
286 
287  $where = implode(' AND ', $wheres);
288  if ($where) {
289  $return['wheres'][] = "($where)";
290  }
291 
292  return $return;
293  }
294 
306  public function get($entity_guid, $name) {
307  $values = $this->cache->getAll($entity_guid);
308  if (isset($values[$name])) {
309  return $values[$name];
310  }
311 
312  $entity_guid = (int) $entity_guid;
313  $name = $this->db->sanitizeString($name);
314 
315  $entity = $this->entities->get($entity_guid);
316 
317  if (!$entity instanceof \ElggEntity) {
318  return null;
319  }
320 
321  $query = "SELECT value FROM {$this->table}
322  where name = '{$name}' and entity_guid = {$entity_guid}";
323  $setting = $this->db->getDataRow($query);
324 
325  if ($setting) {
326  return $setting->value;
327  }
328 
329  return null;
330  }
331 
339  function getAll($entity_guid) {
340  $entity_guid = (int) $entity_guid;
341  $entity = $this->entities->get($entity_guid);
342 
343  if (!$entity instanceof \ElggEntity) {
344  return false;
345  }
346 
347  $query = "SELECT * FROM {$this->table} WHERE entity_guid = {$entity_guid}";
348  $result = $this->db->getData($query);
349 
350  if ($result) {
351  $return = array();
352  foreach ($result as $r) {
353  $return[$r->name] = $r->value;
354  }
355 
356  return $return;
357  }
358 
359  return array();
360  }
361 
370  public function set($entity_guid, $name, $value) {
371  $this->cache->clear($entity_guid);
372  _elgg_services()->boot->invalidateCache();
373 
374  $entity_guid = (int) $entity_guid;
375  $name = $this->db->sanitizeString($name);
376  $value = $this->db->sanitizeString($value);
377 
378  $result = $this->db->insertData("INSERT into {$this->table}
379  (entity_guid, name, value) VALUES
380  ($entity_guid, '$name', '$value')
381  ON DUPLICATE KEY UPDATE value='$value'");
382 
383  return $result !== false;
384  }
385 
393  function remove($entity_guid, $name) {
394  $this->cache->clear($entity_guid);
395  _elgg_services()->boot->invalidateCache();
396 
397  $entity_guid = (int) $entity_guid;
398 
399  $entity = $this->entities->get($entity_guid);
400 
401  if (!$entity instanceof \ElggEntity) {
402  return false;
403  }
404 
405  $name = $this->db->sanitizeString($name);
406 
407  return $this->db->deleteData("DELETE FROM {$this->table}
408  WHERE name = '{$name}'
409  AND entity_guid = {$entity_guid}");
410  }
411 
419  $this->cache->clear($entity_guid);
420  _elgg_services()->boot->invalidateCache();
421 
422  $entity_guid = (int) $entity_guid;
423 
424  $entity = $this->entities->get($entity_guid);
425 
426  if (!$entity instanceof \ElggEntity) {
427  return false;
428  }
429 
430  return $this->db->deleteData("DELETE FROM {$this->table}
431  WHERE entity_guid = {$entity_guid}");
432  }
433 }
$r
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$defaults
set($entity_guid, $name, $value)
Sets a private setting for an entity.
$value
Definition: longtext.php:26
$return
Definition: opendd.php:15
removeAllForEntity($entity_guid)
Deletes all private settings for an entity.
$options
Elgg admin footer.
Definition: footer.php:6
$entity_guid
Definition: save.php:9
Private settings for entities.
In memory cache of (non-user-specific, non-internal) plugin settings.
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2001
__construct(Database $db, EntityTable $entities, PluginSettingsCache $cache)
Constructor.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
table
Definition: admin.css.php:59
$entity
Definition: delete.php:7
getAll($entity_guid)
Return an array of all private settings.
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1440
getEntities(array $options=array())
Returns entities based upon private settings.