Elgg  Version 1.12
PrivateSettingsTable.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Database;
3 
4 use Elgg\Database;
6 
19 
21  private $db;
22 
24  private $entities;
25 
27  private $table;
28 
35  public function __construct(Database $db, EntityTable $entities) {
36  $this->db = $db;
37  $this->entities = $entities;
38  $this->table = $this->db->getTablePrefix() . 'private_settings';
39  }
40 
74  public function getEntities(array $options = array()) {
75  $defaults = array(
76  'private_setting_names' => ELGG_ENTITIES_ANY_VALUE,
77  'private_setting_values' => ELGG_ENTITIES_ANY_VALUE,
78  'private_setting_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
79  'private_setting_name_value_pairs_operator' => 'AND',
80  'private_setting_name_prefix' => '',
81  );
82 
83  $options = array_merge($defaults, $options);
84 
85  $singulars = array(
86  'private_setting_name',
87  'private_setting_value',
88  'private_setting_name_value_pair',
89  );
90 
92 
93  $clauses = $this->getWhereSql('e',
94  $options['private_setting_names'],
95  $options['private_setting_values'],
96  $options['private_setting_name_value_pairs'],
97  $options['private_setting_name_value_pairs_operator'],
98  $options['private_setting_name_prefix']);
99 
100  if ($clauses) {
101  // merge wheres to pass to get_entities()
102  if (isset($options['wheres']) && !is_array($options['wheres'])) {
103  $options['wheres'] = array($options['wheres']);
104  } elseif (!isset($options['wheres'])) {
105  $options['wheres'] = array();
106  }
107 
108  $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']);
109 
110  // merge joins to pass to get_entities()
111  if (isset($options['joins']) && !is_array($options['joins'])) {
112  $options['joins'] = array($options['joins']);
113  } elseif (!isset($options['joins'])) {
114  $options['joins'] = array();
115  }
116 
117  $options['joins'] = array_merge($options['joins'], $clauses['joins']);
118  }
119 
120  return $this->entities->getEntities($options);
121  }
122 
134  private function getWhereSql($table, $names = null, $values = null,
135  $pairs = null, $pair_operator = 'AND', $name_prefix = '') {
136 
137  // @todo short circuit test
138 
139  $return = array (
140  'joins' => array (),
141  'wheres' => array(),
142  );
143 
144  $return['joins'][] = "JOIN {$this->table} ps on
145  {$table}.guid = ps.entity_guid";
146 
147  $wheres = array();
148 
149  // get names wheres
150  $names_where = '';
151  if ($names !== null) {
152  if (!is_array($names)) {
153  $names = array($names);
154  }
155 
156  $sanitised_names = array();
157  foreach ($names as $name) {
158  $name = $name_prefix . $name;
159  $sanitised_names[] = '\'' . $this->db->sanitizeString($name) . '\'';
160  }
161 
162  $names_str = implode(',', $sanitised_names);
163  if ($names_str) {
164  $names_where = "(ps.name IN ($names_str))";
165  }
166  }
167 
168  // get values wheres
169  $values_where = '';
170  if ($values !== null) {
171  if (!is_array($values)) {
172  $values = array($values);
173  }
174 
175  $sanitised_values = array();
176  foreach ($values as $value) {
177  // normalize to 0
178  if (!$value) {
179  $value = 0;
180  }
181  $sanitised_values[] = '\'' . $this->db->sanitizeString($value) . '\'';
182  }
183 
184  $values_str = implode(',', $sanitised_values);
185  if ($values_str) {
186  $values_where = "(ps.value IN ($values_str))";
187  }
188  }
189 
190  if ($names_where && $values_where) {
191  $wheres[] = "($names_where AND $values_where)";
192  } elseif ($names_where) {
193  $wheres[] = "($names_where)";
194  } elseif ($values_where) {
195  $wheres[] = "($values_where)";
196  }
197 
198  // add pairs which must be in arrays.
199  if (is_array($pairs)) {
200  // join counter for incremental joins in pairs
201  $i = 1;
202 
203  // check if this is an array of pairs or just a single pair.
204  if (isset($pairs['name']) || isset($pairs['value'])) {
205  $pairs = array($pairs);
206  }
207 
208  $pair_wheres = array();
209 
210  foreach ($pairs as $index => $pair) {
211  // @todo move this elsewhere?
212  // support shortcut 'n' => 'v' method.
213  if (!is_array($pair)) {
214  $pair = array(
215  'name' => $index,
216  'value' => $pair
217  );
218  }
219 
220  // must have at least a name and value
221  if (!isset($pair['name']) || !isset($pair['value'])) {
222  // @todo should probably return false.
223  continue;
224  }
225 
226  if (isset($pair['operand'])) {
227  $operand = $this->db->sanitizeString($pair['operand']);
228  } else {
229  $operand = ' = ';
230  }
231 
232  // for comparing
233  $trimmed_operand = trim(strtolower($operand));
234 
235  // if the value is an int, don't quote it because str '15' < str '5'
236  // if the operand is IN don't quote it because quoting should be done already.
237  if (is_numeric($pair['value'])) {
238  $value = $this->db->sanitizeString($pair['value']);
239  } else if (is_array($pair['value'])) {
240  $values_array = array();
241 
242  foreach ($pair['value'] as $pair_value) {
243  if (is_numeric($pair_value)) {
244  $values_array[] = $this->db->sanitizeString($pair_value);
245  } else {
246  $values_array[] = "'" . $this->db->sanitizeString($pair_value) . "'";
247  }
248  }
249 
250  if ($values_array) {
251  $value = '(' . implode(', ', $values_array) . ')';
252  }
253 
254  // @todo allow support for non IN operands with array of values.
255  // will have to do more silly joins.
256  $operand = 'IN';
257  } else if ($trimmed_operand == 'in') {
258  $value = "({$pair['value']})";
259  } else {
260  $value = "'" . $this->db->sanitizeString($pair['value']) . "'";
261  }
262 
263  $name = $this->db->sanitizeString($name_prefix . $pair['name']);
264 
265  // @todo The multiple joins are only needed when the operator is AND
266  $return['joins'][] = "JOIN {$this->table} ps{$i}
267  on {$table}.guid = ps{$i}.entity_guid";
268 
269  $pair_wheres[] = "(ps{$i}.name = '$name' AND ps{$i}.value
270  $operand $value)";
271 
272  $i++;
273  }
274 
275  $where = implode(" $pair_operator ", $pair_wheres);
276  if ($where) {
277  $wheres[] = "($where)";
278  }
279  }
280 
281  $where = implode(' AND ', $wheres);
282  if ($where) {
283  $return['wheres'][] = "($where)";
284  }
285 
286  return $return;
287  }
288 
300  public function get($entity_guid, $name) {
301  $entity_guid = (int) $entity_guid;
302  $name = $this->db->sanitizeString($name);
303 
304  $entity = $this->entities->get($entity_guid);
305 
306  if (!$entity instanceof \ElggEntity) {
307  return null;
308  }
309 
310  $query = "SELECT value FROM {$this->table}
311  where name = '{$name}' and entity_guid = {$entity_guid}";
312  $setting = $this->db->getDataRow($query);
313 
314  if ($setting) {
315  return $setting->value;
316  }
317 
318  return null;
319  }
320 
328  function getAll($entity_guid) {
329  $entity_guid = (int) $entity_guid;
330  $entity = $this->entities->get($entity_guid);
331 
332  if (!$entity instanceof \ElggEntity) {
333  return false;
334  }
335 
336  $query = "SELECT * FROM {$this->table} WHERE entity_guid = {$entity_guid}";
337  $result = $this->db->getData($query);
338 
339  if ($result) {
340  $return = array();
341  foreach ($result as $r) {
342  $return[$r->name] = $r->value;
343  }
344 
345  return $return;
346  }
347 
348  return array();
349  }
350 
359  public function set($entity_guid, $name, $value) {
360  $entity_guid = (int) $entity_guid;
361  $name = $this->db->sanitizeString($name);
362  $value = $this->db->sanitizeString($value);
363 
364  $result = $this->db->insertData("INSERT into {$this->table}
365  (entity_guid, name, value) VALUES
366  ($entity_guid, '$name', '$value')
367  ON DUPLICATE KEY UPDATE value='$value'");
368 
369  return $result !== false;
370  }
371 
379  function remove($entity_guid, $name) {
380  $entity_guid = (int) $entity_guid;
381 
382  $entity = $this->entities->get($entity_guid);
383 
384  if (!$entity instanceof \ElggEntity) {
385  return false;
386  }
387 
388  $name = $this->db->sanitizeString($name);
389 
390  return $this->db->deleteData("DELETE FROM {$this->table}
391  WHERE name = '{$name}'
392  AND entity_guid = {$entity_guid}");
393  }
394 
402  $entity_guid = (int) $entity_guid;
403 
404  $entity = $this->entities->get($entity_guid);
405 
406  if (!$entity instanceof \ElggEntity) {
407  return false;
408  }
409 
410  return $this->db->deleteData("DELETE FROM {$this->table}
411  WHERE entity_guid = {$entity_guid}");
412  }
413 }
$r
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$defaults
$value
Definition: longtext.php:26
$return
Definition: opendd.php:15
removeAllForEntity($entity_guid)
Deletes all private settings for an entity.
$entity_guid
Definition: save.php:9
$options
Definition: index.php:14
Private settings for entities.
__construct(Database $db, EntityTable $entities)
Constructor.
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2006
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:1401
$entity
Definition: delete.php:10
getEntities(array $options=array())
Returns entities based upon private settings.
table
Definition: admin.php:59