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) »
  • Smart Groups API
Pages: [1]

Author Topic: Smart Groups API  (Read 3345 times)

spl13

  • Guest
Smart Groups API
October 17, 2008, 03:42:28 am
I'm creating a Drupal module with CiviCRM 2.0 which requires a list of all the user's groups (normal and smart).
I've managed to use the API v2, using civicrm_group_contact_get, to access the user's regular groups.
How can I retrieve the user's smart groups?

There are thousands of contacts, and only a handful of smart groups, so running an optimized query on each smart group to retrieve only the single user's record would be plausible, but I'm not sure how to construct that.

Thanks for any suggestions.

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: Smart Groups API
October 17, 2008, 07:37:19 am

There is no api for this as yet. You will need to interact with the db directly. the approx pseudo code is:

Foreach group do:
   If group is a smart group do:
      check if contact belongs to group (you can use civicrm_contact_search with the group id here)

if you need more help/details, get on IRC (http://embed.mibbit.com/?server=irc.freenode.net&channel=%23civicrm&forcePrompt=true)

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

spl13

  • Guest
Re: Smart Groups API
October 19, 2008, 02:07:51 pm
Thanks!
Actually, for my purpose, I had a short list of specific groups, and needed to check if a user was a member of any of those groups, so a single query to civicrm_contact_search did the trick.
Here's what I did:

Code: [Select]
      $groups2=array();
      foreach($groups as $grp){
        $groups2[$grp]=1;
      }
      global $user;
      $params = array(
                      'email' => $user->mail,
                      'group' => $groups2,
                      'return.email' => 1);
      $contacts =& civicrm_contact_search( $params );
      if (!empty($contacts)){

Thanks again for the lead.
P.S. Is there a reference for what you can put into the params array? I couldn't find any documentation.

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: Smart Groups API
October 19, 2008, 04:21:23 pm
Quote from: spl13 on October 19, 2008, 02:07:51 pm
P.S. Is there a reference for what you can put into the params array? I couldn't find any documentation.

Unfortunately not, the parameters are the same as whats POSTed from the search forms. This would make an excellent community contribution. I'm more than happy to explain stuff to you so you can document. Jump onto IRC if this is of interest (http://embed.mibbit.com/?server=irc.freenode.net&channel=%23civicrm&forcePrompt=true)

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

outinsun

  • Guest
Re: Smart Groups API
February 22, 2010, 08:29:51 am
Quote from: Donald Lobo on October 17, 2008, 07:37:19 am

There is no api for this as yet.
Is this still the case? In order to integrate with CiviMail, I will need to create smart groups on the fly. I assume I'll have to write code to create/get smart groups myself?

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: Smart Groups API
February 22, 2010, 08:31:54 am

yes

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Smart Groups API
February 22, 2010, 12:28:02 pm
If you could wrap your group create specific code into a function an put it in the api, would be very useful for other users I'm sure.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

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

This forum was archived on 2017-11-26.