How to build custom integration with aMember

Discussion in 'Integration' started by microvb, Apr 11, 2012.

  1. microvb

    microvb Member

    Joined:
    Nov 23, 2007
    Messages:
    62
    This guide should assist some of you who are attempting to integrate a custom PHP script with aMember and to check UAC level.

    The first part, is to modify your aMember plugin (custom) to allow the user to enter which products that plugin applies. This is the simplest method. For example purposes, we will use the plugin named "plugin_template".

    amember/plugins/protect/plugin_template/config.inc.php
    Code:
    add_config_field('protect.wingftp.products', 'Product ID\'s', 'text', "Enter the product ID to create access for. For multiple products, use the ; (semicolon). [example:  19;21;22  ]", $notebook_page);
    
    Your script to protect the page. Please read inline comments for where to place your code.
    Code:
    <?php
    include("amember/config.inc.php"); // Include the aMember CONFIG file
    $plugin    =   $config['protect']['plugin_template'];
    $dbconf     =   $config['db']['mysql'];
    $sql        =   '';
    $expires    =   null;
    $products   =   Array();
    $member_id  =   0;
    
    // Check if logged in and get aMember user id
    if(isset($_SESSION['_admin_login'])) {
        // If user logged in as admin, set aMember user id to 0 --- for use later
        $member_id = -1;
    } elseif ( isset($_SESSION['_amember_user']['member_id']) ) {
        // Get member_id from session
        $member_id = $_SESSION['_amember_user']['member_id'];
    }
    
    switch($member_id) {
      case -1:
        // User is Admin
        echo "Logged in as <b>" . $_SESSION['_admin_login'] . "</b> which has administrator access.";
        break;
    
      case 0;
        // Not logged in
        echo sprintf('<a href="%s">Click here to Login</a>', $config['root_surl'] . "/member.php");
        break;
    
      default:
        // Logged in as a member
    
        // Get products allowed from aMember Plugin Settings
        if(strpos($plugin['products'],";") > 0) {
          $products   = explode(";", $plugin['products']);
        } else {
          $products[] = $plugin['products'];
        }
    
        // Build query to check aMember payments for non-expired subscription
        foreach($products as $product) {
          if($sql <> '') { $sql .= ' OR '; }
          $sql .= '`product_id`=' . $product;
        }
    
        // Get expiry date of most recent supported product for member
        $sql = "SELECT `expire_date` FROM `" . $dbconf['prefix'] . "payments` WHERE (`member_id`=" . $member_id . ") AND (" . $sql . ") AND ( `completed` > 0 ) ORDER BY `expire_date` DESC LIMIT 1;";
        $results = $db->query($sql);
        if($results) {
          if($row = mysql_fetch_assoc($results)) {
            $expires = $row['expire_date'];
          }
        }
    
        // Compare expiry date with current date
        if($expires >= date('Y-m-d')) {
          // Display member content here
          echo "Your membership expires" . DateExpires($expires) . ".";
        } else {
          // Access to paid area has expired or membership never existed
          if($expired . "" <> "") {
            // Membership expired
            echo "Your membership expired" . DateExpired($expires) . ".";
          } else {
            // Membership did not exist
            echo "You have never had a membership.";
          }
        }
        break;
    }
    
    function DateExpired($date) {
      $date1 = new DateTime($date);
      $date2 = new DateTime(date('Y-m-d'));
      $interval = $date2->diff($date1);
      $output = '';
      if($interval->d > 0 || $interval->m > 0 || $interval->y > 0 ) {
        $output = $interval->d." days";
        if($interval->m > 0 || $interval->y > 0) { $output = $interval->m." months, " . $output; }
        if($interval->y > 0) { $output = $interval->m." years, " . $output; }
        $output = ' ' . $output . ' ago.';
      } else {
        $output = ' Today';
      }
      return $output;
    }
    
    function DateExpires($date) {
      $date1 = new DateTime($date);
      $date2 = new DateTime(date('Y-m-d'));
      $interval = $date1->diff($date2);
      $output = '';
      if($interval->d > 0 || $interval->m > 0 || $interval->y > 0 ) {
        $output = $interval->d." days";
        if($interval->m > 0 || $interval->y > 0) { $output = $interval->m." months, " . $output; }
        if($interval->y > 0) { $output = $interval->m." years, " . $output; }
        $output = ' in ' . $output;
      } else {
        $output = ' Today';
      }
      return $output;
    }
    ?>
    
    And thats all there is to it. Administrators of aMember should now be able to add their products by going to aMember CP > Plugins > plugin_template by entering the product_id which shows up on the left most column of every product created. For multiple products, they would use a semicolon ( ; ) between the product id's (these instructions are in the comment field for the new field).

    Now you can focus on coding the application without worrying about the aMember integration side of things by using this code. For more advanced scenarios we are still available for custom solutions.

    Enjoy :)

Share This Page