/**
 * retrieve the linked bug id of the relationship: provide source -> return destination; provide destination -> return source
 * @param integer $p_relationship_id Relationship id.
 * @param integer $p_bug_id          A bug identifier.
 * @return int Complementary bug id
 */
function relationship_get_linked_bug_id($p_relationship_id, $p_bug_id)
{
    $t_bug_relationship_data = relationship_get($p_relationship_id);
    if ($t_bug_relationship_data->src_bug_id == $p_bug_id) {
        return $t_bug_relationship_data->dest_bug_id;
    }
    if ($t_bug_relationship_data->dest_bug_id == $p_bug_id) {
        return $t_bug_relationship_data->src_bug_id;
    }
    trigger_error(ERROR_RELATIONSHIP_NOT_FOUND, ERROR);
}
# bug is not read-only...
if (bug_is_readonly($f_bug_id)) {
    error_parameters($f_bug_id);
    trigger_error(ERROR_BUG_READ_ONLY_ACTION_DENIED, ERROR);
}
# retrieve the destination bug of the relationship
$t_dest_bug_id = relationship_get_linked_bug_id($f_rel_id, $f_bug_id);
# user can access to the related bug at least as viewer, if it's exist...
if (bug_exists($t_dest_bug_id)) {
    if (!access_has_bug_level(VIEWER, $t_dest_bug_id)) {
        error_parameters($t_dest_bug_id);
        trigger_error(ERROR_RELATIONSHIP_ACCESS_LEVEL_TO_DEST_BUG_TOO_LOW, ERROR);
    }
}
helper_ensure_confirmed(lang_get('delete_relationship_sure_msg'), lang_get('delete_relationship_button'));
$t_bug_relationship_data = relationship_get($f_rel_id);
$t_rel_type = $t_bug_relationship_data->type;
# delete relationship from the DB
relationship_delete($f_rel_id);
# update bug last updated (just for the src bug)
bug_update_date($f_bug_id);
# set the rel_type for both bug and dest_bug based on $t_rel_type and on who is the dest bug
if ($f_bug_id == $t_bug_relationship_data->src_bug_id) {
    $t_bug_rel_type = $t_rel_type;
    $t_dest_bug_rel_type = relationship_get_complementary_type($t_rel_type);
} else {
    $t_bug_rel_type = relationship_get_complementary_type($t_rel_type);
    $t_dest_bug_rel_type = $t_rel_type;
}
# send email and update the history for the src issue
history_log_event_special($f_bug_id, BUG_DEL_RELATIONSHIP, $t_bug_rel_type, $t_dest_bug_id);
/**
 * Delete the relationship with the specified target id.
 *
 * @param string  $p_username        The name of the user trying to add a note to an issue.
 * @param string  $p_password        The password of the user.
 * @param integer $p_issue_id        The id of the source issue for the relationship.
 * @param integer $p_relationship_id The id of relationship to delete.
 * @return boolean true: success, false: failure
 */
function mc_issue_relationship_delete($p_username, $p_password, $p_issue_id, $p_relationship_id)
{
    global $g_project_override;
    $t_user_id = mci_check_login($p_username, $p_password);
    if ($t_user_id === false) {
        return mci_soap_fault_login_failed();
    }
    $t_project_id = bug_get_field($p_issue_id, 'project_id');
    $g_project_override = $t_project_id;
    if (!mci_has_readwrite_access($t_user_id, $t_project_id)) {
        return mci_soap_fault_access_denied($t_user_id);
    }
    # user has access to update the bug...
    if (!access_has_bug_level(config_get('update_bug_threshold'), $p_issue_id, $t_user_id)) {
        return mci_soap_fault_access_denied($t_user_id, 'Active user does not have access level required to remove a relationship from this issue.');
    }
    # bug is not read-only...
    if (bug_is_readonly($p_issue_id)) {
        return mci_soap_fault_access_denied($t_user_id, 'Issue \'' . $p_issue_id . '\' is readonly.');
    }
    # retrieve the destination bug of the relationship
    $t_dest_issue_id = relationship_get_linked_bug_id($p_relationship_id, $p_issue_id);
    # user can access to the related bug at least as viewer, if it's exist...
    if (bug_exists($t_dest_issue_id)) {
        if (!access_has_bug_level(config_get('view_bug_threshold', null, null, $t_project_id), $t_dest_issue_id, $t_user_id)) {
            return mci_soap_fault_access_denied($t_user_id, 'The issue \'' . $t_dest_issue_id . '\' requires higher access level.');
        }
    }
    $t_bug_relationship_data = relationship_get($p_relationship_id);
    $t_rel_type = $t_bug_relationship_data->type;
    # delete relationship from the DB
    log_event(LOG_WEBSERVICE, 'deleting relationship id \'' . $p_relationship_id . '\'');
    relationship_delete($p_relationship_id);
    # update bug last updated
    bug_update_date($p_issue_id);
    bug_update_date($t_dest_issue_id);
    # set the rel_type for both bug and dest_bug based on $t_rel_type and on who is the dest bug
    if ($p_issue_id == $t_bug_relationship_data->src_bug_id) {
        $t_bug_rel_type = $t_rel_type;
        $t_dest_bug_rel_type = relationship_get_complementary_type($t_rel_type);
    } else {
        $t_bug_rel_type = relationship_get_complementary_type($t_rel_type);
        $t_dest_bug_rel_type = $t_rel_type;
    }
    # send email and update the history for the src issue
    history_log_event_special($p_issue_id, BUG_DEL_RELATIONSHIP, $t_bug_rel_type, $t_dest_issue_id);
    email_relationship_deleted($p_issue_id, $t_dest_issue_id, $t_bug_rel_type);
    if (bug_exists($t_dest_issue_id)) {
        # send email and update the history for the dest issue
        history_log_event_special($t_dest_issue_id, BUG_DEL_RELATIONSHIP, $t_dest_bug_rel_type, $p_issue_id);
        email_relationship_deleted($t_dest_issue_id, $p_issue_id, $t_dest_bug_rel_type);
    }
    return true;
}
/**
 * Delete the relationship with the specified target id.
 *
 * @param string $p_username  The name of the user trying to add a note to an issue.
 * @param string $p_password  The password of the user.
 * @param integer $p_issue_id  The id of the source issue for the relationship
 * @param integer $p_relationship_id  The id of relationship to delete.
 * @return true: success, false: failure
 */
function mc_issue_relationship_delete($p_username, $p_password, $p_issue_id, $p_relationship_id)
{
    $t_user_id = mci_check_login($p_username, $p_password);
    if ($t_user_id === false) {
        return new soap_fault('Client', '', 'Access Denied');
    }
    $t_project_id = bug_get_field($p_issue_id, 'project_id');
    if (!mci_has_readwrite_access($t_user_id, $t_project_id)) {
        return new soap_fault('Client', '', 'Access Denied');
    }
    # user has access to update the bug...
    if (!access_has_bug_level(config_get('update_bug_threshold'), $p_issue_id, $t_user_id)) {
        return new soap_fault('Client', '', "Active user does not have access level required to remove a relationship from this issue.");
    }
    # bug is not read-only...
    if (bug_is_readonly($p_issue_id)) {
        return new soap_fault('Client', '', "Issue '{$p_issue_id}' is readonly.");
    }
    # retrieve the destination bug of the relationship
    $t_dest_issue_id = relationship_get_linked_bug_id($p_relationship_id, $p_issue_id);
    # user can access to the related bug at least as viewer, if it's exist...
    if (bug_exists($t_dest_issue_id)) {
        if (!access_has_bug_level(VIEWER, $t_dest_issue_id, $t_user_id)) {
            return new soap_fault('Client', '', "The issue '{$t_dest_issue_id}' requires higher access level.");
        }
    }
    $t_bug_relationship_data = relationship_get($p_relationship_id);
    $t_rel_type = $t_bug_relationship_data->type;
    # delete relationship from the DB
    relationship_delete($p_relationship_id);
    # update bug last updated (just for the src bug)
    bug_update_date($p_issue_id);
    # set the rel_type for both bug and dest_bug based on $t_rel_type and on who is the dest bug
    if ($p_issue_id == $t_bug_relationship_data->src_bug_id) {
        $t_bug_rel_type = $t_rel_type;
        $t_dest_bug_rel_type = relationship_get_complementary_type($t_rel_type);
    } else {
        $t_bug_rel_type = relationship_get_complementary_type($t_rel_type);
        $t_dest_bug_rel_type = $t_rel_type;
    }
    # send email and update the history for the src issue
    history_log_event_special($p_issue_id, BUG_DEL_RELATIONSHIP, $t_bug_rel_type, $t_dest_issue_id);
    email_relationship_deleted($p_issue_id, $t_dest_issue_id, $t_bug_rel_type);
    if (bug_exists($t_dest_issue_id)) {
        # send email and update the history for the dest issue
        history_log_event_special($t_dest_issue_id, BUG_DEL_RELATIONSHIP, $t_dest_bug_rel_type, $p_issue_id);
        email_relationship_deleted($t_dest_issue_id, $p_issue_id, $t_dest_bug_rel_type);
    }
    return true;
}