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 Profiles (Moderator: Dave Greenberg) »
  • Unselected group checkbox removes group contact
Pages: [1]

Author Topic: Unselected group checkbox removes group contact  (Read 1159 times)

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Unselected group checkbox removes group contact
September 16, 2014, 04:45:12 am
Based on a previous post http://forum.civicrm.org/index.php/topic,34177.msg144944.html#msg144944 (CiviMail - Subscription removed by Web) I'm now struggeling with the following problem:

1. Contact registeres to an event using a profile.
2. Contact is detected as duplicat.
3. Contact details were overwritten, related group contacts were added or removed

I did not expect, that group contacts were removed, if the related profile checkbox is not selected. Is this a bug in the profile/duplicate process or is it correct behaviour?

And how can I prevend group contacts from being removed via profile?
« Last Edit: September 16, 2014, 04:54:20 am by PhilippMikael »

Dan C

  • I post frequently
  • ***
  • Posts: 179
  • Karma: 17
  • CiviCRM version: All
  • CMS version: Joomla 2.5 and 3.X, Drupal 7, Wordpress 3.7 and up
  • MySQL version: 5.1.7 and up
  • PHP version: 5.3.5 and up
Re: Unselected group checkbox removes group contact
September 16, 2014, 08:12:13 am
Someone can correct me if I'm mistaken, but I believe this is correct.  If the user was signed into your site then the check box would automatically be checked.  However, Civi is going to assume they meant to leave it unchecked and all the information now being entered is the most current, so during the dedupe it will overwrite the current information with the new including removing them from the group.
CrusonWeb - Web Development, Implementation, and Support.  Specializing in CiviCRM and Joomla (although also knowledgable in Wordpress and Drupal as well).

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: Unselected group checkbox removes group contact
September 16, 2014, 11:43:21 pm
OK, but any idea how to disable this standard feature? I guess I have to use a hook (probably hook_civicrm_postProcess), right?

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: Unselected group checkbox removes group contact
September 19, 2014, 09:53:49 am
I've tried to delete the submitted not checked checkbox but with no success:

Code: [Select]
function mymodule_civicrm_postProcess($formName, &$form){
      if(strpos($formName, 'CRM_Event_Form_Registration_Register') === 0){
if ($form->elementExists('group')) {
unset($form->_submitValues['group']);
               }
      }
}

(I know that this would delete group values even if the related checkboxes are checked but I first wanted to try if it is possible to remove an element before I go further)

How can I remove a checkbox of the checkbox group? Is there any function I can use (already tried $form->removeElement('group')). I even tried to unset some properties of $form (unset($form->_submitValues['group'])) but with no effect.

Maybe I should use hook_civicrm_pre but the docs say it is not recommended to abort an operation (like the deletion of a group contact) in this place.

Any ideas?! I would realy appreciate any help.

Dan C

  • I post frequently
  • ***
  • Posts: 179
  • Karma: 17
  • CiviCRM version: All
  • CMS version: Joomla 2.5 and 3.X, Drupal 7, Wordpress 3.7 and up
  • MySQL version: 5.1.7 and up
  • PHP version: 5.3.5 and up
Re: Unselected group checkbox removes group contact
September 19, 2014, 12:50:09 pm
I was hoping someone else might have a suggestion because I don't have a good one.  The best thought I can come up with is use a hook like the post hook and the API to check the users record and any blank ones that are previously checked should be overridden so the system thinks they are checked.  That's a lot of work though.  Another consideration, what if someone wants to be unsubscribed from the group (maybe they left it blank on purpose this time around)?
CrusonWeb - Web Development, Implementation, and Support.  Specializing in CiviCRM and Joomla (although also knowledgable in Wordpress and Drupal as well).

JonGold

  • Ask me questions
  • ****
  • Posts: 638
  • Karma: 81
    • Palante Technology
  • CiviCRM version: 4.1 to the latest
  • CMS version: Drupal 6-7, Wordpress 4.0+
  • PHP version: PHP 5.3-5.5
Re: Unselected group checkbox removes group contact
September 22, 2014, 12:37:18 pm
I've also encountered this problem.  The "groups" profile field could definitely use some love.

That said - I wrote an extension that fixes this problem.  I can put the code up on github if it'll be helpful to look at.  The downsides are a) you have an extra custom field hanging out, and b) my client only had a single group they needed to worry about.  It would need significant rewriting to support multiple groups.  The upside is that it's entirely API-based, so it should be relatively sturdy across upgrades.

The basic approach:  Create a custom field that matches your list of groups, and put THAT on the profile.  Run hook_civicrm_post to check the value of that field at appropriate times - in my case, after new contributions or event registration:
Code: [Select]
if($op == 'create' && ($objectName == 'Contribution' || $objectName == 'Participant') )
If the field is NOT blank, join the contact to groups based on the value of the field.  If the field IS blank, do nothing (that's the important part!).  Regardless, after joining them to groups, blank the field, so you don't accidentally join them again in the future (important in my case, because the client wanted a welcome message sent to folks "joining" the group even if they were already members).

PS - Revisiting this code, I realize that instead of hook_civicrm_post, I could rewrite this to use hook_civicrm_custom, which would be more robust.
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: Unselected group checkbox removes group contact
October 09, 2014, 08:23:51 am
Thank you for that! I think I have to go with this solution because I can't find a way to unset a submitted empty group checkbox: http://forum.civicrm.org/index.php/topic,34237.0.html

It would be very helpful, if you put your code.  :)

JonGold

  • Ask me questions
  • ****
  • Posts: 638
  • Karma: 81
    • Palante Technology
  • CiviCRM version: 4.1 to the latest
  • CMS version: Drupal 6-7, Wordpress 4.0+
  • PHP version: PHP 5.3-5.5
Re: Unselected group checkbox removes group contact
October 09, 2014, 10:44:59 am
Hi there,

I just made a Github repository with my code:
https://github.com/PalanteJon/coop.palantetech.module.cpehn.joinnetworkcheckbox

Note that the second function (which is more than half the code) is unnecessary for you!  My client wanted to send a welcome message to users who were already in the group if they checked the box.  If this doesn't apply to you, you can remove that.

I hope this is helpful to you!
Jon
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: Unselected group checkbox removes group contact
October 13, 2014, 07:12:54 am
As you suggested I have used the custom field hook to create a group contact, whenever one of the custom field checkboxes has been selected. In detail: I'm using one single custom field for all newsletter groups and created a checkbox option for each group (values are equal to the contact group ID).  I think it's a really clean solution because I only have to add a checkbox to the custom field if our customer has a new nesletter group. Here is my drupal module which seems to work like charme  :)

Code: [Select]
<?php

/*
 * ID of custom field group for contact details
 */
define('HOOK_NEWSLETTER_CFG_CONTACTDETAILS', '14'); 

/**
 * ID of custom field for newsletter contact group options
 */
define('HOOK_NEWSLETTER_CF_NEWSLETTER', '56');

/**
 * Implementation of hook_civicrm_post
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_custom
 */
function hook_newsletter_civicrm_custom( $op, $groupID, $entityID, &$params ){

if(($groupID != HOOK_NEWSLETTER_CFG_CONTACTDETAILS) || ($op == 'delete')){
return;
}

$contact_id = $entityID;

foreach ($params as $param_key => $param) {

if($param['custom_field_id'] == HOOK_NEWSLETTER_CF_NEWSLETTER){

// values of selected group checkboxes are saved in a single field, seperated by control character (SOH, 0x01)
$group_ids = array_filter(explode(chr(1), $param['value']));

// abort if nothing is checked (prevent infinite loop)
if(empty($group_ids)) return;

foreach ($group_ids as $group_id) {
$params = array(
'version' => 3,
'sequential' => 1,
'contact_id' => $contact_id,
'group_id' => $group_id,
);

$result = civicrm_api('GroupContact','create',$params);

if($result['is_error']){
drupal_set_message('Adding contact to newsletter group failed:\n' . $result['error_message'] , 'error');
}
}

// reset custom field for newsletter subscription
// (api function for custum value deletion is not implemented yet)
$group_ids_blanked = array_flip($group_ids);
$group_ids_blanked = array_fill_keys(array_keys($group_ids_blanked), 0);

$params = array(
'version' => 3,   
'sequential' => 1,
'entity_id' => $contact_id,
'custom_' . HOOK_NEWSLETTER_CF_NEWSLETTER => $group_ids_blanked
);

$result = civicrm_api('CustomValue', 'create', $params);

return;
}
}
}

Thanks again for your help!
Philipp
« Last Edit: October 13, 2014, 07:15:34 am by PhilippMikael »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Unselected group checkbox removes group contact

This forum was archived on 2017-11-26.