Add offense and defense option to player performance
This commit is contained in:
@@ -13,7 +13,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
$totals = array();
|
||||
|
||||
// Set null
|
||||
if ( ! isset( $position ) ) $position = null;
|
||||
if ( ! isset( $section ) ) $section = null;
|
||||
if ( ! isset( $class ) ) $class = null;
|
||||
|
||||
// Initialize arrays
|
||||
@@ -33,15 +33,14 @@ if ( ! isset( $subs ) ) $subs = array();
|
||||
<th class="data-number">#</th>
|
||||
<?php } ?>
|
||||
<th class="data-name">
|
||||
<?php if ( isset( $position ) ) { ?>
|
||||
<?php echo $position; ?>
|
||||
<?php if ( isset( $section ) ) { ?>
|
||||
<?php echo $section; ?>
|
||||
<?php } else { ?>
|
||||
<?php _e( 'Player', 'sportspress' ); ?>
|
||||
<?php } ?>
|
||||
</th>
|
||||
<?php endif; ?>
|
||||
<?php if ( $mode == 'values' ): foreach ( $labels as $key => $label ): ?>
|
||||
<?php if ( isset( $position ) && 'position' == $key ) continue; ?>
|
||||
<th class="data-<?php echo $key; ?>"><?php echo $label; ?></th>
|
||||
<?php endforeach; else: ?>
|
||||
<th class="sp-performance-icons"> </th>
|
||||
@@ -102,8 +101,9 @@ if ( ! isset( $subs ) ) $subs = array();
|
||||
foreach ( $labels as $key => $label ):
|
||||
if ( 'name' == $key )
|
||||
continue;
|
||||
if ( isset( $position ) && 'position' == $key )
|
||||
continue;
|
||||
|
||||
$format = sp_array_value( $formats, $key, 'number' );
|
||||
$placeholder = sp_get_format_placeholder( $format );
|
||||
|
||||
$value = '—';
|
||||
if ( $key == 'position' ):
|
||||
@@ -118,6 +118,8 @@ if ( ! isset( $subs ) ) $subs = array();
|
||||
$player_position = get_term_by( 'id', $position_id, 'sp_position' );
|
||||
if ( $player_position ) $positions[] = $player_position->name;
|
||||
}
|
||||
|
||||
$positions = array_unique( $positions );
|
||||
|
||||
if ( sizeof( $positions ) ):
|
||||
$value = implode( ', ', $positions );
|
||||
@@ -126,13 +128,16 @@ if ( ! isset( $subs ) ) $subs = array();
|
||||
if ( array_key_exists( $key, $row ) && $row[ $key ] != '' ):
|
||||
$value = $row[ $key ];
|
||||
else:
|
||||
$value = 0;
|
||||
$value = $placeholder;
|
||||
endif;
|
||||
endif;
|
||||
if ( ! array_key_exists( $key, $totals ) ):
|
||||
$totals[ $key ] = 0;
|
||||
$totals[ $key ] = $placeholder;
|
||||
endif;
|
||||
$totals[ $key ] += $value;
|
||||
|
||||
if ( 'text' !== $format ) {
|
||||
$totals[ $key ] += $value;
|
||||
}
|
||||
|
||||
if ( $mode == 'values' ):
|
||||
echo '<td class="data-' . $key . '">' . $value . '</td>';
|
||||
@@ -159,7 +164,7 @@ if ( ! isset( $subs ) ) $subs = array();
|
||||
<?php if ( apply_filters( 'sportspress_event_performance_show_footer', $show_total ) ): ?>
|
||||
<<?php echo ( $show_players ? 'tfoot' : 'tbody' ); ?>>
|
||||
<?php
|
||||
do_action( 'sportspress_event_performance_table_footer', $data, $labels, $position, $performance_ids );
|
||||
do_action( 'sportspress_event_performance_table_footer', $data, $labels, $section, $performance_ids );
|
||||
if ( $show_total && ( ! $primary || sizeof( array_intersect_key( $totals, array_flip( (array) $primary ) ) ) ) ) {
|
||||
?>
|
||||
<tr class="sp-total-row <?php echo ( $i % 2 == 0 ? 'odd' : 'even' ); ?>">
|
||||
@@ -178,8 +183,6 @@ if ( ! isset( $subs ) ) $subs = array();
|
||||
foreach ( $labels as $key => $label ):
|
||||
if ( 'name' == $key )
|
||||
continue;
|
||||
if ( isset( $position ) && 'position' == $key )
|
||||
continue;
|
||||
if ( $key == 'position' ):
|
||||
$value = ' ';
|
||||
else:
|
||||
@@ -212,6 +215,6 @@ if ( ! isset( $subs ) ) $subs = array();
|
||||
<?php endif; ?>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<?php do_action( 'sportspress_after_event_performance_table', $data, $lineups, $subs, $class ); ?>
|
||||
</div>
|
||||
|
||||
<?php do_action( 'sportspress_after_event_performance_table', $data, $lineups, $subs, $class ); ?>
|
||||
@@ -13,8 +13,7 @@ $show_players = get_option( 'sportspress_event_show_players', 'yes' ) === 'yes'
|
||||
$show_staff = get_option( 'sportspress_event_show_staff', 'yes' ) === 'yes' ? true : false;
|
||||
$show_total = get_option( 'sportspress_event_show_total', 'yes' ) === 'yes' ? true : false;
|
||||
$show_numbers = get_option( 'sportspress_event_show_player_numbers', 'yes' ) === 'yes' ? true : false;
|
||||
$split_positions = get_option( 'sportspress_event_split_players_by_position', 'no' ) === 'yes' ? true : false;
|
||||
$split_teams = get_option( 'sportspress_event_split_players_by_team', 'yes' ) === 'yes' ? true : false;
|
||||
$sections = get_option( 'sportspress_event_performance_sections', -1 );
|
||||
$reverse_teams = get_option( 'sportspress_event_performance_reverse_teams', 'no' ) === 'yes' ? true : false;
|
||||
$primary = sp_get_main_performance_option();
|
||||
$total = get_option( 'sportspress_event_total_performance', 'all');
|
||||
@@ -28,7 +27,7 @@ $teams = get_post_meta( $id, 'sp_team', false );
|
||||
|
||||
if ( is_array( $teams ) ):
|
||||
?>
|
||||
<div class="sp-event-performance-tables sp-event-performance-<?php echo $split_positions ? 'positions' : 'teams'; ?>">
|
||||
<div class="sp-event-performance-tables sp-event-performance-teams">
|
||||
<?php
|
||||
|
||||
$event = new SP_Event( $id );
|
||||
@@ -61,195 +60,155 @@ if ( is_array( $teams ) ):
|
||||
if ( $reverse_teams ) {
|
||||
$teams = array_reverse( $teams, true );
|
||||
}
|
||||
|
||||
// Get performance columns
|
||||
$args = array(
|
||||
'post_type' => 'sp_performance',
|
||||
'numberposts' => 100,
|
||||
'posts_per_page' => 100,
|
||||
'orderby' => 'menu_order',
|
||||
'order' => 'ASC',
|
||||
);
|
||||
|
||||
if ( $split_teams ) {
|
||||
// Split tables
|
||||
foreach( $teams as $index => $team_id ):
|
||||
if ( -1 == $team_id ) continue;
|
||||
$columns = get_posts( $args );
|
||||
|
||||
// Get formats
|
||||
$formats = array();
|
||||
|
||||
// Add to formats
|
||||
foreach ( $columns as $column ) {
|
||||
$format = get_post_meta( $column->ID, 'sp_format', true );
|
||||
if ( '' === $format ) {
|
||||
$format = 'number';
|
||||
}
|
||||
$formats[ $column->post_name ] = $format;
|
||||
}
|
||||
|
||||
// Get results for players in the team
|
||||
$players = sp_array_between( (array)get_post_meta( $id, 'sp_player', false ), 0, $index );
|
||||
$has_players = sizeof( $players ) > 1;
|
||||
// Prepare for offense and defense sections
|
||||
if ( -1 != $sections ) {
|
||||
|
||||
// Determine order of sections
|
||||
if ( 1 == $sections ) {
|
||||
$section_order = array( __( 'Defense', 'sportspress' ), __( 'Offense', 'sportspress' ) );
|
||||
} else {
|
||||
$section_order = array( __( 'Offense', 'sportspress' ), __( 'Defense', 'sportspress' ) );
|
||||
}
|
||||
|
||||
// Initialize labels
|
||||
$labels = array( array(), array() );
|
||||
|
||||
// Add positions if applicable
|
||||
if ( 'yes' == get_option( 'sportspress_event_show_position', 'yes' ) ) {
|
||||
$labels[0]['position'] = $labels[1]['position'] = __( 'Position', 'sportspress' );
|
||||
}
|
||||
|
||||
$players = apply_filters( 'sportspress_event_performance_split_team_players', $players );
|
||||
// Get labels by section
|
||||
foreach ( $columns as $column ):
|
||||
$section = get_post_meta( $column->ID, 'sp_section', true );
|
||||
if ( '' === $section ) {
|
||||
$section = -1;
|
||||
}
|
||||
switch ( $section ):
|
||||
case 1:
|
||||
$labels[1][ $column->post_name ] = $column->post_title;
|
||||
break;
|
||||
case 0:
|
||||
$labels[0][ $column->post_name ] = $column->post_title;
|
||||
break;
|
||||
default:
|
||||
$labels[0][ $column->post_name ] = $column->post_title;
|
||||
$labels[1][ $column->post_name ] = $column->post_title;
|
||||
endswitch;
|
||||
endforeach;
|
||||
}
|
||||
|
||||
$show_team_players = $show_players && $has_players;
|
||||
foreach( $teams as $index => $team_id ):
|
||||
if ( -1 == $team_id ) continue;
|
||||
|
||||
if ( 0 < $team_id ) {
|
||||
$data = sp_array_combine( $players, sp_array_value( $performance, $team_id, array() ) );
|
||||
} elseif ( 0 == $team_id ) {
|
||||
// Get results for players in the team
|
||||
$players = sp_array_between( (array)get_post_meta( $id, 'sp_player', false ), 0, $index );
|
||||
$has_players = sizeof( $players ) > 1;
|
||||
|
||||
$players = apply_filters( 'sportspress_event_performance_split_team_players', $players );
|
||||
|
||||
$show_team_players = $show_players && $has_players;
|
||||
|
||||
if ( ! $show_team_players && ! $show_staff && ! $show_total ) continue;
|
||||
|
||||
if ( $show_team_players || $show_total ) {
|
||||
if ( -1 != $sections ) {
|
||||
|
||||
$data = array();
|
||||
foreach ( $players as $player_id ) {
|
||||
if ( isset( $performance[ $player_id ][ $player_id ] ) ) {
|
||||
$data[ $player_id ] = $performance[ $player_id ][ $player_id ];
|
||||
|
||||
// Get results for offensive players in the team
|
||||
$offense = sp_array_between( (array)get_post_meta( $id, 'sp_offense', false ), 0, $index );
|
||||
$data[0] = sp_array_combine( $offense, sp_array_value( $performance, $team_id, array() ) );
|
||||
|
||||
// Get results for defensive players in the team
|
||||
$defense = sp_array_between( (array)get_post_meta( $id, 'sp_defense', false ), 0, $index );
|
||||
$data[1] = sp_array_combine( $defense, sp_array_value( $performance, $team_id, array() ) );
|
||||
|
||||
foreach ( $section_order as $section_id => $section_label ) {
|
||||
if ( sizeof( $data[ $section_id ] ) ) {
|
||||
sp_get_template( 'event-performance-table.php', array(
|
||||
'section' => $section_label,
|
||||
'scrollable' => $scrollable,
|
||||
'sortable' => $sortable,
|
||||
'show_players' => $show_team_players,
|
||||
'show_numbers' => $show_numbers,
|
||||
'show_total' => $show_total,
|
||||
'caption' => 0 == $section_id && $team_id ? get_the_title( $team_id ) : null,
|
||||
'labels' => $labels[ $section_id ],
|
||||
'formats' => $formats,
|
||||
'mode' => $mode,
|
||||
'data' => $data[ $section_id ],
|
||||
'event' => $event,
|
||||
'link_posts' => $link_posts,
|
||||
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null,
|
||||
'primary' => 'primary' == $total ? $primary : null,
|
||||
'class' => 'sp-template-event-performance-team-' . $index . '-section-' . $section_id,
|
||||
) );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$data = sp_array_value( array_values( $performance ), $index );
|
||||
}
|
||||
|
||||
if ( ! $show_team_players && ! $show_staff && ! $show_total ) continue;
|
||||
|
||||
if ( $show_team_players || $show_total ) {
|
||||
if ( $split_positions ) {
|
||||
$positions = get_terms( 'sp_position', array(
|
||||
'orderby' => 'slug',
|
||||
'hide_empty' => 0,
|
||||
) );
|
||||
|
||||
foreach ( $positions as $position_index => $position ) {
|
||||
$subdata = array();
|
||||
foreach ( $data as $player_id => $player ) {
|
||||
$player_positions = (array) sp_array_value( $player, 'position' );
|
||||
$player_positions = array_filter( $player_positions );
|
||||
if ( empty( $player_positions ) ) {
|
||||
$player_positions = (array) sp_get_the_term_id( $player_id, 'sp_position' );
|
||||
}
|
||||
if ( in_array( $position->term_id, $player_positions ) ) {
|
||||
$subdata[ $player_id ] = $data[ $player_id ];
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize Sublabels
|
||||
$sublabels = $labels;
|
||||
|
||||
// Get performance with position
|
||||
$performance_labels = get_posts( array(
|
||||
'post_type' => 'sp_performance',
|
||||
'posts_per_page' => -1,
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'sp_position',
|
||||
'terms' => $position->term_id,
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$allowed_labels = array();
|
||||
if ( ! empty( $performance_labels ) ) {
|
||||
foreach ( $performance_labels as $label ) {
|
||||
$allowed_labels[ $label->post_name ] = $label->post_title;
|
||||
}
|
||||
|
||||
$allowed_labels = apply_filters( 'sportspress_event_performance_allowed_labels', $allowed_labels, $position_index );
|
||||
|
||||
$sublabels = array_intersect_key( $sublabels, $allowed_labels );
|
||||
}
|
||||
|
||||
if ( sizeof( $subdata ) ) {
|
||||
$subdata = apply_filters( 'sportspress_event_performance_split_team_split_position_subdata', $subdata, $data, $position_index );
|
||||
|
||||
sp_get_template( 'event-performance-table.php', array(
|
||||
'position' => sp_get_position_caption( $position->term_id ),
|
||||
'scrollable' => $scrollable,
|
||||
'sortable' => $sortable,
|
||||
'show_players' => $show_team_players,
|
||||
'show_numbers' => $show_numbers,
|
||||
'show_total' => $show_total,
|
||||
'caption' => 0 == $position_index && $team_id ? get_the_title( $team_id ) : null,
|
||||
'labels' => $sublabels,
|
||||
'mode' => $mode,
|
||||
'data' => $subdata,
|
||||
'event' => $event,
|
||||
'link_posts' => $link_posts,
|
||||
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null,
|
||||
'primary' => 'primary' == $total ? $primary : null,
|
||||
'class' => 'sp-template-event-performance-team-' . $index . '-position-' . $position_index,
|
||||
) );
|
||||
if ( 0 < $team_id ) {
|
||||
$data = sp_array_combine( $players, sp_array_value( $performance, $team_id, array() ) );
|
||||
} elseif ( 0 == $team_id ) {
|
||||
$data = array();
|
||||
foreach ( $players as $player_id ) {
|
||||
if ( isset( $performance[ $player_id ][ $player_id ] ) ) {
|
||||
$data[ $player_id ] = $performance[ $player_id ][ $player_id ];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sp_get_template( 'event-performance-table.php', array(
|
||||
'scrollable' => $scrollable,
|
||||
'sortable' => $sortable,
|
||||
'show_players' => $show_team_players,
|
||||
'show_numbers' => $show_numbers,
|
||||
'show_total' => $show_total,
|
||||
'caption' => $team_id ? get_the_title( $team_id ) : null,
|
||||
'labels' => $labels,
|
||||
'mode' => $mode,
|
||||
'data' => $data,
|
||||
'event' => $event,
|
||||
'link_posts' => $link_posts,
|
||||
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null,
|
||||
'primary' => 'primary' == $total ? $primary : null,
|
||||
|
||||
) );
|
||||
}
|
||||
}
|
||||
if ( $show_staff ):
|
||||
sp_get_template( 'event-staff.php', array( 'id' => $id, 'index' => $index ) );
|
||||
endif;
|
||||
?>
|
||||
<?php
|
||||
endforeach;
|
||||
} else {
|
||||
// Combined table
|
||||
$data = array();
|
||||
foreach ( $performance as $players ) {
|
||||
foreach ( $players as $player_id => $player ) {
|
||||
if ( $player_id == 0 ) continue;
|
||||
$data[ $player_id ] = $player;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $split_positions ) {
|
||||
$positions = get_terms( 'sp_position', array(
|
||||
'orderby' => 'slug',
|
||||
'hide_empty' => 0,
|
||||
) );
|
||||
|
||||
foreach ( $positions as $position_index => $position ) {
|
||||
$subdata = array();
|
||||
foreach ( $data as $player_id => $player ) {
|
||||
$player_positions = (array) sp_array_value( $player, 'position' );
|
||||
$player_positions = array_filter( $player_positions );
|
||||
if ( empty( $player_positions ) ) {
|
||||
$player_positions = (array) sp_get_the_term_id( $player_id, 'sp_position' );
|
||||
}
|
||||
if ( in_array( $position->term_id, $player_positions ) ) {
|
||||
$subdata[ $player_id ] = $data[ $player_id ];
|
||||
}
|
||||
$data = sp_array_value( array_values( $performance ), $index );
|
||||
}
|
||||
|
||||
$subdata = apply_filters( 'sportspress_event_performance_split_position_subdata', $subdata, $data, $position_index );
|
||||
sp_get_template( 'event-performance-table.php', array(
|
||||
'scrollable' => $scrollable,
|
||||
'sortable' => $sortable,
|
||||
'show_players' => $show_team_players,
|
||||
'show_numbers' => $show_numbers,
|
||||
'show_total' => $show_total,
|
||||
'caption' => $team_id ? get_the_title( $team_id ) : null,
|
||||
'labels' => $labels,
|
||||
'formats' => $formats,
|
||||
'mode' => $mode,
|
||||
'data' => $data,
|
||||
'event' => $event,
|
||||
'link_posts' => $link_posts,
|
||||
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null,
|
||||
'primary' => 'primary' == $total ? $primary : null,
|
||||
|
||||
if ( sizeof( $subdata ) ) {
|
||||
sp_get_template( 'event-performance-table-combined.php', array(
|
||||
'scrollable' => $scrollable,
|
||||
'sortable' => $sortable,
|
||||
'show_players' => $show_players,
|
||||
'show_numbers' => $show_numbers,
|
||||
'show_total' => $show_total,
|
||||
'caption' => sp_get_position_caption( $position->term_id ),
|
||||
'labels' => $labels,
|
||||
'mode' => $mode,
|
||||
'data' => $subdata,
|
||||
'event' => $event,
|
||||
'link_posts' => $link_posts,
|
||||
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null,
|
||||
'primary' => 'primary' == $total ? $primary : null,
|
||||
) );
|
||||
}
|
||||
) );
|
||||
}
|
||||
} else {
|
||||
sp_get_template( 'event-performance-table-combined.php', array(
|
||||
'scrollable' => $scrollable,
|
||||
'sortable' => $sortable,
|
||||
'show_players' => $show_players,
|
||||
'show_numbers' => $show_numbers,
|
||||
'show_total' => $show_total,
|
||||
'caption' => __( 'Scorecard', 'sportspress' ),
|
||||
'labels' => $labels,
|
||||
'mode' => $mode,
|
||||
'data' => $data,
|
||||
'event' => $event,
|
||||
'link_posts' => $link_posts,
|
||||
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null,
|
||||
'primary' => 'primary' == $total ? $primary : null,
|
||||
) );
|
||||
}
|
||||
}
|
||||
if ( $show_staff ):
|
||||
sp_get_template( 'event-staff.php', array( 'id' => $id, 'index' => $index ) );
|
||||
endif;
|
||||
?>
|
||||
<?php
|
||||
endforeach;
|
||||
|
||||
do_action( 'sportspress_event_performance' );
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user