<?php
date_default_timezone_set('Africa/Johannesburg');
class Union_FirstController extends Zend_Controller_Action
{
    private $identity;
    private $unions;
    private $UserTypes;
    private $ProfileDB;
    private $level;
    private $province;
    private $position;
    private $coach = array("Head Coach","Assistant Coach", "Forward Coach","Backline Coach",'Attack Coach','Defence Coach','Lineout Coach','Scrum Coach','Kicking Coach','Conditioning Coach');
    private $admin = array('Team Manager','General Manager','Executive','Non-Executive','CEO','Administration Manager','FP Manager');
    private $medical = array('Executive','Team Docter','Physiotherapist','Masseuse','Conditioner');
    
    public function init()
    {
        /* Initialize action controller here */
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity())
        {
            $this->_redirect('/login/');
        }
        else
        {
            $this->identity = $auth->getIdentity();
        }		
				
				
				$user = $auth->getIdentity();
				
				
					$db = new Model_Profile;
          	$this->identity = $db->getRow($user->UserID);
        // GET CURRENT REQUEST METHOD AND ACTIONS
        $this->view->assign("getControllerName",$this->getRequest()->getControllerName());
        $this->view->assign("getActionName",$this->getRequest()->getActionName());
				
				
				$this->AccessRights = json_decode($this->identity->Role,true);
				$this->view->assign("AccessRoles",$this->AccessRights);
				
				$this->view->assign("UserDetails",$this->identity);
				
				$this->view->assign("getControllerName",$this->getRequest()->getControllerName());
				$this->view->assign("getActionName",$this->getRequest()->getActionName());
				
				//$this->userDetailsDB = new Model_UsersDetails;
        $this->unions = new Model_Union;
        $this->UserTypes = new Model_UserType;
        $this->ProfileDB = new Model_Profile;
        $this->level = new Model_Level;
        $this->province = new Model_Province;
        $this->position = new Model_Positions;
    }
		
		
    
    public function indexAction()
    {
        $ProfileDB = new Model_Profile;
        
        $StatusDB = new Model_RelationshipStatus;
				$this->view->assign("StatusList",$StatusDB->getAll());
                                $RelationshipDB = new Model_Relationship;
				$this->view->assign("Relationship",$RelationshipDB->getAll());
    	$this->view->assign("UserID",$this->identity->UserID);
        if($this->identity->Status==0){
    	if($this->identity->JobType == 2){
    		
    		$this->Player();
    		$this->_helper->viewRenderer('/first/players', null, true);
    	}else{
    		$this->Personnel();
    		$this->_helper->viewRenderer('/first/personnal', null, true);
    	}
        }else if($this->identity->Status==1 && $this->identity->Active==0){
            if($this->identity->JobType == 2){
                    $ProfileDB->update(array("Active"=>1),"UserID ='".$this->identity->UserID."'");
                     $this->_redirect('/union/');
            }
            $this->_helper->viewRenderer('/first/thankyou', null, true);
        }else{
             $this->_redirect('/union/');
        }
        
    }
    private function Personnel()
    {
	$request = $this->getRequest();
	$id=$this->identity->UserID;
	$ProfileDB = new Model_Profile;
                                
                                if($request->isPost())
                                    {
                                    
                                    $this->_helper->viewRenderer->setNoRender();
                                    
                                    $this->_helper->layout->disableLayout();
                                    
                                    $Data = $request->getParam("data");
                                   
                                    $birth = $request->getParam("birth");
                                    
                                    $Data["DateOfBirth"] = date('Y-m-d',strtotime($birth["year"]."-".$birth["month"]."-".$birth["day"]));
                                    
                                    $Data["PassportExpireDate"] = date('Y-m-d',strtotime($birth["xyear"]."-".$birth["xmonth"]."-".$birth["xday"]));
				
				
                                    $Data['Status']   =   1;
                                    $Data['DateModified']   =   date("Y-m-d H:s:i");
                                    $Data["FirstName"]      =   ucfirst($Data["FirstName"] );
                                    $Data["LastName"]       =   ucfirst($Data["LastName"] );
                                    $Data["FullName"]       =   ucfirst($Data["FirstName"] )." ".ucfirst($Data["LastName"] );
					
                                    $insert = $ProfileDB-> updateProfile($Data,$id);
                                    
                                    $dbCredentials = new Model_Credentials;
                                    $insertToDb = $dbCredentials->updateData(array("Username"=>$Data["Email"]),$id);
                                
                                }
                                
                                
		$UserTypeDB = new Model_UserType;
                $CountryDB = new Model_Country;
                $EthnicityDB = new Model_Ethnicity;
                $TitleDB = new Model_Title;
		$RelationDB = new Model_RelationshipStatus;
		$InstTypeDB = new Model_OrganizationTypes;		
				//Get User Datails
		$UserProfile = $ProfileDB->getRow($id);
		
		$Country = $CountryDB->getAll();
		//Accreditation
		$Accr = $ProfileDB->getAccredByID($id);
		//Title
		$Title = $TitleDB->getAll();
		//Relationship
		$RelStatus = $RelationDB->getAll();
		//Docs
		//Get Positions 1-24
		$AllPos = $UserTypeDB->getAll();
		//Ethnicity
		$Ethnicity = $EthnicityDB->getAll();
		$RelStatus = $RelationDB->getAll();
		
		$InstType = $InstTypeDB->getAll();
		$OrgType = 	$InstTypeDB->getTypes($id);
		$UserProfile["UserID"]=$id;
		$this->view->assign("Profile",$UserProfile);
                
                if($this){
                    
                }
                
		$this->view->assign("RelStatus", $RelStatus);
		$this->view->assign("Positions", $AllPos);
		$this->view->assign("Accr", $Accr);
		$this->view->assign("Country", $Country);
		$this->view->assign("Title", $Title);
		$this->view->assign("Ethnicity", $Ethnicity);
		$this->view->assign("InstType", $InstType);
		$this->view->assign("OrgType", $OrgType);
                
                $UnionsDb = new Model_Unions;
                
                $UnionsTypes = new Model_IntuitutionTypes;
		if($this->identity["UnionType"]== 12 || $this->identity["UnionType"] <3){
		$this->view->assign("listTypesUnions",$UnionsTypes->fetchAll("type_id >1 "));
		}else{
                $this->view->assign("listTypesUnions",$UnionsTypes->fetchAll("type_id >2 AND type_id!=11"));     
		}
                $unions = $UnionsDb->getAllDropnew();
		$this->view->assign("listUnions",$unions);
		
                                
    }
    
    private function Player(){
	$request = $this->getRequest();
	$id=$this->identity->UserID;
	$ProfileDB = new Model_Profile;
                                
                                if($request->isPost())
                                    {
                                    
                                    $this->_helper->viewRenderer->setNoRender();
                                    
                                    $this->_helper->layout->disableLayout();
                                    
                                    $Data = $request->getParam("data");
                                   
                                    $birth = $request->getParam("birth");
                                    
                                    $Data["DateOfBirth"] = date('Y-m-d',strtotime($birth["year"]."-".$birth["month"]."-".$birth["day"]));
                                    
                                    $Data["PassportExpireDate"] = date('Y-m-d',strtotime($birth["xyear"]."-".$birth["xmonth"]."-".$birth["xday"]));
				
				
                                    $Data['Status']   =   1;
                                    $Data['Active']   =   1;
                                    $Data['author']   =  $this->identity->UserID;
                                    $Data['DateModified']   =   date("Y-m-d H:s:i");
                                    $Data["FirstName"]      =   ucfirst($Data["FirstName"] );
                                    $Data["LastName"]       =   ucfirst($Data["LastName"] );
                                    $Data["FullName"]       =   ucfirst($Data["FirstName"] )." ".ucfirst($Data["LastName"] );
					
                                    $insert = $ProfileDB-> updateProfile($Data,$id);
                                    
                                    $dbCredentials = new Model_Credentials;
                                    $insertToDb = $dbCredentials->updateData(array("Username"=>$Data["Email"]),$id);
                                
                                }
                                
                                
                                
		$PositionsDB = new Model_Positions;
			$CountryDB = new Model_Country;
			$EthnicityDB = new Model_Ethnicity;
			$TitleDB = new Model_Title;
		$RelationDB = new Model_RelationshipStatus;
		$InstTypeDB = new Model_OrganizationTypes;		
				//Get User Datails
		$UserProfile = $ProfileDB->getRow($id);
		
		$Country = $CountryDB->getAll();
		//Accreditation
		$Accr = $ProfileDB->getAccredByID($id);
		//Title
		$Title = $TitleDB->getAll();
		//Relationship
		$RelStatus = $RelationDB->getAll();
		//Docs
		//Get Positions 1-24
		$AllPos = $PositionsDB->getAll();
		//Ethnicity
		$Ethnicity = $EthnicityDB->getAll();
		$RelStatus = $RelationDB->getAll();
		
		$InstType = $InstTypeDB->getAll();
		$OrgType = 	$InstTypeDB->getTypes($id);
		$UserProfile["UserID"]=$id;
		$this->view->assign("Profile",$UserProfile);
				
				$this->view->assign("RelStatus", $RelStatus);
		$this->view->assign("Positions", $AllPos);
		$this->view->assign("Accr", $Accr);
			$this->view->assign("Country", $Country);
		$this->view->assign("Title", $Title);
		$this->view->assign("Ethnicity", $Ethnicity);
		
		$this->view->assign("InstType", $InstType);
		$this->view->assign("OrgType", $OrgType);
                if($Data['Status'] ==1){
                    $ProfileDB->upadte(array("Active"=>1),"UserID ='".$this->identity->UserID."'");
                     $this->_redirect('/union/');
                }
    }
    
    public function roleAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
       
        $request = $this->getRequest();
        $id =$request->getParam("data");
        //print_r($id); exit;
        if($request->isPost())
        {
            $usertype = $this->UserTypes->getTypes($id);
            //$this->view->assign("role",$usertype->toArray());
            echo json_encode($usertype->toArray());
        }

    }
   
    public function verifyMail($Data)
    {
        $content = "<html><head></head><body><h3>Registration Confirmation</h3><p></p></body></html>";
        
        $mail = new Zend_Mail();
        $mail->setBodyHtml($content);
        
        $mail->setFrom("support@footprintapp.net","FootprintApp");
        $mail->addTo($Data["Email"],$Data["FirstName"]." ".$Data["LastName"]);
        $mail->setSubject("Verification Process");
        $mail->send();
    }
    
}