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 »
  • Pre-installation Questions (Moderator: Dave Greenberg) »
  • CiviCRM and a complex group structure - will it work?
Pages: 1 2 [3] 4

Author Topic: CiviCRM and a complex group structure - will it work?  (Read 8763 times)

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: CiviCRM and a complex group structure - will it work?
July 08, 2010, 03:21:22 pm
Pat - Since I hadn't used this hook before, I did my own experiment (on my 3.2 sandbox). It worked fine for me - I modified the fees for two of the "sample" membership types ("General" and "Student"). I've attached a screenshot of the confirmation page - showing the modified fee of $5.55.

There is a logic error in the 'for' loop in your code  - which I think might be causing some confusion. The membershipTypeValues array is indexed starting at 1, not 0. Compare to my code below.

Code: [Select]
function dgg_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
    for ($i=1; $i <= count($membershipTypeValues); $i++) {
      if ($membershipTypeValues[$i]['name'] == 'General') {
          $membershipTypeValues[$i]['minimum_fee'] = "5.55";
      }
      if ($membershipTypeValues[$i]['name'] == 'Student') {
          $membershipTypeValues[$i]['minimum_fee'] = "2.22";
      }
    }       
}

TIP: when you're working with CiviCRM code, it's often useful to dump one or more variable / array / objects to the screen. Use:

Code: [Select]
CRM_Core_Error::debug('label', $varOrArrayToDump);
Regarding "am I approaching this right" .... I would say yes (for now). Your other option is to get this functionality added to core behaviors by flushing out the specifications for configuring renewal pricing and behaviors and contributing patches to the core to make it happen (or sponsoring / funding the work).
Protect your investment in CiviCRM by  becoming a Member!

PRedmond

  • I’m new here
  • *
  • Posts: 21
  • Karma: 0
Re: CiviCRM and a complex group structure - will it work?
July 08, 2010, 06:27:41 pm
Hi Dave,
Did you forget the attachment? I couldn't find it...

I made the changes to the for loop, but it didn't seem to change what happens: http://humanmilk.info/CiviCRM_Renewal_Prices.jpg

I have attached the exact script I am using now, in the hope that any problem can be identified
Code: [Select]
// reduce the price of renewals by the value set in the admin control
function membershipRenewal_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
   
    for ($i=1; $i < count($membershipTypeValues); $i++) {
      if ($membershipTypeValues[$i]['name'] == '12 month Australian') {
          $membershipTypeValues[$i]['minimum_fee'] = $membershipTypeValues[$i]['minimum_fee'] - variable_get('membershipRenewal_maxdisp', 20);
      }
      if ($membershipTypeValues[$i]['name'] == '24 month Australian') {
          $membershipTypeValues[$i]['minimum_fee'] = $membershipTypeValues[$i]['minimum_fee'] - variable_get('membershipRenewal_maxdisp', 20);
      }
      if ($membershipTypeValues[$i]['name'] == '12 month Concession') {
          $membershipTypeValues[$i]['minimum_fee'] = $membershipTypeValues[$i]['minimum_fee'] - variable_get('membershipRenewal_maxdisp', 20);
      }
    }   
}

Just to clarify, this code resides in the drupal/sites/all/modules/{moduleName}/{moduleName}.php file?


I have considered funding this, but I can't find pricing etc - the pages I found seemed to be a little old. Also, I do enjoy learning it myself, and it will be better for us (and hopefully the entire community) if I can build skills in CiviCRM.


Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: CiviCRM and a complex group structure - will it work?
July 09, 2010, 09:44:16 am
Pat - No attachment, my sample code was inline in previous post.

Your 'for' statement still has an error - which "might" be causing the problem ?? Should be:
Code: [Select]
for ($i=1; $i <= count($membershipTypeValues); $i++) {

(less than or equal to, rather than less than)

If the module is effecting the page (i.e. initial prices are correctly adjusted) - and the above change doesn't fix things - then I'd recommended upgrading to 3.2 beta 3 which is where I did my "experiment". (I say this under assumption that you're not doing this on a production site.)
Protect your investment in CiviCRM by  becoming a Member!

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: CiviCRM and a complex group structure - will it work?
July 09, 2010, 01:01:05 pm
Dave - think he is refering to the screen shot you said you had attached

Quote
I've attached a screenshot of the confirmation page - showing the modified fee of $5.55.
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: CiviCRM and a complex group structure - will it work?
July 09, 2010, 03:57:40 pm
Ahh yes - that attachment :-) Here it is...
Protect your investment in CiviCRM by  becoming a Member!

PRedmond

  • I’m new here
  • *
  • Posts: 21
  • Karma: 0
Re: CiviCRM and a complex group structure - will it work?
July 09, 2010, 10:30:59 pm
Dave, I have found if I select the first subscription type, then it works. If I select any other subscription type then it fails. Out of interest, how does this work for you on 3.2 beta? I am using CiviCRM 3.1.5 - could this be a bug?

I have found when use
Code: [Select]
CRM_Core_Error::debug('membership Type Values', $membershipTypeValues); to dump the variable (which is much nicer than var_dump() which I had been using), this is what I get:

Code: [Select]
membership Type Values

Array
(
    [1] => Array
        (
            [id] => 1
            [minimum_fee] => 45
            [name] => 12 month Australian
            [is_active] => 1
            [description] =>
            [contribution_type_id] => 2
        )

    [2] => Array
        (
            [id] => 2
            [minimum_fee] => 92
            [name] => 24 month Australian
            [is_active] => 1
            [description] =>
            [contribution_type_id] => 2
        )

    [3] => Array
        (
            [id] => 3
            [minimum_fee] => 30
            [name] => 12 month Concession
            [is_active] => 1
            [description] =>
            [contribution_type_id] => 2
        )

)

membership Type Values

Array
(
    [2] => Array
        (
            [id] => 2
            [minimum_fee] => 112.00
            [name] => 24 month Australian
            [is_active] => 1
            [description] =>
            [contribution_type_id] => 2
        )

)

Not really sure why I end up with 2 arrays of the same name on the "/drupal/civicrm/contribute/transact" page...?

If this is working for you on 3.2 Beta, then I will upgrade to that as it will probably be a stable release before we get our site finalised!!  ;)

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: CiviCRM and a complex group structure - will it work?
July 12, 2010, 02:42:30 pm
Arrgh. You're right - selecting the 2nd membership type ('Student' in may case) fails. :-(

I've filed an issue in the bug tracker:
http://issues.civicrm.org/jira/browse/CRM-6479
Protect your investment in CiviCRM by  becoming a Member!

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviCRM and a complex group structure - will it work?
July 19, 2010, 05:43:45 am
Try using below code:

Quote
// reduce the price of renewals by the value set in the admin control
function membershipRenewal_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
   
    foreach ( $membershipTypeValues as &$values) {
      if ($values['name'] == '12 month Australian') {
          $values['minimum_fee'] = $values['minimum_fee']  - variable_get('membershipRenewal_maxdisp', 20);
      }
      if ($values['name'] == '24 month Australian') {
          $values['minimum_fee']  = $values['minimum_fee']  - variable_get('membershipRenewal_maxdisp', 20);
      }
      if ($values['name'] == '12 month Concession') {
          $values['minimum_fee']  = $values['minimum_fee']  - variable_get('membershipRenewal_maxdisp', 20);
      }
    }   
}

HTh

Kurund
Found this reply helpful? Support CiviCRM

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: CiviCRM and a complex group structure - will it work?
July 19, 2010, 04:07:43 pm
I verified that Kurund's approach (using foreach looping) works with my example and I've closed the issue.

Code: [Select]
function mymodule_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
    foreach ( $membershipTypeValues as &$values) {
          if ( $values['name'] == 'General') {
              $values['minimum_fee'] = "5.55";
          }
          if ( $values['name'] == 'Student') {
              $values['minimum_fee'] = "2.22";
          }
    }
}
Protect your investment in CiviCRM by  becoming a Member!

PRedmond

  • I’m new here
  • *
  • Posts: 21
  • Karma: 0
Re: CiviCRM and a complex group structure - will it work?
July 19, 2010, 06:45:58 pm
Thanks so much!! I have to admit that I did try a foreach statement, but couldn't get it working at all, so I dropped back to a basic for statement.

I wonder why this is the case, though..? I would assume that either way would work equally well. Perhaps it has something to do with the variable scope? But this doesn't really explain why it works for the first entry but not others...?

Anyway, thanks for the input. Now I am one step closer to my goal!

PRedmond

  • I’m new here
  • *
  • Posts: 21
  • Karma: 0
Re: CiviCRM and a complex group structure - will it work?
July 19, 2010, 09:36:18 pm
OK, I have learnt a lot, but I would like someone to cast their eyes over this to make sure it is all good:

Code: [Select]
<?php

membershipRenewalContentArray
;

   require_once (
'/home/humanmil/public_html/drupal/sites/all/civicrm.settings.php');
   require_once (
$civicrm_root . '/CRM/Core/Config.php'); 
   
$config =& CRM_Core_Config::singleton( );
   
   require_once 
"api/v2/Contribute.php";
   require_once 
"api/v2/Membership.php";


// reduce the price of renewals by the value set in the admin control
function membershipRenewal_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {

  
// get all memberships
  
$membershipTypes = _civimember_roles_get_data('types');

  
// loop through the membership array and fetch renewal values
    
foreach ( $membershipTypeValues as &$values) {
    
      
// loop through membership types
      
for ($i=1; $i<=count($membershipTypes); $i++) {
      
        
// find membership type in array
        // if a renewal fee exists, use that. Otherwise use the full fee
        
if (array_search($values['name'], $membershipTypes[$i])) {
          
$fieldName = "membershipRenewal_" . str_replace(" ", "_", $membershipTypes[$i][1]);
            
$values['minimum_fee'] = variable_get($fieldName, $membershipTypes[$i][2]);
  
        } 
      }
   
    }
       
}



// admin control panel
function membershipRenewal_admin() {

  
// get all membership types
  
$membershipTypes = _civimember_roles_get_data('types');

  
// loop through membership types, and create a field to add a renewal price
  
for ($i=1; $i<=count($membershipTypes); $i++) {
      
$fieldName = "membershipRenewal_" . str_replace(" ", "_", $membershipTypes[$i][1]);
      
$form[$fieldName] = array(
        
'#type' => 'textfield',
        
'#title' => t('Renewal price of <i>' . $membershipTypes[$i][1] . '</i> (new membership is $' . $membershipTypes[$i][2] . ')'),
        
'#default_value' => variable_get($fieldName, $membershipTypes[$i][2]),
        
'#size' => 2,
        
'#maxlength' => 5,
        
'#description' => t("Price to renew"),
        
'#required' => TRUE,
      );  
    }

  
// return the form
  
return system_settings_form($form);
}


// validate admin input
function membershipRenewal_admin_validate($form, &$form_state) {
  
  
// todo: validate input
 
}


// display link in module menu
function membershipRenewal_menu() {

  
$items = array();

  
$items['admin/settings/membershipRenewal'] = array(
    
'title' => 'Membership Renewal Settings',
    
'description' => 'Setting for when subscribers renew their memberships',
    
'page callback' => 'drupal_get_form',
    
'page arguments' => array('membershipRenewal_admin'),
    
'access arguments' => array('access administration pages'),
    
'type' => MENU_NORMAL_ITEM,
   );

  return 
$items;
}




// define access permissions
function membershipRenewal_perm() {
  return array(
'access membershipRenewal content');
} 



/**
 * This function returns an array of either membership types or status rules depending on the $type param.
 * Function is from civimember_roles module
 */
function _civimember_roles_get_data($type) {
  switch (
$type) {
    case 
'status':
      
$sql = 'SELECT id, name FROM civicrm_membership_status WHERE domain_id = %1;';
      break;
      
    case 
'types':
      
$sql = 'SELECT id, name, minimum_fee FROM civicrm_membership_type WHERE domain_id = %1;';
      break;
      
    default:
      
$sql = '';
      
drupal_set_message(t('Invalid parameter was given to _civimember_roles_get_data(). Param: "%invalid".', array('%invalid' => $type)));
      break;
  }

  
$params = array(1 => array(CIVICRM_DOMAIN_ID, 'Integer'));
  
$dao =& CRM_Core_DAO::executeQuery($sql, $params);
  
  
$result = array();
  while (
$dao->fetch()) {
    
$result[$dao->id] = array($result[$dao->id], $dao->name, $dao->minimum_fee); 
  }
  
  unset(
$dao);
  
  if (empty(
$result)) {
    
drupal_set_message(t('CiviMember Membership Types or Status Rules have not been configured. Please do so on the "Administer CiviCRM" page.'), $type = 'error');
  }
  
  return 
$result;
}

I have pulled bits of code from here and there, and put it together.  What it does is allow you to create a discounted price for all memberships. If a user is a subscriber, it displays the renewal price. If they are not a subscriber, then it shows the price for a new subscription.

I am sure this will not be for everyone, but it fits pretty well with what we need. Now the next step will be to extend the current subscription expiry date, rather than creating a new subscription... It doesn't seem as daunting as it did before...

PRedmond

  • I’m new here
  • *
  • Posts: 21
  • Karma: 0
Re: CiviCRM and a complex group structure - will it work?
July 20, 2010, 04:33:08 am
Well, I was a little optimistic...

I have this:

Code: [Select]
function membershipRenewal_civicrm_postProcess( $formName, &$form ) {
   if ( is_a( $form, 'CRM_Contribute_Form_Contribution_Main' ) ) {
    //CRM_Core_Error::debug('form variables', CRM_Contribute_Form_Contribution_Main::_values);  
    CRM_Core_Error::debug('form variables', $form);  
   }
}

I can use this to identify the values that I want to modify - specifically 'start_date' in the _values array. However I am not sure how to access the object. The commented out line does not work, and I have tried everything I can think of to GET the value, but I can't, so I have no idea how to alter the value...

Any tips?
« Last Edit: July 20, 2010, 04:34:44 am by PRedmond »

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: CiviCRM and a complex group structure - will it work?
July 20, 2010, 09:16:35 am

Code: [Select]
function membershipRenewal_civicrm_postProcess( $formName, &$form ) {
   if ( is_a( $form, 'CRM_Contribute_Form_Contribution_Main' ) ) {
    CRM_Core_Error::debug('form values', $form->_values); 
   }
}

this is because _values is a public variable, in case it was protected, the below code (which is pretty crude but works) would suffice:

Code: [Select]
function membershipRenewal_civicrm_postProcess( $formName, &$form ) {
   if ( is_a( $form, 'CRM_Contribute_Form_Contribution_Main' ) ) {
    CRM_Core_Error::debug('form values', $form->getVar('_values')); 
   }
}

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

PRedmond

  • I’m new here
  • *
  • Posts: 21
  • Karma: 0
Re: CiviCRM and a complex group structure - will it work?
July 20, 2010, 04:04:26 pm

And I would be able to use something like this to set the variable:
Code: [Select]
$form->setVar('_values['foo']', 'bar')

?

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: CiviCRM and a complex group structure - will it work?
July 20, 2010, 07:01:06 pm

not sure if that would work, but this would work:

Code: [Select]
  $values = $form->getVar( '_values' );
  $values['foo'] = 'bar';
  $form->setVar( '_values', $values );

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

Pages: 1 2 [3] 4
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Pre-installation Questions (Moderator: Dave Greenberg) »
  • CiviCRM and a complex group structure - will it work?

This forum was archived on 2017-11-26.