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) »
  • Automatically add ALL new contacts to a smart group
Pages: [1]

Author Topic: Automatically add ALL new contacts to a smart group  (Read 1921 times)

CommonGood

  • Guest
Automatically add ALL new contacts to a smart group
March 14, 2009, 08:20:59 pm
I'd like all new contacts, both that I import from an excel sheet/csv and add individually from the dashboard to a specific smart group.  It is possible to set a default for this function?

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: Automatically add ALL new contacts to a smart group
March 15, 2009, 07:17:52 am

no.but you should be able to do this by implementing the civicrm_post hook on contact create. This will require some code expertise.

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

bentonconsult

  • Guest
Re: Automatically add ALL new contacts to a smart group
March 15, 2009, 03:28:04 pm
try this - it's kind of a hack but doesn't require technical ability and I think it works:

1) Just click the "Go" button on the Contact Search (this should return all records in your database)
2) Select All the records and create a "New Smart Group"

so the criteria for the smart group is basically that the person is in the database. I just tried this, added a new contact, and then the newly added contact showed up in the smart group I had just created.

You should verify.

noah

  • I’m new here
  • *
  • Posts: 18
  • Karma: 4
Re: Automatically add ALL new contacts to a smart group
March 01, 2011, 02:49:33 pm
Quote from: Donald Lobo on March 15, 2009, 07:17:52 am
you should be able to do this by implementing the civicrm_post hook on contact create. This will require some code expertise.

I just implemented this on my CiviCRM 3.3.2/Drupal 6 installation. With CiviCRM's current ACL system, the only way to deny access to contacts in Group A is to create an inverse Group B, and allow access to nothing but this Group B. In my case, we now automatically add every new contact to the "Default" group unless they're specifically added to the "Confidential" group. Certain users can only view/edit the "Default" group (which excludes the "Confidential" group).

I created a module (mymodule.module). http://drupal.org/node/231276

See also http://wiki.civicrm.org/confluence/display/CRMDOC33/CiviCRM+hook+specification
and http://wiki.civicrm.org/confluence/display/CRMDOC33/Group+APIs#GroupAPIs-civicrmgroupcontactget%28%26%24params%29

Code: [Select]
/**
 * On Contact creation, add all contacts to "Default" group unless they're in "Confidential"
 */
function mymodule_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  if ($op == 'create' && in_array($objectName, array('Individual','Household','Organization'))) {
    require_once 'api/v2/Group.php';
    require_once 'api/v2/GroupContact.php';
    $params = array('contact_id' => $objectId);
    $groups = civicrm_group_contact_get($params);
    // Group 4 = Confidential
    if (!array_key_exists(4, $groups)) {
      // Group 5 = Default
      $params = array('contact_id.1' => $objectId, 'group_id' => 5);
      civicrm_group_contact_add($params);
    }
  }
}

noah

  • I’m new here
  • *
  • Posts: 18
  • Karma: 4
Re: Automatically add ALL new contacts to a smart group
April 17, 2011, 02:43:05 pm
I'm not sure when the change happened, but civicrm_group_contact_get now returns its results in a different format. Here's the modified code to work with the new format:

Code: [Select]
/**
 * On Contact creation, add all contacts to "Default" group unless they're in "Confidential"
 */
function civicrm_custom_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  if ($op == 'create' && in_array($objectName, array('Individual','Household','Organization'))) {
    require_once 'api/v2/Group.php';
    require_once 'api/v2/GroupContact.php';
    $params = array('contact_id' => $objectId);
    $group_links = civicrm_group_contact_get($params);
    // Group 4 = Confidential
    $confidential = false;
    foreach ($group_links as $group_link) {
      if ($group_link['group_id'] == 4) {
        $confidential = true;
        break;
      }
    }
    if (!$confidential) {
      // Group 5 = Default
      $params = array('contact_id.1' => $objectId, 'group_id' => 5);
      civicrm_group_contact_add($params);
    }
  }
}

noah

  • I’m new here
  • *
  • Posts: 18
  • Karma: 4
Re: Automatically add ALL new contacts to a smart group
May 31, 2011, 03:35:57 pm
Just updated this solution to API V3.

Code: [Select]
/**
 * On Contact creation, add all contacts to "Default" group unless they're "Confidential"
 */
function civicrm_custom_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  if ($op == 'create' && in_array($objectName, array('Individual','Household','Organization'))) {
    require_once 'api/api.php';
    $group_links = civicrm_api('GroupContact','get', array ('version' =>'3', 'contact_id' => $objectId));
    if ($group_links['is_error']) {
      user_error("Couldn't do automatic group-adding for $objectName");
      return;
    }
    // Group 4 = Confidential
    $confidential = false;
    foreach ($group_links['values'] as $group_link) {
      if ($group_link['group_id'] == 4) {
        $Confidential = true;
        break;
      }
    }
    if (!$confidential) {
      // Group 5 = Default
      $results = civicrm_api("GroupContact","create", array ('version' =>'3', 'contact_id' => $objectId, 'group_id' => 5));
      if ($results['is_error']) {
        user_error("Couldn't do automatic group-adding for $objectName");
      }
    }
  }
}

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Automatically add ALL new contacts to a smart group

This forum was archived on 2017-11-26.