aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/relationships.php
diff options
context:
space:
mode:
Diffstat (limited to 'engine/lib/relationships.php')
-rw-r--r--engine/lib/relationships.php431
1 files changed, 99 insertions, 332 deletions
diff --git a/engine/lib/relationships.php b/engine/lib/relationships.php
index e09493c74..b0cd627fc 100644
--- a/engine/lib/relationships.php
+++ b/engine/lib/relationships.php
@@ -12,7 +12,8 @@
*
* @param stdClass $row Database row from the relationship table
*
- * @return stdClass or ElggMetadata
+ * @return ElggRelationship|stdClass
+ * @access private
*/
function row_to_elggrelationship($row) {
if (!($row instanceof stdClass)) {
@@ -27,7 +28,7 @@ function row_to_elggrelationship($row) {
*
* @param int $id The ID of a relationship
*
- * @return mixed
+ * @return ElggRelationship|false
*/
function get_relationship($id) {
global $CONFIG;
@@ -52,7 +53,7 @@ function delete_relationship($id) {
$relationship = get_relationship($id);
- if (trigger_elgg_event('delete', 'relationship', $relationship)) {
+ if (elgg_trigger_event('delete', 'relationship', $relationship)) {
return delete_data("delete from {$CONFIG->dbprefix}entity_relationships where id=$id");
}
@@ -90,7 +91,7 @@ function add_entity_relationship($guid_one, $relationship, $guid_two) {
if ($result !== false) {
$obj = get_relationship($result);
- if (trigger_elgg_event('create', $relationship, $obj)) {
+ if (elgg_trigger_event('create', $relationship, $obj)) {
return true;
} else {
delete_relationship($result);
@@ -108,7 +109,7 @@ function add_entity_relationship($guid_one, $relationship, $guid_two) {
* @param string $relationship The type of relationship
* @param int $guid_two The GUID of the entity the relationship is with
*
- * @return object|false Depending on success
+ * @return ElggRelationship|false Depending on success
*/
function check_entity_relationship($guid_one, $relationship, $guid_two) {
global $CONFIG;
@@ -122,7 +123,7 @@ function check_entity_relationship($guid_one, $relationship, $guid_two) {
AND relationship='$relationship'
AND guid_two=$guid_two limit 1";
- $row = $row = get_data_row($query);
+ $row = row_to_elggrelationship(get_data_row($query));
if ($row) {
return $row;
}
@@ -151,13 +152,13 @@ function remove_entity_relationship($guid_one, $relationship, $guid_two) {
return false;
}
- if (trigger_elgg_event('delete', $relationship, $obj)) {
+ if (elgg_trigger_event('delete', $relationship, $obj)) {
$query = "DELETE from {$CONFIG->dbprefix}entity_relationships
where guid_one=$guid_one
and relationship='$relationship'
and guid_two=$guid_two";
- return delete_data($query);
+ return (bool)delete_data($query);
} else {
return false;
}
@@ -219,7 +220,7 @@ function remove_entity_relationships($guid_one, $relationship = "", $inverse = f
* @param int $guid The GUID of the relationship owner
* @param bool $inverse_relationship Inverse relationship owners?
*
- * @return mixed
+ * @return ElggRelationship[]
*/
function get_entity_relationships($guid, $inverse_relationship = FALSE) {
global $CONFIG;
@@ -233,9 +234,22 @@ function get_entity_relationships($guid, $inverse_relationship = FALSE) {
return get_data($query, "row_to_elggrelationship");
}
-
/**
* Return entities matching a given query joining against a relationship.
+ * Also accepts all options available to elgg_get_entities() and
+ * elgg_get_entities_from_metadata().
+ *
+ * To ask for entities that do not have a particulat relationship to an entity,
+ * use a custom where clause like the following:
+ *
+ * $options['wheres'][] = "NOT EXISTS (
+ * SELECT 1 FROM {$db_prefix}entity_relationships
+ * WHERE guid_one = e.guid
+ * AND relationship = '$relationship'
+ * )";
+ *
+ * @see elgg_get_entities
+ * @see elgg_get_entities_from_metadata
*
* @param array $options Array in format:
*
@@ -245,7 +259,7 @@ function get_entity_relationships($guid, $inverse_relationship = FALSE) {
*
* inverse_relationship => BOOL Inverse the relationship
*
- * @return array
+ * @return ElggEntity[]|mixed If count, int. If not count, array. false on errors.
* @since 1.7.0
*/
function elgg_get_entities_from_relationship($options) {
@@ -257,7 +271,7 @@ function elgg_get_entities_from_relationship($options) {
$options = array_merge($defaults, $options);
- $clauses = elgg_get_entity_relationship_where_sql('e', $options['relationship'],
+ $clauses = elgg_get_entity_relationship_where_sql('e.guid', $options['relationship'],
$options['relationship_guid'], $options['inverse_relationship']);
if ($clauses) {
@@ -278,6 +292,16 @@ function elgg_get_entities_from_relationship($options) {
}
$options['joins'] = array_merge($options['joins'], $clauses['joins']);
+
+ if (isset($options['selects']) && !is_array($options['selects'])) {
+ $options['selects'] = array($options['selects']);
+ } elseif (!isset($options['selects'])) {
+ $options['selects'] = array();
+ }
+
+ $select = array('r.id');
+
+ $options['selects'] = array_merge($options['selects'], $select);
}
return elgg_get_entities_from_metadata($options);
@@ -288,18 +312,20 @@ function elgg_get_entities_from_relationship($options) {
*
* @todo add support for multiple relationships and guids.
*
- * @param string $table Entities table name
+ * @param string $column Column name the guid should be checked against.
+ * Provide in table.column format.
* @param string $relationship Relationship string
* @param int $relationship_guid Entity guid to check
- * @param string $inverse_relationship Inverse relationship check?
+ * @param bool $inverse_relationship Inverse relationship check?
*
* @return mixed
* @since 1.7.0
+ * @access private
*/
-function elgg_get_entity_relationship_where_sql($table, $relationship = NULL,
+function elgg_get_entity_relationship_where_sql($column, $relationship = NULL,
$relationship_guid = NULL, $inverse_relationship = FALSE) {
- if ($relationship == NULL && $entity_guid == NULL) {
+ if ($relationship == NULL && $relationship_guid == NULL) {
return '';
}
@@ -309,9 +335,9 @@ $relationship_guid = NULL, $inverse_relationship = FALSE) {
$joins = array();
if ($inverse_relationship) {
- $joins[] = "JOIN {$CONFIG->dbprefix}entity_relationships r on r.guid_one = e.guid";
+ $joins[] = "JOIN {$CONFIG->dbprefix}entity_relationships r on r.guid_one = $column";
} else {
- $joins[] = "JOIN {$CONFIG->dbprefix}entity_relationships r on r.guid_two = e.guid";
+ $joins[] = "JOIN {$CONFIG->dbprefix}entity_relationships r on r.guid_two = $column";
}
if ($relationship) {
@@ -335,73 +361,9 @@ $relationship_guid = NULL, $inverse_relationship = FALSE) {
}
/**
- * Return entities from relationships
- *
- * @deprecated 1.7 Use elgg_get_entities_from_relationship()
- *
- * @param string $relationship The relationship type
- * @param int $relationship_guid The GUID of the relationship owner
- * @param bool $inverse_relationship Invert relationship?
- * @param string $type Entity type
- * @param string $subtype Entity subtype
- * @param int $owner_guid Entity owner GUID
- * @param string $order_by Order by clause
- * @param int $limit Limit
- * @param int $offset Offset
- * @param bool $count Return a count instead of entities?
- * @param int $site_guid Site GUID
- *
- * @return mixed
- */
-function get_entities_from_relationship($relationship, $relationship_guid,
-$inverse_relationship = false, $type = "", $subtype = "", $owner_guid = 0,
-$order_by = "", $limit = 10, $offset = 0, $count = false, $site_guid = 0) {
-
- elgg_deprecated_notice('get_entities_from_relationship() was deprecated by elgg_get_entities_from_relationship()!', 1.7);
-
- $options = array();
-
- $options['relationship'] = $relationship;
- $options['relationship_guid'] = $relationship_guid;
- $options['inverse_relationship'] = $inverse_relationship;
-
- if ($type) {
- $options['types'] = $type;
- }
-
- if ($subtype) {
- $options['subtypes'] = $subtype;
- }
-
- if ($owner_guid) {
- $options['owner_guid'] = $owner_guid;
- }
-
- $options['limit'] = $limit;
-
- if ($offset) {
- $options['offset'] = $offset;
- }
-
- if ($order_by) {
- $options['order_by'];
- }
-
- if ($site_guid) {
- $options['site_guid'];
- }
-
- if ($count) {
- $options['count'] = $count;
- }
-
- return elgg_get_entities_from_relationship($options);
-}
-
-/**
* Returns a viewable list of entities by relationship
*
- * @param array $options
+ * @param array $options Options array for retrieval of entities
*
* @see elgg_list_entities()
* @see elgg_get_entities_from_relationship()
@@ -413,240 +375,48 @@ function elgg_list_entities_from_relationship(array $options = array()) {
}
/**
- * @deprecated 1.8 Use elgg_list_entities_from_relationship()
- */
-function list_entities_from_relationship($relationship, $relationship_guid,
-$inverse_relationship = false, $type = ELGG_ENTITIES_ANY_VALUE,
-$subtype = ELGG_ENTITIES_ANY_VALUE, $owner_guid = 0, $limit = 10,
-$fullview = true, $viewtypetoggle = false, $pagination = true) {
-
- elgg_deprecated_notice("list_entities_from_relationship was deprecated by elgg_list_entities_from_relationship()!", 1.8);
- return elgg_list_entities_from_relationship(array(
- 'relationship' => $relationship,
- 'relationship_guid' => $relationship_guid,
- 'inverse_relationship' => $inverse_relationship,
- 'types' => $type,
- 'subtypes' => $subtype,
- 'owner_guid' => $owner_guid,
- 'limit' => $limit,
- 'full_view' => $fullview,
- 'view_type_toggle' => $viewtypetoggle,
- 'pagination' => $pagination,
- ));
-}
-
-/**
* Gets the number of entities by a the number of entities related to them in a particular way.
* This is a good way to get out the users with the most friends, or the groups with the
* most members.
*
- * @param string $relationship The relationship eg "friends_of"
- * @param bool $inverse_relationship Inverse relationship owners
- * @param string $type The type of entity (default: all)
- * @param string $subtype The entity subtype (default: all)
- * @param int $owner_guid The owner of the entities (default: none)
- * @param int $limit Limit
- * @param int $offset Offset
- * @param bool $count Return a count instead of entities
- * @param int $site_guid Site GUID
- *
- * @return array|int|false An array of entities, or the number of entities, or false on failure
+ * @param array $options An options array compatible with
+ * elgg_get_entities_from_relationship()
+ * @return ElggEntity[]|mixed int If count, int. If not count, array. false on errors.
+ * @since 1.8.0
*/
-
-function get_entities_by_relationship_count($relationship, $inverse_relationship = true, $type = "",
-$subtype = "", $owner_guid = 0, $limit = 10, $offset = 0, $count = false, $site_guid = 0) {
-
- global $CONFIG;
-
- $relationship = sanitise_string($relationship);
- $inverse_relationship = (bool)$inverse_relationship;
- $type = sanitise_string($type);
- if ($subtype AND !$subtype = get_subtype_id($type, $subtype)) {
- return false;
- }
- $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) {
- $site_guid = $CONFIG->site_guid;
- }
-
- //$access = get_access_list();
-
- $where = array();
-
- if ($relationship != "") {
- $where[] = "r.relationship='$relationship'";
- }
-
- if ($inverse_relationship) {
- $on = 'e.guid = r.guid_two';
- } else {
- $on = 'e.guid = r.guid_one';
- }
- if ($type != "") {
- $where[] = "e.type='$type'";
- }
-
- if ($subtype) {
- $where[] = "e.subtype=$subtype";
- }
-
- if ($owner_guid != "") {
- $where[] = "e.container_guid='$owner_guid'";
- }
-
- if ($site_guid > 0) {
- $where[] = "e.site_guid = {$site_guid}";
- }
-
- if ($count) {
- $query = "SELECT count(distinct e.guid) as total ";
- } else {
- $query = "SELECT e.*, count(e.guid) as total ";
- }
-
- $query .= " from {$CONFIG->dbprefix}entity_relationships r
- JOIN {$CONFIG->dbprefix}entities e on {$on} where ";
-
- if (!empty($where)) {
- foreach ($where as $w) {
- $query .= " $w and ";
- }
- }
- $query .= get_access_sql_suffix("e");
-
- if (!$count) {
- $query .= " group by e.guid ";
- $query .= " order by total desc limit {$offset}, {$limit}";
- return get_data($query, "entity_row_to_elggstar");
- } else {
- if ($count = get_data_row($query)) {
- return $count->total;
- }
- }
-
- return false;
+function elgg_get_entities_from_relationship_count(array $options = array()) {
+ $options['selects'][] = "COUNT(e.guid) as total";
+ $options['group_by'] = 'r.guid_two';
+ $options['order_by'] = 'total desc';
+ return elgg_get_entities_from_relationship($options);
}
/**
- * Displays a human-readable list of entities
- *
- * @param string $relationship The relationship eg "friends_of"
- * @param bool $inverse_relationship Inverse relationship owners
- * @param string $type The type of entity (eg 'object')
- * @param string $subtype The entity subtype
- * @param int $owner_guid The owner (default: all)
- * @param int $limit The number of entities to display on a page
- * @param bool $fullview Whether or not to display the full view (default: true)
- * @param bool $viewtypetoggle Whether or not to allow gallery view
- * @param bool $pagination Whether to display pagination (default: true)
+ * Returns a list of entities by relationship count
*
- * @return string The viewable list of entities
+ * @see elgg_get_entities_from_relationship_count()
+ *
+ * @param array $options Options array
+ *
+ * @return string
+ * @since 1.8.0
*/
-
-function list_entities_by_relationship_count($relationship, $inverse_relationship = true,
-$type = "", $subtype = "", $owner_guid = 0, $limit = 10, $fullview = true,
-$viewtypetoggle = false, $pagination = true) {
-
- $limit = (int) $limit;
- $offset = (int) get_input('offset');
- $count = get_entities_by_relationship_count($relationship, $inverse_relationship,
- $type, $subtype, $owner_guid, 0, 0, true);
- $entities = get_entities_by_relationship_count($relationship, $inverse_relationship,
- $type, $subtype, $owner_guid, $limit, $offset);
-
- return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
-}
-
-/**
- * Gets the number of entities by a the number of entities related to
- * them in a particular way also constrained by metadata
- *
- * @param string $relationship The relationship eg "friends_of"
- * @param int $relationship_guid The guid of the entity to use query
- * @param bool $inverse_relationship Inverse relationship owner
- * @param String $meta_name The metadata name
- * @param String $meta_value The metadata value
- * @param string $type The type of entity (default: all)
- * @param string $subtype The entity subtype (default: all)
- * @param int $owner_guid The owner of the entities (default: none)
- * @param int $limit Limit
- * @param int $offset Offset
- * @param bool $count Return a count instead of entities
- * @param int $site_guid Site GUID
- *
- * @return array|int|false An array of entities, or the number of entities, or false on failure
- */
-function get_entities_from_relationships_and_meta($relationship, $relationship_guid,
-$inverse_relationship = false, $meta_name = "", $meta_value = "", $type = "",
-$subtype = "", $owner_guid = 0, $limit = 10, $offset = 0, $count = false, $site_guid = 0) {
-
- elgg_deprecated_notice('get_entities_from_relationship_and_meta() was deprecated by elgg_get_entities_from_relationship()!', 1.7);
-
- $options = array();
-
- $options['relationship'] = $relationship;
- $options['relationship_guid'] = $relationship_guid;
- $options['inverse_relationship'] = $inverse_relationship;
-
- if ($meta_value) {
- $options['values'] = $meta_value;
- }
-
- if ($entity_type) {
- $options['types'] = $entity_type;
- }
-
- if ($type) {
- $options['types'] = $type;
- }
-
- if ($subtype) {
- $options['subtypes'] = $subtype;
- }
-
- if ($owner_guid) {
- $options['owner_guid'] = $owner_guid;
- }
-
- if ($limit) {
- $options['limit'] = $limit;
- }
-
- if ($offset) {
- $options['offset'] = $offset;
- }
-
- if ($order_by) {
- $options['order_by'];
- }
-
- if ($site_guid) {
- $options['site_guid'];
- }
-
- if ($count) {
- $options['count'] = $count;
- }
-
- return elgg_get_entities_from_relationship($options);
+function elgg_list_entities_from_relationship_count($options) {
+ return elgg_list_entities($options, 'elgg_get_entities_from_relationship_count');
}
/**
* Sets the URL handler for a particular relationship type
*
- * @param string $function_name The function to register
* @param string $relationship_type The relationship type.
+ * @param string $function_name The function to register
*
* @return bool Depending on success
*/
-function register_relationship_url_handler($function_name, $relationship_type = "all") {
+function elgg_register_relationship_url_handler($relationship_type, $function_name) {
global $CONFIG;
- if (!is_callable($function_name)) {
+ if (!is_callable($function_name, true)) {
return false;
}
@@ -688,7 +458,7 @@ function get_relationship_url($id) {
}
if (is_callable($function)) {
- $url = $function($relationship);
+ $url = call_user_func($function, $relationship);
}
if ($url == "") {
@@ -713,7 +483,8 @@ function get_relationship_url($id) {
* @param int $guid_two This is the object trying to attach to $guid_one
*
* @return bool
- **/
+ * @access private
+ */
function already_attached($guid_one, $guid_two) {
if ($attached = check_entity_relationship($guid_one, "attached", $guid_two)) {
return true;
@@ -728,14 +499,15 @@ function already_attached($guid_one, $guid_two) {
* @param int $guid Entity GUID
* @param string $type The type of object to return e.g. 'file', 'friend_of' etc
*
- * @return an array of objects
-**/
+ * @return ElggEntity[]
+ * @access private
+ */
function get_attachments($guid, $type = "") {
$options = array(
'relationship' => 'attached',
'relationship_guid' => $guid,
'inverse_relationship' => false,
- 'types' => $type,
+ 'type' => $type,
'subtypes' => '',
'owner_guid' => 0,
'order_by' => 'time_created desc',
@@ -755,7 +527,8 @@ function get_attachments($guid, $type = "") {
* @param int $guid_two This is the object to remove from $guid_one
*
* @return void
-**/
+ * @access private
+ */
function remove_attachment($guid_one, $guid_two) {
if (already_attached($guid_one, $guid_two)) {
remove_entity_relationship($guid_one, "attached", $guid_two);
@@ -769,7 +542,8 @@ function remove_attachment($guid_one, $guid_two) {
* @param int $guid_two This is the object trying to attach to $guid_one
*
* @return true|void
-**/
+ * @access private
+ */
function make_attachment($guid_one, $guid_two) {
if (!(already_attached($guid_one, $guid_two))) {
if (add_entity_relationship($guid_one, "attached", $guid_two)) {
@@ -787,7 +561,7 @@ function make_attachment($guid_one, $guid_two) {
* @param mixed $params Array of params
*
* @return mixed
- *
+ * @access private
*/
function import_relationship_plugin_hook($hook, $entity_type, $returnvalue, $params) {
$element = $params['element'];
@@ -797,9 +571,8 @@ function import_relationship_plugin_hook($hook, $entity_type, $returnvalue, $par
if ($element instanceof ODDRelationship) {
$tmp = new ElggRelationship();
$tmp->import($element);
-
- return $tmp;
}
+ return $tmp;
}
/**
@@ -812,10 +585,10 @@ function import_relationship_plugin_hook($hook, $entity_type, $returnvalue, $par
*
* @elgg_event_handler export all
* @return mixed
+ * @throws InvalidParameterException
+ * @access private
*/
function export_relationship_plugin_hook($hook, $entity_type, $returnvalue, $params) {
- global $CONFIG;
-
// Sanity check values
if ((!is_array($params)) && (!isset($params['guid']))) {
throw new InvalidParameterException(elgg_echo('InvalidParameterException:GUIDNotForExport'));
@@ -839,38 +612,32 @@ function export_relationship_plugin_hook($hook, $entity_type, $returnvalue, $par
}
/**
- * An event listener which will notify users based on certain events.
+ * Notify user that someone has friended them
*
- * @param string $event Event name
- * @param string $object_type Object type
- * @param mixed $object Object
+ * @param string $event Event name
+ * @param string $type Object type
+ * @param mixed $object Object
*
* @return bool
+ * @access private
*/
-function relationship_notification_hook($event, $object_type, $object) {
- global $CONFIG;
+function relationship_notification_hook($event, $type, $object) {
+ /* @var ElggRelationship $object */
+ $user_one = get_entity($object->guid_one);
+ /* @var ElggUser $user_one */
- if (
- ($object instanceof ElggRelationship) &&
- ($event == 'create') &&
- ($object_type == 'friend')
- ) {
- $user_one = get_entity($object->guid_one);
- $user_two = get_entity($object->guid_two);
-
- // Notify target user
- return notify_user($object->guid_two, $object->guid_one,
+ return notify_user($object->guid_two,
+ $object->guid_one,
elgg_echo('friend:newfriend:subject', array($user_one->name)),
elgg_echo("friend:newfriend:body", array($user_one->name, $user_one->getURL()))
- );
- }
+ );
}
-/** Register the import hook */
-register_plugin_hook("import", "all", "import_relationship_plugin_hook", 3);
+// Register the import hook
+elgg_register_plugin_hook_handler("import", "all", "import_relationship_plugin_hook", 3);
-/** Register the hook, ensuring entities are serialised first */
-register_plugin_hook("export", "all", "export_relationship_plugin_hook", 3);
+// Register the hook, ensuring entities are serialised first
+elgg_register_plugin_hook_handler("export", "all", "export_relationship_plugin_hook", 3);
-/** Register event to listen to some events **/
-register_elgg_event_handler('create', 'friend', 'relationship_notification_hook');
+// Register event to listen to some events
+elgg_register_event_handler('create', 'friend', 'relationship_notification_hook');