Elgg  Version 1.9
relationships.php
Go to the documentation of this file.
1 <?php
18  if ($row instanceof stdClass) {
19  return new ElggRelationship($row);
20  }
21 
22  return false;
23 }
24 
32 function get_relationship($id) {
34  if (!$row) {
35  return false;
36  }
37 
38  return new ElggRelationship($row);
39 }
40 
51 
52  $id = (int)$id;
53 
54  return get_data_row("SELECT * FROM {$CONFIG->dbprefix}entity_relationships WHERE id = $id");
55 }
56 
66 
67  $id = (int)$id;
68 
69  $relationship = get_relationship($id);
70 
71  if (elgg_trigger_event('delete', 'relationship', $relationship)) {
72  return delete_data("DELETE FROM {$CONFIG->dbprefix}entity_relationships WHERE id = $id");
73  }
74 
75  return false;
76 }
77 
91 function add_entity_relationship($guid_one, $relationship, $guid_two) {
93 
94  if (strlen($relationship) > ElggRelationship::RELATIONSHIP_LIMIT) {
95  $msg = "relationship name cannot be longer than " . ElggRelationship::RELATIONSHIP_LIMIT;
96  throw InvalidArgumentException($msg);
97  }
98 
99  $guid_one = (int)$guid_one;
100  $relationship = sanitise_string($relationship);
101  $guid_two = (int)$guid_two;
102  $time = time();
103 
104  // Check for duplicates
105  if (check_entity_relationship($guid_one, $relationship, $guid_two)) {
106  return false;
107  }
108 
109  $id = insert_data("INSERT INTO {$CONFIG->dbprefix}entity_relationships
110  (guid_one, relationship, guid_two, time_created)
111  VALUES ($guid_one, '$relationship', $guid_two, $time)");
112 
113  if ($id !== false) {
114  $obj = get_relationship($id);
115 
116  // this event has been deprecated in 1.9. Use 'create', 'relationship'
117  $result_old = elgg_trigger_event('create', $relationship, $obj);
118 
119  $result = elgg_trigger_event('create', 'relationship', $obj);
120  if ($result && $result_old) {
121  return true;
122  } else {
124  }
125  }
126 
127  return false;
128 }
129 
141 function check_entity_relationship($guid_one, $relationship, $guid_two) {
142  global $CONFIG;
143 
144  $guid_one = (int)$guid_one;
145  $relationship = sanitise_string($relationship);
146  $guid_two = (int)$guid_two;
147 
148  $query = "SELECT * FROM {$CONFIG->dbprefix}entity_relationships
149  WHERE guid_one=$guid_one
150  AND relationship='$relationship'
151  AND guid_two=$guid_two limit 1";
152 
154  if ($row) {
155  return $row;
156  }
157 
158  return false;
159 }
160 
172 function remove_entity_relationship($guid_one, $relationship, $guid_two) {
173  global $CONFIG;
174 
175  $guid_one = (int)$guid_one;
176  $relationship = sanitise_string($relationship);
177  $guid_two = (int)$guid_two;
178 
179  $obj = check_entity_relationship($guid_one, $relationship, $guid_two);
180  if ($obj == false) {
181  return false;
182  }
183 
184  // this event has been deprecated in 1.9. Use 'delete', 'relationship'
185  $result_old = elgg_trigger_event('delete', $relationship, $obj);
186 
187  $result = elgg_trigger_event('delete', 'relationship', $obj);
188  if ($result && $result_old) {
189  $query = "DELETE FROM {$CONFIG->dbprefix}entity_relationships
190  WHERE guid_one = $guid_one
191  AND relationship = '$relationship'
192  AND guid_two = $guid_two";
193 
194  return (bool)delete_data($query);
195  } else {
196  return false;
197  }
198 }
199 
211 function remove_entity_relationships($guid, $relationship = "", $inverse_relationship = false, $type = '') {
212  global $CONFIG;
213 
214  $guid = (int) $guid;
215 
216  if (!empty($relationship)) {
217  $relationship = sanitize_string($relationship);
218  $where = "AND er.relationship = '$relationship'";
219  } else {
220  $where = "";
221  }
222 
223  if (!empty($type)) {
225  if (!$inverse_relationship) {
226  $join = "JOIN {$CONFIG->dbprefix}entities e ON e.guid = er.guid_two";
227  } else {
228  $join = "JOIN {$CONFIG->dbprefix}entities e ON e.guid = er.guid_one";
229  $where .= " AND ";
230  }
231  $where .= " AND e.type = '$type'";
232  } else {
233  $join = "";
234  }
235 
236  $guid_col = $inverse_relationship ? "guid_two" : "guid_one";
237 
238  delete_data("
239  DELETE er FROM {$CONFIG->dbprefix}entity_relationships AS er
240  $join
241  WHERE $guid_col = $guid
242  $where
243  ");
244 
245  return true;
246 }
247 
257 function get_entity_relationships($guid, $inverse_relationship = false) {
258  global $CONFIG;
259 
260  $guid = (int)$guid;
261 
262  $where = ($inverse_relationship ? "guid_two='$guid'" : "guid_one='$guid'");
263 
264  $query = "SELECT * from {$CONFIG->dbprefix}entity_relationships where {$where}";
265 
266  return get_data($query, "row_to_elggrelationship");
267 }
268 
305  $defaults = array(
306  'relationship' => null,
307  'relationship_guid' => null,
308  'inverse_relationship' => false,
309  'relationship_join_on' => 'guid',
310  );
311 
312  $options = array_merge($defaults, $options);
313 
314  $join_column = "e.{$options['relationship_join_on']}";
315  $clauses = elgg_get_entity_relationship_where_sql($join_column, $options['relationship'],
316  $options['relationship_guid'], $options['inverse_relationship']);
317 
318  if ($clauses) {
319  // merge wheres to pass to get_entities()
320  if (isset($options['wheres']) && !is_array($options['wheres'])) {
321  $options['wheres'] = array($options['wheres']);
322  } elseif (!isset($options['wheres'])) {
323  $options['wheres'] = array();
324  }
325 
326  $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']);
327 
328  // merge joins to pass to get_entities()
329  if (isset($options['joins']) && !is_array($options['joins'])) {
330  $options['joins'] = array($options['joins']);
331  } elseif (!isset($options['joins'])) {
332  $options['joins'] = array();
333  }
334 
335  $options['joins'] = array_merge($options['joins'], $clauses['joins']);
336 
337  if (isset($options['selects']) && !is_array($options['selects'])) {
338  $options['selects'] = array($options['selects']);
339  } elseif (!isset($options['selects'])) {
340  $options['selects'] = array();
341  }
342 
343  $select = array('r.id');
344 
345  $options['selects'] = array_merge($options['selects'], $select);
346 
347  if (!isset($options['group_by'])) {
348  $options['group_by'] = $clauses['group_by'];
349  }
350  }
351 
353 }
354 
370 function elgg_get_entity_relationship_where_sql($column, $relationship = null,
371 $relationship_guid = null, $inverse_relationship = false) {
372 
373  if ($relationship == null && $relationship_guid == null) {
374  return '';
375  }
376 
377  global $CONFIG;
378 
379  $wheres = array();
380  $joins = array();
381  $group_by = '';
382 
383  if ($inverse_relationship) {
384  $joins[] = "JOIN {$CONFIG->dbprefix}entity_relationships r on r.guid_one = $column";
385  } else {
386  $joins[] = "JOIN {$CONFIG->dbprefix}entity_relationships r on r.guid_two = $column";
387  }
388 
389  if ($relationship) {
390  $wheres[] = "r.relationship = '" . sanitise_string($relationship) . "'";
391  }
392 
393  if ($relationship_guid) {
394  if ($inverse_relationship) {
395  $wheres[] = "r.guid_two = '$relationship_guid'";
396  } else {
397  $wheres[] = "r.guid_one = '$relationship_guid'";
398  }
399  } else {
400  // See #5775. Queries that do not include a relationship_guid must be grouped by entity table alias,
401  // otherwise the result set is not unique
402  $group_by = $column;
403  }
404 
405  if ($where_str = implode(' AND ', $wheres)) {
406 
407  return array('wheres' => array("($where_str)"), 'joins' => $joins, 'group_by' => $group_by);
408  }
409 
410  return '';
411 }
412 
424  return elgg_list_entities($options, 'elgg_get_entities_from_relationship');
425 }
426 
438  $options['selects'][] = "COUNT(e.guid) as total";
439  $options['group_by'] = 'r.guid_two';
440  $options['order_by'] = 'total desc';
442 }
443 
455  return elgg_list_entities($options, 'elgg_get_entities_from_relationship_count');
456 }
elgg_get_entities_from_metadata(array $options=array())
ElggEntities interfaces.
Definition: metadata.php:431
get_relationship($id)
Get a relationship by its ID.
get_data_row($query, $callback="")
Retrieve a single row from the database.
Definition: database.php:66
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
$column
Definition: add.php:13
elgg_list_entities_from_relationship_count($options)
Returns a list of entities by relationship count.
$guid
Removes an admin notice.
delete_data($query)
Remove a row from the database.
Definition: database.php:106
remove_entity_relationships($guid, $relationship="", $inverse_relationship=false, $type= '')
Removes all relationships originating from a particular entity.
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
insert_data($query)
Insert a row into the database.
Definition: database.php:80
$options
Definition: index.php:14
get_entity_relationships($guid, $inverse_relationship=false)
Get all the relationships for a given GUID.
global $CONFIG
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
elgg_get_entity_relationship_where_sql($column, $relationship=null, $relationship_guid=null, $inverse_relationship=false)
Returns SQL appropriate for relationship joins and wheres.
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
delete_relationship($id)
Delete a relationship by its ID.
elgg_list_entities_from_relationship(array $options=array())
Returns a viewable list of entities by relationship.
elgg_list_entities(array $options=array(), $getter= 'elgg_get_entities', $viewer= 'elgg_view_entity_list')
Returns a string of rendered entities.
Definition: entities.php:1343
get_data($query, $callback="")
Retrieve rows from the database.
Definition: database.php:50
row_to_elggrelationship($row)
Convert a database row to a new ElggRelationship.
elgg_get_entities_from_relationship_count(array $options=array())
Gets the number of entities by a the number of entities related to them in a particular way...
$row
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
_elgg_get_relationship_row($id)
Get a database row from the relationship table.
$defaults
Definition: access.php:19
elgg_trigger_event($event, $object_type, $object=null)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event...
Definition: elgglib.php:720
if(!$collection_name) $id
Definition: add.php:17