Fix for my module is showing bug .. not sure why ..

  1. <?php # vim: set filetype=php expandtab tabstop=2 shiftwidth=2 autoindent smartindent:
  2.  
  3. /* TODO: add user billing address. User card information or other things may be .. */
  4. /* TODO: may be a block showing billing information and last transaction .. */
  5.  
  6. function user_balance_perma() {
  7.   return array("has balance");
  8. }
  9.  
  10. function user_balance_helpa($path, $arg) {
  11.   switch($path) {
  12.     case "user/%user/balance" :
  13.     return "User Balance and billing details.";
  14.   }
  15. }
  16.  
  17. function user_balance_menua() {
  18.   return array(
  19.     "user/%user/balance" => array(
  20.       "title" => "Balance Information"
  21.       ,"description" => "Billing information as well as billing log view"
  22.       ,"page callback" => "drupal_get_form"
  23.       ,"page arguments" => array("user_balance_details", 1)
  24.       ,"access callback" => "user_balance_has_balance"
  25.       ,"access arguments" => array(1)
  26.       ,"file" => "user_balance.pages.inc"
  27.       ,"type" => MENU_LOCAL_TASK
  28.     )
  29.   );
  30. }
  31.  
  32. function user_balance_has_balancea($account) {
  33.   global $user;
  34.   if($account->uid == 1) {
  35.     return FALSE; /* forbid the admin user.. */
  36.   }
  37.   if(!user_access("has balance", $account)) {
  38.     return FALSE;
  39.   }
  40.   if($account->uid == $user->uid || user_access("administer users", $user)) {
  41.     return TRUE;
  42.   }
  43.   return FALSE;
  44. }
  45.  
  46. function user_balance_user($op, &$edit, &$account, $category = NULL) {
  47.   watchdog('godebug', var_export($account, TRUE));  
  48.   if(!$account || !user_access("has balance", $account)) {
  49.     return;
  50.   }
  51.   return;
  52.   if($op == "categories") {
  53.     return array(
  54.       array(
  55.         "name" => "balance"
  56.         ,"title" => t("Balance Information")
  57.         ,"weight" => 7
  58.         ,'access callback' => 'user_balance_has_balance'
  59.         ,'access arguments' => array (
  60.           1
  61.         )
  62.       )
  63.     );
  64.   }
  65.   if($op == "view") {
  66.     /* TODO: show last transaction .. */
  67.     $account->content["balance"] = array(
  68.       '#type' => 'user_profile_category'
  69.       ,'#title' => "Balance Information"
  70.       ,'current balance' => array(
  71.         '#type' => 'user_profile_item'
  72.         ,'#title' => "Current Balance"
  73.         ,'#value' => theme("balance", $account->amount, variable_get("balance default currency", 0))
  74.       )
  75.     );
  76.     return;
  77.   }
  78.   if($op == "load") {
  79.     $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 .. */
  80.     return;
  81.   }
  82.   $add_balance_entry = FALSE;
  83.   if($op == "insert") {
  84.     $add_balance_entry = TRUE;
  85.   }
  86.   if($op == "update") {
  87.     $bid = db_result(db_query("SELECT bid FROM {user_balance} WHERE uid = %d", $account->uid)); /* see if we have balance entry .. */
  88.     if(!$bid) {
  89.       $add_balance_entry = TRUE;
  90.     }
  91.   }
  92.   if($add_balance_entry) {
  93.     /* when user is inserted we add balance information to his/her account .. */
  94.     db_query("INSERT INTO {balance} SET amount = 0, module = 'user_balance'");
  95.     $bid = db_last_insert_id("balance", "bid");
  96.     db_query("INSERT INTO {user_balance} SET bid = %d AND uid = %d", $bid, $account->uid);
  97.   }
  98. }