<?php
/*
Handy functions for the display of standings
*/

function sls_team_schedule ($teamtosort, $team_game_filter = 0) {

/*
Call this function to show the schedule for all teams or a specific team, for upcoming games, past scores, or all games
The first parameter specifies the team ID, which is 0 to show all teams
The second parameter specifies the filter to apply to the games: 0 or blank means all games, 1 means upcoming games, 2 means past scores

Examples:
sls_team_schedule(0); will show all games for all teams
sls_team_schedule(4, 2); will show past scores for the team with an ID of 4
sls_team_schedule(0, 1); will show upcoming games for all teams
*/
    global $show_fields;
    
    $teamtosort = abs(intval($teamtosort));

    // Specified a team
    if ($teamtosort > 0) {
        // Does this team exist?
        $team_exist_query = 'SELECT COUNT(*) FROM sportsdb_teams WHERE teamid = ' . $teamtosort;
        $team_exist_check = mysql_query($team_exist_query);
            if (mysql_result($team_exist_check, 0) == 0) {
                die ('No such team exists');
        }
    
        // MySQL code to select a specific team
        $team_specific_sort = 'AND (sportsdb_wins.winner = ' . $teamtosort  . ' OR sportsdb_wins.loser = ' . $teamtosort . ') ';
    }

    // Show upcoming games only
    if ($team_game_filter == 1) {
        $score_sort = 'ASC';
        $score_game_filter = 'AND sportsdb_wins.winortie = 3 ';
    }

    // Show past scores only
    if ($team_game_filter == 2) {
        $score_sort = 'DESC';
        $score_game_filter = 'AND sportsdb_wins.winortie != 3 ';
    }
  if ($result_exist) {
    $query = "SELECT teamid, teamname, nickname, teamwins, teamties, teamlosses, teamforfeits, teamrf, teamra, 
    (teamwins + teamties + teamlosses + teamforfeits) AS gamesplayed, 
    (teamwins * $points_win + teamforfeits * $points_forfeit + teamties * $points_tie) AS points, 
    $winning_pct_formula AS winningpct, 
    (teamwins - teamlosses - teamforfeits) AS leadervalue 
    FROM sportsdb_teams 
    WHERE teamdiv = {$divs['divid']} 
    ORDER BY leadervalue DESC, teamties DESC LIMIT 1";
    
    $result = mysql_query($query) or die ("Error in query: $query");
    $results = mysql_fetch_array($result, MYSQL_ASSOC);
    $winningpct = number_format($results['winningpct'], 0);
}  
?>
<table class="scorestable">
    <tr>
        
        <th>&nbsp;Logo</th>
        <th>&nbsp;&nbsp;Domácí</th>
         <?php if ($show_fields) { ?>
        <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
        
<?php } ?>
<?php if ($team_game_filter != 1) { ?>
        <th>Skóre</th> 
       <th>&nbsp;&nbsp;&nbsp;&nbsp;</th> <th>Hosté</th>
        <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Logo</th>
<?php } ?>
        <th>&nbsp;</th>
    </tr>
<?php
$query = "SELECT COUNT(*) FROM  sportsdb_teams WHERE teamdiv = {$divs['divid']}";
$result = mysql_query($query);
$result_exist = mysql_result($result,0);

if ($result_exist) {
    $query = "SELECT teamid, teamname, nickname, teamwins, teamties, teamlosses, teamforfeits, teamrf, teamra, 
    (teamwins + teamties + teamlosses + teamforfeits) AS gamesplayed, 
    (teamwins * $points_win + teamforfeits * $points_forfeit + teamties * $points_tie) AS points, 
    $winning_pct_formula AS winningpct, 
    (teamwins - teamlosses - teamforfeits) AS leadervalue 
    FROM sportsdb_teams 
    WHERE teamdiv = {$divs['divid']} 
    ORDER BY leadervalue DESC, teamties DESC LIMIT 1";
    
    $result = mysql_query($query) or die ("Error in query: $query");
    $results = mysql_fetch_array($result, MYSQL_ASSOC);
    $winningpct = number_format($results['winningpct'], 0);
}  
$queryscores="SELECT sportsdb_wins.winid, sportsdb_wins.windate, sportsdb_wins.wintime, 
            sportsdb_wins.rf, sportsdb_wins.ra, sportsdb_wins.wincomments, 
            sportsdb_wins.field, sportsdb_wins.winortie, 
            sportsdb_teams2.teamname AS winningteam, 
            sportsdb_teams.teamname AS losingteam 
            FROM sportsdb_wins, sportsdb_teams AS sportsdb_teams2, sportsdb_teams 
            WHERE sportsdb_teams2.teamid = sportsdb_wins.winner 
            AND sportsdb_teams.teamid = sportsdb_wins.loser 
            $team_specific_sort
            $score_game_filter
            ORDER BY wintime $score_sort";

$resultscores=mysql_query($queryscores);

while ($scores = mysql_fetch_array($resultscores, MYSQL_ASSOC)) {

    $windateheader=$scores['windate'];
    $windateformatted=date("F d, Y",$windateheader);
    $wintime=$scores['wintime'];
    $hour=date('g',$wintime);
    $minute=date('i',$wintime);
    $ampm=date('a',$wintime);   
?>  
    <tr>
        <td><a href="?conf=<?php print $confid; ?>&team=<?php print $results['teamid']; ?>" title="View scores for <?php print $results['teamname']; ?>"><em><?php print $results['teamname']; ?></em></a></td>
        <td><?php print "\t\t" . '<img src="http://www.michales.eu/img/' . $scores['winningteam'] .'.gif">' . "\n"; ?></td><td>&nbsp;<?php print $scores['winningteam']; ?></td>
     <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>   <td><?php print $scores['rf'] . ' - ' . $scores['ra'];
    if ($scores['winortie'] == 2 || $scores['winortie'] == 5) {
        print ' (ff)';
    }
    print '</td>';
?>         <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
        <td><?php print $scores['losingteam']; ?></td><td>&nbsp;&nbsp;&nbsp;&nbsp;<?php print "\t\t" . '<img src="http://www.michales.eu/img/' . $scores['losingteam'] .'.gif">' . "\n"; ?></td> 

<?php if ($show_fields) { ?>
        
<?php } ?>

        <td><?php print $scores['wincomments']; ?></td>
    </tr>
<?php } 
print '</table>';
}

function get_team_name ($teamid) {
    $team_name_query = 'SELECT teamname FROM sportsdb_teams WHERE teamid = ' . $teamid;
    $team_name_check = mysql_query($team_name_query);
        if (mysql_num_rows($team_name_check) == 0) {
            return 'No such team exists';
        }
        else {
            return mysql_result($team_name_check,0);
        }
} 
?>
