diff options
-rw-r--r-- | engine/lib/relationships.php | 163 |
1 files changed, 113 insertions, 50 deletions
diff --git a/engine/lib/relationships.php b/engine/lib/relationships.php index ba6f0182f..0d21793a3 100644 --- a/engine/lib/relationships.php +++ b/engine/lib/relationships.php @@ -363,35 +363,35 @@ $guid_two = (int)$guid_two; return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid_one and relationship='$relationship' and guid_two=$guid_two"); - }
-
- /**
- * Removes all arbitrary relationships originating from a particular entity
- *
- * @param int $guid_one The GUID of the entity
- * @param string $relationship The name of the relationship (optionally)
- * @param true|false $inverse Whether we're deleting inverse relationships (default false)
- * @return true|false Depending on success
- */
- function remove_entity_relationships($guid_one, $relationship = "", $inverse = false) {
-
- global $CONFIG;
-
- $guid_one = (int) $guid_one;
-
- if (!empty($relationship)) {
- $relationship = sanitise_string($relationship);
- $where = "and relationship='$relationship'";
- } else {
- $where = "";
- }
-
- if (!$inverse) {
- return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid_one {$where}");
- } else {
- return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_two=$guid_one {$where}");
- }
-
+ } + + /** + * Removes all arbitrary relationships originating from a particular entity + * + * @param int $guid_one The GUID of the entity + * @param string $relationship The name of the relationship (optionally) + * @param true|false $inverse Whether we're deleting inverse relationships (default false) + * @return true|false Depending on success + */ + function remove_entity_relationships($guid_one, $relationship = "", $inverse = false) { + + global $CONFIG; + + $guid_one = (int) $guid_one; + + if (!empty($relationship)) { + $relationship = sanitise_string($relationship); + $where = "and relationship='$relationship'"; + } else { + $where = ""; + } + + if (!$inverse) { + return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid_one {$where}"); + } else { + return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_two=$guid_one {$where}"); + } + } /** @@ -421,9 +421,9 @@ * @param int $owner_guid * @param string $order_by * @param int $limit - * @param int $offset
- * @param boolean $count Set to true if you want to count the number of entities instead (default false)
- * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites.
+ * @param int $offset + * @param boolean $count Set to true if you want to count the number of entities instead (default false) + * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites. * @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, $count = false, $site_guid = 0) @@ -438,9 +438,9 @@ $owner_guid = (int)$owner_guid; $order_by = sanitise_string($order_by); $limit = (int)$limit; - $offset = (int)$offset;
- $site_guid = (int) $site_guid;
- if ($site_guid == 0)
+ $offset = (int)$offset; + $site_guid = (int) $site_guid; + if ($site_guid == 0) $site_guid = $CONFIG->site_guid; $access = get_access_list(); @@ -456,36 +456,99 @@ if ($subtype) $where[] = "e.subtype=$subtype"; if ($owner_guid != "") - $where[] = "e.owner_guid='$owner_guid'";
- if ($site_guid > 0)
+ $where[] = "e.owner_guid='$owner_guid'"; + if ($site_guid > 0) $where[] = "e.site_guid = {$site_guid}"; // Select what we're joining based on the options $joinon = "e.guid = r.guid_one"; if (!$inverse_relationship) $joinon = "e.guid = r.guid_two"; -
- if ($count) {
- $query = "select count(distinct e.guid) as total ";
- } else {
- $query = "select distinct e.* ";
- }
+ + if ($count) { + $query = "select count(distinct e.guid) as total "; + } else { + $query = "select distinct e.* "; + } $query .= " from {$CONFIG->dbprefix}entity_relationships r JOIN {$CONFIG->dbprefix}entities e 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 .= " (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 $offset, $limit"; // Add order and limit
- return get_data($query, "entity_row_to_elggstar");
- } else {
- if ($count = get_data_row($query)) {
- return $count->total;
- }
+ $query .= " order by $order_by limit $offset, $limit"; // Add order and limit + return get_data($query, "entity_row_to_elggstar"); + } else { + if ($count = get_data_row($query)) { + return $count->total; + } } return false; } + /**** HELPER FUNCTIONS FOR RELATIONSHIPS OF TYPE 'ATTACHED' ****/ + + /** + * Function to determine if the object trying to attach to other, has already done so + * @param int $guid_one This is the target object + * @param int $guid_two This is the object trying to attach to $guid_one + * @return true | false + **/ + + function already_attached($guid_one, $guid_two){ + + if($attached = check_entity_relationship($guid_one, "attached", $guid_two)){ + return true; + }else{ + return false; + } + } + + /** + * Function to get all objects attached to a particular object + * @param int $guid + * @param string $type - the type of object to return e.g. 'file', 'friend_of' etc + * @return an array of objects + **/ + + function get_attachments($guid, $type=""){ + + $attached = get_entities_from_relationship("attached", $guid, $inverse_relationship = false, $type, $subtype = "", $owner_guid = 0, $order_by = "time_created desc", $limit = 10, $offset = 0, $count = false, $site_guid = 0); + return $attached; + + } + + /** + * Function to remove a particular attachment between two objects + * @param int $guid_one This is the target object + * @param int $guid_two This is the object to remove from $guid_one + * @return a view + **/ + + function remove_attachment($guid_one, $guid_two){ + + if(already_attached($guid_one, $guid_two)) + remove_entity_relationship($guid_one, "attached", $guid_two); + + } + + + + /** + * Function to start the process of attaching one object to another + * @param int $guid_one This is the target object + * @param int $guid_two This is the object trying to attach to $guid_one + * @return a view + **/ + + function make_attachment($guid_one, $guid_two){ + + if(!(already_attached($guid_one, $guid_two))) + if(add_entity_relationship($guid_one, "attached", $guid_two)) + return true; + + } + /** * Handler called by trigger_plugin_hook on the "import" event. */ |