Elgg  Version 1.11
private_settings.php
Go to the documentation of this file.
1 <?php
45  $defaults = array(
46  'private_setting_names' => ELGG_ENTITIES_ANY_VALUE,
47  'private_setting_values' => ELGG_ENTITIES_ANY_VALUE,
48  'private_setting_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE,
49  'private_setting_name_value_pairs_operator' => 'AND',
50  'private_setting_name_prefix' => '',
51  );
52 
53  $options = array_merge($defaults, $options);
54 
55  $singulars = array('private_setting_name', 'private_setting_value',
56  'private_setting_name_value_pair');
57 
59 
60  $clauses = elgg_get_entity_private_settings_where_sql('e', $options['private_setting_names'],
61  $options['private_setting_values'], $options['private_setting_name_value_pairs'],
62  $options['private_setting_name_value_pairs_operator'], $options['private_setting_name_prefix']);
63 
64  if ($clauses) {
65  // merge wheres to pass to get_entities()
66  if (isset($options['wheres']) && !is_array($options['wheres'])) {
67  $options['wheres'] = array($options['wheres']);
68  } elseif (!isset($options['wheres'])) {
69  $options['wheres'] = array();
70  }
71 
72  $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']);
73 
74  // merge joins to pass to get_entities()
75  if (isset($options['joins']) && !is_array($options['joins'])) {
76  $options['joins'] = array($options['joins']);
77  } elseif (!isset($options['joins'])) {
78  $options['joins'] = array();
79  }
80 
81  $options['joins'] = array_merge($options['joins'], $clauses['joins']);
82  }
83 
85 }
86 
100 function elgg_get_entity_private_settings_where_sql($table, $names = null, $values = null,
101 $pairs = null, $pair_operator = 'AND', $name_prefix = '') {
102 
103  global $CONFIG;
104 
105  // @todo short circuit test
106 
107  $return = array (
108  'joins' => array (),
109  'wheres' => array(),
110  );
111 
112  $return['joins'][] = "JOIN {$CONFIG->dbprefix}private_settings ps on
113  {$table}.guid = ps.entity_guid";
114 
115  $wheres = array();
116 
117  // get names wheres
118  $names_where = '';
119  if ($names !== null) {
120  if (!is_array($names)) {
121  $names = array($names);
122  }
123 
124  $sanitised_names = array();
125  foreach ($names as $name) {
126  $name = $name_prefix . $name;
127  $sanitised_names[] = '\'' . sanitise_string($name) . '\'';
128  }
129 
130  $names_str = implode(',', $sanitised_names);
131  if ($names_str) {
132  $names_where = "(ps.name IN ($names_str))";
133  }
134  }
135 
136  // get values wheres
137  $values_where = '';
138  if ($values !== null) {
139  if (!is_array($values)) {
140  $values = array($values);
141  }
142 
143  $sanitised_values = array();
144  foreach ($values as $value) {
145  // normalize to 0
146  if (!$value) {
147  $value = 0;
148  }
149  $sanitised_values[] = '\'' . sanitise_string($value) . '\'';
150  }
151 
152  $values_str = implode(',', $sanitised_values);
153  if ($values_str) {
154  $values_where = "(ps.value IN ($values_str))";
155  }
156  }
157 
158  if ($names_where && $values_where) {
159  $wheres[] = "($names_where AND $values_where)";
160  } elseif ($names_where) {
161  $wheres[] = "($names_where)";
162  } elseif ($values_where) {
163  $wheres[] = "($values_where)";
164  }
165 
166  // add pairs which must be in arrays.
167  if (is_array($pairs)) {
168  // join counter for incremental joins in pairs
169  $i = 1;
170 
171  // check if this is an array of pairs or just a single pair.
172  if (isset($pairs['name']) || isset($pairs['value'])) {
173  $pairs = array($pairs);
174  }
175 
176  $pair_wheres = array();
177 
178  foreach ($pairs as $index => $pair) {
179  // @todo move this elsewhere?
180  // support shortcut 'n' => 'v' method.
181  if (!is_array($pair)) {
182  $pair = array(
183  'name' => $index,
184  'value' => $pair
185  );
186  }
187 
188  // must have at least a name and value
189  if (!isset($pair['name']) || !isset($pair['value'])) {
190  // @todo should probably return false.
191  continue;
192  }
193 
194  if (isset($pair['operand'])) {
195  $operand = sanitise_string($pair['operand']);
196  } else {
197  $operand = ' = ';
198  }
199 
200  // for comparing
201  $trimmed_operand = trim(strtolower($operand));
202 
203  // if the value is an int, don't quote it because str '15' < str '5'
204  // if the operand is IN don't quote it because quoting should be done already.
205  if (is_numeric($pair['value'])) {
206  $value = sanitise_string($pair['value']);
207  } else if (is_array($pair['value'])) {
208  $values_array = array();
209 
210  foreach ($pair['value'] as $pair_value) {
211  if (is_numeric($pair_value)) {
212  $values_array[] = sanitise_string($pair_value);
213  } else {
214  $values_array[] = "'" . sanitise_string($pair_value) . "'";
215  }
216  }
217 
218  if ($values_array) {
219  $value = '(' . implode(', ', $values_array) . ')';
220  }
221 
222  // @todo allow support for non IN operands with array of values.
223  // will have to do more silly joins.
224  $operand = 'IN';
225  } else if ($trimmed_operand == 'in') {
226  $value = "({$pair['value']})";
227  } else {
228  $value = "'" . sanitise_string($pair['value']) . "'";
229  }
230 
231  $name = sanitise_string($name_prefix . $pair['name']);
232 
233  // @todo The multiple joins are only needed when the operator is AND
234  $return['joins'][] = "JOIN {$CONFIG->dbprefix}private_settings ps{$i}
235  on {$table}.guid = ps{$i}.entity_guid";
236 
237  $pair_wheres[] = "(ps{$i}.name = '$name' AND ps{$i}.value
238  $operand $value)";
239 
240  $i++;
241  }
242 
243  $where = implode(" $pair_operator ", $pair_wheres);
244  if ($where) {
245  $wheres[] = "($where)";
246  }
247  }
248 
249  $where = implode(' AND ', $wheres);
250  if ($where) {
251  $return['wheres'][] = "($where)";
252  }
253 
254  return $return;
255 }
256 
276  global $CONFIG;
277  $entity_guid = (int) $entity_guid;
279 
281  if (!$entity instanceof \ElggEntity) {
282  return null;
283  }
284 
285  $query = "SELECT value from {$CONFIG->dbprefix}private_settings
286  where name = '{$name}' and entity_guid = {$entity_guid}";
287  $setting = get_data_row($query);
288 
289  if ($setting) {
290  return $setting->value;
291  }
292  return null;
293 }
294 
307  global $CONFIG;
308 
309  $entity_guid = (int) $entity_guid;
311  if (!$entity instanceof \ElggEntity) {
312  return false;
313  }
314 
315  $query = "SELECT * from {$CONFIG->dbprefix}private_settings where entity_guid = {$entity_guid}";
316  $result = get_data($query);
317  if ($result) {
318  $return = array();
319  foreach ($result as $r) {
320  $return[$r->name] = $r->value;
321  }
322 
323  return $return;
324  }
325 
326  return array();
327 }
328 
343  global $CONFIG;
344 
345  $entity_guid = (int) $entity_guid;
348 
349  $result = insert_data("INSERT into {$CONFIG->dbprefix}private_settings
350  (entity_guid, name, value) VALUES
351  ($entity_guid, '$name', '$value')
352  ON DUPLICATE KEY UPDATE value='$value'");
353 
354  return $result !== false;
355 }
356 
370  global $CONFIG;
371 
372  $entity_guid = (int) $entity_guid;
373 
375  if (!$entity instanceof \ElggEntity) {
376  return false;
377  }
378 
380 
381  return delete_data("DELETE from {$CONFIG->dbprefix}private_settings
382  WHERE name = '{$name}'
383  AND entity_guid = {$entity_guid}");
384 }
385 
398  global $CONFIG;
399 
400  $entity_guid = (int) $entity_guid;
401 
403  if (!$entity instanceof \ElggEntity) {
404  return false;
405  }
406 
407  return delete_data("DELETE from {$CONFIG->dbprefix}private_settings
408  WHERE entity_guid = {$entity_guid}");
409 }
$r
get_data_row($query, $callback="")
Retrieve a single row from the database.
Definition: database.php:66
$table
Definition: cron.php:28
remove_all_private_settings($entity_guid)
Deletes all private settings for an entity.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$defaults
elgg_get_entity_private_settings_where_sql($table, $names=null, $values=null, $pairs=null, $pair_operator= 'AND', $name_prefix= '')
Returns private setting name and value SQL where/join clauses for entities.
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
$value
Definition: longtext.php:26
$return
Definition: opendd.php:15
delete_data($query)
Remove a row from the database.
Definition: database.php:106
insert_data($query)
Insert a row into the database.
Definition: database.php:80
$entity_guid
Definition: save.php:9
$options
Definition: index.php:14
remove_private_setting($entity_guid, $name)
Deletes a private setting for an entity.
global $CONFIG
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:1967
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:490
elgg global
Pointer to the global context.
Definition: elgglib.js:12
get_data($query, $callback="")
Retrieve rows from the database.
Definition: database.php:50
set_private_setting($entity_guid, $name, $value)
Sets a private setting for an entity.
get_private_setting($entity_guid, $name)
Gets a private setting for an entity.
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1376
$entity
Definition: delete.php:10
get_all_private_settings($entity_guid)
Return an array of all private settings.
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382