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) »
  • Showing members their membership info
Pages: [1]

Author Topic: Showing members their membership info  (Read 2881 times)

felixmartel

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Drupal 6.31
  • MySQL version: 5.1.54
  • PHP version: 5.3.5
Showing members their membership info
March 25, 2012, 01:11:45 pm
I'm running CiviCRM 3.4 On Drupal 6. I would like to add a block (or link) to the user's Drupal account page that would display their membership type as well as expiry date and a "Renew now" link to the appropriate contrib page.

Is their an "easy" way to do this with little or no programming knowledge ?


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: Showing members their membership info
March 25, 2012, 02:57:44 pm

easy is a relative term :)

You'll need some PHP knowledge to call the API and do:

1. Get the contact ID from the user ID
2. Use the contact ID to get membership information
3. Use the above info to display the expiry date and the Renew Now link if applicable

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

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Re: Showing members their membership info
March 25, 2012, 06:00:24 pm
You could also use views with civicrm views integration to accomplish that.

felixmartel

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Drupal 6.31
  • MySQL version: 5.1.54
  • PHP version: 5.3.5
Re: Showing members their membership info
March 25, 2012, 08:43:34 pm
Quote from: Donald Lobo on March 25, 2012, 02:57:44 pm

easy is a relative term :)

You'll need some PHP knowledge to call the API and do:

1. Get the contact ID from the user ID
2. Use the contact ID to get membership information
3. Use the above info to display the expiry date and the Renew Now link if applicable

lobo


Is there a library of CiviCRM code snippet examples that can be searched anywhere ? I've managed to get a couple things done in Drupal in this way in the past, but I'm really not very good at writing code from scratch... :'(

Quote from: jaymcgraw on March 25, 2012, 06:00:24 pm
You could also use views with civicrm views integration to accomplish that.

This route sounds interesting, although I've had "issues" with CiviCRM views integration in the past... ::) I'll look into it.

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: Showing members their membership info
March 26, 2012, 02:02:37 pm
It's probably really specific to my install, but here is the block I use. If nothing else, you'll need to change the RenewUrl to point to the correct membership contribution page id.

This is based on someone else's code that was posted to the forums a year or so ago, but I didn't keep a reference to it.

Code: [Select]
<?php
civicrm_initialize
(TRUE);
require_once 
'api/v2/UFGroup.php';
require_once 
'api/v2/Contact.php';
require_once 
'api/v2/Membership.php';
require_once 
'CRM/Contact/BAO/Relationship.php';
echo 
"<style type='text/css'>";
echo 
".lhn { line-height:normal; font-size: 90%;}";
echo 
"</style>";

global 
$user;
//echo '$user->uid:'.$user->uid.'<br>';
if ($user->uid) { // Don't do anything if no uid
  
$scontactID = civicrm_uf_match_id_get ($user->uid);
  
$contactID = intval($scontactID);
  
  if( 
$contactID==0) { // if 1
    
echo '<p class="lhn">Hmmm you do not have a contact id: '.$scontactID.'<br />Please contact us for more information. uid:'.$user->uid."</p>";
  } else {
    
// Get contact name and e-mail
    
$params = array ('contact_id' => $contactID);
    
$contact = civicrm_contact_get($params);
    
$contactName = $contact[$contactID]['display_name'];
    if (!
$contactName) { // If no display name, use first/last name
      
$contactName = $contact[$contactID]['first_name'] . " ". $contact[$contactID]['last_name'];
    }
    if (!
$contactName) { // If no name, use e-mail addr
      
$contactName = $contact[$contactID]['email'];
    }
    
    
// Get membership types
    
$params = array ('domain_id' => 1);
    
$result = civicrm_membership_types_get($params);
    
$memTypeName = array();
    foreach (
$result as $key => $type) {
      
$memTypeName[$type['id']] = $type['name'];
    }
    
    
// Get membership statuses
    
$params = array ();
    
$result = civicrm_membership_statuses_get($params);
    
$memStatusName = array();
    foreach (
$result as $key => $type) {
      
$memStatusName[$type['id']] = $type['name'];
    }

    
// Get membership info
    
$result = civicrm_contact_memberships_get($contactID);
    
    if ( 
civicrm_error ( $result )) { // if 2
      
echo'<p class="lhn">FAILED: '. $result['error_message'] . "</p>";
    } else {
      
$RenewUrl = '/civicrm/user?&amp;reset=1';
      
      
// Look through all returned memberships
      
$MemsCount = $result['record_count'];
      
$Memberships = array();
      if( 
$MemsCount>0) {
$cid = $result[$contactID];
foreach($cid as $key => $mem) {
  //echo "<hr>key=$key<pre>";
  //print_r($mem);
  //echo "</pre>";
  if (! $mem['is_test']) {
    $memStatus = $memStatusName[$mem['status_id']];
    $StatusIsCurrent = ((strcmp($memStatus,'New') == 0) ||
(strcmp($memStatus,'Current') == 0));
    $StatusIsExpired = ((strcmp($memStatus,'Grace') == 0) ||
(strcmp($memStatus,'Expired') == 0));
    $StatusIsPending = ((strcmp($memStatus,'Pending') == 0));
    $ThisEndDate = $mem['membership_end_date'];
    $ThisTypeId = $mem['membership_type_id'];
    $ThisMemId = $mem['membership_id'];
    $BestMembership =& $Memberships[$ThisTypeId];
    if( !isset($BestMembership)) {
      $BestMembership = array();
      $Memberships[$ThisTypeId] =& $BestMembership;
    }
    if( !$BestMembership['Current']) {
      if( !($StatusIsCurrent || $StatusIsExpired)) {
if( $StatusIsPending) {
  $BestMembership['AtLeastOnePending'] = true;
} else
  $BestMembership['SomeOtherStatus'] = $memStatus;
      } else {
if( $StatusIsCurrent) {
  $BestMembership['Current'] = true;
  $BestMembership['EndDate'] = $ThisEndDate;
  $BestMembership['MemId'] = $ThisMemId;
} else {
  if( !isset($BestMembership['EndDate'])) {
    $BestMembership['EndDate'] = $ThisEndDate;
    $BestMembership['MemId'] = $ThisMemId;
  } else if( $ThisEndDate > $BestMembership['EndDate']) {
    $BestMembership['EndDate'] = $ThisEndDate;
    $BestMembership['MemId'] = $ThisMemId;
  }
}
      }
    }
  }
} // end for
      
unset($BestMembership);
foreach($Memberships as $MembershipType => $BestMembership) {
  echo "<p class='lhn'>$contactName<br/>\nMember ID: $contactID<br/>";
  
  $MembershipEndDate = $BestMembership['EndDate'];
  if( !isset($MembershipEndDate)) {
    if( isset($BestMembership['AtLeastOnePending']))
      echo 'You have a Pending '.$memTypenme[$MembershipType].' membership - have you sent in your payment?';
    else if( isset($BestMembership['SomeOtherStatus']))
      echo 'You have '.$memTypeName[$MembershipType].'membership status '.$BestMembership['SomeOtherStatus'];
  } else {
    $MEDts = strtotime($MembershipEndDate);
    if( $MEDts)
      $MembershipEndDate = date("j-M-Y",$MEDts);
    if( $RenewUrl) {
      $MemId = $BestMembership['MemId'];
      if( $MemId)
$RenewUrl = "/civicrm/contribute/transact&amp;id=5&amp;mid=$MemId&amp;reset=1";
    }
    if( isset($BestMembership['Current'])) {
      echo $memTypeName[$MembershipType]." member until $MembershipEndDate<br/>";
      if( $RenewUrl)
echo "<a href='$RenewUrl' title='Renew now'>Click here to renew</a>";
    } else {
      echo 'Your '.$memTypeName[$MembershipType].' membership expired on '.$MembershipEndDate.'<br/>';
      if( $RenewUrl) {
echo "<a href='$RenewUrl' title='Renew now'>Click here to renew</a>";
      }
    }
  }
  echo "</p>";
} // foreach $Memberships
      
} else {
echo '<p class="lhn">You are not a member. If you think you should be, please contact us.</p>';
      } 
// if count
    
} // endif 2
  
}  // endif 1
 
}
?>

Dan Rich <drich@lopsa.org>
    Director, LOPSA - http://lopsa.org/

felixmartel

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Drupal 6.31
  • MySQL version: 5.1.54
  • PHP version: 5.3.5
Re: Showing members their membership info
March 26, 2012, 02:09:43 pm
Great ! Thanks !

For now I've managed to give some basic info in the user's profile using Views, but I'll play around with this code and see if I can use it to provide something better for my users.

Much appreciated... :)

flug

  • I post frequently
  • ***
  • Posts: 126
  • Karma: 12
Re: Showing members their membership info
November 01, 2012, 09:54:02 pm
Here is the version (modded form of the above) that we're using.

It would be good to update this to use api ver 3 rather than 2 but for now it works.

I'm sure it will need updating/customizing in various ways.

Also I used "MyOrg" to stand for your organization's name--you'll either need to change this or re-word things.

I copy & paste this code into a php block in the right sidebar that shows for authenticated users (under drupal you can set blocks to use php code, so it is just a block set to 'php code' and then this code pasted inside).

Code: [Select]
<?php
global $user;
echo 
"<p><strong>" . $user->name . "</strong>" . " | " .  t("<a href='/logout'>Log Out</a><br>");
echo 
t("<a href='/user'>Your account and e-news subscriptions</a><p>");

civicrm_initialize(TRUE);
require_once 
'api/v2/UFGroup.php';
require_once 
'api/v2/Contact.php';
require_once 
'api/v2/Membership.php';
require_once 
'CRM/Contact/BAO/Relationship.php';
$userDashboardURL="/civicrm/user?reset=1";
https://MyOrg.org/civicrm/user?reset=1
echo '<div style="padding-right: 5px;padding-left: 5px;border-color: #294D61;border-style:outset;border-width: 1px;">';
echo 
'<h2 class="title">MyOrg Membership Status</h2>';


echo 
"<style type='text/css'>";
echo 
".lhn { line-height:normal; font-size: 90%;}";
echo 
"</style>";

global 
$user;
//echo '$user->uid:'.$user->uid.'<br>';
if ($user->uid) { // Don't do anything if no uid
  
$scontactID = civicrm_uf_match_id_get ($user->uid);
  
$contactID = intval($scontactID);
  
  if( 
$contactID==0) { // if 1
    
echo '<p class="lhn">This account does not have a membership contact id.<br />Either you are not a MyOrg member, or this account is not properly connected with your MyOrg membership account. Please <a href="/contact">contact us</a> if you feel this is a mistake. Include this info: UID='.$user->uid."</p>";
  } else {
    
// Get contact name and e-mail
    
$params = array ('contact_id' => $contactID);
    
$contact = civicrm_contact_get($params);
    
$contactName = $contact[$contactID]['display_name'];
    if (!
$contactName) { // If no display name, use first/last name
      
$contactName = $contact[$contactID]['first_name'] . " ". $contact[$contactID]['last_name'];
    }
    if (!
$contactName) { // If no name, use e-mail addr
      
$contactName = $contact[$contactID]['email'];
    }
    
    
// Get membership types
    
$params = array ('domain_id' => 1);
    
$result = civicrm_membership_types_get($params);
    
$memTypeName = array();
    foreach (
$result as $key => $type) {
      
$memTypeName[$type['id']] = $type['name'];
    }
    
    
// Get membership statuses
    
$params = array ();
    
$result = civicrm_membership_statuses_get($params);
    
$memStatusName = array();
    foreach (
$result as $key => $type) {
      
$memStatusName[$type['id']] = $type['name'];
    }

    
// Get membership info
    
$result = civicrm_contact_memberships_get($contactID);
    
    if ( 
civicrm_error ( $result )) { // if 2
      
echo'<p class="lhn">ERROR: '. $result['error_message'] . "</p>";
    } else {
      
//$RenewUrl = '/civicrm/user?&amp;reset=1';
      
$RenewUrl = '/join';
      
      
// Look through all returned memberships
      
$MemsCount = $result['record_count'];
      
$Memberships = array();
      
$MemberSince="";
      if( 
$MemsCount>0) {
$cid = $result[$contactID];
foreach($cid as $key => $mem) {
  //echo "<hr>key=$key<pre>";
  //print_r($mem);
  //echo "</pre>";
    
    //bhugh - we're going to count test memberships, too
  //if (! $mem['is_test']) {
            
    $memStatus = $memStatusName[$mem['status_id']];
    $StatusIsCurrent = ((strcmp($memStatus,'New') == 0) ||
(strcmp($memStatus,'Current') == 0));
    $StatusIsExpired = ((strcmp($memStatus,'Grace') == 0) ||
(strcmp($memStatus,'Expired') == 0));
    $StatusIsPending = ((strcmp($memStatus,'Pending') == 0));
    $ThisEndDate = $mem['membership_end_date'];
            
$ThisMemberSince = $mem['join_date'];
            
//echo $ThisMemberSince;
    $ThisTypeId = $mem['membership_type_id'];
    $ThisMemId = $mem['membership_id'];
 
            if (
$MemberSince=="") $MemberSince=$ThisMemberSince;
            elseif (
$ThisMemberSince<$MemberSince)$MemberSince=$ThisMemberSince;
    
            
$BestMembership =& $Memberships[$ThisTypeId];
    if( !isset($BestMembership)) {
      $BestMembership = array();
      $Memberships[$ThisTypeId] =& $BestMembership;
    }
    if( !$BestMembership['Current']) {
      if( !($StatusIsCurrent || $StatusIsExpired)) {
if( $StatusIsPending) {
  $BestMembership['AtLeastOnePending'] = true;
} else
  $BestMembership['SomeOtherStatus'] = $memStatus;
      } else {
if( $StatusIsCurrent) {
  $BestMembership['Current'] = true;
  $BestMembership['EndDate'] = $ThisEndDate;
  $BestMembership['MemId'] = $ThisMemId;
} else {
  if( !isset($BestMembership['EndDate'])) {
    $BestMembership['EndDate'] = $ThisEndDate;
    $BestMembership['MemId'] = $ThisMemId;
  } else if( $ThisEndDate > $BestMembership['EndDate']) {
    $BestMembership['EndDate'] = $ThisEndDate;
    $BestMembership['MemId'] = $ThisMemId;
  }
}
      }
    }
  //} //end if test
} // end for
        //we go through all the memberships. The one with the latest expiration date is the controlling one.
unset($BestMembership);
  
$lastMembershipEndDate="";
  
$start=1;     //make sure at least one membership

foreach($Memberships as $MembershipType => $BestMembership) {
          
$MembershipEndDate = $BestMembership['EndDate'];
          
//print_r($BestMembership);
          
if ($start==1 OR 
              (isset (
$MembershipEndDate) && isset ($lastMembershipEndDate) && $MembershipEndDate > $lastMembershipEndDate) OR 
              (isset(
$MembershipEndDate) && ! isset ($lastMembershipEndDate)  )  
          ) {
                
$start=0;
                
$lastMembershipEndDate=$MembershipEndDate;  
                
$lastBestMembership=$BestMembership;
                
$lastMembershipType= $MembershipType;

          }
  } 
//foreach
        
  
$MembershipEndDate = $lastMembershipEndDate;  
  
$BestMembership = $lastBestMembership;
  
$MembershipType = $lastMembershipType;
  
//print_r($BestMembership);

  echo "<p class='lhn'>$contactName<br/>\nMember ID: $contactID<br/>";
  
  $MembershipEndDate = $BestMembership['EndDate'];
  if( !isset($MembershipEndDate)) {
    if( isset($BestMembership['AtLeastOnePending']))
      echo 'You have a Pending '.$memTypenme[$MembershipType].' membership - payment in process.';
    else if( isset($BestMembership['SomeOtherStatus']))
      echo 'You have '.$memTypeName[$MembershipType].'membership status '.$BestMembership['SomeOtherStatus'];
  } else {
    $MEDts = strtotime($MembershipEndDate);
    if( $MEDts)
      $MembershipEndDate = date("j-M-Y",$MEDts);
            
$MEDts = strtotime($MemberSince);
    if( $MEDts)
      $MemberSince = date("j-M-Y",$MEDts);
    if( $RenewUrl) {
      $MemId = $BestMembership['MemId'];
      if( $MemId)
$RenewUrl = "/membership";
    }
    if( isset($BestMembership['Current'])) {
        echo 
"<br>";
        if (
strlen ($MemberSince)>0) echo "Member since $MemberSince<br>";
      echo $memTypeName[$MembershipType]." membership valid thru $MembershipEndDate<br><br> ";
              
      if( $RenewUrl)
echo "<a href='$RenewUrl' title='Renew now'>Click here to renew</a> - or <a href='http://MyOrg.org/donate'>make a donation.</a>";
    } else {
      echo '<br><b>Your '.$memTypeName[$MembershipType].' membership expired on '.$MembershipEndDate.'<b><br><br>';
      if( $RenewUrl) {
echo "<b><a href='$RenewUrl' title='Renew now'>Click here to renew now!</a></b>";
      }
    }
  }
  echo "</p>";
//} // foreach $Memberships
      
} else {
echo '<p class="lhn">No membership is associated with this account. Is this an error? Please <a href="/contact">contact us</a>.</p> <p class="lhn"><a href="'.$RenewUrl.'">Click here to join.</a></p>';
      } 
// if count
    
} // endif 2
  
}  // endif 1
 
}
//echo "<br><br>";
echo "<p class=\"lhn\"><a href=\"$userDashboardURL\">See your contribution summary page.</a></p>";

echo 
"<p class=\"lhn\"><a href=\"/user\">View/update your account & contact information.</a></p>";

echo 
"</div>";
?>
« Last Edit: November 17, 2012, 09:42:51 am by flug »

needorf

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 4.2.x
  • CMS version: Drupal 7
  • MySQL version: 5.5.25
  • PHP version: 5.4.4
Re: Showing members their membership info
March 15, 2013, 08:23:30 am
Thans for the code, it works for me on Drupal 7, but I received an error: Notice: Undefined index: Current in eval() (line 100 of /xxx/xxx/xxx/modules/php/php.module(80) : eval()'d code).

Changing line 100:

if( !$BestMembership['Current']) {

to:

 if( !isset($BestMembership['Current'])) {

fixed the problem
« Last Edit: March 15, 2013, 08:29:19 am by needorf »

Shefftini

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 3.2
  • CMS version: Drupal 6.x
  • MySQL version: 5.1.5
  • PHP version: 5.2.14
Re: Showing members their membership info
July 22, 2013, 05:34:26 pm
We've got a version of the code given by flug and sjthespian  running fairly well.  The only issue we have is with Life Members not showing up and the logs complaining of an undefined index: membership_end_date in eval(0) (line 71) of the php module we created. 
Anyone have a more recent version of the code we might try?  What we have right now is this:

if( $StatusIsCurrent) {
        $BestMembership['Current'] = true;
        $BestMembership['EndDate'] = $ThisEndDate;
        $BestMembership['MemId'] = $ThisMemId;
      } else {
        if( !isset($BestMembership['EndDate'])) {
          $BestMembership['EndDate'] = $ThisEndDate;
          $BestMembership['MemId'] = $ThisMemId;
        } else if( $ThisEndDate > $BestMembership['EndDate']) {
          $BestMembership['EndDate'] = $ThisEndDate;
          $BestMembership['MemId'] = $ThisMemId;
        }

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMember (Moderator: Deepak Srivastava) »
  • Showing members their membership info

This forum was archived on 2017-11-26.