Using site.inc.php and hooks

From AMember Pro Manual

Jump to: navigation, search

This chapter requies serious PHP programming expirience. We recommend you to contact support@cgi-central.net for suggestion, if you not sure if you have such knowledge.

Contents

[edit] How to add small piece of PHP code to aMember

We recommend you to create file amember/site.inc.php and put all your customization code to this file. If you follow this advice, your customizations won't be lost after upgrade, and it will be easy to track. To start, upload file amember/site.inc.php with the following content:

<?php
 
// customization code must be added below this line

Make sure that there are no spaces, newlines or any other output before <?php .

Before you read this topic, make sure you've read topic Understanding User Record in previous chapter. It is necessary for understanding of this chapter.

[edit] Hooks - validation functions

[edit] validate_signup_form(&$vars)

This function will be called when user submits signup form. $vars is an array of submitted values. You may check it and return array of errors. If no errors found, you may return empty array.

This function is extremely useful, when you added some fields and need to validate entered values for these fields. This function can also change submitted variables (validation goes before any processing).

Example (validates additional phone and fax fields):

function vsf(&$vars){
    $errors = array();
    if (!strlen($vars['phone']))
        $errors[] = "Please enter phone number";
    if (!strlen($vars['fax']))
        $errors[] = "Please enter fax number";
    return $errors;
}
setup_plugin_hook('validate_signup_form', 'vsf');

[edit] validate_member_form(&$vars)

This function will be called when user submits renewal form from http://www.yoursite.com/amember/member.php. $vars is an array of submitted values. You may check it and return array of errors. If no errors found, you may return empty array. Please note, that this form doesn't contain any member info. However, you may get member info from session.

The following example will deny purchase of product #10 if it comes from a customer with aol.com e-mail address. Please note, this validation will only work on http://www.yoursite.com/amember/member.php page.

function vmf(&$vars){
    $errors = array();
    if (preg_match('/\@aol\.com$/', $_SESSION['_amember_user']['email']) &&
        ($vars['product_id'] == 10)){
        $errors[] = "Sorry, but purchase of product #10 is prohibited for @aol.com customers";
    }
    return $errors;
}
setup_plugin_hook('validate_member_form', 'vmf');


[edit] Hooks - payments update

[edit] finish_waiting_payment($payment_id)

This function will be called when a payment(subscription) status changed to Paid ("Completed"). Then you can retreive info from database about payment and use it to execute some custom actions. Please note - if admin switches payment status Paid -> NotPaid -> Paid from control panel, this function can be executed twice for the same payment. Be careful.

Example of this hook function:

function fwp($payment_id){   
    global $db;
    $payment = $db->get_payment($payment_id); // $payment is now an array
    $product = $db->get_product($payment['product_id']);
    $member = $db->get_user($payment['member_id']);
    print "For this period: $payment[begin_date] - $payment[expire_date]<br>";
    print "$product[title] ordered by $member[name_f] $member[name_l]<br>";
}


[edit] update_payments($payment_id)

This function called when payment $payment_id has been updated somehow - changed, marked as paid, added, deleted. You can use the same code as for finish_waiting_payment function to retreive payment information. Use print_r function to get contents of the $payment, $product and $member arrays and see what is available.


[edit] aMember database functions

Often, in plugin hooks, you need to access database records. It is possible in numerous ways. Usually, it is right way to access aMember database with special access functions. You will find description of these functions in this topic. Note: you have to define global $db; in your function to get access to database object.

[edit] $q = $db->query($sql)

It is not recommended, but if you want to execute a MySQL query to aMember database, you should use this function. This function will access right database and it will handle errors automatically.

To substitute aMember tables prefix into query, use {$db->config[prefix]} in your string, like

global $db;
$db->query($sql = "SELECT member_id
    FROM {$db->config[prefix]}members
    WHERE login = 'someone'");

[edit] $u = $db->get_user($user_id)

This function will return you a user record for given user_id

[edit] $db->update_user($user_id, $u)

This function will update user with new user record $u. You should load record with $db->get_user, change some fields, then pass changed record to this function. For example:

global $db;
$u = $db->get_user(132);
$u['city'] = 'New York';
$db->update_user(132, $u);

[edit] $db->delete_user($user_id)

This function will delete user record with given $user_id

[edit] $p = $db->get_payment($payment_id)

This function will return you a payment record with given $payment_id

[edit] $db->update_payment($payment_id, $p)

This function will update payment with id $payment_id to give record $p. Have a look to notes about update_user function to get usage example.

[edit] $db->delete_payment($payment_id)

This function will delete payment with given $payment_id

[edit] $db->get_user_payments($member_id, $completed)

This function will return you an array of member payments, each record in this array is the same as returned from get_payment function. $completed parameter may be

  • 0 - All member payments (including Pending) should be returned;
  • 1 - Only Paid ("completed") payments should be returned.

The following code snippet will show you how to select only active (non-expired) payments from the list:

global $db;
$pl = array();
$dat = date('Y-m-d'); // get today date in SQL format
foreach ($db->get_user_payments($member_id, 1) as $p){
    if (($p['begin_date']<=$dat) && ($p['expire_date']>=$dat)){
        $pl[] = $p;
    }
}
// now $pl contains only completed and non-expired payments
print_rr($pl, 'active payments');

[edit] Advanced hooks (for add-on creators)

[edit] init_admin_menu

This hook can be used to add new items and sections to aMember CP admin menu. For example, you can use the following code in your add-on or site.inc.php file:

<?php
 
setup_plugin_hook('init_admin_menu', 'iam');
 
/**
 * @param $menu AdminMenu (defined in amember/admin/menu.inc.php)
 */
function iam(& $menu){
    // Create new section and add menu item to it
    $section = & $menu->addSection('addon', 'My Add-on', '../plugins/protect/addon/addon_main.php');
    $section->addItem('Do Action', 'http://www.mysite.com/addon_do.php');
    // Get existing "Users" section and add menu item to it
    $section = & $menu->getSection('users');
    $section->addHtml('<br />');
    $section->addItem('Add-on User Action', '/addon_user_action.php');
}

This hooks is available starting from aMember 3.0.8

Personal tools