function user_relationships_save_relationship in User Relationships 7
Same name and namespace in other branches
- 5.3 user_relationships_api/user_relationships_api.api.inc \user_relationships_save_relationship()
- 6 user_relationships_api/user_relationships_api.api.inc \user_relationships_save_relationship()
Create or update a user relationship.
Parameters
$relationship: Object of the current relationship.
$action: The reason for the update (request, approve, update).
Return value
Object of the updated relationship or FALSE if the relationship wasn't able to save
7 calls to user_relationships_save_relationship()
- UserRelationshipsTestCase::testRelationshipAPI in ./
user_relationships.test - Test various relationship APi functions and params.
- UserRelationshipsTestCase::testUserRelationshipsLoadDuplicates in ./
user_relationships.test - user_relationships_request_relationship in ./
user_relationships.module - Request a new user relationship
- user_relationships_rules_action_create_relationship in user_relationships_rules/
user_relationships_rules.rules.inc - Action Implementation: Create relationship.
- user_relationships_ui_pending_requested_submit in user_relationships_ui/
user_relationships_ui.actions.inc - Approve, Decline, or Cancel a relationship request
File
- ./
user_relationships.module, line 629 - User Relationships API. Module shell.
Code
function user_relationships_save_relationship($relationship, $action = 'request') {
// Set basic info if it doesn't already exist.
if (!isset($relationship->flags)) {
$relationship->flags = UR_OK;
}
if (!isset($relationship->created)) {
$relationship->created = REQUEST_TIME;
}
$relationship->changed = REQUEST_TIME;
if ($action == 'approve') {
$relationship->approved = 1;
}
// Start a transaction so that we can roll back in case of an error.
$transaction = db_transaction();
try {
module_invoke_all('user_relationships_presave', $relationship);
$keys = array();
if (isset($relationship->rid)) {
// Delete possible requests coming the other direction.
// @todo: The API prevents this, can this be removed.
$relationship_type = user_relationships_type_load($relationship->rtid);
if (!$relationship_type->is_oneway) {
db_delete('user_relationships')
->condition('rtid', $relationship->rtid)
->condition('requester_id', $relationship->requestee_id)
->condition('requestee_id', $relationship->requester_id)
->execute();
}
$keys = array(
'rid',
);
}
else {
// For a new entry, get a new id.
$relationship->rid = db_next_id(db_query('SELECT MAX(rid) FROM {user_relationships}')
->fetchField());
}
// #454680 make sure that an update is performed if this is an existing
// relationship.
if (drupal_write_record('user_relationships', $relationship, $keys)) {
// If the relationship has been approved and is two-way we need
// to do a double entry for DB efficiency.
$relationship_type = user_relationships_type_load($relationship->rtid);
if ($relationship->approved && !$relationship_type->is_oneway) {
// Make sure a reversed relationship entry is created if it does not
// exist yet.
db_merge('user_relationships')
->key(array(
'rid' => $relationship->rid,
'requester_id' => $relationship->requestee_id,
'requestee_id' => $relationship->requester_id,
))
->fields(array(
'rtid' => $relationship->rtid,
'approved' => $relationship->approved,
'created' => $relationship->created,
'changed' => $relationship->changed,
'flags' => $relationship->flags,
))
->execute();
}
}
// Don't execute a different hook for update/insert since that is not
// relevant. The $action is important if someone needs to differentiate.
module_invoke_all('user_relationships_save', $relationship, $action);
return $relationship;
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception('user_relationships', $e);
return FALSE;
}
}