Thread: (1.3.4) Banks
View Single Post
  #1  
Old 12-05-2016, 09:25 PM
Dinocanid's Avatar
Dinocanid Dinocanid is offline
Member
 
Join Date: Aug 2016
Location: Maryland, USA
Posts: 516
Gender: Unknown/Other
Credits: 65,363
Dinocanid is on a distinguished road
Default (1.3.4) Banks

After over an hour of work and smashing the keyboard wondering why the forms weren't working correctly, I present to you:
banks!

-Step 0-
Go to phpMyAdmin, adopts_users, and create a new column with this info:
Code:
Name: bank
Type: int
Length/Values: 11
Default: As defined 0
check the null box
-Step 1-
Create a new page for your bank from scratch. If you don't know how to make pages from scratch go here. Do not create a new page from the admincp.
Create a file named bank.php in your root folder and add the following contents:
PHP Code:
<?php

class BankController extends AppController{

    public function 
__construct(){
        
parent::__construct("member");    
    }
    
    public function 
index(){
        
$mysidia Registry::get("mysidia");
    }
}
?>
Next, make a new file called bankview.php in your view folder with the following contents:
PHP Code:
 <?php
class BankView extends View{

    public function 
index(){
      
        
$mysidia Registry::get("mysidia");
        
$document $this->document;        
        
$document->setTitle("The Bank");  
        
$balance $mysidia->user->getbank();
        
        if (
$balance <= 0){
        
$document->add(new Comment("<h2>Current Balance: 0 CURRENCY</h2>"));
        }
        else 
$document->add(new Comment("<h2>Current Balance: {$balance} CURRENCY</h2>"FALSE));
        
$document->add(new paragraph);

if(
$mysidia->input->post("deposit")){
$amount $mysidia->input->post("amount");
$balance $mysidia->db->select("users", array("bank"))->fetchColumn();
$mysidia->user->changecash(-$amount);
$mysidia->user->changebank(+$amount);
$document->add(new Comment("<h2>You deposited {$amount} CURRENCY into your bank account</h2>"FALSE));
$document->add(new Comment("<br><a href='{$path}bank'>Return to Bank</a>  "FALSE));
return 
TRUE;}

$depositForm = new FormBuilder("depositForm""""post");
$depositForm->buildComment("Amount: "FALSE)
  ->
buildTextField("amount"FALSE)
  ->
buildButton("Deposit""deposit""submit");
        
$document->add($depositForm);
        
        
        
        
     if(
$mysidia->input->post("withdraw")){
$amount $mysidia->input->post("amount");
$balance $mysidia->db->select("users", array("bank"))->fetchColumn();
$mysidia->user->changecash(+$amount);
$mysidia->user->changebank(-$amount);
$document->add(new Comment("<h2>You withdrew $ {$amount} from your bank account</h2>"FALSE));
$document->add(new Comment("<br><a href='{$path}bank'>Return to Bank</a>  "FALSE));
return 
TRUE;}  

 
$withdrawForm = new FormBuilder("withdrawForm""""post");
$withdrawForm->buildComment("Amount: "FALSE)
  ->
buildTextField("amount"FALSE)
  ->
buildButton("Withdraw""withdraw""submit");
        
$document->add($withdrawForm);
    }
}
?>
-Step 2-
Now go to class_member.php and add this with the other public items:
PHP Code:
public $bank
Afterwards, add this with the rest of the functions:
PHP Code:
  public function changebank($amount){     
      
$mysidia Registry::get("mysidia");
      if(!
is_numeric($amount)) throw new Exception('Cannot change user money by a non-numeric value!');
      
      
$this->bank += $amount;    
      if(
$this->bank >= 0){ 
         
$mysidia->db->update("users", array("bank" => $this->bank), "username = '{$this->username}'");
         return 
TRUE;              
      }
      
///else throw new InvalidActionException("It seems that {$this->username} cannot afford this transaction.");
  

(The else function is commented out for now so the user avoids any errors. I'll clean it up when I find a way)

Next, go to class_user.php and add this with the other public things:
PHP Code:
public $bank
And this with the other functions:
PHP Code:
      public function getbank(){
     return 
$this->bank;

-End-
That should be it. Now users are able to add and withdraw from a bank account that holds their currency. As of right now, the user's money doesn't appear to go down until they go to another page and the balance doesn't appear to go up until they leave the page and come back. If anyone knows how to make it update right then and there, let me know!
Be sure to let your users know that if they refresh the bank page after depositing money then it will deposit the same amount again. The same goes for withdrawing money. A "confirm resubmission" popup appears on some browsers though (including chrome), which should help avoid accidents.
__________________

Last edited by Dinocanid; 12-07-2016 at 01:02 PM.
Reply With Quote