aboutsummaryrefslogtreecommitdiff
path: root/engine/lib
diff options
context:
space:
mode:
Diffstat (limited to 'engine/lib')
-rw-r--r--engine/lib/entities.php22
-rw-r--r--engine/lib/relationships.php28
2 files changed, 43 insertions, 7 deletions
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
@@ -253,6 +253,28 @@
}
/**
+ * 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
*
* @param int $user_guid The user GUID, optionally (defaults to the currently logged in user)
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");
}
/**