Combine event performance table more elegantly

This commit is contained in:
Brian Miyaji
2015-09-18 11:15:37 +10:00
parent 62ddcf0ab0
commit fec6816ead
3 changed files with 187 additions and 129 deletions

View File

@@ -28,43 +28,42 @@ class SP_Meta_Box_Event_Performance {
else
$split_positions = false;
// Determine if we are splitting teams
if ( 'yes' == get_option( 'sportspress_event_split_players_by_team', 'yes' ) )
$split_teams = true;
else
$split_teams = false;
// Determine if columns are auto or manual
if ( 'manual' == get_option( 'sportspress_event_performance_columns', 'auto' ) )
$manual = true;
else
$manual = false;
$i = 0;
// Determine if we need checkboxes
if ( $manual && $i == 0 )
$has_checkboxes = true;
else
$has_checkboxes = false;
foreach ( $teams as $key => $team_id ):
if ( -1 == $team_id ) continue;
// Get positions
$positions = array();
if ( taxonomy_exists( 'sp_position' ) ):
$args = array(
'hide_empty' => false,
'parent' => 0,
'include_children' => true,
);
$positions = get_terms( 'sp_position', $args );
endif;
// 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() ) );
// Get status option
if ( 'yes' == get_option( 'sportspress_event_show_status', 'yes' ) )
$status = true;
else
$status = false;
// Determine if we need checkboxes
if ( $manual && $i == 0 )
$has_checkboxes = true;
else
$has_checkboxes = false;
?>
<?php if ( get_option( 'sportspress_event_split_players_by_team' ) ) { ?>
<div>
<?php if ( $team_id ): ?>
<p><strong><?php echo get_the_title( $team_id ); ?></strong></p>
<?php elseif ( $i ): ?>
<br>
<?php endif; ?>
<?php self::table( $labels, $columns, $data, $team_id, $has_checkboxes, $split_positions ); ?>
</div>
<?php } else { ?>
<?php self::table( $labels, $columns, $data, $team_id, $has_checkboxes, $split_positions ); ?>
<?php } ?>
<?php
$i ++;
endforeach;
self::tables( $post->ID, $stats, $labels, $columns, $teams, $has_checkboxes, $split_positions, $split_teams, $positions, $status );
}
/**
@@ -75,77 +74,73 @@ class SP_Meta_Box_Event_Performance {
update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) );
}
/**
* Admin edit tables
*/
public static function tables( $post_id, $stats = array(), $labels = array(), $columns = array(), $teams = array(), $has_checkboxes = false, $split_positions = false, $split_teams = true, $positions = array(), $status = true ) {
$i = 0;
if ( $split_teams ) {
foreach ( $teams as $key => $team_id ):
if ( -1 == $team_id ) continue;
// 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>
<?php if ( $team_id ): ?>
<p><strong><?php echo get_the_title( $team_id ); ?></strong></p>
<?php elseif ( $i ): ?>
<br>
<?php endif; ?>
<?php self::table( $labels, $columns, $data, $team_id, $has_checkboxes, $split_positions, $positions, $status ); ?>
</div>
<?php
$i ++;
endforeach;
} else {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table sp-performance-table sp-sortable-table">
<?php self::header( $columns, $labels, $positions, $labels, $has_checkboxes, $status, false, false ); ?>
<?php self::footer( sp_array_value( $stats, -1 ), $labels, 0, $positions, $labels, $split_positions, $status, false, false ); ?>
<tbody>
<?php
foreach ( $teams as $key => $team_id ):
if ( -1 == $team_id ) continue;
// 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() ) );
foreach ( $data as $player_id => $player_performance ):
self::row( $labels, $player_id, $player_performance, $team_id, $data, ! empty( $positions ), $status, false, false );
endforeach;
endforeach;
?>
</tbody>
</table>
</div>
<?php
}
}
/**
* Admin edit table
*/
public static function table( $labels = array(), $columns = array(), $data = array(), $team_id, $has_checkboxes = false, $split_positions = false ) {
if ( taxonomy_exists( 'sp_position' ) ) {
$args = array(
'hide_empty' => false,
'parent' => 0,
'include_children' => true,
);
$positions = get_terms( 'sp_position', $args );
}
public static function table( $labels = array(), $columns = array(), $data = array(), $team_id, $has_checkboxes = false, $split_positions = false, $positions = array(), $status = true ) {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table sp-performance-table sp-sortable-table">
<thead>
<tr>
<th class="icon">&nbsp;</th>
<th>#</th>
<th><?php _e( 'Player', 'sportspress' ); ?></th>
<?php if ( ! empty( $positions ) ) { ?>
<th class="column-position">
<?php _e( 'Position', 'sportspress' ); ?>
</th>
<?php } ?>
<?php foreach ( $labels as $key => $label ): ?>
<th>
<?php if ( $has_checkboxes ): ?>
<label for="sp_columns_<?php echo $key; ?>">
<input type="checkbox" name="sp_columns[]" value="<?php echo $key; ?>" id="sp_columns_<?php echo $key; ?>" <?php checked( ! is_array( $columns ) || in_array( $key, $columns ) ); ?>>
<?php echo $label; ?>
</label>
<?php else: ?>
<?php echo $label; ?>
<?php endif; ?>
</th>
<?php endforeach; ?>
<?php if ( $team_id ): ?>
<th>
<?php _e( 'Status', 'sportspress' ); ?>
</th>
<?php else: ?>
<th>
<?php _e( 'Outcome', 'sportspress' ); ?>
</th>
<?php endif; ?>
</tr>
</thead>
<tfoot>
<?php do_action( 'sportspress_event_performance_meta_box_table_footer', $data, $labels, $team_id ); ?>
<tr class="sp-row sp-total">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php if ( ! empty( $positions ) ) { ?>
<td>&nbsp;</td>
<?php } ?>
<?php foreach( $labels as $column => $label ):
$player_id = 0;
$player_performance = sp_array_value( $data, $player_id, array() );
$value = sp_array_value( $player_performance, $column, '' );
?>
<td><input type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" placeholder="0" <?php if ( $split_positions ) { ?>readonly="readonly"<?php } else { ?>value="<?php echo $value; ?>"<?php } ?> /></td>
<?php endforeach; ?>
<td>&nbsp;</td>
</tr>
</tfoot>
<?php self::header( $columns, $labels, $positions, $labels, $has_checkboxes, $status ); ?>
<?php self::footer( $data, $labels, $team_id, $positions, $labels, $split_positions, $status ); ?>
<tbody>
<?php
foreach ( $data as $player_id => $player_performance ):
self::row( $labels, $player_id, $player_performance, $team_id, $data, ! empty( $positions ) );
self::row( $labels, $player_id, $player_performance, $team_id, $data, ! empty( $positions ), $status );
endforeach;
?>
</tbody>
@@ -154,20 +149,100 @@ class SP_Meta_Box_Event_Performance {
<?php
}
/**
* Admin edit table header
*/
public static function header( $columns = array(), $labels = array(), $positions = array(), $labels = array(), $has_checkboxes = false, $status = true, $sortable = true, $numbers = true ) {
?>
<thead>
<tr>
<?php if ( $sortable ) { ?>
<th class="icon">&nbsp;</th>
<?php } ?>
<?php if ( $numbers ) { ?>
<th>#</th>
<?php } ?>
<th><?php _e( 'Player', 'sportspress' ); ?></th>
<?php if ( ! empty( $positions ) ) { ?>
<th class="column-position">
<?php _e( 'Position', 'sportspress' ); ?>
</th>
<?php } ?>
<?php foreach ( $labels as $key => $label ): ?>
<th>
<?php if ( $has_checkboxes ): ?>
<label for="sp_columns_<?php echo $key; ?>">
<input type="checkbox" name="sp_columns[]" value="<?php echo $key; ?>" id="sp_columns_<?php echo $key; ?>" <?php checked( ! is_array( $columns ) || in_array( $key, $columns ) ); ?>>
<?php echo $label; ?>
</label>
<?php else: ?>
<?php echo $label; ?>
<?php endif; ?>
</th>
<?php endforeach; ?>
<?php if ( $status ) { ?>
<th>
<?php _e( 'Status', 'sportspress' ); ?>
</th>
<?php } ?>
</tr>
</thead>
<?php
}
/**
* Admin edit table footer
*/
public static function footer( $data = array(), $labels = array(), $team_id = 0, $positions = array(), $labels = array(), $split_positions = false, $status = true, $sortable = true, $numbers = true ) {
?>
<tfoot>
<?php do_action( 'sportspress_event_performance_meta_box_table_footer', $data, $labels, $team_id, $positions, $status, $sortable, $numbers ); ?>
<?php if ( $team_id ) { ?>
<tr class="sp-row sp-total">
<?php if ( $sortable ) { ?>
<td>&nbsp;</td>
<?php } ?>
<?php if ( $numbers ) { ?>
<td>&nbsp;</td>
<?php } ?>
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php if ( ! empty( $positions ) ) { ?>
<td>&nbsp;</td>
<?php } ?>
<?php foreach( $labels as $column => $label ):
$player_id = 0;
$player_performance = sp_array_value( $data, $player_id, array() );
$value = sp_array_value( $player_performance, $column, '' );
?>
<td><input type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" placeholder="0" <?php if ( $split_positions ) { ?>readonly="readonly"<?php } else { ?>value="<?php echo $value; ?>"<?php } ?> /></td>
<?php endforeach; ?>
<?php if ( $status ) { ?>
<td>&nbsp;</td>
<?php } ?>
</tr>
<?php } ?>
</tfoot>
<?php
}
/**
* Admin edit table row
*/
public static function row( $labels = array(), $player_id = 0, $player_performance = array(), $team_id = 0, $data = array(), $positions = true ) {
public static function row( $labels = array(), $player_id = 0, $player_performance = array(), $team_id = 0, $data = array(), $positions = true, $status = true, $sortable = true, $numbers = true ) {
if ( $player_id <= 0 ) return;
$number = get_post_meta( $player_id, 'sp_number', true );
$value = sp_array_value( $player_performance, 'number', '' );
?>
<tr class="sp-row sp-post" data-player="<?php echo $player_id; ?>">
<td class="icon"><span class="dashicons dashicons-menu post-state-format"></span></td>
<td>
<input class="small-text sp-player-number-input" type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][number]" value="<?php echo $value; ?>" />
</td>
<?php if ( $sortable ) { ?>
<td class="icon"><span class="dashicons dashicons-menu post-state-format"></span></td>
<?php } ?>
<?php if ( $numbers ) { ?>
<td>
<input class="small-text sp-player-number-input" type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][number]" value="<?php echo $value; ?>" />
</td>
<?php } ?>
<td><?php echo get_the_title( $player_id ); ?></td>
<?php if ( $positions ) { ?>
<td>
@@ -198,34 +273,12 @@ class SP_Meta_Box_Event_Performance {
<input class="sp-player-<?php echo $column; ?>-input" type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" placeholder="0" />
</td>
<?php endforeach; ?>
<?php if ( $team_id ): ?>
<?php if ( $status ) { ?>
<td class="sp-status-selector">
<?php echo self::status_select( $team_id, $player_id, sp_array_value( $player_performance, 'status', null ) ); ?>
<?php echo self::sub_select( $team_id, $player_id, sp_array_value( $player_performance, 'sub', null ), $data ); ?>
</td>
<?php else: ?>
<td>
<?php
$values = sp_array_value( $player_performance, 'outcome', '' );
if ( ! is_array( $values ) )
$values = array( $values );
$args = array(
'post_type' => 'sp_outcome',
'name' => 'sp_players[' . $team_id . '][' . $player_id . '][outcome][]',
'option_none_value' => '',
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'selected' => $values,
'class' => 'sp-outcome',
'property' => 'multiple',
'chosen' => true,
'placeholder' => __( 'None', 'sportspress' ),
);
sp_dropdown_pages( $args );
?>
</td>
<?php endif; ?>
<?php } ?>
</tr>
<?php
}

View File

@@ -11,8 +11,12 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Initialize totals
$totals = array();
// Set null
if ( ! isset( $position ) ) $position = null;
if ( ! isset( $class ) ) $class = null;
?>
<div class="sp-template sp-template-event-performance sp-template-event-performance-combined sp-template-event-performance-<?php echo $mode; ?>">
<div class="sp-template sp-template-event-performance sp-template-event-performance-combined sp-template-event-performance-<?php echo $mode; ?><?php if ( isset( $class ) ) { echo ' ' . $class; } ?>">
<?php if ( $caption ): ?>
<h4 class="sp-table-caption"><?php echo $caption; ?></h4>
<?php endif; ?>
@@ -113,9 +117,10 @@ $totals = array();
}
?>
</tbody>
<?php if ( $show_total ): ?>
<?php if ( ! $primary || sizeof( array_intersect_key( $totals, array_flip( (array) $primary ) ) ) ): ?>
<tfoot>
<?php if ( apply_filters( 'sportspress_event_performance_show_footer', $show_total ) ): ?>
<tfoot>
<?php do_action( 'sportspress_event_performance_table_footer', $data, $labels, $position, $performance_ids ); ?>
<?php if ( $show_total && ( ! $primary || sizeof( array_intersect_key( $totals, array_flip( (array) $primary ) ) ) ) ): ?>
<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">
<?php
if ( isset( $labels['number'] ) ):
@@ -123,7 +128,7 @@ $totals = array();
endif;
echo '<td class="data-name">' . __( 'Total', 'sportspress' ) . '</td>';
$row = sp_array_value( $data, 0, array() );
$row = sp_array_value( $data, 1, array() );
if ( $mode == 'icons' ) echo '<td class="sp-performance-icons">';
@@ -155,8 +160,8 @@ $totals = array();
if ( $mode == 'icons' ) echo '</td>';
?>
</tr>
</tfoot>
<?php endif; ?>
<?php endif; ?>
</tfoot>
<?php endif; ?>
</table>
</div>

View File

@@ -155,11 +155,11 @@ if ( ! isset( $class ) ) $class = null;
?>
</tbody>
<?php endif; ?>
<?php if ( $show_total ): ?>
<?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 );
if ( ! $primary || sizeof( array_intersect_key( $totals, array_flip( (array) $primary ) ) ) ) {
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' ); ?>">
<?php