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) »
  • Foreach Loop on a Contact's Groups
Pages: [1]

Author Topic: Foreach Loop on a Contact's Groups  (Read 363 times)

zesgar

  • I post occasionally
  • **
  • Posts: 107
  • Karma: 2
  • CiviCRM version: 4.3.4
  • CMS version: Joomla 2.5
Foreach Loop on a Contact's Groups
August 03, 2014, 03:41:05 pm
I am trying to pull the [title] out of the groups array when using the get group contact api call.  For each of the group [title] s I want to perform a function to add that group to an outside API call.  Any ideas what is wrong with the foreach loop?
Code: [Select]
<?php
/**
 * @version
 * @package Civicrm
 * @subpackage Joomla Plugin
 * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class 
plgCiviCRMBase extends JPlugin
{

/**
 * Base Civicrm Plugin.
 *
 * @package Civicrm
 * @subpackage Joomla plugins
 * @since 1.6
 */

  

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';
$API_KEY = '----';
$PROJECT_ID = '----';
$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
// Most likely need to have a for each as most contacts will be part of multiple groups
//? Now how do I get each of the groups and then add the contact below for each group. for the contact?
foreach ($result as &$groupt){

$group = $project->getOrCreateGroup($groupt['title']);
        
$contact->addToGroup($group); 
}


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



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


Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Foreach Loop on a Contact's Groups
August 04, 2014, 02:26:08 am
Should be
Code: [Select]
foreach ($result['values'] as $groupt){ not
Code: [Select]
foreach ($result as &$groupt){
Try asking your question on the new CiviCRM help site.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Foreach Loop on a Contact's Groups

This forum was archived on 2017-11-26.