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) »
  • Getting and Sending a Contacts Groups
Pages: [1]

Author Topic: Getting and Sending a Contacts Groups  (Read 476 times)

zesgar

  • I post occasionally
  • **
  • Posts: 107
  • Karma: 2
  • CiviCRM version: 4.3.4
  • CMS version: Joomla 2.5
Getting and Sending a Contacts Groups
August 01, 2014, 09:23:46 am
I am trying to add contacts to a group on a sms 3rd party software.  I can't seem to figure out how to grab each of the contacts groups and send them each to this 3rd party service.  Any ideas?
Code: [Select]
function civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

if (($op == 'create' || $op =='edit') && $objectName == 'Individual') {


civicrm_initialize( true );
require_once 'CRM/Core/Config.php';


$config =& CRM_Core_Config::singleton( );

//Telerivet API CONNECT
require_once JPATH_SITE.'/media/civicrm/telerivet/telerivet.php';
$API_KEY = '----';
$PROJECT_ID = '----';
$telerivet = new Telerivet_API($API_KEY);
$project = $telerivet->initProjectById($PROJECT_ID);
//CRM_Core_Error::debug($form);
//CRM_Core_Error::debug($objectRef);
//exit( );

//Get Variables Civicrm
    $firstName = $objectRef->first_name;
        $lastName = $objectRef->last_name;
 
$phone = $objectRef->phone[0]->phone;
$displayName = $objectRef->display_name;

//Get Groups with contact
$params = array(
  'contact_id' => $objectRef->id,
  'version' => 3,
);

  require_once 'api/api.php';
  $result = civicrm_api( 'group_contact','get',$params );

//? Now how do I get each of the groups for this contact and then add the contact below into each group?
 
 
  //return $result;
  //print_r($result);
//exit();

   // Telerivet Post New Contact to Telerivet
    $contact = $project->getOrCreateContact(array(
    'name' => $displayName,
    'phone_number' => $phone,
    'vars' => array('first' => $firstName, 'last' => $lastName)
));


// Add Contact to Telerivet Groups
// Most likely need to have a for each as most contacts will be part of multiple groups some how individually add the contact
//Then move to the next group in the array?  and add the contact to that group.
$group = $project->getOrCreateGroup("Subscribers");
        $contact->addToGroup($group);

}
}
}


JonGold

  • Ask me questions
  • ****
  • Posts: 638
  • Karma: 81
    • Palante Technology
  • CiviCRM version: 4.1 to the latest
  • CMS version: Drupal 6-7, Wordpress 4.0+
  • PHP version: PHP 5.3-5.5
Re: Getting and Sending a Contacts Groups
August 01, 2014, 03:23:02 pm
Hi Zesgar,

A few things:
* You don't have to call civicrm_initialize, singleton, or include Config.php from within a hook (I assume this is being written as an extension).
* $objectId has your contact's ID - you should use this to make an API call to pull up this info.
* I think it's great that you're learning Civi customization!  However, it looks like you may need to brush up on PHP fundamentals before taking on this project. 

Also, I would definitely check out the API Explorer and get a feel for making API calls before taking this project on - once you have a feel for the API via the explorer, this project of yours should fall into place!
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

JonGold

  • Ask me questions
  • ****
  • Posts: 638
  • Karma: 81
    • Palante Technology
  • CiviCRM version: 4.1 to the latest
  • CMS version: Drupal 6-7, Wordpress 4.0+
  • PHP version: PHP 5.3-5.5
Re: Getting and Sending a Contacts Groups
August 04, 2014, 08:40:58 am
Hi zesgar,

Two things:
* First, you should edit your last post to remove your API Key and probably the Project ID as well.  It's comparable to posting an administrator password in public, anyone reading your post could access your Telerivet API!
* To get the groups, make an API call to the GroupContact entity.  Use the API Explorer to do this in the GUI (it's a little harder in 4.4 than 4.5's API Explorer).  You'll want entity of "GroupContact", action of "Get", then edit the URL to get a single user's cid.  Once it works in the GUI, copy the PHP code it generates into your code and edit it to take the cid from $objectId.  You'll probably have to manipulate the data returned from the API call to fit the format that Telerivet is expecting, too.
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

zesgar

  • I post occasionally
  • **
  • Posts: 107
  • Karma: 2
  • CiviCRM version: 4.3.4
  • CMS version: Joomla 2.5
Re: Getting and Sending a Contacts Groups
August 04, 2014, 10:45:40 am
Thanks I figured it all out here.
Code: [Select]
function civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

if (($op == 'create' || $op =='edit') && $objectName == 'Individual') {

//Telerivet API CONNECT
require_once JPATH_SITE.'/media/civicrm/telerivet/telerivet.php';

$telerivet = new Telerivet_API($API_KEY);
$project = $telerivet->initProjectById($PROJECT_ID);


//Get Variables Civicrm
    $firstName = $objectRef->first_name;
$lastName = $objectRef->last_name;
  $phone = $objectRef->phone[0]->phone;
$displayName = $objectRef->display_name;

//Get Groups with contact
$params = array( 'contact_id' => $objectRef->id,'version' => 3,);
require_once 'api/api.php';
$result = civicrm_api( 'group_contact','get',$params );


   // Telerivet Post New Contact to Telerivet
    $contact = $project->getOrCreateContact(array(
    'name' => $displayName,
    'phone_number' => $phone,
    'vars' => array('first' => $firstName, 'last' => $lastName)
));



// Add Contact to Telerivet Groups



$keys = array_keys($result['values']);

foreach($keys as $k) {
    echo $k;
$grouptitle = $result['values'][$k]['title'];
$group = $project->getOrCreateGroup($grouptitle);
$contact->addToGroup($group);
}


//$group = $project->getOrCreateGroup("Subscribers");
        //$contact->addToGroup($group);



//print_r($groupt);
//exit();
}
}
}


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Getting and Sending a Contacts Groups

This forum was archived on 2017-11-26.