Enable displaying total rows in player statistics

This commit is contained in:
Brian Miyaji
2016-03-31 03:07:45 +11:00
parent 3547303c6f
commit d62683ee89
6 changed files with 51 additions and 84 deletions

View File

@@ -36,11 +36,6 @@ class SP_Meta_Box_Player_Statistics {
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $i == 0 ); self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $i == 0 );
$i ++; $i ++;
endforeach; endforeach;
?>
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?></strong></p>
<?php
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( 0, true );
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams );
} else { } else {
// Determine order of sections // Determine order of sections
if ( 1 == $sections ) { if ( 1 == $sections ) {
@@ -60,6 +55,7 @@ class SP_Meta_Box_Player_Statistics {
$columns = get_posts( $args ); $columns = get_posts( $args );
$s = 0;
foreach ( $section_order as $section_id => $section_label ) { foreach ( $section_order as $section_id => $section_label ) {
// Loop through statistics for each league // Loop through statistics for each league
$i = 0; $i = 0;
@@ -68,14 +64,10 @@ class SP_Meta_Box_Player_Statistics {
<p><strong><?php echo $league->name; ?> &mdash; <?php echo $section_label; ?></strong></p> <p><strong><?php echo $league->name; ?> &mdash; <?php echo $section_label; ?></strong></p>
<?php <?php
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( $league->term_id, true, $section_id ); list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( $league->term_id, true, $section_id );
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $i == 0 ); self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, $i == 0 && $s == 0 );
$i ++; $i ++;
endforeach; endforeach;
?> $s ++;
<p><strong><?php _e( 'Career Total', 'sportspress' ); ?> &mdash; <?php echo $section_label; ?></strong></p>
<?php
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( 0, true, $section_id );
self::table( $post->ID, 0, $columns, $data, $placeholders, $merged, $seasons_teams );
} }
} }
} }
@@ -126,24 +118,17 @@ class SP_Meta_Box_Player_Statistics {
?> ?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>"> <tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td> <td>
<?php if ( 0 !== $div_id ): ?> <label>
<label> <?php if ( ! apply_filters( 'sportspress_player_team_statistics', $league_id ) ): ?>
<?php if ( ! apply_filters( 'sportspress_player_team_statistics', $league_id ) ): ?> <?php $value = sp_array_value( $leagues, $div_id, '-1' ); ?>
<?php $value = sp_array_value( $leagues, $div_id, '-1' ); ?> <input type="hidden" name="sp_leagues[<?php echo $league_id; ?>][<?php echo $div_id; ?>]" value="-1">
<input type="hidden" name="sp_leagues[<?php echo $league_id; ?>][<?php echo $div_id; ?>]" value="-1"> <input type="checkbox" name="sp_leagues[<?php echo $league_id; ?>][<?php echo $div_id; ?>]" value="1" <?php checked( $value ); ?>>
<input type="checkbox" name="sp_leagues[<?php echo $league_id; ?>][<?php echo $div_id; ?>]" value="1" <?php checked( $value ); ?>> <?php endif; ?>
<?php endif; ?>
<?php
if ( 'WP_Error' == get_class( $div ) ) _e( 'Total', 'sportspress' );
else echo $div->name;
?>
</label>
<?php else: ?>
<?php <?php
if ( 'WP_Error' == get_class( $div ) ) _e( 'Total', 'sportspress' ); if ( 0 === $div_id ) _e( 'Total', 'sportspress' );
else echo $div->name; elseif ( 'WP_Error' != get_class( $div ) ) echo $div->name;
?> ?>
<?php endif; ?> </label>
</td> </td>
<?php if ( apply_filters( 'sportspress_player_team_statistics', $league_id ) ): ?> <?php if ( apply_filters( 'sportspress_player_team_statistics', $league_id ) ): ?>
<?php if ( $div_id == 0 ): ?> <?php if ( $div_id == 0 ): ?>

View File

@@ -73,6 +73,14 @@ class SP_Settings_Players extends SP_Settings_Page {
'id' => 'sportspress_player_show_statistics', 'id' => 'sportspress_player_show_statistics',
'default' => 'yes', 'default' => 'yes',
'type' => 'checkbox', 'type' => 'checkbox',
'checkboxgroup' => '',
),
array(
'desc' => __( 'Total', 'sportspress' ),
'id' => 'sportspress_player_show_total',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end', 'checkboxgroup' => 'end',
), ),

View File

@@ -539,10 +539,29 @@ class SP_Player extends SP_Custom_Post {
unset( $columns[ $key ] ); unset( $columns[ $key ] );
endif; endif;
endforeach; endforeach;
$labels = array( 'name' => __( 'Season', 'sportspress' ) );
if ( in_array( 'team', $this->columns ) ) $labels = array();
$labels['team'] = __( 'Team', 'sportspress' );
if ( 'no' === get_option( 'sportspress_player_show_statistics', 'yes' ) ) {
$merged = array();
} else {
$labels['name'] = __( 'Season', 'sportspress' );
if ( in_array( 'team', $this->columns ) ) {
$labels['team'] = __( 'Team', 'sportspress' );
}
}
if ( 'yes' === get_option( 'sportspress_player_show_total', 'no' ) ) {
$total_placeholders = sp_array_value( $placeholders, 0, array() );
$total_data = sp_array_value( $data, 0, array() );
$total_data = array_filter( $total_data );
$total = array_merge( $total_placeholders, $total_data );
$merged[-1] = $total;
$merged[-1]['name'] = __( 'Total', 'sportspress' );
}
$merged[0] = array_merge( $labels, $columns ); $merged[0] = array_merge( $labels, $columns );
return $merged; return $merged;
endif; endif;
} }

View File

@@ -9,7 +9,7 @@ unset( $data[0] );
if ( empty( $data ) ) if ( empty( $data ) )
return; return;
$output = '<h4 class="sp-table-caption">' . $league->name . '</h4>' . $output = '<h4 class="sp-table-caption">' . $caption . '</h4>' .
'<div class="sp-table-wrapper">' . '<div class="sp-table-wrapper">' .
'<table class="sp-player-statistics sp-data-table' . ( $scrollable ? ' sp-scrollable-table' : '' ) . '">' . '<thead>' . '<tr>'; '<table class="sp-player-statistics sp-data-table' . ( $scrollable ? ' sp-scrollable-table' : '' ) . '">' . '<thead>' . '<tr>';

View File

@@ -1,46 +0,0 @@
<?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

@@ -8,7 +8,7 @@
*/ */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( get_option( 'sportspress_player_show_statistics', 'yes' ) === 'no' ) return; if ( 'no' === get_option( 'sportspress_player_show_statistics', 'yes' ) && 'no' === get_option( 'sportspress_player_show_total', 'no' ) ) return;
if ( ! isset( $id ) ) if ( ! isset( $id ) )
$id = get_the_ID(); $id = get_the_ID();
@@ -40,18 +40,19 @@ if ( is_array( $leagues ) ):
foreach ( $leagues as $league ): foreach ( $leagues as $league ):
if ( null !== $section_label ) { if ( null !== $section_label ) {
printf( '<h3 class="sp-post-caption sp-player-statistics-section">%s</h3>', $section_label ); if ( sizeof( $leagues ) > 1 ) {
printf( '<h3 class="sp-post-caption sp-player-statistics-section">%s</h3>', $section_label );
$caption = $league->name;
} else {
$caption = $section_label;
}
} }
sp_get_template( 'player-statistics-league.php', array( sp_get_template( 'player-statistics-league.php', array(
'data' => $player->data( $league->term_id, false, $section_id ), 'data' => $player->data( $league->term_id, false, $section_id ),
'league' => $league, 'league' => $league,
'caption' => $caption,
'scrollable' => $scrollable, 'scrollable' => $scrollable,
) ); ) );
endforeach; endforeach;
sp_get_template( 'player-statistics-total.php', array(
'data' => $player->data( 0, false, $section_id ),
'scrollable' => $scrollable,
) );
} }
endif; endif;