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 CiviMember (Moderator: Deepak Srivastava) »
  • Hook for setting membership type?
Pages: [1]

Author Topic: Hook for setting membership type?  (Read 689 times)

sjthespian

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 3
    • The League of Professional System Administrators
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.28
  • MySQL version: 5.1.66
  • PHP version: 5.3.3
Hook for setting membership type?
October 11, 2010, 02:53:44 pm
We have just rolled out CiviCRM for managing our membership data, but I ran into an odd problem that I think can be solved with a hook if I can figure out which one.

We have several legacy membership types, Founding, Charter, etc.  You can no longer join as one of these types but if you are already that type than it should remain if you renew while your membership is still active or in a grace period.  I just tried renewing as I am a Charter member, and it creates a new Individual membership (our default type) instead of extending the my Charter membership.

This leads me to two questions:
  • Is there a hook that can be used to catch the membership renewal and set the type properly?  I think I can write the code to do it if I can find the write hook.
  • Is it even possible to do something like this?

I suppose the other option would be to do this on the front-end, i.e. use hook_civicrm_membershipTypeValues to set the membership type.  Would that work if they types are set with a visibility of admin?  Is this a "better" way to try and do this?
Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

sjthespian

  • I post occasionally
  • **
  • Posts: 63
  • Karma: 3
    • The League of Professional System Administrators
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 6.28
  • MySQL version: 5.1.66
  • PHP version: 5.3.3
Re: Hook for setting membership type?
October 11, 2010, 07:19:33 pm
I'm replying to all my own posts tonight... :)

I just got this woring using civicrm_membershipTypeValues, but I have one question that I can't find in any of the hook documentation.  Is there a way to get the contact_id or drupal_id of the currently logged in user?  I turned on debugging and brought up the smartyDebug window but I don't see anything along these lines in it.

I ended up using the $user global, but I'm not sure that's the best way to go.

Here is what I ended up with just in case it helps anyone else.  Feel free to comment on this if there are better ways for me to do it:
Code: [Select]
function CiviLOPSA_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
    global $user;
    if (!$user->uid) return '';

    /* Query the memberID custom field for the max and add 1 for new ID */
    /* Look for membership status of current or grace                   */
    /* (assume New members not renewing)                                */
    $query = "
SELECT name
FROM civicrm_membership_type
JOIN ( civicrm_membership, civicrm_uf_match )
  ON civicrm_membership_type.id = civicrm_membership.membership_type_id
    AND civicrm_membership.contact_id=civicrm_uf_match.contact_id
WHERE civicrm_uf_match.uf_id=%1
  AND ( civicrm_membership.status_id=2
     OR civicrm_membership.status_id=3 )
";
    if ($user->uid) {
        $params = array( 1  => array( $user->uid, 'Integer' ));
        $dao = CRM_Core_DAO::executeQuery( $query, $params );

        /* If one we found it set the first type to that */
        if ($dao->fetch()) {
          $membershipTypeValues[1]['name']        = $dao->name;
        }
    }
}
Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMember (Moderator: Deepak Srivastava) »
  • Hook for setting membership type?

This forum was archived on 2017-11-26.