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) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Check of Contact in Smart Group
Pages: [1]

Author Topic: Check of Contact in Smart Group  (Read 1419 times)

FredJones

  • Guest
Check of Contact in Smart Group
July 21, 2008, 10:11:56 am
I need to determine, via PHP that is, if a contact is a member of a Smart Group. I don't think the API handles this--an examination of the code there appears to indicate that it only handles 'regular' groups.

Seems that I will need to extract the various clauses (where clause, select clause etc.) from the groups table and run the query manually. Is this correct?

If so, can anyone point me more specifically to where in the code these clauses are processed--I see them but it's not yet clear how to use them exactly. :)

Thanks!

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: Check of Contact in Smart Group
July 21, 2008, 02:07:06 pm

The easiest solution would be to use the search api and get all contact ids belonging to a smart group. Then check if your contact id is in that set. This is not very efficient

once u have the above working, you can check to see if you can get the query to add the contact id in the where clause. This should be possible if you go directly to the BAO/Query.php file (via the includeContactIds flag)

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

FredJones

  • Guest
Re: Check of Contact in Smart Group
July 21, 2008, 09:17:22 pm
Quote from: Donald Lobo on July 21, 2008, 02:07:06 pm

The easiest solution would be to use the search api and get all contact ids belonging to a smart group. Then check if your contact id is in that set. This is not very efficient

I thought of that also, but it doesn't seem to be working. Perhaps my code is wrong. For group id 17 I use this:

Code: [Select]
      require_once 'api/v2/Contact.php';
      $grpparams = array( 'group' => array(17 => 1),
       'return.contactId' => 1);
      $grouplist = civicrm_contact_search($grpparams);
print_r($grouplist);die;

(this code I basically copied from CRM/Contact/Bao/Group.php line 138 "static function getGroupContacts") and the results are

Code: [Select]
Array ( [1] => Array ( [contact_id] => 1 ) [3] => Array ( [contact_id] => 3 ) [24] => Array ( [contact_id] => 24 ) [56] => Array ( [contact_id] => 56 ) [71] => Array ( [contact_id] => 71 ) [81] => Array ( [contact_id] => 81 ) [88] => Array ( [contact_id] => 88 ) [93] => Array ( [contact_id] => 93 ) [119] => Array ( [contact_id] => 119 ) [181] => Array ( [contact_id] => 181 ) [293] => Array ( [contact_id] => 293 ) [299] => Array ( [contact_id] => 299 ) [314] => Array ( [contact_id] => 314 ) [334] => Array ( [contact_id] => 334 ) [339] => Array ( [contact_id] => 339 ) [385] => Array ( [contact_id] => 385 ) [439] => Array ( [contact_id] => 439 ) [442] => Array ( [contact_id] => 442 ) [471] => Array ( [contact_id] => 471 ) [472] => Array ( [contact_id] => 472 ) [535] => Array ( [contact_id] => 535 ) [570] => Array ( [contact_id] => 570 ) [571] => Array ( [contact_id] => 571 ) [572] => Array ( [contact_id] => 572 ) [574] => Array ( [contact_id] => 574 ) )

which is less than 30 members. But if I look at the URL

civicrm/group/search?q=civicrm/group/search&force=1&context=smog&gid=17&crmSID=6_u

I see

Code: [Select]
NameOfMyGroup (smart group) - Found 28266 group members

I was expecting civicrm_contact_search() to also return an array of 28266 ids. Do I have something wrong?

FredJones

  • Guest
Re: Check of Contact in Smart Group
July 22, 2008, 12:49:51 am
I look into the code more and I see that the API automatically only returns 25 rows. Anyhow, I was able to basically copy the code from "static function apiQuery" in CRM/Contact/Bao/Query.php and I removed the 25 rows limit and I added a contact_id specification. In case this function is of interest to anyone, this is what appears to be working for me:

Code: [Select]
function isContactInSmartGroup($gid,$contactid)
{
$grpparams = array( 'group' => array($gid => 1),'contact_id' => $contactid,
       'return.contactId' => 1);
$inputParams      = array( );
$returnProperties = array( );
$otherVars = array( 'sort', 'offset', 'rowCount' );
$sort     = null;
$offset   = 0;
$rowCount = 0; // this means NO limit
foreach ( $grpparams as $n => $v ) {
    if ( substr( $n, 0, 7 ) == 'return.' ) {
        $returnProperties[ substr( $n, 7 ) ] = $v;
    } elseif ( in_array( $n, $otherVars ) ) {
        $$n = $v;
    } else {
        $inputParams[$n] = $v;
    }
}
if ( empty( $returnProperties ) ) {
    $returnProperties = null;
}
require_once 'CRM/Contact/BAO/Query.php';
$newParams =& CRM_Contact_BAO_Query::convertFormValues( $inputParams );
list( $contacts, $options ) = CRM_Contact_BAO_Query::apiQuery( $newParams,
                                                               $returnProperties,
                                                               null,
                                                               $sort,
                                                               $offset,
                                                               $rowCount );
$grouplist = array_keys($contacts);
return in_array($contactid, $grouplist);
}

Thank you for putting me in the right direction!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Check of Contact in Smart Group

This forum was archived on 2017-11-26.