Add combined table for individual mode

This commit is contained in:
Brian Miyaji
2016-02-24 14:29:23 +11:00
parent 8dc8ffa343
commit d7975798df
3 changed files with 280 additions and 212 deletions

View File

@@ -53,8 +53,11 @@ class SP_Meta_Box_Event_Performance {
// Apply filters to labels // Apply filters to labels
$labels = apply_filters( 'sportspress_event_performance_labels_admin', $labels ); $labels = apply_filters( 'sportspress_event_performance_labels_admin', $labels );
// Check if individual mode
$is_individual = get_option( 'sportspress_load_individual_mode_module', 'no' ) === 'yes' ? true : false;
self::tables( $post->ID, $stats, $labels, $columns, $teams, $has_checkboxes, $positions, $status, $formats, $order ); self::tables( $post->ID, $stats, $labels, $columns, $teams, $has_checkboxes, $positions, $status, $formats, $order, $is_individual );
} }
/** /**
@@ -69,96 +72,123 @@ class SP_Meta_Box_Event_Performance {
/** /**
* Admin edit tables * Admin edit tables
*/ */
public static function tables( $post_id, $stats = array(), $labels = array(), $columns = array(), $teams = array(), $has_checkboxes = false, $positions = array(), $status = true, $formats = array(), $order = array() ) { public static function tables( $post_id, $stats = array(), $labels = array(), $columns = array(), $teams = array(), $has_checkboxes = false, $positions = array(), $status = true, $formats = array(), $order = array(), $is_individual = false ) {
$sections = get_option( 'sportspress_event_performance_sections', -1 ); $sections = get_option( 'sportspress_event_performance_sections', -1 );
$i = 0; if ( $is_individual ) {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table sp-performance-table sp-sortable-table">
<?php self::header( $columns, $labels, $positions, $has_checkboxes, $status, false, false, -1, $formats ); ?>
<?php self::footer( sp_array_value( $stats, -1 ), $labels, 0, $positions, $status, false, false, -1, $formats ); ?>
<tbody>
<?php
foreach ( $teams as $key => $team_id ):
if ( -1 == $team_id ) continue;
foreach ( $teams as $key => $team_id ): // Get results for players in the team
if ( -1 == $team_id ) continue; $players = sp_array_between( (array)get_post_meta( $post_id, 'sp_player', false ), 0, $key );
$players[] = -1;
if ( -1 == $sections ) { $data = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) );
// Get results for players in the team
$players = sp_array_between( (array)get_post_meta( $post_id, 'sp_player', false ), 0, $key );
$players[] = -1;
$data = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) );
?>
<div>
<p><strong><?php echo get_the_title( $team_id ); ?></strong></p>
<?php self::table( $labels, $columns, $data, $team_id, $has_checkboxes, $positions, $status, -1, $formats, $order ); ?>
<?php do_action( 'sportspress_after_event_performance_table_admin', $labels, $columns, $data, $team_id ); ?>
</div>
<?php } else { ?>
<?php
// Get labels by section
$args = array(
'post_type' => 'sp_performance',
'numberposts' => 100,
'posts_per_page' => 100,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$columns = get_posts( $args ); foreach ( $data as $player_id => $player_performance ):
self::row( $labels, $player_id, $player_performance, $team_id, $data, ! empty( $positions ), $status, false, false, -1, $formats );
$labels = array( array(), array() ); endforeach;
foreach ( $columns as $column ): endforeach;
$section = get_post_meta( $column->ID, 'sp_section', true ); ?>
if ( '' === $section ) { </tbody>
$section = -1; </table>
} </div>
switch ( $section ): <?php
case 1: } else {
$labels[1][ $column->post_name ] = $column->post_title; $i = 0;
break;
case 0: foreach ( $teams as $key => $team_id ):
$labels[0][ $column->post_name ] = $column->post_title; if ( -1 == $team_id ) continue;
break;
default:
$labels[1][ $column->post_name ] = $column->post_title;
$labels[0][ $column->post_name ] = $column->post_title;
endswitch;
endforeach;
$offense = (array)get_post_meta( $post_id, 'sp_offense', false ); if ( -1 == $sections ) {
$defense = (array)get_post_meta( $post_id, 'sp_defense', false ); // Get results for players in the team
$data = array();
if ( sizeof( $offense ) || sizeof( $defense ) ) {
// Get results for offensive players in the team
$offense = sp_array_between( $offense, 0, $key );
$offense[] = -1;
$data[0] = sp_array_combine( $offense, sp_array_value( $stats, $team_id, array() ) );
// Get results for defensive players in the team
$defense = sp_array_between( $defense, 0, $key );
$defense[] = -1;
$data[1] = sp_array_combine( $defense, sp_array_value( $stats, $team_id, array() ) );
} else {
// Get results for all players in the team
$players = sp_array_between( (array)get_post_meta( $post_id, 'sp_player', false ), 0, $key ); $players = sp_array_between( (array)get_post_meta( $post_id, 'sp_player', false ), 0, $key );
$players[] = -1; $players[] = -1;
$data[0] = $data[1] = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) ); $data = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) );
}
// Determine order of sections
if ( 1 == $sections ) {
$section_order = array( 1 => __( 'Defense', 'sportspress' ), 0 => __( 'Offense', 'sportspress' ) );
} else {
$section_order = array( __( 'Offense', 'sportspress' ), __( 'Defense', 'sportspress' ) );
}
foreach ( $section_order as $section_id => $section_label ) {
?> ?>
<div> <div>
<p><strong><?php echo get_the_title( $team_id ); ?> &mdash; <?php echo $section_label; ?></strong></p> <p><strong><?php echo get_the_title( $team_id ); ?></strong></p>
<?php self::table( $labels[ $section_id ], $columns, $data[ $section_id ], $team_id, $has_checkboxes, $positions, $status, $section_id, $formats, $order ); ?> <?php self::table( $labels, $columns, $data, $team_id, $has_checkboxes, $positions, $status, -1, $formats, $order ); ?>
<?php do_action( 'sportspress_after_event_performance_table_admin', $labels[ $section_id ], $columns, $data[ $section_id ], $team_id ); ?> <?php do_action( 'sportspress_after_event_performance_table_admin', $labels, $columns, $data, $team_id ); ?>
</div> </div>
<?php } else { ?>
<?php <?php
// Get labels by section
$args = array(
'post_type' => 'sp_performance',
'numberposts' => 100,
'posts_per_page' => 100,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$columns = get_posts( $args );
$labels = array( array(), array() );
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[1][ $column->post_name ] = $column->post_title;
$labels[0][ $column->post_name ] = $column->post_title;
endswitch;
endforeach;
$offense = (array)get_post_meta( $post_id, 'sp_offense', false );
$defense = (array)get_post_meta( $post_id, 'sp_defense', false );
$data = array();
if ( sizeof( $offense ) || sizeof( $defense ) ) {
// Get results for offensive players in the team
$offense = sp_array_between( $offense, 0, $key );
$offense[] = -1;
$data[0] = sp_array_combine( $offense, sp_array_value( $stats, $team_id, array() ) );
// Get results for defensive players in the team
$defense = sp_array_between( $defense, 0, $key );
$defense[] = -1;
$data[1] = sp_array_combine( $defense, sp_array_value( $stats, $team_id, array() ) );
} else {
// Get results for all players in the team
$players = sp_array_between( (array)get_post_meta( $post_id, 'sp_player', false ), 0, $key );
$players[] = -1;
$data[0] = $data[1] = sp_array_combine( $players, sp_array_value( $stats, $team_id, array() ) );
}
// Determine order of sections
if ( 1 == $sections ) {
$section_order = array( 1 => __( 'Defense', 'sportspress' ), 0 => __( 'Offense', 'sportspress' ) );
} else {
$section_order = array( __( 'Offense', 'sportspress' ), __( 'Defense', 'sportspress' ) );
}
foreach ( $section_order as $section_id => $section_label ) {
?>
<div>
<p><strong><?php echo get_the_title( $team_id ); ?> &mdash; <?php echo $section_label; ?></strong></p>
<?php self::table( $labels[ $section_id ], $columns, $data[ $section_id ], $team_id, $has_checkboxes, $positions, $status, $section_id, $formats, $order ); ?>
<?php do_action( 'sportspress_after_event_performance_table_admin', $labels[ $section_id ], $columns, $data[ $section_id ], $team_id ); ?>
</div>
<?php
}
} }
} $i ++;
$i ++; endforeach;
endforeach; }
} }
/** /**

View File

@@ -74,6 +74,10 @@ if ( ! isset( $class ) ) $class = null;
foreach ( $labels as $key => $label ): foreach ( $labels as $key => $label ):
if ( in_array( $key, array( 'number', 'name' ) ) ) if ( in_array( $key, array( 'number', 'name' ) ) )
continue; continue;
$format = sp_array_value( $formats, $key, 'number' );
$placeholder = sp_get_format_placeholder( $format );
$value = '&mdash;'; $value = '&mdash;';
if ( $key == 'position' ): if ( $key == 'position' ):
if ( array_key_exists( $key, $row ) && $row[ $key ] != '' ): if ( array_key_exists( $key, $row ) && $row[ $key ] != '' ):
@@ -89,13 +93,16 @@ if ( ! isset( $class ) ) $class = null;
if ( array_key_exists( $key, $row ) && $row[ $key ] != '' ): if ( array_key_exists( $key, $row ) && $row[ $key ] != '' ):
$value = $row[ $key ]; $value = $row[ $key ];
else: else:
$value = 0; $value = $placeholder;
endif; endif;
endif; endif;
if ( ! array_key_exists( $key, $totals ) ): if ( ! array_key_exists( $key, $totals ) ):
$totals[ $key ] = 0; $totals[ $key ] = $placeholder;
endif; endif;
$totals[ $key ] += $value;
if ( 'text' !== $format ) {
$totals[ $key ] += $value;
}
if ( $mode == 'values' ): if ( $mode == 'values' ):
echo '<td class="data-' . $key . '">' . $value . '</td>'; echo '<td class="data-' . $key . '">' . $value . '</td>';

View File

@@ -9,6 +9,7 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$is_individual = get_option( 'sportspress_load_individual_mode_module', 'no' ) === 'yes' ? true : false;
$show_players = get_option( 'sportspress_event_show_players', 'yes' ) === 'yes' ? true : false; $show_players = get_option( 'sportspress_event_show_players', 'yes' ) === 'yes' ? true : false;
$show_staff = get_option( 'sportspress_event_show_staff', 'yes' ) === 'yes' ? true : false; $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_total = get_option( 'sportspress_event_show_total', 'yes' ) === 'yes' ? true : false;
@@ -83,155 +84,185 @@ if ( is_array( $teams ) ):
} }
$formats[ $column->post_name ] = $format; $formats[ $column->post_name ] = $format;
} }
// Prepare for offense and defense sections if ( $is_individual ) {
if ( -1 != $sections ) { // Combined table
$data = array();
// Determine order of sections foreach ( $performance as $players ) {
if ( 1 == $sections ) { foreach ( $players as $player_id => $player ) {
$section_order = array( 1 => __( 'Defense', 'sportspress' ), 0 => __( 'Offense', 'sportspress' ) ); if ( $player_id == 0 ) continue;
} else { $data[ $player_id ] = $player;
$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' );
}
// 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; sp_get_template( 'event-performance-table-combined.php', array(
break; 'scrollable' => $scrollable,
case 0: 'sortable' => $sortable,
$labels[0][ $column->post_name ] = $column->post_title; 'show_players' => $show_players,
break; 'show_numbers' => $show_numbers,
default: 'show_total' => $show_total,
$labels[0][ $column->post_name ] = $column->post_title; 'caption' => __( 'Scorecard', 'sportspress' ),
$labels[1][ $column->post_name ] = $column->post_title; 'labels' => $labels,
endswitch; 'formats' => $formats,
endforeach; 'mode' => $mode,
} 'data' => $data,
'event' => $event,
'link_posts' => $link_posts,
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null,
'primary' => 'primary' == $total ? $primary : null,
) );
} else {
foreach( $teams as $index => $team_id ): // Prepare for offense and defense sections
if ( -1 == $team_id ) continue; if ( -1 != $sections ) {
// Determine order of sections
if ( 1 == $sections ) {
$section_order = array( 1 => __( 'Defense', 'sportspress' ), 0 => __( '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' );
}
// Get results for players in the team // Get labels by section
$players = sp_array_between( (array)get_post_meta( $id, 'sp_player', false ), 0, $index ); foreach ( $columns as $column ):
$has_players = sizeof( $players ) > 1; $section = get_post_meta( $column->ID, 'sp_section', true );
if ( '' === $section ) {
$players = apply_filters( 'sportspress_event_performance_split_team_players', $players ); $section = -1;
$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();
$offense = (array)get_post_meta( $id, 'sp_offense', false );
$defense = (array)get_post_meta( $id, 'sp_defense', false );
if ( sizeof( $offense ) || sizeof( $defense ) ) {
// Get results for offensive players in the team
$offense = sp_array_between( $offense, 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( $defense, 0, $index );
$data[1] = sp_array_combine( $defense, sp_array_value( $performance, $team_id, array() ) );
} else {
// Get results for all players in the team
$players = sp_array_between( (array)get_post_meta( $id, 'sp_player', false ), 0, $index );
$data[0] = $data[1] = sp_array_combine( $players, sp_array_value( $performance, $team_id, array() ) );
} }
switch ( $section ):
$s = 0; 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;
}
foreach( $teams as $index => $team_id ) {
if ( -1 == $team_id ) continue;
// 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 ) {
foreach ( $section_order as $section_id => $section_label ) { $data = array();
if ( sizeof( $data[ $section_id ] ) ) {
if ( 1 == $section_id ) { $offense = (array)get_post_meta( $id, 'sp_offense', false );
$order = (array)get_post_meta( $id, 'sp_order', true ); $defense = (array)get_post_meta( $id, 'sp_defense', false );
if ( is_array( $order ) && sizeof( $order ) ) {
$player_order = sp_array_value( $order, $team_id, array() ); if ( sizeof( $offense ) || sizeof( $defense ) ) {
if ( is_array( $player_order ) && sizeof( $player_order ) ) { // Get results for offensive players in the team
$data[1] = sp_array_combine( $player_order, $data[1], true ); $offense = sp_array_between( $offense, 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( $defense, 0, $index );
$data[1] = sp_array_combine( $defense, sp_array_value( $performance, $team_id, array() ) );
} else {
// Get results for all players in the team
$players = sp_array_between( (array)get_post_meta( $id, 'sp_player', false ), 0, $index );
$data[0] = $data[1] = sp_array_combine( $players, sp_array_value( $performance, $team_id, array() ) );
}
$s = 0;
foreach ( $section_order as $section_id => $section_label ) {
if ( sizeof( $data[ $section_id ] ) ) {
if ( 1 == $section_id ) {
$order = (array)get_post_meta( $id, 'sp_order', true );
if ( is_array( $order ) && sizeof( $order ) ) {
$player_order = sp_array_value( $order, $team_id, array() );
if ( is_array( $player_order ) && sizeof( $player_order ) ) {
$data[1] = sp_array_combine( $player_order, $data[1], true );
}
} }
} }
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 == $s && $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,
) );
} }
sp_get_template( 'event-performance-table.php', array( $s++;
'section' => $section_label,
'scrollable' => $scrollable,
'sortable' => $sortable,
'show_players' => $show_team_players,
'show_numbers' => $show_numbers,
'show_total' => $show_total,
'caption' => 0 == $s && $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,
) );
}
$s++;
}
} else {
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 { } else {
$data = sp_array_value( array_values( $performance ), $index ); if ( 0 < $team_id ) {
} $data = sp_array_combine( $players, sp_array_value( $performance, $team_id, array() ) );
} elseif ( 0 == $team_id ) {
sp_get_template( 'event-performance-table.php', array( $data = array();
'scrollable' => $scrollable, foreach ( $players as $player_id ) {
'sortable' => $sortable, if ( isset( $performance[ $player_id ][ $player_id ] ) ) {
'show_players' => $show_team_players, $data[ $player_id ] = $performance[ $player_id ][ $player_id ];
'show_numbers' => $show_numbers, }
'show_total' => $show_total, }
'caption' => $team_id ? get_the_title( $team_id ) : null, } else {
'labels' => $labels, $data = sp_array_value( array_values( $performance ), $index );
'formats' => $formats, }
'mode' => $mode,
'data' => $data, sp_get_template( 'event-performance-table.php', array(
'event' => $event, 'scrollable' => $scrollable,
'link_posts' => $link_posts, 'sortable' => $sortable,
'performance_ids' => isset( $performance_ids ) ? $performance_ids : null, 'show_players' => $show_team_players,
'primary' => 'primary' == $total ? $primary : null, '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 ( $show_staff ): if ( $show_staff ):
sp_get_template( 'event-staff.php', array( 'id' => $id, 'index' => $index ) ); sp_get_template( 'event-staff.php', array( 'id' => $id, 'index' => $index ) );
endif; endif;
?> ?>
<?php <?php
endforeach; }
do_action( 'sportspress_event_performance' ); do_action( 'sportspress_event_performance' );
?> ?>