View source
<?php
function user_relationships_type_load($param = array()) {
if (!$param) {
return;
}
$types = user_relationships_types_load();
if (is_numeric($param)) {
return $types[$param];
}
foreach ($types as $type) {
$found = TRUE;
foreach ($param as $column => $value) {
$column = strtolower($column);
if ($column == 'name' || $column == 'plural_name') {
$value = strtolower($value);
$col_val = strtolower($type->{$column});
}
else {
$col_val = $type->{$column};
}
if ($col_val != $value) {
$found = FALSE;
break;
}
}
if ($found) {
return $type;
}
}
}
function user_relationships_types_load($reset = FALSE) {
static $relationship_types_list = array();
if ($reset || !sizeof($relationship_types_list)) {
$results = db_query("SELECT * FROM {user_relationship_types}");
while ($relationship = db_fetch_object($results)) {
$relationship_types_list[$relationship->rtid] = $relationship;
}
}
_user_relationships_invoke('load', $relationship_types_list, TRUE);
return $relationship_types_list;
}
function user_relationships_type_save(&$rtype) {
_user_relationships_invoke('presave', $rtype, TRUE);
$op = $rtype->rtid ? 'update' : 'insert';
if (($found_rt = user_relationships_type_load(array(
'name' => $rtype->name,
))) && ($op == 'update' ? $found_rt->rtid != $rtype->rtid : TRUE)) {
return FALSE;
}
if (!is_numeric($rtype->expires_val) || $rtype->expires_val < 0) {
$rtype->expires_val = 0;
}
if ($op == 'update') {
db_query("UPDATE {user_relationship_types} SET \n name = '%s', plural_name = '%s', is_oneway = %d, requires_approval = %d, expires_val = %d\n WHERE rtid = %d", $rtype->name, $rtype->plural_name, $rtype->is_oneway ? 1 : 0, $rtype->requires_approval ? 1 : 0, $rtype->expires_val, $rtype->rtid);
}
else {
$rtype->rtid = db_next_id('{user_relationship_types}_id');
db_query("INSERT INTO {user_relationship_types} (rtid, name, plural_name, is_oneway, requires_approval, expires_val)\n VALUES(%d, '%s', '%s', %d, %d, %d)", $rtype->rtid, $rtype->name, $rtype->plural_name, $rtype->is_oneway ? 1 : 0, $rtype->requires_approval ? 1 : 0, $rtype->expires_val);
}
_user_relationships_invoke($op, $rtype, TRUE);
}
function user_relationships_type_delete($rtid) {
$rtype = user_relationships_type_load($rtid);
db_query('DELETE FROM {user_relationship_types} WHERE rtid = %d', $rtid);
db_query('DELETE FROM {user_relationships} WHERE rtid = %d', $rtid);
_user_relationships_invoke('delete', $rtype, TRUE);
}
function user_relationships_request_relationship($requester, $requestee, $type, $approved = FALSE) {
foreach (array(
'requester' => $requester,
'requestee' => $requestee,
'type' => $type,
) as $key => $value) {
if (!is_object($value)) {
${$key} = $key == 'type' ? user_relationships_type_load($value) : user_load($value);
}
}
if (!user_relationships_load(array(
'between' => array(
$requester->uid,
$requestee->uid,
),
'rtid' => $type->rtid,
), array(
'count' => TRUE,
))) {
$relationship = (object) array(
'requester_id' => $requester->uid,
'requestee_id' => $requestee->uid,
'approved' => $approved || !$type->requires_approval,
'rtid' => $type->rtid,
);
return user_relationships_save_relationship($relationship);
}
}
function user_relationships_save_relationship(&$relationship, $op = 'request') {
!isset($relationship->flags) ? $relationship->flags = UR_OK : NULL;
!isset($relationship->created_at) ? $relationship->created_at = time() : NULL;
$relationship->updated_at = time();
_user_relationships_invoke('presave', $relationship);
if ($relationship->rid) {
db_query("DELETE FROM {user_relationships} WHERE rid = %d", $relationship->rid);
$relationship_type = user_relationships_type_load($relationship->rtid);
if (!$relationship_type->is_oneway) {
db_query('DELETE FROM {user_relationships} WHERE rtid = %d AND requester_id = %d AND requestee_id = %d', $relationship->rtid, $relationship->requestee_id, $relationship->requester_id);
}
}
else {
$relationship->rid = db_next_id('{user_relationships}_id');
}
$queries = array();
$relationship_type = user_relationships_type_load($relationship->rtid);
$query = 'INSERT INTO {user_relationships} (rid, requester_id, requestee_id, rtid, approved, created_at, updated_at, flags) VALUES';
$insertions = '(%d, %d, %d, %d, %d, %d, %d, %d)';
$arguments = array(
$relationship->rid,
$relationship->requester_id,
$relationship->requestee_id,
$relationship->rtid,
$relationship->approved,
$relationship->created_at,
time(),
$relationship->flags,
);
$queries["{$query} {$insertions}"] = $arguments;
if ($relationship->approved && !$relationship_type->is_oneway) {
$query = str_replace('requester_id, requestee_id', 'requestee_id, requester_id', $query);
$queries["{$query} {$insertions}"] = $arguments;
}
$valid = TRUE;
foreach ($queries as $query => $arguments) {
if (db_query($query, $arguments) === FALSE) {
$valid = FALSE;
break;
}
}
if (!$valid) {
db_query('DELETE FROM {user_relationships} WHERE rid = %d', $relationship->rid);
unset($relationship->rid);
return FALSE;
}
else {
_user_relationships_invoke($op, $relationship);
return $relationship;
}
}
function user_relationships_delete_relationship(&$relationship, &$deleted_by, $op = 'remove') {
$relationship->deleted_by = $deleted_by;
db_query("DELETE FROM {user_relationships} WHERE rid = %d", $relationship->rid);
_user_relationships_invoke($op, $relationship);
}
function user_relationships_load($param = array(), $options = array(), $reset = FALSE) {
static $relationships = array();
$default_options = array(
'sort' => 'rid',
);
$options = array_merge($default_options, $options);
extract($options, EXTR_SKIP);
$arguments = array();
if (is_numeric($param)) {
if (!$reset && isset($relationships[$param])) {
return is_object($relationships[$param]) ? drupal_clone($relationships[$param]) : $relationships[$param];
}
$rid = $param;
$param = array(
'rid' => $param,
);
}
$query = _user_relationships_generate_query($param, $options);
$results = db_query($query[$count ? 'count' : 'query'], $query['arguments']);
if ($count) {
return (int) db_result($results);
}
$relationships = array();
while ($relationship = db_fetch_object($results)) {
if ($include_user_info) {
user_relationships_api_translate_user_info($relationship);
}
if ($sort == 'rid') {
$relationships[$relationship->{$sort}] = $relationship;
}
else {
$relationships[$relationship->{$sort}][] = $relationship;
}
}
$return = $rid ? $relationships[$rid] : $relationships;
_user_relationships_invoke('load', $return);
return $return;
}
function user_relationships_api_translate_user_info(&$relationship) {
if ($relationship->requester_name) {
foreach (array(
'requester',
'requestee',
) as $user_type) {
$relationship->{$user_type} = new stdClass();
foreach (array(
'name',
'mail',
'data',
'picture',
) as $field) {
$db_field = "{$user_type}_{$field}";
$relationship->{$user_type}->{$field} = $relationship->{$db_field};
unset($relationship->{$db_field});
}
$user_type_id = "{$user_type}_id";
$relationship->{$user_type}->uid = $relationship->{$user_type_id};
$relationship->{$user_type}->data = unserialize($relationship->{$user_type}->data);
}
}
}