<?php defined('SYSPATH') or die('No direct script access.'); 

        class Controller_Transfer extends Controller { 
    
            public function action_transferstatus() 
            {
               $data = array();

                        if(isset($_POST["search"]) && $_POST["search"] ){
			    $play = Search::instance()->find($_POST["search"]);
			    $id_array=array();
			    for($i=0;$i<count($play);$i++)
				$id_array[$i]=$play[$i]->searchid;  
                             //throw new exception($_POST["search"]);
			    if(count($id_array)!=0){			    
			    $player = ORM::factory("player")->where('id','IN',$id_array)
								    ->where('sale','=',1)
								    ->order_by('id','DESC')								    
								    ->find_all()
                                                                    ->as_array();
                               $data = array("player" => $player);

                            }else{
                              $player = ORM::factory("player")->where('sale','=',1)->find_all()->as_array();
		              $data = array("player" => $player);
                             }
                             }
                            
                            else{

                                
                              
	                      $player = ORM::factory("player")->where('sale','=',1)->find_all()->as_array();
		              $data = array("player" => $player);
                
             
	                    }
                
                $view = View::factory("transfer",$data);  
                $this->request->response = $view; 
            }        
            public function action_buy($id=0) 
            {
               $me = Session::instance()->get("fbUserId");
            $data = array(); 
	    $player = ORM::factory("player")->where('id','=',$id)->find();
            $economy = ORM::factory("economy")->where('id','=',$me)->find();
	    $player->sale = FALSE;
            $economy->total_money -= $player->value;
            $economy->weekly_total_outgoing += $player->value;
            $economy->save();
            if(!empty($player->teamid))
           { $economy2 = ORM::factory("economy")->where('id','=',$player->teamid)->find();
            $economy2->total_money += $player->value;
            $economy2->weekly_total_income += $player->value;
            $economy2->save();}
	   // throw new Exception($player->id);
	    
	    $player->teamid = $me;
	    $player->save();
	    $this->action_transferstatus();
	    //throw new Exception($team[0]->id);
		//$data = array("player" => $player);
                
                //$view = View::factory("player",$data);
	
                
             //  $view = View::factory("transfer",$data);  
             //   $this->request->response = $view; 
            }         
    
        }

?>