<?php

class Model_MatchReportSubstitutions extends Zend_Db_Table
{
    protected $_name = 'Match_Report_Substitutions';
		
public function insertData($data)
    {
        try 
        {
        
            $ID = $this->insert($data);
			
			return $ID;
        } 
        catch (Exception $ex) 
        {
        	echo "<pre>";
        	print_r($ex);
			exit;
            return false;
        }
    }
		
 public function UpdateSubs($Fixture){
 	
   	$NewSubs = array();
			if(!empty($Fixture["Substitutions"])){
				$Substitutions = json_decode($Fixture["Substitutions"],true);
				if(count($Substitutions)>0){
					$MRSubs = new Model_MatchReportSubstitutions;
					$delete = $MRSubs->delete("FID ='".$NewSubs["FID"]."' AND SID ='".$NewSubs["SID"]."'");
					foreach($Substitutions as $Substitution){
				if($Substitution["PlayerID"]>0){
				$sec = $Substitution["GameTime"];
				$GameTime = explode(":", $Substitution["GameTime"]);
				if($GameTime[1]){
					$m = $GameTime[0]; 
					$s = $GameTime[1]; 

				$sec = ($m * 60) + $s;
				}
				$Type = 0;
				if($Substitution["Type"]=="PlayerOn"){
					$Type = 1;
				}
				if($Substitution["Type"]=="PlayerOff"){
					$Type = 2;
				}
				$NewSubs["FID"] = $Fixture["FID"];
				$NewSubs["SID"] = $Fixture["SID"];
				$NewSubs["PlayerID"] = $Substitution["PlayerID"];
				$NewSubs["Type"] = $Type;
				$NewSubs["GameTime"] = $Substitution["GameTime"];
				$NewSubs["Option"] = $Substitution["Option"];
				$NewSubs["TypeOpt"] = $Substitution["Type"];
				$NewSubs["Seconds"] = $sec;
				
				
				
				$MRSubs->insertData($NewSubs);
				}
					}
				}
$db = Zend_Db_Table::getDefaultAdapter();   
$db->delete('Players_Fixture_GameTime', array(
    'FID = ?' => $Fixture["FID"],
    'SID = ?' => $Fixture["SID"]
));
	 
    $db->query("CALL Insert_Game_TimePlayed(".$Fixture["FID"].",".$Fixture["SID"].")");
			}
	
   }
		
    
}