From e05c80acc01561082c3690fa27080b940766397d Mon Sep 17 00:00:00 2001 From: icewing Date: Thu, 13 Mar 2008 15:41:08 +0000 Subject: Marcus Povey * Added relationship code git-svn-id: https://code.elgg.org/elgg/trunk@199 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 102 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/engine/lib/entities.php b/engine/lib/entities.php index c218b8f8a..642c4b5f6 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -424,11 +424,11 @@ $where = array(); if ($type != "") - $where .= " type='$type' "; + $where[] = "type='$type'"; if ($subtype) - $where .= " subtype=$subtype "; + $where[] = "subtype=$subtype"; if ($owner_guid != "") - $where .= " owner_guid='$owner_guid' "; + $where[] = "owner_guid='$owner_guid'"; $query = "SELECT * from {$CONFIG->dbprefix}entities where "; foreach ($where as $w) @@ -439,6 +439,60 @@ return get_data($query, "entity_row_to_elggstar"); } + /** + * Return entities matching a given query joining against a relationship. + * + * @param string $relationship The relationship eg "friends_of" + * @param int $relationship_guid The guid of the entity to use query + * @param bool $inverse_relationship Reverse the normal function of the query to instead say "give me all entities for whome $relationship_guid is a $relationship of" + * @param string $type + * @param string $subtype + * @param int $owner_guid + * @param string $order_by + * @param int $limit + * @param int $offset + */ + function get_entities_from_relationship($relationship, $relationship_guid, $inverse_relationship = false, $type = "", $subtype = "", $owner_guid = 0, $order_by = "time_created desc", $limit = 10, $offset = 0) + { + $relationship = sanitise_string($relationship); + $relationship_guid = (int)$relationship_guid; + $inverse_relationship = (bool)$inverse_relationship; + $type = sanitise_string($type); + $subtype = get_subtype_id($subtype); + $owner_guid = (int)$owner_guid; + $order_by = sanitise_string($order_by); + $limit = (int)$limit; + $offset = (int)$offset; + + $access = get_access_list(); + + $where = array(); + + if ($relationship!="") + $where[] = "r.relationship='$relationship'"; + if ($relationship_guid) + $where[] = ($inverse_relationship ? "r.guid_one='$relationship'" : "r.guid_two='$relationship'"); + if ($type != "") + $where[] = "e.type='$type'"; + if ($subtype) + $where[] = "e.subtype=$subtype"; + if ($owner_guid != "") + $where[] = "e.owner_guid='$owner_guid'"; + + // Select what we're joining based on the options + $joinon = "r.guid_two=e.guid"; + if (!$inverse_relationship) + $joinon = "r.guid_one=e.guid"; + + $query = "SELECT * from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}entity_relationships r on $joinon where "; + foreach ($where as $w) + $query .= " $w and "; + $query .= " (e.access_id in {$access} or (e.access_id = 0 and e.owner_guid = {$_SESSION['id']}))"; // Add access controls + $query .= " order by $order_by limit $offset,$limit"; // Add order and limit + + return get_data($query, "entity_row_to_elggstar"); + } + /** * Delete a given entity. * @@ -456,7 +510,49 @@ } + /** + * Define an arbitrary relationship between two entities. + * This relationship could be a friendship, a group membership or a site membership. + * + * This function lets you make the statement "$guid_one has $relationship with $guid_two". + * + * TODO: Access controls? Are they necessary - I don't think so since we are defining + * relationships between and anyone can do that. The objects should patch some access + * controls over the top tho. + * + * @param int $guid_one + * @param string $relationship + * @param int $guid_two + */ + function add_entity_relationship($guid_one, $relationship, $guid_two) + { + global $CONFIG; + + $guid_one = (int)$guid_one; + $relationship = sanitise_string($relationship); + $guid_two = (int)$guid_two; + + return insert_data("INSERT into {$CONFIG->dbprefix}entity_relationships (guid_one, relationship, guid_two) values ($guid_one, 'relationship', $guid_two)"); + } + /** + * Remove an arbitrary relationship between two entities. + * + * @param int $guid_one + * @param string $relationship + * @param int $guid_two + */ + function remove_entity_relationship($guid_one, $relationship, $guid_two) + { + global $CONFIG; + + $guid_one = (int)$guid_one; + $relationship = sanitise_string($relationship); + $guid_two = (int)$guid_two; + + return insert_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid_one and relationship='$relationship' and guid_two=$guid_two"); + } + // In annotations/ meta -- cgit v1.2.3