Add ability to display player statistics by category

This commit is contained in:
Brian Miyaji
2016-03-31 01:52:21 +11:00
parent 29b4460040
commit 3547303c6f
9 changed files with 317 additions and 130 deletions

View File

@@ -0,0 +1,42 @@
<?php
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
// Skip if there are no rows in the table
if ( empty( $data ) )
return;
$output = '<h4 class="sp-table-caption">' . $league->name . '</h4>' .
'<div class="sp-table-wrapper">' .
'<table class="sp-player-statistics sp-data-table' . ( $scrollable ? ' sp-scrollable-table' : '' ) . '">' . '<thead>' . '<tr>';
foreach( $labels as $key => $label ):
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
$i = 0;
foreach( $data as $season_id => $row ):
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
foreach( $labels as $key => $value ):
$output .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>';
endforeach;
$output .= '</tr>';
$i++;
endforeach;
$output .= '</tbody>' . '</table>' . '</div>';
?>
<div class="sp-template sp-template-player-statistics">
<?php echo $output; ?>
</div>

View File

@@ -0,0 +1,46 @@
<?php
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
// Skip if there are no rows in the table
if ( empty( $data ) )
return false;
$output = '<h4 class="sp-table-caption">' . __( 'Career Total', 'sportspress' ) . '</h4>' .
'<div class="sp-table-wrapper">' .
'<table class="sp-player-statistics sp-data-table' . ( $scrollable ? ' sp-scrollable-table' : '' ) . '">' . '<thead>' . '<tr>';
foreach( $labels as $key => $label ):
if ( 'team' == $key )
continue;
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
$i = 0;
foreach( $data as $season_id => $row ):
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
foreach( $labels as $key => $value ):
if ( 'team' == $key )
continue;
$output .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>';
endforeach;
$output .= '</tr>';
$i++;
endforeach;
$output .= '</tbody>' . '</table>' . '</div>';
?>
<div class="sp-template sp-template-player-statistics sp-template-player-total">
<?php echo $output; ?>
</div>

View File

@@ -16,103 +16,42 @@ if ( ! isset( $id ) )
$player = new SP_Player( $id );
$scrollable = get_option( 'sportspress_enable_scrollable_tables', 'yes' ) == 'yes' ? true : false;
$sections = get_option( 'sportspress_player_performance_sections', -1 );
$leagues = get_the_terms( $id, 'sp_league' );
$positions = $player->positions();
$player_sections = array();
foreach ( $positions as $position ) {
$player_sections = array_merge( $player_sections, sp_get_term_sections( $position->term_id ) );
}
// Determine order of sections
if ( 1 == $sections ) {
$section_order = array( 1 => __( 'Defense', 'sportspress' ), 0 => __( 'Offense', 'sportspress' ) );
} elseif ( 0 == $sections ) {
$section_order = array( __( 'Offense', 'sportspress' ), __( 'Defense', 'sportspress' ) );
} else {
$section_order = array( -1 => null );
}
// Loop through statistics for each league
if ( is_array( $leagues ) ):
foreach ( $leagues as $league ):
$data = $player->data( $league->term_id );
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
// Skip if there are no rows in the table
if ( empty( $data ) )
continue;
$output = '<h4 class="sp-table-caption">' . $league->name . '</h4>' .
'<div class="sp-table-wrapper">' .
'<table class="sp-player-statistics sp-data-table' . ( $scrollable ? ' sp-scrollable-table' : '' ) . '">' . '<thead>' . '<tr>';
foreach( $labels as $key => $label ):
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
foreach ( $section_order as $section_id => $section_label ) {
if ( -1 !== $section_id && ! in_array( $section_id, $player_sections ) ) continue;
foreach ( $leagues as $league ):
if ( null !== $section_label ) {
printf( '<h3 class="sp-post-caption sp-player-statistics-section">%s</h3>', $section_label );
}
sp_get_template( 'player-statistics-league.php', array(
'data' => $player->data( $league->term_id, false, $section_id ),
'league' => $league,
'scrollable' => $scrollable,
) );
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
$i = 0;
foreach( $data as $season_id => $row ):
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
foreach( $labels as $key => $value ):
$output .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>';
endforeach;
$output .= '</tr>';
$i++;
endforeach;
$output .= '</tbody>' . '</table>' . '</div>';
?>
<div class="sp-template sp-template-player-statistics">
<?php echo $output; ?>
</div>
<?php
endforeach;
endif;
// Career total
$data = $player->data( 0 );
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
// Skip if there are no rows in the table
if ( empty( $data ) )
return false;
$output = '<h4 class="sp-table-caption">' . __( 'Career Total', 'sportspress' ) . '</h4>' .
'<div class="sp-table-wrapper">' .
'<table class="sp-player-statistics sp-data-table' . ( $scrollable ? ' sp-scrollable-table' : '' ) . '">' . '<thead>' . '<tr>';
foreach( $labels as $key => $label ):
if ( 'team' == $key )
continue;
$output .= '<th class="data-' . $key . '">' . $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
$i = 0;
foreach( $data as $season_id => $row ):
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
foreach( $labels as $key => $value ):
if ( 'team' == $key )
continue;
$output .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>';
endforeach;
$output .= '</tr>';
$i++;
endforeach;
$output .= '</tbody>' . '</table>' . '</div>';
?>
<div class="sp-template sp-template-player-statistics sp-template-player-total">
<?php echo $output; ?>
</div>
sp_get_template( 'player-statistics-total.php', array(
'data' => $player->data( 0, false, $section_id ),
'scrollable' => $scrollable,
) );
}
endif;