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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Create Relationship when adding a New Activity
Pages: [1]

Author Topic: Create Relationship when adding a New Activity  (Read 791 times)

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Create Relationship when adding a New Activity
August 31, 2010, 12:46:45 pm
I have a need to create a relationship between two Individuals when a specific Activity is created.

The Activity is "Marriage" the relationship should be created as "Spouse Of".

When I create the Activity I enter both Individuals in the With Contact field. The start date of the relationship should be the date of the Activity.

I'm not a programmer by trade but can usually piece things together in a pinch. Wondering if someone could give me some direction on how to proceed or if someone has some code that would do this with a little editing?

Thanks

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Create Relationship when adding a New Activity
August 31, 2010, 01:30:31 pm
Hi

Assuming you are using drupal you will need to write a Drupal module & call a hook - probably civicrm_post

http://wiki.civicrm.org/confluence/display/CRMDOC32/CiviCRM+hook+specification

You can use the api to create the relationship - some sample code

http://wiki.civicrm.org/confluence/display/CRMDOC32/Relationship+APIs
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Create Relationship when adding a New Activity
August 31, 2010, 05:06:03 pm
Thanks Eileen. That got me started. Sorry I forgot to say but yes I am using Drupal 6.x and CiviCRM 3.2

I used FatherShawn's civi relate code (at least part of it) to start from. The code below is where I'm at. Could anyone help?

Code: [Select]
<?php
/* Implementation of hook_civicrm_postProcess()
 * Hook documentation is found at 
 * http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmpostProcess
 *
 * @param $formName
 *   string - the name of the form
 * @param $form
 *   object - reference to the form object
 */
function civi_activity_relate_civicrm_postProcess( $formName, &$form ) {

  
civi_activity_relate_prep();
  
// Not sure if this is correct or not.
  
$action = $form->getVar('_action');
  
$atype = $form->getVar('_atype');


  
/*
   * We only want the relationship created if we are creating an Activity type 'Marriage' which is activity_type_id 42
   */
  
if ( $formName == 'Activity' &&
       
$action == 'add') {
    
// Get session data - CRM_Core_Session doesn't initiate a session if one already exists 
    
$session =& CRM_Core_Session::singleton( );
    
// Set $withcontacts_contact_id to contacts listed in the With Contact field. Now sure how to do this.
    
$withcontacts_contactID = $form->getVar( '_id' );


    
// I need help on how to sort the with contacts item. I'm assuming when multiple contacts are entered in With Contacts that it creates an array.   How to get the activity_date_time to the start_date?
    
$params = array(
      
'contact_id_a' => $withcontacts_contactID,
      
'contact_id_b' => $withcontacts_contactID,
      
'relationship_type_id' => 2,
      
'is_permission_a_b' => 1,
      
'is_active' => 1,
      
'start_date' => array('d' => idate(d), 'M' => idate(m), 'Y' => idate(Y))
    );
    
/*
     * Implementation of CiviCRM API function civicrm_relationship_create()
     * Documentation found at: http://wiki.civicrm.org/confluence/display/CRMDOC/Relationship+APIs
     *
     * @param $params
     *   'params' (array) Associative array of property name/value pairs to be inserted.
     */
    
$created_result = civicrm_relationship_create($params);
    if ( 
civicrm_error ( $result )) {
      return 
$created_result['error_message'];
    }
    else {
      return 
$created_result['id'];
    }
  }
}


?>
« Last Edit: August 31, 2010, 05:22:55 pm by FrTommy »

FrTommy

  • I post frequently
  • ***
  • Posts: 273
  • Karma: 2
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7.x
  • MySQL version: 5.5.32
  • PHP version: 5.3
Re: Create Relationship when adding a New Activity
September 01, 2010, 06:58:47 pm
Frustrated by this.......Can anyone help?

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Create Relationship when adding a New Activity

This forum was archived on 2017-11-26.