<?php

class Model_MatchReport extends Zend_Db_Table
{
    protected $_name = 'Match_Report';
		
public function insertData($data)
    {
        try 
        {
        	if(!isset($data["Total_22Mtry"])){
        		$data["Total_22Mtry"]=0;
        	}
			if(!isset($data["Total_PowerPLay"])){
        		$data["Total_PowerPLay"]=0;
        	}
             $ID = $this->insert($data);
			$this->updateTimePlayed($data["FID"]); 
			return $ID;
        } 
        catch (Exception $ex) 
        {
        	echo "<pre>";
        	print_r($ex);
            return false;
        }
    }
		
   
		
    
    
    public function updateData($data,$id)
    {
        try
        {
        	
			if(!isset($data["Total_22Mtry"])){
        		$data["Total_22Mtry"]=0;
        	}
			if(!isset($data["Total_PowerPLay"])){
        		$data["Total_PowerPLay"]=0;
        	}
				$this->update($data,"MID ='".$id."'");  
						
				$this->updateTimePlayed($data["FID"]); 
		
				
			 return  $id; 
		
			 
        	  
        }
        catch(Exception $ex)
        {
             echo "<pre>";
        	print_r($ex);
            return false;
        }
    }
	public function updateReport($data,$fid,$sid)
    {
        try
        {
        	
            $Save = $this->update($data,"FID ='".$fid."' AND SID ='".$sid."'");   
			$this->updateTimePlayed($fid);     
			return $Save;
        }
        catch(Exception $ex)
        {
            echo "<pre>";
        	print_r($ex);
            return false;
        }
    }
	
	public function updateTimePlayed($fid)
    {
    	
        	
			$MRSubs = new Model_MatchReportSubstitutions;
		
			$Fixtures = $this->getReportsByFixture($fid);
			echo "<pre>";
			print_r($Fixtures->toArray());
			if(count($Fixtures)>0){
				foreach ($Fixtures as $Fixture) {
					$MRSubs->UpdateSubs($Fixture);
			}
		
		}
			
		
    }
    public function getReportsByFixture($fid)
    {
        $select = $this->select();
        $select->from(array('U' => $this->_name),array("FID","SID","Substitutions"));
	$select->where("FID ='".$fid."'");
	
        $select->setIntegrityCheck(false);
        $row = $this->fetchAll($select);
       
        return $row;
    }
    
    public function getReport($sid,$fid)
    {
        $select = $this->select();
        $select->from(array('U' => $this->_name));
	$select->where("SID ='".$sid."' AND FID ='".$fid."'");
        $select->setIntegrityCheck(false);
        $row = $this->fetchRow($select);
       /* if(!$row){
            $nsert =$this->insertData(array("SID"=>$sid,"FID"=>$fid));
            if($nsert){
                $this->getReport($sid,$fid);
            }
        }*/
        return $row;
    }
    
    public function getMembers($who,$id)
    {
    	
        $select = $this->select();
        $select->from(array('U' => $this->_name),array($who));
        //$select->joinLeft(array("O"=>"ef_unions"), "O.un_id = U.UnionID",array("un_id"=>"un_id","un_name"=>"un_name"));
        //$select->joinLeft(array("P"=>"profile"),"P.");
        
				$select->where("SID ='".$id."'");
        $select->setIntegrityCheck(false);
        return $this->fetchRow($select);
    }
		
		public function getAllSquadTable($desc,$col,$search,$unionFilter)
    {   
        $select = $this->select();
        $select->from(array('U' => $this->_name));
				
				$select->joinLeft(array("O"=>"ef_unions"), "O.un_id = U.Organization",array("un_id"=>"un_id","un_name"=>"un_name"));
				
        if($search){
            $explode = explode(" ", $search);

             $select->where("U.SquadName LIKE '%$search%' OR U.FormDate LIKE '%$search%' OR U.EndDate LIKE '%$search%' 
                        OR U.RugbyCode LIKE '%$search%' OR U.RugbyLevel LIKE '%$search%' OR U.AgeGroup LIKE '%$search%' 
                        OR O.un_name LIKE '%$search%'
                        ");


        }
        if($unionFilter >0){
                $select->where("O.un_id ='".$unionFilter."'");
        }
				
				

        if($desc){
                $select->order($col." ".$desc);
        }
				
				
        $select->setIntegrityCheck(false);
        return $this->fetchAll($select);       
    } 
		
		public function getSquadsForFixtures($Array){
			
			 $select = $this->select();
        $select->from(array('U' => $this->_name));
				
        $select->setIntegrityCheck(false);
        $squads = $this->fetchAll($select)->toArray(); 
        $n =0;
        foreach ($squads as $value) {
        	if(in_array($value["SID"], $Array)){
        		$squads[$n]["Selected"] =$value["SID"];
        	}else{
        		$squads[$n]["Selected"] =0;
        	}
             
						$n++;
        }
				
				return $squads;
			
		}
		
		public function getScore($fid,$sid)
    {
    	
        $select = $this->select();
        $select->from(array('U' => $this->_name),array("HHS","HFS","Score","Substitutions"));
        //$select->joinLeft(array("O"=>"ef_unions"), "O.un_id = U.UnionID",array("un_id"=>"un_id","un_name"=>"un_name"));
        //$select->joinLeft(array("P"=>"profile"),"P.");
        
				$select->where("SID ='".$sid."' AND FID ='".$fid."'");
        $select->setIntegrityCheck(false);
        return $this->fetchRow($select);
    }
    
}