/**
 * Get contact membership record.
 *
 * This api will return the membership records for the contacts
 * having membership based on the relationship with the direct members.
 *
 * @param  Array $params key/value pairs for contact_id and some
 *          options affecting the desired results; has legacy support
 *          for just passing the contact_id itself as the argument
 *
 * @return  Array of all found membership property values.
 * @access public
 * @todo needs some love - basically only a get for a given contact right now
 * {@getfields membership_get}
 */
function civicrm_api3_membership_get($params)
{
    $activeOnly = $membershipTypeId = $membershipType = NULL;
    $contactID = CRM_Utils_Array::value('contact_id', $params);
    if (!empty($params['filters']) && is_array($params['filters'])) {
        $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE);
    }
    $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
    if (!empty($params['contact_id']) && !is_array($params['contact_id'])) {
        $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $membershipTypeId, $activeOnly);
    } else {
        //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
        $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
    }
    $options = _civicrm_api3_get_options_from_params($params, TRUE, 'membership', 'get');
    $return = $options['return'];
    if (empty($membershipValues) || !empty($return) && !array_key_exists('related_contact_id', $return) && !array_key_exists('relationship_name', $return)) {
        return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get');
    }
    $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
    return civicrm_api3_create_success($members, $params, 'membership', 'get');
}
/**
 * Get contact membership record.
 *
 * This api will return the membership records for the contacts
 * having membership based on the relationship with the direct members.
 *
 * @param  Array $params key/value pairs for contact_id and some
 *          options affecting the desired results; has legacy support
 *          for just passing the contact_id itself as the argument
 *
 * @return  Array of all found membership property values.
 * @access public
 * @todo needs some love - basically only a get for a given contact right now
 * {@getfields membership_get}
 */
function civicrm_api3_membership_get($params)
{
    $contactID = $activeOnly = $membershipTypeId = $membershipType = NULL;
    $contactID = CRM_Utils_Array::value('contact_id', $params);
    if (is_array(CRM_Utils_Array::value('filters', $params)) && !empty($params['filters'])) {
        $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE);
    }
    $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
    //@todo replace this by handling in API layer - we should have enough info to do this
    // between pseudoconstant & fk - see comments in format_params
    $membershipTypeId = CRM_Utils_Array::value('membership_type_id', $params);
    if (!$membershipTypeId) {
        $membershipType = CRM_Utils_Array::value('membership_type', $params);
        if ($membershipType) {
            $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membershipType, 'id', 'name');
        }
    }
    if (CRM_Utils_Array::value('contact_id', $params)) {
        $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly);
    } else {
        //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
        $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
    }
    if (empty($membershipValues)) {
        # No results is NOT an error!
        return civicrm_api3_create_success($membershipValues, $params);
    }
    $relationships = array();
    foreach ($membershipValues as $membershipId => $values) {
        // populate the membership type name for the membership type id
        $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
        $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
        if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) {
            $relationships[$membershipType['relationship_type_id']] = $membershipId;
        }
        // populating relationship type name.
        $relationshipType = new CRM_Contact_BAO_RelationshipType();
        $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType);
        if ($relationshipType->find(TRUE)) {
            $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
        }
        _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
    }
    $members = $membershipValues;
    // populating contacts in members array based on their relationship with direct members.
    if (!empty($relationships)) {
        foreach ($relationships as $relTypeId => $membershipId) {
            // As members are not direct members, there should not be
            // membership id in the result array.
            unset($membershipValues[$membershipId]['id']);
            $relationship = new CRM_Contact_BAO_Relationship();
            $relationship->contact_id_b = $contactID;
            $relationship->relationship_type_id = $relTypeId;
            if ($relationship->find()) {
                while ($relationship->fetch()) {
                    clone $relationship;
                    $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
                    $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a;
                }
            }
        }
    }
    return civicrm_api3_create_success($members, $params, 'membership', 'get');
}