Add format option to player performance

This commit is contained in:
Brian Miyaji
2016-02-17 01:59:26 +11:00
parent 3dfb3f9f92
commit 7c3d623f98
4 changed files with 114 additions and 91 deletions

View File

@@ -43,6 +43,8 @@ class SP_Admin_CPT_Performance extends SP_Admin_CPT {
'sp_icon' => __( 'Icon', 'sportspress' ),
'title' => __( 'Label', 'sportspress' ),
'sp_key' => __( 'Variable', 'sportspress' ),
'sp_section' => __( 'Category', 'sportspress' ),
'sp_config_format' => __( 'Format', 'sportspress' ),
'sp_description' => __( 'Description', 'sportspress' ),
);
return apply_filters( 'sportspress_performance_admin_columns', $columns );
@@ -61,6 +63,14 @@ class SP_Admin_CPT_Performance extends SP_Admin_CPT {
global $post;
echo $post->post_name;
break;
case 'sp_section':
global $post;
echo sp_get_post_section( $post->ID );
break;
case 'sp_config_format':
global $post;
echo sp_get_post_format( $post->ID );
break;
case 'sp_description':
global $post;
echo '<span class="description">' . $post->post_excerpt . '</span>';

View File

@@ -20,19 +20,7 @@ class SP_Meta_Box_Event_Performance {
*/
public static function output( $post ) {
$event = new SP_Event( $post );
list( $labels, $columns, $stats, $teams ) = $event->performance( true );
// Determine if we are splitting positions
if ( 'yes' == get_option( 'sportspress_event_split_players_by_position', 'no' ) )
$split_positions = true;
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;
list( $labels, $columns, $stats, $teams, $formats ) = $event->performance( true );
// Determine if columns are auto or manual
if ( 'manual' == get_option( 'sportspress_event_performance_columns', 'auto' ) )
@@ -48,7 +36,7 @@ class SP_Meta_Box_Event_Performance {
// Get positions
$positions = array();
if ( taxonomy_exists( 'sp_position' ) ):
if ( 'yes' == get_option( 'sportspress_event_show_position', 'yes' ) && taxonomy_exists( 'sp_position' ) ):
$args = array(
'hide_empty' => false,
'parent' => 0,
@@ -66,7 +54,7 @@ class SP_Meta_Box_Event_Performance {
// Apply filters to labels
$labels = apply_filters( 'sportspress_event_performance_labels_admin', $labels );
self::tables( $post->ID, $stats, $labels, $columns, $teams, $has_checkboxes, $split_positions, $split_teams, $positions, $status );
self::tables( $post->ID, $stats, $labels, $columns, $teams, $has_checkboxes, $positions, $status, $formats );
}
/**
@@ -80,95 +68,96 @@ class SP_Meta_Box_Event_Performance {
/**
* 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 ) {
public static function tables( $post_id, $stats = array(), $labels = array(), $columns = array(), $teams = array(), $has_checkboxes = false, $positions = array(), $status = true, $formats = array() ) {
$sections = get_option( 'sportspress_event_performance_sections', -1 );
$i = 0;
if ( $split_teams ) {
foreach ( $teams as $key => $team_id ):
if ( -1 == $team_id ) continue;
foreach ( $teams as $key => $team_id ):
if ( -1 == $team_id ) continue;
if ( -1 == $sections ) {
// 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() ) );
$tabs = array();
if ( $team_id ):
$tabs['values'] = get_the_title( $team_id );
elseif ( $i ):
echo '<br>';
endif;
$tabs = apply_filters( 'sportspress_event_performance_tabs_admin', $tabs );
?>
<div>
<?php if ( sizeof( $tabs ) ): ?>
<?php if ( sizeof( $tabs ) > 1 ): ?>
<ul class="subsubsub sp-performance-table-bar">
<?php $t = 0; ?>
<?php foreach ( $tabs as $key => $label ): ?>
<li>
<a href="#"<?php if ( 0 == $t ): ?> class="current"<?php endif; ?>>
<?php echo $label; ?>
</a>
</li>
<?php if ( sizeof( $tabs ) > $t + 1 ): ?> | <?php endif; ?>
<?php $t++; ?>
<?php endforeach; ?>
</ul>
<?php else: ?>
<?php $label = reset( $tabs ); ?>
<p><strong><?php echo $label; ?></strong></p>
<?php endif; ?>
<?php endif; ?>
<?php self::table( $labels, $columns, $data, $team_id, $has_checkboxes, $split_positions, $positions, $status ); ?>
<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 ); ?>
<?php do_action( 'sportspress_after_event_performance_table_admin', $labels, $columns, $data, $team_id ); ?>
</div>
<?php } else { ?>
<?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, $has_checkboxes, $status, false, false ); ?>
<?php self::footer( sp_array_value( $stats, -1 ), $labels, 0, $positions, $split_positions, $status, false, false ); ?>
<tbody>
<?php
foreach ( $teams as $key => $team_id ):
if ( -1 == $team_id ) continue;
// Get labels by section
$args = array(
'post_type' => 'sp_performance',
'numberposts' => 100,
'posts_per_page' => 100,
'orderby' => 'menu_order',
'order' => 'ASC',
);
// 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() ) );
$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 );
endforeach;
endforeach;
?>
</tbody>
</table>
</div>
$offense_labels = $defense_labels = array();
foreach ( $columns as $column ):
$section = get_post_meta( $column->ID, 'sp_section', true );
if ( '' === $section ) {
$section = -1;
}
switch ( $section ):
case 1:
$defense_labels[ $column->post_name ] = $column->post_title;
break;
case 0:
$offense_labels[ $column->post_name ] = $column->post_title;
break;
default:
$defense_labels[ $column->post_name ] = $column->post_title;
$offense_labels[ $column->post_name ] = $column->post_title;
endswitch;
endforeach;
// Get results for offensive players in the team
$offense = sp_array_between( (array)get_post_meta( $post_id, 'sp_offense', false ), 0, $key );
$offense[] = -1;
$offense_data = sp_array_combine( $offense, sp_array_value( $stats, $team_id, array() ) );
// Get results for defensive players in the team
$defense = sp_array_between( (array)get_post_meta( $post_id, 'sp_defense', false ), 0, $key );
$defense[] = -1;
$defense_data = sp_array_combine( $defense, sp_array_value( $stats, $team_id, array() ) );
?>
<div>
<p><strong><?php echo get_the_title( $team_id ); ?> &mdash; <?php _e( 'Offense', 'sportspress' ); ?></strong></p>
<?php self::table( $offense_labels, $columns, $offense_data, $team_id, $has_checkboxes, $positions, $status, 0, $formats ); ?>
<?php do_action( 'sportspress_after_event_performance_table_admin', $offense_labels, $columns, $offense_data, $team_id ); ?>
</div>
<div>
<p><strong><?php echo get_the_title( $team_id ); ?> &mdash; <?php _e( 'Defense', 'sportspress' ); ?></strong></p>
<?php self::table( $defense_labels, $columns, $defense_data, $team_id, $has_checkboxes, $positions, $status, 1, $formats ); ?>
<?php do_action( 'sportspress_after_event_performance_table_admin', $defense_labels, $columns, $defense_data, $team_id ); ?>
</div>
<?php
}
}
$i ++;
endforeach;
}
/**
* Admin edit table
*/
public static function table( $labels = array(), $columns = array(), $data = array(), $team_id, $has_checkboxes = false, $split_positions = false, $positions = array(), $status = true ) {
public static function table( $labels = array(), $columns = array(), $data = array(), $team_id, $has_checkboxes = false, $positions = array(), $status = true, $section = -1, $formats = array() ) {
?>
<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 ); ?>
<?php self::footer( $data, $labels, $team_id, $positions, $split_positions, $status ); ?>
<?php self::header( $columns, $labels, $positions, $has_checkboxes, $status, true, true, $section, $formats ); ?>
<?php self::footer( $data, $labels, $team_id, $positions, $status, true, true, $section, $formats ); ?>
<tbody>
<?php
foreach ( $data as $player_id => $player_performance ):
self::row( $labels, $player_id, $player_performance, $team_id, $data, ! empty( $positions ), $status );
self::row( $labels, $player_id, $player_performance, $team_id, $data, ! empty( $positions ), $status, true, true, $section, $formats );
endforeach;
?>
</tbody>
@@ -180,7 +169,7 @@ class SP_Meta_Box_Event_Performance {
/**
* Admin edit table header
*/
public static function header( $columns = array(), $labels = array(), $positions = array(), $has_checkboxes = false, $status = true, $sortable = true, $numbers = true ) {
public static function header( $columns = array(), $labels = array(), $positions = array(), $has_checkboxes = false, $status = true, $sortable = true, $numbers = true, $section = -1, $formats = array() ) {
?>
<thead>
<tr>
@@ -221,10 +210,10 @@ class SP_Meta_Box_Event_Performance {
/**
* Admin edit table footer
*/
public static function footer( $data = array(), $labels = array(), $team_id = 0, $positions = array(), $split_positions = false, $status = true, $sortable = true, $numbers = true ) {
public static function footer( $data = array(), $labels = array(), $team_id = 0, $positions = array(), $status = true, $sortable = true, $numbers = true, $section = -1, $formats = array() ) {
?>
<tfoot>
<?php do_action( 'sportspress_event_performance_meta_box_table_footer', $data, $labels, $team_id, $positions, $status, $sortable, $numbers ); ?>
<?php do_action( 'sportspress_event_performance_meta_box_table_footer', $data, $labels, $team_id, $positions, $status, $sortable, $numbers, $section ); ?>
<?php if ( $team_id ) { ?>
<tr class="sp-row sp-total">
<?php if ( $sortable ) { ?>
@@ -242,7 +231,7 @@ class SP_Meta_Box_Event_Performance {
$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 esc_attr( $value ); ?>"<?php } ?> /></td>
<td><input type="text" name="sp_players[<?php echo $team_id; ?>][<?php echo $player_id; ?>][<?php echo $column; ?>]" placeholder="0" value="<?php echo esc_attr( $value ); ?>" /></td>
<?php endforeach; ?>
<?php if ( $status ) { ?>
<td>&nbsp;</td>
@@ -256,7 +245,7 @@ class SP_Meta_Box_Event_Performance {
/**
* Admin edit table row
*/
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 ) {
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, $section = -1, $formats = array() ) {
if ( $player_id <= 0 ) return;
$number = get_post_meta( $player_id, 'sp_number', true );
@@ -296,9 +285,10 @@ class SP_Meta_Box_Event_Performance {
<?php } ?>
<?php foreach( $labels as $column => $label ):
$value = sp_array_value( $player_performance, $column, '' );
$placeholder = sp_get_format_placeholder( sp_array_value( $formats, $column, 'number' ) );
?>
<td>
<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 esc_attr( $value ); ?>" placeholder="0" />
<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 esc_attr( $value ); ?>" placeholder="<?php echo $placeholder; ?>" />
</td>
<?php endforeach; ?>
<?php if ( $status ) { ?>

View File

@@ -162,7 +162,8 @@
<th class="icon" scope="col"><?php _e( 'Icon', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Label', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Variable', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Position', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Category', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Format', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Description', 'sportspress' ); ?></th>
<th scope="col" class="edit"></th>
</tr>
@@ -171,7 +172,7 @@
<tr>
<th class="radio"><input type="radio" class="sp-primary-performance-option" id="sportspress_primary_performance_0" name="sportspress_primary_performance" value="0" <?php checked( $selection, 0 ); ?>></th>
<th class="icon">&nbsp;</td>
<th colspan="5"><label for="sportspress_primary_performance_0">
<th colspan="6"><label for="sportspress_primary_performance_0">
<?php
if ( sizeof( $data ) > 0 ):
$default = reset( $data );
@@ -198,13 +199,14 @@
</td>
<td class="row-title"><?php echo $row->post_title; ?></td>
<td><code><?php echo $row->post_name; ?></code></td>
<td><?php echo get_the_terms ( $row->ID, 'sp_position' ) ? the_terms( $row->ID, 'sp_position' ) : __( 'All', 'sportspress' );; ?></td>
<td><?php echo sp_get_post_section( $row->ID ); ?></td>
<td><?php echo sp_get_post_format( $row->ID ); ?></td>
<td><p class="description"><?php echo $row->post_excerpt; ?></p></td>
<td class="edit"><a class="button" href="<?php echo get_edit_post_link( $row->ID ); ?>"><?php _e( 'Edit', 'sportspress' ); ?></s></td>
</tr>
<?php $i++; endforeach; else: ?>
<tr class="alternate">
<td colspan="7"><?php _e( 'No results found.', 'sportspress' ); ?></td>
<td colspan="8"><?php _e( 'No results found.', 'sportspress' ); ?></td>
</tr>
<?php endif; ?>
</table>

View File

@@ -75,7 +75,28 @@ class SP_Event extends SP_Custom_Post{
public function performance( $admin = false ) {
$teams = get_post_meta( $this->ID, 'sp_team', false );
$performance = (array)get_post_meta( $this->ID, 'sp_players', true );
$labels = apply_filters( 'sportspress_event_performance_labels', sp_get_var_labels( 'sp_performance' ), $this );
$args = array(
'post_type' => 'sp_performance',
'numberposts' => 100,
'posts_per_page' => 100,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$vars = get_posts( $args );
$labels = array();
foreach ( $vars as $var ) {
$labels[ $var->post_name ] = $var->post_title;
$format = get_post_meta( $var->ID, 'sp_format', true );
if ( '' === $format ) {
$format = 'number';
}
$formats[ $var->post_name ] = $format;
}
$labels = apply_filters( 'sportspress_event_performance_labels', $labels, $this );
$columns = get_post_meta( $this->ID, 'sp_columns', true );
if ( is_array( $teams ) ):
foreach( $teams as $i => $team_id ):
@@ -111,7 +132,7 @@ class SP_Event extends SP_Custom_Post{
endif;
if ( $admin ):
return array( $labels, $columns, $performance, $teams );
return array( $labels, $columns, $performance, $teams, $formats );
else:
// Add position to performance labels
if ( taxonomy_exists( 'sp_position' ) ):