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) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • using membership API to check whether contact is new or current member
Pages: [1]

Author Topic: using membership API to check whether contact is new or current member  (Read 395 times)

holmesg

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.4
  • CMS version: Joomla! 2.5.8
  • MySQL version: 5.1.70
  • PHP version: PHP Version 5.3.22
using membership API to check whether contact is new or current member
December 16, 2014, 08:01:42 am
 I'm trying to use the membership API to check whether a contact is a current lifetime member.

By this, I mean with a status of current OR new.

Code: [Select]
function isLifetimeMember( )
{


try{

$result = civicrm_api3('Membership', 'get', array(
  'sequential' => 1,
  'membership_type_id' => 2,
  'contact_id' => [civicrm-participant:contact-id],
  'status_id' => 2,
));


}
catch (CiviCRM_API3_Exception $e) {
  // handle error here
  $errorMessage = $e->getMessage();
  $errorCode = $e->getErrorCode();
  $errorData = $e->getExtraParams();

error_log('error = ' . $errorMessage);
error_log('error_code = ' . $errorCode);
error_log('error_data = ' . $errorData);
}

    if ( civicrm_error ( $result )) {
        error_log( $result['error_message']);
        return false;
    } else {

        if ($result['count'] == 0) {
            error_log( 'No lifetime membership found for contact ' . [civicrm-participant:contact-id] );
            return false;
        } else {
            error_log( 'Lifetime membership found for contact ' . [civicrm-participant:contact-id] );
            return true;
        }
    }
}

Forgetting any other ugliness in my code for the moment ... how do I check for the contact having either of two statuses here? Would be simple enough in SQL, but I'm not sure how to do it in the API.

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: using membership API to check whether contact is new or current member
December 16, 2014, 08:44:50 am
What about using "getvalue" and returning status_id, then using an "if" statement to evaluate?

Something like:

Code: [Select]
$result = civicrm_api3('Membership', 'getvalue', array(
  'sequential' => 1,
  'membership_type_id' => 2,
  'contact_id' => [civicrm-participant:contact-id],
  'return' => 'status_id'
));

if ($result == 2 || $result == int(whatever)){
//do stuff here
}
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

holmesg

  • I post occasionally
  • **
  • Posts: 61
  • Karma: 0
  • CiviCRM version: CiviCRM 4.3.4
  • CMS version: Joomla! 2.5.8
  • MySQL version: 5.1.70
  • PHP version: PHP Version 5.3.22
Re: using membership API to check whether contact is new or current member
December 17, 2014, 09:16:16 am
Thank you JonGold - that put me on the right track :)

(The result is nested arrays, BTW, if anyone else needs to do this.)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • using membership API to check whether contact is new or current member

This forum was archived on 2017-11-26.