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

Author Topic: SmartGroup_ContactGet  (Read 650 times)

fen

  • I post frequently
  • ***
  • Posts: 216
  • Karma: 13
    • CivicActions
  • CiviCRM version: 3.3-4.3
  • CMS version: Drupal 6/7
  • MySQL version: 5.1/5.5
  • PHP version: 5.3/5.4
SmartGroup_ContactGet
October 25, 2011, 02:34:41 pm
For Mailchimp integration, I need to be able to find all groups (static and smart).  I've written some initial code that does the trick, and am wondering if something like this could be turned into an API call, say ('SmartGroup','get', $params)?

Code: [Select]
// Return array of Smart Group ids that Contact is a member of
function SmartGroup_ContactGet($cid) {
    SmartGroup_CacheUpdate();
    $groups = array();
    $sql = "
SELECT DISTINCT group_id
FROM   civicrm_group_contact_cache
WHERE  contact_id = $cid";

    require_once 'CRM/Core/DAO.php';
    $groupsDAO = CRM_Core_DAO::executeQuery( $sql );

    while ( $groupsDAO->fetch() ) {
        $groups[] = $groupsDAO->group_id;
    }
    return $groups;
}

// Update Smart Group cache
function SmartGroup_CacheUpdate() {
    $groups = SmartGroup_GetIds();
    require_once 'CRM/Contact/BAO/GroupContactCache.php';

    foreach ($groups as $gid) {
        CRM_Contact_BAO_GroupContactCache::check( $gid );
    }
}

// Return array of all Smart Group IDs
function SmartGroup_GetIds() {
    $smart = array();

    // FIXME: Use offset, limit to get all groups (when more than 25)
    // FIXME: Can Group Get return just smart groups?
    $results = civicrm_api('Group','get',array('version' => 3));

    foreach ($results['values'] as $id => $group) {
        if ($group['saved_search_id']) {
            $smart[] = $id;
        }
    }
    return $smart;
}

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: SmartGroup_ContactGet
October 25, 2011, 06:29:59 pm
I think it would be good to grab Coleman on this - he is up to speed on the api wrt smart group caching. My instinct is that it should be an option on group rather than a separate API.

NB - I have some hopes of renewing the drive to improve smart group querying as a 4.2 MIH (there was an MIH for this called 'get smart groups to show on contact tab' but it sank)
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

fen

  • I post frequently
  • ***
  • Posts: 216
  • Karma: 13
    • CivicActions
  • CiviCRM version: 3.3-4.3
  • CMS version: Drupal 6/7
  • MySQL version: 5.1/5.5
  • PHP version: 5.3/5.4
Re: SmartGroup_ContactGet
October 26, 2011, 07:47:25 am
Sounds great - it would be great to have a better way to do this (and would be interested in reviving the smart group optimization MIH).  It takes about 25 seconds for my SmartGroup_CacheUpdate() function to run (six smart groups across 50K contacts).  What could help (I think) is being smarter about when to invalidate the cache.

I don't have Coleman's email address - is this @colemanw? - can you point him to this discussion for comment?

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

This forum was archived on 2017-11-26.