View source
<?php
include_once drupal_get_path('module', 'user_relationships') . '/user_relationships_api.inc';
function user_relationship_service_service() {
return array(
array(
'#method' => 'user_relationships.types',
'#callback' => 'user_relationship_service_types',
'#args' => array(
array(
'#name' => 'version',
'#type' => 'int',
'#description' => t('API version to use'),
),
),
'#return' => 'array',
'#help' => t('Get a list of relationship types'),
),
array(
'#method' => 'user_relationships.mine',
'#callback' => 'user_relationship_service_mine',
'#args' => array(
array(
'#name' => 'version',
'#type' => 'int',
'#description' => t('API version to use'),
),
),
'#return' => 'array',
'#help' => t('Get a list of my relationships'),
),
array(
'#method' => 'user_relationships.delete',
'#callback' => 'user_relationship_service_delete',
'#args' => array(
array(
'#name' => 'version',
'#type' => 'int',
'#description' => t('API version to use'),
),
array(
'#name' => 'rid',
'#type' => 'int',
'#description' => t('Relationship ID to delete'),
),
array(
'#name' => 'reason',
'#type' => 'string',
'#description' => t('Reason for deletion (cancel, disapprove, remove)'),
),
),
'#return' => 'struct',
'#help' => t('Delete an existing or pending relationship'),
),
array(
'#method' => 'user_relationships.approve',
'#callback' => 'user_relationship_service_approve',
'#args' => array(
array(
'#name' => 'version',
'#type' => 'int',
'#description' => t('API version to use'),
),
array(
'#name' => 'rid',
'#type' => 'int',
'#description' => t('Relationship ID to approve'),
),
),
'#return' => 'struct',
'#help' => t('Approve a requested relationship'),
),
array(
'#method' => 'user_relationships.request',
'#callback' => 'user_relationship_service_request',
'#args' => array(
array(
'#name' => 'version',
'#type' => 'int',
'#description' => t('API version to use'),
),
array(
'#name' => 'uid',
'#type' => 'int',
'#description' => t('UID to request a relationship with'),
),
array(
'#name' => 'type',
'#type' => 'string',
'#description' => t('Name of relationship type to create'),
),
),
'#return' => 'struct',
'#help' => t('Request a relationship with another user'),
),
);
}
function user_relationship_service_types($ver) {
return user_relationships_types_load();
}
function user_relationship_service_mine($ver) {
try {
global $user;
$param = array(
'user' => $user->uid,
);
$rels = user_relationships_load($param);
if (!$rels || !is_array($rels)) {
throw new Exception("User relationship load failed");
}
return array_values($rels);
} catch (Exception $ex) {
return services_error(t('Error getting user relationships: @msg', array(
'@msg' => $ex
->getMessage(),
)));
}
}
function user_relationship_service_approve($ver, $rid) {
try {
global $user;
$rels = user_relationships_load(array(
'rid' => $rid,
'requestee_id' => $user->uid,
'approved' => 0,
));
if (!$rels || !is_array($rels) || count($rels) != 1) {
throw new Exception("User relationship load failed");
}
$rel = array_shift($rels);
if ($rel->requestee_id != $user->uid) {
throw new Exception("Access denied");
}
$updated = $rel;
$updated->approved = TRUE;
user_relationships_update_relationship($rel, $updated, 'approved');
return $rel;
} catch (Exception $ex) {
return services_error(t('Error approving relationship: @msg', array(
'@msg' => $ex
->getMessage(),
)));
}
}
function user_relationship_service_delete($ver, $rid, $reason) {
try {
global $user;
$rels = user_relationships_load(array(
'rid' => $rid,
'user' => $user->uid,
));
if (!$rels || !is_array($rels) || count($rels) != 1) {
throw new Exception("User relationship load failed");
}
$rel = array_shift($rels);
if ($rel->requestee_id != $user->uid && $rel->requester_id != $user->uid) {
throw new Exception("Access denied");
}
user_relationships_delete_relationship($rel, $user, $reason);
return $rel;
} catch (Exception $ex) {
return services_error(t('Error removing relationship: @msg', array(
'@msg' => $ex
->getMessage(),
)));
}
}
function user_relationship_service_request($ver, $uid, $type_name) {
try {
$type = user_relationships_type_load(array(
'name' => $type_name,
));
error_log("Relationship request with '{$uid}' type '{$type_name}'");
if (!$type) {
throw new Exception(t('No such relationship type'));
}
global $user;
$ret = user_relationships_request_relationship($user, $uid, $type);
if (!$ret) {
throw new Exception(t('Unknown failure'));
}
else {
if (!is_object($ret)) {
throw new Exception($ret);
}
}
return $ret;
} catch (Exception $ex) {
return services_error(t('Error requesting relationship: @msg', array(
'@msg' => $ex
->getMessage(),
)));
}
}