<?php

  include("html_functions.php");



  ################## PAGE INITIALIZATION #########################

  $lgid=$_SESSION['lgid'];

  $date=new DateTime($_SESSION['lgdate']);

  $lgyear=$date->format("Y");

  $hmid=$_SESSION['user_id'];

  if ((isset($_GET['hmid']))&&($_SESSION['user_is_commish'])) {$hmid=$_GET['hmid'];}



  ################## START OF PAGE CONTENT #########################

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

  

  ##### Export Draft Pool #####

  $ageCol="(".$date->format("Y")."-YEAR(date_of_birth)-(RIGHT(date_of_birth,5)>'".$date->format("m-d")."')) as age";

  $query="SELECT p.player_id,first_name,last_name,date_of_birth,$ageCol,position,role FROM players as p,ootp_sql_draft_list as dl WHERE p.player_id=dl.player_id AND human_manager_id=$hmid AND dl.league_id=$lgid ORDER BY porder";

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

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

  if (mysqli_num_rows($result))

  {

	  $cnt=0;

	  $line[$cnt]=Array("//Format: Player ID","Player Name", "Date of Birth", "Age", "Position", "Role");

	  $cnt++;



	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  if ($pid=="") {$pid=0;}



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

		  $dob=$row['date_of_birth'];

		  $age=$row['age'];

		  $pos=get_pos($row['position'],1);

		  if ($row['position']!=1) {$role="";} else {$role=get_pos($row['role'],1);}



		  $line[$cnt]=Array($pid,"$name","$dob",$age,$pos,$role);

		  $cnt++;

	  }



	  header("Content-type: text/csv");

	  if ($hmid==-999) {header("Content-Disposition: attachment; filename=draft_auto_list.csv");}

	  else {header("Content-Disposition: attachment; filename=draft_list.csv");}

	  header("Pragma: no-cache");

	  header("Expires: 0");

	  outputCSV($line); 

  }

  else

  {

	  echo "You have no draft list defined";

  }



  mysqli_close($db);



?>

