diff options
Diffstat (limited to 'engine/lib')
-rw-r--r-- | engine/lib/entities.php | 102 |
1 files 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) @@ -440,6 +440,60 @@ } /** + * 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. * * @param int $guid @@ -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 |