From c0412aa41a1efe0be21bdd6807280b6a55005053 Mon Sep 17 00:00:00 2001 From: ben Date: Wed, 30 Apr 2008 11:20:10 +0000 Subject: Introducing getEntitiesFromRelationship and countEntitiesFromRelationship methods on ElggEntity git-svn-id: https://code.elgg.org/elgg/trunk@577 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 22 ++++++++++++++++++++++ engine/lib/relationships.php | 28 +++++++++++++++++++++------- 2 files changed, 43 insertions(+), 7 deletions(-) (limited to 'engine') diff --git a/engine/lib/entities.php b/engine/lib/entities.php index d71cf2170..9d4e9c538 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -252,6 +252,28 @@ return get_annotations_max($this->getGUID(), "","",$name); } + /** + * Gets an array of entities from a specific relationship type + * + * @param string $relationship Relationship type (eg "friends") + * @param int $limit Number of elements to return + * @param int $offset Indexing offset + * @return array|false An array of entities or false on failure + */ + function getEntitiesFromRelationship($relationship, $limit = 50, $offset = 0) { + return get_entities_from_relationship($relationship,$this->getGUID(),false,"","",9,"time_created desc",$limit,$offset); + } + + /** + * Gets the number of of entities from a specific relationship type + * + * @param string $relationship Relationship type (eg "friends") + * @return int|false The number of entities or false on failure + */ + function countEntitiesFromRelationship($relationship) { + return get_entities_from_relationship($relationship,$this->getGUID(),false,"","",9,"time_created desc",$limit,$offset,true); + } + /** * Determines whether or not the specified user (by default the current one) can edit the entity * diff --git a/engine/lib/relationships.php b/engine/lib/relationships.php index c1d2af71f..a3bb10ca1 100644 --- a/engine/lib/relationships.php +++ b/engine/lib/relationships.php @@ -372,9 +372,11 @@ * @param int $owner_guid * @param string $order_by * @param int $limit - * @param int $offset + * @param int $offset + * @param boolean $count Set to true if you want to count the number of entities instead (default false) + * @return array|int|false An array of entities, or the number of entities, or false on failure */ - 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) + 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, $count = false) { global $CONFIG; @@ -407,14 +409,26 @@ $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 "; + + if ($count) { + $query = "select count(distinct e.id) as total "; + } else { + $query = "select distinct e.* "; + } + $query .= " 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 $limit, $offset"; // Add order and limit + $query .= " (e.access_id in {$access} or (e.access_id = 0 and e.owner_guid = {$_SESSION['id']}))"; // Add access controls + if (!$count) { + $query .= " order by $order_by limit $limit, $offset"; // Add order and limit + return get_data($query, "entity_row_to_elggstar"); + } else { + if ($count = get_data_row($query)) { + return $count->total; + } + } + return false; - return get_data($query, "entity_row_to_elggstar"); } /** -- cgit v1.2.3