y<?php
date_default_timezone_set('Africa/Johannesburg');
class Union_BlacklistController extends Zend_Controller_Action
{
    private $identity;
    private $unions;
    private $UserTypes;
    private $ProfileDB;
    private $level;
    private $province;
    private $position;
    
    
    public function init()
    {
        /* Initialize action controller here */
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity())
        {
            $this->_redirect('/login/');
        }
        else
        {
            $this->identity = $auth->getIdentity();
            if($this->identity->UnionType <3){
                
            }else{
                $this->_redirect('/');
            }
        }		
				
				
		
        
    }
		
		
    
    public function indexAction()
    {
        
        
    }
    public function checkmailAction()
    {
        $this->_helper->viewRenderer->setNoRender();
      	$this->_helper->layout->disableLayout();
        $request = $this->getRequest();
        $mail = $request->getParam("mail");
        $CrDb = new Model_Credentials;
        $details = $CrDb->fetchRow("Username ='".$mail."'");
        if(!$details){
            echo 0;
        }else{
        $BlacklistDb = new Model_Blacklist;
        $check = $BlacklistDb->fetchRow("Email ='".$mail."'");
        if(!isset($check["id"])){
        $ProfileDB = new Model_Profile;
        $Profile = $ProfileDB->getRow($details["UserID"]);
       
        $PlayerCareerDB  = new Model_PlayerCareer;
        $PlayerCareer = $PlayerCareerDB->getRow($details["UserID"]);
        
        $SquadsLis = json_decode($PlayerCareer["Squads"],true);
        echo "<pre>";
        //print_r($Profile->toArray());
        
        //print_r($$PlayerCareer->toArray());
        end($SquadsLis);         // move the internal pointer to the end of the array
       $key = key($SquadsLis);
       
        
        $SquadDB  = new Model_Squad;
        $SquadDetails = $SquadDB->getSquad($SquadsLis[$key]["SID"]);
        $Coaches = json_decode($SquadDetails["Coaches"],true);
        $teammanager = 0;
        foreach($Coaches as $Coache){
            if($Coache["Position"]==42){
               $teammanager =$Coache["ID"]; 
            }
        }
        if($teammanager==0){
            foreach($Coaches as $Coache){
            if($Coache["Position"]==200){
               $teammanager =$Coache["ID"];
            }
        }
        }
        
     
        $TeamManagerProfile = $ProfileDB->getRow($teammanager);
        
        $arrayData["FullName"]= $Profile["FirstName"]." ".$Profile["LastName"];
        $arrayData["DateAdded"]= date("Y-m-d H:i:s");
        $arrayData["Email"]= $Profile["Email"];
        $arrayData["Squad"]= $SquadDetails["SquadName"];
        $arrayData["SID"]= $SquadDetails["SID"];
        $arrayData["TeamManagerID"]= $teammanager;
        $arrayData["TeamEmail"]= $TeamManagerProfile["Email"];
        $arrayData["TeamName"]= $TeamManagerProfile["FirstName"]." ".$TeamManagerProfile["LastName"];
        
        
        $BlacklistDb->insert($arrayData);
        echo 2;
        }else{
            echo 1;
        }
        
       // print_r($TeamManagerProfile->toArray());
        
        }
    }
    
    public function listmailAction()
    {
        $this->_helper->viewRenderer->setNoRender();
      	$this->_helper->layout->disableLayout();
        $BlacklistDb = new Model_Blacklist;
        $details = $BlacklistDb->getAll();
        echo json_encode($details->toarray());
    }
    
}