<?php # vim: set filetype=php expandtab tabstop=2 shiftwidth=2 autoindent smartindent:
/* TODO: add user billing address. User card information or other things may be .. */
/* TODO: may be a block showing billing information and last transaction .. */
function user_balance_perma() {
return array("has balance");
}
function user_balance_helpa($path, $arg) {
switch($path) {
case "user/%user/balance" :
return "User Balance and billing details.";
}
}
function user_balance_menua() {
return array(
"user/%user/balance" => array(
"title" => "Balance Information"
,"description" => "Billing information as well as billing log view"
,"page callback" => "drupal_get_form"
,"page arguments" => array("user_balance_details", 1)
,"access callback" => "user_balance_has_balance"
,"access arguments" => array(1)
,"file" => "user_balance.pages.inc"
,"type" => MENU_LOCAL_TASK
)
);
}
function user_balance_has_balancea($account) {
global $user;
if($account->uid == 1) {
return FALSE; /* forbid the admin user.. */
}
if(!user_access("has balance", $account)) {
return FALSE;
}
if($account->uid == $user->uid || user_access("administer users", $user)) {
return TRUE;
}
return FALSE;
}
function user_balance_user($op, &$edit, &$account, $category = NULL) {
watchdog('godebug', var_export($account, TRUE));
if(!$account || !user_access("has balance", $account)) {
return;
}
return;
if($op == "categories") {
return array(
array(
"name" => "balance"
,"title" => t("Balance Information")
,"weight" => 7
,'access callback' => 'user_balance_has_balance'
,'access arguments' => array (
1
)
)
);
}
if($op == "view") {
/* TODO: show last transaction .. */
$account->content["balance"] = array(
'#type' => 'user_profile_category'
,'#title' => "Balance Information"
,'current balance' => array(
'#type' => 'user_profile_item'
,'#title' => "Current Balance"
,'#value' => theme("balance", $account->amount, variable_get("balance default currency", 0))
)
);
return;
}
if($op == "load") {
$account->amount = db_result(db_query("SELECT amount FROM {user_balance} AS ub JOIN {balance} AS b ON (ub.bid = b.bid) WHERE ub.uid = %d", $account->uid)); /* see if we have balance entry .. */
return;
}
$add_balance_entry = FALSE;
if($op == "insert") {
$add_balance_entry = TRUE;
}
if($op == "update") {
$bid = db_result(db_query("SELECT bid FROM {user_balance} WHERE uid = %d", $account->uid)); /* see if we have balance entry .. */
if(!$bid) {
$add_balance_entry = TRUE;
}
}
if($add_balance_entry) {
/* when user is inserted we add balance information to his/her account .. */
db_query("INSERT INTO {balance} SET amount = 0, module = 'user_balance'");
$bid = db_last_insert_id("balance", "bid");
db_query("INSERT INTO {user_balance} SET bid = %d AND uid = %d", $bid, $account->uid);
}
}