CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Improvement to v2 relationship API
Pages: [1]

Author Topic: Improvement to v2 relationship API  (Read 1741 times)

greenmachine

  • I post occasionally
  • **
  • Posts: 58
  • Karma: 6
Improvement to v2 relationship API
June 25, 2008, 06:14:42 pm
The old relationship API has a function called crm_get_relationships() that allowed you to grab a contact's relationships. However, it requires a contact object as input, and v2 API only deals in contact arrays (civicrm_contact_get, civicrm_contact_search).

So, here's a new version, civicrm_get_relationships() that you can stick in the v2/Relationship.php API file. I haven't tested much at all, but the code is pretty simple so it should work as before:

Code: [Select]
/**
 * Function to get the relationship
 *
 * @param array   $contact_a                  Contact object from civicrm_contact_get
 * @param array   $contact_b                  Contact object from civicrm_contact_get
 * @param array   $relationship_type_name     An array of Relationship Type Name.
 * @param array   $returnProperties           Which properties should be included in the related Contact object(s). If NULL, the default set of contact properties will be included.
 * @param array   $sort                       Associative array of one or more "property_name"=>"sort direction" pairs which will control order of Contact objects returned
 * @param int     $offset                     Starting row index.
 *
 * @return        Array of all relationship.
 *
 * @access  public
 *
 */
function civicrm_get_relationships($contact_a,
                               $contact_b=null,
                               $relationship_type_name = null,
                               $returnProperties = null,
                               $sort = null,
                               $offset = 0,
                               $row_count = 25 ) {

    if( ! isset( $contact_a['contact_id'] ) ) {
        return _crm_error('$contact_a is not valid contact datatype');
    }

    require_once 'CRM/Contact/BAO/Relationship.php';
    $contactID = $contact_a['contact_id'];
    $relationships = CRM_Contact_BAO_Relationship::getRelationship($contactID);

    if ( isset( $relationship_type_name ) && is_array( $relationship_type_name )  ){
        $result =array();
        foreach ( $relationship_type_name as $relationshipType ) {
            foreach( $relationships as $key => $relationship ) {
                if ( $relationship['relation'] ==  $relationshipType ) {
                    $result[$key] = $relationship;
                }
            }
        }
        $relationships = $result;
    }

    if( isset( $contact_b['contact_id']) ) {
        $cid = $contact_b['contact_id'];
        $result =array();
        foreach($relationships as $key => $relationship) {
            if ($relationship['cid'] == $cid ) {
                $result[$key] = $relationship;
            }
        }
        $relationships = $result;
    }

    return $relationships;
}

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Improvement to v2 relationship API
June 26, 2008, 01:30:02 am

jesse:

can u please file an issue and attach the code to the issue.

thanx

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

greenmachine

  • I post occasionally
  • **
  • Posts: 58
  • Karma: 6
Re: Improvement to v2 relationship API
July 11, 2008, 02:55:14 pm
Didn't see this before, but done now.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Improvement to v2 relationship API

This forum was archived on 2017-11-26.