<?php

  include('html_functions.php');

  if (!isset($_SESSION['adminpath'])) {set_adminpath();}

  include($_SESSION['adminpath']."/dbopen.php");



  ##### Get Parameters #####

  $lgid=$_POST['league_id'];

  $srchStr=$_POST['srchStr'];

  $yr=$_POST['year'];

  $team_id=$_POST['team_id'];



  if (isset($_GET['league_id'])) {$lgid=$_GET['league_id'];}

  if (isset($_GET['srchStr']))   {$srchStr=$_GET['srchStr'];}

  if (isset($_GET['year']))      {$yr=$_GET['year'];}

  if (isset($_GET['team_id']))   {$team_id=$_GET['team_id'];}



  $srchStr=mysqli_real_escape_string($db,$srchStr);



  ##### Search For Players #####

  if (is_numeric($yr))

  {

	  $query="SELECT y.*,t.abbr FROM teams as t,(";

  }

  $query.="SELECT p.player_id,first_name,last_name,organization_id,position,role,MIN(minYr) as minYr,MAX(maxYr) as maxYr FROM players as p,(SELECT DISTINCT player_id,MIN(year) as minYr,MAX(year) as maxYr FROM players_career_batting_stats WHERE league_id=$lgid";

  if (is_numeric($yr)) {$query.=" AND year=$yr";}

  if (is_numeric($team_id)) {$query.=" AND team_id=$team_id";}

  $query.=" GROUP BY player_id";

  $query.=" UNION ";

  $query.="SELECT DISTINCT player_id,MIN(year) as minYr,MAX(year) as maxYr FROM players_career_pitching_stats WHERE league_id=$lgid";

  if (is_numeric($yr)) {$query.=" AND year=$yr";}

  if (is_numeric($team_id)) {$query.=" AND team_id=$team_id";}

  $query.=" GROUP BY player_id";

  $query.=") as x WHERE p.player_id=x.player_id";

  if (is_numeric($team_id)) {$query.=" AND organization_id=$team_id";}

  $query.=" AND (";

  $query.="first_name LIKE '%$srchStr%'";

  $query.=" OR ";

  $query.="last_name LIKE '%$srchStr%'";

  $query.=" OR ";

  $query.="CONCAT(first_name,' ',last_name) LIKE '%$srchStr%'";

  $query.=") GROUP BY player_id";

  if (is_numeric($yr))

  {

	  $query.=") as y WHERE t.team_id=y.organization_id";

  }

  $query.=" ORDER BY last_name,first_name";

  if (isset($_GET['showq'])) {echo "$query<br/>\n";exit;}

  $result=mysqli_query($db,$query);

  	 

  ## Generate XML

  header('Content-type: text/xml');

  $memory = xmlwriter_open_memory();

  xmlwriter_start_document($memory,'1.0','UTF-8');

  xmlwriter_start_element ($memory,'PLAYERS'); // <PLAYERS>



  if (mysqli_num_rows($result)) 

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  $pos=$row['position'];

		  if ($pos==1) {$pos=$row['role'];}

		  $pos=get_pos($pos,1);

		  $name=$row['first_name']." ".$row['last_name'];

		  $years=$row['minYr'];

		  if ($row['minYr']<$row['maxYr']) {$years.="-".$row['maxYr'];}

		  $tabbr=$row['abbr'];



		  xmlwriter_start_element ($memory,'PLAYER'); // <PLAYER>

		  xmlwriter_write_element($memory,'PID',$pid);

  		  xmlwriter_write_element($memory,'NAME',htmlentities($name));

		  xmlwriter_write_element($memory,'POS',$pos);

		  xmlwriter_write_element($memory,'YEARS',$years);

		  xmlwriter_write_element($memory,'TID',$row['organization_id']);

		  xmlwriter_write_element($memory,'TABBR',$tabbr);

		  xmlwriter_end_element($memory); // </PLAYER>		

	  }



  }



  xmlwriter_end_element ($memory); // </PLAYERS>

  echo xmlwriter_output_memory($memory,true);



  mysqli_close($db);

?>

