<?php
  include("html_functions.php");

  ################## PAGE INITIALIZATION #########################
  $lgid=$_SESSION['lgid'];
  $lgpath=$_SESSION['lgpath'];

  # Title Bar
  $title='Managers';

  page_head($title,$_SESSION['lgpath']);
  echo " <div id='topbar'>\n";
  main_titlebar($lgid,$_SESSION['lgpath'],$title);
  nav_toolbar('managers','Manager');
  echo " </div>   <!-- END topbar DIV -->\n";
  echo " <div id='contentpane'>\n";

  ################## START OF PAGE CONTENT #########################
  include($_SESSION['adminpath']."/dbopen.php");
  
  ##### Check Config for Non GM's #####
  $query="SHOW TABLES LIKE 'ootp_sql_admin';";
  $result=mysqli_query($db,$query);
  if (mysqli_num_rows($result)) 
  {
	  $query="SELECT value FROM ootp_sql_admin WHERE setting='showAllUsers';";
  	  $result=mysqli_query($db,$query);
	  $row=mysqli_fetch_array($result);
  	  $showAllUsers=$row['value'];
  }

  ##### Get Manager PM/Email Information #####
  $gmCnt=0;
  $lids="";
  $query="SELECT first_name,last_name,league_id,team_id,is_commish,hm.human_manager_id,setting,value FROM human_managers as hm LEFT JOIN (SELECT identifier as human_manager_id,setting,value FROM ootp_sql_admin WHERE module='users') as x ON hm.human_manager_id=x.human_manager_id";
  if ($showAllUsers!=1) {$query.=" WHERE league_id>0 OR is_commish>0";}
  if ($_GET['showq']==1) {echo $query."<br/>\n";}
  $result=mysqli_query($db,$query);
  if (mysqli_num_rows($result))
  {
	  while ($row=mysqli_fetch_array($result))
	  {
		  $tid=$row['team_id'];
		  $setting=$row['setting'];
		  $hmid=$row['human_manager_id'];

		  if ($tid==0) {$tid=10000*$hmid;}

	       	  $managers[$tid]['id']=$row['human_manager_id'];
		  $managers[$tid]['name']=$row['first_name']." ".$row['last_name'];
		  $managers[$tid]['league_id']=$row['league_id'];
		  $managers[$tid]['is_commish']=$row['is_commish'];
		  $managers[$tid][$setting]=$row['value'];
	  }
	  foreach ($managers as $tid => $val)
	  {
		  $lid=$managers[$tid]['league_id'];
		  if ($lid==0) {$gmCnt++;}
		  if (!isset($leagues[$lid]))
		  {
			  $lids.=",".$lid;
			  if ($lid==0) {$leagues[$lid]=9999;}
			  if ($lid==$lgid) {$leagues[$lid]=0;} else {$leagues[$lid]=$lid;}
		  }

		  if (($managers[$tid]['hideinfo']==1)&&(!isset($_SESSION['user_id'])))
		  {
			  unset($managers[$tid]['fname']);
			  unset($managers[$tid]['flink']);
			  unset($managers[$tid]['email']);
			  unset($managers[$tid]['aim']);
			  unset($managers[$tid]['yim']);
			  unset($managers[$tid]['msn']);
			  unset($managers[$tid]['icq']);
			  unset($managers[$tid]['facebook']);
			  unset($managers[$tid]['twitter']);
			  unset($managers[$tid]['extlink']);
		  }
	  }
  }

  ##### Get League Names #####
  $lids=trim($lids,",");
  if ($lids!="")
  {
       	  $query="SELECT league_id,name FROM leagues WHERE league_id IN ($lids);";
	  if ($_GET['showq']==1) {echo $query."<br/>\n";}
	  $result=mysqli_query($db,$query);
       	  if (mysqli_num_rows($result))
	  {
	    	  while ($row=mysqli_fetch_array($result))
		  {
		 	  $lid=$row['league_id'];
			  $leaguenames[$lid]=$row['name'];
	   	  }
	  }
  }
  $leaguenames[0]="Unaffiliated Users";
  
  ##### Loop Through Human Controlled Leagues
  if ($leagues!="") 
  {
	  asort($leagues);
       	  foreach ($leagues as $lid => $val)
	  {
		  if ($lid==0) {continue;}

	    	  ##### Get Team Info #####
		  $query="SELECT t.team_id,t.name as city,t.abbr,t.nickname,t.logo_file_name,sl.name as sub_league,d.name as division FROM teams as t,team_relations as tr,sub_leagues as sl,divisions as d WHERE t.league_id=$lid AND t.team_id=tr.team_id AND tr.division_id=d.division_id AND t.league_id=d.league_id AND t.sub_league_id=d.sub_league_id AND tr.sub_league_id=d.sub_league_id AND t.league_id=sl.league_id AND t.sub_league_id=sl.sub_league_id ORDER BY sl.sub_league_id,d.division_id,t.team_id;";
		  if ($_GET['showq']==1) {echo $query."<br/>\n";}
		  $result=mysqli_query($db,$query);
		  echo "   <div class='textbox'>\n";
	    	  echo "   <table cellpadding=2 cellspacing=0 border=0 width='920px';>\n";
		  echo "    <tr class='title'><td>".$leaguenames[$lid]."</td></tr>\n";
	    	  $lgcnt=0;
		  $divcnt=0;
	    	  $tcnt=0;
		  while ($row=mysqli_fetch_array($result))
	   	  {
			  $tid=$row['team_id'];
		 	  $tfull=$row['city']." ".$row['nickname'];
			  $logo=urlencode($row['logo_file_name']);
		 
			  $sl=$row['sub_league'];
			  if ($sl!=$prevSl)
			  {
				  if ($lgcnt>0) 
		     		  {
					  echo "\n     </table>\n";
			   		  echo "    </div>\n";
					  echo "   </td></tr>\n";
		     		  }
				  echo "   <tr class='headline'><td class='hsc2_l'>$sl</td></tr>\n";
		      		  echo "   <tr><td>\n";
				  echo "    <div class='tablebox'>\n";
		      		  echo "     <table cellpadding=5 cellspacing=0 width='910px'>\n";
				  $tcnt=0;
		      		  $divcnt=0;
				  $lgcnt++;
			  }
			  $div=$row['division'];
		 	  if ($div!=$prevDiv)
			  {
		      		  if ($divcnt>0) {echo "</tr>\n";}
					  echo "      <tr class='title'><td colspan=6>$div</td></tr>\n";
		      		  $tcnt=0;
				  $divcnt++;
			  }
			  if ($tcnt==0) {echo "      <tr>";}
			  echo "<td valign='top' width='103px'><img src='$lgpath/images/team_logos/".str_replace(".png","_110.png",$logo)."' style='width:100px;height:100px;'/></td>";
			  echo "<td valign='top' width='208px'>";
		 	  if (isset($managers[$tid]['id']))
			  {
		      		  echo "<a href='./manager.php?human_manager_id=".$managers[$tid]['id']."' style='font-size:1.5em;'>".$managers[$tid]['name']."</a>";
				  if ($managers[$tid]['is_commish']==1) {echo " <span title='Commissioner'><sup>(c)</sup></span>";}
		      		  $hide_info=$managers[$tid]['hide_info'];
				  if (($hide_info==0)||(isset($_SESSION['user_id'])))
		     		  {
					  $forum_name=$managers[$tid]['fname'];
			   		  $pm_link=$managers[$tid]['flink'];
					  if (($forum_name=="")&&($pm_link!="")) {$forum_name="PM";}
			   		  if ($pm_link!="") {echo " (<a href='$pm_link'>$forum_name</a>)";}
				  }
		      		  echo "<br /><a href='$lgpath/teams/team_$tid.html'>$tfull</a>";
				  if (($hide_info==0)||(isset($_SESSION['user_id'])))
		     		  {
					  $email=$managers[$tid]['email'];
					  $ext_url=$managers[$tid]['ext_url'];
			   		  $aim=$managers[$tid]['aim'];
					  $yim=$managers[$tid]['yim'];
			   		  $msn=$managers[$tid]['msn'];
					  $icq=$managers[$tid]['icq'];
			   		  $facebook=$managers[$tid]['facebook'];
					  $twitter=$managers[$tid]['twitter'];
			   
					  if ($email!="")
					  {
						  $e=explode(";",$email);
						  echo "<br/>Email: ";
						  for ($idx=0;$idx<count($e);$idx++) {if ($idx>0) {echo ", ";} echo "<a href='mailto:".$e[$idx]."'>".$e[$idx]."</a>";}
					  }
			   		  if ($ext_url!="") {echo "<br /><a href='$ext_url'>Website</a>";}
					  if ($aim!="") {echo "<br /><a href='aim:goim?screenname=$aim'>AIM</a> (<a href='aim:addbuddy?screenname=$aim'>Add</a>)";}
					  if ($yim!="") {echo "<br /><a href='http://webmessenger.yahoo.com/?im=$yim'>Yahoo</a>";}
					  if ($msn!="") {echo "<br /><a href='msnim:chat?contact=$msn'>MSN</a> (<a href='msnim:add?contact=$msn'>Add</a>)";}
					  if ($icq!="") {echo "<br /><a href='http://www.icq.com/people/cmd.php?uin=$icq&action=message'>ICQ</a> (<a href='http://www.icq.com/people/cmd.php?uin=$icq&action=add'>Add</a>)";}
					  if (($facebook!="")||($twitter!=""))
					  {
						  echo "<br />";
						  if ($facebook!="") {echo "<a href='$facebook'>Facebook</a>";}
						  if (($facebook!="")&&($twitter!="")) {echo "/";}
						  if ($twitter!="") {echo "<a href='$twitter'>Twitter</a>";}
			  		  }
				  }
		      		  echo "</td>";
			  }
			  else {echo "<span style='font-size:1.5em;'>CPU Controlled</span><br /><a href='$lgpath/teams/team_$tid.html'>$tfull</a><br />";}
			  if ($tcnt==2) {echo "</tr>\n";$tcnt=-1;}
		 	  $prevDiv=$div;
			  $prevSl=$sl;
		 	  $tcnt++;
		  }
	    	  echo "     </table>\n";
		  echo "    </div>\n";
	    	  echo "   </td></tr></table>\n";
		  echo "   </div>\n";
      	  }
  }
  
  ##### Show Managers #####
  if ($gmCnt>0)
  {
	  $gmCnt=ceil($gmCnt/2);
       	  echo "<div class='textbox'>\n";
	  echo " <table cellpadding=0 cellspacing=0 border=0 width='935px'>\n";
       	  echo "  <tr class='title'><td colspan=3 style='padding:2px;'>Unaffiliated Users</td></tr>\n";
	  echo "  <tr><td valign='top'>\n";
       	  $cnt=0;
	  foreach ($managers as $tid => $val)
	  {
		  $lid=$managers[$tid]['league_id'];
		  if ($lid>0) {continue;}

		  $hmid=$tid/10000;

		  $cnt++;
		  if ($cnt>$gmCnt) {echo "  </td><td width='10px'>&nbsp;</td><td valign='top'>\n";$cnt=0;}
		  echo "    <table cellpadding=2 cellspacing=0 border=0 width='445px' style='border:1px solid black;margin:10px 10px 0 10px;'>\n";
	    	  echo "     <tr><td><a href='./manager.php?human_manager_id=$hmid'style='font-size:1.5em;'>".$managers[$tid]['name']."</a>";
		  $hide_info=$managers[$tid]['hide_info'];
	    	  if (($hide_info==0)||(isset($_SESSION['user_id'])))
		  {
			  $forum_name=$managers[$tid]['fname'];
			  $pm_link=$managers[$tid]['flink'];
		 	  if (($forum_name=="")&&($pm_link!="")) {$forum_name="PM";}
			  if ($pm_link!="") {echo " (<a href='$pm_link'>$forum_name</a>)";}
	   	  }
		  if ($managers[$tid]['is_commish']==1) {echo " (Commissioner)";}
	    	  if (($hide_info==0)||(isset($_SESSION['user_id'])))
		  {
			  if ($row['email']!="")
			  {
		      		  $e=explode(";",$managers[$tid]['email']);
				  echo "     <tr><td>Email: ";
		      		  for ($idx=0;$idx<count($e);$idx++) {if ($idx>0) {echo ", ";} echo "<a href='mailto:".$e[$idx]."'>".$e[$idx]."</a>";}
				  echo "</td></tr>\n";
			  }
			  if ($managers[$tid]['extlink']!="") {echo "     <tr><td><a href='".$managers[$tid]['extlink']."'>Website</a></td></tr>\n";}
			  $aim=$managers[$tid]['aim'];
			  $yim=$managers[$tid]['yim'];
		 	  $msn=$managers[$tid]['msn'];
			  $icq=$managers[$tid]['icq'];
		 	  $facebook=$managers[$tid]['facebook'];
			  $twitter=$managers[$tid]['twitter'];
		 	  if ($aim!="") {echo "     <tr><td><a href='aim:goim?screenname=$aim'>AIM</a> (<a href='aim:addbuddy?screenname=$aim'>Add</a>)</td></tr>\n";}
			  if ($yim!="") {echo "     <tr><td><a href='http://webmessenger.yahoo.com/?im=$yim'>Yahoo</a></td></tr>\n";}
			  if ($msn!="") {echo "     <tr><td><a href='msnim:chat?contact=$msn'>MSN</a> (<a href='msnim:add?contact=$msn'>Add</a>)</td></tr>\n";}
			  if ($icq!="") {echo "     <tr><td><a href='http://www.icq.com/people/cmd.php?uin=$icq&action=message'>ICQ</a> (<a href='http://www.icq.com/people/cmd.php?uin=$icq&action=add'>Add</a>)</td></tr>\n";}
		 	  if (($facebook!="")||($twitter!=""))
			  {
		      		  echo "     <tr><td>";
				  if ($facebook!="") {echo "<a href='$facebook'>Facebook</a>";}
		      		  if (($facebook!="")&&($twitter!="")) {echo "/";}
				  if ($twitter!="") {echo "<a href='$twitter'>Twitter</a>";}
		      		  echo "</td></tr>\n";
			  }
	   	  }
		  else {echo "     <tr><td rowspan=3></td></tr>\n";}
	    	  echo "     </td></tr>\n";
		  echo "    </table><br/>\n";
      	  }
	  echo "  </td></tr>";
       	  echo " </table>\n";
	  echo "</div>\n";
  }

  mysqli_close($db);

  ################## END OF PAGE #########################
  echo " </div>   <!-- END contentpane DIV -->\n";
  page_foot($_SESSION['lgpath']);


?>
